don't try to use gnu wget specific options in ipkg
[openwrt.git] / package / busybox / patches / 913-libbb_hash.patch
1 # Copyright (C) 2006 OpenWrt.org
2 #
3 # This is free software, licensed under the GNU General Public License v2.
4 # See /LICENSE for more information.
5 #
6 # expose (again) an hash_fd function (used in 911-ipkg.patch)
7 #
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
11 @@ -16,79 +16,10 @@
12  
13  #include "busybox.h"
14  
15 -typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
16 -
17  #define FLAG_SILENT    1
18  #define FLAG_CHECK     2
19  #define FLAG_WARN      4
20  
21 -/* This might be useful elsewhere */
22 -static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
23 -                                                                         unsigned char hash_length)
24 -{
25 -       int x, len, max;
26 -       unsigned char *hex_value;
27 -
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]);
32 -       }
33 -       return (hex_value);
34 -}
35 -
36 -static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
37 -{
38 -       int src_fd, hash_len, count;
39 -       union _ctx_ {
40 -               sha1_ctx_t sha1;
41 -               md5_ctx_t md5;
42 -       } context;
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*);
47 -
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);
52 -               return NULL;
53 -       }
54 -
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;
60 -               hash_len = 16;
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;
65 -               hash_len = 20;
66 -       } else {
67 -               bb_error_msg_and_die("algorithm not supported");
68 -       }
69 -
70 -       while (0 < (count = read(src_fd, in_buf, 4096))) {
71 -               update(in_buf, count, &context);
72 -       }
73 -
74 -       if (count == 0) {
75 -               final(in_buf, &context);
76 -               hash_value = hash_bin_to_hex(in_buf, hash_len);
77 -       }
78 -
79 -       RELEASE_CONFIG_BUFFER(in_buf);
80 -
81 -       if (src_fd != STDIN_FILENO) {
82 -               close(src_fd);
83 -       }
84 -
85 -       return hash_value;
86 -}
87 -
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)
90  {
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
94 @@ -518,6 +518,8 @@
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 *));
97  
98 +typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
99 +
100  typedef struct _sha1_ctx_t_ {
101         uint32_t count[2];
102         uint32_t hash[5];
103 @@ -542,6 +544,10 @@
104  void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
105  void *md5_end(void *resbuf, md5_ctx_t *ctx);
106  
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);
110 +
111  extern uint32_t *bb_crc32_filltable (int endian);
112  
113  #ifndef RB_POWER_OFF
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
117 @@ -0,0 +1,100 @@
118 +/*
119 + *  Copyright (C) 2003 Glenn L. McGrath
120 + *  Copyright (C) 2003-2004 Erik Andersen
121 + *
122 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
123 + */
124 +
125 +#include <fcntl.h>
126 +#include <limits.h>
127 +#include <stdio.h>
128 +#include <stdint.h>
129 +#include <stdlib.h>
130 +#include <string.h>
131 +#include <unistd.h>
132 +
133 +#include "busybox.h"
134 +
135 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length)
136 +{
137 +       int x, len, max;
138 +       unsigned char *hex_value;
139 +
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]);
144 +       }
145 +       return (hex_value);
146 +}
147 +
148 +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value)
149 +{
150 +       int count, result = 0;
151 +       union _ctx_ {
152 +               sha1_ctx_t sha1;
153 +               md5_ctx_t md5;
154 +       } context;
155 +       RESERVE_CONFIG_UBUFFER(in_buf, 4096);
156 +       void (*update)(const void*, size_t, void*) = NULL;
157 +       void (*final)(void*, void*) = NULL;
158 +       
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;
168 +       }
169 +
170 +
171 +       while (0 < (count = read(fd, in_buf, sizeof in_buf))) {
172 +               update(in_buf, count, &context);
173 +               result += count;
174 +       }
175 +
176 +       if (count == 0) {
177 +               final(hash_value, &context);
178 +       }
179 +       
180 +       RELEASE_CONFIG_BUFFER(in_buf);
181 +       
182 +       return result;
183 +}
184 +
185 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
186 +{
187 +       int src_fd, hash_len;
188 +       RESERVE_CONFIG_UBUFFER(hash_buf, 20);
189 +       uint8_t *hash_value = NULL;
190 +       
191 +       if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
192 +               hash_len = 16;
193 +       } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
194 +               hash_len = 20;
195 +       } else {
196 +               bb_error_msg_and_die("algotithm not supported");
197 +       }
198 +
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);
203 +               return NULL;
204 +       }
205 +
206 +       if (hash_fd(src_fd, hash_algo, hash_buf) > 0) {
207 +               hash_value = hash_bin_to_hex(hash_buf, hash_len);
208 +       }
209 +       
210 +       if (src_fd != STDIN_FILENO) {
211 +               close(src_fd);
212 +       }
213 +       
214 +       RELEASE_CONFIG_BUFFER(hash_buf);
215 +
216 +       return hash_value;
217 +}
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
221 @@ -11,6 +11,7 @@
222  
223  LIBBB-n:=
224  LIBBB-y:= \
225 +       hash.c \
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 \