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.

102 lines
3.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // LZMA Codec interface for engine.
  4. //
  5. // LZMA SDK 9.38 beta
  6. // 2015-01-03 : Igor Pavlov : Public domain
  7. // http://www.7-zip.org/
  8. //
  9. //========================================================================//
  10. #ifndef _LZMADECODER_H
  11. #define _LZMADECODER_H
  12. #pragma once
  13. // Thanks for the useful define namespacing, LZMA
  14. #include "../../utils/lzma/C/7zVersion.h"
  15. #define LZMA_SDK_VERSION_MAJOR MY_VER_MAJOR
  16. #define LZMA_SDK_VERSION_MINOR MY_VER_MINOR
  17. #if !defined( _X360 )
  18. #define LZMA_ID (('A'<<24)|('M'<<16)|('Z'<<8)|('L'))
  19. #else
  20. #define LZMA_ID (('L'<<24)|('Z'<<16)|('M'<<8)|('A'))
  21. #endif
  22. // bind the buffer for correct identification
  23. #pragma pack(1)
  24. struct lzma_header_t
  25. {
  26. unsigned int id;
  27. unsigned int actualSize; // always little endian
  28. unsigned int lzmaSize; // always little endian
  29. unsigned char properties[5];
  30. };
  31. #pragma pack()
  32. class CLZMAStream;
  33. class CLZMA
  34. {
  35. public:
  36. static unsigned int Uncompress( unsigned char *pInput, unsigned char *pOutput );
  37. static bool IsCompressed( unsigned char *pInput );
  38. static unsigned int GetActualSize( unsigned char *pInput );
  39. };
  40. // For files besides the implementation, we forward declare a dummy struct. We can't unconditionally forward declare
  41. // this because LzmaEnc.h typedefs this directly to an unnamed struct :-/
  42. #ifndef CLzmaDec_t
  43. struct _CLzmaDec_t;
  44. #define CLzmaDec_t struct _CLzmaDec_t
  45. #endif
  46. class CLZMAStream
  47. {
  48. public:
  49. CLZMAStream();
  50. ~CLZMAStream();
  51. // Initialize a stream to read data from a LZMA style zip file, passing the original size from the zip headers.
  52. // Streams with a source-engine style header (lzma_header_t) do not need an init call.
  53. void InitZIPHeader( unsigned int nCompressedSize, unsigned int nOriginalSize );
  54. // Attempt to read up to nMaxInputBytes from the compressed stream, writing up to nMaxOutputBytes to pOutput.
  55. // Makes progress until blocked on input or output.
  56. // Returns false if read stops due to an error or if called at EOF (GetExpectedBytesRemaining == 0)
  57. bool Read( unsigned char *pInput, unsigned int nMaxInputBytes,
  58. unsigned char *pOutput, unsigned int nMaxOutputBytes,
  59. /* out */ unsigned int &nCompressedBytesRead, /* out */ unsigned int &nOutputBytesWritten );
  60. // Get the expected uncompressed bytes yet to be read from this stream. Returns false if not yet known, such as
  61. // before being fed the header.
  62. bool GetExpectedBytesRemaining( /* out */ unsigned int &nBytesRemaining );
  63. private:
  64. enum eHeaderParse
  65. {
  66. eHeaderParse_OK,
  67. eHeaderParse_Fail,
  68. eHeaderParse_NeedMoreBytes
  69. };
  70. eHeaderParse TryParseHeader( unsigned char *pInput, unsigned int nBytesAvailable, /* out */ unsigned int &nBytesConsumed );
  71. void FreeDecoderState();
  72. bool CreateDecoderState( const unsigned char *pProperties );
  73. // Init from a zip-embedded LZMA stream. Requires the original size be passed from zip headers.
  74. CLzmaDec_t *m_pDecoderState;
  75. unsigned int m_nActualSize;
  76. unsigned int m_nActualBytesRead;
  77. unsigned int m_nCompressedSize;
  78. unsigned int m_nCompressedBytesRead;
  79. // If we have read past the header
  80. bool m_bParsedHeader : 1;
  81. // If InitZIPHeader() was called. We're expecting a zip-style header and have size information.
  82. bool m_bZIPStyleHeader : 1;
  83. };
  84. #endif