2 utils.c -- gathering of some stupid small functions
3 Copyright (C) 1999-2005 Ivo Timmermans
4 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "../src/logger.h"
26 static const char hexadecimals[] = "0123456789ABCDEF";
27 static const char base64imals[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29 static int charhex2bin(char c) {
33 return toupper(c) - 'A' + 10;
36 static int charb64decode(char c) {
45 void hex2bin(char *src, char *dst, int length) {
47 for(i = 0; i < length; i++)
48 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
51 void bin2hex(char *src, char *dst, int length) {
53 for(i = length - 1; i >= 0; i--) {
54 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
55 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
59 int b64decode(const char *src, char *dst, int length) {
63 for(int i = 0; i < length; i++) {
64 triplet |= charb64decode(src[i]) << (6 * (i % 4));
66 udst[0] = triplet & 0xff; triplet >>= 8;
67 udst[1] = triplet & 0xff; triplet >>= 8;
73 if((length % 4) == 3) {
74 udst[0] = triplet & 0xff; triplet >>= 8;
75 udst[1] = triplet & 0xff;
76 return length / 4 * 3 + 2;
77 } else if((length % 4) == 2) {
78 udst[0] = triplet & 0xff;
79 return length / 4 * 3 + 1;
81 return length / 4 * 3;
85 int b64encode(const char *src, char *dst, int length) {
87 const unsigned char *usrc = src;
92 triplet = usrc[0] | usrc[1] << 8 | usrc[2] << 16;
93 dst[0] = base64imals[triplet & 63]; triplet >>= 6;
94 dst[1] = base64imals[triplet & 63]; triplet >>= 6;
95 dst[2] = base64imals[triplet & 63]; triplet >>= 6;
96 dst[3] = base64imals[triplet];
97 dst += 4; usrc += 3; length -= 3;
98 } else if(length >=2) {
99 triplet = usrc[0] | usrc[1] << 8;
100 dst[0] = base64imals[triplet & 63]; triplet >>= 6;
101 dst[1] = base64imals[triplet & 63]; triplet >>= 6;
102 dst[2] = base64imals[triplet];
104 return origlen / 3 * 4 + 3;
107 dst[0] = base64imals[triplet & 63]; triplet >>= 6;
108 dst[1] = base64imals[triplet];
110 return origlen / 3 * 4 + 2;
115 return origlen / 4 * 3;
118 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
120 #include <w32api/windows.h>
123 const char *winerror(int err) {
124 static char buf[1024], *newline;
126 if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
127 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof(buf), NULL)) {
128 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
131 if((newline = strchr(buf, '\r')))
138 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
139 unsigned int value = 0;
140 if(size > sizeof value)
142 memcpy(&value, bitfield, size);