]> git.meshlink.io Git - meshlink/blob - src/utils.c
Merge branch 'master' of git://tinc-vpn.org/tinc into 1.1
[meshlink] / src / 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 static const char hexadecimals[] = "0123456789ABCDEF";
27 static const char base64imals[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28
29 static int charhex2bin(char c) {
30         if(isdigit(c))
31                 return c - '0';
32         else
33                 return toupper(c) - 'A' + 10;
34 }
35
36 static int charb64decode(char c) {
37         if(c >= 'a')
38                 return c - 'a' + 26;
39         else if(c >= 'A')
40                 return c - 'A';
41         else if(c >= '0') 
42                 return c - '0' + 52;
43         else if(c == '+')
44                 return 62;
45         else
46                 return 63;
47 }
48
49 int hex2bin(const char *src, char *dst, int length) {
50         int i;
51         for(i = 0; i < length && src[i * 2] && src[i * 2 + 1]; i++)
52                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
53         return i;
54 }
55
56 int bin2hex(const char *src, char *dst, int length) {
57         int i;
58         for(i = length - 1; i >= 0; i--) {
59                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
60                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
61         }
62         dst[length * 2] = 0;
63         return length * 2;
64 }
65
66 int b64decode(const char *src, char *dst, int length) {
67         int i;
68         uint32_t triplet = 0;
69         unsigned char *udst = (unsigned char *)dst;
70
71         for(i = 0; i < length / 3 * 4 && src[i]; i++) {
72                 triplet |= charb64decode(src[i]) << (6 * (i & 3));
73                 if((i & 3) == 3) {
74                         udst[0] = triplet & 0xff; triplet >>= 8;
75                         udst[1] = triplet & 0xff; triplet >>= 8;
76                         udst[2] = triplet;
77                         triplet = 0;
78                         udst += 3;
79                 }
80         }
81         if((i & 3) == 3) {
82                 udst[0] = triplet & 0xff; triplet >>= 8;
83                 udst[1] = triplet & 0xff;
84                 return i / 4 * 3 + 2;
85         } else if((i & 3) == 2) {
86                 udst[0] = triplet & 0xff;
87                 return i / 4 * 3 + 1;
88         } else {
89                 return i / 4 * 3;
90         }
91 }
92
93 int b64encode(const char *src, char *dst, int length) {
94         uint32_t triplet;
95         const unsigned char *usrc = (unsigned char *)src;
96         int si = length / 3 * 3;
97         int di = length / 3 * 4;
98
99         switch(length % 3) {
100                 case 2: 
101                         triplet = usrc[si] | usrc[si + 1] << 8;
102                         dst[di] = base64imals[triplet & 63]; triplet >>= 6;
103                         dst[di + 1] = base64imals[triplet & 63]; triplet >>= 6;
104                         dst[di + 2] = base64imals[triplet];
105                         dst[di + 3] = 0;
106                         length = di + 2;
107                         break;
108                 case 1:
109                         triplet = usrc[si];
110                         dst[di] = base64imals[triplet & 63]; triplet >>= 6;
111                         dst[di + 1] = base64imals[triplet];
112                         dst[di + 2] = 0;
113                         length = di + 1;
114                         break;
115                 default:
116                         dst[di] = 0;
117                         length = di;
118                         break;
119         }
120
121         while(si > 0) {
122                 di -= 4;
123                 si -= 3;
124                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
125                 dst[di] = base64imals[triplet & 63]; triplet >>= 6;
126                 dst[di + 1] = base64imals[triplet & 63]; triplet >>= 6;
127                 dst[di + 2] = base64imals[triplet & 63]; triplet >>= 6;
128                 dst[di + 3] = base64imals[triplet];
129         }
130
131         return length;
132 }
133
134 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
135 #ifdef HAVE_CYGWIN
136 #include <w32api/windows.h>
137 #endif
138
139 const char *winerror(int err) {
140         static char buf[1024], *ptr;
141
142         ptr = buf + sprintf(buf, "(%d) ", err);
143
144         if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
145                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
146                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
147         };
148
149         if((ptr = strchr(buf, '\r')))
150                 *ptr = '\0';
151
152         return buf;
153 }
154 #endif
155
156 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
157         unsigned int value = 0;
158         if(size > sizeof value)
159                 size = sizeof value;
160         memcpy(&value, bitfield, size);
161         return value;
162 }