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