Doxygen Source Code Documentation
niml_b64.c File Reference
#include "niml_private.h"Go to the source code of this file.
| 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 | 
| int | nocrlf = 0 | 
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 19 of file niml_b64.c. 
 | 
| 
 | 
| 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 niml_b64.c. References linelen. 
 | 
| 
 | ||||||||||||||||||||
| 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 177 of file niml_b64.c. References a, B64_encode1, B64_encode2, B64_encode3, c, linelen, load_encode_table(), malloc, ncrlf, nocrlf, and realloc. 
 00178 {
00179    int ii,jj , nn,n3 ;
00180    byte a,b,c , w,x,y,z ;
00181 
00182    /*- sanity checks -*/
00183 
00184    if( nb64 == NULL || b64 == NULL ) return ;
00185    if( nbin <= 0    || bin == NULL ){ *nb64 = 0 ; *b64 = NULL ; return ; }
00186 
00187    /* calculate size of output (3 bytes in -> 4 bytes out, plus EOL */
00188 
00189    nn   = (int)((4.0*(linelen+ncrlf+1.0)/(3.0*linelen))*nbin + 256.0) ;
00190    *b64 = (byte *) malloc(sizeof(byte)*nn) ;
00191    if( *b64 == NULL ){ *nb64 = 0 ; return ; }  /* this is bad */
00192 
00193    /*- do blocks of 3 bytes in -*/
00194 
00195    load_encode_table() ;
00196    n3 = (nbin/3)*3 ;
00197    for( nn=jj=ii=0 ; ii < n3 ; ){
00198 
00199       /* encode next 3 bytes to 4 outputs */
00200 
00201       a = bin[ii++] ; b = bin[ii++] ; c = bin[ii++] ;
00202       B64_encode3(a,b,c,w,x,y,z) ;
00203       (*b64)[jj++] = w ;
00204       (*b64)[jj++] = x ;
00205       (*b64)[jj++] = y ;
00206       (*b64)[jj++] = z ;
00207 
00208       /* if we past the line length, add the EOL stuff */
00209 
00210       if( !nocrlf ){
00211         nn += 4 ; if( nn >= linelen ){
00212                      if( ncrlf == 2 ) (*b64)[jj++] = B64_EOL1 ;
00213                      (*b64)[jj++] = B64_EOL2 ;
00214                      nn = 0 ;
00215                   }
00216       }
00217    }
00218 
00219    /*- do the leftover data, if any (1 or 2 bytes) -*/
00220 
00221    if( ii < nbin ){
00222       if( ii == nbin-2 )
00223          B64_encode2(bin[ii],bin[ii+1],w,x,y,z) ;
00224       else
00225          B64_encode1(bin[ii],w,x,y,z) ;
00226 
00227       (*b64)[jj++] = w ;
00228       (*b64)[jj++] = x ;
00229       (*b64)[jj++] = y ;
00230       (*b64)[jj++] = z ; nn += 4 ;
00231    }
00232 
00233    /* if any output bytes are left, add EOL */
00234 
00235    if( nn > 0 && !nocrlf ){
00236       if( ncrlf == 2 ) (*b64)[jj++] = B64_EOL1 ;
00237       (*b64)[jj++] = B64_EOL2 ;
00238    }
00239 
00240    /* resize output array to be exact fit */
00241 
00242    *b64  = (byte *) realloc( *b64 , sizeof(byte)*jj ) ;
00243    *nb64 = jj ;
00244    return ;
00245 }
 | 
| 
 | ||||||||||||||||||||
| Convert base64-encoded array to a binary array (decoding). Inputs: 
 
 
 Definition at line 92 of file niml_b64.c. References a, B64_decode4, B64_decode_count, B64_goodchar, c, load_decode_table(), malloc, and realloc. 
 00093 {
00094    int ii,jj , nn ;
00095    byte a,b,c , w,x,y,z ;
00096 
00097    /*- sanity checks -*/
00098 
00099    if( nbin == NULL || bin == NULL ) return ;
00100 
00101    if( nb64 < 4 || b64 == NULL ){ *nbin = 0 ; *bin = NULL ; return ; }
00102 
00103    *bin = (byte *) malloc(sizeof(byte)*(2+3*nb64/4)) ;
00104    if( *bin == NULL ){ *nbin = 0 ; return ; }
00105 
00106    /*- some work -*/
00107 
00108    load_decode_table() ;
00109    for( ii=jj=0 ; ii < nb64 ; ){  /* scan inputs, skipping bad characters */
00110 
00111       /* get next 4 characters (use '=' if we hit the end early) */
00112 
00113       w = b64[ii++] ;
00114       while( !B64_goodchar(w) && ii < nb64 ) w = b64[ii++] ;
00115       x = (ii < nb64) ? b64[ii++] : '=' ;
00116       while( !B64_goodchar(x) && ii < nb64 ) x = b64[ii++] ;
00117       y = (ii < nb64) ? b64[ii++] : '=' ;
00118       while( !B64_goodchar(y) && ii < nb64 ) y = b64[ii++] ;
00119       z = (ii < nb64) ? b64[ii++] : '=' ;
00120       while( !B64_goodchar(z) && ii < nb64 ) z = b64[ii++] ;
00121 
00122       B64_decode4(w,x,y,z,a,b,c) ;           /* decode 4 bytes into 3 */
00123 
00124       if( z == '=' ){                        /* got to the end? */
00125          nn = B64_decode_count(w,x,y,z) ;    /* see how many to save */
00126          if( nn > 0 ) (*bin)[jj++] = a ;
00127          if( nn > 1 ) (*bin)[jj++] = b ;
00128          break ;                             /* end of decoding loop */
00129       }
00130 
00131       /* not at the end => save all 3 outputs, loop back */
00132 
00133       (*bin)[jj++] = a ; (*bin)[jj++] = b ; (*bin)[jj++] = c ;
00134    }
00135 
00136    /* resize output array to be exact fit */
00137 
00138    *bin  = (byte *) realloc( *bin , sizeof(byte)*jj ) ;
00139    *nbin = jj ;
00140    return ;
00141 }
 | 
| 
 | 
| Load the base64 decoding table. ------------------------------------------------------------------------ Definition at line 59 of file niml_b64.c. References dtable, dtable_mode, and i. Referenced by B64_to_binary(), and NI_stream_readbuf64(). 
 00060 {
00061     int i;
00062     if( dtable_mode == 2 ) return ;
00063     for (i = 0  ; i < 255 ; i++) dtable[i] = 0x80;             /* bad */
00064     for (i = 'A'; i <= 'Z'; i++) dtable[i] =  0 + (i - 'A');
00065     for (i = 'a'; i <= 'z'; i++) dtable[i] = 26 + (i - 'a');
00066     for (i = '0'; i <= '9'; i++) dtable[i] = 52 + (i - '0');
00067     dtable['+'] = 62; dtable['/'] = 63; dtable['='] = 0; dtable_mode = 2 ;
00068     return ;
00069 }
 | 
| 
 | 
| Load the base64 encoding table. ------------------------------------------------------------------------ Definition at line 42 of file niml_b64.c. References dtable, dtable_mode, and i. Referenced by B64_to_base64(), and NI_write_columns(). 
 | 
Variable Documentation
| 
 | 
| 
 Definition at line 9 of file niml_b64.c. Referenced by load_decode_table(), and load_encode_table(). | 
| 
 | 
| [Most are not actually used in NIML, but are here for completeness] * Definition at line 8 of file niml_b64.c. Referenced by load_decode_table(), and load_encode_table(). | 
| 
 | 
| 
 Definition at line 10 of file niml_b64.c. Referenced by B64_set_linelen(), and B64_to_base64(). | 
| 
 | 
| 
 Definition at line 11 of file niml_b64.c. Referenced by B64_set_crlf(), and B64_to_base64(). | 
| 
 | 
| 
 Definition at line 12 of file niml_b64.c. Referenced by B64_set_crlf(), and B64_to_base64(). | 
 
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
 
 
 
 
       
	   
	   
	   
	  