Import ezmlm-idx 0.40
[ezmlm] / decodeB.c
CommitLineData
f8beb284
MW
1#include "stralloc.h"
2#include "strerr.h"
3#include "uint32.h"
4#include "errtxt.h"
5
6 /* Characters and translation as per rfc2047. */
7static char char64table[128] = {
8 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
9 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
10 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
11 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
12 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
13 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
14 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
15 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
16};
17
18#define char64enc(c) (((c) & 0x80) ? -1 : char64table[(c)])
19
20static void die_nomem(fatal)
21 char *fatal;
22{
23 strerr_die2x(111,fatal,ERR_NOMEM);
24}
25
26void decodeB(cpfrom,n,outdata,fatal)
27char *cpfrom;
28unsigned int n;
29stralloc *outdata;
30char *fatal;
31/* does B decoding of the string pointed to by cpfrom up to the character */
32/* before the one pointed to by cpnext, and appends the results to mimeline*/
33{
34 uint32 hold32;
35 char holdch[4] = "???";
36 int i,j;
37 char c; /* needs to be signed */
38 char *cp, *cpnext;
39
40 cp = cpfrom;
41 cpnext = cp + n;
42 i = 0;
43 hold32 = 0L;
44 if (!stralloc_readyplus(outdata,n)) die_nomem(fatal);
45 for (;;) {
46 if (i == 4) {
47 for (j = 2; j >= 0; --j) {
48 holdch[j] = hold32 & 0xff;
49 hold32 = hold32 >> 8;
50 }
51 if (!stralloc_cats(outdata,holdch)) die_nomem(fatal);
52 if (cp >= cpnext)
53 break;
54 hold32 = 0L;
55 i = 0;
56 }
57 if (cp >= cpnext) { /* pad */
58 c = 0;
59 } else {
60 c = char64enc(*cp);
61 ++cp;
62 }
63 if (c < 0) /* ignore illegal characters */
64 continue;
65 else {
66 hold32 = (hold32 << 6) | (c & 0x7f);
67 ++i;
68 }
69 }
70}
71