lshpack.h revision 14e3680d
1/* Copyright (c) 2017 - 2018 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3MIT License 4 5Copyright (c) 2018 LiteSpeed Technologies Inc 6 7Permission is hereby granted, free of charge, to any person obtaining a copy 8of this software and associated documentation files (the "Software"), to deal 9in the Software without restriction, including without limitation the rights 10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11copies of the Software, and to permit persons to whom the Software is 12furnished to do so, subject to the following conditions: 13 14The above copyright notice and this permission notice shall be included in all 15copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23SOFTWARE. 24*/ 25 26#ifndef LITESPEED_HPACK_H 27#define LITESPEED_HPACK_H 1 28 29#ifdef __cplusplus 30extern "C" { 31#endif 32 33#include <stdint.h> 34 35/** 36 * Strings up to 65535 characters in length are supported. 37 */ 38typedef uint16_t lshpack_strlen_t; 39 40/** Maximum length is defined for convenience */ 41#define LSHPACK_MAX_STRLEN UINT16_MAX 42 43struct lshpack_enc; 44struct lshpack_dec; 45 46/** 47 * Initialization routine allocates memory. -1 is returned if memory 48 * could not be allocated. 0 is returned on success. 49 */ 50int 51lshpack_enc_init (struct lshpack_enc *); 52 53/** 54 * Clean up HPACK encoder, freeing all allocated memory. 55 */ 56void 57lshpack_enc_cleanup (struct lshpack_enc *); 58 59/** 60 * @brief Encode one name/value pair 61 * 62 * @param[in,out] henc - A pointer to a valid HPACK API struct 63 * @param[out] dst - A pointer to destination buffer 64 * @param[out] dst_end - A pointer to end of destination buffer 65 * @param[in] name - A pointer to the item name 66 * @param[in] name_len - The item name's length 67 * @param[in] value - A pointer to the item value 68 * @param[in] value_len - The item value's length 69 * @param[in] indexed_type - 0, Add, 1,: without, 2: never 70 * 71 * @return The (possibly advanced) dst pointer. If the destination 72 * pointer was not advanced, an error must have occurred. 73 */ 74unsigned char * 75lshpack_enc_encode (struct lshpack_enc *henc, unsigned char *dst, 76 unsigned char *dst_end, const char *name, lshpack_strlen_t name_len, 77 const char *value, lshpack_strlen_t value_len, int indexed_type); 78 79void 80lshpack_enc_set_max_capacity (struct lshpack_enc *, unsigned); 81 82/** 83 * Initialize HPACK decoder structure. 84 */ 85void 86lshpack_dec_init (struct lshpack_dec *); 87 88/** 89 * Clean up HPACK decoder structure, freeing all allocated memory. 90 */ 91void 92lshpack_dec_cleanup (struct lshpack_dec *); 93 94/* 95 * Returns 0 on success, a negative value on failure. 96 * 97 * If 0 is returned, `src' is advanced. Calling with a zero-length input 98 * buffer results in an error. 99 */ 100int 101lshpack_dec_decode (struct lshpack_dec *dec, 102 const unsigned char **src, const unsigned char *src_end, 103 char *dst, char *const dst_end, lshpack_strlen_t *name_len, 104 lshpack_strlen_t *val_len, uint32_t *name_idx); 105 106void 107lshpack_dec_set_max_capacity (struct lshpack_dec *, unsigned); 108 109/* Some internals follow. Struct definitions are exposed to save a malloc. 110 * These structures are not very complicated. 111 */ 112 113#include <sys/queue.h> 114 115struct lshpack_enc_table_entry; 116 117STAILQ_HEAD(lshpack_enc_head, lshpack_enc_table_entry); 118struct lshpack_double_enc_head; 119 120struct lshpack_enc 121{ 122 unsigned hpe_cur_capacity; 123 unsigned hpe_max_capacity; 124 125 /* Each new dynamic table entry gets the next number. It is used to 126 * calculate the entry's position in the decoder table without having 127 * to maintain an actual array. 128 */ 129 unsigned hpe_next_id; 130 131 /* Dynamic table entries (struct enc_table_entry) live in two hash 132 * tables: name/value hash table and name hash table. These tables 133 * are the same size. 134 */ 135 unsigned hpe_nelem; 136 unsigned hpe_nbits; 137 struct lshpack_enc_head 138 hpe_all_entries; 139 struct lshpack_double_enc_head 140 *hpe_buckets; 141}; 142 143struct lshpack_arr 144{ 145 unsigned nalloc, 146 nelem, 147 off; 148 uintptr_t *els; 149}; 150 151struct lshpack_dec 152{ 153 unsigned hpd_max_capacity; /* Maximum set by caller */ 154 unsigned hpd_cur_max_capacity; /* Adjusted at runtime */ 155 unsigned hpd_cur_capacity; 156 struct lshpack_arr hpd_dyn_table; 157}; 158 159unsigned 160lshpack_enc_get_stx_tab_id (const char *name, lshpack_strlen_t name_len, 161 const char *val, lshpack_strlen_t val_len, int *val_matched); 162 163#ifdef __cplusplus 164} 165#endif 166 167#endif 168