1*e25c779eSMatthew Dillon /*-
2*e25c779eSMatthew Dillon * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3*e25c779eSMatthew Dillon * All rights reserved.
4*e25c779eSMatthew Dillon *
5*e25c779eSMatthew Dillon * Redistribution and use in source and binary forms, with or without
6*e25c779eSMatthew Dillon * modification, are permitted provided that the following conditions
7*e25c779eSMatthew Dillon * are met:
8*e25c779eSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
9*e25c779eSMatthew Dillon * notice, this list of conditions and the following disclaimer.
10*e25c779eSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
11*e25c779eSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
12*e25c779eSMatthew Dillon * documentation and/or other materials provided with the distribution.
13*e25c779eSMatthew Dillon *
14*e25c779eSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*e25c779eSMatthew Dillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*e25c779eSMatthew Dillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*e25c779eSMatthew Dillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*e25c779eSMatthew Dillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*e25c779eSMatthew Dillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*e25c779eSMatthew Dillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*e25c779eSMatthew Dillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*e25c779eSMatthew Dillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*e25c779eSMatthew Dillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*e25c779eSMatthew Dillon * SUCH DAMAGE.
25*e25c779eSMatthew Dillon *
26*e25c779eSMatthew Dillon */
27*e25c779eSMatthew Dillon
28*e25c779eSMatthew Dillon /*
29*e25c779eSMatthew Dillon | $Id: misc.c,v 2.1 2006/11/12 08:06:51 danny Exp $
30*e25c779eSMatthew Dillon */
31*e25c779eSMatthew Dillon
32*e25c779eSMatthew Dillon #include <sys/param.h>
33*e25c779eSMatthew Dillon #include <sys/types.h>
34*e25c779eSMatthew Dillon #include <sys/socket.h>
35*e25c779eSMatthew Dillon #include <sys/sysctl.h>
36*e25c779eSMatthew Dillon
37*e25c779eSMatthew Dillon #include <netinet/in.h>
38*e25c779eSMatthew Dillon #include <netinet/tcp.h>
39*e25c779eSMatthew Dillon #include <arpa/inet.h>
40*e25c779eSMatthew Dillon #include <stdlib.h>
41*e25c779eSMatthew Dillon #include <stdio.h>
42*e25c779eSMatthew Dillon #include <string.h>
43*e25c779eSMatthew Dillon
44*e25c779eSMatthew Dillon #include "misc.h"
45*e25c779eSMatthew Dillon
46*e25c779eSMatthew Dillon static inline char
c2b(unsigned char c)47*e25c779eSMatthew Dillon c2b(unsigned char c)
48*e25c779eSMatthew Dillon {
49*e25c779eSMatthew Dillon switch(c) {
50*e25c779eSMatthew Dillon case '0' ... '9':
51*e25c779eSMatthew Dillon return c - '0';
52*e25c779eSMatthew Dillon case 'a' ... 'f':
53*e25c779eSMatthew Dillon return c - 'a' + 10;
54*e25c779eSMatthew Dillon case 'A' ... 'F':
55*e25c779eSMatthew Dillon return c - 'A' + 10;
56*e25c779eSMatthew Dillon }
57*e25c779eSMatthew Dillon return 0;
58*e25c779eSMatthew Dillon }
59*e25c779eSMatthew Dillon
60*e25c779eSMatthew Dillon static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
61*e25c779eSMatthew Dillon "abcdefghijklmnopqrstuvwxyz"
62*e25c779eSMatthew Dillon "0123456789+/";
63*e25c779eSMatthew Dillon
64*e25c779eSMatthew Dillon static __inline unsigned char
c64tobin(unsigned char c64)65*e25c779eSMatthew Dillon c64tobin(unsigned char c64)
66*e25c779eSMatthew Dillon {
67*e25c779eSMatthew Dillon int i;
68*e25c779eSMatthew Dillon for(i = 0; i < 64; i++)
69*e25c779eSMatthew Dillon if(base64[i] == c64)
70*e25c779eSMatthew Dillon break;
71*e25c779eSMatthew Dillon return i;
72*e25c779eSMatthew Dillon }
73*e25c779eSMatthew Dillon /*
74*e25c779eSMatthew Dillon | according to rfc3720, the binary string
75*e25c779eSMatthew Dillon | cannot be larger than 1024 - but i can't find it :-) XXX
76*e25c779eSMatthew Dillon | not enforced yet.
77*e25c779eSMatthew Dillon */
78*e25c779eSMatthew Dillon int
str2bin(char * str,char ** rsp)79*e25c779eSMatthew Dillon str2bin(char *str, char **rsp)
80*e25c779eSMatthew Dillon {
81*e25c779eSMatthew Dillon char *src, *dst, *tmp;
82*e25c779eSMatthew Dillon int i, len = 0;
83*e25c779eSMatthew Dillon
84*e25c779eSMatthew Dillon src = str;
85*e25c779eSMatthew Dillon tmp = NULL;
86*e25c779eSMatthew Dillon if(strncasecmp("0x", src, 2) == 0) {
87*e25c779eSMatthew Dillon src += 2;
88*e25c779eSMatthew Dillon len = strlen(src);
89*e25c779eSMatthew Dillon
90*e25c779eSMatthew Dillon if((tmp = malloc((len+1)/2)) == NULL) {
91*e25c779eSMatthew Dillon // XXX: print some error?
92*e25c779eSMatthew Dillon return 0;
93*e25c779eSMatthew Dillon }
94*e25c779eSMatthew Dillon dst = tmp;
95*e25c779eSMatthew Dillon if(len & 1)
96*e25c779eSMatthew Dillon *dst++ = c2b(*src++);
97*e25c779eSMatthew Dillon while(*src) {
98*e25c779eSMatthew Dillon *dst = c2b(*src++) << 4;
99*e25c779eSMatthew Dillon *dst++ |= c2b(*src++);
100*e25c779eSMatthew Dillon }
101*e25c779eSMatthew Dillon len = dst - tmp;
102*e25c779eSMatthew Dillon } else
103*e25c779eSMatthew Dillon if(strncasecmp("0b", src , 2) == 0) {
104*e25c779eSMatthew Dillon // base64
105*e25c779eSMatthew Dillon unsigned char b6;
106*e25c779eSMatthew Dillon
107*e25c779eSMatthew Dillon src += 2;
108*e25c779eSMatthew Dillon len = strlen(src) / 4 * 3;
109*e25c779eSMatthew Dillon if((tmp = malloc(len)) == NULL) {
110*e25c779eSMatthew Dillon // XXX: print some error?
111*e25c779eSMatthew Dillon return 0;
112*e25c779eSMatthew Dillon }
113*e25c779eSMatthew Dillon dst = tmp;
114*e25c779eSMatthew Dillon i = 0;
115*e25c779eSMatthew Dillon while(*src && ((b6 = c64tobin(*src++)) != 64)) {
116*e25c779eSMatthew Dillon switch(i % 4) {
117*e25c779eSMatthew Dillon case 0:
118*e25c779eSMatthew Dillon *dst = b6 << 2;
119*e25c779eSMatthew Dillon break;
120*e25c779eSMatthew Dillon case 1:
121*e25c779eSMatthew Dillon *dst++ |= b6 >> 4;
122*e25c779eSMatthew Dillon *dst = b6 << 4;
123*e25c779eSMatthew Dillon break;
124*e25c779eSMatthew Dillon case 2:
125*e25c779eSMatthew Dillon *dst++ |= b6 >> 2;
126*e25c779eSMatthew Dillon *dst = b6 << 6;
127*e25c779eSMatthew Dillon break;
128*e25c779eSMatthew Dillon case 3:
129*e25c779eSMatthew Dillon *dst++ |= b6;
130*e25c779eSMatthew Dillon break;
131*e25c779eSMatthew Dillon }
132*e25c779eSMatthew Dillon i++;
133*e25c779eSMatthew Dillon }
134*e25c779eSMatthew Dillon len = dst - tmp;
135*e25c779eSMatthew Dillon }
136*e25c779eSMatthew Dillon else {
137*e25c779eSMatthew Dillon /*
138*e25c779eSMatthew Dillon | assume it to be an ascii string, so just copy it
139*e25c779eSMatthew Dillon */
140*e25c779eSMatthew Dillon len = strlen(str);
141*e25c779eSMatthew Dillon if((tmp = malloc(len)) == NULL)
142*e25c779eSMatthew Dillon return 0;
143*e25c779eSMatthew Dillon dst = tmp;
144*e25c779eSMatthew Dillon src = str;
145*e25c779eSMatthew Dillon while(*src)
146*e25c779eSMatthew Dillon *dst++ = *src++;
147*e25c779eSMatthew Dillon }
148*e25c779eSMatthew Dillon
149*e25c779eSMatthew Dillon *rsp = tmp;
150*e25c779eSMatthew Dillon return len;
151*e25c779eSMatthew Dillon }
152*e25c779eSMatthew Dillon
153*e25c779eSMatthew Dillon char *
bin2str(char * encoding,unsigned char * md,int blen)154*e25c779eSMatthew Dillon bin2str(char *encoding, unsigned char *md, int blen)
155*e25c779eSMatthew Dillon {
156*e25c779eSMatthew Dillon int len;
157*e25c779eSMatthew Dillon char *dst, *ds;
158*e25c779eSMatthew Dillon unsigned char *cp;
159*e25c779eSMatthew Dillon
160*e25c779eSMatthew Dillon if(strncasecmp(encoding, "0x", 2) == 0) {
161*e25c779eSMatthew Dillon char ofmt[5];
162*e25c779eSMatthew Dillon
163*e25c779eSMatthew Dillon len = blen * 2;
164*e25c779eSMatthew Dillon dst = malloc(len + 3);
165*e25c779eSMatthew Dillon strcpy(dst, encoding);
166*e25c779eSMatthew Dillon ds = dst + 2;
167*e25c779eSMatthew Dillon cp = md;
168*e25c779eSMatthew Dillon sprintf(ofmt, "%%02%c", encoding[1]);
169*e25c779eSMatthew Dillon while(blen-- > 0) {
170*e25c779eSMatthew Dillon sprintf(ds, ofmt, *cp++);
171*e25c779eSMatthew Dillon ds += 2;
172*e25c779eSMatthew Dillon }
173*e25c779eSMatthew Dillon *ds = 0;
174*e25c779eSMatthew Dillon return dst;
175*e25c779eSMatthew Dillon }
176*e25c779eSMatthew Dillon if(strncasecmp(encoding, "0b", 2) == 0) {
177*e25c779eSMatthew Dillon int i, b6;
178*e25c779eSMatthew Dillon
179*e25c779eSMatthew Dillon len = (blen + 2) * 4 / 3;
180*e25c779eSMatthew Dillon dst = malloc(len + 3);
181*e25c779eSMatthew Dillon strcpy(dst, encoding);
182*e25c779eSMatthew Dillon ds = dst + 2;
183*e25c779eSMatthew Dillon cp = md;
184*e25c779eSMatthew Dillon b6 = 0; // to keep compiler happy.
185*e25c779eSMatthew Dillon for(i = 0; i < blen; i++) {
186*e25c779eSMatthew Dillon switch(i % 3) {
187*e25c779eSMatthew Dillon case 0:
188*e25c779eSMatthew Dillon *ds++ = base64[*cp >> 2];
189*e25c779eSMatthew Dillon b6 = (*cp & 0x3) << 4;
190*e25c779eSMatthew Dillon break;
191*e25c779eSMatthew Dillon case 1:
192*e25c779eSMatthew Dillon b6 += (*cp >> 4);
193*e25c779eSMatthew Dillon *ds++ = base64[b6];
194*e25c779eSMatthew Dillon b6 = (*cp & 0xf) << 2;
195*e25c779eSMatthew Dillon break;
196*e25c779eSMatthew Dillon case 2:
197*e25c779eSMatthew Dillon b6 += (*cp >> 6);
198*e25c779eSMatthew Dillon *ds++ = base64[b6];
199*e25c779eSMatthew Dillon *ds++ = base64[*cp & 0x3f];
200*e25c779eSMatthew Dillon }
201*e25c779eSMatthew Dillon cp++;
202*e25c779eSMatthew Dillon }
203*e25c779eSMatthew Dillon switch(blen % 3) {
204*e25c779eSMatthew Dillon case 0:
205*e25c779eSMatthew Dillon break;
206*e25c779eSMatthew Dillon case 1:
207*e25c779eSMatthew Dillon *ds++ = base64[b6];
208*e25c779eSMatthew Dillon *ds++ = '=';
209*e25c779eSMatthew Dillon *ds++ = '=';
210*e25c779eSMatthew Dillon break;
211*e25c779eSMatthew Dillon case 2:
212*e25c779eSMatthew Dillon *ds++ = base64[b6];
213*e25c779eSMatthew Dillon *ds++ = '=';
214*e25c779eSMatthew Dillon break;
215*e25c779eSMatthew Dillon }
216*e25c779eSMatthew Dillon
217*e25c779eSMatthew Dillon *ds = 0;
218*e25c779eSMatthew Dillon return dst;
219*e25c779eSMatthew Dillon }
220*e25c779eSMatthew Dillon
221*e25c779eSMatthew Dillon return NULL;
222*e25c779eSMatthew Dillon }
223