summaryrefslogtreecommitdiffstats
path: root/epan/tvbuff_brotli.c
blob: 3e38c6508b431797e98a5ba3bf84bcc7af86e35f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* tvbuff_brotli.c
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <config.h>

#include <glib.h>

#include <string.h>

#ifdef HAVE_BROTLI
#include <brotli/decode.h>
#endif

#include "tvbuff.h"

#ifdef HAVE_BROTLI

/*
 * 512KiB is the buffer size used by the brotli tool, so we
 * use that too.
 */
#define TVB_BROTLI_BUFSIZ (1 << 19)

static void*
brotli_g_malloc_wrapper(void *opaque _U_, size_t size)
{
    return g_malloc(size);
}

static void
brotli_g_free_wrapper(void *opaque _U_, void *address)
{
    g_free(address);
}

/*
 * Uncompresses a brotli compressed packet inside a message of tvb at offset with
 * length comprlen.  Returns an uncompressed tvbuffer if uncompression
 * succeeded or NULL if uncompression failed.
 */

tvbuff_t *
tvb_uncompress_brotli(tvbuff_t *tvb, const int offset, int comprlen)
{
    guint8              *compr;
    guint8              *uncompr        = NULL;
    tvbuff_t            *uncompr_tvb;
    BrotliDecoderState  *decoder;
    guint8              *strmbuf;
    const size_t         bufsiz         = TVB_BROTLI_BUFSIZ;
    size_t               available_in;
    const guint8        *next_in;
    size_t               available_out;
    guint8              *next_out;
    size_t               total_out;
    guint                needs_more_output;
    guint                finished;

    if (tvb == NULL || comprlen <= 0) {
        return NULL;
    }

    compr = (guint8 *)tvb_memdup(NULL, tvb, offset, comprlen);
    if (compr == NULL) {
        return NULL;
    }

    decoder = BrotliDecoderCreateInstance(
      &brotli_g_malloc_wrapper /*alloc_func*/,
      &brotli_g_free_wrapper /*free_func*/,
      NULL /*opaque*/);
    if (decoder == NULL) {
        wmem_free(NULL, compr);
        return NULL;
    }
    strmbuf = (guint8 *)g_malloc(bufsiz);

    available_in = comprlen;
    next_in = compr;
    total_out = 0;
    needs_more_output = 0;
    finished = 0;
    while (available_in > 0 || needs_more_output) {
        needs_more_output = 0;
        available_out = bufsiz;
        next_out = strmbuf;

        BrotliDecoderResult result = BrotliDecoderDecompressStream(
          decoder, &available_in, &next_in, &available_out, &next_out, &total_out);
        switch (result) {
        case BROTLI_DECODER_RESULT_SUCCESS:
            if (available_in > 0) {
                goto cleanup;
            }
            finished = 1;
            break;
        case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
            needs_more_output = 1;
            break;
        case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
            /*
             * It's possible that not enough frames were captured
             * to decompress this fully, so return what we've done
             * so far, if any.
             */
            break;
        case BROTLI_DECODER_RESULT_ERROR:
        default:
            goto cleanup;
        }

        /*
         * Check if decompressed size is too large.
         */
        if (total_out > G_MAXINT) {
            goto cleanup;
        }

        /*
         * BrotliDecoderDecompressStream sets available_out to the number of bytes
         * left unused from the buffer. But we are interested in the bytes it wrote
         * to the buffer in this pass, so we calculate pass_out.
         */
        size_t pass_out = bufsiz - available_out;
        if (pass_out > 0) {
            uncompr = (guint8 *)g_realloc(uncompr, total_out);
            memcpy(uncompr + (total_out - pass_out), strmbuf, pass_out);
        }
    }

    if (uncompr == NULL) {
        /*
         * This is for the case when the validly decompressed
         * length is 0.
         */
        if (finished) {
            uncompr = (guint8 *)g_strdup("");
        } else {
            goto cleanup;
        }
    }

    uncompr_tvb = tvb_new_real_data((guint8 *)uncompr, (guint)total_out, (gint)total_out);
    tvb_set_free_cb(uncompr_tvb, g_free);

    g_free(strmbuf);
    wmem_free(NULL, compr);
    BrotliDecoderDestroyInstance(decoder);
    return uncompr_tvb;

cleanup:
    g_free(strmbuf);
    g_free(uncompr);
    wmem_free(NULL, compr);
    BrotliDecoderDestroyInstance(decoder);
    return NULL;
}
#else
tvbuff_t *
tvb_uncompress_brotli(tvbuff_t *tvb _U_, const int offset _U_, int comprlen _U_)
{
    return NULL;
}
#endif

tvbuff_t *
tvb_child_uncompress_brotli(tvbuff_t *parent, tvbuff_t *tvb, const int offset, int comprlen)
{
    tvbuff_t *new_tvb = tvb_uncompress_brotli(tvb, offset, comprlen);
    if (new_tvb)
        tvb_set_child_real_data_tvbuff(parent, new_tvb);
    return new_tvb;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */