Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,865 +0,0 @@
1
-/*
2
- * convert/decode to/from ascii using various bases
3
- *
4
- * Copyright (C) 2008 iptelorg GmbH
5
- *
6
- * Permission to use, copy, modify, and distribute this software for any
7
- * purpose with or without fee is hereby granted, provided that the above
8
- * copyright notice and this permission notice appear in all copies.
9
- *
10
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
- */
18
-
19
-/*!
20
- * \file
21
- * \brief Kamailio core :: convert/decode to/from ascii using various bases
22
- *
23
- * Copyright (C) 2008 iptelorg GmbH
24
- * \ingroup core
25
- *
26
- * Module: \ref core
27
- *
28
- *
29
- * Functions:
30
- *  - base16_enc(src, src_len, dst, dst_len)    : encode to standard hex
31
- *  - base16_dec(src, src_len, dst, dst_len)    : decode from standard hex
32
- *  - base16_enc_len(len)                       : length needed to encode len bytes (macro)
33
- *  - base16_max_dec_len(len)                   : length needed to decode a string of size len
34
- *
35
- *  - base64_enc(src, src_len, dst, dst_len)    : encode to base64, standard alphabet
36
- *  - base64_dec(src, src_len, dst, dst_len)    : decode from base64, standard  alphabet
37
- *  - base64_enc_len(len)                       : length needed to encode len bytes (macro)
38
- *  - base64_max_dec_len(len)                   : maximum length needed to decode len bytes (macro)
39
- *  - base64_dec_len(str, len)                  : size of the decoded str 
40
- *  - q_base64_enc(src, src_len, dst, dst_len)  : encode to special base64 alphabet (non standard)
41
- *  - q_base64_dec(src, src_len, dst, dst_len)  - decode from special non-standard base64 alphabet
42
- *
43
- *  All the above functions return the size used (in dst) on success and
44
- *   0 or a negative number (which is -1*size_needed) on error.
45
- *
46
- * There are close to no checks for validity, an unexpected char will lead
47
- * to a corrupted result, but the functions won't return error.
48
- *
49
- * Notes:
50
- *  on a core2 duo the versions with lookup tables are way faster (see
51
- *  http://www.experts-exchange.com/Programming/Languages/CPP/Q_21988706.html
52
- *  for some interesting tests and ideeas).
53
- *
54
- *  Test results for 40 bytes  (typical ser nounce) in average cpu cycles:
55
-\verbatim
56
- *                    lookup   lookup_large lookup8k no-lookup
57
- *  base16_enc           211/231  218/199      -       1331
58
- *  base16_dec           252/251  236          -       1226
59
- *  base64_enc           209      186         156      1005
60
- *  base64_dec           208      207         207      1242
61
- *  q_base64_enc         -                              288
62
- *  q_base64_dec         -                              281
63
- *  (see test/basex.txt for more results)
64
-\endverbatim
65
- *
66
- * Defines:
67
- *  - BASE64_LOOKUP_TABLE/NO_BASE64_LOOKUP_TABLE : use (default)/don't use
68
- *     small lookup tables for conversions (faster in general).
69
- *  - BASE64_LOOKUP_LARGE    : use large lookup tables (2560 bytes for 
70
- *    encoding and 256 bytes for decoding; without it 64 bytes are used for
71
- *    encoding and 85 bytes for decoding.
72
- *  - BASE64_LOOKUP_8K : use even larger lookup tables (8K for encoding and
73
- *    256 for decoding); also try to write 2 bytes at a time (short) if
74
- *    the destination is 2 byte aligned
75
- *
76
- *  - BASE16_LOOKUP_TABLE/NO_BASE16_LOOKUP_TABLE : use (default)/don't use
77
- *     small lookup tables for conversions (faster in general).
78
- *  - BASE16_LOOKUP_LARGE  : use large lookup tables (512 bytes for 
79
- *    encoding and 256 bytes for decoding
80
- *  - BASE16_READ_WHOLE_INTS : read an int at a time
81
- *
82
- * History:
83
- * --------
84
- *  2008-06-11  created by andrei
85
- */
86
- 
87
-
88
-
89
-#ifndef _basex_h
90
-#define _basex_h
91
-
92
-#include "compiler_opt.h"
93
-
94
-/* defaults */
95
-#ifndef NO_BASE16_LOOKUP_TABLE
96
-#define BASE16_LOOKUP_TABLE
97
-#endif
98
-
99
-#ifndef NO_BASE64_LOOKUP_TABLE
100
-#define BASE64_LOOKUP_TABLE
101
-#endif
102
-
103
-#ifndef NO_BASE64_LOOKUP_8K
104
-#define BASE64_LOOKUP_8K
105
-#endif
106
-
107
-#ifndef NO_BASE16_LOOKUP_LARGE
108
-#define BASE16_LOOKUP_LARGE
109
-#endif
110
-
111
-#if !defined NO_BASE64_LOOKUP_LARGE && !defined BASE64_LOOKUP_8K
112
-#define BASE64_LOOKUP_LARGE
113
-#endif
114
-
115
-
116
-
117
-#if defined BASE16_READ_WHOLE_INTS || defined BASE64_READ_WHOLE_INTS || \
118
-	defined BASE64_LOOKUP_8K
119
-#include "endianness.h"
120
-
121
-/*! \brief aligns p to a type* pointer, type must have a 2^k size */
122
-#define ALIGN_POINTER(p, type) \
123
-	((type*) ((long)((char*)(p)+sizeof(type)-1)&~(long)(sizeof(type)-1)))
124
-
125
-#define ALIGN_UINT_POINTER(p) ALIGN_POINTER(p, unsigned int)
126
-
127
-#endif
128
-
129
-
130
-#ifdef BASE16_LOOKUP_TABLE
131
-
132
-#ifdef BASE16_LOOKUP_LARGE
133
-/*! \brief use large tables: 512 for lookup and 256 for decode */
134
-
135
-extern unsigned char _bx_hexdig_hi[256];
136
-extern unsigned char _bx_hexdig_low[256];
137
-
138
-/*! \brief returns the first 4 bits of c converted to a hex digit */
139
-#define HEX_HI(h)	_bx_hexdig_hi[(unsigned char)(h)]
140
-/*! \brief returns the low 4 bits of converted to a hex digit */
141
-#define HEX_LOW(h)	_bx_hexdig_low[(unsigned char)(h)]
142
-
143
-extern unsigned char _bx_unhexdig256[256];
144
-
145
-/*! \brief  converts hex_digit to a number (0..15); it might
146
- *      \return 0xff for invalid digit (but with some compile
147
- *      option it won't check)
148
- */
149
-#define UNHEX(h)	_bx_unhexdig256[(h)]
150
-
151
-#else /* BASE16_LOOKUP_LARGE */
152
-/*! \brief use small tabes: 16 bytes for lookup and 32 for decode */
153
-
154
-extern unsigned char _bx_hexdig[16+1];
155
-
156
-#define HEX_4BITS(h) _bx_hexdig[(h)]
157
-#define HEX_HI(h)	HEX_4BITS(((unsigned char)(h))>>4)
158
-#define HEX_LOW(h)	HEX_4BITS((h)&0xf)
159
-
160
-extern unsigned char _bx_unhexdig32[32];
161
-#define UNHEX(h) _bx_unhexdig32[(((h))-'0')&0x1f]
162
-
163
-#endif /* BASE16_LOOKUP_LARGE */
164
-
165
-#else /* BASE16_LOOKUP_TABLE */
166
-/* no lookup tables */
167
-#if 0
168
-#define HEX_4BITS(h) (unsigned char)((unlikely((h)>=10))?((h)-10+'A'):(h)+'0')
169
-#define UNHEX(c) (unsigned char)((unlikely((c)>='A'))?(c)-'A'+10:(c)-'0')
170
-#else
171
-#define HEX_4BITS(hc) (unsigned char)( ((((hc)>=10)-1)&((hc)+'0')) | \
172
-									((((hc)<10)-1)&((hc)+'A')) )
173
-#define UNHEX(c) (unsigned char) ( ((((c)>'9')-1)& ((c)-'0')) | \
174
-								((((c)<='9')-1)&((c)-'A')) )
175
-#endif 
176
-
177
-#define HEX_HI(h)	HEX_4BITS(((unsigned char)(h))>>4)
178
-#define HEX_LOW(h)	HEX_4BITS((h)&0xf)
179
-
180
-#endif /* BASE16_LOOKUP_TABLE */
181
-
182
-
183
-#ifdef BASE64_LOOKUP_TABLE
184
-#ifdef BASE64_LOOKUP_LARGE
185
-/* large lookup tables, 2.5 k */
186
-
187
-extern unsigned char _bx_b64_first[256];
188
-extern unsigned char _bx_b64_second[4][256];
189
-extern unsigned char _bx_b64_third[4][256];
190
-extern unsigned char _bx_b64_fourth[256];
191
-
192
-#define BASE64_1(a) _bx_b64_first[(a)]
193
-#define BASE64_2(a,b) _bx_b64_second[(a)&0x3][(b)]
194
-#define BASE64_3(b,c) _bx_b64_third[(c)>>6][(b)]
195
-#define BASE64_4(c) _bx_b64_fourth[(c)]
196
-
197
-extern unsigned char _bx_ub64[256];
198
-#define UNBASE64(v) _bx_ub64[(v)]
199
-
200
-#elif defined BASE64_LOOKUP_8K
201
-/* even larger encode tables: 8k */
202
-extern unsigned short _bx_b64_12[4096];
203
-
204
-/* return a word (16 bits) */
205
-#define BASE64_12(a,b)	_bx_b64_12[((a)<<4)|((b)>>4)]
206
-#define BASE64_34(b,c)	_bx_b64_12[(((b)&0xf)<<8)|(c)]
207
-#ifdef __IS_LITTLE_ENDIAN
208
-#define FIRST_8B(s)	((unsigned char)(s))
209
-#define LAST_8B(s)	((s)>>8)
210
-#elif defined __IS_BIG_ENDIAN
211
-#define FIRST_8B(s)	((s)>>8)
212
-#define LAST_8B(s)	((unsigned char)(s))
213
-#else
214
-#error neither __IS_LITTLE_ENDIAN nor __IS_BIG_ENDIAN are defined
215
-#endif
216
-
217
-
218
-extern unsigned char _bx_ub64[256];
219
-#define UNBASE64(v) _bx_ub64[(v)]
220
-
221
-#else /* BASE64_LOOKUP_LARGE */
222
-/* small lookup tables */
223
-extern unsigned char _bx_b64[64+1];
224
-
225
-#define BASE64_DIG(v)	_bx_b64[(v)]
226
-
227
-#define BASE64_1(a)		BASE64_DIG((a)>>2)
228
-#define BASE64_2(a, b)	BASE64_DIG( (((a)<<4)&0x3f) | ((b)>>4))
229
-#define BASE64_3(b, c)	BASE64_DIG( (((b)<<2)&0x3f) | ((c)>>6))
230
-#define BASE64_4(c)		BASE64_DIG((c)&0x3f)
231
-
232
-extern unsigned char _bx_ub64[0x54+1];
233
-#define UNBASE64(v) _bx_ub64[(((v)&0x7f)-0x2b)]
234
-
235
-#endif /* BASE64_LOOKUP_LARGE */
236
-
237
-
238
-#else /* BASE64_LOOKUP_TABLE */
239
-
240
-#define BASE64_DIG(v) base64_enc_char(v)
241
-#define BASE64_1(a)		BASE64_DIG((a)>>2)
242
-#define BASE64_2(a, b)	BASE64_DIG( (((a)<<4)&0x3f) | ((b)>>4))
243
-#define BASE64_3(b, c)	BASE64_DIG( (((b)<<2)&0x3f) | ((c)>>6))
244
-#define BASE64_4(c)		BASE64_DIG((c)&0x3f)
245
-
246
-#define UNBASE64(v) base64_dec_char(v)
247
-
248
-#endif /* BASE64_LOOKUP_TABLE */
249
-
250
-
251
-
252
-/*! \brief lenght needed for encoding l bytes */
253
-#define base16_enc_len(l) (l*2)
254
-/*! \brief maximum lenght needed for decoding l bytes */
255
-#define base16_max_dec_len(l) (l/2)
256
-/*! \brief actual space needed for decoding a string b of size l */
257
-#define base16_dec_len(b, l) base16_max_dec_len(l)
258
-/*! \brief minimum valid source len for decoding */
259
-#define base16_dec_min_len() 2
260
-/*! \brief minimum valid source len for encoding */
261
-#define base16_enc_min_len() 0
262
-
263
-/*! \brief space needed for encoding l bytes */
264
-#define base64_enc_len(l) (((l)+2)/3*4)
265
-/*! \brief maximum space needed for encoding l bytes */
266
-#define base64_max_dec_len(l) ((l)/4*3)
267
-/*! \brief actual space needed for decoding a string b of size l, l>=4 */
268
-#define base64_dec_len(b, l) \
269
-	(base64_max_dec_len(l)-((b)[(l)-2]=='=') -((b)[(l)-1]=='='))
270
-/*! \brief minimum valid source len for decoding */
271
-#define base64_dec_min_len() 4
272
-/*! \brief minimum valid source len for encoding */
273
-#define base64_enc_min_len() 0
274
-
275
-
276
-#ifdef BASE16_READ_WHOLE_INTS
277
-
278
-/*! 
279
- * \params: 
280
- * \return: size used from the output buffer (dst) on success,
281
- *          -size_needed on error
282
- *
283
- * WARNING: the output string is not 0-term
284
- */
285
-inline static int base16_enc(unsigned char* src, int slen, unsigned char*  dst, int dlen)
286
-{
287
-	unsigned int* p;
288
-	unsigned char* end;
289
-	int osize;
290
-	unsigned short us;
291
-	
292
-	osize=2*slen;
293
-	if (unlikely(dlen<osize))
294
-		return -osize;
295
-	end=src+slen;
296
-	p=ALIGN_UINT_POINTER(src);
297
-	if (likely((unsigned char*)p<end)){
298
-		switch((unsigned char)((unsigned char*)p-src)){
299
-			case 3:
300
-				*dst=HEX_HI(*src);
301
-				*(dst+1)=HEX_LOW(*src);
302
-				dst+=2;
303
-				src++;
304
-				/* no break */
305
-			case 2:
306
-				us=*(unsigned short*)(src);
307
-#if   defined __IS_LITTLE_ENDIAN
308
-				*(dst+0)=HEX_HI(us);
309
-				*(dst+1)=HEX_LOW(us);
310
-				*(dst+2)=HEX_HI(us>>8);
311
-				*(dst+3)=HEX_LOW(us>>8);
312
-#elif defined __IS_BIG_ENDIAN
313
-				*(dst+2)=HEX_HI(us);
314
-				*(dst+3)=HEX_LOW(us);
315
-				*(dst+0)=HEX_HI(us>>8);
316
-				*(dst+1)=HEX_LOW(us>>8);
317
-#endif
318
-				dst+=4;
319
-				/* no need to inc src */
320
-				break;
321
-			case 1:
322
-				*dst=HEX_HI(*src);
323
-				*(dst+1)=HEX_LOW(*src);
324
-				dst+=2;
325
-				/* no need to inc src */
326
-			case 0:
327
-				break;
328
-		}
329
-		for(;(unsigned char*)p<=(end-4);p++,dst+=8){
330
-#if   defined __IS_LITTLE_ENDIAN
331
-			*(dst+0)=HEX_HI(*p);
332
-			*(dst+1)=HEX_LOW(*p);
333
-			*(dst+2)=HEX_HI(((*p)>>8));
334
-			*(dst+3)=HEX_LOW(((*p)>>8));
335
-			*(dst+4)=HEX_HI(((*p)>>16));
336
-			*(dst+5)=HEX_LOW(((*p)>>16));
337
-			*(dst+6)=HEX_HI(((*p)>>24));
338
-			*(dst+7)=HEX_LOW(((*p)>>24));
339
-#elif defined __IS_BIG_ENDIAN
340
-			*(dst+6)=HEX_HI(*p);
341
-			*(dst+7)=HEX_LOW(*p);
342
-			*(dst+4)=HEX_HI(((*p)>>8));
343
-			*(dst+5)=HEX_LOW(((*p)>>8));
344
-			*(dst+2)=HEX_HI(((*p)>>16));
345
-			*(dst+3)=HEX_LOW(((*p)>>16));
346
-			*(dst+0)=HEX_HI(((*p)>>24));
347
-			*(dst+1)=HEX_LOW(((*p)>>24));
348
-#else
349
-#error neither BIG ro LITTLE endian defined
350
-#endif /* __IS_*_ENDIAN */
351
-		}
352
-		src=(unsigned char*)p;
353
-		/* src is 2-bytes aligned (short) */
354
-		switch((unsigned char)((unsigned char*)end-src)){
355
-			case 3:
356
-			case 2:
357
-				us=*(unsigned short*)(src);
358
-#if   defined __IS_LITTLE_ENDIAN
359
-				*(dst+0)=HEX_HI(us);
360
-				*(dst+1)=HEX_LOW(us);
361
-				*(dst+2)=HEX_HI(us>>8);
362
-				*(dst+3)=HEX_LOW(us>>8);
363
-#elif defined __IS_BIG_ENDIAN
364
-				*(dst+2)=HEX_HI(us);
365
-				*(dst+3)=HEX_LOW(us);
366
-				*(dst+0)=HEX_HI(us>>8);
367
-				*(dst+1)=HEX_LOW(us>>8);
368
-#endif
369
-				if ((end-src)==3){
370
-					*(dst+4)=HEX_HI(*(src+2));
371
-					*(dst+5)=HEX_LOW(*(src+2));
372
-				}
373
-				/* no need to inc anything */
374
-				break;
375
-			case 1:
376
-				*dst=HEX_HI(*src);
377
-				*(dst+1)=HEX_LOW(*src);
378
-				/* no need to inc anything */
379
-			case 0:
380
-				break;
381
-		}
382
-	}else if (unlikely((long)src&1)){
383
-		/* src is not 2-bytes (short) aligned */
384
-		switch((unsigned char)((unsigned char*)end-src)){
385
-			case 3:
386
-				*dst=HEX_HI(*src);
387
-				*(dst+1)=HEX_LOW(*src);
388
-				dst+=2;
389
-				src++;
390
-				/* no break */
391
-			case 2:
392
-				us=*(unsigned short*)(src);
393
-#if   defined __IS_LITTLE_ENDIAN
394
-				*(dst+0)=HEX_HI(us);
395
-				*(dst+1)=HEX_LOW(us);
396
-				*(dst+2)=HEX_HI(us>>8);
397
-				*(dst+3)=HEX_LOW(us>>8);
398
-#elif defined __IS_BIG_ENDIAN
399
-				*(dst+2)=HEX_HI(us);
400
-				*(dst+3)=HEX_LOW(us);
401
-				*(dst+0)=HEX_HI(us>>8);
402
-				*(dst+1)=HEX_LOW(us>>8);
403
-#endif
404
-				/* no need to inc anything */
405
-				break;
406
-			case 1:
407
-				*dst=HEX_HI(*src);
408
-				*(dst+1)=HEX_LOW(*src);
409
-				/* no need to inc anything */
410
-			case 0:
411
-				break;
412
-		}
413
-	}else{
414
-		/* src is 2-bytes aligned (short) */
415
-		switch((unsigned char)((unsigned char*)end-src)){
416
-			case 3:
417
-			case 2:
418
-				us=*(unsigned short*)(src);
419
-#if   defined __IS_LITTLE_ENDIAN
420
-				*(dst+0)=HEX_HI(us);
421
-				*(dst+1)=HEX_LOW(us);
422
-				*(dst+2)=HEX_HI(us>>8);
423
-				*(dst+3)=HEX_LOW(us>>8);
424
-#elif defined __IS_BIG_ENDIAN
425
-				*(dst+2)=HEX_HI(us);
426
-				*(dst+3)=HEX_LOW(us);
427
-				*(dst+0)=HEX_HI(us>>8);
428
-				*(dst+1)=HEX_LOW(us>>8);
429
-#endif
430
-				if ((end-src)==3){
431
-					*(dst+4)=HEX_HI(*(src+2));
432
-					*(dst+5)=HEX_LOW(*(src+2));
433
-				}
434
-				/* no need to inc anything */
435
-				break;
436
-			case 1:
437
-				*dst=HEX_HI(*src);
438
-				*(dst+1)=HEX_LOW(*src);
439
-				/* no need to inc anything */
440
-			case 0:
441
-				break;
442
-		}
443
-	}
444
-	
445
-	return osize;
446
-}
447
-
448
-
449
-
450
-#else /* BASE16_READ_WHOLE_INTS */
451
-
452
-
453
-/*!
454
- * \return : size used from the output buffer (dst) on success,
455
- *          -size_needed on error
456
- *
457
- * \note WARNING: the output string is not 0-term
458
- */
459
-inline static int base16_enc(unsigned char* src, int slen,
460
-							 unsigned char*  dst, int dlen)
461
-{
462
-	unsigned char* end;
463
-	int osize;
464
-	
465
-	osize=2*slen;
466
-	if (unlikely(dlen<osize))
467
-		return -osize;
468
-	end=src+slen;
469
-	for (;src<end; src++,dst+=2){
470
-		*dst=HEX_HI(*src);
471
-		*(dst+1)=HEX_LOW(*src);
472
-	}
473
-	return osize;
474
-}
475
-
476
-
477
-#endif /* BASE16_READ_WHOLE_INTS */
478
-
479
-inline static int base16_dec(unsigned char* src, int slen, unsigned char* dst, int dlen)
480
-{
481
-	unsigned char* end;
482
-	int osize;
483
-	
484
-	osize=slen/2;
485
-	if (unlikely(dlen<osize))
486
-		return -osize;
487
-	end=src+2*osize;
488
-	for (; src<end; src+=2, dst++)
489
-		*dst=(UNHEX(*src)<<4) | UNHEX(*(src+1));
490
-	return osize;
491
-}
492
-
493
-
494
-
495
-
496
-
497
-/*! \brief helper internal function: encodes v (6 bits value)
498
- * \return char ascii encoding on success and 0xff on error
499
- * (value out of range) */
500
-inline static unsigned char base64_enc_char(unsigned char v)
501
-{
502
-	switch(v){
503
-		case 0x3f:
504
-			return '/';
505
-		case 0x3e:
506
-			return '+';
507
-		default:
508
-			if (v<=25)
509
-				return v+'A';
510
-			else if (v<=51)
511
-				return v-26+'a';
512
-			else if (v<=61)
513
-				return v-52+'0';
514
-	}
515
-	return 0xff;
516
-}
517
-
518
-/*! \brief helper internal function: decodes a base64 "digit",
519
- * \return value on success (0-63) and 0xff on error (invalid)*/
520
-inline static unsigned base64_dec_char(unsigned char v)
521
-{
522
-	switch(v){
523
-		case '/':
524
-			return 0x3f;
525
-		case '+':
526
-			return 0x3e;
527
-		case ':':
528
-		case ';':
529
-		case '<':
530
-		case '=':
531
-		case '>':
532
-		case '?':
533
-		case '@':
534
-		case '[':
535
-		case '\\':
536
-		case ']':
537
-		case '^':
538
-		case '_':
539
-		case '`':
540
-			return 0xff;
541
-		default:
542
-			if ((v)<'0')
543
-				return 0xff;
544
-			if ((v)<='9')
545
-				return (v)-'0'+0x34;
546
-			else if ((v)<='Z')
547
-				return (v)-'A';
548
-			else if ((v) <='z')
549
-				return (v)-'a'+0x1a;
550
-	}
551
-	return 0xff;
552
-}
553
-
554
-
555
-#ifdef BASE64_LOOKUP_8K
556
-/*!
557
- * \return : size used from the output buffer (dst) on success ((slen+2)/3*4)
558
- *          -size_needed on error
559
- *
560
- * \note WARNING: the output string is not 0-term
561
- */
562
-inline static int base64_enc(unsigned char* src, int slen,
563
-							unsigned char* dst,  int dlen)
564
-{
565
-	unsigned char* end;
566
-	int osize;
567
-	
568
-	osize=(slen+2)/3*4;
569
-	if (unlikely(dlen<osize))
570
-		return -osize;
571
-	end=src+slen/3*3;
572
-	if (unlikely((long)dst%2)){
573
-		for (;src<end; src+=3,dst+=4){
574
-			dst[0]=FIRST_8B(BASE64_12(src[0], src[1]));
575
-			dst[1]=LAST_8B(BASE64_12(src[0], src[1]));
576
-			dst[2]=FIRST_8B(BASE64_34(src[1], src[2]));
577
-			dst[3]=LAST_8B(BASE64_34(src[1], src[2]));
578
-		}
579
-		switch(slen%3){
580
-			case 2:
581
-				dst[0]=FIRST_8B(BASE64_12(src[0], src[1]));
582
-				dst[1]=LAST_8B(BASE64_12(src[0], src[1]));
583
-				dst[2]=FIRST_8B(BASE64_34(src[1], 0));
584
-				dst[3]='=';
585
-				break;
586
-			case 1:
587
-				dst[0]=FIRST_8B(BASE64_12(src[0], 0));
588
-				dst[1]=LAST_8B(BASE64_12(src[0], 0));
589
-				dst[2]='=';
590
-				dst[3]='=';
591
-				break;
592
-		}
593
-	}else{
594
-		for (;src<end; src+=3,dst+=4){
595
-			*(unsigned short*)(dst+0)=_bx_b64_12[(src[0]<<4)|(src[1]>>4)];
596
-			*(unsigned short*)(dst+2)=_bx_b64_12[((src[1]&0xf)<<8)|src[2]];
597
-		}
598
-		switch(slen%3){
599
-			case 2:
600
-				*(unsigned short*)(dst+0)=_bx_b64_12[(src[0]<<4)|(src[1]>>4)];
601
-				*(unsigned short*)(dst+2)=_bx_b64_12[((src[1]&0xf)<<8)|0];
602
-				dst[3]='=';
603
-				break;
604
-			case 1:
605
-				*(unsigned short*)(dst+0)=_bx_b64_12[(src[0]<<4)|0];
606
-				dst[2]='=';
607
-				dst[3]='=';
608
-				break;
609
-		}
610
-	}
611
-	return osize;
612
-}
613
-#else /*BASE64_LOOKUP_8K*/
614
-/*! \brief Convert to base64
615
- * \return size used from the output buffer (dst) on success ((slen+2)/3*4)
616
- *          -size_needed on error
617
- * \note WARNING: the output string is not 0-term
618
- */
619
-inline static int base64_enc(unsigned char* src, int slen,
620
-							unsigned char* dst,  int dlen)
621
-{
622
-	unsigned char* end;
623
-	int osize;
624
-	
625
-	osize=(slen+2)/3*4;
626
-	if (unlikely(dlen<osize))
627
-		return -osize;
628
-	end=src+slen/3*3;
629
-	for (;src<end; src+=3,dst+=4){
630
-		dst[0]=BASE64_1(src[0]);
631
-		dst[1]=BASE64_2(src[0], src[1]);
632
-		dst[2]=BASE64_3(src[1], src[2]);
633
-		dst[3]=BASE64_4(src[2]);
634
-	}
635
-	switch(slen%3){
636
-		case 2:
637
-			dst[0]=BASE64_1(src[0]);
638
-			dst[1]=BASE64_2(src[0], src[1]);
639
-			dst[2]=BASE64_3(src[1], 0);
640
-			dst[3]='=';
641
-			break;
642
-		case 1:
643
-			dst[0]=BASE64_1(src[0]);
644
-			dst[1]=BASE64_2(src[0], 0);
645
-			dst[2]='=';
646
-			dst[3]='=';
647
-			break;
648
-	}
649
-	return osize;
650
-}
651
-#endif /*BASE64_LOOKUP_8K*/
652
-
653
-
654
-
655
-/*! \brief
656
- * \return size used from the output buffer (dst) on success (max: slen/4*3)
657
- *          -size_needed on error or 0 on bad base64 encoded string
658
- * \note WARNING: the output string is not 0-term
659
- */
660
-inline static int base64_dec(unsigned char* src, int slen,
661
-							unsigned char* dst,  int dlen)
662
-{
663
-	
664
-	unsigned char* end;
665
-	int osize;
666
-	register unsigned a, b, c, d; /* more registers used, but allows for
667
-									 paralles execution */
668
-	
669
-	if (unlikely((slen<4) || (slen%4) || 
670
-				(src[slen-2]=='=' && src[slen-1]!='=')))
671
-		return 0; /* invalid base64 enc. */
672
-	osize=(slen/4*3)-(src[slen-2]=='=')-(src[slen-1]=='=');
673
-	if (unlikely(dlen<osize))
674
-		return -osize;
675
-	end=src+slen-4;
676
-	for (;src<end; src+=4,dst+=3){
677
-#if 0
678
-		u=	(UNBASE64(src[0])<<18) | (UNBASE64(src[1])<<12) | 
679
-			(UNBASE64(src[2])<<6)  |  UNBASE64(src[3]);
680
-		dst[0]=u>>16;
681
-		dst[1]=u>>8;
682
-		dst[3]=u;
683
-#endif
684
-		a=UNBASE64(src[0]);
685
-		b=UNBASE64(src[1]);
686
-		c=UNBASE64(src[2]);
687
-		d=UNBASE64(src[3]);
688
-		dst[0]=(a<<2) | (b>>4);
689
-		dst[1]=(b<<4) | (c>>2);
690
-		dst[2]=(c<<6) | d;
691
-	}
692
-	switch(osize%3){
693
-		case 0: /* no '=' => 3 output bytes at the end */
694
-			a=UNBASE64(src[0]);
695
-			b=UNBASE64(src[1]);
696
-			c=UNBASE64(src[2]);
697
-			d=UNBASE64(src[3]);
698
-			dst[0]=(a<<2) | (b>>4);
699
-			dst[1]=(b<<4) | (c>>2);
700
-			dst[2]=(c<<6) | d;
701
-			break;
702
-		case 2: /* 1  '=' => 2 output bytes at the end */
703
-			a=UNBASE64(src[0]);
704
-			b=UNBASE64(src[1]);
705
-			c=UNBASE64(src[2]);
706
-			dst[0]=(a<<2) | (b>>4);
707
-			dst[1]=(b<<4) | (c>>2);
708
-			break;
709
-		case 1: /* 2  '=' => 1 output byte at the end */
710
-			a=UNBASE64(src[0]);
711
-			b=UNBASE64(src[1]);
712
-			dst[0]=(a<<2) | (b>>4);
713
-			break;
714
-	}
715
-	return osize;
716
-}
717
-
718
-
719
-
720
-
721
-/*! \brief
722
- * same as \ref base64_enc() but with a different alphabet, that allows simpler and
723
- *  faster enc/dec
724
- * \return size used from the output buffer (dst) on success ((slen+2)/3*4)
725
- *          -size_needed on error
726
- * \note WARNING: the alphabet includes ":;<>?@[]\`", so it might not be suited
727
- *  in all cases (e.g. encoding something in a sip uri).
728
- */
729
-inline static int q_base64_enc(unsigned char* src, int slen,
730
-							unsigned char* dst,  int dlen)
731
-{
732
-#define q_b64_base	'0'
733
-#define q_b64_pad	'z'
734
-#define Q_BASE64(v)	(unsigned char)((v)+q_b64_base)
735
-	unsigned char* end;
736
-	int osize;
737
-	
738
-	osize=(slen+2)/3*4;
739
-	if (unlikely(dlen<osize))
740
-		return -osize;
741
-	end=src+slen/3*3;
742
-	for (;src<end; src+=3,dst+=4){
743
-		dst[0]=Q_BASE64(src[0]>>2);
744
-		dst[1]=(Q_BASE64((src[0]<<4)&0x3f) | (src[1]>>4));
745
-		dst[2]=(Q_BASE64((src[1]<<2)&0x3f) | (src[2]>>6) );
746
-		dst[3]=Q_BASE64(src[2]&0x3f);
747
-	}
748
-	switch(slen%3){
749
-		case 2:
750
-			dst[0]=Q_BASE64(src[0]>>2);
751
-			dst[1]=(Q_BASE64((src[0]<<4)&0x3f) | (src[1]>>4));
752
-			dst[2]=Q_BASE64((src[1]<<2)&0x3f);
753
-			dst[3]=q_b64_pad;
754
-			break;
755
-		case 1:
756
-			dst[0]=Q_BASE64(src[0]>>2);
757
-			dst[1]=Q_BASE64((src[0]<<4)&0x3f);
758
-			dst[2]=q_b64_pad;
759
-			dst[3]=q_b64_pad;
760
-			break;
761
-	}
762
-	return osize;
763
-#undef Q_BASE64
764
-}
765
-
766
-
767
-
768
-/*! \brief
769
- * same as \ref base64_enc() but with a different alphabet, that allows simpler and
770
- *  faster enc/dec
771
- *
772
- * \return size used from the output buffer (dst) on success (max: slen/4*3)
773
- *          -size_needed on error or 0 on bad base64 encoded string
774
- * \note WARNING: the output string is not 0-term
775
- */
776
-inline static int q_base64_dec(unsigned char* src, int slen,
777
-							unsigned char* dst,  int dlen)
778
-{
779
-#define Q_UNBASE64(v) (unsigned char)((v)-q_b64_base)
780
-	
781
-	unsigned char* end;
782
-	int osize;
783
-#ifdef SINGLE_REG
784
-	register unsigned u;
785
-#else
786
-	register unsigned a, b, c, d; /* more registers used, but allows for
787
-									 paralles execution */
788
-#endif
789
-	
790
-	if (unlikely((slen<4) || (slen%4) || 
791
-				(src[slen-2]==q_b64_pad && src[slen-1]!=q_b64_pad)))
792
-		return 0; /* invalid base64 enc. */
793
-	osize=(slen/4*3)-(src[slen-2]==q_b64_pad)-(src[slen-1]==q_b64_pad);
794
-	if (unlikely(dlen<osize))
795
-		return -osize;
796
-	end=src+slen-4;
797
-	for (;src<end; src+=4,dst+=3){
798
-#ifdef SINGLE_REG
799
-		u=	(Q_UNBASE64(src[0])<<18) | (Q_UNBASE64(src[1])<<12) | 
800
-			(Q_UNBASE64(src[2])<<6)  |  Q_UNBASE64(src[3]);
801
-		dst[0]=u>>16;
802
-		dst[1]=u>>8;
803
-		dst[2]=u;
804
-#else
805
-		a=Q_UNBASE64(src[0]);
806
-		b=Q_UNBASE64(src[1]);
807
-		c=Q_UNBASE64(src[2]);
808
-		d=Q_UNBASE64(src[3]);
809
-		dst[0]=(a<<2) | (b>>4);
810
-		dst[1]=(b<<4) | (c>>2);
811
-		dst[2]=(c<<6) | d;
812
-#endif
813
-	}
814
-	switch(osize%3){
815
-		case 0: /* no '=' => 3 output bytes at the end */
816
-#ifdef SINGLE_REG
817
-			u=	(Q_UNBASE64(src[0])<<18) | (Q_UNBASE64(src[1])<<12) | 
818
-				(Q_UNBASE64(src[2])<<6)  |  Q_UNBASE64(src[3]);
819
-			dst[0]=u>>16;
820
-			dst[1]=u>>8;
821
-			dst[2]=u;
822
-#else
823
-			a=Q_UNBASE64(src[0]);
824
-			b=Q_UNBASE64(src[1]);
825
-			c=Q_UNBASE64(src[2]);
826
-			d=Q_UNBASE64(src[3]);
827
-			dst[0]=(a<<2) | (b>>4);
828
-			dst[1]=(b<<4) | (c>>2);
829
-			dst[2]=(c<<6) | d;
830
-#endif
831
-			break;
832
-		case 2: /* 1  '=' => 2 output bytes at the end */
833
-#ifdef SINGLE_REG
834
-			u=	(Q_UNBASE64(src[0])<<12) | (Q_UNBASE64(src[1])<<6) | 
835
-				(Q_UNBASE64(src[2]));
836
-			dst[0]=u>>10;
837
-			dst[1]=u>>2;
838
-#else
839
-			a=Q_UNBASE64(src[0]);
840
-			b=Q_UNBASE64(src[1]);
841
-			c=Q_UNBASE64(src[2]);
842
-			dst[0]=(a<<2) | (b>>4);
843
-			dst[1]=(b<<4) | (c>>2);
844
-#endif
845
-			break;
846
-		case 1: /* 2  '=' => 1 output byte at the end */
847
-#ifdef SINGLE_REG
848
-			dst[0]=(Q_UNBASE64(src[0])<<2) | (Q_UNBASE64(src[1])>>4); 
849
-#else
850
-			a=Q_UNBASE64(src[0]);
851
-			b=Q_UNBASE64(src[1]);
852
-			dst[0]=(a<<2) | (b>>4);
853
-#endif
854
-			break;
855
-	}
856
-	return osize;
857
-#undef q_b64_base
858
-#undef q_b64_pad
859
-}
860
-
861
-/*! \brief inits internal lookup tables */
862
-int init_basex(void);
863
-
864
-
865
-#endif /* _basex_h */
Browse code

core: Modify doxygen, remove history, remove SVN IDs, change "ser" and "sip-router" to "KAMAILIO"

Olle E. Johansson authored on 16/01/2015 16:38:46
Showing 1 changed files
... ...
@@ -18,7 +18,7 @@
18 18
 
19 19
 /*!
20 20
  * \file
21
- * \brief SIP-router core :: convert/decode to/from ascii using various bases
21
+ * \brief Kamailio core :: convert/decode to/from ascii using various bases
22 22
  *
23 23
  * Copyright (C) 2008 iptelorg GmbH
24 24
  * \ingroup core
Browse code

core : Update include files - delete IDs, update doxygen, delete history

Olle E. Johansson authored on 03/01/2015 10:55:48
Showing 1 changed files
... ...
@@ -1,6 +1,4 @@
1 1
 /*
2
- * $Id$
3
- *
4 2
  * convert/decode to/from ascii using various bases
5 3
  *
6 4
  * Copyright (C) 2008 iptelorg GmbH
... ...
@@ -22,6 +20,7 @@
22 20
  * \file
23 21
  * \brief SIP-router core :: convert/decode to/from ascii using various bases
24 22
  *
23
+ * Copyright (C) 2008 iptelorg GmbH
25 24
  * \ingroup core
26 25
  *
27 26
  * Module: \ref core
Browse code

core: Added void to declarations of functions with empty argument list

In C language, a declaration in the form int f(); is equivalent to int f(...);, thus being able to accept an indefinit number of parameters. With the -Wstrict-prototypes GCC options, these declarations are reported as "function declaration isn’t a prototype".
On some cases, this may trick the compiler into generating unoptimized code (like preparing to handle variadic argument list).
In all cases having a declaration int f() and a definition inf f(int) is missleading, even if standard compliant.
This is still Work in Progress. (maybe adding the -Wstrict-prototypes option to default is desireable)

Marius Zbihlei authored on 02/04/2012 14:19:17
Showing 1 changed files
... ...
@@ -860,7 +860,7 @@ inline static int q_base64_dec(unsigned char* src, int slen,
860 860
 }
861 861
 
862 862
 /*! \brief inits internal lookup tables */
863
-int init_basex();
863
+int init_basex(void);
864 864
 
865 865
 
866 866
 #endif /* _basex_h */
Browse code

Doxygen updates

oej authored on 25/10/2009 19:11:28
Showing 1 changed files
... ...
@@ -17,6 +17,7 @@
17 17
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF