ssl_tls: fix format warning in ssl_parse_certificate() on x86_64
[tropicssl.git] / programs / hash / md5sum.c
blob253d3fd7e6cfb191d96419cb474d03e3c548d1a8
1 /*
2 * md5sum demonstration program
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _CRT_SECURE_NO_DEPRECATE
37 #define _CRT_SECURE_NO_DEPRECATE 1
38 #endif
40 #include <string.h>
41 #include <stdio.h>
43 #include "tropicssl/md5.h"
45 static int md5_wrapper(char *filename, unsigned char *sum)
47 int ret = md5_file(filename, sum);
49 if (ret == 1)
50 fprintf(stderr, "failed to open: %s\n", filename);
52 if (ret == 2)
53 fprintf(stderr, "failed to read: %s\n", filename);
55 return (ret);
58 static int md5_print(char *filename)
60 int i;
61 unsigned char sum[16];
63 if (md5_wrapper(filename, sum) != 0)
64 return (1);
66 for (i = 0; i < 16; i++)
67 printf("%02x", sum[i]);
69 printf(" %s\n", filename);
70 return (0);
73 static int md5_check(char *filename)
75 int i;
76 size_t n;
77 FILE *f;
78 int nb_err1, nb_err2;
79 int nb_tot1, nb_tot2;
80 unsigned char sum[16];
81 char buf[33], line[1024];
83 if ((f = fopen(filename, "rb")) == NULL) {
84 printf("failed to open: %s\n", filename);
85 return (1);
88 nb_err1 = nb_err2 = 0;
89 nb_tot1 = nb_tot2 = 0;
91 memset(line, 0, sizeof(line));
93 n = sizeof(line);
95 while (fgets(line, n - 1, f) != NULL) {
96 n = strlen(line);
98 if (n < 36)
99 continue;
101 if (line[32] != ' ' || line[33] != ' ')
102 continue;
104 if (line[n - 1] == '\n') {
105 n--;
106 line[n] = '\0';
108 if (line[n - 1] == '\r') {
109 n--;
110 line[n] = '\0';
113 nb_tot1++;
115 if (md5_wrapper(line + 34, sum) != 0) {
116 nb_err1++;
117 continue;
120 nb_tot2++;
122 for (i = 0; i < 16; i++)
123 sprintf(buf + i * 2, "%02x", sum[i]);
125 if (memcmp(line, buf, 32) != 0) {
126 nb_err2++;
127 fprintf(stderr, "wrong checksum: %s\n", line + 34);
130 n = sizeof(line);
133 if (nb_err1 != 0) {
134 printf("WARNING: %d (out of %d) input files could "
135 "not be read\n", nb_err1, nb_tot1);
138 if (nb_err2 != 0) {
139 printf("WARNING: %d (out of %d) computed checksums did "
140 "not match\n", nb_err2, nb_tot2);
143 return (nb_err1 != 0 || nb_err2 != 0);
146 int main(int argc, char *argv[])
148 int ret, i;
150 if (argc == 1) {
151 printf("print mode: md5sum <file> <file> ...\n");
152 printf("check mode: md5sum -c <checksum file>\n");
154 #ifdef WIN32
155 printf("\n Press Enter to exit this program.\n");
156 fflush(stdout);
157 getchar();
158 #endif
160 return (1);
163 if (argc == 3 && strcmp("-c", argv[1]) == 0)
164 return (md5_check(argv[2]));
166 ret = 0;
167 for (i = 1; i < argc; i++)
168 ret |= md5_print(argv[i]);
170 return (ret);