1 /* 2 * nghttp3 3 * 4 * Copyright (c) 2019 nghttp3 contributors 5 * Copyright (c) 2013 nghttp2 contributors 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be 16 * included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 #ifndef NGHTTP3_QPACK_HUFFMAN_H 27 #define NGHTTP3_QPACK_HUFFMAN_H 28 29 #ifdef HAVE_CONFIG_H 30 # include <config.h> 31 #endif /* HAVE_CONFIG_H */ 32 33 #include <nghttp3/nghttp3.h> 34 35 typedef struct nghttp3_qpack_huffman_sym { 36 /* The number of bits in this code */ 37 uint32_t nbits; 38 /* Huffman code aligned to LSB */ 39 uint32_t code; 40 } nghttp3_qpack_huffman_sym; 41 42 extern const nghttp3_qpack_huffman_sym huffman_sym_table[]; 43 44 size_t nghttp3_qpack_huffman_encode_count(const uint8_t *src, size_t len); 45 46 uint8_t *nghttp3_qpack_huffman_encode(uint8_t *dest, const uint8_t *src, 47 size_t srclen); 48 49 typedef enum nghttp3_qpack_huffman_decode_flag { 50 /* FSA accepts this state as the end of huffman encoding 51 sequence. */ 52 NGHTTP3_QPACK_HUFFMAN_ACCEPTED = 1 << 14, 53 /* This state emits symbol */ 54 NGHTTP3_QPACK_HUFFMAN_SYM = 1 << 15, 55 } nghttp3_qpack_huffman_decode_flag; 56 57 typedef struct nghttp3_qpack_huffman_decode_node { 58 /* fstate is the current huffman decoding state, which is actually 59 the node ID of internal huffman tree with 60 nghttp3_qpack_huffman_decode_flag OR-ed. We have 257 leaf nodes, 61 but they are identical to root node other than emitting a symbol, 62 so we have 256 internal nodes [1..256], inclusive. The node ID 63 256 is a special node and it is a terminal state that means 64 decoding failed. */ 65 uint16_t fstate; 66 /* symbol if NGHTTP3_QPACK_HUFFMAN_SYM flag set */ 67 uint8_t sym; 68 } nghttp3_qpack_huffman_decode_node; 69 70 typedef struct nghttp3_qpack_huffman_decode_context { 71 /* fstate is the current huffman decoding state. */ 72 uint16_t fstate; 73 } nghttp3_qpack_huffman_decode_context; 74 75 extern const nghttp3_qpack_huffman_decode_node qpack_huffman_decode_table[][16]; 76 77 void nghttp3_qpack_huffman_decode_context_init( 78 nghttp3_qpack_huffman_decode_context *ctx); 79 80 /* 81 * nghttp3_qpack_huffman_decode decodes huffman encoded byte string 82 * stored in |src| of length |srclen|. |ctx| is a decoding context. 83 * |ctx| remembers the decoding state, and caller can call this 84 * function multiple times to feed each chunk of huffman encoded 85 * substring. |fin| must be nonzero if |src| contains the last chunk 86 * of huffman string. The decoded string is written to the buffer 87 * pointed by |dest|. This function assumes that the buffer pointed 88 * by |dest| contains enough memory to store decoded byte string. 89 * 90 * This function returns the number of bytes written to |dest|, or one 91 * of the following negative error codes: 92 * 93 * NGHTTP3_ERR_QPACK_FATAL 94 * Could not decode huffman string. 95 */ 96 nghttp3_ssize 97 nghttp3_qpack_huffman_decode(nghttp3_qpack_huffman_decode_context *ctx, 98 uint8_t *dest, const uint8_t *src, size_t srclen, 99 int fin); 100 101 /* 102 * nghttp3_qpack_huffman_decode_failure_state returns nonzero if |ctx| 103 * indicates that huffman decoding context is in failure state. 104 */ 105 int nghttp3_qpack_huffman_decode_failure_state( 106 nghttp3_qpack_huffman_decode_context *ctx); 107 108 #endif /* NGHTTP3_QPACK_HUFFMAN_H */ 109