File: mime_encode.c

package info (click to toggle)
youbin 3.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,652 kB
  • ctags: 1,234
  • sloc: ansic: 5,882; makefile: 584; sh: 24
file content (31 lines) | stat: -rw-r--r-- 675 bytes parent folder | download
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
26
27
28
29
30
31
static char *base64 =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static struct {
  int r_shift;
  int l_shift;
  int mask;
  } rlm[3]={{2,4,0x30}, {4,2,0x3c}, {6,0,0x3f}};

void mime_encode( unsigned char *sptr, unsigned char *dptr, int pslen )
{ int           mod3;
  unsigned char c;

  for( *dptr = c = mod3 = 0; pslen; pslen-- ){
    *dptr++ = base64[ c + ((*sptr) >> rlm[mod3].r_shift) ];
    c = (( *sptr++ ) << rlm[mod3].l_shift ) & rlm[ mod3 ].mask;
    if( mod3 == 2 ) {
      *dptr++ = base64[ c ];
      c = 0;
      mod3 = 0;
    } else {
      mod3++;
    }
  }
  if( mod3 ){
    *dptr++ = base64[ c ];
  }
  *dptr = 0;
}