诸暨麻将添加redis
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

334 satır
11 KiB

  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(ZeroCopyInputStream* sub_stream, Format format,
  43. 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(z_stream* zcontext,
  70. GzipInputStream::Format format) {
  71. int windowBitsFormat = 0;
  72. switch (format) {
  73. case GzipInputStream::GZIP:
  74. windowBitsFormat = 16;
  75. break;
  76. case GzipInputStream::AUTO:
  77. windowBitsFormat = 32;
  78. break;
  79. case GzipInputStream::ZLIB:
  80. windowBitsFormat = 0;
  81. break;
  82. }
  83. return inflateInit2(zcontext, /* windowBits */ 15 | windowBitsFormat);
  84. }
  85. int GzipInputStream::Inflate(int flush) {
  86. if ((zerror_ == Z_OK) && (zcontext_.avail_out == 0)) {
  87. // previous inflate filled output buffer. don't change input params yet.
  88. } else if (zcontext_.avail_in == 0) {
  89. const void* in;
  90. int in_size;
  91. bool first = zcontext_.next_in == NULL;
  92. bool ok = sub_stream_->Next(&in, &in_size);
  93. if (!ok) {
  94. zcontext_.next_out = NULL;
  95. zcontext_.avail_out = 0;
  96. return Z_STREAM_END;
  97. }
  98. zcontext_.next_in = static_cast<Bytef*>(const_cast<void*>(in));
  99. zcontext_.avail_in = in_size;
  100. if (first) {
  101. int error = internalInflateInit2(&zcontext_, format_);
  102. if (error != Z_OK) {
  103. return error;
  104. }
  105. }
  106. }
  107. zcontext_.next_out = static_cast<Bytef*>(output_buffer_);
  108. zcontext_.avail_out = output_buffer_length_;
  109. output_position_ = output_buffer_;
  110. int error = inflate(&zcontext_, flush);
  111. return error;
  112. }
  113. void GzipInputStream::DoNextOutput(const void** data, int* size) {
  114. *data = output_position_;
  115. *size = ((uintptr_t)zcontext_.next_out) - ((uintptr_t)output_position_);
  116. output_position_ = zcontext_.next_out;
  117. }
  118. // implements ZeroCopyInputStream ----------------------------------
  119. bool GzipInputStream::Next(const void** data, int* size) {
  120. bool ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) ||
  121. (zerror_ == Z_BUF_ERROR);
  122. if ((!ok) || (zcontext_.next_out == NULL)) {
  123. return false;
  124. }
  125. if (zcontext_.next_out != output_position_) {
  126. DoNextOutput(data, size);
  127. return true;
  128. }
  129. if (zerror_ == Z_STREAM_END) {
  130. if (zcontext_.next_out != NULL) {
  131. // sub_stream_ may have concatenated streams to follow
  132. zerror_ = inflateEnd(&zcontext_);
  133. byte_count_ += zcontext_.total_out;
  134. if (zerror_ != Z_OK) {
  135. return false;
  136. }
  137. zerror_ = internalInflateInit2(&zcontext_, format_);
  138. if (zerror_ != Z_OK) {
  139. return false;
  140. }
  141. } else {
  142. *data = NULL;
  143. *size = 0;
  144. return false;
  145. }
  146. }
  147. zerror_ = Inflate(Z_NO_FLUSH);
  148. if ((zerror_ == Z_STREAM_END) && (zcontext_.next_out == NULL)) {
  149. // The underlying stream's Next returned false inside Inflate.
  150. return false;
  151. }
  152. ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) ||
  153. (zerror_ == Z_BUF_ERROR);
  154. if (!ok) {
  155. return false;
  156. }
  157. DoNextOutput(data, size);
  158. return true;
  159. }
  160. void GzipInputStream::BackUp(int count) {
  161. output_position_ = reinterpret_cast<void*>(
  162. reinterpret_cast<uintptr_t>(output_position_) - count);
  163. }
  164. bool GzipInputStream::Skip(int count) {
  165. const void* data;
  166. int size = 0;
  167. bool ok = Next(&data, &size);
  168. while (ok && (size < count)) {
  169. count -= size;
  170. ok = Next(&data, &size);
  171. }
  172. if (size > count) {
  173. BackUp(size - count);
  174. }
  175. return ok;
  176. }
  177. int64_t GzipInputStream::ByteCount() const {
  178. int64 ret = byte_count_ + zcontext_.total_out;
  179. if (zcontext_.next_out != NULL && output_position_ != NULL) {
  180. ret += reinterpret_cast<uintptr_t>(zcontext_.next_out) -
  181. reinterpret_cast<uintptr_t>(output_position_);
  182. }
  183. return ret;
  184. }
  185. // =========================================================================
  186. GzipOutputStream::Options::Options()
  187. : format(GZIP),
  188. buffer_size(kDefaultBufferSize),
  189. compression_level(Z_DEFAULT_COMPRESSION),
  190. compression_strategy(Z_DEFAULT_STRATEGY) {}
  191. GzipOutputStream::GzipOutputStream(ZeroCopyOutputStream* sub_stream) {
  192. Init(sub_stream, Options());
  193. }
  194. GzipOutputStream::GzipOutputStream(ZeroCopyOutputStream* sub_stream,
  195. const Options& options) {
  196. Init(sub_stream, options);
  197. }
  198. void GzipOutputStream::Init(ZeroCopyOutputStream* sub_stream,
  199. const Options& options) {
  200. sub_stream_ = sub_stream;
  201. sub_data_ = NULL;
  202. sub_data_size_ = 0;
  203. input_buffer_length_ = options.buffer_size;
  204. input_buffer_ = operator new(input_buffer_length_);
  205. GOOGLE_CHECK(input_buffer_ != NULL);
  206. zcontext_.zalloc = Z_NULL;
  207. zcontext_.zfree = Z_NULL;
  208. zcontext_.opaque = Z_NULL;
  209. zcontext_.next_out = NULL;
  210. zcontext_.avail_out = 0;
  211. zcontext_.total_out = 0;
  212. zcontext_.next_in = NULL;
  213. zcontext_.avail_in = 0;
  214. zcontext_.total_in = 0;
  215. zcontext_.msg = NULL;
  216. // default to GZIP format
  217. int windowBitsFormat = 16;
  218. if (options.format == ZLIB) {
  219. windowBitsFormat = 0;
  220. }
  221. zerror_ =
  222. deflateInit2(&zcontext_, options.compression_level, Z_DEFLATED,
  223. /* windowBits */ 15 | windowBitsFormat,
  224. /* memLevel (default) */ 8, options.compression_strategy);
  225. }
  226. GzipOutputStream::~GzipOutputStream() {
  227. Close();
  228. operator delete(input_buffer_);
  229. }
  230. // private
  231. int GzipOutputStream::Deflate(int flush) {
  232. int error = Z_OK;
  233. do {
  234. if ((sub_data_ == NULL) || (zcontext_.avail_out == 0)) {
  235. bool ok = sub_stream_->Next(&sub_data_, &sub_data_size_);
  236. if (!ok) {
  237. sub_data_ = NULL;
  238. sub_data_size_ = 0;
  239. return Z_BUF_ERROR;
  240. }
  241. GOOGLE_CHECK_GT(sub_data_size_, 0);
  242. zcontext_.next_out = static_cast<Bytef*>(sub_data_);
  243. zcontext_.avail_out = sub_data_size_;
  244. }
  245. error = deflate(&zcontext_, flush);
  246. } while (error == Z_OK && zcontext_.avail_out == 0);
  247. if ((flush == Z_FULL_FLUSH) || (flush == Z_FINISH)) {
  248. // Notify lower layer of data.
  249. sub_stream_->BackUp(zcontext_.avail_out);
  250. // We don't own the buffer anymore.
  251. sub_data_ = NULL;
  252. sub_data_size_ = 0;
  253. }
  254. return error;
  255. }
  256. // implements ZeroCopyOutputStream ---------------------------------
  257. bool GzipOutputStream::Next(void** data, int* size) {
  258. if ((zerror_ != Z_OK) && (zerror_ != Z_BUF_ERROR)) {
  259. return false;
  260. }
  261. if (zcontext_.avail_in != 0) {
  262. zerror_ = Deflate(Z_NO_FLUSH);
  263. if (zerror_ != Z_OK) {
  264. return false;
  265. }
  266. }
  267. if (zcontext_.avail_in == 0) {
  268. // all input was consumed. reset the buffer.
  269. zcontext_.next_in = static_cast<Bytef*>(input_buffer_);
  270. zcontext_.avail_in = input_buffer_length_;
  271. *data = input_buffer_;
  272. *size = input_buffer_length_;
  273. } else {
  274. // The loop in Deflate should consume all avail_in
  275. GOOGLE_LOG(DFATAL) << "Deflate left bytes unconsumed";
  276. }
  277. return true;
  278. }
  279. void GzipOutputStream::BackUp(int count) {
  280. GOOGLE_CHECK_GE(zcontext_.avail_in, count);
  281. zcontext_.avail_in -= count;
  282. }
  283. int64_t GzipOutputStream::ByteCount() const {
  284. return zcontext_.total_in + zcontext_.avail_in;
  285. }
  286. bool GzipOutputStream::Flush() {
  287. zerror_ = Deflate(Z_FULL_FLUSH);
  288. // Return true if the flush succeeded or if it was a no-op.
  289. return (zerror_ == Z_OK) ||
  290. (zerror_ == Z_BUF_ERROR && zcontext_.avail_in == 0 &&
  291. zcontext_.avail_out != 0);
  292. }
  293. bool GzipOutputStream::Close() {
  294. if ((zerror_ != Z_OK) && (zerror_ != Z_BUF_ERROR)) {
  295. return false;
  296. }
  297. do {
  298. zerror_ = Deflate(Z_FINISH);
  299. } while (zerror_ == Z_OK);
  300. zerror_ = deflateEnd(&zcontext_);
  301. bool ok = zerror_ == Z_OK;
  302. zerror_ = Z_STREAM_END;
  303. return ok;
  304. }
  305. } // namespace io
  306. } // namespace protobuf
  307. } // namespace google
  308. #endif // HAVE_ZLIB