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