nghttp3_qpack_huffman.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "nghttp3_qpack_huffman.h"
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <stdio.h>
  30. #include "nghttp3_conv.h"
  31. size_t nghttp3_qpack_huffman_encode_count(const uint8_t *src, size_t len) {
  32. size_t i;
  33. size_t nbits = 0;
  34. for (i = 0; i < len; ++i) {
  35. nbits += huffman_sym_table[src[i]].nbits;
  36. }
  37. /* pad the prefix of EOS (256) */
  38. return (nbits + 7) / 8;
  39. }
  40. uint8_t *nghttp3_qpack_huffman_encode(uint8_t *dest, const uint8_t *src,
  41. size_t srclen) {
  42. const nghttp3_qpack_huffman_sym *sym;
  43. const uint8_t *end = src + srclen;
  44. uint64_t code = 0;
  45. size_t nbits = 0;
  46. uint32_t x;
  47. for (; src != end;) {
  48. sym = &huffman_sym_table[*src++];
  49. code |= (uint64_t)sym->code << (32 - nbits);
  50. nbits += sym->nbits;
  51. if (nbits < 32) {
  52. continue;
  53. }
  54. x = htonl((uint32_t)(code >> 32));
  55. memcpy(dest, &x, 4);
  56. dest += 4;
  57. code <<= 32;
  58. nbits -= 32;
  59. }
  60. for (; nbits >= 8;) {
  61. *dest++ = (uint8_t)(code >> 56);
  62. code <<= 8;
  63. nbits -= 8;
  64. }
  65. if (nbits) {
  66. *dest++ = (uint8_t)((uint8_t)(code >> 56) | ((1 << (8 - nbits)) - 1));
  67. }
  68. return dest;
  69. }
  70. void nghttp3_qpack_huffman_decode_context_init(
  71. nghttp3_qpack_huffman_decode_context *ctx) {
  72. ctx->fstate = NGHTTP3_QPACK_HUFFMAN_ACCEPTED;
  73. }
  74. nghttp3_ssize
  75. nghttp3_qpack_huffman_decode(nghttp3_qpack_huffman_decode_context *ctx,
  76. uint8_t *dest, const uint8_t *src, size_t srclen,
  77. int fin) {
  78. uint8_t *p = dest;
  79. const uint8_t *end = src + srclen;
  80. nghttp3_qpack_huffman_decode_node node = {ctx->fstate, 0};
  81. const nghttp3_qpack_huffman_decode_node *t = &node;
  82. uint8_t c;
  83. /* We use the decoding algorithm described in
  84. - http://graphics.ics.uci.edu/pub/Prefix.pdf [!!! NO LONGER VALID !!!]
  85. - https://ics.uci.edu/~dan/pubs/Prefix.pdf
  86. - https://github.com/nghttp2/nghttp2/files/15141264/Prefix.pdf */
  87. for (; src != end;) {
  88. c = *src++;
  89. t = &qpack_huffman_decode_table[t->fstate & 0x1ff][c >> 4];
  90. if (t->fstate & NGHTTP3_QPACK_HUFFMAN_SYM) {
  91. *p++ = t->sym;
  92. }
  93. t = &qpack_huffman_decode_table[t->fstate & 0x1ff][c & 0xf];
  94. if (t->fstate & NGHTTP3_QPACK_HUFFMAN_SYM) {
  95. *p++ = t->sym;
  96. }
  97. }
  98. ctx->fstate = t->fstate;
  99. if (fin && !(ctx->fstate & NGHTTP3_QPACK_HUFFMAN_ACCEPTED)) {
  100. return NGHTTP3_ERR_QPACK_FATAL;
  101. }
  102. return p - dest;
  103. }
  104. int nghttp3_qpack_huffman_decode_failure_state(
  105. nghttp3_qpack_huffman_decode_context *ctx) {
  106. return ctx->fstate == 0x100;
  107. }