libopkg: flag package with SF_NEED_DETAIL in pkg_init_from_file()
[oweals/opkg-lede.git] / libopkg / file_util.c
index 4f949acf730a4785899e3f0b40e2055c009903f2..b1d793023154ff5f7da0f9ef76efc6601454b718 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/stat.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <ctype.h>
 
 #include "sprintf_alloc.h"
 #include "file_util.h"
@@ -228,6 +229,64 @@ char *file_sha256sum_alloc(const char *file_name)
 
 #endif
 
+char *checksum_bin2hex(const char *src, size_t len)
+{
+       unsigned char *p;
+       static unsigned char buf[65];
+       const unsigned char *s = (unsigned char *)src;
+       static const unsigned char bin2hex[16] = {
+               '0', '1', '2', '3',
+               '4', '5', '6', '7',
+               '8', '9', 'a', 'b',
+               'c', 'd', 'e', 'f'
+       };
+
+       if (!s || len > 32)
+               return NULL;
+
+       for (p = buf; len > 0; s++, len--) {
+               *p++ = bin2hex[*s / 16];
+               *p++ = bin2hex[*s % 16];
+       }
+
+       *p = 0;
+
+       return (char *)buf;
+}
+
+char *checksum_hex2bin(const char *src, size_t *len)
+{
+       size_t slen;
+       unsigned char *p;
+       const unsigned char *s = (unsigned char *)src;
+       static unsigned char buf[32];
+
+       if (!src) {
+               *len = 0;
+               return NULL;
+       }
+
+       while (isspace(*src))
+               src++;
+
+       slen = strlen(src);
+
+       if (slen > 64) {
+               *len = 0;
+               return NULL;
+       }
+
+#define hex(c) \
+       (c >= 'a' ? (c - 'a') : (c >= 'A' ? (c - 'A') : (c - '0')))
+
+       for (p = buf, *len = 0;
+            slen > 0 && isxdigit(s[0]) && isxdigit(s[1]);
+            slen--, s += 2, (*len)++)
+               *p++ = hex(s[0]) * 16 + hex(s[1]);
+
+       return (char *)buf;
+}
+
 int rm_r(const char *path)
 {
        int ret = 0;