]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/check.h
1dfc4d7390de692d904e3f975783bc89433b58d2
[icculus/xz.git] / src / liblzma / check / check.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       check.h
4 /// \brief      Internal API to different integrity check functions
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 #ifndef LZMA_CHECK_H
15 #define LZMA_CHECK_H
16
17 #include "common.h"
18
19
20 // Index hashing needs the best possible hash function (preferably
21 // a cryptographic hash) for maximum reliability.
22 #if defined(HAVE_CHECK_SHA256)
23 #       define LZMA_CHECK_BEST LZMA_CHECK_SHA256
24 #elif defined(HAVE_CHECK_CRC64)
25 #       define LZMA_CHECK_BEST LZMA_CHECK_CRC64
26 #else
27 #       define LZMA_CHECK_BEST LZMA_CHECK_CRC32
28 #endif
29
30
31 /// \brief      Structure to hold internal state of the check being calculated
32 ///
33 /// \note       This is not in the public API because this structure may
34 ///             change in future if new integrity check algorithms are added.
35 typedef struct {
36         /// Buffer to hold the final result and a temporary buffer for SHA256.
37         union {
38                 uint8_t u8[64];
39                 uint32_t u32[16];
40                 uint64_t u64[8];
41         } buffer;
42
43         /// Check-specific data
44         union {
45                 uint32_t crc32;
46                 uint64_t crc64;
47
48                 struct {
49                         /// Internal state
50                         uint32_t state[8];
51
52                         /// Size of the message excluding padding
53                         uint64_t size;
54                 } sha256;
55         } state;
56
57 } lzma_check_state;
58
59
60 /// lzma_crc32_table[0] is needed by LZ encoder so we need to keep
61 /// the array two-dimensional.
62 #ifdef HAVE_SMALL
63 extern uint32_t lzma_crc32_table[1][256];
64 extern void lzma_crc32_init(void);
65 #else
66 extern const uint32_t lzma_crc32_table[8][256];
67 extern const uint64_t lzma_crc64_table[4][256];
68 #endif
69
70
71 /// \brief      Initialize *check depending on type
72 ///
73 /// \return     LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
74 ///             supported by the current version or build of liblzma.
75 ///             LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
76 extern void lzma_check_init(lzma_check_state *check, lzma_check type);
77
78 /// Update the check state
79 extern void lzma_check_update(lzma_check_state *check, lzma_check type,
80                 const uint8_t *buf, size_t size);
81
82 /// Finish the check calculation and store the result to check->buffer.u8.
83 extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
84
85
86 /// Prepare SHA-256 state for new input.
87 extern void lzma_sha256_init(lzma_check_state *check);
88
89 /// Update the SHA-256 hash state
90 extern void lzma_sha256_update(
91                 const uint8_t *buf, size_t size, lzma_check_state *check);
92
93 /// Finish the SHA-256 calculation and store the result to check->buffer.u8.
94 extern void lzma_sha256_finish(lzma_check_state *check);
95
96 #endif