commit: 27d280152f017c9cf7918c94f09e5e1516a4f585 Author: Fabian Groffen <grobian <AT> gentoo <DOT> org> AuthorDate: Mon Aug 25 10:47:39 2025 +0000 Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org> CommitDate: Mon Aug 25 10:47:39 2025 +0000 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=27d28015
libq/hash: fix confusion around DIGEST_SIZE and DIGEST_LENGTH define LENGTH (and SIZE for BLAKE2B) so consuming code can always asume them to be there: SIZE the byte storage, LENGTH the hex-encoded storage. Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org> libq/hash.h | 10 +++++++++- libq/tree.c | 8 ++++---- qmanifest.c | 12 ++++++------ qmerge.c | 4 ++-- qpkg.c | 4 ++-- qtegrity.c | 6 ------ 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/libq/hash.h b/libq/hash.h index 85fc71d..cc9d8a9 100644 --- a/libq/hash.h +++ b/libq/hash.h @@ -1,5 +1,5 @@ /* - * Copyright 2018-2024 Gentoo Foundation + * Copyright 2018-2025 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 * * Copyright 2018- Fabian Groffen - <[email protected]> @@ -22,6 +22,14 @@ #include "sha256.h" #include "sha512.h" +#define BLAKE2B_DIGEST_SIZE BLAKE2B_OUTBYTES + +#define MD5_DIGEST_LENGTH (MD5_DIGEST_SIZE * 2) +#define SHA1_DIGEST_LENGTH (SHA1_DIGEST_SIZE * 2) +#define SHA256_DIGEST_LENGTH (SHA256_DIGEST_SIZE * 2) +#define SHA512_DIGEST_LENGTH (SHA512_DIGEST_SIZE * 2) +#define BLAKE2B_DIGEST_LENGTH (BLAKE2B_DIGEST_SIZE * 2) + enum hash_impls { HASH_MD5 = 1<<0, HASH_SHA1 = 1<<1, diff --git a/libq/tree.c b/libq/tree.c index aad6716..c2a480c 100644 --- a/libq/tree.c +++ b/libq/tree.c @@ -1140,7 +1140,7 @@ tree_read_file_binpkg(tree_pkg_ctx *pkg_ctx) * fake, but allows to transparantly use a dir of binpkgs */ if (newfd != -1) { size_t fsize; - size_t needlen = (SHA1_DIGEST_SIZE * 2) + 1 + 19 + 1; + size_t needlen = SHA1_DIGEST_LENGTH + 1 + 19 + 1; size_t pos = 0; size_t len = 0; @@ -1156,7 +1156,7 @@ tree_read_file_binpkg(tree_pkg_ctx *pkg_ctx) } m->Q_SHA1 = m->storage->ptr + pos; - m->Q_SIZE = m->Q_SHA1 + (SHA1_DIGEST_SIZE * 2) + 1; + m->Q_SIZE = m->Q_SHA1 + SHA1_DIGEST_LENGTH + 1; m->storage->pos += needlen; lseek(newfd, 0, SEEK_SET); /* reposition at the start of file */ @@ -1236,7 +1236,7 @@ tree_pkg_read(tree_pkg_ctx *pkg_ctx) * obviously when the source ebuild doesn't exist, * we never get here */ char *mdmd5; - char srcmd5[MD5_DIGEST_SIZE]; + char srcmd5[MD5_DIGEST_LENGTH + 1]; size_t flen; if (hash_multiple_file_fd(pkg_ctx->fd, @@ -1249,7 +1249,7 @@ tree_pkg_read(tree_pkg_ctx *pkg_ctx) /* is this a valid cache? use it! */ if (mdmd5 != NULL && - memcmp(mdmd5, srcmd5, MD5_DIGEST_SIZE) == 0) + memcmp(mdmd5, srcmd5, MD5_DIGEST_LENGTH) == 0) { ret = tree_pkg_read(spkg); } diff --git a/qmanifest.c b/qmanifest.c index 7ee6f3a..8064931 100644 --- a/qmanifest.c +++ b/qmanifest.c @@ -183,9 +183,9 @@ write_hashes( gzFile gm) { size_t flen = 0; - char sha256[(SHA256_DIGEST_SIZE * 2) + 1]; - char sha512[(SHA512_DIGEST_SIZE * 2) + 1]; - char blak2b[(BLAKE2B_OUTBYTES * 2) + 1]; + char sha256[SHA256_DIGEST_LENGTH + 1]; + char sha512[SHA512_DIGEST_LENGTH + 1]; + char blak2b[BLAKE2B_DIGEST_LENGTH + 1]; char data[8192]; char fname[8192]; size_t len; @@ -1034,9 +1034,9 @@ verify_file(const char *dir, char *mfline, const char *mfest, verify_msg **msgs) char *p; char buf[8192]; size_t flen = 0; - char sha256[(SHA256_DIGEST_SIZE * 2) + 1]; - char sha512[(SHA512_DIGEST_SIZE * 2) + 1]; - char blak2b[(BLAKE2B_OUTBYTES * 2) + 1]; + char sha256[SHA256_DIGEST_LENGTH + 1]; + char sha512[SHA512_DIGEST_LENGTH + 1]; + char blak2b[BLAKE2B_DIGEST_LENGTH + 1]; char ret = 0; /* mfline is a Manifest file line with type and leading path diff --git a/qmerge.c b/qmerge.c index e3f5f50..9f1aa0d 100644 --- a/qmerge.c +++ b/qmerge.c @@ -1949,8 +1949,8 @@ pkg_verify_checksums( int display) { int ret = 0; - char md5[(MD5_DIGEST_SIZE * 2) + 1]; - char sha1[(SHA1_DIGEST_SIZE * 2) + 1]; + char md5[MD5_DIGEST_LENGTH + 1]; + char sha1[SHA1_DIGEST_LENGTH + 1]; size_t flen; int mlen; diff --git a/qpkg.c b/qpkg.c index 15d4226..aa15a2b 100644 --- a/qpkg.c +++ b/qpkg.c @@ -186,8 +186,8 @@ write_hashes ) { size_t flen = 0; - char sha512[(SHA512_DIGEST_SIZE * 2) + 1]; - char blak2b[(BLAKE2B_OUTBYTES * 2) + 1]; + char sha512[SHA512_DIGEST_LENGTH + 1]; + char blak2b[BLAKE2B_DIGEST_LENGTH + 1]; char data[8192]; size_t len; const char *name; diff --git a/qtegrity.c b/qtegrity.c index f686a87..6eba1de 100644 --- a/qtegrity.c +++ b/qtegrity.c @@ -56,12 +56,6 @@ struct qtegrity_opt_state { #define FILE_EMPTY 2 #define FILE_RELATIVE 3 -#define SHA1_DIGEST_LENGTH 40 -#define SHA256_PREFIX_LENGTH 8 -#define SHA256_DIGEST_LENGTH 64 -#define SHA256_LENGTH (SHA256_PREFIX_LENGTH + SHA256_DIGEST_LENGTH) -#define SHA512_DIGEST_LENGTH 128 - static void check_sha(char *ret_digest, char *path, char *algo) {
