]> git.meshlink.io Git - meshlink/blob - src/utils.c
Merge branch 'discovery' of chicago.everbase.net:meshlink/meshlink into everbase
[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, void *vdst, int length) {
55         uint8_t *dst = vdst;
56         int i;
57         for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++)
58                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
59         return i;
60 }
61
62 int bin2hex(const void *vsrc, char *dst, int length) {
63         const uint8_t *src = vsrc;
64         for(int i = length - 1; i >= 0; i--) {
65                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
66                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
67         }
68         dst[length * 2] = 0;
69         return length * 2;
70 }
71
72 int b64decode(const char *src, void *dst, int length) {
73         int i;
74         uint32_t triplet = 0;
75         unsigned char *udst = dst;
76
77         for(i = 0; i < length / 3 * 4 && src[i]; i++) {
78                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
79                 if((i & 3) == 3) {
80                         if(triplet & 0xff000000U)
81                                 return 0;
82                         udst[0] = triplet & 0xff; triplet >>= 8;
83                         udst[1] = triplet & 0xff; triplet >>= 8;
84                         udst[2] = triplet;
85                         triplet = 0;
86                         udst += 3;
87                 }
88         }
89         if(triplet & 0xff000000U)
90                 return 0;
91         if((i & 3) == 3) {
92                 udst[0] = triplet & 0xff; triplet >>= 8;
93                 udst[1] = triplet & 0xff;
94                 return i / 4 * 3 + 2;
95         } else if((i & 3) == 2) {
96                 udst[0] = triplet & 0xff;
97                 return i / 4 * 3 + 1;
98         } else {
99                 return i / 4 * 3;
100         }
101 }
102
103 static int b64encode_internal(const void *src, char *dst, int length, const char *alphabet) {
104         uint32_t triplet;
105         const unsigned char *usrc = src;
106         int si = length / 3 * 3;
107         int di = length / 3 * 4;
108
109         switch(length % 3) {
110                 case 2:
111                         triplet = usrc[si] | usrc[si + 1] << 8;
112                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
113                         dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
114                         dst[di + 2] = alphabet[triplet];
115                         dst[di + 3] = 0;
116                         length = di + 2;
117                         break;
118                 case 1:
119                         triplet = usrc[si];
120                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
121                         dst[di + 1] = alphabet[triplet];
122                         dst[di + 2] = 0;
123                         length = di + 1;
124                         break;
125                 default:
126                         dst[di] = 0;
127                         length = di;
128                         break;
129         }
130
131         while(si > 0) {
132                 di -= 4;
133                 si -= 3;
134                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
135                 dst[di] = alphabet[triplet & 63]; triplet >>= 6;
136                 dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
137                 dst[di + 2] = alphabet[triplet & 63]; triplet >>= 6;
138                 dst[di + 3] = alphabet[triplet];
139         }
140
141         return length;
142 }
143
144 int b64encode(const void *src, char *dst, int length) {
145         return b64encode_internal(src, dst, length, base64_original);
146 }
147
148 int b64encode_urlsafe(const void *src, char *dst, int length) {
149         return b64encode_internal(src, dst, length, base64_urlsafe);
150 }
151
152 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
153 #ifdef HAVE_CYGWIN
154 #include <w32api/windows.h>
155 #endif
156
157 const char *winerror(int err) {
158         static char buf[1024], *ptr;
159
160         ptr = buf + sprintf(buf, "(%d) ", err);
161
162         if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
163                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
164                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
165         };
166
167         if((ptr = strchr(buf, '\r')))
168                 *ptr = '\0';
169
170         return buf;
171 }
172 #endif
173
174 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
175         unsigned int value = 0;
176         if(size > sizeof value)
177                 size = sizeof value;
178         memcpy(&value, bitfield, size);
179         return value;
180 }