From e13ad2cc01deb8724c7f3cd2a4480d27493e0788 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 26 Oct 2024 12:47:22 +0000 Subject: [PATCH] util: Move hex function into a separate file Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/libpakfire/archive.c | 1 + src/libpakfire/digest.c | 1 + src/libpakfire/hex.c | 84 +++++++++++++++++++++++++++ src/libpakfire/include/pakfire/hex.h | 35 +++++++++++ src/libpakfire/include/pakfire/util.h | 5 -- src/libpakfire/package.c | 1 + src/libpakfire/util.c | 63 -------------------- src/libpakfire/xfer.c | 1 + 9 files changed, 125 insertions(+), 68 deletions(-) create mode 100644 src/libpakfire/hex.c create mode 100644 src/libpakfire/include/pakfire/hex.h diff --git a/Makefile.am b/Makefile.am index 6dc88ba25..ece8a3c48 100644 --- a/Makefile.am +++ b/Makefile.am @@ -200,6 +200,7 @@ libpakfire_la_SOURCES = \ src/libpakfire/fhs.c \ src/libpakfire/file.c \ src/libpakfire/filelist.c \ + src/libpakfire/hex.c \ src/libpakfire/httpclient.c \ src/libpakfire/jail.c \ src/libpakfire/job.c \ @@ -252,6 +253,7 @@ pkginclude_HEADERS += \ src/libpakfire/include/pakfire/fhs.h \ src/libpakfire/include/pakfire/file.h \ src/libpakfire/include/pakfire/filelist.h \ + src/libpakfire/include/pakfire/hex.h \ src/libpakfire/include/pakfire/httpclient.h \ src/libpakfire/include/pakfire/i18n.h \ src/libpakfire/include/pakfire/jail.h \ diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index 8fd1ea3ef..489c7f639 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include diff --git a/src/libpakfire/digest.c b/src/libpakfire/digest.c index f31dfae85..c42632844 100644 --- a/src/libpakfire/digest.c +++ b/src/libpakfire/digest.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include diff --git a/src/libpakfire/hex.c b/src/libpakfire/hex.c new file mode 100644 index 000000000..aaa36c078 --- /dev/null +++ b/src/libpakfire/hex.c @@ -0,0 +1,84 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2024 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include +#include + +#include + +char* __pakfire_hexlify(const unsigned char* digest, const size_t length) { + const char* hexdigits = "0123456789abcdef"; + + char* s = malloc((length * 2) + 1); + if (!s) + return NULL; + + for (unsigned int i = 0, j = 0; i < length; i++) { + char b = digest[i]; + + s[j++] = hexdigits[(b >> 4) & 0xf]; + s[j++] = hexdigits[(b & 0x0f)]; + } + + // Terminate result + s[length * 2] = '\0'; + + return s; +} + +static int parse_nibble(char nibble) { + // Handle digits + if (nibble >= '0' && nibble <= '9') + return nibble - '0'; + + // Handle lowercase letters + if (nibble >= 'a' && nibble <= 'f') + return 10 + nibble - 'a'; + + // Handle uppercase letters + if (nibble >= 'A' && nibble <= 'F') + return 10 + nibble - 'A'; + + // Error + return -1; +} + +int __pakfire_unhexlify(unsigned char* dst, const size_t l, const char* src) { + if (!src) + return -EINVAL; + + // Determine input length + const size_t length = strlen(src); + + for (unsigned int i = 0, j = 0; i < length && j < l; i += 2, j++) { + int high = parse_nibble(src[i]); + int low = parse_nibble(src[i+1]); + + // Check for invalid input + if (high < 0 || low < 0) + return -EINVAL; + + // Store result + dst[j] = high << 4 | low; + } + + return 0; +} diff --git a/src/libpakfire/include/pakfire/hex.h b/src/libpakfire/include/pakfire/hex.h new file mode 100644 index 000000000..4caa929c2 --- /dev/null +++ b/src/libpakfire/include/pakfire/hex.h @@ -0,0 +1,35 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2024 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_HEX_H +#define PAKFIRE_HEX_H + +#ifdef PAKFIRE_PRIVATE + +#define pakfire_hexlify(digest) __pakfire_hexlify(digest, sizeof(digest)) + +char* __pakfire_hexlify(const unsigned char* digest, const size_t length); + +#define pakfire_unhexlify(dst, src) __pakfire_unhexlify(dst, sizeof(dst), src) + +int __pakfire_unhexlify(unsigned char* dst, const size_t l, const char* src); + +#endif /* PAKFIRE_PRIVATE */ +#endif /* PAKFIRE_HEX_H */ diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 2be0756cb..e30675ba4 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -50,11 +50,6 @@ static inline void* pakfire_realloc(void* p, size_t size) { int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len); -#define pakfire_hexlify(digest) __pakfire_hexlify(digest, sizeof(digest)) -char* __pakfire_hexlify(const unsigned char* digest, const size_t length); -#define pakfire_unhexlify(dst, src) __pakfire_unhexlify(dst, sizeof(dst), src) -int __pakfire_unhexlify(unsigned char* dst, const size_t l, const char* src); - const char* pakfire_path_relpath(const char* root, const char* path); // File stuff diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index 6268f24e3..09f40002c 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 5f49a5acd..2d9dbb50b 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -178,69 +178,6 @@ int pakfire_uuid_is_valid(const char* s) { return (r == 0); } -char* __pakfire_hexlify(const unsigned char* digest, const size_t length) { - const char* hexdigits = "0123456789abcdef"; - - char* s = malloc((length * 2) + 1); - if (!s) - return NULL; - - for (unsigned int i = 0, j = 0; i < length; i++) { - char b = digest[i]; - - s[j++] = hexdigits[(b >> 4) & 0xf]; - s[j++] = hexdigits[(b & 0x0f)]; - } - - // Terminate result - s[length * 2] = '\0'; - - return s; -} - -static int parse_nibble(char nibble) { - // Handle digits - if (nibble >= '0' && nibble <= '9') - return nibble - '0'; - - // Handle lowercase letters - if (nibble >= 'a' && nibble <= 'f') - return 10 + nibble - 'a'; - - // Handle uppercase letters - if (nibble >= 'A' && nibble <= 'F') - return 10 + nibble - 'A'; - - // Error - return -1; -} - -int __pakfire_unhexlify(unsigned char* dst, const size_t l, const char* src) { - if (!src) { - errno = EINVAL; - return 1; - } - - // Determine input length - const size_t length = strlen(src); - - for (unsigned int i = 0, j = 0; i < length && j < l; i += 2, j++) { - int high = parse_nibble(src[i]); - int low = parse_nibble(src[i+1]); - - // Check for invalid input - if (high < 0 || low < 0) { - errno = EINVAL; - return 1; - } - - // Store result - dst[j] = high << 4 | low; - } - - return 0; -} - int pakfire_touch(const char* path, mode_t mode) { int r = 1; diff --git a/src/libpakfire/xfer.c b/src/libpakfire/xfer.c index a9200b934..f05c48cf0 100644 --- a/src/libpakfire/xfer.c +++ b/src/libpakfire/xfer.c @@ -33,6 +33,7 @@ #include #include +#include #include #include #include -- 2.39.5