Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (7 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

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

    r2725 r3232  
    77 */
    88
     9//usage:#define md5sum_trivial_usage
     10//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
     11//usage:#define md5sum_full_usage "\n\n"
     12//usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " MD5 checksums"
     13//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
     14//usage:     "\n    -c  Check sums against list in FILEs"
     15//usage:     "\n    -s  Don't output anything, status code shows success"
     16//usage:     "\n    -w  Warn about improperly formatted checksum lines"
     17//usage:    )
     18//usage:
     19//usage:#define md5sum_example_usage
     20//usage:       "$ md5sum < busybox\n"
     21//usage:       "6fd11e98b98a58f64ff3398d7b324003\n"
     22//usage:       "$ md5sum busybox\n"
     23//usage:       "6fd11e98b98a58f64ff3398d7b324003  busybox\n"
     24//usage:       "$ md5sum -c -\n"
     25//usage:       "6fd11e98b98a58f64ff3398d7b324003  busybox\n"
     26//usage:       "busybox: OK\n"
     27//usage:       "^D\n"
     28//usage:
     29//usage:#define sha1sum_trivial_usage
     30//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
     31//usage:#define sha1sum_full_usage "\n\n"
     32//usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA1 checksums"
     33//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
     34//usage:     "\n    -c  Check sums against list in FILEs"
     35//usage:     "\n    -s  Don't output anything, status code shows success"
     36//usage:     "\n    -w  Warn about improperly formatted checksum lines"
     37//usage:    )
     38//usage:
     39//usage:#define sha256sum_trivial_usage
     40//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
     41//usage:#define sha256sum_full_usage "\n\n"
     42//usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA256 checksums"
     43//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
     44//usage:     "\n    -c  Check sums against list in FILEs"
     45//usage:     "\n    -s  Don't output anything, status code shows success"
     46//usage:     "\n    -w  Warn about improperly formatted checksum lines"
     47//usage:    )
     48//usage:
     49//usage:#define sha512sum_trivial_usage
     50//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
     51//usage:#define sha512sum_full_usage "\n\n"
     52//usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA512 checksums"
     53//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
     54//usage:     "\n    -c  Check sums against list in FILEs"
     55//usage:     "\n    -s  Don't output anything, status code shows success"
     56//usage:     "\n    -w  Warn about improperly formatted checksum lines"
     57//usage:    )
     58//usage:
     59//usage:#define sha3sum_trivial_usage
     60//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
     61//usage:#define sha3sum_full_usage "\n\n"
     62//usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA3-512 checksums"
     63//usage:    IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
     64//usage:     "\n    -c  Check sums against list in FILEs"
     65//usage:     "\n    -s  Don't output anything, status code shows success"
     66//usage:     "\n    -w  Warn about improperly formatted checksum lines"
     67//usage:    )
     68
    969#include "libbb.h"
    1070
     
    1676    HASH_SHA1 = '1',
    1777    HASH_SHA256 = '2',
     78    HASH_SHA3 = '3',
    1879    HASH_SHA512 = '5',
    1980};
     
    3798    int src_fd, hash_len, count;
    3899    union _ctx_ {
     100        sha3_ctx_t sha3;
    39101        sha512_ctx_t sha512;
    40102        sha256_ctx_t sha256;
     
    42104        md5_ctx_t md5;
    43105    } context;
    44     uint8_t *hash_value = NULL;
    45     RESERVE_CONFIG_UBUFFER(in_buf, 4096);
     106    uint8_t *hash_value;
    46107    void FAST_FUNC (*update)(void*, const void*, size_t);
    47108    void FAST_FUNC (*final)(void*, void*);
     
    55116    hash_algo = applet_name[3];
    56117
    57     /* figure specific hash algorithims */
     118    /* figure specific hash algorithms */
    58119    if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
    59120        md5_begin(&context.md5);
     
    76137        final = (void*)sha512_end;
    77138        hash_len = 64;
     139    } else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) {
     140        sha3_begin(&context.sha3);
     141        update = (void*)sha3_hash;
     142        final = (void*)sha3_end;
     143        hash_len = 64;
    78144    } else {
    79145        xfunc_die(); /* can't reach this */
    80146    }
    81147
    82     while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
    83         update(&context, in_buf, count);
    84     }
    85 
    86     if (count == 0) {
    87         final(&context, in_buf);
    88         hash_value = hash_bin_to_hex(in_buf, hash_len);
    89     }
    90 
    91     RELEASE_CONFIG_BUFFER(in_buf);
     148    {
     149        RESERVE_CONFIG_UBUFFER(in_buf, 4096);
     150        while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
     151            update(&context, in_buf, count);
     152        }
     153        hash_value = NULL;
     154        if (count == 0) {
     155            final(&context, in_buf);
     156            hash_value = hash_bin_to_hex(in_buf, hash_len);
     157        }
     158        RELEASE_CONFIG_BUFFER(in_buf);
     159    }
    92160
    93161    if (src_fd != STDIN_FILENO) {
     
    125193    }
    126194
    127     if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
    128         FILE *pre_computed_stream;
    129         int count_total = 0;
    130         int count_failed = 0;
    131         char *line;
    132 
    133         if (argv[1]) {
    134             bb_error_msg_and_die("only one argument may be specified with -c");
    135         }
    136 
    137         pre_computed_stream = xfopen_stdin(argv[0]);
    138 
    139         while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
    140             uint8_t *hash_value;
    141             char *filename_ptr;
    142 
    143             count_total++;
    144             filename_ptr = strstr(line, "  ");
    145             /* handle format for binary checksums */
    146             if (filename_ptr == NULL) {
    147                 filename_ptr = strstr(line, " *");
     195    do {
     196        if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
     197            FILE *pre_computed_stream;
     198            char *line;
     199            int count_total = 0;
     200            int count_failed = 0;
     201
     202            pre_computed_stream = xfopen_stdin(*argv);
     203
     204            while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
     205                uint8_t *hash_value;
     206                char *filename_ptr;
     207
     208                count_total++;
     209                filename_ptr = strstr(line, "  ");
     210                /* handle format for binary checksums */
     211                if (filename_ptr == NULL) {
     212                    filename_ptr = strstr(line, " *");
     213                }
     214                if (filename_ptr == NULL) {
     215                    if (flags & FLAG_WARN) {
     216                        bb_error_msg("invalid format");
     217                    }
     218                    count_failed++;
     219                    return_value = EXIT_FAILURE;
     220                    free(line);
     221                    continue;
     222                }
     223                *filename_ptr = '\0';
     224                filename_ptr += 2;
     225
     226                hash_value = hash_file(filename_ptr);
     227
     228                if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
     229                    if (!(flags & FLAG_SILENT))
     230                        printf("%s: OK\n", filename_ptr);
     231                } else {
     232                    if (!(flags & FLAG_SILENT))
     233                        printf("%s: FAILED\n", filename_ptr);
     234                    count_failed++;
     235                    return_value = EXIT_FAILURE;
     236                }
     237                /* possible free(NULL) */
     238                free(hash_value);
     239                free(line);
    148240            }
    149             if (filename_ptr == NULL) {
    150                 if (flags & FLAG_WARN) {
    151                     bb_error_msg("invalid format");
    152                 }
    153                 count_failed++;
    154                 return_value = EXIT_FAILURE;
    155                 free(line);
    156                 continue;
     241            if (count_failed && !(flags & FLAG_SILENT)) {
     242                bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
     243                        count_failed, count_total);
    157244            }
    158             *filename_ptr = '\0';
    159             filename_ptr += 2;
    160 
    161             hash_value = hash_file(filename_ptr);
    162 
    163             if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
    164                 if (!(flags & FLAG_SILENT))
    165                     printf("%s: OK\n", filename_ptr);
    166             } else {
    167                 if (!(flags & FLAG_SILENT))
    168                     printf("%s: FAILED\n", filename_ptr);
    169                 count_failed++;
    170                 return_value = EXIT_FAILURE;
    171             }
    172             /* possible free(NULL) */
    173             free(hash_value);
    174             free(line);
    175         }
    176         if (count_failed && !(flags & FLAG_SILENT)) {
    177             bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
    178                          count_failed, count_total);
    179         }
    180         /*
    181         if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
    182             bb_perror_msg_and_die("can't close file %s", file_ptr);
    183         }
    184         */
    185     } else {
    186         do {
     245            fclose_if_not_stdin(pre_computed_stream);
     246        } else {
    187247            uint8_t *hash_value = hash_file(*argv);
    188248            if (hash_value == NULL) {
     
    192252                free(hash_value);
    193253            }
    194         } while (*++argv);
    195     }
     254        }
     255    } while (*++argv);
     256
    196257    return return_value;
    197258}
Note: See TracChangeset for help on using the changeset viewer.