base64.c File Reference

#include "stdinc.h"
#include "server.h"

Go to the source code of this file.

Defines

#define NUMNICKLOG   6
#define NUMNICKBASE   (1 << NUMNICKLOG)
#define NUMNICKMASK   (NUMNICKBASE - 1)

Functions

static char * int_to_base64 (long)
static long base64_to_int (char *)
char * base64enc (long i)
long base64dec (char *b64)
int b64_encode (char *src, size_t srclength, char *target, size_t targsize)
int b64_decode (char *src, char *target, size_t targsize)
char * encode_ip (unsigned char *ip)
int decode_ip (char *buf)
long base64dects (char *ts)
unsigned long int base64toint (const char *s, int count)
const char * inttobase64 (char *buf, unsigned int v, unsigned int count)

Variables

static const char convert2y [256]
static const unsigned char convert2n [256]
static const char Base64 []
static const char Pad64 = '='
char int6_to_base64_map []
char base64_to_int6_map []

Define Documentation

#define NUMNICKBASE   (1 << NUMNICKLOG)

Definition at line 38 of file base64.c.

#define NUMNICKLOG   6

Definition at line 37 of file base64.c.

Referenced by base64toint(), and inttobase64().

#define NUMNICKMASK   (NUMNICKBASE - 1)

Definition at line 39 of file base64.c.

Referenced by inttobase64().


Function Documentation

int b64_decode ( char *  src,
char *  target,
size_t  targsize 
)

This function skips all whitespace anywhere. converts characters, four at a time, starting at (or after) src from base - 64 numbers into three 8 bit bytes in the target area.

Parameters:
src String we are attempting to decode
target Destenation of the decoded string.
targsize Size of target.
Returns:
The number of data bytes stored at the target, or -1 on error.

Definition at line 268 of file base64.c.

References Base64, and Pad64.

Referenced by decode_ip().

int b64_encode ( char *  src,
size_t  srclength,
char *  target,
size_t  targsize 
)

The following encoding technique is taken from RFC 1521 by Borenstein and Freed. It is reproduced here in a slightly edited form for convenience. This function takeas a char* and returns a base64 encoded string.

Parameters:
src A pointer to the string we are encoding.
srclength The length of the string we are to encode
target Destination of our encoded/end result
targsize Size of the end result.
Note:
Since all base64 input is an integral number of octets, only the -------------------------------------------------

(1) the final quantum of encoding input is an integral multiple of 24 bits; here, the final unit of encoded output will be an integral multiple of 4 characters with no "=" padding, (2) the final quantum of encoding input is exactly 8 bits; here, the final unit of encoded output will be two characters followed by two "=" padding characters, or (3) the final quantum of encoding input is exactly 16 bits; here, the final unit of encoded output will be three characters followed by one "=" padding character.

Returns:
the number of bytes encoded or -1 on fail.

Definition at line 201 of file base64.c.

References Base64, and Pad64.

Referenced by encode_ip().

static long base64_to_int ( char *  b64  )  [static]

Convert a base64 expression to an intiger

Parameters:
b64 the string we are converting
Returns:
return the base64 string in long form. If we dont get a good value for b64 return false.
Note:
There are no sanity checks imposed here, besides null value checking on b64, so v should always return something useable - at least that is what is assumed here.

Definition at line 491 of file base64.c.

References base64_to_int6_map.

Referenced by base64dec().

long base64dec ( char *  b64  ) 

Definition at line 72 of file base64.c.

References base64_to_int().

Referenced by base64dects().

long base64dects ( char *  ts  ) 

Definition at line 506 of file base64.c.

References base64dec(), and generictoken().

char* base64enc ( long  i  ) 

Definition at line 65 of file base64.c.

References int_to_base64().

unsigned long int base64toint ( const char *  s,
int  count 
)

Definition at line 530 of file base64.c.

References convert2n, and NUMNICKLOG.

int decode_ip ( char *  buf  ) 

Translate a standard IPv4 IP (17bits) into base64 encrypted ip

Parameters:
buf unsigned char pointer to the IP we are encoding.
Returns:
Return s_addr of the corresponding buf/ip we are decoding or false if we fail or IP is IPv6

Definition at line 426 of file base64.c.

References b64_decode().

char* encode_ip ( unsigned char *  ip  ) 

Translate a standard IP (17bits) into base64 encrypted ip

Parameters:
ip unsigned char pointer to the IP we are encoding.
Returns:
Return the end result or null if we fail for any reason.

Definition at line 396 of file base64.c.

References b64_encode(), and str_signed().

static char * int_to_base64 ( long  val  )  [static]

Convert an int to base64

Parameters:
val the string we are converting
Returns:
Always returns base64 version of val
Note:
this doesnt return null under any circumstances but asserts to make sure we are within the boundaries of acceptable parameters.

Definition at line 454 of file base64.c.

References int6_to_base64_map.

Referenced by base64enc().

const char* inttobase64 ( char *  buf,
unsigned int  v,
unsigned int  count 
)

Definition at line 540 of file base64.c.

References convert2y, NUMNICKLOG, and NUMNICKMASK.

Referenced by VerifyConf().


Variable Documentation

const char Base64[] [static]
Initial value:
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Definition at line 81 of file base64.c.

Referenced by b64_decode(), and b64_encode().

Initial value:
 {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
    -1, 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, -1, -1, -1, -1, -1,
    -1, 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, -1, 63, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
}

Definition at line 150 of file base64.c.

Referenced by base64_to_int().

const unsigned char convert2n[256] [static]
Initial value:
 {
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0,
   0, 0, 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,62, 0,63, 0, 0,
   0,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, 0, 0, 0, 0, 0
}

Definition at line 50 of file base64.c.

Referenced by base64toint().

const char convert2y[256] [static]
Initial value:
 {
  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
  'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
  'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
}

Definition at line 43 of file base64.c.

Referenced by inttobase64().

Initial value:
 {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
    'E', 'F',
    'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V',
    'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
    'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    '{', '}'
}

(From RFC1521 and draft-ietf-dnssec-secext-03.txt) The following encoding technique is taken from RFC 1521 by Borenstein and Freed. It is reproduced here in a slightly edited form for convenience.

A 65-character subset of US-ASCII is used, enabling 6 bits to be represented per printable character. (The extra 65th character, "=", is used to signify a special processing function.)

The encoding process represents 24-bit groups of input bits as output strings of 4 encoded characters. Proceeding from left to right, a 24-bit input group is formed by concatenating 3 8-bit input groups. These 24 bits are then treated as 4 concatenated 6-bit groups, each of which is translated into a single digit in the base64 alphabet.

Each 6-bit group is used as an index into an array of 64 printable characters. The character referenced by the index is placed in the output string.

Table 1: The Base64 Alphabet

Value Encoding Value Encoding Value Encoding Value Encoding 0 A 17 R 34 i 51 z 1 B 18 S 35 j 52 0 2 C 19 T 36 k 53 1 3 D 20 U 37 l 54 2 4 E 21 V 38 m 55 3 5 F 22 W 39 n 56 4 6 G 23 X 40 o 57 5 7 H 24 Y 41 p 58 6 8 I 25 Z 42 q 59 7 9 J 26 a 43 r 60 8 10 K 27 b 44 s 61 9 11 L 28 c 45 t 62 + 12 M 29 d 46 u 63 / 13 N 30 e 47 v 14 O 31 f 48 w (pad) = 15 P 32 g 49 x 16 Q 33 h 50 y

Special processing is performed if fewer than 24 bits are available at the end of the data being encoded. A full encoding quantum is always completed at the end of a quantity. When fewer than 24 input bits are available in an input group, zero bits are added (on the right) to form an integral number of 6-bit groups. Padding at the end of the data is performed using the '=' character.

':' and '#' and '&' and '+' and '@' must never be in this table. these tables must NEVER CHANGE!

Definition at line 139 of file base64.c.

Referenced by int_to_base64().

const char Pad64 = '=' [static]

Definition at line 83 of file base64.c.

Referenced by b64_decode(), and b64_encode().

Generated on Mon Jul 19 21:53:04 2010 for Omega Security Services by  doxygen 1.6.3