THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
6.1KB

  1. /*
  2. * zlib wrappers to make sure we don't silently miss errors
  3. * at init time.
  4. */
  5. #include "cache.h"
  6. static const char *zerr_to_string(int status)
  7. {
  8. switch (status) {
  9. case Z_MEM_ERROR:
  10. return "out of memory";
  11. case Z_VERSION_ERROR:
  12. return "wrong version";
  13. case Z_NEED_DICT:
  14. return "needs dictionary";
  15. case Z_DATA_ERROR:
  16. return "data stream error";
  17. case Z_STREAM_ERROR:
  18. return "stream consistency error";
  19. default:
  20. return "unknown error";
  21. }
  22. }
  23. /*
  24. * avail_in and avail_out in zlib are counted in uInt, which typically
  25. * limits the size of the buffer we can use to 4GB when interacting
  26. * with zlib in a single call to inflate/deflate.
  27. */
  28. /* #define ZLIB_BUF_MAX ((uInt)-1) */
  29. #define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
  30. static inline uInt zlib_buf_cap(unsigned long len)
  31. {
  32. return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
  33. }
  34. static void zlib_pre_call(git_zstream *s)
  35. {
  36. s->z.next_in = s->next_in;
  37. s->z.next_out = s->next_out;
  38. s->z.total_in = s->total_in;
  39. s->z.total_out = s->total_out;
  40. s->z.avail_in = zlib_buf_cap(s->avail_in);
  41. s->z.avail_out = zlib_buf_cap(s->avail_out);
  42. }
  43. static void zlib_post_call(git_zstream *s)
  44. {
  45. unsigned long bytes_consumed;
  46. unsigned long bytes_produced;
  47. bytes_consumed = s->z.next_in - s->next_in;
  48. bytes_produced = s->z.next_out - s->next_out;
  49. if (s->z.total_out != s->total_out + bytes_produced)
  50. BUG("total_out mismatch");
  51. if (s->z.total_in != s->total_in + bytes_consumed)
  52. BUG("total_in mismatch");
  53. s->total_out = s->z.total_out;
  54. s->total_in = s->z.total_in;
  55. s->next_in = s->z.next_in;
  56. s->next_out = s->z.next_out;
  57. s->avail_in -= bytes_consumed;
  58. s->avail_out -= bytes_produced;
  59. }
  60. void git_inflate_init(git_zstream *strm)
  61. {
  62. int status;
  63. zlib_pre_call(strm);
  64. status = inflateInit(&strm->z);
  65. zlib_post_call(strm);
  66. if (status == Z_OK)
  67. return;
  68. die("inflateInit: %s (%s)", zerr_to_string(status),
  69. strm->z.msg ? strm->z.msg : "no message");
  70. }
  71. void git_inflate_init_gzip_only(git_zstream *strm)
  72. {
  73. /*
  74. * Use default 15 bits, +16 is to accept only gzip and to
  75. * yield Z_DATA_ERROR when fed zlib format.
  76. */
  77. const int windowBits = 15 + 16;
  78. int status;
  79. zlib_pre_call(strm);
  80. status = inflateInit2(&strm->z, windowBits);
  81. zlib_post_call(strm);
  82. if (status == Z_OK)
  83. return;
  84. die("inflateInit2: %s (%s)", zerr_to_string(status),
  85. strm->z.msg ? strm->z.msg : "no message");
  86. }
  87. void git_inflate_end(git_zstream *strm)
  88. {
  89. int status;
  90. zlib_pre_call(strm);
  91. status = inflateEnd(&strm->z);
  92. zlib_post_call(strm);
  93. if (status == Z_OK)
  94. return;
  95. error("inflateEnd: %s (%s)", zerr_to_string(status),
  96. strm->z.msg ? strm->z.msg : "no message");
  97. }
  98. int git_inflate(git_zstream *strm, int flush)
  99. {
  100. int status;
  101. for (;;) {
  102. zlib_pre_call(strm);
  103. /* Never say Z_FINISH unless we are feeding everything */
  104. status = inflate(&strm->z,
  105. (strm->z.avail_in != strm->avail_in)
  106. ? 0 : flush);
  107. if (status == Z_MEM_ERROR)
  108. die("inflate: out of memory");
  109. zlib_post_call(strm);
  110. /*
  111. * Let zlib work another round, while we can still
  112. * make progress.
  113. */
  114. if ((strm->avail_out && !strm->z.avail_out) &&
  115. (status == Z_OK || status == Z_BUF_ERROR))
  116. continue;
  117. break;
  118. }
  119. switch (status) {
  120. /* Z_BUF_ERROR: normal, needs more space in the output buffer */
  121. case Z_BUF_ERROR:
  122. case Z_OK:
  123. case Z_STREAM_END:
  124. return status;
  125. default:
  126. break;
  127. }
  128. error("inflate: %s (%s)", zerr_to_string(status),
  129. strm->z.msg ? strm->z.msg : "no message");
  130. return status;
  131. }
  132. #if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
  133. #define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
  134. #endif
  135. unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
  136. {
  137. return deflateBound(&strm->z, size);
  138. }
  139. void git_deflate_init(git_zstream *strm, int level)
  140. {
  141. int status;
  142. memset(strm, 0, sizeof(*strm));
  143. zlib_pre_call(strm);
  144. status = deflateInit(&strm->z, level);
  145. zlib_post_call(strm);
  146. if (status == Z_OK)
  147. return;
  148. die("deflateInit: %s (%s)", zerr_to_string(status),
  149. strm->z.msg ? strm->z.msg : "no message");
  150. }
  151. static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
  152. {
  153. int status;
  154. memset(strm, 0, sizeof(*strm));
  155. zlib_pre_call(strm);
  156. status = deflateInit2(&strm->z, level,
  157. Z_DEFLATED, windowBits,
  158. 8, Z_DEFAULT_STRATEGY);
  159. zlib_post_call(strm);
  160. if (status == Z_OK)
  161. return;
  162. die("deflateInit2: %s (%s)", zerr_to_string(status),
  163. strm->z.msg ? strm->z.msg : "no message");
  164. }
  165. void git_deflate_init_gzip(git_zstream *strm, int level)
  166. {
  167. /*
  168. * Use default 15 bits, +16 is to generate gzip header/trailer
  169. * instead of the zlib wrapper.
  170. */
  171. do_git_deflate_init(strm, level, 15 + 16);
  172. }
  173. void git_deflate_init_raw(git_zstream *strm, int level)
  174. {
  175. /*
  176. * Use default 15 bits, negate the value to get raw compressed
  177. * data without zlib header and trailer.
  178. */
  179. do_git_deflate_init(strm, level, -15);
  180. }
  181. int git_deflate_abort(git_zstream *strm)
  182. {
  183. int status;
  184. zlib_pre_call(strm);
  185. status = deflateEnd(&strm->z);
  186. zlib_post_call(strm);
  187. return status;
  188. }
  189. void git_deflate_end(git_zstream *strm)
  190. {
  191. int status = git_deflate_abort(strm);
  192. if (status == Z_OK)
  193. return;
  194. error("deflateEnd: %s (%s)", zerr_to_string(status),
  195. strm->z.msg ? strm->z.msg : "no message");
  196. }
  197. int git_deflate_end_gently(git_zstream *strm)
  198. {
  199. int status;
  200. zlib_pre_call(strm);
  201. status = deflateEnd(&strm->z);
  202. zlib_post_call(strm);
  203. return status;
  204. }
  205. int git_deflate(git_zstream *strm, int flush)
  206. {
  207. int status;
  208. for (;;) {
  209. zlib_pre_call(strm);
  210. /* Never say Z_FINISH unless we are feeding everything */
  211. status = deflate(&strm->z,
  212. (strm->z.avail_in != strm->avail_in)
  213. ? 0 : flush);
  214. if (status == Z_MEM_ERROR)
  215. die("deflate: out of memory");
  216. zlib_post_call(strm);
  217. /*
  218. * Let zlib work another round, while we can still
  219. * make progress.
  220. */
  221. if ((strm->avail_out && !strm->z.avail_out) &&
  222. (status == Z_OK || status == Z_BUF_ERROR))
  223. continue;
  224. break;
  225. }
  226. switch (status) {
  227. /* Z_BUF_ERROR: normal, needs more space in the output buffer */
  228. case Z_BUF_ERROR:
  229. case Z_OK:
  230. case Z_STREAM_END:
  231. return status;
  232. default:
  233. break;
  234. }
  235. error("deflate: %s (%s)", zerr_to_string(status),
  236. strm->z.msg ? strm->z.msg : "no message");
  237. return status;
  238. }