]> git.meshlink.io Git - meshlink/blob - src/utils.c
Finish base64 decoding routine.
[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 void hex2bin(char *src, char *dst, int length) {
50         int i;
51         for(i = 0; i < length; i++)
52                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
53 }
54
55 void bin2hex(char *src, char *dst, int length) {
56         int i;
57         for(i = length - 1; i >= 0; i--) {
58                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
59                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
60         }
61 }
62
63 int b64decode(const char *src, char *dst, int length) {
64         uint32_t triplet = 0;
65         unsigned char *udst = dst;
66
67         for(int i = 0; i < length; i++) {
68                 triplet |= charb64decode(src[i]) << (6 * (i & 3));
69                 if((i & 3) == 3) {
70                         udst[0] = triplet & 0xff; triplet >>= 8;
71                         udst[1] = triplet & 0xff; triplet >>= 8;
72                         udst[2] = triplet;
73                         triplet = 0;
74                         udst += 3;
75                 }
76         }
77         if((length & 3) == 3) {
78                 udst[0] = triplet & 0xff; triplet >>= 8;
79                 udst[1] = triplet & 0xff;
80                 return length / 4 * 3 + 2;
81         } else if((length & 3) == 2) {
82                 udst[0] = triplet & 0xff;
83                 return length / 4 * 3 + 1;
84         } else {
85                 return length / 4 * 3;
86         }
87 }
88
89 int b64encode(const char *src, char *dst, int length) {
90         uint32_t triplet;
91         const unsigned char *usrc = src;
92         int origlen = length;
93
94         while(length > 0) {
95                 if(length >= 3) {
96                         triplet = usrc[0] | usrc[1] << 8 | usrc[2] << 16;
97                         dst[0] = base64imals[triplet & 63]; triplet >>= 6;
98                         dst[1] = base64imals[triplet & 63]; triplet >>= 6;
99                         dst[2] = base64imals[triplet & 63]; triplet >>= 6;
100                         dst[3] = base64imals[triplet];
101                         dst += 4; usrc += 3; length -= 3;
102                 } else if(length >=2) {
103                         triplet = usrc[0] | usrc[1] << 8;
104                         dst[0] = base64imals[triplet & 63]; triplet >>= 6;
105                         dst[1] = base64imals[triplet & 63]; triplet >>= 6;
106                         dst[2] = base64imals[triplet];
107                         dst[3] = 0;
108                         return origlen / 3 * 4 + 3;
109                 } else {
110                         triplet = usrc[0];
111                         dst[0] = base64imals[triplet & 63]; triplet >>= 6;
112                         dst[1] = base64imals[triplet];
113                         dst[2] = 0;
114                         return origlen / 3 * 4 + 2;
115                 }
116         }
117
118         *dst = 0;
119         return origlen / 4 * 3;
120 }
121
122 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
123 #ifdef HAVE_CYGWIN
124 #include <w32api/windows.h>
125 #endif
126
127 const char *winerror(int err) {
128         static char buf[1024], *newline;
129
130         if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
131                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof(buf), NULL)) {
132                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
133         };
134
135         if((newline = strchr(buf, '\r')))
136                 *newline = '\0';
137
138         return buf;
139 }
140 #endif
141
142 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
143         unsigned int value = 0;
144         if(size > sizeof value)
145                 size = sizeof value;
146         memcpy(&value, bitfield, size);
147         return value;
148 }