1 /* $Id: encodeB.c,v 1.3 1998/03/21 18:30:27 lindberg Exp $*/
2 /* $Name: ezmlm-idx-040 $*/
10 static void die_nomem(fatal)
13 strerr_die2x(111,fatal,ERR_NOMEM);
16 static unsigned char base64char[] =
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
19 static unsigned int pos = 0;
20 static unsigned int i = 0;
22 static unsigned char *cpout;
24 static void addone(ch)
30 hold32 = (hold32 << 8) | ch;
32 *cpout++ = base64char[(hold32 >> 18) & 0x3f];
33 *cpout++ = base64char[(hold32 >> 12) & 0x3f];
34 *cpout++ = base64char[(hold32 >> 6) & 0x3f];
35 *cpout++ = base64char[hold32 & 0x3f];
49 *cpout++ = base64char[(hold32 >> 12) & 0x3f];
50 *cpout++ = base64char[(hold32 >> 06) & 0x3f];
51 *cpout++ = base64char[hold32 & 0x3f];
56 *cpout++ = base64char[(hold32 >> 06) & 0x3f];
57 *cpout++ = base64char[hold32 & 0x3f];
67 void encodeB(indata,n,outdata,control,fatal)
68 unsigned char *indata;
71 int control; /* 1 = init, 2 = flush */
73 /* converts any character with the high order bit set to */
74 /* base64. In: n chars of indata, out: stralloc outdata */
75 /* as '=' is not allowed within the block, we cannot flush after */
76 /* each line, so we carry over data from call to call. The last */
77 /* call to encodeB should have control = 2 to do the flushing. */
78 /* control = 0 resets, and the routine starts out reset. */
80 register unsigned char ch;
86 if (!stralloc_copys(outdata,"")) die_nomem(fatal);
87 if (!stralloc_ready(outdata,n*8/3 + n/72 + 5)) die_nomem(fatal);
88 cpout = (unsigned char *) outdata->s;
97 outdata->len = (unsigned int) (cpout - (unsigned char *) outdata->s);