Compression.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifdef JA2_PRECOMPILED_HEADERS
  2. #include "JA2 SGP ALL.H"
  3. #elif defined( WIZ8_PRECOMPILED_HEADERS )
  4. #include "WIZ8 SGP ALL.H"
  5. #else
  6. #include "MemMan.h"
  7. #include "debug.h"
  8. #include "zlib.h"
  9. #endif
  10. // mem allocation functions for ZLIB's purposes
  11. voidpf ZAlloc( voidpf opaque, uInt items, uInt size )
  12. {
  13. return( MemAlloc( items * size ) );
  14. }
  15. void ZFree( voidpf opaque, voidpf address )
  16. {
  17. MemFree( address );
  18. }
  19. PTR DecompressInit( BYTE * pCompressedData, UINT32 uiDataSize )
  20. {
  21. z_stream * pZStream;
  22. int iZRetCode;
  23. // allocate memory for the z_stream struct
  24. pZStream = MemAlloc( sizeof( z_stream ) );
  25. if( pZStream == NULL )
  26. { // out of memory!
  27. return( NULL );
  28. }
  29. // initial defines
  30. pZStream->zalloc = ZAlloc;
  31. pZStream->zfree = ZFree;
  32. pZStream->opaque = NULL;
  33. // call the ZLIB init routine
  34. iZRetCode = inflateInit( pZStream );
  35. if( iZRetCode != Z_OK )
  36. { // ZLIB init error!
  37. MemFree( pZStream );
  38. return( NULL );
  39. }
  40. // set up our parameters
  41. pZStream->next_in = pCompressedData;
  42. pZStream->avail_in = uiDataSize;
  43. return( (PTR) pZStream );
  44. }
  45. UINT32 Decompress( PTR pDecompPtr, BYTE * pBuffer, UINT32 uiBufferLen )
  46. {
  47. int iZRetCode;
  48. z_stream * pZStream = (z_stream *) pDecompPtr;
  49. // these assertions is in here to ensure that we get passed a proper z_stream pointer
  50. Assert( pZStream != NULL );
  51. Assert( pZStream->zalloc == ZAlloc );
  52. if (pZStream->avail_in == 0)
  53. { // There is nothing left to decompress!
  54. return( 0 );
  55. }
  56. // set up the z_stream with our parameters
  57. pZStream->next_out = pBuffer;
  58. pZStream->avail_out = uiBufferLen;
  59. // decompress!
  60. iZRetCode = inflate( pZStream, Z_PARTIAL_FLUSH );
  61. Assert( iZRetCode == Z_OK || iZRetCode == Z_STREAM_END );
  62. return( uiBufferLen - pZStream->avail_out );
  63. }
  64. void DecompressFini( PTR pDecompPtr )
  65. {
  66. z_stream * pZStream = (z_stream *) pDecompPtr;
  67. // these assertions is in here to ensure that we get passed a proper z_stream pointer
  68. Assert( pZStream != NULL );
  69. Assert( pZStream->zalloc == ZAlloc );
  70. inflateEnd( pZStream );
  71. MemFree( pZStream );
  72. }
  73. UINT32 CompressedBufferSize( UINT32 uiDataSize )
  74. { // Function that calculates the worst-case buffer size needed to
  75. // hold uiDataSize bytes compressed
  76. return( uiDataSize + uiDataSize / 10 + 13 );
  77. }
  78. PTR CompressInit( BYTE * pUncompressedData, UINT32 uiDataSize )
  79. {
  80. z_stream * pZStream;
  81. int iZRetCode;
  82. // allocate memory for the z_stream struct
  83. pZStream = MemAlloc( sizeof( z_stream ) );
  84. if( pZStream == NULL )
  85. { // out of memory!
  86. return( NULL );
  87. }
  88. // initial defines
  89. pZStream->zalloc = ZAlloc;
  90. pZStream->zfree = ZFree;
  91. pZStream->opaque = NULL;
  92. // call the ZLIB init routine
  93. iZRetCode = deflateInit( pZStream, Z_BEST_COMPRESSION );
  94. if( iZRetCode != Z_OK )
  95. { // ZLIB init error!
  96. MemFree( pZStream );
  97. return( NULL );
  98. }
  99. // set up our parameters
  100. pZStream->next_in = pUncompressedData;
  101. pZStream->avail_in = uiDataSize;
  102. return( (PTR) pZStream );
  103. }
  104. UINT32 Compress( PTR pCompPtr, BYTE * pBuffer, UINT32 uiBufferLen )
  105. {
  106. int iZRetCode;
  107. z_stream * pZStream = (z_stream *) pCompPtr;
  108. // these assertions is in here to ensure that we get passed a proper z_stream pointer
  109. Assert( pZStream != NULL );
  110. Assert( pZStream->zalloc == ZAlloc );
  111. if (pZStream->avail_in == 0)
  112. { // There is nothing left to compress!
  113. return( 0 );
  114. }
  115. // set up the z_stream with our parameters
  116. pZStream->next_out = pBuffer;
  117. pZStream->avail_out = uiBufferLen;
  118. // decompress!
  119. iZRetCode = deflate( pZStream, Z_FINISH );
  120. Assert( iZRetCode == Z_STREAM_END );
  121. return( uiBufferLen - pZStream->avail_out );
  122. }
  123. void CompressFini( PTR pCompPtr )
  124. {
  125. z_stream * pZStream = (z_stream *) pCompPtr;
  126. // these assertions is in here to ensure that we get passed a proper z_stream pointer
  127. Assert( pZStream != NULL );
  128. Assert( pZStream->zalloc == ZAlloc );
  129. deflateEnd( pZStream );
  130. MemFree( pZStream );
  131. }