SIP-router core :: convert/decode to/from ascii using various bases.
More...
#include "compiler_opt.h"
#include "endianness.h"
Go to the source code of this file.
Defines
-
#define ALIGN_POINTER(p, type) ((type*) ((long)((char*)(p)+sizeof(type)-1)&~(long)(sizeof(type)-1)))
- aligns p to a type* pointer, type must have a 2^k size
-
#define base16_dec_len(b, l) base16_max_dec_len(l)
- actual space needed for decoding a string b of size l
-
#define base16_dec_min_len() 2
- minimum valid source len for decoding
-
#define base16_enc_len(l) (l*2)
- lenght needed for encoding l bytes
-
#define base16_enc_min_len() 0
- minimum valid source len for encoding
-
#define base16_max_dec_len(l) (l/2)
- maximum lenght needed for decoding l bytes
-
#define base64_dec_len(b, l) (base64_max_dec_len(l)-((b)[(l)-2]=='=') -((b)[(l)-1]=='='))
- actual space needed for decoding a string b of size l, l>=4
-
#define base64_dec_min_len() 4
- minimum valid source len for decoding
-
#define base64_enc_len(l) (((l)+2)/3*4)
- space needed for encoding l bytes
-
#define base64_enc_min_len() 0
- minimum valid source len for encoding
-
#define base64_max_dec_len(l) ((l)/4*3)
- maximum space needed for encoding l bytes
-
#define HEX_HI(h) _bx_hexdig_hi[(unsigned char)(h)]
- returns the first 4 bits of c converted to a hex digit
-
#define HEX_LOW(h) _bx_hexdig_low[(unsigned char)(h)]
- returns the low 4 bits of converted to a hex digit
- #define UNHEX(h) _bx_unhexdig256[(h)]
- converts hex_digit to a number (0..15); it might
Functions
- static int base16_enc (unsigned char *src, int slen, unsigned char *dst, int dlen)
- static int base64_dec (unsigned char *src, int slen, unsigned char *dst, int dlen)
- static unsigned base64_dec_char (unsigned char v)
- helper internal function: decodes a base64 "digit",
- static int base64_enc (unsigned char *src, int slen, unsigned char *dst, int dlen)
- static unsigned char base64_enc_char (unsigned char v)
- helper internal function: encodes v (6 bits value)
-
int init_basex (void)
- inits internal lookup tables
- static int q_base64_dec (unsigned char *src, int slen, unsigned char *dst, int dlen)
- same as base64_enc() but with a different alphabet, that allows simpler and faster enc/dec
- static int q_base64_enc (unsigned char *src, int slen, unsigned char *dst, int dlen)
- same as base64_enc() but with a different alphabet, that allows simpler and faster enc/dec
Variables
-
unsigned char _bx_hexdig_hi [256]
- use large tables: 512 for lookup and 256 for decode
Detailed Description
Module: SIP-router core
Functions:
- base16_enc(src, src_len, dst, dst_len) : encode to standard hex
- base16_dec(src, src_len, dst, dst_len) : decode from standard hex
- base16_enc_len(len) : length needed to encode len bytes (macro)
- base16_max_dec_len(len) : length needed to decode a string of size len
- base64_enc(src, src_len, dst, dst_len) : encode to base64, standard alphabet
- base64_dec(src, src_len, dst, dst_len) : decode from base64, standard alphabet
- base64_enc_len(len) : length needed to encode len bytes (macro)
- base64_max_dec_len(len) : maximum length needed to decode len bytes (macro)
- base64_dec_len(str, len) : size of the decoded str
- q_base64_enc(src, src_len, dst, dst_len) : encode to special base64 alphabet (non standard)
- q_base64_dec(src, src_len, dst, dst_len) - decode from special non-standard base64 alphabet
All the above functions return the size used (in dst) on success and 0 or a negative number (which is -1*size_needed) on error.
There are close to no checks for validity, an unexpected char will lead to a corrupted result, but the functions won't return error.
Notes: on a core2 duo the versions with lookup tables are way faster (see http://www.experts-exchange.com/Programming/Languages/CPP/Q_21988706.html for some interesting tests and ideeas).
Test results for 40 bytes (typical ser nounce) in average cpu cycles:
* lookup lookup_large lookup8k no-lookup
* base16_enc 211/231 218/199 - 1331
* base16_dec 252/251 236 - 1226
* base64_enc 209 186 156 1005
* base64_dec 208 207 207 1242
* q_base64_enc - 288
* q_base64_dec - 281
* (see test/basex.txt for more results)
Defines:
- BASE64_LOOKUP_TABLE/NO_BASE64_LOOKUP_TABLE : use (default)/don't use small lookup tables for conversions (faster in general).
- BASE64_LOOKUP_LARGE : use large lookup tables (2560 bytes for encoding and 256 bytes for decoding; without it 64 bytes are used for encoding and 85 bytes for decoding.
- BASE64_LOOKUP_8K : use even larger lookup tables (8K for encoding and 256 for decoding); also try to write 2 bytes at a time (short) if the destination is 2 byte aligned
- BASE16_LOOKUP_TABLE/NO_BASE16_LOOKUP_TABLE : use (default)/don't use small lookup tables for conversions (faster in general).
- BASE16_LOOKUP_LARGE : use large lookup tables (512 bytes for encoding and 256 bytes for decoding
- BASE16_READ_WHOLE_INTS : read an int at a time
History: -------- 2008-06-11 created by andrei
Definition in file basex.h.
Define Documentation
| #define UNHEX |
( |
|
h |
) |
_bx_unhexdig256[(h)] |
- Returns:
- 0xff for invalid digit (but with some compile option it won't check)
Definition at line 150 of file basex.h.
Function Documentation
| static int base16_enc |
( |
unsigned char * |
src, |
|
|
int |
slen, |
|
|
unsigned char * |
dst, |
|
|
int |
dlen | |
|
) |
| | [inline, static] |
- Returns:
- : size used from the output buffer (dst) on success, -size_needed on error
- Note:
- WARNING: the output string is not 0-term
Definition at line 460 of file basex.h.
References HEX_HI, and HEX_LOW.
| static int base64_dec |
( |
unsigned char * |
src, |
|
|
int |
slen, |
|
|
unsigned char * |
dst, |
|
|
int |
dlen | |
|
) |
| | [inline, static] |
- Returns:
- size used from the output buffer (dst) on success (max: slen/4*3) -size_needed on error or 0 on bad base64 encoded string
- Note:
- WARNING: the output string is not 0-term
Definition at line 661 of file basex.h.
| static unsigned base64_dec_char |
( |
unsigned char |
v |
) |
[inline, static] |
- Returns:
- value on success (0-63) and 0xff on error (invalid)
Definition at line 521 of file basex.h.
| static int base64_enc |
( |
unsigned char * |
src, |
|
|
int |
slen, |
|
|
unsigned char * |
dst, |
|
|
int |
dlen | |
|
) |
| | [inline, static] |
- Returns:
- : size used from the output buffer (dst) on success ((slen+2)/3*4) -size_needed on error
- Note:
- WARNING: the output string is not 0-term
Definition at line 563 of file basex.h.
| static unsigned char base64_enc_char |
( |
unsigned char |
v |
) |
[inline, static] |
- Returns:
- char ascii encoding on success and 0xff on error (value out of range)
Definition at line 501 of file basex.h.
| static int q_base64_dec |
( |
unsigned char * |
src, |
|
|
int |
slen, |
|
|
unsigned char * |
dst, |
|
|
int |
dlen | |
|
) |
| | [inline, static] |
- Returns:
- size used from the output buffer (dst) on success (max: slen/4*3) -size_needed on error or 0 on bad base64 encoded string
- Note:
- WARNING: the output string is not 0-term
Definition at line 777 of file basex.h.
| static int q_base64_enc |
( |
unsigned char * |
src, |
|
|
int |
slen, |
|
|
unsigned char * |
dst, |
|
|
int |
dlen | |
|
) |
| | [inline, static] |
- Returns:
- size used from the output buffer (dst) on success ((slen+2)/3*4) -size_needed on error
- Note:
- WARNING: the alphabet includes ":;<>?@[]\`", so it might not be suited in all cases (e.g. encoding something in a sip uri).
Definition at line 730 of file basex.h.