dcerpc-netlogon: improve NetrLogonGetCapabilities dissection
[wireshark-sm.git] / epan / tvbuff_hpackhuff.c
blob189ce49805d90ee87536c2e727ba997053ea41da
1 /* @file
2 * Decompression of the Huffman encoding used for HTTP fields in HPACK (HTTP/2)
3 * and QPACK (HTTP/3)
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "config.h"
14 #include <epan/tvbuff.h>
15 #include <epan/nghttp2_hd_huffman.h>
17 static wmem_strbuf_t *
18 get_hpack_huffman_strbuf(wmem_allocator_t *scope, const uint8_t *ptr, size_t len)
20 wmem_strbuf_t *strbuf;
21 strbuf = wmem_strbuf_new_sized(scope, len + 1);
23 nghttp2_huff_decode node = {0, 0};
24 const nghttp2_huff_decode *nodep = &node;
26 while (len > 0) {
27 uint8_t ch = *ptr++;
29 nodep = &huff_decode_table[nodep->fstate & 0x1ff][ch >> 4];
30 if (nodep->fstate & NGHTTP2_HUFF_SYM) {
31 wmem_strbuf_append_c(strbuf, nodep->sym);
34 nodep = &huff_decode_table[nodep->fstate & 0x1ff][ch & 0xf];
35 if (nodep->fstate & NGHTTP2_HUFF_SYM) {
36 wmem_strbuf_append_c(strbuf, nodep->sym);
39 len--;
42 if (!(nodep->fstate & NGHTTP2_HUFF_ACCEPTED)) {
43 wmem_strbuf_destroy(strbuf);
44 return NULL;
47 return strbuf;
50 wmem_strbuf_t *
51 tvb_get_hpack_huffman_strbuf(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, const int len)
53 return get_hpack_huffman_strbuf(scope, tvb_get_ptr(tvb, offset, len), len);
56 tvbuff_t*
57 tvb_child_uncompress_hpack_huff(tvbuff_t* parent, int offset, int length)
59 tvbuff_t* tvb = NULL;
60 wmem_strbuf_t *strbuf;
61 char* data;
62 size_t len;
64 strbuf = tvb_get_hpack_huffman_strbuf(NULL, parent, offset, length);
66 if (strbuf) {
67 len = wmem_strbuf_get_len(strbuf);
68 data = wmem_strbuf_finalize(strbuf);
70 tvb = tvb_new_child_real_data(parent, (const uint8_t*)data, (unsigned)len, (int)len);
72 tvb_set_free_cb(tvb, g_free);
75 return tvb;