Changeset 4587:92d0a6279c00 in roaraudio


Ignore:
Timestamp:
11/06/10 14:32:43 (13 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

some basic stuff to implement base64 encoding

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • include/libroar/base64.h

    r4586 r4587  
    3939#include "libroar.h" 
    4040 
     41#define ROAR_BASE64_FLAG_NONE           0x0000 
     42#define ROAR_BASE64_FLAG_EOF            0x0001 
     43#define ROAR_BASE64_FLAG_OPENPGP        0x0002 
     44#define ROAR_BASE64_FLAG_CRC_OK         0x0004 
     45 
     46struct roar_base64 { 
     47 int flags; 
     48 unsigned char iobuf[3]; 
     49 int buflen; 
     50 int reg, reglen; 
     51}; 
     52 
     53int roar_base64_init(struct roar_base64 * state, int flags); 
     54 
     55#define roar_base64_init_encode(state,flags) roar_base64_init((state),(flags)) 
     56#define roar_base64_init_decode(state,flags) roar_base64_init((state),(flags)) 
     57 
     58ssize_t roar_base64_encode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off, int eof); 
     59 
     60ssize_t roar_base64_decode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off); 
     61 
     62int roar_base64_is_eof(struct roar_base64 * state); 
     63 
     64int roar_base64_uninit(struct roar_base64 * state); 
     65 
    4166#endif 
    4267 
  • libroar/base64.c

    r4586 r4587  
    3636#include "libroar.h" 
    3737 
     38static char _base64_tab_fw[] = 
     39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
     40 "abcdefghijklmnopqrstuvwxyz" 
     41 "0123456789+/"; 
     42 
     43static char _base64_tab_bw[] = { 
     44  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
     45  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
     46  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
     47  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, 
     48  0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 
     49  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 
     50  0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 
     51  0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 
     52  0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 
     53  0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 
     54  0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 
     55}; 
     56 
     57 
     58int roar_base64_init(struct roar_base64 * state, int flags) { 
     59 if ( flags == -1 ) 
     60  flags = 0; 
     61 
     62 if ( state == NULL ) 
     63  return -1; 
     64 
     65 memset(state, 0, sizeof(struct roar_base64)); 
     66 
     67 state->flags = flags; 
     68 
     69 return 0; 
     70} 
     71 
     72static inline void _libroar_base64_encode_block(unsigned char out[4], const unsigned char in[3]) { 
     73 out[0] = _base64_tab_fw[(in[0] >> 2) & 0x3F]; 
     74 out[1] = _base64_tab_fw[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3F]; 
     75 out[2] = _base64_tab_fw[(((in[1] & 0x0F) << 2) | ((in[2] >> 6) & 0x03)) & 0x3F]; 
     76 out[3] = _base64_tab_fw[in[2] & 0x3F]; 
     77} 
     78 
     79#define _setoff(x) do { if ( off != NULL ) { *off = (x) } } while (0) 
     80 
     81ssize_t roar_base64_encode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off, int eof) { 
     82 struct roar_base64 state_store; 
     83 size_t offset = 0; 
     84 size_t tlen; 
     85 
     86 if ( inlen && in == NULL ) 
     87  return -1; 
     88 
     89 if ( outlen && out == NULL ) 
     90  return -1; 
     91 
     92 // this test should be removed as soon as those modes are supported. 
     93 if ( in == NULL || out == NULL ) 
     94  return -1; 
     95 
     96 if ( state == NULL ) { 
     97  state = &state_store; 
     98  roar_base64_init_encode(state); 
     99  eof = 1; 
     100 } 
     101 
     102 if ( state->buflen ) { 
     103  if ( (state->buflen + inlen) >= 3 ) { 
     104   tlen = 3 - state->buflen; 
     105   memcpy(state->buf + state->buflen, in, tlen); 
     106   offset += tlen; 
     107   in += tlen; 
     108   inlen -= tlen; 
     109  } else { 
     110   memcpy(state->buf + state->buflen, in, inlen); 
     111   _setoff(inlen); 
     112   return 0; 
     113  } 
     114 } 
     115} 
     116 
     117ssize_t roar_base64_decode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off); 
     118 
     119int roar_base64_is_eof(struct roar_base64 * state) { 
     120 if ( state == NULL ) 
     121  return -1; 
     122 
     123 return !!(state->flags & ROAR_BASE64_FLAG_EOF); 
     124} 
     125 
     126int roar_base64_uninit(struct roar_base64 * state) { 
     127 if ( state == NULL ) 
     128  return -1; 
     129 
     130 if ( state->buflen ) { 
     131  ROAR_WARN("roar_base64_uninit(state=%p): State has bytes left in IO buffer. This is a application error."); 
     132 } 
     133 
     134 if ( state->reglen ) { 
     135  ROAR_WARN("roar_base64_uninit(state=%p): State has bits left in decode register. This is a application error."); 
     136 } 
     137 
     138 return 0; 
     139} 
     140 
    38141//ll 
Note: See TracChangeset for help on using the changeset viewer.