]> wimlib.net Git - wimlib/blob - include/wimlib/sha1.h
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / include / wimlib / sha1.h
1 /*
2  * sha1.h
3  *
4  * Copyright 2022-2023 Eric Biggers
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifndef _WIMLIB_SHA1_H
29 #define _WIMLIB_SHA1_H
30
31 #include <string.h>
32
33 #include "wimlib/types.h"
34 #include "wimlib/util.h"
35
36 #define SHA1_HASH_SIZE  20
37 #define SHA1_BLOCK_SIZE 64
38
39 struct sha1_ctx {
40         u64 bytecount;
41         u32 h[5];
42         u8 buffer[SHA1_BLOCK_SIZE];
43 };
44
45 void
46 sha1_init(struct sha1_ctx *ctx);
47
48 void
49 sha1_update(struct sha1_ctx *ctx, const void *data, size_t len);
50
51 void
52 sha1_final(struct sha1_ctx *ctx, u8 hash[SHA1_HASH_SIZE]);
53
54 void
55 sha1(const void *data, size_t len, u8 hash[SHA1_HASH_SIZE]);
56
57 extern const u8 zero_hash[SHA1_HASH_SIZE];
58
59 #define SHA1_HASH_STRING_LEN    (2 * SHA1_HASH_SIZE + 1)
60 void
61 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_STRING_LEN]);
62
63 static inline void
64 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
65 {
66         memcpy(dest, src, SHA1_HASH_SIZE);
67 }
68
69 static inline int
70 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
71 {
72         return memcmp(h1, h2, SHA1_HASH_SIZE);
73 }
74
75 static inline bool
76 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
77 {
78         return !hashes_cmp(h1, h2);
79 }
80
81 static inline bool
82 is_zero_hash(const u8 *hash)
83 {
84         return hash == zero_hash || hashes_equal(hash, zero_hash);
85 }
86
87 #endif /* _WIMLIB_SHA1_H */