Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalizing sha256-functions #586

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ xbps-fetch:
- configurable libfetch timeout
- configurable number of connection retries

xbps-digest:
- blake2b support

Issues listed at https://github.com/void-linux/xbps/issues
2 changes: 1 addition & 1 deletion bin/xbps-create/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
xe->type = ENTRY_TYPE_FILES;
}

if (!xbps_file_sha256(xe->sha256, sizeof sha256, fpath))
if (!xbps_file_hash(XBPS_HASH_SHA256, xe->sha256, sizeof sha256, fpath))
die("failed to process hash for: %s", fpath);
xbps_dictionary_set_cstring(fileinfo, "sha256", xe->sha256);

Expand Down
38 changes: 27 additions & 11 deletions bin/xbps-digest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ usage(bool fail)
"\n"
"OPTIONS\n"
" -h, --help Show usage\n"
" -m, --mode <sha256> Selects the digest mode, sha256 (default)\n"
" -m, --mode <mode> Selects the digest mode, sha256 (default), blake2b\n"
" -V, --version Show XBPS version\n"
"\nNOTES\n"
" If [file] not set, reads from stdin\n");
Expand All @@ -52,8 +52,10 @@ int
main(int argc, char **argv)
{
int c;
char sha256[XBPS_SHA256_SIZE];
const char *mode = NULL, *progname = argv[0];
char* digest;
const char *modestr = NULL, *progname = argv[0];
xbps_hash_algorithm_t mode = XBPS_HASH_SHA256;
int digest_size;
const struct option longopts[] = {
{ "mode", required_argument, NULL, 'm' },
{ "help", no_argument, NULL, 'h' },
Expand All @@ -67,7 +69,7 @@ main(int argc, char **argv)
usage(false);
/* NOTREACHED */
case 'm':
mode = optarg;
modestr = optarg;
break;
case 'V':
printf("%s\n", XBPS_RELVER);
Expand All @@ -82,27 +84,41 @@ main(int argc, char **argv)
argc -= optind;
argv += optind;

if (mode && strcmp(mode, "sha256")) {
if (modestr) {
if (strcmp(modestr, "sha256") == 0) {
mode = XBPS_HASH_SHA256;
} else if (strcmp(modestr, "blake2b") == 0) {
mode = XBPS_HASH_BLAKE2B256;
} else {
/* sha256 is the only supported mode currently */
xbps_error_printf("%s: unsupported digest mode\n", progname);
exit(EXIT_FAILURE);
}
}

digest_size = xbps_hash_size(mode);
if ((digest = malloc(digest_size)) == NULL) {
/* sha256 is the only supported mode currently */
xbps_error_printf("%s: unsupported digest mode\n", progname);
xbps_error_printf("%s: unable to allocate digest: %s\n", progname, strerror(errno));
exit(EXIT_FAILURE);
}

if (argc < 1) {
if (!xbps_file_sha256(sha256, sizeof sha256, "/dev/stdin"))
if (!xbps_file_hash(mode, digest, digest_size, "/dev/stdin"))
exit(EXIT_FAILURE);

printf("%s\n", sha256);
} else {
for (int i = 0; i < argc; i++) {
if (!xbps_file_sha256(sha256, sizeof sha256, argv[i])) {
if (!xbps_file_hash(mode, digest, digest_size, argv[i])) {
xbps_error_printf(
"%s: couldn't get hash for %s (%s)\n",
progname, argv[i], strerror(errno));
exit(EXIT_FAILURE);
}
printf("%s\n", sha256);
}
}

printf("%s\n", digest);

free(digest);
exit(EXIT_SUCCESS);
}
2 changes: 1 addition & 1 deletion bin/xbps-fetch/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ main(int argc, char **argv)
} else if (rv == 0) {
xbps_warn_printf("%s: %s: file is identical with remote.\n", progname, argv[i]);
if (shasum) {
if (!xbps_file_sha256_raw(digest, sizeof digest, filename)) {
if (!xbps_file_hash_raw(XBPS_HASH_SHA256, digest, sizeof digest, filename)) {
xbps_error_printf("%s: failed to hash: %s: %s\n",
progname, filename, strerror(rv));
failure = true;
Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-pkgdb/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ check_pkg_integrity(struct xbps_handle *xhp,
free(buf);
return -ENOENT;
}
rv = xbps_file_sha256_check(buf, sha256);
rv = xbps_file_hash_check(XBPS_HASH_SHA256, buf, sha256);
free(buf);
if (rv == ENOENT) {
xbps_dictionary_remove(opkgd, "metafile-sha256");
Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-pkgdb/check_pkg_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ check_pkg_files(struct xbps_handle *xhp, const char *pkgname, void *arg)
path = xbps_xasprintf("%s/%s", xhp->rootdir, file);
xbps_dictionary_get_cstring_nocopy(obj,
"sha256", &sha256);
rv = xbps_file_sha256_check(path, sha256);
rv = xbps_file_hash_check(XBPS_HASH_SHA256, path, sha256);
switch (rv) {
case 0:
free(path);
Expand Down
112 changes: 53 additions & 59 deletions bin/xbps-query/ownedby.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "defs.h"

#include <assert.h>
#include <dirent.h>
#include <errno.h>
Expand All @@ -33,26 +35,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <xbps.h>
#include "defs.h"

struct ffdata {
bool rematch;
const char *pat, *repouri;
regex_t regex;
xbps_array_t allkeys;
bool rematch;
const char * pat, *repouri;
regex_t regex;
xbps_array_t allkeys;
xbps_dictionary_t filesd;
};

static void
match_files_by_pattern(xbps_dictionary_t pkg_filesd,
xbps_dictionary_keysym_t key,
struct ffdata *ffd,
const char *pkgver)
{
match_files_by_pattern(xbps_dictionary_t pkg_filesd,
xbps_dictionary_keysym_t key,
struct ffdata* ffd,
const char* pkgver) {
xbps_array_t array;
const char *keyname = NULL, *typestr = NULL;
const char * keyname = NULL, *typestr = NULL;

keyname = xbps_dictionary_keysym_cstring_nocopy(key);

Expand All @@ -68,7 +67,7 @@ match_files_by_pattern(xbps_dictionary_t pkg_filesd,
array = xbps_dictionary_get_keysym(pkg_filesd, key);
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
xbps_object_t obj;
const char *filestr = NULL, *tgt = NULL;
const char * filestr = NULL, *tgt = NULL;

obj = xbps_array_get(array, i);
xbps_dictionary_get_cstring_nocopy(obj, "file", &filestr);
Expand All @@ -78,37 +77,36 @@ match_files_by_pattern(xbps_dictionary_t pkg_filesd,
if (ffd->rematch) {
if (regexec(&ffd->regex, filestr, 0, 0, 0) == 0) {
printf("%s: %s%s%s (%s)\n",
pkgver, filestr,
tgt ? " -> " : "",
tgt ? tgt : "",
typestr);
pkgver, filestr,
tgt ? " -> " : "",
tgt ? tgt : "",
typestr);
}
} else {
if ((fnmatch(ffd->pat, filestr, FNM_PERIOD)) == 0) {
printf("%s: %s%s%s (%s)\n",
pkgver, filestr,
tgt ? " -> " : "",
tgt ? tgt : "",
typestr);
pkgver, filestr,
tgt ? " -> " : "",
tgt ? tgt : "",
typestr);
}
}
}
}

static int
ownedby_pkgdb_cb(struct xbps_handle *xhp,
xbps_object_t obj,
const char *obj_key UNUSED,
void *arg,
bool *done UNUSED)
{
ownedby_pkgdb_cb(struct xbps_handle* xhp,
xbps_object_t obj,
const char* obj_key UNUSED,
void* arg,
bool* done UNUSED) {
xbps_dictionary_t pkgmetad;
xbps_array_t files_keys;
struct ffdata *ffd = arg;
const char *pkgver = NULL;
xbps_array_t files_keys;
struct ffdata* ffd = arg;
const char* pkgver = NULL;

(void)obj_key;
(void)done;
(void) obj_key;
(void) done;

xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
pkgmetad = xbps_pkgdb_get_pkg_files(xhp, pkgver);
Expand All @@ -118,7 +116,7 @@ ownedby_pkgdb_cb(struct xbps_handle *xhp,
files_keys = xbps_dictionary_all_keys(pkgmetad);
for (unsigned int i = 0; i < xbps_array_count(files_keys); i++) {
match_files_by_pattern(pkgmetad,
xbps_array_get(files_keys, i), ffd, pkgver);
xbps_array_get(files_keys, i), ffd, pkgver);
}
xbps_object_release(pkgmetad);
xbps_object_release(files_keys);
Expand All @@ -128,18 +126,17 @@ ownedby_pkgdb_cb(struct xbps_handle *xhp,


static int
repo_match_cb(struct xbps_handle *xhp,
xbps_object_t obj,
const char *key UNUSED,
void *arg,
bool *done UNUSED)
{
char bfile[PATH_MAX];
repo_match_cb(struct xbps_handle* xhp,
xbps_object_t obj,
const char* key UNUSED,
void* arg,
bool* done UNUSED) {
char bfile[PATH_MAX];
xbps_dictionary_t filesd;
xbps_array_t files_keys;
struct ffdata *ffd = arg;
const char *pkgver = NULL;
int r;
xbps_array_t files_keys;
struct ffdata* ffd = arg;
const char* pkgver = NULL;
int r;

xbps_dictionary_set_cstring_nocopy(obj, "repository", ffd->repouri);
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
Expand All @@ -152,13 +149,13 @@ repo_match_cb(struct xbps_handle *xhp,
filesd = xbps_archive_fetch_plist(bfile, "/files.plist");
if (!filesd) {
xbps_error_printf("%s: couldn't fetch files.plist from %s: %s\n",
pkgver, bfile, strerror(errno));
pkgver, bfile, strerror(errno));
return EINVAL;
}
files_keys = xbps_dictionary_all_keys(filesd);
for (unsigned int i = 0; i < xbps_array_count(files_keys); i++) {
match_files_by_pattern(filesd,
xbps_array_get(files_keys, i), ffd, pkgver);
xbps_array_get(files_keys, i), ffd, pkgver);
}
xbps_object_release(files_keys);
xbps_object_release(filesd);
Expand All @@ -167,32 +164,29 @@ repo_match_cb(struct xbps_handle *xhp,
}

static int
repo_ownedby_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
{
xbps_array_t allkeys;
struct ffdata *ffd = arg;
int rv;
repo_ownedby_cb(struct xbps_repo* repo, void* arg, bool* done UNUSED) {
xbps_array_t allkeys;
struct ffdata* ffd = arg;
int rv;

ffd->repouri = repo->uri;
allkeys = xbps_dictionary_all_keys(repo->idx);
rv = xbps_array_foreach_cb_multi(repo->xhp, allkeys, repo->idx, repo_match_cb, ffd);
allkeys = xbps_dictionary_all_keys(repo->idx);
rv = xbps_array_foreach_cb_multi(repo->xhp, allkeys, repo->idx, repo_match_cb, ffd);
xbps_object_release(allkeys);

return rv;
}

int
ownedby(struct xbps_handle *xhp, const char *pat, bool repo, bool regex)
{
int ownedby(struct xbps_handle* xhp, const char* pat, bool repo, bool regex) {
struct ffdata ffd;
int rv;
int rv;

ffd.rematch = false;
ffd.pat = pat;
ffd.pat = pat;

if (regex) {
ffd.rematch = true;
if (regcomp(&ffd.regex, ffd.pat, REG_EXTENDED|REG_NOSUB|REG_ICASE) != 0)
if (regcomp(&ffd.regex, ffd.pat, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0)
return EINVAL;
}
if (repo)
Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-remove/clean-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
if (pkgd) {
xbps_dictionary_get_cstring_nocopy(pkgd,
"filename-sha256", &rsha256);
r = xbps_file_sha256_check(binpkg, rsha256);
r = xbps_file_hash_check(XBPS_HASH_SHA256, binpkg, rsha256);
if (r == 0) {
/* hash matched */
return 0;
Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-rindex/index-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
* - filename-size
* - filename-sha256
*/
if (!xbps_file_sha256(sha256, sizeof sha256, pkg)) {
if (!xbps_file_hash(XBPS_HASH_SHA256, sha256, sizeof sha256, pkg)) {
xbps_object_release(binpkgd);
free(pkgver);
rv = EINVAL;
Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-rindex/index-clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ idx_cleaner_cb(struct xbps_handle *xhp UNUSED,
*/
xbps_dictionary_get_cstring_nocopy(obj,
"filename-sha256", &sha256);
if (xbps_file_sha256_check(filen, sha256) != 0) {
if (xbps_file_hash_check(XBPS_HASH_SHA256, filen, sha256) != 0) {
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
goto out;
xbps_dictionary_remove(dest, pkgname);
Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-rindex/sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ rsa_sign_file(RSA *rsa, const char *file,
{
unsigned char digest[XBPS_SHA256_DIGEST_SIZE];

if (!xbps_file_sha256_raw(digest, sizeof digest, file))
if (!xbps_file_hash_raw(XBPS_HASH_SHA256, digest, sizeof digest, file))
return false;

if ((*sigret = calloc(1, RSA_size(rsa) + 1)) == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-uhelper/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ main(int argc, char **argv)
usage();

for (i = 1; i < argc; i++) {
if (!xbps_file_sha256(sha256, sizeof sha256, argv[i])) {
if (!xbps_file_hash(XBPS_HASH_SHA256, sha256, sizeof sha256, argv[i])) {
xbps_error_printf(
"couldn't get hash for %s (%s)\n",
argv[i], strerror(errno));
Expand Down
Loading