File: conversion.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (261 lines) | stat: -rw-r--r-- 7,215 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * $Id$
 *
 * Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
 * Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
 * 
 * The initial version of this code was written by Dragos Vingarzan
 * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
 * Fruanhofer Institute. It was and still is maintained in a separate
 * branch of the original SER. We are therefore migrating it to
 * Kamailio/SR and look forward to maintaining it from here on out.
 * 2011/2012 Smile Communications, Pty. Ltd.
 * ported/maintained/improved by 
 * Jason Penton (jason(dot)penton(at)smilecoms.com and
 * Richard Good (richard(dot)good(at)smilecoms.com) as part of an 
 * effort to add full IMS support to Kamailio/SR using a new and
 * improved architecture
 * 
 * NB: Alot of this code was originally part of OpenIMSCore,
 * FhG Fokus. 
 * Copyright (C) 2004-2006 FhG Fokus
 * Thanks for great work! This is an effort to 
 * break apart the various CSCF functions into logically separate
 * components. We hope this will drive wider use. We also feel
 * that in this way the architecture is more complete and thereby easier
 * to manage in the Kamailio/SR environment
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */
 
/**
 * \file
 * 
 * Serving-CSCF - Conversion between base16, base64 and base256
 * 
 *  \author Dragos Vingarzan vingarzan -at- fokus dot fraunhofer dot de
 * 
 */
 
/** base16 char constants */ 
char *hexchars="0123456789abcdef";
/**
 * Converts a binary encoded value to its base16 representation.
 * @param from - buffer containing the input data
 * @param len - the size of from
 * @param to - the output buffer  !!! must have at least len*2 allocated memory 
 * @returns the written length 
 */
int bin_to_base16(char *from,int len, char *to)
{
	int i,j;
	for(i=0,j=0;i<len;i++,j+=2){
		to[j] = hexchars[(((unsigned char)from[i]) >>4 )&0x0F];
		to[j+1] = hexchars[(((unsigned char)from[i]))&0x0F];
	}	
	return 2*len;
}

/** from base16 char to int */
#define HEX_DIGIT(x) \
	((x>='0'&&x<='9')?x-'0':((x>='a'&&x<='f')?x-'a'+10:((x>='A'&&x<='F')?x-'A'+10:0)))
/**
 * Converts a hex encoded value to its binary value
 * @param from - buffer containing the input data
 * @param len - the size of from
 * @param to - the output buffer  !!! must have at least len/2 allocated memory 
 * @returns the written length 
 */
int base16_to_bin(char *from,int len, char *to)
{
	int i,j;
	for(i=0,j=0;j<len;i++,j+=2){
		to[i] = (unsigned char) ( HEX_DIGIT(from[j])<<4 | HEX_DIGIT(from[j+1]));
	}	
	return i;
}

/**
 * Convert on character from base64 encoding to integer.
 *"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 * @param x - characted to convert
 * @returns the int value or -1 if terminal character ('=')
 */ 
static inline int base64_val(char x)\
{
	switch(x){
		case '=': return -1;
		case 'A': return 0;
		case 'B': return 1;
		case 'C': return 2;
		case 'D': return 3;
		case 'E': return 4;
		case 'F': return 5;
		case 'G': return 6;
		case 'H': return 7;
		case 'I': return 8;
		case 'J': return 9;
		case 'K': return 10;
		case 'L': return 11;
		case 'M': return 12;
		case 'N': return 13;
		case 'O': return 14;
		case 'P': return 15;
		case 'Q': return 16;
		case 'R': return 17;
		case 'S': return 18;
		case 'T': return 19;
		case 'U': return 20;
		case 'V': return 21;
		case 'W': return 22;
		case 'X': return 23;
		case 'Y': return 24;
		case 'Z': return 25;
		case 'a': return 26;
		case 'b': return 27;
		case 'c': return 28;
		case 'd': return 29;
		case 'e': return 30;
		case 'f': return 31;
		case 'g': return 32;
		case 'h': return 33;
		case 'i': return 34;
		case 'j': return 35;
		case 'k': return 36;
		case 'l': return 37;
		case 'm': return 38;
		case 'n': return 39;
		case 'o': return 40;
		case 'p': return 41;
		case 'q': return 42;
		case 'r': return 43;
		case 's': return 44;
		case 't': return 45;
		case 'u': return 46;
		case 'v': return 47;
		case 'w': return 48;
		case 'x': return 49;
		case 'y': return 50;
		case 'z': return 51;
		case '0': return 52;
		case '1': return 53;
		case '2': return 54;
		case '3': return 55;
		case '4': return 56;
		case '5': return 57;
		case '6': return 58;
		case '7': return 59;
		case '8': return 60;
		case '9': return 61;
		case '+': return 62;
		case '/': return 63;
	}
	return 0;
}

/**
 * Convert a string encoded in base64 to binary value.
 * @param from - buffer containing the input data
 * @param from_len - the size of from
 * @param to - the output buffer  !!! must have at least len*2 allocated memory 
 * @returns the written length 
 */
int base64_to_bin(char *from,int from_len, char *to)
{
	int i,j,x1,x2,x3,x4;

	for(i=0,j=0;i<from_len;i+=4){
		x1=base64_val(from[i]);
		x2=base64_val(from[i+1]);
		x3=base64_val(from[i+2]);
		x4=base64_val(from[i+3]);
		to[j++]=(x1<<2) | ((x2 & 0x30)>>4);
		if (x3==-1) break;
		to[j++]=((x2 & 0x0F)<<4) | ((x3 & 0x3C)>>2);
		if (x4==-1) break;
		to[j++]=((x3 & 0x03)<<6) | (x4 & 0x3F);
	}
	return j;
}

/** base64 characters constant */
char base64[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
 * Convert a binary string to base64 encoding.
 * @param src - the source buffer
 * @param src_len - length of the source buffer
 * @param ptr - the destination buffer - must be allocated to at least src_len/3*4+4 
 * @returns the length of the resulted buffer 
 */
int bin_to_base64(char *src,int src_len,char* ptr)
{
	int i,k;
	int triplets,rest;
	char *s;
	s=ptr;

	triplets = src_len/3;
	rest = src_len%3;
	for(i=0;i<triplets*3;i+=3){
		k = (((unsigned char) src[i])&0xFC)>>2;
		*ptr=base64[k];ptr++;

		k = (((unsigned char) src[i])&0x03)<<4;
		k |=(((unsigned char) src[i+1])&0xF0)>>4;
		*ptr=base64[k];ptr++;

		k = (((unsigned char) src[i+1])&0x0F)<<2;
		k |=(((unsigned char) src[i+2])&0xC0)>>6;
		*ptr=base64[k];ptr++;

		k = (((unsigned char) src[i+2])&0x3F);
		*ptr=base64[k];ptr++;
	}
	i=triplets*3;
	switch(rest){
		case 0:
			break;
		case 1:
			k = (((unsigned char) src[i])&0xFC)>>2;
			*ptr=base64[k];ptr++;

			k = (((unsigned char) src[i])&0x03)<<4;
			*ptr=base64[k];ptr++;

			*ptr='=';ptr++;

			*ptr='=';ptr++;
			break;
		case 2:
			k = (((unsigned char) src[i])&0xFC)>>2;
			*ptr=base64[k];ptr++;

			k = (((unsigned char) src[i])&0x03)<<4;
			k |=(((unsigned char) src[i+1])&0xF0)>>4;
			*ptr=base64[k];ptr++;

			k = (((unsigned char) src[i+1])&0x0F)<<2;
			*ptr=base64[k];ptr++;

			*ptr='=';ptr++;
			break;
	}

	return ptr-s;
}