Path: blob/master/Utilities/cmnghttp2/lib/nghttp2_hd_huffman.c
3153 views
/*1* nghttp2 - HTTP/2 C Library2*3* Copyright (c) 2013 Tatsuhiro Tsujikawa4*5* Permission is hereby granted, free of charge, to any person obtaining6* a copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sublicense, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice shall be14* included in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE20* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*/24#include "nghttp2_hd_huffman.h"2526#include <string.h>27#include <assert.h>28#include <stdio.h>2930#include "nghttp2_hd.h"31#include "nghttp2_net.h"3233size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len) {34size_t i;35size_t nbits = 0;3637for (i = 0; i < len; ++i) {38nbits += huff_sym_table[src[i]].nbits;39}40/* pad the prefix of EOS (256) */41return (nbits + 7) / 8;42}4344int nghttp2_hd_huff_encode(nghttp2_bufs *bufs, const uint8_t *src,45size_t srclen) {46const nghttp2_huff_sym *sym;47const uint8_t *end = src + srclen;48uint64_t code = 0;49uint32_t x;50size_t nbits = 0;51size_t avail;52int rv;5354avail = nghttp2_bufs_cur_avail(bufs);5556for (; src != end;) {57sym = &huff_sym_table[*src++];58code |= (uint64_t)sym->code << (32 - nbits);59nbits += sym->nbits;60if (nbits < 32) {61continue;62}63if (avail >= 4) {64x = htonl((uint32_t)(code >> 32));65memcpy(bufs->cur->buf.last, &x, 4);66bufs->cur->buf.last += 4;67avail -= 4;68code <<= 32;69nbits -= 32;70continue;71}7273for (; nbits >= 8;) {74rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 56));75if (rv != 0) {76return rv;77}78code <<= 8;79nbits -= 8;80}8182avail = nghttp2_bufs_cur_avail(bufs);83}8485for (; nbits >= 8;) {86rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 56));87if (rv != 0) {88return rv;89}90code <<= 8;91nbits -= 8;92}9394if (nbits) {95rv = nghttp2_bufs_addb(96bufs, (uint8_t)((uint8_t)(code >> 56) | ((1 << (8 - nbits)) - 1)));97if (rv != 0) {98return rv;99}100}101102return 0;103}104105void nghttp2_hd_huff_decode_context_init(nghttp2_hd_huff_decode_context *ctx) {106ctx->fstate = NGHTTP2_HUFF_ACCEPTED;107}108109ssize_t nghttp2_hd_huff_decode(nghttp2_hd_huff_decode_context *ctx,110nghttp2_buf *buf, const uint8_t *src,111size_t srclen, int final) {112const uint8_t *end = src + srclen;113nghttp2_huff_decode node = {ctx->fstate, 0};114const nghttp2_huff_decode *t = &node;115uint8_t c;116117/* We use the decoding algorithm described in118http://graphics.ics.uci.edu/pub/Prefix.pdf */119for (; src != end;) {120c = *src++;121t = &huff_decode_table[t->fstate & 0x1ff][c >> 4];122if (t->fstate & NGHTTP2_HUFF_SYM) {123*buf->last++ = t->sym;124}125126t = &huff_decode_table[t->fstate & 0x1ff][c & 0xf];127if (t->fstate & NGHTTP2_HUFF_SYM) {128*buf->last++ = t->sym;129}130}131132ctx->fstate = t->fstate;133134if (final && !(ctx->fstate & NGHTTP2_HUFF_ACCEPTED)) {135return NGHTTP2_ERR_HEADER_COMP;136}137138return (ssize_t)srclen;139}140141int nghttp2_hd_huff_decode_failure_state(nghttp2_hd_huff_decode_context *ctx) {142return ctx->fstate == 0x100;143}144145146