X-Git-Url: https://git.librecmc.org/?p=oweals%2Fopkg-lede.git;a=blobdiff_plain;f=libopkg%2Ffile_util.c;h=759c21b3ae76d206f717022c38dc8538fecf0d52;hp=c54903c30b66907d126cd6ebb5ed0b58759e0f34;hb=d1fe095d2c1463ca1e945028b15f57a256070a06;hpb=733eb7729491810c346bbbecd31051ecc118fd82 diff --git a/libopkg/file_util.c b/libopkg/file_util.c index c54903c..759c21b 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -16,27 +16,21 @@ General Public License for more details. */ -#include "config.h" - #include #include #include #include #include +#include #include "sprintf_alloc.h" #include "file_util.h" -#ifdef HAVE_MD5 -#include "md5.h" -#endif +#include #include "libbb/libbb.h" -#if defined HAVE_SHA256 #include "sha256.h" -#endif -int -file_exists(const char *file_name) +int file_exists(const char *file_name) { struct stat st; @@ -46,8 +40,7 @@ file_exists(const char *file_name) return 1; } -int -file_is_dir(const char *file_name) +int file_is_dir(const char *file_name) { struct stat st; @@ -64,8 +57,7 @@ file_is_dir(const char *file_name) Return value is NULL if the file is at EOF when called. */ -char * -file_read_line_alloc(FILE *fp) +char *file_read_line_alloc(FILE * fp) { char buf[BUFSIZ]; unsigned int buf_len; @@ -84,7 +76,7 @@ file_read_line_alloc(FILE *fp) } if (line) { line_size += buf_len; - line = xrealloc(line, line_size+1); + line = xrealloc(line, line_size + 1); strncat(line, buf, line_size); } else { line_size = buf_len + 1; @@ -97,8 +89,7 @@ file_read_line_alloc(FILE *fp) return line; } -int -file_move(const char *src, const char *dest) +int file_move(const char *src, const char *dest) { int err; @@ -111,132 +102,166 @@ file_move(const char *src, const char *dest) unlink(src); } else { opkg_perror(ERROR, "Failed to rename %s to %s", - src, dest); + src, dest); } } return err; } -int -file_copy(const char *src, const char *dest) +int file_copy(const char *src, const char *dest) { int err; err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS); if (err) - opkg_msg(ERROR, "Failed to copy file %s to %s.\n", - src, dest); + opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest); return err; } -int -file_mkdir_hier(const char *path, long mode) +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 int hex2bin(unsigned char x) { - static const int md5sum_bin_len = 16; - static const int md5sum_hex_len = 32; + if (x >= 'a' && x <= 'f') + return x - 'a' + 10; + else if (x >= 'A' && x <= 'F') + return x - 'A' + 10; + else if (x >= '0' && x <= '9') + return x - '0'; + else + return 0; +} - static const unsigned char bin2hex[16] = { +static const unsigned char bin2hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' - }; +}; - int i, err; - FILE *file; - char *md5sum_hex; - unsigned char md5sum_bin[md5sum_bin_len]; +char *file_md5sum_alloc(const char *file_name) +{ + static const int md5sum_bin_len = 16; + static const int md5sum_hex_len = 32; - md5sum_hex = xcalloc(1, md5sum_hex_len + 1); + int i, len; + char *md5sum_hex; + unsigned char md5sum_bin[md5sum_bin_len]; - 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) { - opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name); - fclose(file); - free(md5sum_hex); - return NULL; - } + if (len) { + opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name); + 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]; - md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf]; - } + for (i = 0; i < md5sum_bin_len; i++) { + md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4]; + md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf]; + } - md5sum_hex[md5sum_hex_len] = '\0'; + md5sum_hex[md5sum_hex_len] = '\0'; - return md5sum_hex; + return md5sum_hex; } -#endif -#ifdef HAVE_SHA256 char *file_sha256sum_alloc(const char *file_name) { - static const int sha256sum_bin_len = 32; - static const int sha256sum_hex_len = 64; + static const int sha256sum_bin_len = 32; + static const int sha256sum_hex_len = 64; - static const unsigned char bin2hex[16] = { - '0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f' - }; + int i, err; + FILE *file; + char *sha256sum_hex; + unsigned char sha256sum_bin[sha256sum_bin_len]; - int i, err; - FILE *file; - char *sha256sum_hex; - unsigned char sha256sum_bin[sha256sum_bin_len]; + sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1); - sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1); + file = fopen(file_name, "r"); + if (file == NULL) { + opkg_perror(ERROR, "Failed to open file %s", file_name); + free(sha256sum_hex); + return NULL; + } - file = fopen(file_name, "r"); - if (file == NULL) { - opkg_perror(ERROR, "Failed to open file %s", file_name); - free(sha256sum_hex); - return NULL; - } + err = sha256_stream(file, sha256sum_bin); + if (err) { + opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", + file_name); + fclose(file); + free(sha256sum_hex); + return NULL; + } - err = sha256_stream(file, sha256sum_bin); - if (err) { - opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", file_name); fclose(file); - free(sha256sum_hex); - return NULL; - } - fclose(file); + for (i = 0; i < sha256sum_bin_len; i++) { + sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4]; + sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf]; + } + + sha256sum_hex[sha256sum_hex_len] = '\0'; + + return sha256sum_hex; +} - for (i=0; i < sha256sum_bin_len; i++) { - sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4]; - sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf]; - } +char *checksum_bin2hex(const char *src, size_t len) +{ + unsigned char *p; + static unsigned char buf[65]; + const unsigned char *s = (unsigned char *)src; + if (!s || len > 32) + return NULL; + + for (p = buf; len > 0; s++, len--) { + *p++ = bin2hex[*s / 16]; + *p++ = bin2hex[*s % 16]; + } - sha256sum_hex[sha256sum_hex_len] = '\0'; + *p = 0; - return sha256sum_hex; + return (char *)buf; } -#endif +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; + } + + for (p = buf, *len = 0; + slen > 0 && isxdigit(s[0]) && isxdigit(s[1]); + slen--, s += 2, (*len)++) + *p++ = hex2bin(s[0]) * 16 + hex2bin(s[1]); + return (char *)buf; +} -int -rm_r(const char *path) +int rm_r(const char *path) { int ret = 0; DIR *dir; @@ -264,7 +289,7 @@ rm_r(const char *path) if ((dent = readdir(dir)) == NULL) { if (errno) { opkg_perror(ERROR, "Failed to read dir %s", - path); + path); ret = -1; } break; @@ -284,7 +309,7 @@ rm_r(const char *path) struct stat st; if ((ret = lstat(dent->d_name, &st)) == -1) { opkg_perror(ERROR, "Failed to lstat %s", - dent->d_name); + dent->d_name); break; } if (S_ISDIR(st.st_mode)) { @@ -305,7 +330,7 @@ rm_r(const char *path) opkg_perror(ERROR, "Failed to change to dir %s/..", path); } - if (rmdir(path) == -1 ) { + if (rmdir(path) == -1) { ret = -1; opkg_perror(ERROR, "Failed to remove dir %s", path); } @@ -317,3 +342,56 @@ rm_r(const char *path) return ret; } + +static int urlencode_is_specialchar(char c) +{ + switch (c) { + case ':': + case '?': + case '#': + case '[': + case ']': + case '@': + case '!': + case '$': + case '&': + case '\'': + case '(': + case ')': + case '*': + case '+': + case ',': + case ';': + case '=': + case '%': + return 1; + + default: + return 0; + } +} + +char *urlencode_path(const char *filename) +{ + size_t len = 0; + const unsigned char *in; + unsigned char *copy, *out; + + for (in = (unsigned char *)filename; *in != 0; in++) + len += urlencode_is_specialchar(*in) ? 3 : 1; + + copy = xcalloc(1, len + 1); + + for (in = (unsigned char *)filename, out = copy; *in != 0; in++) { + if (urlencode_is_specialchar(*in)) { + *out++ = '%'; + *out++ = bin2hex[*in / 16]; + *out++ = bin2hex[*in % 16]; + } + else { + *out++ = *in; + } + } + + return (char *)copy; +}