]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/check.c
Sort of garbage collection commit. :-| Many things are still
[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
17 extern LZMA_API lzma_bool
18 lzma_check_is_supported(lzma_check type)
19 {
20         if ((unsigned)(type) > LZMA_CHECK_ID_MAX)
21                 return false;
22
23         static const lzma_bool available_checks[LZMA_CHECK_ID_MAX + 1] = {
24                 true,   // LZMA_CHECK_NONE
25
26 #ifdef HAVE_CHECK_CRC32
27                 true,
28 #else
29                 false,
30 #endif
31
32                 false,  // Reserved
33                 false,  // Reserved
34
35 #ifdef HAVE_CHECK_CRC64
36                 true,
37 #else
38                 false,
39 #endif
40
41                 false,  // Reserved
42                 false,  // Reserved
43                 false,  // Reserved
44                 false,  // Reserved
45                 false,  // Reserved
46
47 #ifdef HAVE_CHECK_SHA256
48                 true,
49 #else
50                 false,
51 #endif
52
53                 false,  // Reserved
54                 false,  // Reserved
55                 false,  // Reserved
56                 false,  // Reserved
57                 false,  // Reserved
58         };
59
60         return available_checks[(unsigned)(type)];
61 }
62
63
64 extern LZMA_API uint32_t
65 lzma_check_size(lzma_check type)
66 {
67         if ((unsigned)(type) > LZMA_CHECK_ID_MAX)
68                 return UINT32_MAX;
69
70         // See file-format.txt section 2.1.1.2.
71         static const uint8_t check_sizes[LZMA_CHECK_ID_MAX + 1] = {
72                 0,
73                 4, 4, 4,
74                 8, 8, 8,
75                 16, 16, 16,
76                 32, 32, 32,
77                 64, 64, 64
78         };
79
80         return check_sizes[(unsigned)(type)];
81 }
82
83
84 extern void
85 lzma_check_init(lzma_check_state *check, lzma_check type)
86 {
87         switch (type) {
88         case LZMA_CHECK_NONE:
89                 break;
90
91 #ifdef HAVE_CHECK_CRC32
92         case LZMA_CHECK_CRC32:
93                 check->state.crc32 = 0;
94                 break;
95 #endif
96
97 #ifdef HAVE_CHECK_CRC64
98         case LZMA_CHECK_CRC64:
99                 check->state.crc64 = 0;
100                 break;
101 #endif
102
103 #ifdef HAVE_CHECK_SHA256
104         case LZMA_CHECK_SHA256:
105                 lzma_sha256_init(check);
106                 break;
107 #endif
108
109         default:
110                 break;
111         }
112
113         return;
114 }
115
116
117 extern void
118 lzma_check_update(lzma_check_state *check, lzma_check type,
119                 const uint8_t *buf, size_t size)
120 {
121         switch (type) {
122 #ifdef HAVE_CHECK_CRC32
123         case LZMA_CHECK_CRC32:
124                 check->state.crc32 = lzma_crc32(buf, size, check->state.crc32);
125                 break;
126 #endif
127
128 #ifdef HAVE_CHECK_CRC64
129         case LZMA_CHECK_CRC64:
130                 check->state.crc64 = lzma_crc64(buf, size, check->state.crc64);
131                 break;
132 #endif
133
134 #ifdef HAVE_CHECK_SHA256
135         case LZMA_CHECK_SHA256:
136                 lzma_sha256_update(buf, size, check);
137                 break;
138 #endif
139
140         default:
141                 break;
142         }
143
144         return;
145 }
146
147
148 extern void
149 lzma_check_finish(lzma_check_state *check, lzma_check type)
150 {
151         switch (type) {
152 #ifdef HAVE_CHECK_CRC32
153         case LZMA_CHECK_CRC32:
154                 check->buffer.u32[0] = integer_le_32(check->state.crc32);
155                 break;
156 #endif
157
158 #ifdef HAVE_CHECK_CRC64
159         case LZMA_CHECK_CRC64:
160                 check->buffer.u64[0] = integer_le_64(check->state.crc64);
161                 break;
162 #endif
163
164 #ifdef HAVE_CHECK_SHA256
165         case LZMA_CHECK_SHA256:
166                 lzma_sha256_finish(check);
167                 break;
168 #endif
169
170         default:
171                 break;
172         }
173
174         return;
175 }