Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / tvbuff_base64.c
blobf1d7d67a3391e1325ead7c6845d75734eef477cd
1 /* tvbuff_base64.c
2 * Base-64 tvbuff implementation (based on real tvb)
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include "config.h"
13 #include <glib.h>
15 #include <epan/tvbuff.h>
16 #include "proto.h"
18 /* Copy of glib function modified for base64uri */
20 static const unsigned char mime_base64uri_rank[256] = {
21 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
22 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
23 255,255,255,255,255,255,255,255,255,255,255, 255,255,63,255,255,
24 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
25 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
26 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255, 63,
27 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
28 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
29 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
30 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
31 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
32 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
33 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
34 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
35 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
36 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
38 /**
39 * Copy of glib function modified for base64uri
40 * g_base64uri_decode_step: (skip)
41 * @in: (array length=len) (element-type uint8_t): binary input data
42 * @len: max length of @in data to decode
43 * @out: (out caller-allocates) (array) (element-type uint8_t): output buffer
44 * @state: (inout): Saved state between steps, initialize to 0
45 * @save: (inout): Saved state between steps, initialize to 0
47 * Incrementally decode a sequence of binary data from its Base-64 stringified
48 * representation. By calling this function multiple times you can convert
49 * data in chunks to avoid having to have the full encoded data in memory.
51 * The output buffer must be large enough to fit all the data that will
52 * be written to it. Since base64 encodes 3 bytes in 4 chars you need
53 * at least: (@len / 4) * 3 + 3 bytes (+ 3 may be needed in case of non-zero
54 * state).
56 * Returns: The number of bytes of output that was written
58 * Since: 2.12
59 **/
60 static size_t
61 g_base64uri_decode_step(const char* in,
62 size_t len,
63 unsigned char* out,
64 int* state,
65 unsigned* save)
67 const unsigned char* inptr;
68 unsigned char* outptr;
69 const unsigned char* inend;
70 unsigned char c, rank;
71 unsigned char last[2];
72 unsigned int v;
73 int i;
75 g_return_val_if_fail(in != NULL || len == 0, 0);
76 g_return_val_if_fail(out != NULL, 0);
77 g_return_val_if_fail(state != NULL, 0);
78 g_return_val_if_fail(save != NULL, 0);
80 if (len == 0)
81 return 0;
83 inend = (const unsigned char*)in + len;
84 outptr = out;
86 /* convert 4 base64 bytes to 3 normal bytes */
87 v = *save;
88 i = *state;
90 last[0] = last[1] = 0;
92 /* we use the sign in the state to determine if we got a padding character
93 in the previous sequence */
94 if (i < 0)
96 i = -i;
97 last[0] = '=';
100 inptr = (const unsigned char*)in;
101 while (inptr < inend)
103 c = *inptr++;
104 rank = mime_base64uri_rank[c];
105 if (rank != 0xff)
107 last[1] = last[0];
108 last[0] = c;
109 v = (v << 6) | rank;
110 i++;
111 if (i == 4)
113 *outptr++ = v >> 16;
114 if (last[1] != '=')
115 *outptr++ = v >> 8;
116 if (last[0] != '=')
117 *outptr++ = v;
118 i = 0;
123 *save = v;
124 *state = last[0] == '=' ? -i : i;
126 return outptr - out;
129 * Copy of glib function modified for base64uri
130 * g_base64uri_decode:
131 * @text: (not nullable): zero-terminated string with base64 text to decode
132 * @out_len: (out): The length of the decoded data is written here
134 * Decode a sequence of Base-64 encoded text into binary data. Note
135 * that the returned binary data is not necessarily zero-terminated,
136 * so it should not be used as a character string.
138 * Returns: (transfer full) (array length=out_len) (element-type uint8_t):
139 * newly allocated buffer containing the binary data
140 * that @text represents. The returned buffer must
141 * be freed with g_free().
143 * Since: 2.12
145 static unsigned char*
146 g_base64uri_decode(const char* text,
147 size_t* out_len)
149 unsigned char* ret;
150 size_t input_length;
151 int state = 0;
152 unsigned save = 0;
154 g_return_val_if_fail(text != NULL, NULL);
155 g_return_val_if_fail(out_len != NULL, NULL);
157 input_length = strlen(text);
159 /* We can use a smaller limit here, since we know the saved state is 0,
160 +1 used to avoid calling g_malloc0(0), and hence returning NULL */
161 ret = (unsigned char * )g_malloc0((input_length / 4) * 3 + 1);
163 *out_len = g_base64uri_decode_step(text, input_length, ret, &state, &save);
165 return ret;
168 tvbuff_t *
169 base64_to_tvb(tvbuff_t *parent, const char *base64)
171 tvbuff_t *tvb;
172 char *data;
173 size_t len;
175 data = g_base64_decode(base64, &len);
176 tvb = tvb_new_child_real_data(parent, (const uint8_t *)data, (int)len, (int)len);
178 tvb_set_free_cb(tvb, g_free);
180 return tvb;
183 tvbuff_t*
184 base64_tvb_to_new_tvb(tvbuff_t* parent, int offset, int length)
186 tvbuff_t* tvb;
187 char* data, *tmp;
188 size_t len;
190 tmp = tvb_get_string_enc(NULL, parent, offset, length, ENC_ASCII);
191 data = g_base64_decode(tmp, &len);
192 wmem_free(NULL, tmp);
194 tvb = tvb_new_child_real_data(parent, (const uint8_t*)data, (int)len, (int)len);
196 tvb_set_free_cb(tvb, g_free);
198 return tvb;
201 tvbuff_t*
202 base64uri_tvb_to_new_tvb(tvbuff_t* parent, int offset, int length)
204 tvbuff_t* tvb;
205 char* data, *tmp;
206 size_t len = 0;
208 tmp = tvb_get_string_enc(NULL, parent, offset, length, ENC_ASCII);
209 data = g_base64uri_decode(tmp, &len);
210 wmem_free(NULL, tmp);
212 tvb = tvb_new_child_real_data(parent, (const uint8_t*)data, (int)len, (int)len);
214 tvb_set_free_cb(tvb, g_free);
216 return tvb;
219 * Editor modelines - https://www.wireshark.org/tools/modelines.html
221 * Local Variables:
222 * c-basic-offset: 2
223 * tab-width: 8
224 * indent-tabs-mode: nil
225 * End:
227 * ex: set shiftwidth=2 tabstop=8 expandtab:
228 * :indentSize=2:tabSize=8:noTabs=true: