| 139 | } |
| 140 | |
| 141 | bool Inflate(std::streambuf& in, std::streambuf& out) { |
| 142 | |
| 143 | // Inflate the input file to the output file |
| 144 | z_stream o{}; |
| 145 | ByteArray buf_in(kZlibChunk, 0); |
| 146 | ByteArray buf_out(kZlibChunk, 0); |
| 147 | auto ret = inflateInit(&o); |
| 148 | if (ret != Z_OK) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | /* decompress until deflate stream ends or end of file */ |
| 153 | do { |
| 154 | o.avail_in = static_cast<uInt>(in.sgetn( |
| 155 | reinterpret_cast<char*>(buf_in.data()), |
| 156 | kZlibChunk) ); |
| 157 | if (o.avail_in == 0) { |
| 158 | break; |
| 159 | } |
| 160 | o.next_in = buf_in.data(); |
| 161 | |
| 162 | /* run inflate() on input until output buffer not full */ |
| 163 | do { |
| 164 | o.avail_out = kZlibChunk; |
| 165 | o.next_out = buf_out.data(); |
| 166 | ret = inflate(&o, Z_NO_FLUSH); |
| 167 | |
| 168 | switch (ret) { |
| 169 | case Z_STREAM_ERROR: |
| 170 | return false; |
| 171 | |
| 172 | case Z_NEED_DICT: |
| 173 | case Z_DATA_ERROR: |
| 174 | case Z_MEM_ERROR: |
| 175 | inflateEnd(&o); |
| 176 | return false; |
| 177 | |
| 178 | default: |
| 179 | break; |
| 180 | } |
| 181 | const auto have = kZlibChunk - o.avail_out; |
| 182 | if (out.sputn( |
| 183 | reinterpret_cast<char*>(buf_out.data()), |
| 184 | static_cast<std::streamsize>(have) ) != have ) { |
| 185 | inflateEnd(&o); |
| 186 | return false; |
| 187 | } |
| 188 | } while (o.avail_out == 0); |
| 189 | } while (ret != Z_STREAM_END); |
| 190 | |
| 191 | /* clean up and return */ |
| 192 | inflateEnd(&o); |
| 193 | return ret == Z_STREAM_END; |
| 194 | } |
| 195 | |
| 196 | bool Inflate(std::streambuf& in, std::streambuf& out, uint64_t nof_bytes) { |
| 197 | // Inflate the input file to the output file |