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 13 #include <glib.h> 14 15 #include <string.h> 16 #include "base32.h" 17 18 /* 19 * Cjdns style base32 encoding 20 */ 21 22 /** Returned by ws_base32_encode() if the input is not valid base32. */ 23 #define Base32_BAD_INPUT -1 24 /** Returned by ws_base32_encode() if the output buffer is too small. */ 25 #define Base32_TOO_BIG -2 26 ws_base32_decode(guint8 * output,const guint32 outputLength,const guint8 * in,const guint32 inputLength)27int ws_base32_decode(guint8* output, const guint32 outputLength, 28 const guint8* in, const guint32 inputLength) 29 { 30 guint32 outIndex = 0; 31 guint32 inIndex = 0; 32 guint32 work = 0; 33 guint32 bits = 0; 34 static const guint8* kChars = (guint8*) "0123456789bcdfghjklmnpqrstuvwxyz"; 35 while (inIndex < inputLength) { 36 work |= ((unsigned) in[inIndex++]) << bits; 37 bits += 8; 38 while (bits >= 5) { 39 if (outIndex >= outputLength) { 40 return Base32_TOO_BIG; 41 } 42 output[outIndex++] = kChars[work & 31]; 43 bits -= 5; 44 work >>= 5; 45 } 46 } 47 if (bits) { 48 if (outIndex >= outputLength) { 49 return Base32_TOO_BIG; 50 } 51 output[outIndex++] = kChars[work & 31]; 52 } 53 if (outIndex < outputLength) { 54 output[outIndex] = '\0'; 55 } 56 return outIndex; 57 } 58 59 /* 60 * Editor modelines - https://www.wireshark.org/tools/modelines.html 61 * 62 * Local variables: 63 * c-basic-offset: 8 64 * tab-width: 8 65 * indent-tabs-mode: t 66 * End: 67 * 68 * vi: set shiftwidth=8 tabstop=8 noexpandtab: 69 * :indentSize=8:tabSize=8:noTabs=false: 70 */ 71