• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Xz_WriteVarInt(Byte * buf,UInt64 v)14 unsigned Xz_WriteVarInt(Byte *buf, UInt64 v)
15 {
16   unsigned i = 0;
17   do
18   {
19     buf[i++] = (Byte)((v & 0x7F) | 0x80);
20     v >>= 7;
21   }
22   while (v != 0);
23   buf[(size_t)i - 1] &= 0x7F;
24   return i;
25 }
26 
Xz_Construct(CXzStream * p)27 void Xz_Construct(CXzStream *p)
28 {
29   p->numBlocks = 0;
30   p->blocks = NULL;
31   p->flags = 0;
32 }
33 
Xz_Free(CXzStream * p,ISzAllocPtr alloc)34 void Xz_Free(CXzStream *p, ISzAllocPtr alloc)
35 {
36   ISzAlloc_Free(alloc, p->blocks);
37   p->numBlocks = 0;
38   p->blocks = NULL;
39 }
40 
XzFlags_GetCheckSize(CXzStreamFlags f)41 unsigned XzFlags_GetCheckSize(CXzStreamFlags f)
42 {
43   unsigned t = XzFlags_GetCheckType(f);
44   return (t == 0) ? 0 : (4 << ((t - 1) / 3));
45 }
46 
XzCheck_Init(CXzCheck * p,unsigned mode)47 void XzCheck_Init(CXzCheck *p, unsigned mode)
48 {
49   p->mode = mode;
50   switch (mode)
51   {
52     case XZ_CHECK_CRC32: p->crc = CRC_INIT_VAL; break;
53     case XZ_CHECK_CRC64: p->crc64 = CRC64_INIT_VAL; break;
54     case XZ_CHECK_SHA256: Sha256_Init(&p->sha); break;
55   }
56 }
57 
XzCheck_Update(CXzCheck * p,const void * data,size_t size)58 void XzCheck_Update(CXzCheck *p, const void *data, size_t size)
59 {
60   switch (p->mode)
61   {
62     case XZ_CHECK_CRC32: p->crc = CrcUpdate(p->crc, data, size); break;
63     case XZ_CHECK_CRC64: p->crc64 = Crc64Update(p->crc64, data, size); break;
64     case XZ_CHECK_SHA256: Sha256_Update(&p->sha, (const Byte *)data, size); break;
65   }
66 }
67 
XzCheck_Final(CXzCheck * p,Byte * digest)68 int XzCheck_Final(CXzCheck *p, Byte *digest)
69 {
70   switch (p->mode)
71   {
72     case XZ_CHECK_CRC32:
73       SetUi32(digest, CRC_GET_DIGEST(p->crc));
74       break;
75     case XZ_CHECK_CRC64:
76     {
77       int i;
78       UInt64 v = CRC64_GET_DIGEST(p->crc64);
79       for (i = 0; i < 8; i++, v >>= 8)
80         digest[i] = (Byte)(v & 0xFF);
81       break;
82     }
83     case XZ_CHECK_SHA256:
84       Sha256_Final(&p->sha, digest);
85       break;
86     default:
87       return 0;
88   }
89   return 1;
90 }
91