ee47a3a4a9b9e01fbd0e30a326b208d7a492ceb1
1 /*
2 * zlib wrappers to make sure we don't silently miss errors
3 * at init time.
4 */
5 #include "cache.h"
7 static const char *zerr_to_string(int status)
8 {
9 switch (status) {
10 case Z_MEM_ERROR:
11 return "out of memory";
12 case Z_VERSION_ERROR:
13 return "wrong version";
14 case Z_NEED_DICT:
15 return "needs dictionary";
16 case Z_DATA_ERROR:
17 return "data stream error";
18 case Z_STREAM_ERROR:
19 return "stream consistency error";
20 default:
21 return "unknown error";
22 }
23 }
25 void git_inflate_init(z_streamp strm)
26 {
27 int status = inflateInit(strm);
29 if (status == Z_OK)
30 return;
31 die("inflateInit: %s (%s)", zerr_to_string(status),
32 strm->msg ? strm->msg : "no message");
33 }
35 void git_inflate_init_gzip_only(z_streamp strm)
36 {
37 /*
38 * Use default 15 bits, +16 is to accept only gzip and to
39 * yield Z_DATA_ERROR when fed zlib format.
40 */
41 const int windowBits = 15 + 16;
42 int status = inflateInit2(strm, windowBits);
44 if (status == Z_OK)
45 return;
46 die("inflateInit2: %s (%s)", zerr_to_string(status),
47 strm->msg ? strm->msg : "no message");
48 }
50 void git_inflate_end(z_streamp strm)
51 {
52 int status = inflateEnd(strm);
54 if (status == Z_OK)
55 return;
56 error("inflateEnd: %s (%s)", zerr_to_string(status),
57 strm->msg ? strm->msg : "no message");
58 }
60 int git_inflate(z_streamp strm, int flush)
61 {
62 int status = inflate(strm, flush);
64 switch (status) {
65 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
66 case Z_BUF_ERROR:
67 case Z_OK:
68 case Z_STREAM_END:
69 return status;
71 case Z_MEM_ERROR:
72 die("inflate: out of memory");
73 default:
74 break;
75 }
76 error("inflate: %s (%s)", zerr_to_string(status),
77 strm->msg ? strm->msg : "no message");
78 return status;
79 }
81 void git_deflate_init(z_streamp strm, int level)
82 {
83 int status = deflateInit(strm, level);
85 if (status == Z_OK)
86 return;
87 die("deflateInit: %s (%s)", zerr_to_string(status),
88 strm->msg ? strm->msg : "no message");
89 }
91 void git_deflate_init_gzip(z_streamp strm, int level)
92 {
93 /*
94 * Use default 15 bits, +16 is to generate gzip header/trailer
95 * instead of the zlib wrapper.
96 */
97 const int windowBits = 15 + 16;
98 int status = deflateInit2(strm, level,
99 Z_DEFLATED, windowBits,
100 8, Z_DEFAULT_STRATEGY);
101 if (status == Z_OK)
102 return;
103 die("deflateInit2: %s (%s)", zerr_to_string(status),
104 strm->msg ? strm->msg : "no message");
105 }
107 void git_deflate_end(z_streamp strm)
108 {
109 int status = deflateEnd(strm);
111 if (status == Z_OK)
112 return;
113 error("deflateEnd: %s (%s)", zerr_to_string(status),
114 strm->msg ? strm->msg : "no message");
115 }
117 int git_deflate_end_gently(z_streamp strm)
118 {
119 return deflateEnd(strm);
120 }
122 int git_deflate(z_streamp strm, int flush)
123 {
124 int status = deflate(strm, flush);
126 switch (status) {
127 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
128 case Z_BUF_ERROR:
129 case Z_OK:
130 case Z_STREAM_END:
131 return status;
133 case Z_MEM_ERROR:
134 die("deflate: out of memory");
135 default:
136 break;
137 }
138 error("deflate: %s (%s)", zerr_to_string(status),
139 strm->msg ? strm->msg : "no message");
140 return status;
141 }