]> git.ipfire.org Git - pakfire.git/commitdiff
util: Move hex function into a separate file
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 12:47:22 +0000 (12:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 12:47:22 +0000 (12:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/archive.c
src/libpakfire/digest.c
src/libpakfire/hex.c [new file with mode: 0644]
src/libpakfire/include/pakfire/hex.h [new file with mode: 0644]
src/libpakfire/include/pakfire/util.h
src/libpakfire/package.c
src/libpakfire/util.c
src/libpakfire/xfer.c

index 6dc88ba252cae4d14e6db74bc7688e3a443d6a59..ece8a3c48bde7da57b437312415ccb799332fd42 100644 (file)
@@ -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 \
index 8fd1ea3ef557ae38b6bf646e6c244047fd00c5d2..489c7f63941a9a64c69ca96905f16c12e20e7b9b 100644 (file)
@@ -40,6 +40,7 @@
 #include <pakfire/digest.h>
 #include <pakfire/file.h>
 #include <pakfire/filelist.h>
+#include <pakfire/hex.h>
 #include <pakfire/i18n.h>
 #include <pakfire/jail.h>
 #include <pakfire/linter.h>
index f31dfae851af0f9316f5efdb2c740b50ad780f14..c426328448f59d09bcb1d553891ace56390c355f 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <pakfire/ctx.h>
 #include <pakfire/digest.h>
+#include <pakfire/hex.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
diff --git a/src/libpakfire/hex.c b/src/libpakfire/hex.c
new file mode 100644 (file)
index 0000000..aaa36c0
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <pakfire/hex.h>
+
+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 (file)
index 0000000..4caa929
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */
index 2be0756cb8a810ad9cffff420e6d41b83586c594..e30675ba432b9f174b91ec94e6f4dae7c0af1781 100644 (file)
@@ -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
index 6268f24e38a4412d104ff175594760f5213f4630..09f40002c2ffb9febf2fa4c14a5e4b41b1cd5272 100644 (file)
@@ -39,6 +39,7 @@
 #include <pakfire/digest.h>
 #include <pakfire/file.h>
 #include <pakfire/filelist.h>
+#include <pakfire/hex.h>
 #include <pakfire/i18n.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
index 5f49a5acd9a5a7224bf869c88a415a4c3be2330f..2d9dbb50bf09c159d4f7c20cc2109a9819567329 100644 (file)
@@ -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;
 
index a9200b9347f7b0a42af97c7f5d068fa2f307fe3a..f05c48cf0bfd9f2dfe61736589c36449d53f7009 100644 (file)
@@ -33,6 +33,7 @@
 #include <systemd/sd-event.h>
 
 #include <pakfire/ctx.h>
+#include <pakfire/hex.h>
 #include <pakfire/mirrorlist.h>
 #include <pakfire/path.h>
 #include <pakfire/progress.h>