]> git.meshlink.io Git - meshlink/blob - src/utils.c
Merge branch 'master' into saverio
[meshlink] / src / utils.c
1 /*
2     utils.c -- gathering of some stupid small functions
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "../src/logger.h"
23 #include "utils.h"
24
25 static const char hexadecimals[] = "0123456789ABCDEF";
26 static const char base64_original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
27 static const char base64_urlsafe[] =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
28 static const char base64_decode[256] = {
29         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
32         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
33         -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
34         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
35         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
36         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
37         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 };
46
47 static int charhex2bin(char c) {
48         if(isdigit(c))
49                 return c - '0';
50         else
51                 return toupper(c) - 'A' + 10;
52 }
53
54 int hex2bin(const char *src, char *dst, int length) {
55         int i;
56         for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++)
57                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
58         return i;
59 }
60
61 int bin2hex(const char *src, char *dst, int length) {
62         for(int i = length - 1; i >= 0; i--) {
63                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
64                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
65         }
66         dst[length * 2] = 0;
67         return length * 2;
68 }
69
70 int b64decode(const char *src, char *dst, int length) {
71         int i;
72         uint32_t triplet = 0;
73         unsigned char *udst = (unsigned char *)dst;
74
75         for(i = 0; i < length / 3 * 4 && src[i]; i++) {
76                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
77                 if((i & 3) == 3) {
78                         if(triplet & 0xff000000U)
79                                 return 0;
80                         udst[0] = triplet & 0xff; triplet >>= 8;
81                         udst[1] = triplet & 0xff; triplet >>= 8;
82                         udst[2] = triplet;
83                         triplet = 0;
84                         udst += 3;
85                 }
86         }
87         if(triplet & 0xff000000U)
88                 return 0;
89         if((i & 3) == 3) {
90                 udst[0] = triplet & 0xff; triplet >>= 8;
91                 udst[1] = triplet & 0xff;
92                 return i / 4 * 3 + 2;
93         } else if((i & 3) == 2) {
94                 udst[0] = triplet & 0xff;
95                 return i / 4 * 3 + 1;
96         } else {
97                 return i / 4 * 3;
98         }
99 }
100
101 static int b64encode_internal(const char *src, char *dst, int length, const char *alphabet) {
102         uint32_t triplet;
103         const unsigned char *usrc = (unsigned char *)src;
104         int si = length / 3 * 3;
105         int di = length / 3 * 4;
106
107         switch(length % 3) {
108                 case 2:
109                         triplet = usrc[si] | usrc[si + 1] << 8;
110                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
111                         dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
112                         dst[di + 2] = alphabet[triplet];
113                         dst[di + 3] = 0;
114                         length = di + 2;
115                         break;
116                 case 1:
117                         triplet = usrc[si];
118                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
119                         dst[di + 1] = alphabet[triplet];
120                         dst[di + 2] = 0;
121                         length = di + 1;
122                         break;
123                 default:
124                         dst[di] = 0;
125                         length = di;
126                         break;
127         }
128
129         while(si > 0) {
130                 di -= 4;
131                 si -= 3;
132                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
133                 dst[di] = alphabet[triplet & 63]; triplet >>= 6;
134                 dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
135                 dst[di + 2] = alphabet[triplet & 63]; triplet >>= 6;
136                 dst[di + 3] = alphabet[triplet];
137         }
138
139         return length;
140 }
141
142 int b64encode(const char *src, char *dst, int length) {
143         return b64encode_internal(src, dst, length, base64_original);
144 }
145
146 int b64encode_urlsafe(const char *src, char *dst, int length) {
147         return b64encode_internal(src, dst, length, base64_urlsafe);
148 }
149
150 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
151 #ifdef HAVE_CYGWIN
152 #include <w32api/windows.h>
153 #endif
154
155 const char *winerror(int err) {
156         static char buf[1024], *ptr;
157
158         ptr = buf + sprintf(buf, "(%d) ", err);
159
160         if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
161                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
162                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
163         };
164
165         if((ptr = strchr(buf, '\r')))
166                 *ptr = '\0';
167
168         return buf;
169 }
170 #endif
171
172 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
173         unsigned int value = 0;
174         if(size > sizeof value)
175                 size = sizeof value;
176         memcpy(&value, bitfield, size);
177         return value;
178 }