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