1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/*
* jigsum
*
* Tool to calculate and print MD5 checksums in jigdo's awkward
* base64-ish encoding.
*
* Copyright (c) 2004 Steve McIntyre <steve@einval.com>
*
* GPL v2 - see COPYING
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "md5.h"
#define BUF_SIZE 65536
#ifndef MIN
#define MIN(x,y) ( ((x) < (y)) ? (x) : (y))
#endif
static int md5_file(char *filename)
{
FILE *file = NULL;
char buf[BUF_SIZE];
unsigned char file_md5[16] = {0};
char *base64_md5 = NULL;
struct mk_MD5Context file_context;
int done = 0;
int bytes_read = 0;
mk_MD5Init(&file_context);
/* Check if we're reading from stdin */
if (!strcmp("-", filename))
file = stdin;
else
{
fprintf(stderr, "\r %-75.75s", filename);
file = fopen(filename, "rb");
if (!file)
{
switch (errno)
{
case EACCES:
case EISDIR:
break;
default:
fprintf(stderr, "Unable to open file %s; error %d\n", filename, errno);
break;
}
return errno;
}
}
while (!done)
{
int used = 0;
memset(buf, 0, BUF_SIZE);
used = fread(buf, 1, BUF_SIZE, file);
bytes_read += used;
if (used)
mk_MD5Update(&file_context, (unsigned char *)buf, used);
else
{
if (ferror(file) && (EISDIR != errno))
fprintf(stderr, "Unable to read from file %s; error %d\n",
filename, errno);
break;
}
}
mk_MD5Final(file_md5, &file_context);
base64_md5 = base64_dump(file_md5, 16);
if (file != stdin)
{
fclose(file);
if (bytes_read)
printf("%s %s\n", base64_md5, filename);
}
else
if (bytes_read)
printf("%s\n", base64_md5);
fflush(stdout);
return 0;
}
int main(int argc, char **argv)
{
int i = 0;
for (i = 1; i < argc; i++)
(void) md5_file(argv[i]);
return 0;
}
|