gzip_stream.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: brianolson@google.com (Brian Olson)
  31. //
  32. // This file contains the implementation of classes GzipInputStream and
  33. // GzipOutputStream.
  34. #if HAVE_ZLIB
  35. #include <google/protobuf/io/gzip_stream.h>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/stubs/logging.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace io {
  41. static const int kDefaultBufferSize = 65536;
  42. GzipInputStream::GzipInputStream(
  43. ZeroCopyInputStream* sub_stream, Format format, int buffer_size)
  44. : format_(format), sub_stream_(sub_stream), zerror_(Z_OK), byte_count_(0) {
  45. zcontext_.state = Z_NULL;
  46. zcontext_.zalloc = Z_NULL;
  47. zcontext_.zfree = Z_NULL;
  48. zcontext_.opaque = Z_NULL;
  49. zcontext_.total_out = 0;
  50. zcontext_.next_in = NULL;
  51. zcontext_.avail_in = 0;
  52. zcontext_.total_in = 0;
  53. zcontext_.msg = NULL;
  54. if (buffer_size == -1) {
  55. output_buffer_length_ = kDefaultBufferSize;
  56. } else {
  57. output_buffer_length_ = buffer_size;
  58. }
  59. output_buffer_ = operator new(output_buffer_length_);
  60. GOOGLE_CHECK(output_buffer_ != NULL);
  61. zcontext_.next_out = static_cast<Bytef*>(output_buffer_);
  62. zcontext_.avail_out = output_buffer_length_;
  63. output_position_ = output_buffer_;
  64. }
  65. GzipInputStream::~GzipInputStream() {
  66. operator delete(output_buffer_);
  67. zerror_ = inflateEnd(&zcontext_);
  68. }
  69. static inline int internalInflateInit2(
  70. z_stream* zcontext, GzipInputStream::Format format) {
  71. int windowBitsFormat = 0;
  72. switch (format) {
  73. case GzipInputStream::GZIP: windowBitsFormat = 16; break;
  74. case GzipInputStream::AUTO: windowBitsFormat = 32; break;
  75. case GzipInputStream::ZLIB: windowBitsFormat = 0; break;
  76. }
  77. return inflateInit2(zcontext, /* windowBits */15 | windowBitsFormat);
  78. }
  79. int GzipInputStream::Inflate(int flush) {
  80. if ((zerror_ == Z_OK) && (zcontext_.avail_out == 0)) {
  81. // previous inflate filled output buffer. don't change input params yet.
  82. } else if (zcontext_.avail_in == 0) {
  83. const void* in;
  84. int in_size;
  85. bool first = zcontext_.next_in == NULL;
  86. bool ok = sub_stream_->Next(&in, &in_size);
  87. if (!ok) {
  88. zcontext_.next_out = NULL;
  89. zcontext_.avail_out = 0;
  90. return Z_STREAM_END;
  91. }
  92. zcontext_.next_in = static_cast<Bytef*>(const_cast<void*>(in));
  93. zcontext_.avail_in = in_size;
  94. if (first) {
  95. int error = internalInflateInit2(&zcontext_, format_);
  96. if (error != Z_OK) {
  97. return error;
  98. }
  99. }
  100. }
  101. zcontext_.next_out = static_cast<Bytef*>(output_buffer_);
  102. zcontext_.avail_out = output_buffer_length_;
  103. output_position_ = output_buffer_;
  104. int error = inflate(&zcontext_, flush);
  105. return error;
  106. }
  107. void GzipInputStream::DoNextOutput(const void** data, int* size) {
  108. *data = output_position_;
  109. *size = ((uintptr_t)zcontext_.next_out) - ((uintptr_t)output_position_);
  110. output_position_ = zcontext_.next_out;
  111. }
  112. // implements ZeroCopyInputStream ----------------------------------
  113. bool GzipInputStream::Next(const void** data, int* size) {
  114. bool ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END)
  115. || (zerror_ == Z_BUF_ERROR);
  116. if ((!ok) || (zcontext_.next_out == NULL)) {
  117. return false;
  118. }
  119. if (zcontext_.next_out != output_position_) {
  120. DoNextOutput(data, size);
  121. return true;
  122. }
  123. if (zerror_ == Z_STREAM_END) {
  124. if (zcontext_.next_out != NULL) {
  125. // sub_stream_ may have concatenated streams to follow
  126. zerror_ = inflateEnd(&zcontext_);
  127. byte_count_ += zcontext_.total_out;
  128. if (zerror_ != Z_OK) {
  129. return false;
  130. }
  131. zerror_ = internalInflateInit2(&zcontext_, format_);
  132. if (zerror_ != Z_OK) {
  133. return false;
  134. }
  135. } else {
  136. *data = NULL;
  137. *size = 0;
  138. return false;
  139. }
  140. }
  141. zerror_ = Inflate(Z_NO_FLUSH);
  142. if ((zerror_ == Z_STREAM_END) && (zcontext_.next_out == NULL)) {
  143. // The underlying stream's Next returned false inside Inflate.
  144. return false;
  145. }
  146. ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END)
  147. || (zerror_ == Z_BUF_ERROR);
  148. if (!ok) {
  149. return false;
  150. }
  151. DoNextOutput(data, size);
  152. return true;
  153. }
  154. void GzipInputStream::BackUp(int count) {
  155. output_position_ = reinterpret_cast<void*>(
  156. reinterpret_cast<uintptr_t>(output_position_) - count);
  157. }
  158. bool GzipInputStream::Skip(int count) {
  159. const void* data;
  160. int size = 0;
  161. bool ok = Next(&data, &size);
  162. while (ok && (size < count)) {
  163. count -= size;
  164. ok = Next(&data, &size);
  165. }
  166. if (size > count) {
  167. BackUp(size - count);
  168. }
  169. return ok;
  170. }
  171. int64 GzipInputStream::ByteCount() const {
  172. int64 ret = byte_count_ + zcontext_.total_out;
  173. if (zcontext_.next_out != NULL && output_position_ != NULL) {
  174. ret += reinterpret_cast<uintptr_t>(zcontext_.next_out) -
  175. reinterpret_cast<uintptr_t>(output_position_);
  176. }
  177. return ret;
  178. }
  179. // =========================================================================
  180. GzipOutputStream::Options::Options()
  181. : format(GZIP),
  182. buffer_size(kDefaultBufferSize),
  183. compression_level(Z_DEFAULT_COMPRESSION),
  184. compression_strategy(Z_DEFAULT_STRATEGY) {}
  185. GzipOutputStream::GzipOutputStream(ZeroCopyOutputStream* sub_stream) {
  186. Init(sub_stream, Options());
  187. }
  188. GzipOutputStream::GzipOutputStream(ZeroCopyOutputStream* sub_stream,
  189. const Options& options) {
  190. Init(sub_stream, options);
  191. }
  192. void GzipOutputStream::Init(ZeroCopyOutputStream* sub_stream,
  193. const Options& options) {
  194. sub_stream_ = sub_stream;
  195. sub_data_ = NULL;
  196. sub_data_size_ = 0;
  197. input_buffer_length_ = options.buffer_size;
  198. input_buffer_ = operator new(input_buffer_length_);
  199. GOOGLE_CHECK(input_buffer_ != NULL);
  200. zcontext_.zalloc = Z_NULL;
  201. zcontext_.zfree = Z_NULL;
  202. zcontext_.opaque = Z_NULL;
  203. zcontext_.next_out = NULL;
  204. zcontext_.avail_out = 0;
  205. zcontext_.total_out = 0;
  206. zcontext_.next_in = NULL;
  207. zcontext_.avail_in = 0;
  208. zcontext_.total_in = 0;
  209. zcontext_.msg = NULL;
  210. // default to GZIP format
  211. int windowBitsFormat = 16;
  212. if (options.format == ZLIB) {
  213. windowBitsFormat = 0;
  214. }
  215. zerror_ = deflateInit2(
  216. &zcontext_,
  217. options.compression_level,
  218. Z_DEFLATED,
  219. /* windowBits */15 | windowBitsFormat,
  220. /* memLevel (default) */8,
  221. options.compression_strategy);
  222. }
  223. GzipOutputStream::~GzipOutputStream() {
  224. Close();
  225. operator delete(input_buffer_);
  226. }
  227. // private
  228. int GzipOutputStream::Deflate(int flush) {
  229. int error = Z_OK;
  230. do {
  231. if ((sub_data_ == NULL) || (zcontext_.avail_out == 0)) {
  232. bool ok = sub_stream_->Next(&sub_data_, &sub_data_size_);
  233. if (!ok) {
  234. sub_data_ = NULL;
  235. sub_data_size_ = 0;
  236. return Z_BUF_ERROR;
  237. }
  238. GOOGLE_CHECK_GT(sub_data_size_, 0);
  239. zcontext_.next_out = static_cast<Bytef*>(sub_data_);
  240. zcontext_.avail_out = sub_data_size_;
  241. }
  242. error = deflate(&zcontext_, flush);
  243. } while (error == Z_OK && zcontext_.avail_out == 0);
  244. if ((flush == Z_FULL_FLUSH) || (flush == Z_FINISH)) {
  245. // Notify lower layer of data.
  246. sub_stream_->BackUp(zcontext_.avail_out);
  247. // We don't own the buffer anymore.
  248. sub_data_ = NULL;
  249. sub_data_size_ = 0;
  250. }
  251. return error;
  252. }
  253. // implements ZeroCopyOutputStream ---------------------------------
  254. bool GzipOutputStream::Next(void** data, int* size) {
  255. if ((zerror_ != Z_OK) && (zerror_ != Z_BUF_ERROR)) {
  256. return false;
  257. }
  258. if (zcontext_.avail_in != 0) {
  259. zerror_ = Deflate(Z_NO_FLUSH);
  260. if (zerror_ != Z_OK) {
  261. return false;
  262. }
  263. }
  264. if (zcontext_.avail_in == 0) {
  265. // all input was consumed. reset the buffer.
  266. zcontext_.next_in = static_cast<Bytef*>(input_buffer_);
  267. zcontext_.avail_in = input_buffer_length_;
  268. *data = input_buffer_;
  269. *size = input_buffer_length_;
  270. } else {
  271. // The loop in Deflate should consume all avail_in
  272. GOOGLE_LOG(DFATAL) << "Deflate left bytes unconsumed";
  273. }
  274. return true;
  275. }
  276. void GzipOutputStream::BackUp(int count) {
  277. GOOGLE_CHECK_GE(zcontext_.avail_in, count);
  278. zcontext_.avail_in -= count;
  279. }
  280. int64 GzipOutputStream::ByteCount() const {
  281. return zcontext_.total_in + zcontext_.avail_in;
  282. }
  283. bool GzipOutputStream::Flush() {
  284. zerror_ = Deflate(Z_FULL_FLUSH);
  285. // Return true if the flush succeeded or if it was a no-op.
  286. return (zerror_ == Z_OK) ||
  287. (zerror_ == Z_BUF_ERROR && zcontext_.avail_in == 0 &&
  288. zcontext_.avail_out != 0);
  289. }
  290. bool GzipOutputStream::Close() {
  291. if ((zerror_ != Z_OK) && (zerror_ != Z_BUF_ERROR)) {
  292. return false;
  293. }
  294. do {
  295. zerror_ = Deflate(Z_FINISH);
  296. } while (zerror_ == Z_OK);
  297. zerror_ = deflateEnd(&zcontext_);
  298. bool ok = zerror_ == Z_OK;
  299. zerror_ = Z_STREAM_END;
  300. return ok;
  301. }
  302. } // namespace io
  303. } // namespace protobuf
  304. } // namespace google
  305. #endif // HAVE_ZLIB