1 # Copyright (C) 2006 OpenWrt.org
3 # This is free software, licensed under the GNU General Public License v2.
4 # See /LICENSE for more information.
6 # expose (again) an hash_fd function (used in 911-ipkg.patch)
8 diff -ruN busybox-1.2.0-orig/coreutils/md5_sha1_sum.c busybox-1.2.0-libbb_hash/coreutils/md5_sha1_sum.c
9 --- busybox-1.2.0-orig/coreutils/md5_sha1_sum.c 2006-07-01 00:42:07.000000000 +0200
10 +++ busybox-1.2.0-libbb_hash/coreutils/md5_sha1_sum.c 2006-07-22 17:08:02.000000000 +0200
15 -typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
21 -/* This might be useful elsewhere */
22 -static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
23 - unsigned char hash_length)
26 - unsigned char *hex_value;
28 - max = (hash_length * 2) + 2;
29 - hex_value = xmalloc(max);
30 - for (x = len = 0; x < hash_length; x++) {
31 - len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
36 -static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
38 - int src_fd, hash_len, count;
43 - uint8_t *hash_value = NULL;
44 - RESERVE_CONFIG_UBUFFER(in_buf, 4096);
45 - void (*update)(const void*, size_t, void*);
46 - void (*final)(void*, void*);
48 - if (strcmp(filename, "-") == 0) {
49 - src_fd = STDIN_FILENO;
50 - } else if(0 > (src_fd = open(filename, O_RDONLY))) {
51 - bb_perror_msg("%s", filename);
55 - /* figure specific hash algorithims */
56 - if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
57 - md5_begin(&context.md5);
58 - update = (void (*)(const void*, size_t, void*))md5_hash;
59 - final = (void (*)(void*, void*))md5_end;
61 - } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
62 - sha1_begin(&context.sha1);
63 - update = (void (*)(const void*, size_t, void*))sha1_hash;
64 - final = (void (*)(void*, void*))sha1_end;
67 - bb_error_msg_and_die("algorithm not supported");
70 - while (0 < (count = read(src_fd, in_buf, 4096))) {
71 - update(in_buf, count, &context);
75 - final(in_buf, &context);
76 - hash_value = hash_bin_to_hex(in_buf, hash_len);
79 - RELEASE_CONFIG_BUFFER(in_buf);
81 - if (src_fd != STDIN_FILENO) {
88 /* This could become a common function for md5 as well, by using md5_stream */
89 static int hash_files(int argc, char **argv, hash_algo_t hash_algo)
91 diff -ruN busybox-1.2.0-orig/include/libbb.h busybox-1.2.0-libbb_hash/include/libbb.h
92 --- busybox-1.2.0-orig/include/libbb.h 2006-07-01 00:42:10.000000000 +0200
93 +++ busybox-1.2.0-libbb_hash/include/libbb.h 2006-07-22 17:01:06.000000000 +0200
95 extern int get_terminal_width_height(int fd, int *width, int *height);
96 extern unsigned long get_ug_id(const char *s, long (*__bb_getxxnam)(const char *));
98 +typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
100 typedef struct _sha1_ctx_t_ {
104 void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
105 void *md5_end(void *resbuf, md5_ctx_t *ctx);
107 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length);
108 +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value);
109 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo);
111 extern uint32_t *bb_crc32_filltable (int endian);
114 diff -ruN busybox-1.2.0-orig/libbb/hash.c busybox-1.2.0-libbb_hash/libbb/hash.c
115 --- busybox-1.2.0-orig/libbb/hash.c 1970-01-01 01:00:00.000000000 +0100
116 +++ busybox-1.2.0-libbb_hash/libbb/hash.c 2006-07-22 17:07:34.000000000 +0200
119 + * Copyright (C) 2003 Glenn L. McGrath
120 + * Copyright (C) 2003-2004 Erik Andersen
122 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
133 +#include "busybox.h"
135 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length)
138 + unsigned char *hex_value;
140 + max = (hash_length * 2) + 2;
141 + hex_value = xmalloc(max);
142 + for (x = len = 0; x < hash_length; x++) {
143 + len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
145 + return (hex_value);
148 +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value)
150 + int count, result = 0;
155 + RESERVE_CONFIG_UBUFFER(in_buf, 4096);
156 + void (*update)(const void*, size_t, void*) = NULL;
157 + void (*final)(void*, void*) = NULL;
159 + // figure specific hash algorithims
160 + if (hash_algo==HASH_MD5) {
161 + md5_begin(&context.md5);
162 + update = (void (*)(const void*, size_t, void*))md5_hash;
163 + final = (void (*)(void*, void*))md5_end;
164 + } else if (hash_algo==HASH_SHA1) {
165 + sha1_begin(&context.sha1);
166 + update = (void (*)(const void*, size_t, void*))sha1_hash;
167 + final = (void (*)(void*, void*))sha1_end;
171 + while (0 < (count = read(fd, in_buf, sizeof in_buf))) {
172 + update(in_buf, count, &context);
177 + final(hash_value, &context);
180 + RELEASE_CONFIG_BUFFER(in_buf);
185 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
187 + int src_fd, hash_len;
188 + RESERVE_CONFIG_UBUFFER(hash_buf, 20);
189 + uint8_t *hash_value = NULL;
191 + if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
193 + } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
196 + bb_error_msg_and_die("algotithm not supported");
199 + if (strcmp(filename, "-") == 0) {
200 + src_fd = STDIN_FILENO;
201 + } else if (0 > (src_fd = open(filename, O_RDONLY))) {
202 + bb_perror_msg("%s", filename);
206 + if (hash_fd(src_fd, hash_algo, hash_buf) > 0) {
207 + hash_value = hash_bin_to_hex(hash_buf, hash_len);
210 + if (src_fd != STDIN_FILENO) {
214 + RELEASE_CONFIG_BUFFER(hash_buf);
218 diff -ruN busybox-1.2.0-orig/libbb/Makefile.in busybox-1.2.0-libbb_hash/libbb/Makefile.in
219 --- busybox-1.2.0-orig/libbb/Makefile.in 2006-07-01 00:42:08.000000000 +0200
220 +++ busybox-1.2.0-libbb_hash/libbb/Makefile.in 2006-07-22 16:51:47.000000000 +0200
226 bb_asprintf.c ask_confirmation.c change_identity.c chomp.c \
227 compare_string_array.c concat_path_file.c copy_file.c copyfd.c \
228 crc32.c create_icmp_socket.c create_icmp6_socket.c \