diff options
Diffstat (limited to 'debian/vendor-h2o/lib/handler/compress')
-rw-r--r-- | debian/vendor-h2o/lib/handler/compress/brotli.cc | 138 | ||||
-rw-r--r-- | debian/vendor-h2o/lib/handler/compress/gzip.c | 190 |
2 files changed, 0 insertions, 328 deletions
diff --git a/debian/vendor-h2o/lib/handler/compress/brotli.cc b/debian/vendor-h2o/lib/handler/compress/brotli.cc deleted file mode 100644 index d4d06e9..0000000 --- a/debian/vendor-h2o/lib/handler/compress/brotli.cc +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ -#include <cassert> -#include <cstdlib> -#include <vector> -#include <zlib.h> -#include "h2o.h" -#include "encode.h" - -namespace { - class brotli_context : public h2o_compress_context_t { - protected: - brotli::BrotliCompressor *brotli_; - brotli::BrotliParams params_; - std::vector<h2o_iovec_t> bufs_; // all bufs_[nnn].base must be free(3)ed - public: - brotli_context(int quality, size_t estimated_content_length) : brotli_(NULL) { - name = h2o_iovec_init(H2O_STRLIT("br")); - transform = _compress; - params_.quality = quality; - if (estimated_content_length != std::numeric_limits<size_t>::max()) - _update_lgwin(params_, estimated_content_length); - } - ~brotli_context() { - _clear_bufs(); - delete brotli_; - } - static void dispose(void *_self) { - brotli_context *self = static_cast<brotli_context *>(_self); - self->~brotli_context(); - } - private: - void _clear_bufs(); - void _emit(bool is_last, bool force_flush); - void _compress(h2o_iovec_t *inbufs, size_t inbufcnt, int is_final, h2o_iovec_t **outbufs, size_t *outbufcnt); - static void _compress(h2o_compress_context_t *self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state, - h2o_iovec_t **outbufs, size_t *outbufcnt) { - static_cast<brotli_context*>(self)->_compress(inbufs, inbufcnt, !h2o_send_state_is_in_progress(state), outbufs, outbufcnt); - } - static void _update_lgwin(brotli::BrotliParams ¶ms, size_t estimated_content_length); - }; -} - -void brotli_context::_clear_bufs() -{ - for (std::vector<h2o_iovec_t>::iterator i = bufs_.begin(); i != bufs_.end(); ++i) - free(i->base); - bufs_.clear(); -} - -void brotli_context::_emit(bool is_last, bool force_flush) -{ - uint8_t *output; - size_t out_size; - bool ret = brotli_->WriteBrotliData(is_last, force_flush, &out_size, &output); - assert(ret); - (void)ret; - if (out_size != 0) - bufs_.push_back(h2o_strdup(NULL, reinterpret_cast<const char *>(output), out_size)); -} - -void brotli_context::_compress(h2o_iovec_t *inbufs, size_t inbufcnt, int is_final, h2o_iovec_t **outbufs, size_t *outbufcnt) -{ - if (brotli_ == NULL) { - if (is_final) { - uint64_t len = 0; - for (size_t i = 0; i != inbufcnt; ++i) - len += inbufs[i].len; - if (len < std::numeric_limits<size_t>::max()) - _update_lgwin(params_, len); - } - brotli_ = new brotli::BrotliCompressor(params_); - } - - _clear_bufs(); - - if (inbufcnt != 0) { - size_t inbufindex = 0, offset = 0, block_space = brotli_->input_block_size(); - while (inbufindex != inbufcnt) { - size_t copy_len = std::min(block_space, inbufs[inbufindex].len - offset); - brotli_->CopyInputToRingBuffer(copy_len, reinterpret_cast<const uint8_t *>(inbufs[inbufindex].base) + offset); - offset += copy_len; - if (inbufs[inbufindex].len == offset) { - if (++inbufindex == inbufcnt) - break; - offset = 0; - } - if (block_space == 0) { - _emit(false, false); - block_space = brotli_->input_block_size(); - } - } - _emit(is_final, !is_final); - } else { - if (is_final) - _emit(true, false); - } - - if (is_final) { - delete brotli_; - brotli_ = NULL; - } - - *outbufs = &bufs_.front(); - *outbufcnt = bufs_.size(); -} - -void brotli_context::_update_lgwin(brotli::BrotliParams ¶ms, size_t estimated_content_length) -{ - int bits = estimated_content_length > 1 ? sizeof(unsigned long long) * 8 - __builtin_clzll(estimated_content_length - 1) : 1; - if (bits < params.lgwin) - params.lgwin = std::max(bits, brotli::kMinWindowBits); -} - -h2o_compress_context_t *h2o_compress_brotli_open(h2o_mem_pool_t *pool, int quality, size_t estimated_content_length) -{ - brotli_context *ctx = static_cast<brotli_context *>(h2o_mem_alloc_shared(pool, sizeof(*ctx), brotli_context::dispose)); - return new (ctx) brotli_context(quality, estimated_content_length); -} diff --git a/debian/vendor-h2o/lib/handler/compress/gzip.c b/debian/vendor-h2o/lib/handler/compress/gzip.c deleted file mode 100644 index c12260d..0000000 --- a/debian/vendor-h2o/lib/handler/compress/gzip.c +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ -#include <assert.h> -#include <stdlib.h> -#include <zlib.h> -#include "h2o.h" - -#define WINDOW_BITS 31 -#ifndef BUF_SIZE /* is altered by unit test */ -#define BUF_SIZE 8192 -#endif - -typedef H2O_VECTOR(h2o_iovec_t) iovec_vector_t; - -struct st_gzip_context_t { - h2o_compress_context_t super; - z_stream zs; - int zs_is_open; - iovec_vector_t bufs; -}; - -static void do_compress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state, - h2o_iovec_t **outbufs, size_t *outbufcnt); -static void do_decompress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state, - h2o_iovec_t **outbufs, size_t *outbufcnt); - -static void *alloc_cb(void *_unused, unsigned int cnt, unsigned int sz) -{ - return h2o_mem_alloc(cnt * (size_t)sz); -} - -static void free_cb(void *_unused, void *p) -{ - free(p); -} - -static void expand_buf(iovec_vector_t *bufs) -{ - h2o_vector_reserve(NULL, bufs, bufs->size + 1); - bufs->entries[bufs->size++] = h2o_iovec_init(h2o_mem_alloc(BUF_SIZE), 0); -} - -typedef int (*processor)(z_streamp strm, int flush); - -static size_t process_chunk(struct st_gzip_context_t *self, const void *src, size_t len, int flush, size_t bufindex, processor proc) -{ - int ret; - - self->zs.next_in = (void *)src; - self->zs.avail_in = (unsigned)len; - - /* man says: If inflate/deflate returns with avail_out == 0, this function must be called again with the same value of the flush - * parameter and more output space (updated avail_out), until the flush is complete (function returns with non-zero avail_out). - */ - do { - /* expand buffer (note: in case of Z_SYNC_FLUSH we need to supply at least 6 bytes of output buffer) */ - if (self->bufs.entries[bufindex].len + 32 > BUF_SIZE) { - ++bufindex; - if (bufindex == self->bufs.size) - expand_buf(&self->bufs); - self->bufs.entries[bufindex].len = 0; - } - self->zs.next_out = (void *)(self->bufs.entries[bufindex].base + self->bufs.entries[bufindex].len); - self->zs.avail_out = (unsigned)(BUF_SIZE - self->bufs.entries[bufindex].len); - ret = proc(&self->zs, flush); - /* inflate() returns Z_BUF_ERROR if flush is set to Z_FINISH at the middle of the compressed data */ - assert(ret == Z_OK || ret == Z_STREAM_END || ret == Z_BUF_ERROR); - self->bufs.entries[bufindex].len = BUF_SIZE - self->zs.avail_out; - } while (self->zs.avail_out == 0 && ret != Z_STREAM_END); - - return bufindex; -} - -static void do_process(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state, - h2o_iovec_t **outbufs, size_t *outbufcnt, processor proc) -{ - struct st_gzip_context_t *self = (void *)_self; - size_t outbufindex; - h2o_iovec_t last_buf; - - outbufindex = 0; - self->bufs.entries[0].len = 0; - - if (inbufcnt != 0) { - size_t i; - for (i = 0; i != inbufcnt - 1; ++i) - outbufindex = process_chunk(self, inbufs[i].base, inbufs[i].len, Z_NO_FLUSH, outbufindex, proc); - last_buf = inbufs[i]; - } else { - last_buf = h2o_iovec_init(NULL, 0); - } - outbufindex = process_chunk(self, last_buf.base, last_buf.len, h2o_send_state_is_in_progress(state) ? Z_SYNC_FLUSH : Z_FINISH, - outbufindex, proc); - - *outbufs = self->bufs.entries; - *outbufcnt = outbufindex + 1; - - if (!h2o_send_state_is_in_progress(state)) { - if (self->super.transform == do_compress) { - deflateEnd(&self->zs); - } else { - inflateEnd(&self->zs); - } - self->zs_is_open = 0; - } -} - -static void do_compress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state, - h2o_iovec_t **outbufs, size_t *outbufcnt) -{ - do_process(_self, inbufs, inbufcnt, state, outbufs, outbufcnt, (processor)deflate); -} - -static void do_decompress(h2o_compress_context_t *_self, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_send_state_t state, - h2o_iovec_t **outbufs, size_t *outbufcnt) -{ - do_process(_self, inbufs, inbufcnt, state, outbufs, outbufcnt, (processor)inflate); -} - -static void do_free(void *_self) -{ - struct st_gzip_context_t *self = _self; - size_t i; - - if (self->zs_is_open) { - if (self->super.transform == do_compress) { - deflateEnd(&self->zs); - } else { - inflateEnd(&self->zs); - } - } - - for (i = 0; i != self->bufs.size; ++i) - free(self->bufs.entries[i].base); - free(self->bufs.entries); -} - -static struct st_gzip_context_t *gzip_open(h2o_mem_pool_t *pool) -{ - struct st_gzip_context_t *self = h2o_mem_alloc_shared(pool, sizeof(*self), do_free); - - self->super.name = h2o_iovec_init(H2O_STRLIT("gzip")); - self->super.transform = NULL; - self->zs.zalloc = alloc_cb; - self->zs.zfree = free_cb; - self->zs.opaque = NULL; - self->zs_is_open = 1; - self->bufs = (iovec_vector_t){NULL}; - expand_buf(&self->bufs); - - return self; -} - -h2o_compress_context_t *h2o_compress_gzip_open(h2o_mem_pool_t *pool, int quality) -{ - struct st_gzip_context_t *self = gzip_open(pool); - self->super.transform = do_compress; - /* Z_BEST_SPEED for on-the-fly compression, memlevel set to 8 as suggested by the manual */ - deflateInit2(&self->zs, quality, Z_DEFLATED, WINDOW_BITS, 8, Z_DEFAULT_STRATEGY); - - return &self->super; -} - -h2o_compress_context_t *h2o_compress_gunzip_open(h2o_mem_pool_t *pool) -{ - struct st_gzip_context_t *self = gzip_open(pool); - self->super.transform = do_decompress; - inflateInit2(&self->zs, WINDOW_BITS); - - return &self->super; -} |