update ipkg patch for 1.1.1, expose an hash_fd function again removed in 1.1.1
[openwrt.git] / package / busybox / patches / 913-libbb_hash.patch
1 diff -ruN busybox-1.1.1-old/coreutils/md5_sha1_sum.c busybox-1.1.1-new/coreutils/md5_sha1_sum.c
2 --- busybox-1.1.1-old/coreutils/md5_sha1_sum.c  2006-03-30 00:14:50.000000000 +0200
3 +++ busybox-1.1.1-new/coreutils/md5_sha1_sum.c  2006-03-29 23:46:51.000000000 +0200
4 @@ -15,80 +15,10 @@
5  
6  #include "busybox.h"
7  
8 -typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
9 -
10  #define FLAG_SILENT    1
11  #define FLAG_CHECK     2
12  #define FLAG_WARN      4
13  
14 -/* This might be useful elsewhere */
15 -static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
16 -                                                                         unsigned char hash_length)
17 -{
18 -       int x, len, max;
19 -       unsigned char *hex_value;
20 -
21 -       max = (hash_length * 2) + 2;
22 -       hex_value = xmalloc(max);
23 -       for (x = len = 0; x < hash_length; x++) {
24 -               len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
25 -       }
26 -       return (hex_value);
27 -}
28 -
29 -static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
30 -{
31 -       int src_fd, hash_len, count;
32 -       union _ctx_ {
33 -               sha1_ctx_t sha1;
34 -               md5_ctx_t md5;
35 -       } context;
36 -       uint8_t *hash_value = NULL;
37 -       RESERVE_CONFIG_UBUFFER(in_buf, 4096);
38 -       void (*update)(const void*, size_t, void*);
39 -       void (*final)(void*, void*);
40 -       
41 -       if(strcmp(filename, "-") == 0) {
42 -               src_fd = STDIN_FILENO;
43 -       } else if(0 > (src_fd = open(filename, O_RDONLY))) {
44 -               bb_perror_msg("%s", filename);
45 -               return NULL;
46 -       }
47 -
48 -       // figure specific hash algorithims
49 -       if(ENABLE_MD5SUM && hash_algo==HASH_MD5) {
50 -               md5_begin(&context.md5);
51 -               update = (void (*)(const void*, size_t, void*))md5_hash;
52 -               final = (void (*)(void*, void*))md5_end;
53 -               hash_len = 16;
54 -       } else if(ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
55 -               sha1_begin(&context.sha1);
56 -               update = (void (*)(const void*, size_t, void*))sha1_hash;
57 -               final = (void (*)(void*, void*))sha1_end;
58 -               hash_len = 20;
59 -       } else {
60 -               bb_error_msg_and_die("algotithm not supported");
61 -       }
62 -       
63 -
64 -       while(0 < (count = read(src_fd, in_buf, sizeof in_buf))) {
65 -               update(in_buf, count, &context);
66 -       }
67 -
68 -       if(count == 0) {
69 -               final(in_buf, &context);
70 -               hash_value = hash_bin_to_hex(in_buf, hash_len);
71 -       }
72 -       
73 -       RELEASE_CONFIG_BUFFER(in_buf);
74 -       
75 -       if(src_fd != STDIN_FILENO) {
76 -               close(src_fd);
77 -       }
78 -       
79 -       return hash_value;
80 -}
81 -
82  /* This could become a common function for md5 as well, by using md5_stream */
83  static int hash_files(int argc, char **argv, hash_algo_t hash_algo)
84  {
85 diff -ruN busybox-1.1.1-old/include/libbb.h busybox-1.1.1-new/include/libbb.h
86 --- busybox-1.1.1-old/include/libbb.h   2006-03-30 00:14:50.000000000 +0200
87 +++ busybox-1.1.1-new/include/libbb.h   2006-03-30 00:31:48.000000000 +0200
88 @@ -490,6 +490,12 @@
89  void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
90  void *md5_end(void *resbuf, md5_ctx_t *ctx);
91  
92 +typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
93 +
94 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length);
95 +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value);
96 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo);
97 +
98  /* busybox.h will include dmalloc later for us, else include it here.  */
99  #if !defined _BB_INTERNAL_H_ && defined DMALLOC
100  #include <dmalloc.h>
101 diff -ruN busybox-1.1.1-old/libbb/Makefile.in busybox-1.1.1-new/libbb/Makefile.in
102 --- busybox-1.1.1-old/libbb/Makefile.in 2006-03-30 00:14:50.000000000 +0200
103 +++ busybox-1.1.1-new/libbb/Makefile.in 2006-03-29 23:46:51.000000000 +0200
104 @@ -11,6 +11,7 @@
105  
106  LIBBB-n:=
107  LIBBB-y:= \
108 +       hash.c \
109         bb_asprintf.c ask_confirmation.c change_identity.c chomp.c \
110         compare_string_array.c concat_path_file.c copy_file.c copyfd.c \
111         create_icmp_socket.c create_icmp6_socket.c \
112 diff -ruN busybox-1.1.1-old/libbb/hash.c busybox-1.1.1-new/libbb/hash.c
113 --- busybox-1.1.1-old/libbb/hash.c      1970-01-01 01:00:00.000000000 +0100
114 +++ busybox-1.1.1-new/libbb/hash.c      2006-03-30 00:35:54.000000000 +0200
115 @@ -0,0 +1,100 @@
116 +/*
117 + *  Copyright (C) 2003 Glenn L. McGrath
118 + *  Copyright (C) 2003-2004 Erik Andersen
119 + *
120 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
121 + */
122 +
123 +#include <fcntl.h>
124 +#include <limits.h>
125 +#include <stdio.h>
126 +#include <stdint.h>
127 +#include <stdlib.h>
128 +#include <string.h>
129 +#include <unistd.h>
130 +
131 +#include "busybox.h"
132 +
133 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length)
134 +{
135 +       int x, len, max;
136 +       unsigned char *hex_value;
137 +
138 +       max = (hash_length * 2) + 2;
139 +       hex_value = xmalloc(max);
140 +       for (x = len = 0; x < hash_length; x++) {
141 +               len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
142 +       }
143 +       return (hex_value);
144 +}
145 +
146 +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value)
147 +{
148 +       int count, result = 0;
149 +       union _ctx_ {
150 +               sha1_ctx_t sha1;
151 +               md5_ctx_t md5;
152 +       } context;
153 +       RESERVE_CONFIG_UBUFFER(in_buf, 4096);
154 +       void (*update)(const void*, size_t, void*) = NULL;
155 +       void (*final)(void*, void*) = NULL;
156 +       
157 +       // figure specific hash algorithims
158 +       if(hash_algo==HASH_MD5) {
159 +               md5_begin(&context.md5);
160 +               update = (void (*)(const void*, size_t, void*))md5_hash;
161 +               final = (void (*)(void*, void*))md5_end;
162 +       } else if(hash_algo==HASH_SHA1) {
163 +               sha1_begin(&context.sha1);
164 +               update = (void (*)(const void*, size_t, void*))sha1_hash;
165 +               final = (void (*)(void*, void*))sha1_end;
166 +       }
167 +
168 +
169 +       while(0 < (count = read(fd, in_buf, sizeof in_buf))) {
170 +               update(in_buf, count, &context);
171 +               result += count;
172 +       }
173 +
174 +       if(count == 0) {
175 +               final(hash_value, &context);
176 +       }
177 +       
178 +       RELEASE_CONFIG_BUFFER(in_buf);
179 +       
180 +       return result;
181 +}
182 +
183 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
184 +{
185 +       int src_fd, hash_len;
186 +       RESERVE_CONFIG_UBUFFER(hash_buf, 20);
187 +       uint8_t *hash_value = NULL;
188 +       
189 +       if(ENABLE_MD5SUM && hash_algo==HASH_MD5) {
190 +               hash_len = 16;
191 +       } else if(ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
192 +               hash_len = 20;
193 +       } else {
194 +               bb_error_msg_and_die("algotithm not supported");
195 +       }
196 +
197 +       if(strcmp(filename, "-") == 0) {
198 +               src_fd = STDIN_FILENO;
199 +       } else if(0 > (src_fd = open(filename, O_RDONLY))) {
200 +               bb_perror_msg("%s", filename);
201 +               return NULL;
202 +       }
203 +
204 +       if(hash_fd(src_fd, hash_algo, hash_buf) > 0) {
205 +               hash_value = hash_bin_to_hex(hash_buf, hash_len);
206 +       }
207 +       
208 +       if(src_fd != STDIN_FILENO) {
209 +               close(src_fd);
210 +       }
211 +       
212 +       RELEASE_CONFIG_BUFFER(hash_buf);
213 +
214 +       return hash_value;
215 +}