Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013-2017, Matt Godbolt |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "seasocks/ZlibContext.h" |
| 27 | |
| 28 | #include <memory> |
| 29 | #include <stdexcept> |
| 30 | |
| 31 | #include <zlib.h> |
| 32 | |
| 33 | namespace seasocks { |
| 34 | |
| 35 | struct ZlibContext::Impl { |
| 36 | z_stream deflateStream; |
| 37 | z_stream inflateStream; |
| 38 | bool streamsInitialised = false; |
| 39 | uint8_t buffer[16384]; |
| 40 | |
| 41 | Impl(int deflateBits, int inflateBits, int memLevel) { |
| 42 | int ret; |
| 43 | |
| 44 | deflateStream.zalloc = Z_NULL; |
| 45 | deflateStream.zfree = Z_NULL; |
| 46 | deflateStream.opaque = Z_NULL; |
| 47 | |
| 48 | ret = ::deflateInit2( |
| 49 | &deflateStream, |
| 50 | Z_DEFAULT_COMPRESSION, |
| 51 | Z_DEFLATED, |
| 52 | deflateBits * -1, |
| 53 | memLevel, |
| 54 | Z_DEFAULT_STRATEGY); |
| 55 | |
| 56 | if (ret != Z_OK) { |
| 57 | throw std::runtime_error("error initialising zlib deflater"); |
| 58 | } |
| 59 | |
| 60 | inflateStream.zalloc = Z_NULL; |
| 61 | inflateStream.zfree = Z_NULL; |
| 62 | inflateStream.opaque = Z_NULL; |
| 63 | inflateStream.avail_in = 0; |
| 64 | inflateStream.next_in = Z_NULL; |
| 65 | |
| 66 | ret = ::inflateInit2( |
| 67 | &inflateStream, |
| 68 | inflateBits * -1); |
| 69 | |
| 70 | if (ret != Z_OK) { |
| 71 | ::deflateEnd(&deflateStream); |
| 72 | throw std::runtime_error("error initialising zlib inflater"); |
| 73 | } |
| 74 | |
| 75 | streamsInitialised = true; |
| 76 | } |
| 77 | |
| 78 | ~Impl() { |
| 79 | if (!streamsInitialised) |
| 80 | return; |
| 81 | ::deflateEnd(&deflateStream); |
| 82 | ::inflateEnd(&inflateStream); |
| 83 | } |
| 84 | |
| 85 | void deflate(const uint8_t* input, size_t inputLen, std::vector<uint8_t>& output) { |
| 86 | deflateStream.next_in = (unsigned char*) input; |
| 87 | deflateStream.avail_in = inputLen; |
| 88 | |
| 89 | do { |
| 90 | deflateStream.next_out = buffer; |
| 91 | deflateStream.avail_out = sizeof(buffer); |
| 92 | |
| 93 | (void) ::deflate(&deflateStream, Z_SYNC_FLUSH); |
| 94 | |
| 95 | output.insert(output.end(), buffer, buffer + sizeof(buffer) - deflateStream.avail_out); |
| 96 | } while (deflateStream.avail_out == 0); |
| 97 | |
| 98 | // Remove 4-byte tail end prior to transmission (see RFC 7692, section 7.2.1) |
| 99 | output.resize(output.size() - 4); |
| 100 | } |
| 101 | |
| 102 | bool inflate(std::vector<uint8_t>& input, std::vector<uint8_t>& output, int& zlibError) { |
| 103 | // Append 4 octets prior to decompression (see RFC 7692, section 7.2.2) |
| 104 | uint8_t tail_end[4] = {0x00, 0x00, 0xff, 0xff}; |
| 105 | input.insert(input.end(), tail_end, tail_end + 4); |
| 106 | |
| 107 | inflateStream.next_in = input.data(); |
| 108 | inflateStream.avail_in = input.size(); |
| 109 | |
| 110 | do { |
| 111 | inflateStream.next_out = buffer; |
| 112 | inflateStream.avail_out = sizeof(buffer); |
| 113 | |
| 114 | int ret = ::inflate(&inflateStream, Z_SYNC_FLUSH); |
| 115 | |
| 116 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 117 | zlibError = ret; |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | output.insert(output.end(), buffer, buffer + sizeof(buffer) - inflateStream.avail_out); |
| 122 | } while (inflateStream.avail_out == 0); |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | ZlibContext::ZlibContext() = default; |
| 129 | |
| 130 | ZlibContext::~ZlibContext() = default; |
| 131 | |
| 132 | void ZlibContext::initialise(int deflateBits, int inflateBits, int memLevel) { |
| 133 | _impl = std::make_unique<Impl>(deflateBits, inflateBits, memLevel); |
| 134 | } |
| 135 | |
| 136 | void ZlibContext::deflate(const uint8_t* input, size_t inputLen, std::vector<uint8_t>& output) { |
| 137 | return _impl->deflate(input, inputLen, output); |
| 138 | } |
| 139 | |
| 140 | bool ZlibContext::inflate(std::vector<uint8_t>& input, std::vector<uint8_t>& output, int& zlibError) { |
| 141 | return _impl->inflate(input, output, zlibError); |
| 142 | } |
| 143 | |
| 144 | } |