AUTO_LT_SYNC
[tore.git] / libtorrent / src / gzip.cpp
blob9298162407d211b4a1f25019d68b2476ea2678da
1 /*
3 Copyright (c) 2007, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/assert.hpp"
35 #include "zlib.h"
37 #include <vector>
39 namespace
41 enum
43 FTEXT = 0x01,
44 FHCRC = 0x02,
45 FEXTRA = 0x04,
46 FNAME = 0x08,
47 FCOMMENT = 0x10,
48 FRESERVED = 0xe0,
50 GZIP_MAGIC0 = 0x1f,
51 GZIP_MAGIC1 = 0x8b
56 namespace libtorrent
58 // returns -1 if gzip header is invalid or the header size in bytes
59 int gzip_header(const char* buf, int size)
61 TORRENT_ASSERT(buf != 0);
62 TORRENT_ASSERT(size > 0);
64 const unsigned char* buffer = reinterpret_cast<const unsigned char*>(buf);
65 const int total_size = size;
67 // The zip header cannot be shorter than 10 bytes
68 if (size < 10) return -1;
70 // check the magic header of gzip
71 if ((buffer[0] != GZIP_MAGIC0) || (buffer[1] != GZIP_MAGIC1)) return -1;
73 int method = buffer[2];
74 int flags = buffer[3];
76 // check for reserved flag and make sure it's compressed with the correct metod
77 if (method != Z_DEFLATED || (flags & FRESERVED) != 0) return -1;
79 // skip time, xflags, OS code
80 size -= 10;
81 buffer += 10;
83 if (flags & FEXTRA)
85 int extra_len;
87 if (size < 2) return -1;
89 extra_len = (buffer[1] << 8) | buffer[0];
91 if (size < (extra_len+2)) return -1;
92 size -= (extra_len + 2);
93 buffer += (extra_len + 2);
96 if (flags & FNAME)
98 while (size && *buffer)
100 --size;
101 ++buffer;
103 if (!size || *buffer) return -1;
105 --size;
106 ++buffer;
109 if (flags & FCOMMENT)
111 while (size && *buffer)
113 --size;
114 ++buffer;
116 if (!size || *buffer) return -1;
118 --size;
119 ++buffer;
122 if (flags & FHCRC)
124 if (size < 2) return -1;
126 size -= 2;
127 buffer += 2;
130 return total_size - size;
133 bool inflate_gzip(
134 char const* in
135 , int size
136 , std::vector<char>& buffer
137 , int maximum_size
138 , std::string& error)
140 TORRENT_ASSERT(maximum_size > 0);
142 int header_len = gzip_header(in, size);
143 if (header_len < 0)
145 error = "invalid gzip header in tracker response";
146 return true;
149 // start off with one kilobyte and grow
150 // if needed
151 buffer.resize(1024);
153 // initialize the zlib-stream
154 z_stream str;
156 // subtract 8 from the end of the buffer since that's CRC32 and input size
157 // and those belong to the gzip file
158 str.avail_in = (int)size - header_len - 8;
159 str.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(in + header_len));
160 str.next_out = reinterpret_cast<Bytef*>(&buffer[0]);
161 str.avail_out = (int)buffer.size();
162 str.zalloc = Z_NULL;
163 str.zfree = Z_NULL;
164 str.opaque = 0;
165 // -15 is really important. It will make inflate() not look for a zlib header
166 // and just deflate the buffer
167 if (inflateInit2(&str, -15) != Z_OK)
169 error = "gzip out of memory";
170 return true;
173 // inflate and grow inflate_buffer as needed
174 int ret = inflate(&str, Z_SYNC_FLUSH);
175 while (ret == Z_OK)
177 if (str.avail_out == 0)
179 if (buffer.size() >= (unsigned)maximum_size)
181 inflateEnd(&str);
182 error = "response too large";
183 return true;
185 int new_size = (int)buffer.size() * 2;
186 if (new_size > maximum_size)
187 new_size = maximum_size;
188 int old_size = (int)buffer.size();
190 buffer.resize(new_size);
191 str.next_out = reinterpret_cast<Bytef*>(&buffer[old_size]);
192 str.avail_out = new_size - old_size;
195 ret = inflate(&str, Z_SYNC_FLUSH);
198 buffer.resize(buffer.size() - str.avail_out);
199 inflateEnd(&str);
201 if (ret != Z_STREAM_END)
203 error = "gzip error";
204 return true;
207 // commit the resulting buffer
208 return false;