lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2059 lines
62 KiB

  1. /*
  2. MIT License
  3. Copyright (c) 2018 LiteSpeed Technologies Inc
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. /*(lighttpd customization)*/
  21. #ifndef XXH_HEADER_NAME
  22. #define XXH_HEADER_NAME "algo_xxhash.h"
  23. #endif
  24. #ifndef LS_HPACK_USE_LARGE_TABLES
  25. #define LS_HPACK_USE_LARGE_TABLES 0
  26. #endif
  27. #define NDEBUG
  28. #include <assert.h>
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <sys/queue.h>
  33. #include "lshpack.h"
  34. #if LS_HPACK_EMIT_TEST_CODE
  35. #include "lshpack-test.h"
  36. #endif
  37. #include XXH_HEADER_NAME
  38. #ifndef LS_HPACK_USE_LARGE_TABLES
  39. #define LS_HPACK_USE_LARGE_TABLES 1
  40. #endif
  41. #include "huff-tables.h"
  42. #define HPACK_STATIC_TABLE_SIZE 61
  43. #define INITIAL_DYNAMIC_TABLE_SIZE 4096
  44. /* RFC 7541, Section 4.1:
  45. *
  46. * " The size of the dynamic table is the sum of the size of its entries.
  47. * "
  48. * " The size of an entry is the sum of its name's length in octets (as
  49. * " defined in Section 5.2), its value's length in octets, and 32.
  50. */
  51. #define DYNAMIC_ENTRY_OVERHEAD 32
  52. #define NAME_VAL(a, b) sizeof(a) - 1, sizeof(b) - 1, (a), (b)
  53. static const struct
  54. {
  55. unsigned name_len;
  56. unsigned val_len;
  57. const char *name;
  58. const char *val;
  59. }
  60. static_table[HPACK_STATIC_TABLE_SIZE] =
  61. {
  62. { NAME_VAL(":authority", "") },
  63. { NAME_VAL(":method", "GET") },
  64. { NAME_VAL(":method", "POST") },
  65. { NAME_VAL(":path", "/") },
  66. { NAME_VAL(":path", "/index.html") },
  67. { NAME_VAL(":scheme", "http") },
  68. { NAME_VAL(":scheme", "https") },
  69. { NAME_VAL(":status", "200") },
  70. { NAME_VAL(":status", "204") },
  71. { NAME_VAL(":status", "206") },
  72. { NAME_VAL(":status", "304") },
  73. { NAME_VAL(":status", "400") },
  74. { NAME_VAL(":status", "404") },
  75. { NAME_VAL(":status", "500") },
  76. { NAME_VAL("accept-charset", "") },
  77. { NAME_VAL("accept-encoding", "gzip, deflate") },
  78. { NAME_VAL("accept-language", "") },
  79. { NAME_VAL("accept-ranges", "") },
  80. { NAME_VAL("accept", "") },
  81. { NAME_VAL("access-control-allow-origin", "") },
  82. { NAME_VAL("age", "") },
  83. { NAME_VAL("allow", "") },
  84. { NAME_VAL("authorization", "") },
  85. { NAME_VAL("cache-control", "") },
  86. { NAME_VAL("content-disposition", "") },
  87. { NAME_VAL("content-encoding", "") },
  88. { NAME_VAL("content-language", "") },
  89. { NAME_VAL("content-length", "") },
  90. { NAME_VAL("content-location", "") },
  91. { NAME_VAL("content-range", "") },
  92. { NAME_VAL("content-type", "") },
  93. { NAME_VAL("cookie", "") },
  94. { NAME_VAL("date", "") },
  95. { NAME_VAL("etag", "") },
  96. { NAME_VAL("expect", "") },
  97. { NAME_VAL("expires", "") },
  98. { NAME_VAL("from", "") },
  99. { NAME_VAL("host", "") },
  100. { NAME_VAL("if-match", "") },
  101. { NAME_VAL("if-modified-since", "") },
  102. { NAME_VAL("if-none-match", "") },
  103. { NAME_VAL("if-range", "") },
  104. { NAME_VAL("if-unmodified-since", "") },
  105. { NAME_VAL("last-modified", "") },
  106. { NAME_VAL("link", "") },
  107. { NAME_VAL("location", "") },
  108. { NAME_VAL("max-forwards", "") },
  109. { NAME_VAL("proxy-authenticate", "") },
  110. { NAME_VAL("proxy-authorization", "") },
  111. { NAME_VAL("range", "") },
  112. { NAME_VAL("referer", "") },
  113. { NAME_VAL("refresh", "") },
  114. { NAME_VAL("retry-after", "") },
  115. { NAME_VAL("server", "") },
  116. { NAME_VAL("set-cookie", "") },
  117. { NAME_VAL("strict-transport-security", "") },
  118. { NAME_VAL("transfer-encoding", "") },
  119. { NAME_VAL("user-agent", "") },
  120. { NAME_VAL("vary", "") },
  121. { NAME_VAL("via", "") },
  122. { NAME_VAL("www-authenticate", "") }
  123. };
  124. static const uint32_t static_table_name_hash[HPACK_STATIC_TABLE_SIZE] =
  125. {
  126. 0x653A915Bu, 0xC7742BE4u, 0xC7742BE4u, 0x3513518Du, 0x3513518Du,
  127. 0xF49F1451u, 0xF49F1451u, 0x672BDA53u, 0x672BDA53u, 0x672BDA53u,
  128. 0x672BDA53u, 0x672BDA53u, 0x672BDA53u, 0x672BDA53u, 0xCD2C0296u,
  129. 0xF93AD8A9u, 0x98BD32D3u, 0x1DC691C8u, 0x1AB214F8u, 0x7D3B7A3Bu,
  130. 0xBEC8E440u, 0xE9C1D9E1u, 0x19D88141u, 0xC25511F2u, 0x16020A90u,
  131. 0x48011191u, 0x7D9AAB7Eu, 0x48F5CC19u, 0x8847A08Cu, 0x0D19F766u,
  132. 0x085EF7C5u, 0x0B486ED8u, 0x1A7AA369u, 0x6DE855BAu, 0xA6006EFDu,
  133. 0xA1BB4284u, 0xAE56E25Fu, 0xB6787110u, 0x791C6A0Du, 0xF2BADABEu,
  134. 0xD8CA2594u, 0xFBA64C54u, 0x4BEB0951u, 0x6B86C0B5u, 0xC62FECD2u,
  135. 0x8DA64A26u, 0x6CA35045u, 0xF614D165u, 0xE4D1DF14u, 0xB396750Au,
  136. 0x01F10233u, 0x798BEE18u, 0x5239F142u, 0x82E1B4E1u, 0x8F7E493Eu,
  137. 0x85E74C58u, 0xBD17F160u, 0x34C0456Au, 0x1A04DF3Du, 0xB1B15AB2u,
  138. 0xDDDAB6FFu,
  139. };
  140. static const uint32_t static_table_nameval_hash[HPACK_STATIC_TABLE_SIZE] =
  141. {
  142. 0xF8614896u, 0x25D95A15u, 0x33968BB7u, 0xC8C267F6u, 0x8351136Fu,
  143. 0x98573F68u, 0x16DDE443u, 0x352A6556u, 0xD4F462D2u, 0x125E66E0u,
  144. 0xD7988BC9u, 0x4C3C90DEu, 0x65E6ECA1u, 0xB05B7B87u, 0x96816317u,
  145. 0x8BBF5398u, 0x97E01849u, 0xD7B48DD4u, 0x9C180569u, 0xC7C63B45u,
  146. 0xF4223EE5u, 0x12C8A744u, 0xAA95A0BCu, 0x14F65730u, 0x8410A906u,
  147. 0x98F440DDu, 0x627E4803u, 0x5A5CC325u, 0x137FC223u, 0x1529262Fu,
  148. 0x7950B9BDu, 0x51D448A4u, 0x52C167CFu, 0xFB22AA54u, 0x540DB9FEu,
  149. 0x75A6C685u, 0xE1C54196u, 0xDC0C3733u, 0x6D78CB84u, 0x4F5272CDu,
  150. 0x9D4170E4u, 0xD4E28BA1u, 0x028C7846u, 0x4E8C1DC3u, 0x684BDDBCu,
  151. 0xE113A2B0u, 0x55F7BBD1u, 0x15BD3710u, 0xE82B715Du, 0x3674BC1Fu,
  152. 0x5010D24Bu, 0x953DE1CBu, 0x9F2C92D9u, 0xB2DE5570u, 0xBCA5998Fu,
  153. 0x0FF5B88Eu, 0x1FED156Bu, 0xDC83E7ECu, 0x07B79E35u, 0xA6D145A9u,
  154. 0x43638CBAu,
  155. };
  156. #define lshpack_arr_init(a) do { \
  157. memset((a), 0, sizeof(*(a))); \
  158. } while (0)
  159. #define lshpack_arr_cleanup(a) do { \
  160. free((a)->els); \
  161. memset((a), 0, sizeof(*(a))); \
  162. } while (0)
  163. #define lshpack_arr_get(a, i) ( \
  164. assert((i) < (a)->nelem), \
  165. (a)->els[(a)->off + (i)] \
  166. )
  167. #define lshpack_arr_shift(a) ( \
  168. assert((a)->nelem > 0), \
  169. (a)->nelem -= 1, \
  170. (a)->els[(a)->off++] \
  171. )
  172. #define lshpack_arr_pop(a) ( \
  173. assert((a)->nelem > 0), \
  174. (a)->nelem -= 1, \
  175. (a)->els[(a)->off + (a)->nelem] \
  176. )
  177. #define lshpack_arr_count(a) (+(a)->nelem)
  178. static int
  179. lshpack_arr_push (struct lshpack_arr *arr, uintptr_t val)
  180. {
  181. uintptr_t *new_els;
  182. unsigned n;
  183. if (arr->off + arr->nelem < arr->nalloc)
  184. {
  185. arr->els[arr->off + arr->nelem] = val;
  186. ++arr->nelem;
  187. return 0;
  188. }
  189. if (arr->off > arr->nalloc / 2)
  190. {
  191. memmove(arr->els, arr->els + arr->off,
  192. sizeof(arr->els[0]) * arr->nelem);
  193. arr->off = 0;
  194. arr->els[arr->nelem] = val;
  195. ++arr->nelem;
  196. return 0;
  197. }
  198. if (arr->nalloc)
  199. n = arr->nalloc * 2;
  200. else
  201. n = 64;
  202. new_els = malloc(n * sizeof(arr->els[0]));
  203. if (!new_els)
  204. return -1;
  205. memcpy(new_els, arr->els + arr->off, sizeof(arr->els[0]) * arr->nelem);
  206. free(arr->els);
  207. arr->off = 0;
  208. arr->els = new_els;
  209. arr->nalloc = n;
  210. arr->els[arr->off + arr->nelem] = val;
  211. ++arr->nelem;
  212. return 0;
  213. }
  214. struct lshpack_double_enc_head
  215. {
  216. struct lshpack_enc_head by_name;
  217. struct lshpack_enc_head by_nameval;
  218. };
  219. struct lshpack_enc_table_entry
  220. {
  221. /* An entry always lives on the `all' and `nameval' lists. If its name
  222. * is not in the static table, it also lives on the `name' list.
  223. */
  224. STAILQ_ENTRY(lshpack_enc_table_entry)
  225. ete_next_nameval,
  226. ete_next_name,
  227. ete_next_all;
  228. unsigned ete_id;
  229. unsigned ete_nameval_hash;
  230. unsigned ete_name_hash;
  231. unsigned ete_name_len;
  232. unsigned ete_val_len;
  233. char ete_buf[];
  234. };
  235. #define ETE_NAME(ete) ((ete)->ete_buf)
  236. #define ETE_VALUE(ete) (&(ete)->ete_buf[(ete)->ete_name_len])
  237. #define N_BUCKETS(n_bits) (1U << (n_bits))
  238. #define BUCKNO(n_bits, hash) ((hash) & (N_BUCKETS(n_bits) - 1))
  239. /* We estimate average number of entries in the dynamic table to be 1/3
  240. * of the theoretical maximum. This number is used to size the history
  241. * buffer: we want it large enough to cover recent entries, yet not too
  242. * large to cover entries that appear with a period larger than the
  243. * dynamic table.
  244. */
  245. static unsigned
  246. henc_hist_size (unsigned max_capacity)
  247. {
  248. return max_capacity / DYNAMIC_ENTRY_OVERHEAD / 3;
  249. }
  250. int
  251. lshpack_enc_init (struct lshpack_enc *enc)
  252. {
  253. struct lshpack_double_enc_head *buckets;
  254. unsigned nbits = 2;
  255. unsigned i;
  256. buckets = malloc(sizeof(buckets[0]) * N_BUCKETS(nbits));
  257. if (!buckets)
  258. return -1;
  259. for (i = 0; i < N_BUCKETS(nbits); ++i)
  260. {
  261. STAILQ_INIT(&buckets[i].by_name);
  262. STAILQ_INIT(&buckets[i].by_nameval);
  263. }
  264. memset(enc, 0, sizeof(*enc));
  265. STAILQ_INIT(&enc->hpe_all_entries);
  266. enc->hpe_max_capacity = INITIAL_DYNAMIC_TABLE_SIZE;
  267. enc->hpe_buckets = buckets;
  268. /* The initial value of the entry ID is completely arbitrary. As long as
  269. * there are fewer than 2^32 dynamic table entries, the math to calculate
  270. * the entry ID works. To prove to ourselves that the wraparound works
  271. * and to have the unit tests cover it, we initialize the next ID so that
  272. * it is just about to wrap around.
  273. */
  274. enc->hpe_next_id = ~0 - 3;
  275. enc->hpe_nbits = nbits;
  276. enc->hpe_nelem = 0;
  277. return 0;
  278. }
  279. void
  280. lshpack_enc_cleanup (struct lshpack_enc *enc)
  281. {
  282. struct lshpack_enc_table_entry *entry, *next;
  283. for (entry = STAILQ_FIRST(&enc->hpe_all_entries); entry; entry = next)
  284. {
  285. next = STAILQ_NEXT(entry, ete_next_all);
  286. free(entry);
  287. }
  288. free(enc->hpe_hist_buf);
  289. free(enc->hpe_buckets);
  290. }
  291. static int
  292. henc_use_hist (struct lshpack_enc *enc)
  293. {
  294. unsigned hist_size;
  295. if (enc->hpe_hist_buf)
  296. return 0;
  297. hist_size = henc_hist_size(INITIAL_DYNAMIC_TABLE_SIZE);
  298. if (!hist_size)
  299. return 0;
  300. enc->hpe_hist_buf = malloc(sizeof(enc->hpe_hist_buf[0]) * (hist_size + 1));
  301. if (!enc->hpe_hist_buf)
  302. return -1;
  303. enc->hpe_hist_size = hist_size;
  304. enc->hpe_flags |= LSHPACK_ENC_USE_HIST;
  305. return 0;
  306. }
  307. int
  308. lshpack_enc_use_hist (struct lshpack_enc *enc, int on)
  309. {
  310. if (on)
  311. return henc_use_hist(enc);
  312. else
  313. {
  314. enc->hpe_flags &= ~LSHPACK_ENC_USE_HIST;
  315. free(enc->hpe_hist_buf);
  316. enc->hpe_hist_buf = NULL;
  317. enc->hpe_hist_size = 0;
  318. enc->hpe_hist_idx = 0;
  319. enc->hpe_hist_wrapped = 0;
  320. return 0;
  321. }
  322. }
  323. int
  324. lshpack_enc_hist_used (const struct lshpack_enc *enc)
  325. {
  326. return (enc->hpe_flags & LSHPACK_ENC_USE_HIST) != 0;
  327. }
  328. #define LSHPACK_XXH_SEED 39378473
  329. #define XXH_NAMEVAL_WIDTH 9
  330. #define XXH_NAMEVAL_SHIFT 0
  331. #define XXH_NAME_WIDTH 9
  332. #define XXH_NAME_SHIFT 0
  333. static const unsigned char nameval2id[ 1 << XXH_NAMEVAL_WIDTH ] =
  334. {
  335. [150] = 1, [21] = 2, [439] = 3, [502] = 4, [367] = 5,
  336. [360] = 6, [67] = 7, [342] = 8, [210] = 9, [224] = 10,
  337. [457] = 11, [222] = 12, [161] = 13, [391] = 14, [279] = 15,
  338. [408] = 16, [73] = 17, [468] = 18, [361] = 19, [325] = 20,
  339. [229] = 21, [324] = 22, [188] = 23, [304] = 24, [262] = 25,
  340. [221] = 26, [3] = 27, [293] = 28, [35] = 29, [47] = 30,
  341. [445] = 31, [164] = 32, [463] = 33, [84] = 34, [510] = 35,
  342. [133] = 36, [406] = 37, [307] = 38, [388] = 39, [205] = 40,
  343. [228] = 41, [417] = 42, [70] = 43, [451] = 44, [444] = 45,
  344. [176] = 46, [465] = 47, [272] = 48, [349] = 49, [31] = 50,
  345. [75] = 51, [459] = 52, [217] = 53, [368] = 54, [399] = 55,
  346. [142] = 56, [363] = 57, [492] = 58, [53] = 59, [425] = 60,
  347. [186] = 61,
  348. };
  349. static const unsigned char name2id[ 1 << XXH_NAME_WIDTH ] =
  350. {
  351. [347] = 1, [484] = 2, [397] = 4, [81] = 6, [83] = 8,
  352. [150] = 15, [169] = 16, [211] = 17, [456] = 18, [248] = 19,
  353. [59] = 20, [64] = 21, [481] = 22, [321] = 23, [498] = 24,
  354. [144] = 25, [401] = 26, [382] = 27, [25] = 28, [140] = 29,
  355. [358] = 30, [453] = 31, [216] = 32, [361] = 33, [442] = 34,
  356. [253] = 35, [132] = 36, [95] = 37, [272] = 38, [13] = 39,
  357. [190] = 40, [404] = 41, [84] = 42, [337] = 43, [181] = 44,
  358. [210] = 45, [38] = 46, [69] = 47, [357] = 48, [276] = 49,
  359. [266] = 50, [51] = 51, [24] = 52, [322] = 53, [225] = 54,
  360. [318] = 55, [88] = 56, [352] = 57, [362] = 58, [317] = 59,
  361. [178] = 60, [255] = 61,
  362. };
  363. //not find return 0, otherwise return the index
  364. #if !LS_HPACK_EMIT_TEST_CODE
  365. static
  366. #endif
  367. unsigned
  368. lshpack_enc_get_static_nameval (const struct lsxpack_header *input)
  369. {
  370. unsigned i;
  371. assert(input->name_len > 0);
  372. assert(input->flags & LSXPACK_NAMEVAL_HASH);
  373. i = (input->nameval_hash >> XXH_NAMEVAL_SHIFT) & ((1 << XXH_NAMEVAL_WIDTH) - 1);
  374. if (nameval2id[i])
  375. {
  376. i = nameval2id[i] - 1;
  377. if (static_table[i].name_len == input->name_len
  378. && static_table[i].val_len == input->val_len
  379. && memcmp(lsxpack_header_get_name(input), static_table[i].name, input->name_len) == 0
  380. && memcmp(lsxpack_header_get_value(input), static_table[i].val, input->val_len) == 0)
  381. {
  382. return i + 1;
  383. }
  384. }
  385. return 0;
  386. }
  387. #if !LS_HPACK_EMIT_TEST_CODE
  388. static
  389. #endif
  390. unsigned
  391. lshpack_enc_get_static_name (const struct lsxpack_header *input)
  392. {
  393. unsigned i;
  394. assert(input->flags & LSXPACK_NAME_HASH);
  395. i = (input->name_hash >> XXH_NAME_SHIFT) & ((1 << XXH_NAME_WIDTH) - 1);
  396. if (name2id[i])
  397. {
  398. i = name2id[i] - 1;
  399. if (static_table[i].name_len == input->name_len
  400. && memcmp(lsxpack_header_get_name(input), static_table[i].name,
  401. input->name_len) == 0)
  402. {
  403. return i + 1;
  404. }
  405. }
  406. return 0;
  407. }
  408. static void
  409. update_hash (struct lsxpack_header *input)
  410. {
  411. if (!(input->flags & LSXPACK_NAME_HASH))
  412. input->name_hash = XXH32(lsxpack_header_get_name(input),
  413. input->name_len, LSHPACK_XXH_SEED);
  414. else
  415. assert(input->name_hash == XXH32(lsxpack_header_get_name(input),
  416. input->name_len, LSHPACK_XXH_SEED));
  417. if (!(input->flags & LSXPACK_NAMEVAL_HASH))
  418. input->nameval_hash = XXH32(input->buf + input->val_offset,
  419. input->val_len, input->name_hash);
  420. else
  421. assert(input->nameval_hash == XXH32(input->buf + input->val_offset,
  422. input->val_len, input->name_hash));
  423. input->flags |= (LSXPACK_NAME_HASH | LSXPACK_NAMEVAL_HASH);
  424. }
  425. unsigned
  426. lshpack_enc_get_stx_tab_id (struct lsxpack_header *input)
  427. {
  428. unsigned i;
  429. update_hash(input);
  430. i = (input->nameval_hash >> XXH_NAMEVAL_SHIFT) & ((1 << XXH_NAMEVAL_WIDTH) - 1);
  431. if (nameval2id[i])
  432. {
  433. i = nameval2id[i] - 1;
  434. if (static_table[i].name_len == input->name_len
  435. && static_table[i].val_len == input->val_len
  436. && memcmp(lsxpack_header_get_name(input), static_table[i].name,
  437. input->name_len) == 0
  438. && memcmp(input->buf + input->val_offset, static_table[i].val,
  439. input->val_len) == 0)
  440. {
  441. return i + 1;
  442. }
  443. }
  444. i = (input->name_hash >> XXH_NAME_SHIFT) & ((1 << XXH_NAME_WIDTH) - 1);
  445. if (name2id[i])
  446. {
  447. i = name2id[i] - 1;
  448. if (static_table[i].name_len == input->name_len
  449. && memcmp(lsxpack_header_get_name(input), static_table[i].name,
  450. input->name_len) == 0)
  451. {
  452. return i + 1;
  453. }
  454. }
  455. return 0;
  456. }
  457. /* Given a dynamic entry, return its table ID */
  458. static unsigned
  459. henc_calc_table_id (const struct lshpack_enc *enc,
  460. const struct lshpack_enc_table_entry *entry)
  461. {
  462. return HPACK_STATIC_TABLE_SIZE
  463. + (enc->hpe_next_id - entry->ete_id)
  464. ;
  465. }
  466. static unsigned
  467. henc_find_table_id (struct lshpack_enc *enc, lsxpack_header_t *input,
  468. int *val_matched)
  469. {
  470. struct lshpack_enc_table_entry *entry;
  471. unsigned buckno, id;
  472. const char *val_ptr = input->buf + input->val_offset;
  473. const char *name;
  474. unsigned int name_len;
  475. name_len = input->name_len;
  476. name = lsxpack_header_get_name(input);
  477. /* First, look for a match in the static table: */
  478. if (input->hpack_index)
  479. {
  480. id = input->hpack_index - 1;
  481. #ifndef NDEBUG
  482. if (name_len)
  483. {
  484. lsxpack_header_t input_copy = *input;
  485. const unsigned hpack_index = lshpack_enc_get_stx_tab_id(&input_copy);
  486. assert(input_copy.hpack_index == hpack_index);
  487. }
  488. #endif
  489. if (id <= LSHPACK_HDR_ACCEPT_ENCODING || input->val_len == 0)
  490. {
  491. if (static_table[id].val_len == input->val_len
  492. && memcmp(val_ptr, static_table[id].val, input->val_len) == 0)
  493. {
  494. input->flags |= LSXPACK_HPACK_VAL_MATCHED;
  495. *val_matched = 1;
  496. return input->hpack_index;
  497. }
  498. }
  499. if (!name_len)
  500. {
  501. name = static_table[id].name;
  502. name_len = static_table[id].name_len;
  503. }
  504. if (!(input->flags & LSXPACK_NAME_HASH))
  505. input->name_hash = static_table_name_hash[id];
  506. else
  507. assert(input->name_hash == static_table_name_hash[id]);
  508. if (!(input->flags & LSXPACK_NAMEVAL_HASH))
  509. input->nameval_hash = XXH32(val_ptr, input->val_len,
  510. input->name_hash);
  511. else
  512. assert(input->nameval_hash == XXH32(val_ptr, input->val_len,
  513. input->name_hash));
  514. input->flags |= (LSXPACK_NAME_HASH | LSXPACK_NAMEVAL_HASH);
  515. }
  516. else
  517. {
  518. update_hash(input);
  519. input->hpack_index = lshpack_enc_get_static_nameval(input);
  520. if (input->hpack_index != LSHPACK_HDR_UNKNOWN)
  521. {
  522. input->flags |= LSXPACK_HPACK_VAL_MATCHED;
  523. *val_matched = 1;
  524. return input->hpack_index;
  525. }
  526. }
  527. /* Search by name and value: */
  528. buckno = BUCKNO(enc->hpe_nbits, input->nameval_hash);
  529. STAILQ_FOREACH(entry, &enc->hpe_buckets[buckno].by_nameval,
  530. ete_next_nameval)
  531. if (input->nameval_hash == entry->ete_nameval_hash &&
  532. name_len == entry->ete_name_len &&
  533. input->val_len == entry->ete_val_len &&
  534. 0 == memcmp(name, ETE_NAME(entry), name_len) &&
  535. 0 == memcmp(val_ptr, ETE_VALUE(entry), input->val_len))
  536. {
  537. *val_matched = 1;
  538. return henc_calc_table_id(enc, entry);
  539. }
  540. /* Name/value match is not found, look for header: */
  541. if (input->hpack_index == LSHPACK_HDR_UNKNOWN)
  542. input->hpack_index = lshpack_enc_get_static_name(input);
  543. if (input->hpack_index != LSHPACK_HDR_UNKNOWN)
  544. {
  545. input->flags &= ~LSXPACK_HPACK_VAL_MATCHED;
  546. return input->hpack_index;
  547. }
  548. /* Search by name only: */
  549. buckno = BUCKNO(enc->hpe_nbits, input->name_hash);
  550. STAILQ_FOREACH(entry, &enc->hpe_buckets[buckno].by_name, ete_next_name)
  551. if (input->name_hash == entry->ete_name_hash &&
  552. input->name_len == entry->ete_name_len &&
  553. 0 == memcmp(name, ETE_NAME(entry), name_len))
  554. {
  555. input->flags &= ~LSXPACK_HPACK_VAL_MATCHED;
  556. return henc_calc_table_id(enc, entry);
  557. }
  558. return 0;
  559. }
  560. #if !LS_HPACK_EMIT_TEST_CODE
  561. static
  562. #endif
  563. unsigned char *
  564. lshpack_enc_enc_int (unsigned char *dst, unsigned char *const end,
  565. uint32_t value, uint8_t prefix_bits)
  566. {
  567. unsigned char *const dst_orig = dst;
  568. /* This function assumes that at least one byte is available */
  569. assert(dst < end);
  570. if (value < (uint32_t)(1 << prefix_bits) - 1)
  571. *dst++ |= value;
  572. else
  573. {
  574. *dst++ |= (1 << prefix_bits) - 1;
  575. value -= (1 << prefix_bits) - 1;
  576. while (value >= 128)
  577. {
  578. if (dst < end)
  579. {
  580. *dst++ = (0x80 | value);
  581. value >>= 7;
  582. }
  583. else
  584. return dst_orig;
  585. }
  586. if (dst < end)
  587. *dst++ = value;
  588. else
  589. return dst_orig;
  590. }
  591. return dst;
  592. }
  593. /* This whole pragma business has to do with turning off uninitialized warnings.
  594. * We do it for gcc and clang. Other compilers get slightly slower code, where
  595. * unnecessary initialization is performed.
  596. */
  597. #if __GNUC__
  598. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  599. #if __clang__
  600. #pragma GCC diagnostic ignored "-Wunknown-warning-option"
  601. #endif
  602. #endif
  603. static int
  604. lshpack_enc_huff_encode (const unsigned char *src,
  605. const unsigned char *const src_end, unsigned char *const dst, int dst_len)
  606. {
  607. unsigned char *p_dst = dst;
  608. unsigned char *dst_end = p_dst + dst_len;
  609. uintptr_t bits; /* OK not to initialize this variable */
  610. unsigned bits_used = 0, adj;
  611. struct encode_el cur_enc_code;
  612. #if __GNUC__ && !defined(__COVERITY__)
  613. #pragma GCC diagnostic push
  614. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  615. #pragma GCC diagnostic ignored "-Wuninitialized"
  616. #else
  617. bits = 0;
  618. #endif
  619. #if LS_HPACK_USE_LARGE_TABLES
  620. const struct henc *henc;
  621. uint16_t idx;
  622. while (src + sizeof(bits) * 8 / 5 + sizeof(idx) < src_end
  623. && p_dst + sizeof(bits) <= dst_end)
  624. {
  625. memcpy(&idx, src, 2);
  626. henc = &hencs[idx];
  627. src += 2;
  628. while (bits_used + henc->lens < sizeof(bits) * 8)
  629. {
  630. bits <<= henc->lens;
  631. bits |= henc->code;
  632. bits_used += henc->lens;
  633. memcpy(&idx, src, 2);
  634. henc = &hencs[idx];
  635. src += 2;
  636. }
  637. if (henc->lens < 64)
  638. {
  639. bits <<= sizeof(bits) * 8 - bits_used;
  640. bits_used = henc->lens - (sizeof(bits) * 8 - bits_used);
  641. bits |= henc->code >> bits_used;
  642. #if UINTPTR_MAX == 18446744073709551615ull
  643. *p_dst++ = bits >> 56;
  644. *p_dst++ = bits >> 48;
  645. *p_dst++ = bits >> 40;
  646. *p_dst++ = bits >> 32;
  647. #endif
  648. *p_dst++ = bits >> 24;
  649. *p_dst++ = bits >> 16;
  650. *p_dst++ = bits >> 8;
  651. *p_dst++ = bits;
  652. bits = henc->code; /* OK not to clear high bits */
  653. }
  654. else
  655. {
  656. src -= 2;
  657. break;
  658. }
  659. }
  660. #endif
  661. while (src != src_end)
  662. {
  663. cur_enc_code = encode_table[*src++];
  664. if (bits_used + cur_enc_code.bits < sizeof(bits) * 8)
  665. {
  666. bits <<= cur_enc_code.bits;
  667. bits |= cur_enc_code.code;
  668. bits_used += cur_enc_code.bits;
  669. continue;
  670. }
  671. else if (p_dst + sizeof(bits) <= dst_end)
  672. {
  673. #ifdef __COVERITY__
  674. assert(bits_used);
  675. #endif
  676. bits <<= sizeof(bits) * 8 - bits_used;
  677. bits_used = cur_enc_code.bits - (sizeof(bits) * 8 - bits_used);
  678. bits |= cur_enc_code.code >> bits_used;
  679. #if UINTPTR_MAX == 18446744073709551615ull
  680. *p_dst++ = bits >> 56;
  681. *p_dst++ = bits >> 48;
  682. *p_dst++ = bits >> 40;
  683. *p_dst++ = bits >> 32;
  684. #endif
  685. *p_dst++ = bits >> 24;
  686. *p_dst++ = bits >> 16;
  687. *p_dst++ = bits >> 8;
  688. *p_dst++ = bits;
  689. bits = cur_enc_code.code; /* OK not to clear high bits */
  690. }
  691. else
  692. return -1;
  693. }
  694. adj = bits_used + (-bits_used & 7); /* Round up to 8 */
  695. if (bits_used && p_dst + (adj >> 3) <= dst_end)
  696. {
  697. bits <<= -bits_used & 7; /* Align to byte boundary */
  698. bits |= ((1 << (-bits_used & 7)) - 1); /* EOF */
  699. switch (adj >> 3)
  700. { /* Write out */
  701. #if UINTPTR_MAX == 18446744073709551615ull
  702. case 8: *p_dst++ = bits >> 56;
  703. /* fall through */
  704. case 7: *p_dst++ = bits >> 48;
  705. /* fall through */
  706. case 6: *p_dst++ = bits >> 40;
  707. /* fall through */
  708. case 5: *p_dst++ = bits >> 32;
  709. #endif
  710. /* fall through */
  711. case 4: *p_dst++ = bits >> 24;
  712. /* fall through */
  713. case 3: *p_dst++ = bits >> 16;
  714. /* fall through */
  715. case 2: *p_dst++ = bits >> 8;
  716. /* fall through */
  717. default: *p_dst++ = bits;
  718. }
  719. return p_dst - dst;
  720. }
  721. else if (p_dst + (adj >> 3) <= dst_end)
  722. return p_dst - dst;
  723. else
  724. return -1;
  725. #if __GNUC__
  726. #pragma GCC diagnostic pop
  727. #endif
  728. }
  729. #if !LS_HPACK_EMIT_TEST_CODE
  730. static
  731. #endif
  732. int
  733. lshpack_enc_enc_str (unsigned char *const dst, size_t dst_len,
  734. const unsigned char *str, unsigned str_len)
  735. {
  736. unsigned char size_buf[4];
  737. unsigned char *p;
  738. unsigned size_len;
  739. int rc;
  740. if (dst_len > 1)
  741. /* We guess that the string size fits into a single byte -- meaning
  742. * compressed string of size 126 and smaller -- which is the normal
  743. * case. Thus, we immediately write compressed string to the output
  744. * buffer. If our guess is not correct, we fix it later.
  745. */
  746. rc = lshpack_enc_huff_encode(str, str + str_len, dst + 1, dst_len - 1);
  747. else if (dst_len == 1)
  748. /* Here, the call can only succeed if the string to encode is empty. */
  749. rc = 0;
  750. else
  751. return -1;
  752. /*
  753. * Check if need huffman encoding or not
  754. * Comment: (size_t)rc <= str_len = means if same length, still use
  755. * Huffman
  756. * ^
  757. */
  758. if (rc > 0 && (size_t)rc <= str_len)
  759. {
  760. if (rc < 127)
  761. {
  762. *dst = 0x80 | rc;
  763. return 1 + rc;
  764. }
  765. size_buf[0] = 0x80;
  766. str_len = rc;
  767. str = dst + 1;
  768. }
  769. else if (str_len <= dst_len - 1)
  770. {
  771. if (str_len < 127)
  772. {
  773. *dst = (unsigned char) str_len;
  774. memcpy(dst + 1, str, str_len);
  775. return 1 + str_len;
  776. }
  777. size_buf[0] = 0x00;
  778. }
  779. else
  780. return -1;
  781. /* The guess of one-byte size was incorrect. Perform necessary
  782. * adjustments.
  783. */
  784. p = lshpack_enc_enc_int(size_buf, size_buf + sizeof(size_buf), str_len, 7);
  785. if (p == size_buf)
  786. return -1;
  787. size_len = p - size_buf;
  788. assert(size_len > 1);
  789. /* Check if there is enough room in the output buffer for both
  790. * encoded size and the string.
  791. */
  792. if (size_len + str_len > dst_len)
  793. return -1;
  794. memmove(dst + size_len, str, str_len);
  795. memcpy(dst, size_buf, size_len);
  796. return size_len + str_len;
  797. }
  798. static void
  799. henc_drop_oldest_entry (struct lshpack_enc *enc)
  800. {
  801. struct lshpack_enc_table_entry *entry;
  802. unsigned buckno;
  803. entry = STAILQ_FIRST(&enc->hpe_all_entries);
  804. assert(entry);
  805. STAILQ_REMOVE_HEAD(&enc->hpe_all_entries, ete_next_all);
  806. buckno = BUCKNO(enc->hpe_nbits, entry->ete_nameval_hash);
  807. assert(entry == STAILQ_FIRST(&enc->hpe_buckets[buckno].by_nameval));
  808. STAILQ_REMOVE_HEAD(&enc->hpe_buckets[buckno].by_nameval, ete_next_nameval);
  809. buckno = BUCKNO(enc->hpe_nbits, entry->ete_name_hash);
  810. if (entry == STAILQ_FIRST(&enc->hpe_buckets[buckno].by_name))
  811. STAILQ_REMOVE_HEAD(&enc->hpe_buckets[buckno].by_name, ete_next_name);
  812. enc->hpe_cur_capacity -= DYNAMIC_ENTRY_OVERHEAD + entry->ete_name_len
  813. + entry->ete_val_len;
  814. --enc->hpe_nelem;
  815. free(entry);
  816. }
  817. static void
  818. henc_remove_overflow_entries (struct lshpack_enc *enc)
  819. {
  820. while (enc->hpe_cur_capacity > enc->hpe_max_capacity)
  821. henc_drop_oldest_entry(enc);
  822. }
  823. static int
  824. henc_grow_tables (struct lshpack_enc *enc)
  825. {
  826. struct lshpack_double_enc_head *new_buckets, *new[2];
  827. struct lshpack_enc_table_entry *entry;
  828. unsigned n, old_nbits;
  829. int idx;
  830. old_nbits = enc->hpe_nbits;
  831. new_buckets = malloc(sizeof(enc->hpe_buckets[0])
  832. * N_BUCKETS(old_nbits + 1));
  833. if (!new_buckets)
  834. return -1;
  835. for (n = 0; n < N_BUCKETS(old_nbits); ++n)
  836. {
  837. new[0] = &new_buckets[n];
  838. new[1] = &new_buckets[n + N_BUCKETS(old_nbits)];
  839. STAILQ_INIT(&new[0]->by_name);
  840. STAILQ_INIT(&new[1]->by_name);
  841. STAILQ_INIT(&new[0]->by_nameval);
  842. STAILQ_INIT(&new[1]->by_nameval);
  843. while ((entry = STAILQ_FIRST(&enc->hpe_buckets[n].by_name)))
  844. {
  845. STAILQ_REMOVE_HEAD(&enc->hpe_buckets[n].by_name, ete_next_name);
  846. idx = (BUCKNO(old_nbits + 1, entry->ete_name_hash)
  847. >> old_nbits) & 1;
  848. STAILQ_INSERT_TAIL(&new[idx]->by_name, entry, ete_next_name);
  849. }
  850. while ((entry = STAILQ_FIRST(&enc->hpe_buckets[n].by_nameval)))
  851. {
  852. STAILQ_REMOVE_HEAD(&enc->hpe_buckets[n].by_nameval,
  853. ete_next_nameval);
  854. idx = (BUCKNO(old_nbits + 1, entry->ete_nameval_hash)
  855. >> old_nbits) & 1;
  856. STAILQ_INSERT_TAIL(&new[idx]->by_nameval, entry,
  857. ete_next_nameval);
  858. }
  859. }
  860. free(enc->hpe_buckets);
  861. enc->hpe_nbits = old_nbits + 1;
  862. enc->hpe_buckets = new_buckets;
  863. return 0;
  864. }
  865. #if !LS_HPACK_EMIT_TEST_CODE
  866. static
  867. #endif
  868. int
  869. lshpack_enc_push_entry (struct lshpack_enc *enc,
  870. const struct lsxpack_header *input)
  871. {
  872. unsigned buckno;
  873. struct lshpack_enc_table_entry *entry;
  874. size_t size;
  875. const char *name;
  876. unsigned int name_len;
  877. if (enc->hpe_nelem >= N_BUCKETS(enc->hpe_nbits) / 2 &&
  878. 0 != henc_grow_tables(enc))
  879. return -1;
  880. name_len = input->name_len;
  881. if (name_len == 0)
  882. {
  883. assert(input->hpack_index != LSHPACK_HDR_UNKNOWN);
  884. name = static_table[input->hpack_index - 1].name;
  885. name_len = static_table[input->hpack_index - 1].name_len;
  886. }
  887. else
  888. name = lsxpack_header_get_name(input);
  889. size = sizeof(*entry) + name_len + input->val_len;
  890. entry = malloc(size);
  891. if (!entry)
  892. return -1;
  893. entry->ete_name_hash = input->name_hash;
  894. entry->ete_nameval_hash = input->nameval_hash;
  895. entry->ete_name_len = name_len;
  896. entry->ete_val_len = input->val_len;
  897. entry->ete_id = enc->hpe_next_id++;
  898. memcpy(ETE_NAME(entry), name, name_len);
  899. memcpy(ETE_VALUE(entry), input->buf + input->val_offset, input->val_len);
  900. STAILQ_INSERT_TAIL(&enc->hpe_all_entries, entry, ete_next_all);
  901. buckno = BUCKNO(enc->hpe_nbits, input->nameval_hash);
  902. STAILQ_INSERT_TAIL(&enc->hpe_buckets[buckno].by_nameval, entry,
  903. ete_next_nameval);
  904. if (input->hpack_index == LSHPACK_HDR_UNKNOWN)
  905. {
  906. buckno = BUCKNO(enc->hpe_nbits, input->name_hash);
  907. STAILQ_INSERT_TAIL(&enc->hpe_buckets[buckno].by_name, entry,
  908. ete_next_name);
  909. }
  910. enc->hpe_cur_capacity += DYNAMIC_ENTRY_OVERHEAD + name_len
  911. + input->val_len;
  912. ++enc->hpe_nelem;
  913. henc_remove_overflow_entries(enc);
  914. return 0;
  915. }
  916. static void
  917. henc_resize_history (struct lshpack_enc *enc)
  918. {
  919. uint32_t *hist_buf;
  920. unsigned hist_size, first, count, i, j;
  921. hist_size = henc_hist_size(enc->hpe_max_capacity);
  922. if (hist_size == enc->hpe_hist_size)
  923. return;
  924. if (hist_size == 0)
  925. {
  926. free(enc->hpe_hist_buf);
  927. enc->hpe_hist_buf = NULL;
  928. enc->hpe_hist_size = 0;
  929. enc->hpe_hist_idx = 0;
  930. enc->hpe_hist_wrapped = 0;
  931. return;
  932. }
  933. hist_buf = malloc(sizeof(hist_buf[0]) * (hist_size + 1));
  934. if (!hist_buf)
  935. return;
  936. if (enc->hpe_hist_wrapped)
  937. {
  938. first = (enc->hpe_hist_idx + 1) % enc->hpe_hist_size;
  939. count = enc->hpe_hist_size;
  940. }
  941. else
  942. {
  943. first = 0;
  944. count = enc->hpe_hist_idx;
  945. }
  946. for (i = 0, j = 0; count > 0 && j < hist_size; ++i, ++j, --count)
  947. hist_buf[j] = enc->hpe_hist_buf[ (first + i) % enc->hpe_hist_size ];
  948. enc->hpe_hist_size = hist_size;
  949. enc->hpe_hist_idx = j % hist_size;
  950. enc->hpe_hist_wrapped = enc->hpe_hist_idx == 0;
  951. free(enc->hpe_hist_buf);
  952. enc->hpe_hist_buf = hist_buf;
  953. }
  954. /* Returns true if `nameval_hash' was already in history, false otherwise. */
  955. static int
  956. henc_hist_add (struct lshpack_enc *enc, uint32_t nameval_hash)
  957. {
  958. unsigned last;
  959. uint32_t *p;
  960. if (enc->hpe_hist_wrapped)
  961. last = enc->hpe_hist_size;
  962. else
  963. last = enc->hpe_hist_idx;
  964. enc->hpe_hist_buf[ last ] = nameval_hash;
  965. for (p = enc->hpe_hist_buf; *p != nameval_hash; ++p)
  966. ;
  967. enc->hpe_hist_buf[ enc->hpe_hist_idx ] = nameval_hash;
  968. enc->hpe_hist_idx = (enc->hpe_hist_idx + 1) % enc->hpe_hist_size;
  969. enc->hpe_hist_wrapped |= enc->hpe_hist_idx == 0;
  970. return p < enc->hpe_hist_buf + last;
  971. }
  972. unsigned char *
  973. lshpack_enc_encode (struct lshpack_enc *enc, unsigned char *dst,
  974. unsigned char *dst_end, lsxpack_header_t *input)
  975. {
  976. //indexed_type: 0, Add, 1,: without, 2: never
  977. static const char indexed_prefix_number[] = {0x40, 0x00, 0x10};
  978. unsigned char *const dst_org = dst;
  979. int rc;
  980. int val_matched = 0;
  981. unsigned table_id;
  982. if (dst_end <= dst)
  983. return dst_org;
  984. if (input->flags & LSXPACK_HPACK_VAL_MATCHED)
  985. {
  986. assert(input->hpack_index != LSHPACK_HDR_UNKNOWN);
  987. assert(input->val_len == static_table[input->hpack_index - 1].val_len);
  988. assert(memcmp(lsxpack_header_get_value(input),
  989. static_table[input->hpack_index - 1].val,
  990. input->val_len) == 0);
  991. table_id = input->hpack_index;
  992. val_matched = 1;
  993. }
  994. else
  995. {
  996. if (input->flags & LSXPACK_NEVER_INDEX)
  997. input->indexed_type = 2;
  998. table_id = henc_find_table_id(enc, input, &val_matched);
  999. if (enc->hpe_hist_buf)
  1000. {
  1001. rc = henc_hist_add(enc, input->nameval_hash);
  1002. if (!rc && enc->hpe_hist_wrapped && input->indexed_type == 0)
  1003. input->indexed_type = 1;
  1004. }
  1005. }
  1006. if (table_id > 0)
  1007. {
  1008. if (val_matched)
  1009. {
  1010. // LSXPACK_VAL_MATCHED MUST NOT set for dynamic table
  1011. // otherwise, it may cause trouble when feed the input to a different encoder.
  1012. if (table_id > HPACK_STATIC_TABLE_SIZE)
  1013. assert(!(input->flags & LSXPACK_HPACK_VAL_MATCHED));
  1014. *dst = 0x80;
  1015. dst = lshpack_enc_enc_int(dst, dst_end, table_id, 7);
  1016. /* No need to check return value: we pass it up as-is because
  1017. * the behavior is the same.
  1018. */
  1019. return dst;
  1020. }
  1021. else
  1022. {
  1023. *dst = indexed_prefix_number[input->indexed_type];
  1024. dst = lshpack_enc_enc_int(dst, dst_end, table_id,
  1025. ((input->indexed_type == 0) ? 6 : 4));
  1026. if (dst == dst_org)
  1027. return dst_org;
  1028. }
  1029. }
  1030. else
  1031. {
  1032. assert(input->name_len > 0);
  1033. *dst++ = indexed_prefix_number[input->indexed_type];
  1034. rc = lshpack_enc_enc_str(dst, dst_end - dst,
  1035. (unsigned char *)lsxpack_header_get_name(input),
  1036. input->name_len);
  1037. if (rc < 0)
  1038. return dst_org; //Failed to enc this header, return unchanged ptr.
  1039. dst += rc;
  1040. }
  1041. rc = lshpack_enc_enc_str(dst, dst_end - dst,
  1042. (const unsigned char *)input->buf + input->val_offset,
  1043. input->val_len);
  1044. if (rc < 0)
  1045. return dst_org; //Failed to enc this header, return unchanged ptr.
  1046. dst += rc;
  1047. if (input->indexed_type == 0)
  1048. {
  1049. rc = lshpack_enc_push_entry(enc, input);
  1050. if (rc != 0)
  1051. return dst_org; //Failed to enc this header, return unchanged ptr.
  1052. }
  1053. return dst;
  1054. }
  1055. void
  1056. lshpack_enc_set_max_capacity (struct lshpack_enc *enc, unsigned max_capacity)
  1057. {
  1058. enc->hpe_max_capacity = max_capacity;
  1059. henc_remove_overflow_entries(enc);
  1060. if (lshpack_enc_hist_used(enc))
  1061. henc_resize_history(enc);
  1062. }
  1063. #if LS_HPACK_EMIT_TEST_CODE
  1064. void
  1065. lshpack_enc_iter_init (struct lshpack_enc *enc, void **iter)
  1066. {
  1067. *iter = STAILQ_FIRST(&enc->hpe_all_entries);
  1068. }
  1069. /* Returns 0 if entry is found */
  1070. int
  1071. lshpack_enc_iter_next (struct lshpack_enc *enc, void **iter,
  1072. struct enc_dyn_table_entry *retval)
  1073. {
  1074. const struct lshpack_enc_table_entry *entry;
  1075. entry = *iter;
  1076. if (!entry)
  1077. return -1;
  1078. *iter = STAILQ_NEXT(entry, ete_next_all);
  1079. retval->name = ETE_NAME(entry);
  1080. retval->value = ETE_VALUE(entry);
  1081. retval->name_len = entry->ete_name_len;
  1082. retval->value_len = entry->ete_val_len;
  1083. retval->entry_id = henc_calc_table_id(enc, entry);
  1084. return 0;
  1085. }
  1086. #endif
  1087. /* Dynamic table entry: */
  1088. struct dec_table_entry
  1089. {
  1090. unsigned dte_name_len;
  1091. unsigned dte_val_len;
  1092. #if LSHPACK_DEC_CALC_HASH
  1093. uint32_t dte_name_hash;
  1094. uint32_t dte_nameval_hash;
  1095. enum {
  1096. DTEF_NAME_HASH = LSXPACK_NAME_HASH,
  1097. DTEF_NAMEVAL_HASH = LSXPACK_NAMEVAL_HASH,
  1098. } dte_flags:8;
  1099. #endif
  1100. uint8_t dte_name_idx;
  1101. char dte_buf[]; /* Contains both name and value */
  1102. };
  1103. #define DTE_NAME(dte) ((dte)->dte_buf)
  1104. #define DTE_VALUE(dte) (&(dte)->dte_buf[(dte)->dte_name_len])
  1105. enum
  1106. {
  1107. HPACK_HUFFMAN_FLAG_ACCEPTED = 0x01,
  1108. HPACK_HUFFMAN_FLAG_SYM = 0x02,
  1109. HPACK_HUFFMAN_FLAG_FAIL = 0x04,
  1110. };
  1111. struct decode_status
  1112. {
  1113. uint8_t state;
  1114. uint8_t eos;
  1115. };
  1116. void
  1117. lshpack_dec_init (struct lshpack_dec *dec)
  1118. {
  1119. memset(dec, 0, sizeof(*dec));
  1120. dec->hpd_max_capacity = INITIAL_DYNAMIC_TABLE_SIZE;
  1121. dec->hpd_cur_max_capacity = INITIAL_DYNAMIC_TABLE_SIZE;
  1122. lshpack_arr_init(&dec->hpd_dyn_table);
  1123. }
  1124. void
  1125. lshpack_dec_cleanup (struct lshpack_dec *dec)
  1126. {
  1127. uintptr_t val;
  1128. while (lshpack_arr_count(&dec->hpd_dyn_table) > 0)
  1129. {
  1130. val = lshpack_arr_pop(&dec->hpd_dyn_table);
  1131. free((struct dec_table_entry *) val);
  1132. }
  1133. lshpack_arr_cleanup(&dec->hpd_dyn_table);
  1134. }
  1135. /* Maximum number of bytes required to encode a 32-bit integer */
  1136. #define LSHPACK_UINT32_ENC_SZ 6
  1137. /* Assumption: we have at least one byte to work with */
  1138. #if !LS_HPACK_EMIT_TEST_CODE
  1139. static
  1140. #endif
  1141. int
  1142. lshpack_dec_dec_int (const unsigned char **src_p, const unsigned char *src_end,
  1143. unsigned prefix_bits, uint32_t *value_p)
  1144. {
  1145. const unsigned char *const orig_src = *src_p;
  1146. const unsigned char *src;
  1147. unsigned prefix_max, M;
  1148. uint32_t val, B;
  1149. src = *src_p;
  1150. prefix_max = (1 << prefix_bits) - 1;
  1151. val = *src++;
  1152. val &= prefix_max;
  1153. if (val < prefix_max)
  1154. {
  1155. *src_p = src;
  1156. *value_p = val;
  1157. return 0;
  1158. }
  1159. M = 0;
  1160. do
  1161. {
  1162. if (src < src_end)
  1163. {
  1164. B = *src++;
  1165. val = val + ((B & 0x7f) << M);
  1166. M += 7;
  1167. }
  1168. else if (src - orig_src < LSHPACK_UINT32_ENC_SZ)
  1169. return -1;
  1170. else
  1171. return -2;
  1172. }
  1173. while (B & 0x80);
  1174. if (M <= 28 || (M == 35 && src[-1] <= 0xF && val - (src[-1] << 28) < val))
  1175. {
  1176. *src_p = src;
  1177. *value_p = val;
  1178. return 0;
  1179. }
  1180. else
  1181. return -2;
  1182. }
  1183. static void
  1184. hdec_drop_oldest_entry (struct lshpack_dec *dec)
  1185. {
  1186. struct dec_table_entry *entry;
  1187. entry = (void *) lshpack_arr_shift(&dec->hpd_dyn_table);
  1188. dec->hpd_cur_capacity -= DYNAMIC_ENTRY_OVERHEAD + entry->dte_name_len
  1189. + entry->dte_val_len;
  1190. ++dec->hpd_state;
  1191. free(entry);
  1192. }
  1193. static void
  1194. hdec_remove_overflow_entries (struct lshpack_dec *dec)
  1195. {
  1196. while (dec->hpd_cur_capacity > dec->hpd_cur_max_capacity)
  1197. hdec_drop_oldest_entry(dec);
  1198. }
  1199. static void
  1200. hdec_update_max_capacity (struct lshpack_dec *dec, uint32_t new_capacity)
  1201. {
  1202. dec->hpd_cur_max_capacity = new_capacity;
  1203. hdec_remove_overflow_entries(dec);
  1204. }
  1205. void
  1206. lshpack_dec_set_max_capacity (struct lshpack_dec *dec, unsigned max_capacity)
  1207. {
  1208. dec->hpd_max_capacity = max_capacity;
  1209. hdec_update_max_capacity(dec, max_capacity);
  1210. }
  1211. static unsigned char *
  1212. hdec_huff_dec4bits (uint8_t src_4bits, unsigned char *dst,
  1213. struct decode_status *status)
  1214. {
  1215. const struct decode_el cur_dec_code =
  1216. decode_tables[status->state][src_4bits];
  1217. if (cur_dec_code.flags & HPACK_HUFFMAN_FLAG_FAIL) {
  1218. return NULL; //failed
  1219. }
  1220. if (cur_dec_code.flags & HPACK_HUFFMAN_FLAG_SYM)
  1221. {
  1222. *dst = cur_dec_code.sym;
  1223. dst++;
  1224. }
  1225. status->state = cur_dec_code.state;
  1226. status->eos = ((cur_dec_code.flags & HPACK_HUFFMAN_FLAG_ACCEPTED) != 0);
  1227. return dst;
  1228. }
  1229. #if !LS_HPACK_USE_LARGE_TABLES
  1230. #define lshpack_dec_huff_decode_full lshpack_dec_huff_decode
  1231. #endif
  1232. static int
  1233. lshpack_dec_huff_decode_full (const unsigned char *src, int src_len,
  1234. unsigned char *dst, int dst_len)
  1235. {
  1236. const unsigned char *p_src = src;
  1237. const unsigned char *const src_end = src + src_len;
  1238. unsigned char *p_dst = dst;
  1239. unsigned char *dst_end = dst + dst_len;
  1240. struct decode_status status = { 0, 1 };
  1241. while (p_src != src_end)
  1242. {
  1243. if (p_dst == dst_end)
  1244. return LSHPACK_ERR_MORE_BUF;
  1245. if ((p_dst = hdec_huff_dec4bits(*p_src >> 4, p_dst, &status))
  1246. == NULL)
  1247. return -1;
  1248. if (p_dst == dst_end)
  1249. return LSHPACK_ERR_MORE_BUF;
  1250. if ((p_dst = hdec_huff_dec4bits(*p_src & 0xf, p_dst, &status))
  1251. == NULL)
  1252. return -1;
  1253. ++p_src;
  1254. }
  1255. if (!status.eos)
  1256. return -1;
  1257. return p_dst - dst;
  1258. }
  1259. static int
  1260. lshpack_dec_huff_decode (const unsigned char *src, int src_len,
  1261. unsigned char *dst, int dst_len);
  1262. //reutrn the length in the dst, also update the src
  1263. #if !LS_HPACK_EMIT_TEST_CODE
  1264. static
  1265. #endif
  1266. int
  1267. hdec_dec_str (unsigned char *dst, size_t dst_len, const unsigned char **src,
  1268. const unsigned char *src_end)
  1269. {
  1270. if ((*src) == src_end)
  1271. return 0;
  1272. int is_huffman = (*(*src) & 0x80);
  1273. uint32_t len;
  1274. if (0 != lshpack_dec_dec_int(src, src_end, 7, &len))
  1275. return LSHPACK_ERR_BAD_DATA; //wrong int
  1276. int ret = 0;
  1277. if ((uint32_t)(src_end - (*src)) < len) {
  1278. return LSHPACK_ERR_BAD_DATA; //wrong int
  1279. }
  1280. if (is_huffman)
  1281. {
  1282. ret = lshpack_dec_huff_decode(*src, len, dst, dst_len);
  1283. if (ret < 0)
  1284. return ret; //Wrong code
  1285. (*src) += len;
  1286. }
  1287. else
  1288. {
  1289. if (dst_len < len)
  1290. {
  1291. ret = dst_len - len;
  1292. if (ret > LSHPACK_ERR_MORE_BUF)
  1293. ret = LSHPACK_ERR_MORE_BUF; //dst not enough space
  1294. }
  1295. else
  1296. {
  1297. memcpy(dst, (*src), len);
  1298. (*src) += len;
  1299. ret = len;
  1300. }
  1301. }
  1302. return ret;
  1303. }
  1304. /* hpd_dyn_table is a dynamic array. New entries are pushed onto it,
  1305. * while old entries are shifted from it.
  1306. */
  1307. static struct dec_table_entry *
  1308. hdec_get_table_entry (struct lshpack_dec *dec, uint32_t index)
  1309. {
  1310. uintptr_t val;
  1311. index -= HPACK_STATIC_TABLE_SIZE;
  1312. if (index == 0 || index > lshpack_arr_count(&dec->hpd_dyn_table))
  1313. return NULL;
  1314. index = lshpack_arr_count(&dec->hpd_dyn_table) - index;
  1315. val = lshpack_arr_get(&dec->hpd_dyn_table, index);
  1316. return (struct dec_table_entry *) val;
  1317. }
  1318. #if !LS_HPACK_EMIT_TEST_CODE
  1319. static
  1320. #endif
  1321. int
  1322. lshpack_dec_push_entry (struct lshpack_dec *dec,
  1323. const struct lsxpack_header *xhdr)
  1324. {
  1325. struct dec_table_entry *entry;
  1326. unsigned name_len, val_len;
  1327. size_t size;
  1328. name_len = xhdr->name_len;
  1329. val_len = xhdr->val_len;
  1330. size = sizeof(*entry) + name_len + val_len;
  1331. entry = malloc(size);
  1332. if (!entry)
  1333. return -1;
  1334. if (0 != lshpack_arr_push(&dec->hpd_dyn_table, (uintptr_t) entry))
  1335. {
  1336. free(entry);
  1337. return -1;
  1338. }
  1339. ++dec->hpd_state;
  1340. dec->hpd_cur_capacity += DYNAMIC_ENTRY_OVERHEAD + name_len + val_len;
  1341. entry->dte_name_len = name_len;
  1342. entry->dte_val_len = val_len;
  1343. entry->dte_name_idx = xhdr->hpack_index;
  1344. #if LSHPACK_DEC_CALC_HASH
  1345. entry->dte_flags = xhdr->flags & (LSXPACK_NAME_HASH|LSXPACK_NAMEVAL_HASH);
  1346. entry->dte_name_hash = xhdr->name_hash;
  1347. entry->dte_nameval_hash = xhdr->nameval_hash;
  1348. #endif
  1349. memcpy(DTE_NAME(entry), lsxpack_header_get_name(xhdr), name_len);
  1350. memcpy(DTE_VALUE(entry), lsxpack_header_get_value(xhdr), val_len);
  1351. return 0;
  1352. }
  1353. static int
  1354. lshpack_dec_copy_value (lsxpack_header_t *output, char *dest, const char *val,
  1355. unsigned val_len)
  1356. {
  1357. if (val_len + LSHPACK_DEC_HTTP1X_EXTRA > (unsigned)output->val_len)
  1358. return LSHPACK_ERR_MORE_BUF;
  1359. output->val_offset = output->name_offset + output->name_len
  1360. + LSHPACK_DEC_HTTP1X_EXTRA;
  1361. assert(dest == output->buf + output->val_offset);
  1362. output->val_len = val_len;
  1363. memcpy(dest, val, output->val_len);
  1364. dest += output->val_len;
  1365. #if LSHPACK_DEC_HTTP1X_OUTPUT
  1366. *dest++ = '\r';
  1367. *dest++ = '\n';
  1368. #endif
  1369. return 0;
  1370. }
  1371. static int
  1372. lshpack_dec_copy_name (lsxpack_header_t *output, char **dest, const char *name,
  1373. unsigned name_len)
  1374. {
  1375. if (name_len + LSHPACK_DEC_HTTP1X_EXTRA > (unsigned)output->val_len)
  1376. return LSHPACK_ERR_MORE_BUF;
  1377. output->val_len -= name_len + LSHPACK_DEC_HTTP1X_EXTRA;
  1378. output->name_len = name_len;
  1379. memcpy(*dest, name, name_len);
  1380. *dest += name_len;
  1381. #if LSHPACK_DEC_HTTP1X_OUTPUT
  1382. *(*dest)++ = ':';
  1383. *(*dest)++ = ' ';
  1384. #endif
  1385. return 0;
  1386. }
  1387. enum
  1388. {
  1389. LSHPACK_ADD_INDEX = 0,
  1390. LSHPACK_NO_INDEX = 1,
  1391. LSHPACK_NEVER_INDEX = 2,
  1392. LSHPACK_VAL_INDEX = 3,
  1393. };
  1394. int
  1395. lshpack_dec_decode (struct lshpack_dec *dec,
  1396. const unsigned char **src, const unsigned char *src_end,
  1397. struct lsxpack_header *output)
  1398. {
  1399. struct dec_table_entry *entry;
  1400. uint32_t index, new_capacity;
  1401. int indexed_type, len;
  1402. const unsigned char *s;
  1403. size_t buf_len = output->val_len;
  1404. size_t extra_buf = 0;
  1405. if ((*src) == src_end)
  1406. return LSHPACK_ERR_BAD_DATA;
  1407. buf_len = output->val_len;
  1408. extra_buf = 0;
  1409. s = *src;
  1410. while ((*s & 0xe0) == 0x20) //001 xxxxx
  1411. {
  1412. if (0 != lshpack_dec_dec_int(&s, src_end, 5, &new_capacity))
  1413. return LSHPACK_ERR_BAD_DATA;
  1414. if (new_capacity > dec->hpd_max_capacity)
  1415. return LSHPACK_ERR_BAD_DATA;
  1416. hdec_update_max_capacity(dec, new_capacity);
  1417. if (s == src_end)
  1418. return LSHPACK_ERR_BAD_DATA;
  1419. }
  1420. /* lshpack_dec_dec_int() sets `index' and advances `src'. If we do not
  1421. * call it, we set `index' and advance `src' ourselves:
  1422. */
  1423. if (*s & 0x80) //1 xxxxxxx
  1424. {
  1425. if (0 != lshpack_dec_dec_int(&s, src_end, 7, &index))
  1426. return LSHPACK_ERR_BAD_DATA;
  1427. if (index == 0)
  1428. return LSHPACK_ERR_BAD_DATA;
  1429. indexed_type = LSHPACK_VAL_INDEX; //need to parse value
  1430. }
  1431. else if (*s > 0x40) //01 xxxxxx
  1432. {
  1433. if (0 != lshpack_dec_dec_int(&s, src_end, 6, &index))
  1434. return LSHPACK_ERR_BAD_DATA;
  1435. indexed_type = LSHPACK_ADD_INDEX;
  1436. }
  1437. else if (*s == 0x40) //custmized //0100 0000
  1438. {
  1439. indexed_type = LSHPACK_ADD_INDEX;
  1440. index = LSHPACK_HDR_UNKNOWN;
  1441. ++s;
  1442. }
  1443. //Never indexed
  1444. else if (*s == 0x10) //00010000
  1445. {
  1446. indexed_type = LSHPACK_NEVER_INDEX;
  1447. output->flags |= LSXPACK_NEVER_INDEX;
  1448. index = LSHPACK_HDR_UNKNOWN;
  1449. ++s;
  1450. }
  1451. else if ((*s & 0xf0) == 0x10) //0001 xxxx
  1452. {
  1453. if (0 != lshpack_dec_dec_int(&s, src_end, 4, &index))
  1454. return LSHPACK_ERR_BAD_DATA;
  1455. indexed_type = LSHPACK_NEVER_INDEX;
  1456. output->flags |= LSXPACK_NEVER_INDEX;
  1457. }
  1458. //without indexed
  1459. else if (*s == 0x00) //0000 0000
  1460. {
  1461. indexed_type = LSHPACK_NO_INDEX;
  1462. index = LSHPACK_HDR_UNKNOWN;
  1463. ++s;
  1464. }
  1465. else // 0000 xxxx
  1466. {
  1467. if (0 != lshpack_dec_dec_int(&s, src_end, 4, &index))
  1468. return LSHPACK_ERR_BAD_DATA;
  1469. indexed_type = LSHPACK_NO_INDEX;
  1470. }
  1471. if (index != LSHPACK_HDR_UNKNOWN && index <= LSHPACK_HDR_WWW_AUTHENTICATE)
  1472. {
  1473. output->hpack_index = index;
  1474. }
  1475. char *name = output->buf + output->name_offset;
  1476. if (index > 0)
  1477. {
  1478. if (index <= HPACK_STATIC_TABLE_SIZE) //static table
  1479. {
  1480. if (lshpack_dec_copy_name(output, &name,
  1481. static_table[index - 1].name,
  1482. static_table[index - 1].name_len) == LSHPACK_ERR_MORE_BUF)
  1483. {
  1484. extra_buf = static_table[index - 1].name_len
  1485. + LSHPACK_DEC_HTTP1X_EXTRA;
  1486. goto need_more_buf;
  1487. }
  1488. output->flags |= LSXPACK_NAME_HASH;
  1489. output->name_hash = static_table_name_hash[index - 1];
  1490. if (indexed_type == LSHPACK_VAL_INDEX)
  1491. {
  1492. if (lshpack_dec_copy_value(output, name,
  1493. static_table[index - 1].val,
  1494. static_table[index - 1].val_len) == 0)
  1495. {
  1496. output->flags |= LSXPACK_NAMEVAL_HASH;
  1497. output->nameval_hash = static_table_nameval_hash[index - 1];
  1498. goto decode_end;
  1499. }
  1500. else
  1501. {
  1502. extra_buf = static_table[index - 1].val_len
  1503. + LSHPACK_DEC_HTTP1X_EXTRA;
  1504. goto need_more_buf;
  1505. }
  1506. }
  1507. }
  1508. else
  1509. {
  1510. entry = hdec_get_table_entry(dec, index);
  1511. if (entry == NULL)
  1512. return LSHPACK_ERR_BAD_DATA;
  1513. if (lshpack_dec_copy_name(output, &name, DTE_NAME(entry),
  1514. entry->dte_name_len) == LSHPACK_ERR_MORE_BUF)
  1515. {
  1516. extra_buf = entry->dte_name_len + LSHPACK_DEC_HTTP1X_EXTRA;
  1517. goto need_more_buf;
  1518. }
  1519. if (entry->dte_name_idx)
  1520. output->hpack_index = entry->dte_name_idx;
  1521. else
  1522. output->hpack_index = LSHPACK_HDR_UNKNOWN;
  1523. #if LSHPACK_DEC_CALC_HASH
  1524. output->flags |= entry->dte_flags & DTEF_NAME_HASH;
  1525. output->name_hash = entry->dte_name_hash;
  1526. #endif
  1527. if (indexed_type == LSHPACK_VAL_INDEX)
  1528. {
  1529. if (lshpack_dec_copy_value(output, name, DTE_VALUE(entry),
  1530. entry->dte_val_len) == 0)
  1531. {
  1532. #if LSHPACK_DEC_CALC_HASH
  1533. output->flags |= entry->dte_flags & DTEF_NAMEVAL_HASH;
  1534. output->nameval_hash = entry->dte_nameval_hash;
  1535. #endif
  1536. goto decode_end;
  1537. }
  1538. else
  1539. {
  1540. extra_buf = entry->dte_val_len + LSHPACK_DEC_HTTP1X_EXTRA;
  1541. goto need_more_buf;
  1542. }
  1543. }
  1544. }
  1545. }
  1546. else
  1547. {
  1548. len = hdec_dec_str((unsigned char *)name, output->val_len,
  1549. &s, src_end);
  1550. if (len < 0)
  1551. {
  1552. if (len <= LSHPACK_ERR_MORE_BUF)
  1553. {
  1554. extra_buf = -len;
  1555. goto need_more_buf;
  1556. }
  1557. return len; //error
  1558. }
  1559. if (len > UINT16_MAX)
  1560. return LSHPACK_ERR_TOO_LARGE;
  1561. #if LSHPACK_DEC_CALC_HASH
  1562. output->flags |= LSXPACK_NAME_HASH;
  1563. output->name_hash = XXH32(name, (size_t) len, LSHPACK_XXH_SEED);
  1564. #endif
  1565. output->name_len = len;
  1566. name += output->name_len;
  1567. #if LSHPACK_DEC_HTTP1X_OUTPUT
  1568. if (output->name_len + 2 <= output->val_len)
  1569. {
  1570. *name++ = ':';
  1571. *name++ = ' ';
  1572. }
  1573. else
  1574. {
  1575. extra_buf = 2;
  1576. goto need_more_buf;
  1577. }
  1578. #endif
  1579. output->val_len -= len + LSHPACK_DEC_HTTP1X_EXTRA;
  1580. }
  1581. len = hdec_dec_str((unsigned char *)name, output->val_len, &s, src_end);
  1582. if (len < 0)
  1583. {
  1584. if (len <= LSHPACK_ERR_MORE_BUF)
  1585. {
  1586. extra_buf = -len;
  1587. goto need_more_buf;
  1588. }
  1589. return len; //error
  1590. }
  1591. if (len > UINT16_MAX)
  1592. return LSHPACK_ERR_TOO_LARGE;
  1593. #if LSHPACK_DEC_CALC_HASH
  1594. assert(output->flags & LSXPACK_NAME_HASH);
  1595. output->flags |= LSXPACK_NAMEVAL_HASH;
  1596. output->nameval_hash = XXH32(name, (size_t) len, output->name_hash);
  1597. #endif
  1598. #if LSHPACK_DEC_HTTP1X_OUTPUT
  1599. if ((unsigned) len + 2 <= output->val_len)
  1600. memcpy(name + len, "\r\n", 2);
  1601. else
  1602. {
  1603. extra_buf = 2;
  1604. goto need_more_buf;
  1605. }
  1606. #endif
  1607. output->val_offset = output->name_offset + output->name_len
  1608. + LSHPACK_DEC_HTTP1X_EXTRA;
  1609. output->val_len = len;
  1610. if (indexed_type == LSHPACK_ADD_INDEX &&
  1611. 0 != lshpack_dec_push_entry(dec, output))
  1612. return LSHPACK_ERR_BAD_DATA; //error
  1613. decode_end:
  1614. *src = s;
  1615. #if LSHPACK_DEC_HTTP1X_OUTPUT
  1616. output->dec_overhead = 4;
  1617. #endif
  1618. return 0;
  1619. need_more_buf:
  1620. buf_len += extra_buf;
  1621. output->val_len = buf_len;
  1622. return LSHPACK_ERR_MORE_BUF;
  1623. }
  1624. #if LS_HPACK_USE_LARGE_TABLES
  1625. #define SHORTEST_CODE 5
  1626. /* The decoder is optimized for the common case. Most of the time, we decode
  1627. * data whose encoding is 16 bits or shorter. This lets us use a 64 KB table
  1628. * indexed by two bytes of input and outputs 1, 2, or 3 bytes at a time.
  1629. *
  1630. * In the case a longer code is encoutered, we fall back to the original
  1631. * Huffman decoder that supports all code lengths.
  1632. */
  1633. static int
  1634. lshpack_dec_huff_decode (const unsigned char *src, int src_len,
  1635. unsigned char *dst, int dst_len)
  1636. {
  1637. unsigned char *const orig_dst = dst;
  1638. const unsigned char *const src_end = src + src_len;
  1639. unsigned char *const dst_end = dst + dst_len;
  1640. uintptr_t buf; /* OK not to initialize the buffer */
  1641. unsigned avail_bits, len;
  1642. struct hdec hdec;
  1643. uint16_t idx;
  1644. int r;
  1645. #if __GNUC__
  1646. #pragma GCC diagnostic push
  1647. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  1648. #pragma GCC diagnostic ignored "-Wuninitialized"
  1649. #else
  1650. buf = 0;
  1651. #endif
  1652. avail_bits = 0;
  1653. while (1)
  1654. {
  1655. if (src + sizeof(buf) <= src_end)
  1656. {
  1657. len = (sizeof(buf) * 8 - avail_bits) >> 3;
  1658. avail_bits += len << 3;
  1659. switch (len)
  1660. {
  1661. #if UINTPTR_MAX == 18446744073709551615ull
  1662. case 8:
  1663. buf <<= 8;
  1664. buf |= (uintptr_t) *src++;
  1665. /* fall through */
  1666. case 7:
  1667. buf <<= 8;
  1668. buf |= (uintptr_t) *src++;
  1669. /* fall through */
  1670. default:
  1671. buf <<= 48;
  1672. buf |= (uintptr_t) *src++ << 40;
  1673. buf |= (uintptr_t) *src++ << 32;
  1674. buf |= (uintptr_t) *src++ << 24;
  1675. buf |= (uintptr_t) *src++ << 16;
  1676. #else
  1677. /* fall through */
  1678. case 4:
  1679. buf <<= 8;
  1680. buf |= (uintptr_t) *src++;
  1681. /* fall through */
  1682. case 3:
  1683. buf <<= 8;
  1684. buf |= (uintptr_t) *src++;
  1685. /* fall through */
  1686. default:
  1687. buf <<= 16;
  1688. #endif
  1689. buf |= (uintptr_t) *src++ << 8;
  1690. buf |= (uintptr_t) *src++ << 0;
  1691. }
  1692. }
  1693. else if (src < src_end)
  1694. do
  1695. {
  1696. buf <<= 8;
  1697. buf |= (uintptr_t) *src++;
  1698. avail_bits += 8;
  1699. }
  1700. while (src < src_end && avail_bits <= sizeof(buf) * 8 - 8);
  1701. else
  1702. break; /* Normal case terminating condition: out of input */
  1703. if (dst_end - dst >= (ptrdiff_t) (8 * sizeof(buf) / SHORTEST_CODE)
  1704. && avail_bits >= 16)
  1705. {
  1706. /* Fast path: don't check destination bounds */
  1707. do
  1708. {
  1709. idx = buf >> (avail_bits - 16);
  1710. hdec = hdecs[idx];
  1711. dst[0] = hdec.out[0];
  1712. dst[1] = hdec.out[1];
  1713. dst[2] = hdec.out[2];
  1714. dst += hdec.lens & 3;
  1715. avail_bits -= hdec.lens >> 2;
  1716. }
  1717. while (avail_bits >= 16 && hdec.lens);
  1718. if (avail_bits < 16)
  1719. continue;
  1720. goto slow_path;
  1721. }
  1722. else
  1723. while (avail_bits >= 16)
  1724. {
  1725. idx = buf >> (avail_bits - 16);
  1726. hdec = hdecs[idx];
  1727. len = hdec.lens & 3;
  1728. if (len && dst + len <= dst_end)
  1729. {
  1730. switch (len)
  1731. {
  1732. case 3:
  1733. *dst++ = hdec.out[0];
  1734. *dst++ = hdec.out[1];
  1735. *dst++ = hdec.out[2];
  1736. break;
  1737. case 2:
  1738. *dst++ = hdec.out[0];
  1739. *dst++ = hdec.out[1];
  1740. break;
  1741. default:
  1742. *dst++ = hdec.out[0];
  1743. break;
  1744. }
  1745. avail_bits -= hdec.lens >> 2;
  1746. }
  1747. else if (dst + len > dst_end)
  1748. {
  1749. r = dst_end - dst - len;
  1750. if (r > LSHPACK_ERR_MORE_BUF)
  1751. r = LSHPACK_ERR_MORE_BUF;
  1752. return r;
  1753. }
  1754. else
  1755. goto slow_path;
  1756. }
  1757. }
  1758. if (avail_bits >= SHORTEST_CODE)
  1759. {
  1760. idx = buf << (16 - avail_bits);
  1761. idx |= (1 << (16 - avail_bits)) - 1; /* EOF */
  1762. if (idx == 0xFFFF && avail_bits < 8)
  1763. goto end;
  1764. /* If a byte or more of input is left, this mean there is a valid
  1765. * encoding, not just EOF.
  1766. */
  1767. hdec = hdecs[idx];
  1768. len = hdec.lens & 3;
  1769. if (((unsigned) hdec.lens >> 2) > avail_bits)
  1770. return -1;
  1771. if (len && dst + len <= dst_end)
  1772. {
  1773. switch (len)
  1774. {
  1775. case 3:
  1776. *dst++ = hdec.out[0];
  1777. *dst++ = hdec.out[1];
  1778. *dst++ = hdec.out[2];
  1779. break;
  1780. case 2:
  1781. *dst++ = hdec.out[0];
  1782. *dst++ = hdec.out[1];
  1783. break;
  1784. default:
  1785. *dst++ = hdec.out[0];
  1786. break;
  1787. }
  1788. avail_bits -= hdec.lens >> 2;
  1789. }
  1790. else if (dst + len > dst_end)
  1791. {
  1792. r = dst_end - dst - len;
  1793. if (r > LSHPACK_ERR_MORE_BUF)
  1794. r = LSHPACK_ERR_MORE_BUF;
  1795. return r;
  1796. }
  1797. else
  1798. /* This must be an invalid code, otherwise it would have fit */
  1799. return -1;
  1800. }
  1801. if (avail_bits > 0)
  1802. {
  1803. if (((1u << avail_bits) - 1) != (buf & ((1u << avail_bits) - 1)))
  1804. return -1; /* Not EOF as expected */
  1805. }
  1806. #if __GNUC__
  1807. #pragma GCC diagnostic pop
  1808. #endif
  1809. end:
  1810. return dst - orig_dst;
  1811. slow_path:
  1812. /* Find previous byte boundary and finish decoding thence. */
  1813. while ((avail_bits & 7) && dst > orig_dst)
  1814. avail_bits += encode_table[ *--dst ].bits;
  1815. assert((avail_bits & 7) == 0);
  1816. src -= avail_bits >> 3;
  1817. r = lshpack_dec_huff_decode_full(src, src_end - src, dst, dst_end - dst);
  1818. if (r >= 0)
  1819. return dst - orig_dst + r;
  1820. else
  1821. return r;
  1822. }
  1823. #endif
  1824. #if __GNUC__
  1825. #pragma GCC diagnostic pop /* -Wunknown-pragmas */
  1826. #endif