]> git.notmuchmail.org Git - notmuch/blob - lib/sha1.c
lib: Implement versioning in the database and provide upgrade function.
[notmuch] / lib / sha1.c
1 /* sha1.c - Interfaces to SHA-1 hash for the notmuch mail system
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22
23 #include "libsha1.h"
24
25 /* Just some simple interfaces on top of libsha1 so that we can leave
26  * libsha1 as untouched as possible. */
27
28 static char *
29 _hex_of_sha1_digest (const unsigned char digest[SHA1_DIGEST_SIZE])
30 {
31     char *result, *r;
32     int i;
33
34     result = xcalloc (SHA1_DIGEST_SIZE * 2 + 1, 1);
35
36     for (r = result, i = 0;
37          i < SHA1_DIGEST_SIZE;
38          r += 2, i++)
39     {
40         sprintf (r, "%02x", digest[i]);
41     }
42
43     return result;
44 }
45
46 /* Create a hexadecimal string version of the SHA-1 digest of 'str'
47  * (including its null terminating character).
48  *
49  * This function returns a newly allocated string which the caller
50  * should free() when finished.
51  */
52 char *
53 notmuch_sha1_of_string (const char *str)
54 {
55     sha1_ctx sha1;
56     unsigned char digest[SHA1_DIGEST_SIZE];
57
58     sha1_begin (&sha1);
59
60     sha1_hash ((unsigned char *) str, strlen (str) + 1, &sha1);
61
62     sha1_end (digest, &sha1);
63
64     return _hex_of_sha1_digest (digest);
65 }
66
67 /* Create a hexadecimal string version of the SHA-1 digest of the
68  * contents of the named file.
69  *
70  * This function returns a newly allocated string which the caller
71  * should free() when finished.
72  *
73  * If any error occurs while reading the file, (permission denied,
74  * file not found, etc.), this function returns NULL.
75  */
76 char *
77 notmuch_sha1_of_file (const char *filename)
78 {
79     FILE *file;
80 #define BLOCK_SIZE 4096
81     unsigned char block[BLOCK_SIZE];
82     size_t bytes_read;
83     sha1_ctx sha1;
84     unsigned char digest[SHA1_DIGEST_SIZE];
85     char *result;
86
87     file = fopen (filename, "r");
88     if (file == NULL)
89         return NULL;
90
91     sha1_begin (&sha1);
92
93     while (1) {
94         bytes_read = fread (block, 1, 4096, file);
95         if (bytes_read == 0) {
96             if (feof (file)) {
97                 break;
98             } else if (ferror (file)) {
99                 fclose (file);
100                 return NULL;
101             }
102         } else {
103             sha1_hash (block, bytes_read, &sha1);
104         }
105     }
106
107     sha1_end (digest, &sha1);
108
109     result = _hex_of_sha1_digest (digest);
110
111     fclose (file);
112
113     return result;
114 }
115