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.

307 lines
8.1 KiB

  1. /* XzIn.c - Xz input
  2. 2014-12-30 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <string.h>
  5. #include "7zCrc.h"
  6. #include "CpuArch.h"
  7. #include "Xz.h"
  8. SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream)
  9. {
  10. Byte sig[XZ_STREAM_HEADER_SIZE];
  11. RINOK(SeqInStream_Read2(inStream, sig, XZ_STREAM_HEADER_SIZE, SZ_ERROR_NO_ARCHIVE));
  12. if (memcmp(sig, XZ_SIG, XZ_SIG_SIZE) != 0)
  13. return SZ_ERROR_NO_ARCHIVE;
  14. return Xz_ParseHeader(p, sig);
  15. }
  16. #define READ_VARINT_AND_CHECK(buf, pos, size, res) \
  17. { unsigned s = Xz_ReadVarInt(buf + pos, size - pos, res); \
  18. if (s == 0) return SZ_ERROR_ARCHIVE; pos += s; }
  19. SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, Bool *isIndex, UInt32 *headerSizeRes)
  20. {
  21. Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
  22. unsigned headerSize;
  23. *headerSizeRes = 0;
  24. RINOK(SeqInStream_ReadByte(inStream, &header[0]));
  25. headerSize = ((unsigned)header[0] << 2) + 4;
  26. if (headerSize == 0)
  27. {
  28. *headerSizeRes = 1;
  29. *isIndex = True;
  30. return SZ_OK;
  31. }
  32. *isIndex = False;
  33. *headerSizeRes = headerSize;
  34. RINOK(SeqInStream_Read(inStream, header + 1, headerSize - 1));
  35. return XzBlock_Parse(p, header);
  36. }
  37. #define ADD_SIZE_CHECH(size, val) \
  38. { UInt64 newSize = size + (val); if (newSize < size) return XZ_SIZE_OVERFLOW; size = newSize; }
  39. UInt64 Xz_GetUnpackSize(const CXzStream *p)
  40. {
  41. UInt64 size = 0;
  42. size_t i;
  43. for (i = 0; i < p->numBlocks; i++)
  44. ADD_SIZE_CHECH(size, p->blocks[i].unpackSize);
  45. return size;
  46. }
  47. UInt64 Xz_GetPackSize(const CXzStream *p)
  48. {
  49. UInt64 size = 0;
  50. size_t i;
  51. for (i = 0; i < p->numBlocks; i++)
  52. ADD_SIZE_CHECH(size, (p->blocks[i].totalSize + 3) & ~(UInt64)3);
  53. return size;
  54. }
  55. /*
  56. SRes XzBlock_ReadFooter(CXzBlock *p, CXzStreamFlags f, ISeqInStream *inStream)
  57. {
  58. return SeqInStream_Read(inStream, p->check, XzFlags_GetCheckSize(f));
  59. }
  60. */
  61. static SRes Xz_ReadIndex2(CXzStream *p, const Byte *buf, size_t size, ISzAlloc *alloc)
  62. {
  63. size_t i, numBlocks, pos = 1;
  64. UInt32 crc;
  65. if (size < 5 || buf[0] != 0)
  66. return SZ_ERROR_ARCHIVE;
  67. size -= 4;
  68. crc = CrcCalc(buf, size);
  69. if (crc != GetUi32(buf + size))
  70. return SZ_ERROR_ARCHIVE;
  71. {
  72. UInt64 numBlocks64;
  73. READ_VARINT_AND_CHECK(buf, pos, size, &numBlocks64);
  74. numBlocks = (size_t)numBlocks64;
  75. if (numBlocks != numBlocks64 || numBlocks * 2 > size)
  76. return SZ_ERROR_ARCHIVE;
  77. }
  78. Xz_Free(p, alloc);
  79. if (numBlocks != 0)
  80. {
  81. p->numBlocks = numBlocks;
  82. p->numBlocksAllocated = numBlocks;
  83. p->blocks = alloc->Alloc(alloc, sizeof(CXzBlockSizes) * numBlocks);
  84. if (p->blocks == 0)
  85. return SZ_ERROR_MEM;
  86. for (i = 0; i < numBlocks; i++)
  87. {
  88. CXzBlockSizes *block = &p->blocks[i];
  89. READ_VARINT_AND_CHECK(buf, pos, size, &block->totalSize);
  90. READ_VARINT_AND_CHECK(buf, pos, size, &block->unpackSize);
  91. if (block->totalSize == 0)
  92. return SZ_ERROR_ARCHIVE;
  93. }
  94. }
  95. while ((pos & 3) != 0)
  96. if (buf[pos++] != 0)
  97. return SZ_ERROR_ARCHIVE;
  98. return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
  99. }
  100. static SRes Xz_ReadIndex(CXzStream *p, ILookInStream *stream, UInt64 indexSize, ISzAlloc *alloc)
  101. {
  102. SRes res;
  103. size_t size;
  104. Byte *buf;
  105. if (indexSize > ((UInt32)1 << 31))
  106. return SZ_ERROR_UNSUPPORTED;
  107. size = (size_t)indexSize;
  108. if (size != indexSize)
  109. return SZ_ERROR_UNSUPPORTED;
  110. buf = alloc->Alloc(alloc, size);
  111. if (buf == 0)
  112. return SZ_ERROR_MEM;
  113. res = LookInStream_Read2(stream, buf, size, SZ_ERROR_UNSUPPORTED);
  114. if (res == SZ_OK)
  115. res = Xz_ReadIndex2(p, buf, size, alloc);
  116. alloc->Free(alloc, buf);
  117. return res;
  118. }
  119. static SRes SeekFromCur(ILookInStream *inStream, Int64 *res)
  120. {
  121. return inStream->Seek(inStream, res, SZ_SEEK_CUR);
  122. }
  123. static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOffset, ISzAlloc *alloc)
  124. {
  125. UInt64 indexSize;
  126. Byte buf[XZ_STREAM_FOOTER_SIZE];
  127. if ((*startOffset & 3) != 0 || *startOffset < XZ_STREAM_FOOTER_SIZE)
  128. return SZ_ERROR_NO_ARCHIVE;
  129. *startOffset = -XZ_STREAM_FOOTER_SIZE;
  130. RINOK(SeekFromCur(stream, startOffset));
  131. RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
  132. if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
  133. {
  134. UInt32 total = 0;
  135. *startOffset += XZ_STREAM_FOOTER_SIZE;
  136. for (;;)
  137. {
  138. size_t i;
  139. #define TEMP_BUF_SIZE (1 << 10)
  140. Byte tempBuf[TEMP_BUF_SIZE];
  141. if (*startOffset < XZ_STREAM_FOOTER_SIZE || total > (1 << 16))
  142. return SZ_ERROR_NO_ARCHIVE;
  143. i = (*startOffset > TEMP_BUF_SIZE) ? TEMP_BUF_SIZE : (size_t)*startOffset;
  144. total += (UInt32)i;
  145. *startOffset = -(Int64)i;
  146. RINOK(SeekFromCur(stream, startOffset));
  147. RINOK(LookInStream_Read2(stream, tempBuf, i, SZ_ERROR_NO_ARCHIVE));
  148. for (; i != 0; i--)
  149. if (tempBuf[i - 1] != 0)
  150. break;
  151. if (i != 0)
  152. {
  153. if ((i & 3) != 0)
  154. return SZ_ERROR_NO_ARCHIVE;
  155. *startOffset += i;
  156. break;
  157. }
  158. }
  159. if (*startOffset < XZ_STREAM_FOOTER_SIZE)
  160. return SZ_ERROR_NO_ARCHIVE;
  161. *startOffset -= XZ_STREAM_FOOTER_SIZE;
  162. RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
  163. RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
  164. if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
  165. return SZ_ERROR_NO_ARCHIVE;
  166. }
  167. p->flags = (CXzStreamFlags)GetBe16(buf + 8);
  168. if (!XzFlags_IsSupported(p->flags))
  169. return SZ_ERROR_UNSUPPORTED;
  170. if (GetUi32(buf) != CrcCalc(buf + 4, 6))
  171. return SZ_ERROR_ARCHIVE;
  172. indexSize = ((UInt64)GetUi32(buf + 4) + 1) << 2;
  173. *startOffset = -(Int64)(indexSize + XZ_STREAM_FOOTER_SIZE);
  174. RINOK(SeekFromCur(stream, startOffset));
  175. RINOK(Xz_ReadIndex(p, stream, indexSize, alloc));
  176. {
  177. UInt64 totalSize = Xz_GetPackSize(p);
  178. UInt64 sum = XZ_STREAM_HEADER_SIZE + totalSize + indexSize;
  179. if (totalSize == XZ_SIZE_OVERFLOW ||
  180. sum >= ((UInt64)1 << 63) ||
  181. totalSize >= ((UInt64)1 << 63))
  182. return SZ_ERROR_ARCHIVE;
  183. *startOffset = -(Int64)sum;
  184. RINOK(SeekFromCur(stream, startOffset));
  185. }
  186. {
  187. CXzStreamFlags headerFlags;
  188. CSecToRead secToRead;
  189. SecToRead_CreateVTable(&secToRead);
  190. secToRead.realStream = stream;
  191. RINOK(Xz_ReadHeader(&headerFlags, &secToRead.s));
  192. return (p->flags == headerFlags) ? SZ_OK : SZ_ERROR_ARCHIVE;
  193. }
  194. }
  195. /* ---------- Xz Streams ---------- */
  196. void Xzs_Construct(CXzs *p)
  197. {
  198. p->num = p->numAllocated = 0;
  199. p->streams = 0;
  200. }
  201. void Xzs_Free(CXzs *p, ISzAlloc *alloc)
  202. {
  203. size_t i;
  204. for (i = 0; i < p->num; i++)
  205. Xz_Free(&p->streams[i], alloc);
  206. alloc->Free(alloc, p->streams);
  207. p->num = p->numAllocated = 0;
  208. p->streams = 0;
  209. }
  210. UInt64 Xzs_GetNumBlocks(const CXzs *p)
  211. {
  212. UInt64 num = 0;
  213. size_t i;
  214. for (i = 0; i < p->num; i++)
  215. num += p->streams[i].numBlocks;
  216. return num;
  217. }
  218. UInt64 Xzs_GetUnpackSize(const CXzs *p)
  219. {
  220. UInt64 size = 0;
  221. size_t i;
  222. for (i = 0; i < p->num; i++)
  223. ADD_SIZE_CHECH(size, Xz_GetUnpackSize(&p->streams[i]));
  224. return size;
  225. }
  226. /*
  227. UInt64 Xzs_GetPackSize(const CXzs *p)
  228. {
  229. UInt64 size = 0;
  230. size_t i;
  231. for (i = 0; i < p->num; i++)
  232. ADD_SIZE_CHECH(size, Xz_GetTotalSize(&p->streams[i]));
  233. return size;
  234. }
  235. */
  236. SRes Xzs_ReadBackward(CXzs *p, ILookInStream *stream, Int64 *startOffset, ICompressProgress *progress, ISzAlloc *alloc)
  237. {
  238. Int64 endOffset = 0;
  239. RINOK(stream->Seek(stream, &endOffset, SZ_SEEK_END));
  240. *startOffset = endOffset;
  241. for (;;)
  242. {
  243. CXzStream st;
  244. SRes res;
  245. Xz_Construct(&st);
  246. res = Xz_ReadBackward(&st, stream, startOffset, alloc);
  247. st.startOffset = *startOffset;
  248. RINOK(res);
  249. if (p->num == p->numAllocated)
  250. {
  251. size_t newNum = p->num + p->num / 4 + 1;
  252. Byte *data = (Byte *)alloc->Alloc(alloc, newNum * sizeof(CXzStream));
  253. if (data == 0)
  254. return SZ_ERROR_MEM;
  255. p->numAllocated = newNum;
  256. if (p->num != 0)
  257. memcpy(data, p->streams, p->num * sizeof(CXzStream));
  258. alloc->Free(alloc, p->streams);
  259. p->streams = (CXzStream *)data;
  260. }
  261. p->streams[p->num++] = st;
  262. if (*startOffset == 0)
  263. break;
  264. RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
  265. if (progress && progress->Progress(progress, endOffset - *startOffset, (UInt64)(Int64)-1) != SZ_OK)
  266. return SZ_ERROR_PROGRESS;
  267. }
  268. return SZ_OK;
  269. }