dc29bb05c0d3aaacd66625c52940a56d3b0c795f
[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.3.1-orig/coreutils/md5_sha1_sum.c busybox-1.3.1-913/coreutils/md5_sha1_sum.c
9 --- busybox-1.3.1-orig/coreutils/md5_sha1_sum.c 2006-12-27 05:54:50.000000000 +0100
10 +++ busybox-1.3.1-913/coreutils/md5_sha1_sum.c  2006-12-28 00:59:35.000000000 +0100
11 @@ -8,78 +8,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 hash_length)
24 -{
25 -       int len = 0;
26 -       char *hex_value = xmalloc((hash_length * 2) + 2);
27 -       while (hash_length--) {
28 -               len += sprintf(hex_value + len, "%02x", *hash_value++);
29 -       }
30 -       return hex_value;
31 -}
32 -
33 -static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
34 -{
35 -       int src_fd, hash_len, count;
36 -       union _ctx_ {
37 -               sha1_ctx_t sha1;
38 -               md5_ctx_t md5;
39 -       } context;
40 -       uint8_t *hash_value = NULL;
41 -       RESERVE_CONFIG_UBUFFER(in_buf, 4096);
42 -       void (*update)(const void*, size_t, void*);
43 -       void (*final)(void*, void*);
44 -
45 -       src_fd = STDIN_FILENO;
46 -       if (filename[0] != '-' || filename[1]) { /* not "-" */
47 -               src_fd = open(filename, O_RDONLY);
48 -               if (src_fd < 0) {
49 -                       bb_perror_msg("%s", filename);
50 -                       return NULL;
51 -               }
52 -       }
53 -
54 -       /* figure specific hash algorithims */
55 -       if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
56 -               md5_begin(&context.md5);
57 -               update = (void (*)(const void*, size_t, void*))md5_hash;
58 -               final = (void (*)(void*, void*))md5_end;
59 -               hash_len = 16;
60 -       } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
61 -               sha1_begin(&context.sha1);
62 -               update = (void (*)(const void*, size_t, void*))sha1_hash;
63 -               final = (void (*)(void*, void*))sha1_end;
64 -               hash_len = 20;
65 -       } else {
66 -               bb_error_msg_and_die("algorithm not supported");
67 -       }
68 -
69 -       while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
70 -               update(in_buf, count, &context);
71 -       }
72 -
73 -       if (count == 0) {
74 -               final(in_buf, &context);
75 -               hash_value = hash_bin_to_hex(in_buf, hash_len);
76 -       }
77 -
78 -       RELEASE_CONFIG_BUFFER(in_buf);
79 -
80 -       if (src_fd != STDIN_FILENO) {
81 -               close(src_fd);
82 -       }
83 -
84 -       return hash_value;
85 -}
86 -
87  int md5_sha1_sum_main(int argc, char **argv)
88  {
89         int return_value = EXIT_SUCCESS;
90 diff -ruN busybox-1.3.1-orig/include/libbb.h busybox-1.3.1-913/include/libbb.h
91 --- busybox-1.3.1-orig/include/libbb.h  2006-12-27 05:56:18.000000000 +0100
92 +++ busybox-1.3.1-913/include/libbb.h   2006-12-27 23:25:52.000000000 +0100
93 @@ -528,6 +528,8 @@
94  extern const char bb_uuenc_tbl_std[];
95  void bb_uuencode(const unsigned char *s, char *store, const int length, const char *tbl);
96  
97 +typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
98 +
99  typedef struct sha1_ctx_t {
100         uint32_t count[2];
101         uint32_t hash[5];
102 @@ -550,6 +552,10 @@
103  void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
104  void *md5_end(void *resbuf, md5_ctx_t *ctx);
105  
106 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length);
107 +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value);
108 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo);
109 +
110  uint32_t *crc32_filltable(int endian);
111  
112  
113 diff -ruN busybox-1.3.1-orig/libbb/hash.c busybox-1.3.1-913/libbb/hash.c
114 --- busybox-1.3.1-orig/libbb/hash.c     1970-01-01 01:00:00.000000000 +0100
115 +++ busybox-1.3.1-913/libbb/hash.c      2006-12-28 00:48:52.000000000 +0100
116 @@ -0,0 +1,99 @@
117 +/*
118 + *  Copyright (C) 2003 Glenn L. McGrath
119 + *  Copyright (C) 2003-2004 Erik Andersen
120 + *
121 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
122 + */
123 +
124 +#include <fcntl.h>
125 +#include <limits.h>
126 +#include <stdio.h>
127 +#include <stdint.h>
128 +#include <stdlib.h>
129 +#include <string.h>
130 +#include <unistd.h>
131 +
132 +#include "busybox.h"
133 +
134 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length)
135 +{
136 +       int len = 0;
137 +       char *hex_value = xmalloc((hash_length * 2) + 2);
138 +       while (hash_length--) {
139 +               len += sprintf(hex_value + len, "%02x", *hash_value++);
140 +       }
141 +       return hex_value;
142 +}
143 +
144 +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value)
145 +{
146 +       int count, result = 0;
147 +       union _ctx_ {
148 +               sha1_ctx_t sha1;
149 +               md5_ctx_t md5;
150 +       } context;
151 +       RESERVE_CONFIG_UBUFFER(in_buf, 4096);
152 +       void (*update)(const void*, size_t, void*) = NULL;
153 +       void (*final)(void*, void*) = NULL;
154 +       
155 +       // figure specific hash algorithims
156 +       if (hash_algo==HASH_MD5) {
157 +               md5_begin(&context.md5);
158 +               update = (void (*)(const void*, size_t, void*))md5_hash;
159 +               final = (void (*)(void*, void*))md5_end;
160 +       } else if (hash_algo==HASH_SHA1) {
161 +               sha1_begin(&context.sha1);
162 +               update = (void (*)(const void*, size_t, void*))sha1_hash;
163 +               final = (void (*)(void*, void*))sha1_end;
164 +       }
165 +
166 +
167 +       while (0 < (count = safe_read(fd, in_buf, sizeof in_buf))) {
168 +               update(in_buf, count, &context);
169 +               result += count;
170 +       }
171 +
172 +       if (count == 0) {
173 +               final(hash_value, &context);
174 +       }
175 +       
176 +       RELEASE_CONFIG_BUFFER(in_buf);
177 +       
178 +       return result;
179 +}
180 +
181 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
182 +{
183 +       int src_fd, hash_len;
184 +       RESERVE_CONFIG_UBUFFER(hash_buf, 20);
185 +       uint8_t *hash_value = NULL;
186 +       
187 +       if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
188 +               hash_len = 16;
189 +       } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
190 +               hash_len = 20;
191 +       } else {
192 +               bb_error_msg_and_die("algotithm not supported");
193 +       }
194 +
195 +       src_fd = STDIN_FILENO;
196 +       if (filename[0] != '-' || filename[1]) { /* not "-" */
197 +               src_fd = open(filename, O_RDONLY);
198 +               if (src_fd < 0) {
199 +                       bb_perror_msg("%s", filename);
200 +                       return NULL;
201 +               }
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 +}
216 diff -ruN busybox-1.3.1-orig/libbb/Kbuild busybox-1.3.1-913/libbb/Kbuild
217 --- busybox-1.3.1-orig/libbb/Kbuild     2006-12-27 05:55:04.000000000 +0100
218 +++ busybox-1.3.1-913/libbb/Kbuild      2006-12-27 23:31:20.000000000 +0100
219 @@ -37,6 +37,7 @@
220  lib-y += get_last_path_component.o
221  lib-y += get_line_from_file.o
222  lib-y += getopt32.o
223 +lib-y += hash.o
224  lib-y += herror_msg.o
225  lib-y += herror_msg_and_die.o
226  lib-y += human_readable.o