Replace bogus #else with #endif.
[tinc] / lib / utils.c
1 /*
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>
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "../src/logger.h"
24 #include "utils.h"
25
26 const char hexadecimals[] = "0123456789ABCDEF";
27
28 int charhex2bin(char c) {
29         if(isdigit(c))
30                 return c - '0';
31         else
32                 return toupper(c) - 'A' + 10;
33 }
34
35
36 void hex2bin(char *src, char *dst, int length) {
37         int i;
38         for(i = 0; i < length; i++)
39                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
40 }
41
42 void bin2hex(char *src, char *dst, int length) {
43         int i;
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];
47         }
48 }
49
50 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
51 #ifdef HAVE_CYGWIN
52 #include <w32api/windows.h>
53 #endif
54
55 const char *winerror(int err) {
56         static char buf[1024], *newline;
57
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));
61         };
62
63         if((newline = strchr(buf, '\r')))
64                 *newline = '\0';
65
66         return buf;
67 }
68 #endif
69
70 unsigned int bitfield_to_int(void *bitfield, size_t size) {
71         unsigned int value = 0;
72         if(size > sizeof value)
73                 size = sizeof value;
74         memcpy(&value, bitfield, size);
75         return value;
76 }