1 #ifndef INCLUDED_BASE32_H 2 #define INCLUDED_BASE32_H 3 /* vim: set ts=8 sts=4 sw=4 tw=80 noet: */ 4 /*====================================================================== 5 Copyright (C) 2004,2005,2009 Walter Doekes <walter+tthsum@wjd.nu> 6 This file is part of tthsum. 7 8 tthsum is free software: you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation, either version 3 of the License, or 11 (at your option) any later version. 12 13 tthsum is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with tthsum. If not, see <http://www.gnu.org/licenses/>. 20 ======================================================================*/ 21 22 /** 23 * Conversion routines to and from BASE32. 24 */ 25 26 #include "types.h" 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif /* __cplusplus */ 31 32 /** 33 * Convert an octet-stream to base32. 34 * The octet-stream is supplied through src, the size of the octet-stream 35 * through len. 36 * The base32 will be put in dest, along with a terminating zero, make 37 * sure there is room for ceil(len*8/5)+1 characters. 38 */ 39 int uint8tobase32(char* dest, const uint8_t* src, unsigned len); 40 41 /** 42 * Convert an uint64_t-stream to base32. 43 * See uint8tobase32(). dest must be at least ceil(len*8*8/5)+1 characters 44 * long. 45 * Use this if you're feeding 64bit numbers into the stream. If you use the 46 * 8bit equivalent, BIG_ENDIAN machines will get things wrong. 47 * This may actually fail if the malloc fails. 48 */ 49 int uint64tobase32(char* dest, const uint64_t* src, unsigned len); 50 51 /** 52 * Convert a base32 octet-stream to exactly len uint8's. 53 * Make sure your dest memory is initialized to zero! 54 * If the conversion fails (due to invalid characters in src), it will return 55 * -1 instead of 0. 56 */ 57 int base32touint8(uint8_t* dest, const char* src, unsigned len); 58 59 /** 60 * Convert a base32 octet-stream to exactly len uint64's. 61 * Make sure your dest memory is initialized to zero! 62 * If the conversion fails (due to invalid characters in src), it will return 63 * -1 instead of 0. 64 */ 65 int base32touint64(uint64_t* dest, const char* src, unsigned len); 66 67 #ifdef __cplusplus 68 } /* extern "C" */ 69 #endif /* __cplusplus */ 70 71 #endif /* INCLUDED_BASE32_H */ 72