Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "vm/Compression.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "js/Utility.h" |
michael@0 | 10 | |
michael@0 | 11 | using namespace js; |
michael@0 | 12 | |
michael@0 | 13 | #if USE_ZLIB |
michael@0 | 14 | static void * |
michael@0 | 15 | zlib_alloc(void *cx, uInt items, uInt size) |
michael@0 | 16 | { |
michael@0 | 17 | return js_calloc(items, size); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | static void |
michael@0 | 21 | zlib_free(void *cx, void *addr) |
michael@0 | 22 | { |
michael@0 | 23 | js_free(addr); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | Compressor::Compressor(const unsigned char *inp, size_t inplen) |
michael@0 | 27 | : inp(inp), |
michael@0 | 28 | inplen(inplen), |
michael@0 | 29 | outbytes(0), |
michael@0 | 30 | initialized(false) |
michael@0 | 31 | { |
michael@0 | 32 | JS_ASSERT(inplen > 0); |
michael@0 | 33 | zs.opaque = nullptr; |
michael@0 | 34 | zs.next_in = (Bytef *)inp; |
michael@0 | 35 | zs.avail_in = 0; |
michael@0 | 36 | zs.next_out = nullptr; |
michael@0 | 37 | zs.avail_out = 0; |
michael@0 | 38 | zs.zalloc = zlib_alloc; |
michael@0 | 39 | zs.zfree = zlib_free; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | |
michael@0 | 43 | Compressor::~Compressor() |
michael@0 | 44 | { |
michael@0 | 45 | if (initialized) { |
michael@0 | 46 | int ret = deflateEnd(&zs); |
michael@0 | 47 | if (ret != Z_OK) { |
michael@0 | 48 | // If we finished early, we can get a Z_DATA_ERROR. |
michael@0 | 49 | JS_ASSERT(ret == Z_DATA_ERROR); |
michael@0 | 50 | JS_ASSERT(uInt(zs.next_in - inp) < inplen || !zs.avail_out); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | bool |
michael@0 | 56 | Compressor::init() |
michael@0 | 57 | { |
michael@0 | 58 | if (inplen >= UINT32_MAX) |
michael@0 | 59 | return false; |
michael@0 | 60 | // zlib is slow and we'd rather be done compression sooner |
michael@0 | 61 | // even if it means decompression is slower which penalizes |
michael@0 | 62 | // Function.toString() |
michael@0 | 63 | int ret = deflateInit(&zs, Z_BEST_SPEED); |
michael@0 | 64 | if (ret != Z_OK) { |
michael@0 | 65 | JS_ASSERT(ret == Z_MEM_ERROR); |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | initialized = true; |
michael@0 | 69 | return true; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | void |
michael@0 | 73 | Compressor::setOutput(unsigned char *out, size_t outlen) |
michael@0 | 74 | { |
michael@0 | 75 | JS_ASSERT(outlen > outbytes); |
michael@0 | 76 | zs.next_out = out + outbytes; |
michael@0 | 77 | zs.avail_out = outlen - outbytes; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | Compressor::Status |
michael@0 | 81 | Compressor::compressMore() |
michael@0 | 82 | { |
michael@0 | 83 | JS_ASSERT(zs.next_out); |
michael@0 | 84 | uInt left = inplen - (zs.next_in - inp); |
michael@0 | 85 | bool done = left <= CHUNKSIZE; |
michael@0 | 86 | if (done) |
michael@0 | 87 | zs.avail_in = left; |
michael@0 | 88 | else if (zs.avail_in == 0) |
michael@0 | 89 | zs.avail_in = CHUNKSIZE; |
michael@0 | 90 | Bytef *oldout = zs.next_out; |
michael@0 | 91 | int ret = deflate(&zs, done ? Z_FINISH : Z_NO_FLUSH); |
michael@0 | 92 | outbytes += zs.next_out - oldout; |
michael@0 | 93 | if (ret == Z_MEM_ERROR) { |
michael@0 | 94 | zs.avail_out = 0; |
michael@0 | 95 | return OOM; |
michael@0 | 96 | } |
michael@0 | 97 | if (ret == Z_BUF_ERROR || (done && ret == Z_OK)) { |
michael@0 | 98 | JS_ASSERT(zs.avail_out == 0); |
michael@0 | 99 | return MOREOUTPUT; |
michael@0 | 100 | } |
michael@0 | 101 | JS_ASSERT_IF(!done, ret == Z_OK); |
michael@0 | 102 | JS_ASSERT_IF(done, ret == Z_STREAM_END); |
michael@0 | 103 | return done ? DONE : CONTINUE; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | bool |
michael@0 | 107 | js::DecompressString(const unsigned char *inp, size_t inplen, unsigned char *out, size_t outlen) |
michael@0 | 108 | { |
michael@0 | 109 | JS_ASSERT(inplen <= UINT32_MAX); |
michael@0 | 110 | z_stream zs; |
michael@0 | 111 | zs.zalloc = zlib_alloc; |
michael@0 | 112 | zs.zfree = zlib_free; |
michael@0 | 113 | zs.opaque = nullptr; |
michael@0 | 114 | zs.next_in = (Bytef *)inp; |
michael@0 | 115 | zs.avail_in = inplen; |
michael@0 | 116 | zs.next_out = out; |
michael@0 | 117 | JS_ASSERT(outlen); |
michael@0 | 118 | zs.avail_out = outlen; |
michael@0 | 119 | int ret = inflateInit(&zs); |
michael@0 | 120 | if (ret != Z_OK) { |
michael@0 | 121 | JS_ASSERT(ret == Z_MEM_ERROR); |
michael@0 | 122 | return false; |
michael@0 | 123 | } |
michael@0 | 124 | ret = inflate(&zs, Z_FINISH); |
michael@0 | 125 | JS_ASSERT(ret == Z_STREAM_END); |
michael@0 | 126 | ret = inflateEnd(&zs); |
michael@0 | 127 | JS_ASSERT(ret == Z_OK); |
michael@0 | 128 | return true; |
michael@0 | 129 | } |
michael@0 | 130 | #endif /* USE_ZLIB */ |
michael@0 | 131 |