str_list_prev: remove unused function
[oweals/opkg-lede.git] / libopkg / file_util.c
index 912b147ad306766f6275e93a3b9860de81b29242..1f89541420afdf6092c04e098df68e0271144a92 100644 (file)
@@ -16,8 +16,6 @@
    General Public License for more details.
 */
 
-#include "config.h"
-
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #include "sprintf_alloc.h"
 #include "file_util.h"
-#ifdef HAVE_MD5
-#include "md5.h"
-#endif
+#include <libubox/md5.h>
 #include "libbb/libbb.h"
 
-#if defined HAVE_SHA256
 #include "sha256.h"
-#endif
 
 int file_exists(const char *file_name)
 {
@@ -131,7 +125,6 @@ int file_mkdir_hier(const char *path, long mode)
        return make_directory(path, mode, FILEUTILS_RECUR);
 }
 
-#ifdef HAVE_MD5
 char *file_md5sum_alloc(const char *file_name)
 {
        static const int md5sum_bin_len = 16;
@@ -144,29 +137,18 @@ char *file_md5sum_alloc(const char *file_name)
                'c', 'd', 'e', 'f'
        };
 
-       int i, err;
-       FILE *file;
+       int i, len;
        char *md5sum_hex;
        unsigned char md5sum_bin[md5sum_bin_len];
 
-       md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
-
-       file = fopen(file_name, "r");
-       if (file == NULL) {
-               opkg_perror(ERROR, "Failed to open file %s", file_name);
-               free(md5sum_hex);
-               return NULL;
-       }
+       len = md5sum(file_name, md5sum_bin);
 
-       err = md5_stream(file, md5sum_bin);
-       if (err) {
+       if (len) {
                opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
-               fclose(file);
-               free(md5sum_hex);
                return NULL;
        }
 
-       fclose(file);
+       md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
 
        for (i = 0; i < md5sum_bin_len; i++) {
                md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
@@ -177,9 +159,7 @@ char *file_md5sum_alloc(const char *file_name)
 
        return md5sum_hex;
 }
-#endif
 
-#ifdef HAVE_SHA256
 char *file_sha256sum_alloc(const char *file_name)
 {
        static const int sha256sum_bin_len = 32;
@@ -227,12 +207,11 @@ char *file_sha256sum_alloc(const char *file_name)
        return sha256sum_hex;
 }
 
-#endif
-
 char *checksum_bin2hex(const char *src, size_t len)
 {
-       char *p;
-       static char buf[65];
+       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',
@@ -240,24 +219,30 @@ char *checksum_bin2hex(const char *src, size_t len)
                'c', 'd', 'e', 'f'
        };
 
-       if (len > 32)
+       if (!s || len > 32)
                return NULL;
 
-       for (p = buf; len > 0; src++, len--) {
-               *p++ = bin2hex[*src / 16];
-               *p++ = bin2hex[*src % 16];
+       for (p = buf; len > 0; s++, len--) {
+               *p++ = bin2hex[*s / 16];
+               *p++ = bin2hex[*s % 16];
        }
 
        *p = 0;
 
-       return buf;
+       return (char *)buf;
 }
 
 char *checksum_hex2bin(const char *src, size_t *len)
 {
-       char *p;
        size_t slen;
-       static char buf[32];
+       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++;
@@ -273,11 +258,11 @@ char *checksum_hex2bin(const char *src, size_t *len)
        (c >= 'a' ? (c - 'a') : (c >= 'A' ? (c - 'A') : (c - '0')))
 
        for (p = buf, *len = 0;
-            slen > 0 && isxdigit(src[0]) && isxdigit(src[1]);
-            slen--, src += 2, (*len)++)
-               *p++ = hex(src[0]) * 16 + hex(src[1]);
+            slen > 0 && isxdigit(s[0]) && isxdigit(s[1]);
+            slen--, s += 2, (*len)++)
+               *p++ = hex(s[0]) * 16 + hex(s[1]);
 
-       return buf;
+       return (char *)buf;
 }
 
 int rm_r(const char *path)