]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/check.c
Update the code to mostly match the new simpler file format
[icculus/xz.git] / src / liblzma / check / check.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       check.c
4 /// \brief      Check sizes
5 //
6 //  This code has been put into the public domain.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #include "check.h"
15
16 // See the .lzma header format specification section 2.1.1.2.
17 LZMA_API const uint32_t lzma_check_sizes[LZMA_CHECK_ID_MAX + 1] = {
18         0,
19         4, 4, 4,
20         8, 8, 8,
21         16, 16, 16,
22         32, 32, 32,
23         64, 64, 64
24 };
25
26
27 LZMA_API const lzma_bool lzma_available_checks[LZMA_CHECK_ID_MAX + 1] = {
28         true,   // LZMA_CHECK_NONE
29
30 #ifdef HAVE_CHECK_CRC32
31         true,
32 #else
33         false,
34 #endif
35
36         false,  // Reserved
37         false,  // Reserved
38
39 #ifdef HAVE_CHECK_CRC64
40         true,
41 #else
42         false,
43 #endif
44
45         false,  // Reserved
46         false,  // Reserved
47         false,  // Reserved
48         false,  // Reserved
49         false,  // Reserved
50
51 #ifdef HAVE_CHECK_SHA256
52         true,
53 #else
54         false,
55 #endif
56
57         false,  // Reserved
58         false,  // Reserved
59         false,  // Reserved
60         false,  // Reserved
61         false,  // Reserved
62 };
63
64
65 extern lzma_ret
66 lzma_check_init(lzma_check *check, lzma_check_type type)
67 {
68         lzma_ret ret = LZMA_OK;
69
70         switch (type) {
71         case LZMA_CHECK_NONE:
72                 break;
73
74 #ifdef HAVE_CHECK_CRC32
75         case LZMA_CHECK_CRC32:
76                 check->state.crc32 = 0;
77                 break;
78 #endif
79
80 #ifdef HAVE_CHECK_CRC64
81         case LZMA_CHECK_CRC64:
82                 check->state.crc64 = 0;
83                 break;
84 #endif
85
86 #ifdef HAVE_CHECK_SHA256
87         case LZMA_CHECK_SHA256:
88                 lzma_sha256_init(check);
89                 break;
90 #endif
91
92         default:
93                 if ((unsigned)(type) <= LZMA_CHECK_ID_MAX)
94                         ret = LZMA_UNSUPPORTED_CHECK;
95                 else
96                         ret = LZMA_PROG_ERROR;
97                 break;
98         }
99
100         return ret;
101 }
102
103
104 extern void
105 lzma_check_update(lzma_check *check, lzma_check_type type,
106                 const uint8_t *buf, size_t size)
107 {
108         switch (type) {
109 #ifdef HAVE_CHECK_CRC32
110         case LZMA_CHECK_CRC32:
111                 check->state.crc32 = lzma_crc32(buf, size, check->state.crc32);
112                 break;
113 #endif
114
115 #ifdef HAVE_CHECK_CRC64
116         case LZMA_CHECK_CRC64:
117                 check->state.crc64 = lzma_crc64(buf, size, check->state.crc64);
118                 break;
119 #endif
120
121 #ifdef HAVE_CHECK_SHA256
122         case LZMA_CHECK_SHA256:
123                 lzma_sha256_update(buf, size, check);
124                 break;
125 #endif
126
127         default:
128                 break;
129         }
130
131         return;
132 }
133
134
135 extern void
136 lzma_check_finish(lzma_check *check, lzma_check_type type)
137 {
138         switch (type) {
139 #ifdef HAVE_CHECK_CRC32
140         case LZMA_CHECK_CRC32:
141                 *(uint32_t *)(check->buffer) = check->state.crc32;
142                 break;
143 #endif
144
145 #ifdef HAVE_CHECK_CRC64
146         case LZMA_CHECK_CRC64:
147                 *(uint64_t *)(check->buffer) = check->state.crc64;
148                 break;
149 #endif
150
151 #ifdef HAVE_CHECK_SHA256
152         case LZMA_CHECK_SHA256:
153                 lzma_sha256_finish(check);
154                 break;
155 #endif
156
157         default:
158                 break;
159         }
160
161         return;
162 }
163
164
165 /*
166 extern bool
167 lzma_check_compare(
168                 lzma_check *check1, lzma_check *check2, lzma_check_type type)
169 {
170         bool ret;
171
172         switch (type) {
173         case LZMA_CHECK_NONE:
174                 break;
175
176         case LZMA_CHECK_CRC32:
177                 ret = check1->crc32 != check2->crc32;
178                 break;
179
180         case LZMA_CHECK_CRC64:
181                 ret = check1->crc64 != check2->crc64;
182                 break;
183
184         default:
185                 // Unsupported check
186                 assert(type <= 7);
187                 ret = false;
188                 break;
189         }
190
191         return ret;
192 }
193 */