Doxygen Source Code Documentation
thd_base64.c File Reference
#include "stdlib.h"
Go to the source code of this file.
Defines | |
#define | B64_goodchar(c) (dtable[c] != 0x80) |
#define | B64_EOL1 '\r' |
#define | B64_EOL2 '\n' |
#define | B64_encode3(a, b, c, w, x, y, z) |
#define | B64_encode2(a, b, w, x, y, z) ( B64_encode3(a,b,0,w,x,y,z) , z = '=' ) |
#define | B64_encode1(a, w, x, y, z) ( B64_encode3(a,0,0,w,x,y,z) , y=z = '=' ) |
#define | B64_decode4(w, x, y, z, a, b, c) |
#define | B64_decode_count(w, x, y, z) |
Typedefs | |
typedef unsigned char | byte |
Functions | |
void | B64_set_crlf (int nn) |
void | B64_set_linelen (int ll) |
void | load_encode_table (void) |
void | load_decode_table (void) |
void | B64_to_binary (int nb64, byte *b64, int *nbin, byte **bin) |
void | B64_to_base64 (int nbin, byte *bin, int *nb64, byte **b64) |
Variables | |
int | dtable_mode = -1 |
byte | dtable [256] |
int | linelen = 72 |
int | ncrlf = 1 |
Define Documentation
|
Value: ( a = (dtable[w] << 2) | (dtable[x] >> 4) , \ b = (dtable[x] << 4) | (dtable[y] >> 2) , \ c = (dtable[y] << 6) | dtable[z] ) Definition at line 79 of file thd_base64.c. |
|
Value: ( ((w)=='='||(x)=='=') ? 0 \ : ((y)=='=') ? 1 \ : ((z)=='=') ? 2 : 3 ) Definition at line 84 of file thd_base64.c. |
|
Definition at line 76 of file thd_base64.c. |
|
Definition at line 73 of file thd_base64.c. |
|
Value: ( w = dtable[(a)>>2] , \ x = dtable[((a & 3) << 4) | (b >> 4)] , \ y = dtable[((b & 0xF) << 2) | (c >> 6)] , \ z = dtable[c & 0x3F] ) Definition at line 67 of file thd_base64.c. |
|
Definition at line 18 of file thd_base64.c. Referenced by B64_to_base64(). |
|
Definition at line 19 of file thd_base64.c. Referenced by B64_to_base64(). |
|
Definition at line 16 of file thd_base64.c. |
Typedef Documentation
|
Definition at line 9 of file thd_base64.c. Referenced by B64_to_base64(), and B64_to_binary(). |
Function Documentation
|
Set the number of characters to use for end of line: 1 = Unix standard (LF only); 2 = DOS standard (CR LF). ------------------------------------------------------------------------ Definition at line 23 of file thd_base64.c. References ncrlf. Referenced by main().
00024 { 00025 if( nn >= 1 && nn <= 2 ) ncrlf = nn ; 00026 return ; 00027 } |
|
Set the length of a line of output in base64; ll should be between 16 and 76 (inclusive). Will round down to a multiple of 4. ------------------------------------------------------------------------ Definition at line 31 of file thd_base64.c. References linelen. Referenced by main().
|
|
Convert binary array to base64 encoding. Inputs: nbin = number of bytes in bin bin = array of binary bytes to encode Outputs: *nb64 = number of base64 bytes [*nb64==0 flags an error] b64 = pointer to newly malloc()-ed space with bytes The output array (*b64) line length can be set by B64_set_linelen(n) where n is from 16 to 76. The default is 72. Note, however, that encoded bytes will always be written out in groups of 4. The output array line separator can be the LF character only (Unix) or the CR-LF combination (DOS, etc.). This is controlled by B64_set_crlf(n) where n=1 for LF, n=2 for CR LF. The default is LF. The output array will be terminated with a line separator. If you call B64_set_crlf(0) then this will toggle the use of line separators. There will be no ASCII NUL character at the end of *b64 -- that is, the output is not a C string. Example:
Definition at line 168 of file thd_base64.c. References a, B64_encode1, B64_encode2, B64_encode3, B64_EOL1, B64_EOL2, byte, c, linelen, load_encode_table(), malloc, ncrlf, and realloc.
00169 { 00170 int ii,jj , nn,n3 ; 00171 byte a,b,c , w,x,y,z ; 00172 00173 /*- sanity checks -*/ 00174 00175 if( nb64 == NULL || b64 == NULL ) return ; 00176 if( nbin <= 0 || bin == NULL ){ *nb64 = 0 ; *b64 = NULL ; return ; } 00177 00178 nn = (4.0*(linelen+ncrlf+1.0)/(3.0*linelen))*nbin + 256 ; 00179 *b64 = (byte *) malloc(sizeof(byte)*nn) ; 00180 if( *b64 == NULL ){ *nb64 = 0 ; return ; } 00181 00182 /*- do blocks of 3 -*/ 00183 00184 load_encode_table() ; 00185 n3 = (nbin/3)*3 ; 00186 for( nn=jj=ii=0 ; ii < n3 ; ){ 00187 a = bin[ii++] ; b = bin[ii++] ; c = bin[ii++] ; 00188 B64_encode3(a,b,c,w,x,y,z) ; 00189 (*b64)[jj++] = w ; 00190 (*b64)[jj++] = x ; 00191 (*b64)[jj++] = y ; 00192 (*b64)[jj++] = z ; 00193 nn += 4 ; if( nn >= linelen ){ 00194 if( ncrlf == 2 ) (*b64)[jj++] = B64_EOL1 ; 00195 (*b64)[jj++] = B64_EOL2 ; 00196 nn = 0 ; 00197 } 00198 } 00199 00200 /*- do the leftovers, if any (1 or 2 bytes) -*/ 00201 00202 if( ii < nbin ){ 00203 if( ii == nbin-2 ) 00204 B64_encode2(bin[ii],bin[ii+1],w,x,y,z) ; 00205 else 00206 B64_encode1(bin[ii],w,x,y,z) ; 00207 00208 (*b64)[jj++] = w ; 00209 (*b64)[jj++] = x ; 00210 (*b64)[jj++] = y ; 00211 (*b64)[jj++] = z ; nn += 4 ; 00212 } 00213 00214 if( nn > 0 ){ 00215 if( ncrlf == 2 ) (*b64)[jj++] = B64_EOL1 ; 00216 (*b64)[jj++] = B64_EOL2 ; 00217 } 00218 00219 *b64 = (byte *) realloc( *b64 , sizeof(byte)*jj ) ; 00220 *nb64 = jj ; 00221 return ; 00222 } |
|
Convert base64-encoded array to a binary array (decoding). Inputs:
Definition at line 100 of file thd_base64.c. References a, B64_decode4, B64_decode_count, B64_goodchar, byte, c, load_decode_table(), malloc, and realloc. Referenced by main().
00101 { 00102 int ii,jj , nn ; 00103 byte a,b,c , w,x,y,z ; 00104 00105 /*- sanity checks -*/ 00106 00107 if( nbin == NULL || bin == NULL ) return ; 00108 00109 if( nb64 < 4 || b64 == NULL ){ *nbin = 0 ; *bin = NULL ; return ; } 00110 00111 *bin = (byte *) malloc(sizeof(byte)*(2+3*nb64/4)) ; 00112 if( *bin == NULL ){ *nbin = 0 ; return ; } 00113 00114 /*- some work -*/ 00115 00116 load_decode_table() ; 00117 for( ii=jj=0 ; ii < nb64 ; ){ /* scan inputs, skipping bad characters */ 00118 w = b64[ii++] ; 00119 while( !B64_goodchar(w) && ii < nb64 ) w = b64[ii++] ; 00120 x = (ii < nb64) ? b64[ii++] : '=' ; 00121 while( !B64_goodchar(x) && ii < nb64 ) x = b64[ii++] ; 00122 y = (ii < nb64) ? b64[ii++] : '=' ; 00123 while( !B64_goodchar(y) && ii < nb64 ) y = b64[ii++] ; 00124 z = (ii < nb64) ? b64[ii++] : '=' ; 00125 while( !B64_goodchar(z) && ii < nb64 ) z = b64[ii++] ; 00126 00127 B64_decode4(w,x,y,z,a,b,c) ; 00128 00129 if( z == '=' ){ /* got to the end? */ 00130 nn = B64_decode_count(w,x,y,z) ; /* see how many to save */ 00131 if( nn > 0 ) (*bin)[jj++] = a ; 00132 if( nn > 1 ) (*bin)[jj++] = b ; 00133 break ; 00134 } 00135 00136 /* not at the end => save all 3 outputs */ 00137 00138 (*bin)[jj++] = a ; (*bin)[jj++] = b ; (*bin)[jj++] = c ; 00139 } 00140 00141 *bin = (byte *) realloc( *bin , sizeof(byte)*jj ) ; 00142 *nbin = jj ; 00143 return ; 00144 } |
|
Definition at line 55 of file thd_base64.c. References dtable, dtable_mode, and i.
00056 { 00057 int i; 00058 if( dtable_mode == 2 ) return ; 00059 for (i = 0 ; i < 255 ; i++) dtable[i] = 0x80; /* bad */ 00060 for (i = 'A'; i <= 'Z'; i++) dtable[i] = 0 + (i - 'A'); 00061 for (i = 'a'; i <= 'z'; i++) dtable[i] = 26 + (i - 'a'); 00062 for (i = '0'; i <= '9'; i++) dtable[i] = 52 + (i - '0'); 00063 dtable['+'] = 62; dtable['/'] = 63; dtable['='] = 0; dtable_mode = 2 ; 00064 return ; 00065 } |
|
Definition at line 40 of file thd_base64.c. References dtable, dtable_mode, and i.
|
Variable Documentation
|
Definition at line 12 of file thd_base64.c. Referenced by load_decode_table(), and load_encode_table(). |
|
Definition at line 11 of file thd_base64.c. Referenced by load_decode_table(), and load_encode_table(). |
|
Definition at line 13 of file thd_base64.c. Referenced by B64_set_linelen(), and B64_to_base64(). |
|
Definition at line 14 of file thd_base64.c. Referenced by B64_set_crlf(), and B64_to_base64(). |