Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.9 KiB

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