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 const char hexadecimals[] = "0123456789ABCDEF";
28 int charhex2bin(char c) {
32 return toupper(c) - 'A' + 10;
36 void hex2bin(char *src, char *dst, int length) {
38 for(i = 0; i < length; i++)
39 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
42 void bin2hex(char *src, char *dst, int length) {
44 for(i = length - 1; i >= 0; i--) {
45 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
46 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
50 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
52 #include <w32api/windows.h>
55 const char *winerror(int err) {
56 static char buf[1024], *newline;
58 if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
59 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof(buf), NULL)) {
60 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
63 if((newline = strchr(buf, '\r')))
70 unsigned int bitfield_to_int(void *bitfield, size_t size) {
71 unsigned int value = 0;
72 if(size > sizeof value)
74 memcpy(&value, bitfield, size);