Fix checks for Cygwin-related macros.
[tinc] / src / utils.c
1 /*
2     utils.c -- gathering of some stupid small functions
3     Copyright (C) 1999-2005 Ivo Timmermans
4                   2000-2014 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 static const char hexadecimals[] = "0123456789ABCDEF";
27
28 static int charhex2bin(char c) {
29         if(isdigit(c)) {
30                 return c - '0';
31         } else {
32                 return toupper(c) - 'A' + 10;
33         }
34 }
35
36 bool hex2bin(char *src, char *dst, int length) {
37         for(int i = 0; i < length; i++) {
38                 if(!isxdigit(src[i * 2]) || !isxdigit(src[i * 2 + 1])) {
39                         return false;
40                 }
41
42                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
43         }
44
45         return true;
46 }
47
48 void bin2hex(char *src, char *dst, int length) {
49         int i;
50
51         for(i = length - 1; i >= 0; i--) {
52                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
53                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
54         }
55 }
56
57 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
58 #ifdef HAVE_CYGWIN
59 #include <w32api/windows.h>
60 #endif
61
62 const char *winerror(int err) {
63         static char buf[1024], *ptr;
64
65         ptr = buf + sprintf(buf, "(%d) ", err);
66
67         if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
68                           NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), ptr, sizeof(buf) - (ptr - buf), NULL)) {
69                 strcpy(ptr, "(unable to format errormessage)");
70         };
71
72         if((ptr = strchr(buf, '\r'))) {
73                 *ptr = '\0';
74         }
75
76         return buf;
77 }
78 #endif
79
80 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
81         unsigned int value = 0;
82
83         if(size > sizeof(value)) {
84                 size = sizeof(value);
85         }
86
87         memcpy(&value, bitfield, size);
88         return value;
89 }
90
91 /**
92  * As memcmp(), but constant-time.
93  * Returns 0 when data is equal, non-zero otherwise.
94  */
95 int memcmp_constant_time(const void *a, const void *b, size_t size) {
96         const uint8_t *a1 = a, *b1 = b;
97         int ret = 0;
98         size_t i;
99
100         for(i = 0; i < size; i++) {
101                 ret |= *a1++ ^ *b1++;
102         }
103
104         return ret;
105 }