| 6 |
| 7 | namespace tgcalls { |
| 8 | namespace { |
| 9 |
| 10 | using uint = decltype(z_stream::avail_in); |
| 11 |
| 12 | } // namespace |
| 13 |
| 14 | bool isGzip(std::vector<uint8_t> const &data) { |
| 15 | if (data.size() < 2) { |
| 16 | return false; |
| 17 | } |
| 18 |
| 19 | if ((data[0] == 0x1f && data[1] == 0x8b) || (data[0] == 0x78 && data[1] == 0x9c)) { |
| 20 | return true; |
| 21 | } else { |
| 22 | return false; |
| 23 | } |
| 24 | } |
| 25 |
| 26 | absl::optional<std::vector<uint8_t>> gzipData(std::vector<uint8_t> const &data) { |
| 27 | z_stream stream; |
| 28 | stream.zalloc = Z_NULL; |
| 29 | stream.zfree = Z_NULL; |
| 30 | stream.opaque = Z_NULL; |
| 31 | stream.avail_in = (uint)data.size(); |
| 32 | stream.next_in = (Bytef *)(void *)data.data(); |
| 33 | stream.total_out = 0; |
| 34 | stream.avail_out = 0; |
| 35 |
| 36 | static const uint ChunkSize = 16384; |
| 37 |
| 38 | std::vector<uint8_t> output; |
| 39 | int compression = 9; |
| 40 | if (deflateInit2(&stream, compression, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) == Z_OK) { |
| 41 | output.resize(ChunkSize); |
| 42 |
| 43 | while (stream.avail_out == 0) { |
| 44 | if (stream.total_out >= output.size()) { |
| 45 | output.resize(output.size() + ChunkSize); |
| 46 | } |
| 47 | stream.next_out = (uint8_t *)output.data() + stream.total_out; |
| 48 | stream.avail_out = (uInt)(output.size() - stream.total_out); |
| 49 | deflate(&stream, Z_FINISH); |
| 50 | } |
| 51 | deflateEnd(&stream); |
| 52 | output.resize(stream.total_out); |
| 53 | } |
| 54 |
| 55 | return output; |
| 56 | } |
| 57 |
| 58 | absl::optional<std::vector<uint8_t>> gunzipData(std::vector<uint8_t> const &data, size_t sizeLimit) { |
| 59 | if (!isGzip(data)) { |
| 60 | return absl::nullopt; |
| 61 | } |
| 62 |
| 63 | z_stream stream; |
| 64 | stream.zalloc = Z_NULL; |
| 65 | stream.zfree = Z_NULL; |
| 66 | stream.avail_in = (uint)data.size(); |
| 67 | stream.next_in = (Bytef *)data.data(); |
| 68 | stream.total_out = 0; |
| 69 | stream.avail_out = 0; |
| 70 |
| 71 | std::vector<uint8_t> output; |
| 72 | if (inflateInit2(&stream, 47) == Z_OK) { |
| 73 | int status = Z_OK; |
| 74 | output.resize(data.size() * 2); |
| 75 | while (status == Z_OK) { |
| 76 | if (sizeLimit > 0 && stream.total_out > sizeLimit) { |
| 77 | return absl::nullopt; |
| 78 | } |
| 79 |
| 80 | if (stream.total_out >= output.size()) { |
| 81 | output.resize(output.size() + data.size() / 2); |
| 82 | } |
| 83 | stream.next_out = (uint8_t *)output.data() + stream.total_out; |
| 84 | stream.avail_out = (uInt)(output.size() - stream.total_out); |
| 85 | status = inflate(&stream, Z_SYNC_FLUSH); |
| 86 | } |
| 87 | if (inflateEnd(&stream) == Z_OK) { |
| 88 | if (status == Z_STREAM_END) { |
| 89 | output.resize(stream.total_out); |
| 90 | } else if (sizeLimit > 0 && output.size() > sizeLimit) { |
| 91 | return absl::nullopt; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 |
| 96 | return output; |
| 97 | } |
| 98 |
| 99 | } |
| 100 |
| 101 | /*bool TGIsGzippedData(NSData *data) { |
| 102 | const UInt8 *bytes = (const UInt8 *)data.bytes; |
| 103 | return data.length >= 2 && ((bytes[0] == 0x1f && bytes[1] == 0x8b) || (bytes[0] == 0x78 && bytes[1] == 0x9c)); |
| 104 | } |
| 105 |
| 106 | NSData *TGGZipData(NSData *data, float level) { |
| 107 | if (data.length == 0 || TGIsGzippedData(data)) { |
| 108 | return data; |
| 109 | } |
| 110 |
| 111 | z_stream stream; |
| 112 | stream.zalloc = Z_NULL; |
| 113 | stream.zfree = Z_NULL; |
| 114 | stream.opaque = Z_NULL; |
| 115 | stream.avail_in = (uint)data.length; |
| 116 | stream.next_in = (Bytef *)(void *)data.bytes; |
| 117 | stream.total_out = 0; |
| 118 | stream.avail_out = 0; |
| 119 |
| 120 | static const NSUInteger ChunkSize = 16384; |
| 121 |
| 122 | NSMutableData *output = nil; |
| 123 | int compression = (level < 0.0f) ? Z_DEFAULT_COMPRESSION : (int)(roundf(level * 9)); |
| 124 | if (deflateInit2(&stream, compression, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) == Z_OK) { |
| 125 | output = [NSMutableData dataWithLength:ChunkSize]; |
| 126 | while (stream.avail_out == 0) { |
| 127 | if (stream.total_out >= output.length) { |
| 128 | output.length += ChunkSize; |
| 129 | } |
| 130 | stream.next_out = (uint8_t *)output.mutableBytes + stream.total_out; |
| 131 | stream.avail_out = (uInt)(output.length - stream.total_out); |
| 132 | deflate(&stream, Z_FINISH); |
| 133 | } |
| 134 | deflateEnd(&stream); |
| 135 | output.length = stream.total_out; |
| 136 | } |
| 137 |
| 138 | return output; |
| 139 | } |
| 140 |
| 141 | NSData * _Nullable TGGUnzipData(NSData *data, uint sizeLimit) |
| 142 | { |
| 143 | if (data.length == 0 || !TGIsGzippedData(data)) { |
| 144 | return nil; |
| 145 | } |
| 146 |
| 147 | z_stream stream; |
| 148 | stream.zalloc = Z_NULL; |
| 149 | stream.zfree = Z_NULL; |
| 150 | stream.avail_in = (uint)data.length; |
| 151 | stream.next_in = (Bytef *)data.bytes; |
| 152 | stream.total_out = 0; |
| 153 | stream.avail_out = 0; |
| 154 |
| 155 | NSMutableData *output = nil; |
| 156 | if (inflateInit2(&stream, 47) == Z_OK) { |
| 157 | int status = Z_OK; |
| 158 | output = [NSMutableData dataWithCapacity:data.length * 2]; |
| 159 | while (status == Z_OK) { |
| 160 | if (sizeLimit > 0 && stream.total_out > sizeLimit) { |
| 161 | return nil; |
| 162 | } |
| 163 |
| 164 | if (stream.total_out >= output.length) { |
| 165 | output.length = output.length + data.length / 2; |
| 166 | } |
| 167 | stream.next_out = (uint8_t *)output.mutableBytes + stream.total_out; |
| 168 | stream.avail_out = (uInt)(output.length - stream.total_out); |
| 169 | status = inflate(&stream, Z_SYNC_FLUSH); |
| 170 | } |
| 171 | if (inflateEnd(&stream) == Z_OK) { |
| 172 | if (status == Z_STREAM_END) { |
| 173 | output.length = stream.total_out; |
| 174 | } else if (sizeLimit > 0 && output.length > sizeLimit) { |
| 175 | return nil; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 |
| 180 | return output; |
| 181 | } |
| 182 |
| 183 | */ |
| 184 |