MemoryCompressor.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. // See http://www.bzip.org/1.0.3/bzip2-manual-1.0.3.html#low-level for docs on bzip
  11. #include "MemoryCompressor.h"
  12. #include <assert.h>
  13. #include <stdlib.h>
  14. CompressorBase::CompressorBase()
  15. {
  16. output=0;
  17. allocatedSize=0;
  18. streamInited=false;
  19. stream.bzalloc=0;
  20. stream.bzfree=0;
  21. stream.opaque=0;
  22. totalRead=totalWritten=0;
  23. }
  24. CompressorBase::~CompressorBase()
  25. {
  26. }
  27. MemoryCompressor::MemoryCompressor()
  28. {
  29. compressedInputLength=0;
  30. }
  31. MemoryCompressor::~MemoryCompressor()
  32. {
  33. Clear();
  34. }
  35. MemoryDecompressor::~MemoryDecompressor()
  36. {
  37. Clear();
  38. }
  39. bool MemoryCompressor::Compress(char *input, const unsigned inputLength, bool finish)
  40. {
  41. int res;
  42. unsigned inBefore,outBefore, read;
  43. unsigned written;
  44. if (output==0)
  45. {
  46. allocatedSize=inputLength;
  47. if (allocatedSize < BZ_MAX_UNUSED)
  48. allocatedSize=BZ_MAX_UNUSED;
  49. output=(char*)malloc(allocatedSize);
  50. }
  51. if (streamInited==false)
  52. {
  53. res = BZ2_bzCompressInit ( &stream,
  54. 9, // x 100K Block size. Memory to use = 400k + ( 8 x block size ). So this is 400K + 9 x 900K = 7.6 megabytes. Larger sizes give better compression.
  55. 0, // Verbosity.
  56. 0 ); // Default work factor
  57. streamInited=true;
  58. if (res!=BZ_OK)
  59. return false;
  60. }
  61. read=written=0;
  62. unsigned readThisSession;
  63. readThisSession=0;
  64. compressedInputLength+=inputLength;
  65. if (totalWritten==allocatedSize)
  66. {
  67. allocatedSize+=inputLength;
  68. output=(char*)realloc(output, allocatedSize);
  69. }
  70. while(1)
  71. {
  72. stream.next_out=output+totalWritten;
  73. stream.avail_in=inputLength-readThisSession;
  74. stream.avail_out=allocatedSize-totalWritten;
  75. stream.next_in=input+readThisSession;
  76. inBefore=stream.total_in_lo32;
  77. outBefore=stream.total_out_lo32;
  78. //printf("%i\n", stream.avail_in);
  79. res = BZ2_bzCompress( &stream, finish ? BZ_FINISH : BZ_RUN );
  80. read=stream.total_in_lo32-inBefore;
  81. written=stream.total_out_lo32-outBefore;
  82. totalRead+=read;
  83. totalWritten+=written;
  84. readThisSession+=read;
  85. if (stream.avail_out>0)
  86. // if ((stream.avail_in==0 && stream.avail_out>0) || (read==0 && written==0))
  87. {
  88. if (finish)
  89. {
  90. allocatedSize=GetTotalOutputSize();
  91. output=(char*)realloc(output,allocatedSize);
  92. BZ2_bzCompressEnd( &stream );
  93. streamInited=false;
  94. }
  95. return true;
  96. }
  97. if (totalWritten==allocatedSize || read==0)
  98. {
  99. allocatedSize*=2;
  100. output=(char*)realloc(output, allocatedSize);
  101. }
  102. }
  103. }
  104. bool MemoryDecompressor::Decompress(char *input, const unsigned inputLength, bool ignoreStreamEnd)
  105. {
  106. unsigned inBefore,outBefore, read;
  107. unsigned written;
  108. int res;
  109. if (output==0)
  110. {
  111. allocatedSize=inputLength*2;
  112. if (allocatedSize < BZ_MAX_UNUSED)
  113. allocatedSize=BZ_MAX_UNUSED;
  114. output=(char*)malloc(allocatedSize);
  115. }
  116. if (streamInited==false)
  117. {
  118. res = BZ2_bzDecompressInit( &stream,
  119. 0, // Verbosity.
  120. 0 ); // Disable small memory usage
  121. streamInited=true;
  122. if (res!=BZ_OK)
  123. return false;
  124. }
  125. unsigned readThisSession;
  126. readThisSession=0;
  127. read=written=0;
  128. if (totalWritten==allocatedSize)
  129. {
  130. allocatedSize+=inputLength*4;
  131. output=(char*)realloc(output, allocatedSize);
  132. }
  133. while(1)
  134. {
  135. stream.next_out=output+totalWritten;
  136. stream.avail_in=inputLength-readThisSession;
  137. stream.avail_out=allocatedSize-totalWritten;
  138. stream.next_in=input+readThisSession;
  139. inBefore=stream.total_in_lo32;
  140. outBefore=stream.total_out_lo32;
  141. res = BZ2_bzDecompress( &stream );
  142. read=stream.total_in_lo32-inBefore;
  143. written=stream.total_out_lo32-outBefore;
  144. readThisSession+=read;
  145. totalRead+=read;
  146. totalWritten+=written;
  147. if (res==BZ_STREAM_END)
  148. {
  149. BZ2_bzDecompressEnd( &stream );
  150. if (ignoreStreamEnd==true)
  151. {
  152. // Stream end marker but there is more data so just keep reading
  153. res = BZ2_bzDecompressInit( &stream,
  154. 0, // Verbosity.
  155. 0 ); // Disable small memory usage
  156. }
  157. else
  158. {
  159. streamInited=false;
  160. return true;
  161. }
  162. }
  163. else if ((stream.avail_in==0 && stream.avail_out>0) || (read==0 && written==0))
  164. {
  165. allocatedSize=GetTotalOutputSize();
  166. output=(char*)realloc(output,allocatedSize);
  167. return true;
  168. }
  169. else if (res!=BZ_OK)
  170. {
  171. Clear();
  172. return false;
  173. }
  174. if (totalWritten==allocatedSize || read==0)
  175. {
  176. allocatedSize+=inputLength*4;
  177. output=(char*)realloc(output, allocatedSize);
  178. }
  179. }
  180. }
  181. char *CompressorBase::GetOutput(void) const
  182. {
  183. return output;
  184. }
  185. unsigned MemoryCompressor::GetCompressedInputLength(void) const
  186. {
  187. return compressedInputLength;
  188. }
  189. unsigned CompressorBase::GetTotalOutputSize(void) const
  190. {
  191. return totalWritten;
  192. }
  193. unsigned CompressorBase::GetTotalInputSize(void) const
  194. {
  195. return totalRead;
  196. }
  197. void MemoryCompressor::Clear(void)
  198. {
  199. if (output)
  200. {
  201. free(output);
  202. output=0;
  203. }
  204. if (streamInited)
  205. BZ2_bzCompressEnd( &stream );
  206. totalRead=totalWritten=compressedInputLength=0;
  207. }
  208. void MemoryDecompressor::Clear(void)
  209. {
  210. if (output)
  211. {
  212. free(output);
  213. output=0;
  214. }
  215. if (streamInited)
  216. BZ2_bzDecompressEnd( &stream );
  217. }
粤ICP备19079148号