ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
encode_b64.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "uint_t.h"
3#include "mime.h"
4#include "errtxt.h"
5#include "idx.h"
6#include "logmsg.h"
7
8#define WHO "encode_b64"
9
21
22
23static const unsigned char base64char[64] =
24 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
25
26static unsigned int pos = 0;
27static unsigned int i = 0;
28static uint32 hold32;
29static unsigned char *cpout;
30
31static void die_nomem() { logmsg(WHO,111,FATAL,ERR_NOMEM); }
32
33static void addone(unsigned char ch)
34{
35 if (!(pos++))
36 hold32 = (uint32) ch;
37 else
38 hold32 = (hold32 << 8) | ch;
39 if (pos == 3) {
40 *cpout++ = base64char[(hold32 >> 18) & 0x3f];
41 *cpout++ = base64char[(hold32 >> 12) & 0x3f];
42 *cpout++ = base64char[(hold32 >> 6) & 0x3f];
43 *cpout++ = base64char[hold32 & 0x3f];
44 if (++i == 18) {
45 *cpout++ = '\n';
46 i = 0;
47 }
48 pos = 0;
49 }
50}
51
52static void dorest(void)
53{
54 switch (pos) {
55 case 2:
56 hold32 = hold32 << 2;
57 *cpout++ = base64char[(hold32 >> 12) & 0x3f];
58 *cpout++ = base64char[(hold32 >> 06) & 0x3f];
59 *cpout++ = base64char[hold32 & 0x3f];
60 *cpout++ = '=';
61 break;
62 case 1:
63 hold32 = hold32 << 4;
64 *cpout++ = base64char[(hold32 >> 06) & 0x3f];
65 *cpout++ = base64char[hold32 & 0x3f];
66 *cpout++ = '=';
67 *cpout++ = '=';
68 break;
69 default:
70 break;
71 }
72 *cpout++ = '\n';
73}
74
75void encode_b64(const unsigned char *indata,unsigned int n,stralloc *outdata,int control)
76{
77 unsigned char ch;
78
79 if (control == 1) {
80 pos = 0;
81 i = 0;
82 }
83 if (!stralloc_copys(outdata,"")) die_nomem();
84 if (!stralloc_ready(outdata,n*8/3 + n/72 + 5)) die_nomem();
85 cpout = (unsigned char *) outdata->s;
86 while (n--) {
87 ch = *indata++;
88 if (ch == '\n')
89 addone('\r');
90 addone(ch);
91 }
92 if (control == 2)
93 dorest();
94 outdata->len = (unsigned int) (cpout - (unsigned char *) outdata->s);
95}
Error messages. If you translate these, I would urge you to keep the English version as well....
#define ERR_NOMEM
Definition errtxt.h:14
void die_nomem()
Definition getconf.c:17
#define WHO
Definition author.c:1
#define outdata
Definition makehash.c:46
void encode_b64(const unsigned char *indata, unsigned int n, stralloc *outdata, int control)
Definition encode_b64.c:75
const char * logmsg(const char *dir, unsigned long num, unsigned long listno, unsigned long subs, int done)
Definition loginfo.c:32