home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / zip.h < prev   
Encoding:
C/C++ Source or Header  |  2009-09-14  |  4.9 KB  |  221 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #ifndef f_ZIP_H
  27. #define f_ZIP_H
  28.  
  29. // Rest in peace, Phil Katz.
  30.  
  31. #include <vd2/system/vdtypes.h>
  32. #include <vd2/system/file.h>
  33. #include <vd2/system/file.h>
  34. #include <vd2/system/VDString.h>
  35. #include <string.h>
  36. #include <vector>
  37.  
  38. class VDDeflateBitReader {
  39. public:
  40.     VDDeflateBitReader() : mpSrc(0), mBufferPt(0), accum(0), bits(0) {}
  41.  
  42.     void init(IVDStream *pSrc, uint64 limit) {
  43.         mpSrc = pSrc;
  44.         mBytesLeft = limit;
  45.         refill();
  46.         consume(0);
  47.     }
  48.  
  49.     IVDStream *stream() const {
  50.         return mpSrc;
  51.     }
  52.  
  53.     unsigned long peek() const {
  54.         return accum;
  55.     }
  56.  
  57.     bool consume(unsigned n) {
  58. //        printf("%08lx/%d\n", accum << ((-bits)&7), bits);
  59.         bits -= n;
  60.  
  61.         if ((int)bits < 0)
  62.             return false;
  63.  
  64.         accum >>= n;
  65.  
  66.         while(bits <= 24 && (mBufferPt || refill())) {
  67.             accum += mBuffer[kBufferSize + mBufferPt++] << bits;
  68.             bits += 8;
  69.         }
  70.  
  71.         return true;
  72.     }
  73.  
  74.     bool refill();
  75.  
  76.     bool getbit() {
  77.         unsigned rv = accum;
  78.  
  79.         consume(1);
  80.  
  81.         return (rv&1) != 0;
  82.     }
  83.  
  84.     unsigned getbits(unsigned n) {
  85.         unsigned rv = accum & ((1<<n)-1);
  86.  
  87.         consume(n);
  88.  
  89.         return rv;
  90.     }
  91.  
  92.     bool empty() const {
  93.         return bits != 0;
  94.     }
  95.  
  96.     unsigned avail() const {
  97.         return bits;
  98.     }
  99.  
  100.     unsigned bitsleft() const {
  101.         return bits + (mBytesLeftLimited<<3);
  102.     }
  103.  
  104.     unsigned bytesleft() const {
  105.         return (bits>>3) + mBytesLeftLimited;
  106.     }
  107.  
  108.     void align() {
  109.         consume(bits&7);
  110.     }
  111.  
  112.     void readbytes(void *dst, unsigned len);
  113.  
  114. protected:
  115.     enum { kBigAvailThreshold = 16777216 };
  116.     enum { kBufferSize = 256 };
  117.  
  118.     unsigned long accum;
  119.     unsigned    bits;
  120.     int            mBufferPt;            // counts from -256 to 0
  121.     uint64        mBytesLeft;
  122.     unsigned    mBytesLeftLimited;
  123.  
  124.     IVDStream *mpSrc;
  125.     uint8    mBuffer[kBufferSize];
  126. };
  127.  
  128. class VDCRCChecker {
  129. public:
  130.     enum {
  131.         kCRC32        = 0xEDB88320        // CRC-32 used by PKZIP, PNG (x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1)
  132.     };
  133.  
  134.     VDCRCChecker() {}
  135.     VDCRCChecker(uint32 crc) { Init(crc); }
  136.  
  137.     void Init(uint32 crc);
  138.     void Process(const void *src, sint32 len);
  139.  
  140.     uint32 CRC() const { return ~mValue; }
  141.     uint32 CRC(uint32 crc, const void *src, sint32 len);
  142.  
  143. protected:
  144.     uint32    mValue;
  145.     uint32    mTable[256];
  146. };
  147.  
  148. class VDZipStream : public IVDStream {
  149. public:
  150.     VDZipStream();
  151.     VDZipStream(IVDStream *pSrc, uint64 limit, bool bStored);
  152.     ~VDZipStream();
  153.  
  154.     void    Init(IVDStream *pSrc, uint64 limit, bool bStored);
  155.     void    EnableCRC(uint32 crc = VDCRCChecker::kCRC32) { mCRCChecker.Init(crc); mbCRCEnabled = true; }
  156.     uint32    CRC() { return mCRCChecker.CRC(); }
  157.  
  158.     const wchar_t *GetNameForError();
  159.  
  160.     sint64    Pos();
  161.     void    Read(void *buffer, sint32 bytes);
  162.     sint32    ReadData(void *buffer, sint32 bytes);
  163.     void    Write(const void *buffer, sint32 bytes);
  164.  
  165. protected:
  166.     bool    ParseBlockHeader();
  167.     bool    Inflate();
  168.  
  169.     VDDeflateBitReader mBits;                    // critical -- make this first!
  170.     uint32    mReadPt, mWritePt, mBufferLevel;
  171.  
  172.     enum {
  173.         kNoBlock,
  174.         kStoredBlock,
  175.         kDeflatedBlock
  176.     } mBlockType;
  177.  
  178.     uint32    mStoredBytesLeft;
  179.     bool    mbNoMoreBlocks;
  180.     bool    mbCRCEnabled;
  181.  
  182.     sint64    mPos;
  183.     uint8    mBuffer[65536];
  184.  
  185.     uint16    mCodeDecode[32768];
  186.     uint8    mCodeLengths[288 + 32];
  187.     uint16    mDistDecode[32768];
  188.  
  189.     VDCRCChecker    mCRCChecker;
  190. };
  191.  
  192. class VDZipArchive {
  193. public:
  194.     struct FileInfo {
  195.         VDString    mFileName;
  196.         uint32        mCompressedSize;
  197.         uint32        mUncompressedSize;
  198.         uint32        mCRC32;
  199.         bool        mbPacked;
  200.     };
  201.  
  202.     VDZipArchive();
  203.     ~VDZipArchive();
  204.  
  205.     void Init(IVDRandomAccessStream *pSrc);
  206.  
  207.     sint32            GetFileCount();
  208.     const FileInfo&    GetFileInfo(sint32 idx);
  209.     IVDStream        *OpenRawStream(sint32 idx);
  210.  
  211. protected:
  212.     struct FileInfoInternal : public FileInfo {
  213.         uint32        mDataStart;
  214.     };
  215.  
  216.     std::vector<FileInfoInternal>    mDirectory;
  217.     IVDRandomAccessStream            *mpStream;
  218. };
  219.  
  220. #endif
  221.