/src/wireshark/wsutil/base32.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* base32.c |
2 | | * Base-32 conversion |
3 | | * |
4 | | * Wireshark - Network traffic analyzer |
5 | | * By Gerald Combs <gerald@wireshark.org> |
6 | | * Copyright 1998 Gerald Combs |
7 | | * |
8 | | * SPDX-License-Identifier: GPL-2.0-or-later |
9 | | */ |
10 | | |
11 | | #include "config.h" |
12 | | #include "base32.h" |
13 | | |
14 | | #include <string.h> |
15 | | |
16 | | /* |
17 | | * Cjdns style base32 encoding |
18 | | */ |
19 | | |
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 | 0 | #define Base32_TOO_BIG -2 |
24 | | |
25 | | int ws_base32_decode(uint8_t* output, const uint32_t outputLength, |
26 | | const uint8_t* in, const uint32_t inputLength) |
27 | 0 | { |
28 | 0 | uint32_t outIndex = 0; |
29 | 0 | uint32_t inIndex = 0; |
30 | 0 | uint32_t work = 0; |
31 | 0 | uint32_t bits = 0; |
32 | 0 | static const uint8_t* kChars = (uint8_t*) "0123456789bcdfghjklmnpqrstuvwxyz"; |
33 | 0 | while (inIndex < inputLength) { |
34 | 0 | work |= ((unsigned) in[inIndex++]) << bits; |
35 | 0 | bits += 8; |
36 | 0 | while (bits >= 5) { |
37 | 0 | if (outIndex >= outputLength) { |
38 | 0 | return Base32_TOO_BIG; |
39 | 0 | } |
40 | 0 | output[outIndex++] = kChars[work & 31]; |
41 | 0 | bits -= 5; |
42 | 0 | work >>= 5; |
43 | 0 | } |
44 | 0 | } |
45 | 0 | if (bits) { |
46 | 0 | if (outIndex >= outputLength) { |
47 | 0 | return Base32_TOO_BIG; |
48 | 0 | } |
49 | 0 | output[outIndex++] = kChars[work & 31]; |
50 | 0 | } |
51 | 0 | if (outIndex < outputLength) { |
52 | 0 | output[outIndex] = '\0'; |
53 | 0 | } |
54 | 0 | return outIndex; |
55 | 0 | } |
56 | | |
57 | | /* |
58 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
59 | | * |
60 | | * Local variables: |
61 | | * c-basic-offset: 8 |
62 | | * tab-width: 8 |
63 | | * indent-tabs-mode: t |
64 | | * End: |
65 | | * |
66 | | * vi: set shiftwidth=8 tabstop=8 noexpandtab: |
67 | | * :indentSize=8:tabSize=8:noTabs=false: |
68 | | */ |