nghttp3_qpack_huffman.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #include <nghttp3/nghttp3.h>
  32. typedef struct nghttp3_qpack_huffman_sym {
  33. /* The number of bits in this code */
  34. uint32_t nbits;
  35. /* Huffman code aligned to LSB */
  36. uint32_t code;
  37. } nghttp3_qpack_huffman_sym;
  38. extern const nghttp3_qpack_huffman_sym huffman_sym_table[];
  39. size_t nghttp3_qpack_huffman_encode_count(const uint8_t *src, size_t len);
  40. uint8_t *nghttp3_qpack_huffman_encode(uint8_t *dest, const uint8_t *src,
  41. size_t srclen);
  42. typedef enum nghttp3_qpack_huffman_decode_flag {
  43. /* FSA accepts this state as the end of huffman encoding
  44. sequence. */
  45. NGHTTP3_QPACK_HUFFMAN_ACCEPTED = 1 << 14,
  46. /* This state emits symbol */
  47. NGHTTP3_QPACK_HUFFMAN_SYM = 1 << 15,
  48. } nghttp3_qpack_huffman_decode_flag;
  49. typedef struct nghttp3_qpack_huffman_decode_node {
  50. /* fstate is the current huffman decoding state, which is actually
  51. the node ID of internal huffman tree with
  52. nghttp3_qpack_huffman_decode_flag OR-ed. We have 257 leaf nodes,
  53. but they are identical to root node other than emitting a symbol,
  54. so we have 256 internal nodes [1..256], inclusive. The node ID
  55. 256 is a special node and it is a terminal state that means
  56. decoding failed. */
  57. uint16_t fstate;
  58. /* symbol if NGHTTP3_QPACK_HUFFMAN_SYM flag set */
  59. uint8_t sym;
  60. } nghttp3_qpack_huffman_decode_node;
  61. typedef struct nghttp3_qpack_huffman_decode_context {
  62. /* fstate is the current huffman decoding state. */
  63. uint16_t fstate;
  64. } nghttp3_qpack_huffman_decode_context;
  65. extern const nghttp3_qpack_huffman_decode_node qpack_huffman_decode_table[][16];
  66. void nghttp3_qpack_huffman_decode_context_init(
  67. nghttp3_qpack_huffman_decode_context *ctx);
  68. /*
  69. * nghttp3_qpack_huffman_decode decodes huffman encoded byte string
  70. * stored in |src| of length |srclen|. |ctx| is a decoding context.
  71. * |ctx| remembers the decoding state, and caller can call this
  72. * function multiple times to feed each chunk of huffman encoded
  73. * substring. |fin| must be nonzero if |src| contains the last chunk
  74. * of huffman string. The decoded string is written to the buffer
  75. * pointed by |dest|. This function assumes that the buffer pointed
  76. * by |dest| contains enough memory to store decoded byte string.
  77. *
  78. * This function returns the number of bytes written to |dest|, or one
  79. * of the following negative error codes:
  80. *
  81. * NGHTTP3_ERR_QPACK_FATAL
  82. * Could not decode huffman string.
  83. */
  84. nghttp3_ssize
  85. nghttp3_qpack_huffman_decode(nghttp3_qpack_huffman_decode_context *ctx,
  86. uint8_t *dest, const uint8_t *src, size_t srclen,
  87. int fin);
  88. /*
  89. * nghttp3_qpack_huffman_decode_failure_state returns nonzero if |ctx|
  90. * indicates that huffman decoding context is in failure state.
  91. */
  92. int nghttp3_qpack_huffman_decode_failure_state(
  93. nghttp3_qpack_huffman_decode_context *ctx);
  94. #endif /* !defined(NGHTTP3_QPACK_HUFFMAN_H) */