/src/lzma-fuzz/sdk/C/Xz.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Xz.c - Xz |
2 | | 2017-05-12 : Igor Pavlov : Public domain */ |
3 | | |
4 | | #include "Precomp.h" |
5 | | |
6 | | #include "7zCrc.h" |
7 | | #include "CpuArch.h" |
8 | | #include "Xz.h" |
9 | | #include "XzCrc64.h" |
10 | | |
11 | | const Byte XZ_SIG[XZ_SIG_SIZE] = { 0xFD, '7', 'z', 'X', 'Z', 0 }; |
12 | | /* const Byte XZ_FOOTER_SIG[XZ_FOOTER_SIG_SIZE] = { 'Y', 'Z' }; */ |
13 | | |
14 | | unsigned Xz_WriteVarInt(Byte *buf, UInt64 v) |
15 | 123k | { |
16 | 123k | unsigned i = 0; |
17 | 123k | do |
18 | 208k | { |
19 | 208k | buf[i++] = (Byte)((v & 0x7F) | 0x80); |
20 | 208k | v >>= 7; |
21 | 208k | } |
22 | 208k | while (v != 0); |
23 | 123k | buf[(size_t)i - 1] &= 0x7F; |
24 | 123k | return i; |
25 | 123k | } |
26 | | |
27 | | void Xz_Construct(CXzStream *p) |
28 | 0 | { |
29 | 0 | p->numBlocks = 0; |
30 | 0 | p->blocks = NULL; |
31 | 0 | p->flags = 0; |
32 | 0 | } |
33 | | |
34 | | void Xz_Free(CXzStream *p, ISzAllocPtr alloc) |
35 | 0 | { |
36 | 0 | ISzAlloc_Free(alloc, p->blocks); |
37 | 0 | p->numBlocks = 0; |
38 | 0 | p->blocks = NULL; |
39 | 0 | } |
40 | | |
41 | | unsigned XzFlags_GetCheckSize(CXzStreamFlags f) |
42 | 120k | { |
43 | 120k | unsigned t = XzFlags_GetCheckType(f); |
44 | 120k | return (t == 0) ? 0 : (4 << ((t - 1) / 3)); |
45 | 120k | } |
46 | | |
47 | | void XzCheck_Init(CXzCheck *p, unsigned mode) |
48 | 66.0k | { |
49 | 66.0k | p->mode = mode; |
50 | 66.0k | switch (mode) |
51 | 66.0k | { |
52 | 1.64k | case XZ_CHECK_CRC32: p->crc = CRC_INIT_VAL; break; |
53 | 5.02k | case XZ_CHECK_CRC64: p->crc64 = CRC64_INIT_VAL; break; |
54 | 1.68k | case XZ_CHECK_SHA256: Sha256_Init(&p->sha); break; |
55 | 66.0k | } |
56 | 66.0k | } |
57 | | |
58 | | void XzCheck_Update(CXzCheck *p, const void *data, size_t size) |
59 | 71.2k | { |
60 | 71.2k | switch (p->mode) |
61 | 71.2k | { |
62 | 2.20k | case XZ_CHECK_CRC32: p->crc = CrcUpdate(p->crc, data, size); break; |
63 | 7.75k | case XZ_CHECK_CRC64: p->crc64 = Crc64Update(p->crc64, data, size); break; |
64 | 2.31k | case XZ_CHECK_SHA256: Sha256_Update(&p->sha, (const Byte *)data, size); break; |
65 | 71.2k | } |
66 | 71.2k | } |
67 | | |
68 | | int XzCheck_Final(CXzCheck *p, Byte *digest) |
69 | 59.9k | { |
70 | 59.9k | switch (p->mode) |
71 | 59.9k | { |
72 | 1.02k | case XZ_CHECK_CRC32: |
73 | 1.02k | SetUi32(digest, CRC_GET_DIGEST(p->crc)); |
74 | 1.02k | break; |
75 | 1.51k | case XZ_CHECK_CRC64: |
76 | 1.51k | { |
77 | 1.51k | int i; |
78 | 1.51k | UInt64 v = CRC64_GET_DIGEST(p->crc64); |
79 | 13.6k | for (i = 0; i < 8; i++, v >>= 8) |
80 | 12.1k | digest[i] = (Byte)(v & 0xFF); |
81 | 1.51k | break; |
82 | 0 | } |
83 | 884 | case XZ_CHECK_SHA256: |
84 | 884 | Sha256_Final(&p->sha, digest); |
85 | 884 | break; |
86 | 56.4k | default: |
87 | 56.4k | return 0; |
88 | 59.9k | } |
89 | 3.41k | return 1; |
90 | 59.9k | } |