Add macosx fix from #1407
[openwrt.git] / package / busybox / patches / 913-libbb_hash.patch
1 diff -ur busybox.old/coreutils/md5_sha1_sum.c busybox.dev/coreutils/md5_sha1_sum.c
2 --- busybox.old/coreutils/md5_sha1_sum.c        2007-01-19 22:23:05.000000000 +0100
3 +++ busybox.dev/coreutils/md5_sha1_sum.c        2007-01-22 13:24:51.000000000 +0100
4 @@ -8,76 +8,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 hash_length)
17 -{
18 -       /* xzalloc zero-terminates */
19 -       char *hex_value = xzalloc((hash_length * 2) + 1);
20 -       bin2hex(hex_value, (char*)hash_value, hash_length);
21 -       return hex_value;
22 -}
23 -
24 -static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
25 -{
26 -       int src_fd, hash_len, count;
27 -       union _ctx_ {
28 -               sha1_ctx_t sha1;
29 -               md5_ctx_t md5;
30 -       } context;
31 -       uint8_t *hash_value = NULL;
32 -       RESERVE_CONFIG_UBUFFER(in_buf, 4096);
33 -       void (*update)(const void*, size_t, void*);
34 -       void (*final)(void*, void*);
35 -
36 -       src_fd = STDIN_FILENO;
37 -       if (NOT_LONE_DASH(filename)) {
38 -               src_fd = open(filename, O_RDONLY);
39 -               if (src_fd < 0) {
40 -                       bb_perror_msg("%s", filename);
41 -                       return NULL;
42 -               }
43 -       }
44 -
45 -       /* figure specific hash algorithims */
46 -       if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
47 -               md5_begin(&context.md5);
48 -               update = (void (*)(const void*, size_t, void*))md5_hash;
49 -               final = (void (*)(void*, void*))md5_end;
50 -               hash_len = 16;
51 -       } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
52 -               sha1_begin(&context.sha1);
53 -               update = (void (*)(const void*, size_t, void*))sha1_hash;
54 -               final = (void (*)(void*, void*))sha1_end;
55 -               hash_len = 20;
56 -       } else {
57 -               bb_error_msg_and_die("algorithm not supported");
58 -       }
59 -
60 -       while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
61 -               update(in_buf, count, &context);
62 -       }
63 -
64 -       if (count == 0) {
65 -               final(in_buf, &context);
66 -               hash_value = hash_bin_to_hex(in_buf, hash_len);
67 -       }
68 -
69 -       RELEASE_CONFIG_BUFFER(in_buf);
70 -
71 -       if (src_fd != STDIN_FILENO) {
72 -               close(src_fd);
73 -       }
74 -
75 -       return hash_value;
76 -}
77 -
78  int md5_sha1_sum_main(int argc, char **argv)
79  {
80         int return_value = EXIT_SUCCESS;
81 diff -ur busybox.old/include/libbb.h busybox.dev/include/libbb.h
82 --- busybox.old/include/libbb.h 2007-01-19 22:23:10.000000000 +0100
83 +++ busybox.dev/include/libbb.h 2007-01-22 13:28:56.000000000 +0100
84 @@ -637,6 +637,7 @@
85  extern const char bb_uuenc_tbl_std[];
86  void bb_uuencode(const unsigned char *s, char *store, const int length, const char *tbl);
87  
88 +typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
89  typedef struct sha1_ctx_t {
90         uint32_t count[2];
91         uint32_t hash[5];
92 @@ -658,6 +659,8 @@
93  void md5_begin(md5_ctx_t *ctx);
94  void md5_hash(const void *data, size_t length, md5_ctx_t *ctx);
95  void *md5_end(void *resbuf, md5_ctx_t *ctx);
96 +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned hash_length);
97 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo);
98  
99  uint32_t *crc32_filltable(int endian);
100  
101 diff -ur busybox.old/libbb/Kbuild busybox.dev/libbb/Kbuild
102 --- busybox.old/libbb/Kbuild    2007-01-19 22:23:06.000000000 +0100
103 +++ busybox.dev/libbb/Kbuild    2007-01-22 13:29:24.000000000 +0100
104 @@ -37,6 +37,7 @@
105  lib-y += get_last_path_component.o
106  lib-y += get_line_from_file.o
107  lib-y += getopt32.o
108 +lib-y += hash.o
109  lib-y += herror_msg.o
110  lib-y += herror_msg_and_die.o
111  lib-y += human_readable.o
112 --- busybox.old/libbb/hash.c    1970-01-01 01:00:00.000000000 +0100
113 +++ busybox.dev/libbb/hash.c    2007-01-22 13:52:41.000000000 +0100
114 @@ -0,0 +1,82 @@
115 +/*
116 + *  Copyright (C) 2003 Glenn L. McGrath
117 + *  Copyright (C) 2003-2004 Erik Andersen
118 + *
119 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
120 + */
121 +
122 +#include <fcntl.h>
123 +#include <limits.h>
124 +#include <stdio.h>
125 +#include <stdint.h>
126 +#include <stdlib.h>
127 +#include <string.h>
128 +#include <unistd.h>
129 +
130 +#include "busybox.h"
131 +
132 +/* This might be useful elsewhere */
133 +unsigned char *hash_bin_to_hex(unsigned char *hash_value,
134 +                               unsigned hash_length)
135 +{
136 +       /* xzalloc zero-terminates */
137 +       char *hex_value = xzalloc((hash_length * 2) + 1);
138 +       bin2hex(hex_value, (char*)hash_value, hash_length);
139 +       return hex_value;
140 +}
141 +
142 +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
143 +{
144 +       int src_fd, hash_len, count;
145 +       union _ctx_ {
146 +               sha1_ctx_t sha1;
147 +               md5_ctx_t md5;
148 +       } context;
149 +       uint8_t *hash_value = NULL;
150 +       RESERVE_CONFIG_UBUFFER(in_buf, 4096);
151 +       void (*update)(const void*, size_t, void*);
152 +       void (*final)(void*, void*);
153 +
154 +       src_fd = STDIN_FILENO;
155 +       if (NOT_LONE_DASH(filename)) {
156 +               src_fd = open(filename, O_RDONLY);
157 +               if (src_fd < 0) {
158 +                       bb_perror_msg("%s", filename);
159 +                       return NULL;
160 +               }
161 +       }
162 +
163 +       /* figure specific hash algorithims */
164 +       if (hash_algo==HASH_MD5) {
165 +               md5_begin(&context.md5);
166 +               update = (void (*)(const void*, size_t, void*))md5_hash;
167 +               final = (void (*)(void*, void*))md5_end;
168 +               hash_len = 16;
169 +       } else if (hash_algo==HASH_SHA1) {
170 +               sha1_begin(&context.sha1);
171 +               update = (void (*)(const void*, size_t, void*))sha1_hash;
172 +               final = (void (*)(void*, void*))sha1_end;
173 +               hash_len = 20;
174 +       } else {
175 +               bb_error_msg_and_die("algorithm not supported");
176 +       }
177 +
178 +       while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
179 +               update(in_buf, count, &context);
180 +       }
181 +
182 +       if (count == 0) {
183 +               final(in_buf, &context);
184 +               hash_value = hash_bin_to_hex(in_buf, hash_len);
185 +       }
186 +
187 +       RELEASE_CONFIG_BUFFER(in_buf);
188 +
189 +       if (src_fd != STDIN_FILENO) {
190 +               close(src_fd);
191 +       }
192 +
193 +       return hash_value;
194 +}
195 +
196 +