Project.pm: tighten up is_empty check
[girocco/readme.git] / src / ltsha1.c
blob881e1cf85033fd0db83a9ca2190ba68b6e82ae2a
1 /* License: public domain -or- http://www.wtfpl.net/txt/copying/ */
3 #include "lt1.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <unistd.h>
9 #define READSIZE 32768
11 static const char hextab[16] = {
12 '0', '1', '2', '3', '4', '5', '6', '7',
13 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
15 static char buffer[READSIZE];
17 int main(int arc, char *argv[])
19 SHA1_CTX c;
20 unsigned char md[SHA1_DIGEST_LENGTH];
21 char mdhex[(2*SHA1_DIGEST_LENGTH)+1];
22 ssize_t e;
23 unsigned i;
25 if (!SHA1_Init(&c))
26 return EXIT_FAILURE;
28 for (;;) {
29 do {
30 e = read(STDIN_FILENO, buffer, READSIZE);
31 } while (e == -1 &&
32 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
33 if (e < 0)
34 return EXIT_FAILURE;
35 if (!e)
36 break;
37 if (!SHA1_Update(&c, buffer, (size_t)e))
38 return EXIT_FAILURE;
40 if (!SHA1_Final(md, &c))
41 return 0;
42 for (i=0; i < SHA1_DIGEST_LENGTH; ++i) {
43 unsigned char c = md[i];
44 mdhex[i<<1] = hextab[c >> 4];
45 mdhex[(i<<1)+1] = hextab[c & 0xf];
47 mdhex[2*SHA1_DIGEST_LENGTH] = '\0';
48 if (puts(mdhex) < 0)
49 return EXIT_FAILURE;
51 return 0;
54 #include "lt1.c"