Line data Source code
1 : /* 2 : * nghttp2 - HTTP/2 C Library 3 : * 4 : * Copyright (c) 2013 Tatsuhiro Tsujikawa 5 : * 6 : * Permission is hereby granted, free of charge, to any person obtaining 7 : * a copy of this software and associated documentation files (the 8 : * "Software"), to deal in the Software without restriction, including 9 : * without limitation the rights to use, copy, modify, merge, publish, 10 : * distribute, sublicense, and/or sell copies of the Software, and to 11 : * permit persons to whom the Software is furnished to do so, subject to 12 : * the following conditions: 13 : * 14 : * The above copyright notice and this permission notice shall be 15 : * included in all copies or substantial portions of the Software. 16 : * 17 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 : * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 : * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 : * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 : */ 25 : #include "nghttp2_hd_huffman.h" 26 : 27 : #include <string.h> 28 : #include <assert.h> 29 : #include <stdio.h> 30 : 31 : //#include "nghttp2_hd.h" 32 : //#include "nghttp2_net.h" 33 : 34 24 : void nghttp2_hd_huff_decode_context_init(nghttp2_hd_huff_decode_context *ctx) { 35 24 : ctx->fstate = NGHTTP2_HUFF_ACCEPTED; 36 24 : } 37 : 38 : nghttp2_ssize nghttp2_hd_huff_decode(nghttp2_hd_huff_decode_context *ctx, 39 : nghttp2_buf *buf, const uint8_t *src, 40 24 : size_t srclen, int final) { 41 24 : const uint8_t *end = src + srclen; 42 24 : nghttp2_huff_decode node = {ctx->fstate, 0}; 43 24 : const nghttp2_huff_decode *t = &node; 44 24 : uint8_t c; 45 : 46 : /* We use the decoding algorithm described in 47 : - http://graphics.ics.uci.edu/pub/Prefix.pdf [!!! NO LONGER VALID !!!] 48 : - https://ics.uci.edu/~dan/pubs/Prefix.pdf 49 : - https://github.com/nghttp2/nghttp2/files/15141264/Prefix.pdf */ 50 267 : for (; src != end;) { 51 243 : c = *src++; 52 243 : t = &huff_decode_table[t->fstate & 0x1ff][c >> 4]; 53 243 : if (t->fstate & NGHTTP2_HUFF_SYM) { 54 150 : *buf->last++ = t->sym; 55 150 : } 56 : 57 243 : t = &huff_decode_table[t->fstate & 0x1ff][c & 0xf]; 58 243 : if (t->fstate & NGHTTP2_HUFF_SYM) { 59 171 : *buf->last++ = t->sym; 60 171 : } 61 243 : } 62 : 63 24 : ctx->fstate = t->fstate; 64 : 65 24 : if (final && !(ctx->fstate & NGHTTP2_HUFF_ACCEPTED)) { 66 0 : return NGHTTP2_ERR_HEADER_COMP; 67 0 : } 68 : 69 24 : return (nghttp2_ssize)srclen; 70 24 : }