]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/check.c
Imported to git.
[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.2.2.
17 LZMA_API const uint32_t lzma_check_sizes[8] = { 0, 4, 4, 8, 16, 32, 32, 64 };
18
19
20 LZMA_API const lzma_bool lzma_available_checks[LZMA_CHECK_ID_MAX + 1] = {
21         true,   // LZMA_CHECK_NONE
22
23 #ifdef HAVE_CHECK_CRC32
24         true,
25 #else
26         false,
27 #endif
28
29         false,  // Reserved
30
31 #ifdef HAVE_CHECK_CRC64
32         true,
33 #else
34         false,
35 #endif
36
37         false,  // Reserved
38
39 #ifdef HAVE_CHECK_SHA256
40         true,
41 #else
42         false,
43 #endif
44
45         false,  // Reserved
46         false,  // Reserved
47 };
48
49
50 extern lzma_ret
51 lzma_check_init(lzma_check *check, lzma_check_type type)
52 {
53         lzma_ret ret = LZMA_OK;
54
55         switch (type) {
56         case LZMA_CHECK_NONE:
57                 break;
58
59 #ifdef HAVE_CHECK_CRC32
60         case LZMA_CHECK_CRC32:
61                 check->crc32 = 0;
62                 break;
63 #endif
64
65 #ifdef HAVE_CHECK_CRC64
66         case LZMA_CHECK_CRC64:
67                 check->crc64 = 0;
68                 break;
69 #endif
70
71 #ifdef HAVE_CHECK_SHA256
72         case LZMA_CHECK_SHA256:
73                 lzma_sha256_init(&check->sha256);
74                 break;
75 #endif
76
77         default:
78                 if (type <= LZMA_CHECK_ID_MAX)
79                         ret = LZMA_UNSUPPORTED_CHECK;
80                 else
81                         ret = LZMA_PROG_ERROR;
82                 break;
83         }
84
85         return ret;
86 }
87
88
89 extern void
90 lzma_check_update(lzma_check *check, lzma_check_type type,
91                 const uint8_t *buf, size_t size)
92 {
93         switch (type) {
94 #ifdef HAVE_CHECK_CRC32
95         case LZMA_CHECK_CRC32:
96                 check->crc32 = lzma_crc32(buf, size, check->crc32);
97                 break;
98 #endif
99
100 #ifdef HAVE_CHECK_CRC64
101         case LZMA_CHECK_CRC64:
102                 check->crc64 = lzma_crc64(buf, size, check->crc64);
103                 break;
104 #endif
105
106 #ifdef HAVE_CHECK_SHA256
107         case LZMA_CHECK_SHA256:
108                 lzma_sha256_update(buf, size, &check->sha256);
109                 break;
110 #endif
111
112         default:
113                 break;
114         }
115
116         return;
117 }
118
119
120 extern void
121 lzma_check_finish(lzma_check *check, lzma_check_type type)
122 {
123 #ifdef HAVE_CHECK_SHA256
124         if (type == LZMA_CHECK_SHA256)
125                 lzma_sha256_finish(&check->sha256);
126 #endif
127
128         return;
129 }
130
131
132 /*
133 extern bool
134 lzma_check_compare(
135                 lzma_check *check1, lzma_check *check2, lzma_check_type type)
136 {
137         bool ret;
138
139         switch (type) {
140         case LZMA_CHECK_NONE:
141                 break;
142
143         case LZMA_CHECK_CRC32:
144                 ret = check1->crc32 != check2->crc32;
145                 break;
146
147         case LZMA_CHECK_CRC64:
148                 ret = check1->crc64 != check2->crc64;
149                 break;
150
151         default:
152                 // Unsupported check
153                 assert(type <= 7);
154                 ret = false;
155                 break;
156         }
157
158         return ret;
159 }
160 */