xzstream.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*++
  2. Copyright (c) Alex Ionescu. All rights reserved.
  3. Module Name:
  4. xzstream.h
  5. Abstract:
  6. This header file contains C-style data structures and enumerations that map
  7. back to the XZ stream and file format standard, including for the decoding
  8. of Variable Length Integers (VLI). This includes definitions for the stream
  9. header, block header, index and stream footer, and associated check types.
  10. Author:
  11. Alex Ionescu (@aionescu) 15-Apr-2020 - Initial version
  12. Environment:
  13. Windows & Linux, user mode and kernel mode.
  14. --*/
  15. #pragma once
  16. #ifdef _MSC_VER
  17. #pragma warning(disable:4214)
  18. #endif
  19. //
  20. // This is the filter ID for LZMA2 as part of an XZ block header's "LzmaFlags"
  21. //
  22. const uint8_t k_XzLzma2FilterIdentifier = 0x21;
  23. //
  24. // These are the magic bytes at the beginning of an XZ stream footer
  25. //
  26. const uint16_t k_XzStreamFooterMagic = 0x595a;//'ZY';
  27. //
  28. // These are the magic bytes at the beginning of an XZ stream header
  29. //
  30. const uint8_t k_XzStreamHeaderMagic0 = 0xFD;
  31. const uint32_t k_XzStreamHeaderMagic1 = 0x377a585a;//'ZXz7';
  32. const uint8_t k_XzStreamHeaderMagic5 = 0x00;
  33. //
  34. // XZ Blocks can be checksumed with algorithms of one of these possible sizes,
  35. // based on the 4 bits indicated in the "CheckType" field of the stream header.
  36. //
  37. const uint8_t k_XzBlockCheckSizes[] =
  38. {
  39. 0, 4, 4, 4, 8, 8, 8, 16, 16, 16, 32, 32, 32, 64, 64, 64
  40. };
  41. //
  42. // XZ streams encode certain numbers as "variable length integers", with 7 bits
  43. // for the data, and a high bit to encode that another byte must be consumed.
  44. //
  45. typedef uint32_t vli_type;
  46. #define VLI_BYTES_MAX (sizeof(vli_type) * 8 / 7)
  47. //
  48. // These are the possible supported types for integrity checking in an XZ file
  49. //
  50. typedef enum _XZ_CHECK_TYPES
  51. {
  52. XzCheckTypeNone = 0,
  53. XzCheckTypeCrc32 = 1,
  54. XzCheckTypeCrc64 = 4,
  55. XzCheckTypeSha2 = 10
  56. } XZ_CHECK_TYPES;
  57. //
  58. // This describes the first 12 bytes of any XZ container file / stream
  59. //
  60. typedef struct _XZ_STREAM_HEADER
  61. {
  62. uint8_t Magic[6];
  63. union
  64. {
  65. struct
  66. {
  67. uint8_t ReservedFlags;
  68. uint8_t CheckType : 4;
  69. uint8_t ReservedType : 4;
  70. } s;
  71. uint16_t Flags;
  72. } u;
  73. uint32_t Crc32;
  74. } XZ_STREAM_HEADER, * PXZ_STREAM_HEADER;
  75. //static_assert(sizeof(XZ_STREAM_HEADER) == 12, "Invalid Stream Header Size");
  76. //
  77. // This describes the last 12 bytes of any XZ container file / stream
  78. //
  79. typedef struct _XZ_STREAM_FOOTER
  80. {
  81. uint32_t Crc32;
  82. uint32_t BackwardSize;
  83. union
  84. {
  85. struct
  86. {
  87. uint8_t ReservedFlags;
  88. uint8_t CheckType : 4;
  89. uint8_t ReservedType : 4;
  90. } s;
  91. uint16_t Flags;
  92. } u;
  93. uint16_t Magic;
  94. } XZ_STREAM_FOOTER, * PXZ_STREAM_FOOTER;
  95. //static_assert(sizeof(XZ_STREAM_FOOTER) == 12, "Invalid Stream Footer Size");
  96. //
  97. // This describes the beginning of a compressed payload stored in an XZ stream,
  98. // with hardcoded expectations for an LZMA2-compressed payload that has 0 extra
  99. // filters (such as BCJ2).
  100. //
  101. typedef struct _XZ_BLOCK_HEADER
  102. {
  103. uint8_t Size;
  104. union
  105. {
  106. struct
  107. {
  108. uint8_t FilterCount : 2;
  109. uint8_t Reserved : 4;
  110. uint8_t HasCompressedSize : 1;
  111. uint8_t HasUncompressedSize : 1;
  112. } s;
  113. uint8_t Flags;
  114. } u;
  115. struct
  116. {
  117. uint8_t Id;
  118. uint8_t Size;
  119. union
  120. {
  121. struct
  122. {
  123. uint8_t DictionarySize : 6;
  124. uint8_t Reserved : 2;
  125. } s;
  126. uint8_t Properties;
  127. } u;
  128. } LzmaFlags;
  129. uint8_t Padding[3];
  130. uint32_t Crc32;
  131. } XZ_BLOCK_HEADER, * PXZ_BLOCK_HEADER;
  132. //static_assert(sizeof(XZ_BLOCK_HEADER) == 12, "Invalid Block Header Size");