TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / wsutil / base32.c
blobad8b800c91da985a5c027e81b26120f3e8ba8e45
1 /* base32.c
2 * Base-32 conversion
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"
12 #include "base32.h"
14 #include <string.h>
17 * Cjdns style base32 encoding
20 /** Returned by ws_base32_encode() if the input is not valid base32. */
21 #define Base32_BAD_INPUT -1
22 /** Returned by ws_base32_encode() if the output buffer is too small. */
23 #define Base32_TOO_BIG -2
25 int ws_base32_decode(uint8_t* output, const uint32_t outputLength,
26 const uint8_t* in, const uint32_t inputLength)
28 uint32_t outIndex = 0;
29 uint32_t inIndex = 0;
30 uint32_t work = 0;
31 uint32_t bits = 0;
32 static const uint8_t* kChars = (uint8_t*) "0123456789bcdfghjklmnpqrstuvwxyz";
33 while (inIndex < inputLength) {
34 work |= ((unsigned) in[inIndex++]) << bits;
35 bits += 8;
36 while (bits >= 5) {
37 if (outIndex >= outputLength) {
38 return Base32_TOO_BIG;
40 output[outIndex++] = kChars[work & 31];
41 bits -= 5;
42 work >>= 5;
45 if (bits) {
46 if (outIndex >= outputLength) {
47 return Base32_TOO_BIG;
49 output[outIndex++] = kChars[work & 31];
51 if (outIndex < outputLength) {
52 output[outIndex] = '\0';
54 return outIndex;
58 * Editor modelines - https://www.wireshark.org/tools/modelines.html
60 * Local variables:
61 * c-basic-offset: 8
62 * tab-width: 8
63 * indent-tabs-mode: t
64 * End:
66 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
67 * :indentSize=8:tabSize=8:noTabs=false: