Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/coreutils/md5_sha1_sum.c

    r1765 r2725  
    44 *  Copyright (C) 2003-2004 Erik Andersen
    55 *
    6  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
     6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    77 */
    88
    99#include "libbb.h"
    1010
    11 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
     11/* This is a NOEXEC applet. Be very careful! */
    1212
    13 #define FLAG_SILENT 1
    14 #define FLAG_CHECK  2
    15 #define FLAG_WARN   4
     13enum {
     14    /* 4th letter of applet_name is... */
     15    HASH_MD5 = 's', /* "md5>s<um" */
     16    HASH_SHA1 = '1',
     17    HASH_SHA256 = '2',
     18    HASH_SHA512 = '5',
     19};
     20
     21#define FLAG_SILENT  1
     22#define FLAG_CHECK   2
     23#define FLAG_WARN    4
    1624
    1725/* This might be useful elsewhere */
     
    2230    char *hex_value = xzalloc((hash_length * 2) + 1);
    2331    bin2hex(hex_value, (char*)hash_value, hash_length);
    24     return hex_value;
     32    return (unsigned char *)hex_value;
    2533}
    2634
    27 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
     35static uint8_t *hash_file(const char *filename)
    2836{
    2937    int src_fd, hash_len, count;
    3038    union _ctx_ {
     39        sha512_ctx_t sha512;
     40        sha256_ctx_t sha256;
    3141        sha1_ctx_t sha1;
    3242        md5_ctx_t md5;
     
    3444    uint8_t *hash_value = NULL;
    3545    RESERVE_CONFIG_UBUFFER(in_buf, 4096);
    36     void (*update)(const void*, size_t, void*);
    37     void (*final)(void*, void*);
     46    void FAST_FUNC (*update)(void*, const void*, size_t);
     47    void FAST_FUNC (*final)(void*, void*);
     48    char hash_algo;
    3849
    39     src_fd = STDIN_FILENO;
    40     if (NOT_LONE_DASH(filename)) {
    41         src_fd = open_or_warn(filename, O_RDONLY);
    42         if (src_fd < 0) {
    43             return NULL;
    44         }
     50    src_fd = open_or_warn_stdin(filename);
     51    if (src_fd < 0) {
     52        return NULL;
    4553    }
    4654
     55    hash_algo = applet_name[3];
     56
    4757    /* figure specific hash algorithims */
    48     if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
     58    if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
    4959        md5_begin(&context.md5);
    50         update = (void (*)(const void*, size_t, void*))md5_hash;
    51         final = (void (*)(void*, void*))md5_end;
     60        update = (void*)md5_hash;
     61        final = (void*)md5_end;
    5262        hash_len = 16;
    53     } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
     63    } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
    5464        sha1_begin(&context.sha1);
    55         update = (void (*)(const void*, size_t, void*))sha1_hash;
    56         final = (void (*)(void*, void*))sha1_end;
     65        update = (void*)sha1_hash;
     66        final = (void*)sha1_end;
    5767        hash_len = 20;
     68    } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
     69        sha256_begin(&context.sha256);
     70        update = (void*)sha256_hash;
     71        final = (void*)sha256_end;
     72        hash_len = 32;
     73    } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
     74        sha512_begin(&context.sha512);
     75        update = (void*)sha512_hash;
     76        final = (void*)sha512_end;
     77        hash_len = 64;
    5878    } else {
    59         bb_error_msg_and_die("algorithm not supported");
     79        xfunc_die(); /* can't reach this */
    6080    }
    6181
    62     while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
    63         update(in_buf, count, &context);
     82    while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
     83        update(&context, in_buf, count);
    6484    }
    6585
    6686    if (count == 0) {
    67         final(in_buf, &context);
     87        final(&context, in_buf);
    6888        hash_value = hash_bin_to_hex(in_buf, hash_len);
    6989    }
     
    7898}
    7999
    80 int md5_sha1_sum_main(int argc, char **argv);
    81 int md5_sha1_sum_main(int argc, char **argv)
     100int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     101int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
    82102{
    83103    int return_value = EXIT_SUCCESS;
    84     uint8_t *hash_value;
    85104    unsigned flags;
    86     hash_algo_t hash_algo = ENABLE_MD5SUM
    87         ? (ENABLE_SHA1SUM ? (applet_name[0] == 'm' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)
    88         : HASH_SHA1;
    89105
    90     if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
    91         flags = getopt32(argv, "scw");
    92     else optind = 1;
     106    if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
     107        /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
     108        flags = getopt32(argv, "scwbt");
     109        argv += optind;
     110        //argc -= optind;
     111    } else {
     112        argv += 1;
     113        //argc -= 1;
     114    }
     115    if (!*argv)
     116        *--argv = (char*)"-";
    93117
    94118    if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
    95119        if (flags & FLAG_SILENT) {
    96             bb_error_msg_and_die
    97                 ("-%c is meaningful only when verifying checksums", 's');
    98         } else if (flags & FLAG_WARN) {
    99             bb_error_msg_and_die
    100                 ("-%c is meaningful only when verifying checksums", 'w');
     120            bb_error_msg_and_die("-%c is meaningful only with -c", 's');
    101121        }
    102     }
    103 
    104     if (argc == optind) {
    105         argv[argc++] = (char*)"-";
     122        if (flags & FLAG_WARN) {
     123            bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
     124        }
    106125    }
    107126
     
    110129        int count_total = 0;
    111130        int count_failed = 0;
    112         char *file_ptr = argv[optind];
    113131        char *line;
    114132
    115         if (optind + 1 != argc) {
    116             bb_error_msg_and_die
    117                 ("only one argument may be specified when using -c");
     133        if (argv[1]) {
     134            bb_error_msg_and_die("only one argument may be specified with -c");
    118135        }
    119136
    120         pre_computed_stream = stdin;
    121         if (NOT_LONE_DASH(file_ptr)) {
    122             pre_computed_stream = xfopen(file_ptr, "r");
    123         }
     137        pre_computed_stream = xfopen_stdin(argv[0]);
    124138
    125         while ((line = xmalloc_getline(pre_computed_stream)) != NULL) {
     139        while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
     140            uint8_t *hash_value;
    126141            char *filename_ptr;
    127142
     
    144159            filename_ptr += 2;
    145160
    146             hash_value = hash_file(filename_ptr, hash_algo);
     161            hash_value = hash_file(filename_ptr);
    147162
    148163            if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
     
    165180        /*
    166181        if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
    167             bb_perror_msg_and_die("cannot close file %s", file_ptr);
     182            bb_perror_msg_and_die("can't close file %s", file_ptr);
    168183        }
    169184        */
    170185    } else {
    171         while (optind < argc) {
    172             char *file_ptr = argv[optind++];
    173 
    174             hash_value = hash_file(file_ptr, hash_algo);
     186        do {
     187            uint8_t *hash_value = hash_file(*argv);
    175188            if (hash_value == NULL) {
    176189                return_value = EXIT_FAILURE;
    177190            } else {
    178                 printf("%s  %s\n", hash_value, file_ptr);
     191                printf("%s  %s\n", hash_value, *argv);
    179192                free(hash_value);
    180193            }
    181         }
     194        } while (*++argv);
    182195    }
    183196    return return_value;
Note: See TracChangeset for help on using the changeset viewer.