X-Git-Url: https://git.librecmc.org/?p=oweals%2Fopkg-lede.git;a=blobdiff_plain;f=libopkg%2Ffile_util.c;h=155d73b52be1ac81d88ebfd851c50c98ede6f012;hp=4f949acf730a4785899e3f0b40e2055c009903f2;hb=54cc7e3bd1f79569022aa9fc3d0e748c81e3bcd8;hpb=4ea955bc7e5575bc1d8b34c364591c47653f2cfd diff --git a/libopkg/file_util.c b/libopkg/file_util.c index 4f949ac..155d73b 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "sprintf_alloc.h" #include "file_util.h" @@ -228,6 +229,59 @@ 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 (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]; + + 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;