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