| 42 | |
| 43 | |
| 44 | bool inflate_data(datablock &p_compressed_data, datablock &p_uncompressed_data) |
| 45 | { |
| 46 | z_stream stream; |
| 47 | int ret; |
| 48 | bool ok = true; |
| 49 | |
| 50 | memset(&stream, 0, sizeof(stream)); |
| 51 | stream.next_in = reinterpret_cast < Bytef* > (&(p_compressed_data[0])); |
| 52 | stream.avail_in = p_compressed_data.size(); |
| 53 | stream.next_out = reinterpret_cast < Bytef* > (&(p_uncompressed_data[0])); |
| 54 | stream.avail_out = p_uncompressed_data.size(); |
| 55 | |
| 56 | // -MAX_WBITS is necessary for deflated data in a zip |
| 57 | if (ok && ((ret = inflateInit2(&stream, -MAX_WBITS)) != Z_OK)) |
| 58 | { |
| 59 | LOG_MSG(error, "could not initialize zlib inflate: " << zError(ret)); |
| 60 | ok = false; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | if (ok) |
| 65 | { |
| 66 | ret = inflate(&stream, Z_SYNC_FLUSH); |
| 67 | |
| 68 | if ((ret != Z_OK) && (ret != Z_STREAM_END)) |
| 69 | { |
| 70 | LOG_MSG(error, "could not inflate data: " << zError(ret)); |
| 71 | ok = false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // uninitialize even if inflate() failed to properly clean up zlib resources |
| 76 | if ((ret = inflateEnd(&stream) != Z_OK)) |
| 77 | { |
| 78 | LOG_MSG(error, "error while cleaning up zlib inflate: " << zError(ret)); |
| 79 | ok = false; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return ok; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | } // unnamed namespace end |