/[debburn]/cdrkit/trunk/genisoimage/checksum.c
ViewVC logotype

Contents of /cdrkit/trunk/genisoimage/checksum.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 806 - (show annotations) (download)
Sun May 25 20:46:17 2008 UTC (4 years, 11 months ago) by 93sam
File MIME type: text/plain
File size: 6002 byte(s)
genisoimage/checksum.[ch]: Added test code; changed internal layout
slightly to make for easier debug.

1 /*
2 * checksum.c
3 *
4 * Copyright (c) 2008- Steve McIntyre <steve@einval.com>
5 *
6 * Implementation of a generic checksum interface, used in JTE.
7 *
8 * GNU GPL v2
9 */
10
11 #include <mconfig.h>
12 #include "genisoimage.h"
13 #include <timedefs.h>
14 #include <fctldefs.h>
15 #include <regex.h>
16 #include <stdlib.h>
17 #include "md5.h"
18 #include "sha1.h"
19 #include "checksum.h"
20
21 static void md5_init(void *context)
22 {
23 mk_MD5Init(context);
24 }
25 static void md5_update(void *context, unsigned char const *buf, unsigned int len)
26 {
27 mk_MD5Update(context, buf, len);
28 }
29 static void md5_final(unsigned char *digest, void *context)
30 {
31 mk_MD5Final(digest, context);
32 }
33
34 static void sha1_init(void *context)
35 {
36 sha1_init_ctx(context);
37 }
38 static void sha1_update(void *context, unsigned char const *buf, unsigned int len)
39 {
40 sha1_process_bytes(buf, len, context);
41 }
42 static void sha1_final(unsigned char *digest, void *context)
43 {
44 sha1_finish_ctx(context, digest);
45 }
46
47 struct checksum_details
48 {
49 char *name;
50 char *prog;
51 int digest_size;
52 int context_size;
53 void (*init)(void *context);
54 void (*update)(void *context, unsigned char const *buf, unsigned int len);
55 void (*final)(unsigned char *digest, void *context);
56 };
57
58 static const struct checksum_details algorithms[] =
59 {
60 {
61 "MD5",
62 "md5sum",
63 16,
64 sizeof(struct mk_MD5Context),
65 md5_init,
66 md5_update,
67 md5_final
68 },
69 {
70 "SHA1",
71 "sha1sum",
72 20,
73 sizeof(struct sha1_ctx),
74 sha1_init,
75 sha1_update,
76 sha1_final
77 }
78 };
79
80 struct algo_context
81 {
82 void *context;
83 unsigned char *digest;
84 int enabled;
85 };
86
87 struct _checksum_context
88 {
89 char *owner;
90 struct algo_context algo[NUM_CHECKSUMS];
91 };
92
93 struct checksum_info *checksum_information(enum checksum_types which)
94 {
95 return (struct checksum_info *)&algorithms[which];
96 }
97
98 checksum_context_t *checksum_init_context(int checksums, const char *owner)
99 {
100 int i = 0;
101 struct _checksum_context *context = malloc(sizeof(struct _checksum_context));
102 if (!context)
103 return NULL;
104
105 context->owner = strdup(owner);
106 if (!context->owner)
107 {
108 free(context);
109 return NULL;
110 }
111
112 for (i = 0; i < NUM_CHECKSUMS; i++)
113 {
114 if ( (1 << i) & checksums)
115 {
116 context->algo[i].context = malloc(algorithms[i].context_size);
117 if (!context->algo[i].context)
118 return NULL;
119 context->algo[i].digest = malloc(algorithms[i].digest_size);
120 if (!context->algo[i].digest)
121 return NULL;
122 algorithms[i].init(context->algo[i].context);
123 context->algo[i].enabled = 1;
124 }
125 else
126 context->algo[i].enabled = 0;
127 }
128
129 return context;
130 }
131
132 void checksum_free_context(checksum_context_t *context)
133 {
134 int i = 0;
135 struct _checksum_context *c = context;
136
137 for (i = 0; i < NUM_CHECKSUMS; i++)
138 {
139 free(c->algo[i].context);
140 free(c->algo[i].digest);
141 }
142 free(c->owner);
143 free(c);
144 }
145
146 void checksum_update(checksum_context_t *context,
147 unsigned char const *buf, unsigned int len)
148 {
149 int i = 0;
150 struct _checksum_context *c = context;
151
152 for (i = 0; i < NUM_CHECKSUMS; i++)
153 {
154 if (c->algo[i].enabled)
155 algorithms[i].update(c->algo[i].context, buf, len);
156 }
157 }
158
159 void checksum_final(checksum_context_t *context)
160 {
161 int i = 0;
162 struct _checksum_context *c = context;
163
164 for (i = 0; i < NUM_CHECKSUMS; i++)
165 {
166 if (c->algo[i].enabled)
167 algorithms[i].final(c->algo[i].digest, c->algo[i].context);
168 }
169 }
170
171 void checksum_copy(checksum_context_t *context,
172 enum checksum_types which,
173 unsigned char *digest)
174 {
175 struct _checksum_context *c = context;
176
177 if (c->algo[which].enabled)
178 memcpy(digest, c->algo[which].digest, algorithms[which].digest_size);
179 else
180 fprintf(stderr, "Asked for %s checksum, not enabled!\n",
181 algorithms[which].name);
182 }
183
184 #ifdef CHECKSUM_SELF_TEST
185 #include <sys/types.h>
186 #include <sys/stat.h>
187 #include <fcntl.h>
188 #include <unistd.h>
189 #include <errno.h>
190 #include <stdlib.h>
191
192 int main(int argc, char **argv)
193 {
194 char buf[1024];
195 int fd = -1;
196 char *filename;
197 int err = 0;
198 static checksum_context_t *test_context = NULL;
199 int i = 0;
200
201 if (argc != 2)
202 {
203 fprintf(stderr, "Need a filename to act on!\n");
204 return 1;
205 }
206
207 filename = argv[1];
208 fd = open(filename, O_RDONLY);
209 if (fd < 0)
210 {
211 fprintf(stderr, "Unable to open file %s, errno %d\n", filename, errno);
212 return 1;
213 }
214
215 test_context = checksum_init_context(CHECK_ALL_USED, "test");
216 if (!test_context)
217 {
218 fprintf(stderr, "Unable to initialise checksum context\n");
219 return 1;
220 }
221
222 while(1)
223 {
224 err = read(fd, buf, sizeof(buf));
225 if (err < 0)
226 {
227 fprintf(stderr, "Failed to read from file, errno %d\n", errno);
228 return 1;
229 }
230
231 if (err == 0)
232 break; // EOF
233
234 /* else */
235 checksum_update(test_context, buf, err);
236 }
237 close(fd);
238 checksum_final(test_context);
239
240 for (i = 0; i < NUM_CHECKSUMS; i++)
241 {
242 struct checksum_info *info;
243 unsigned char r[64];
244 int j = 0;
245
246 info = checksum_information(i);
247 memset(r, 0, sizeof(r));
248
249 checksum_copy(test_context, i, r);
250
251 printf("OUR %s:\n", info->name);
252 for (j = 0; j < info->digest_size; j++)
253 printf("%2.2x", r[j]);
254 printf(" %s\n", filename);
255 printf("system checksum program (%s):\n", info->prog);
256 sprintf(buf, "%s %s", info->prog, filename);
257 system(buf);
258 printf("\n");
259 }
260 return 0;
261 }
262 #endif /* CHECKSUM_SELF_TEST */
263

  ViewVC Help
Powered by ViewVC 1.1.5