Import ezmlm-idx 0.40
[ezmlm] / encodeQ.c
1 /* $Id: encodeQ.c,v 1.2 1998/02/28 19:03:02 lindberg Exp $*/
2 /* $Name: ezmlm-idx-040 $*/
3
4 #include "errtxt.h"
5 #include "mime.h"
6 #include "stralloc.h"
7 #include "strerr.h"
8
9 static void die_nomem(fatal)
10 char *fatal;
11 {
12 strerr_die2x(111,fatal,ERR_NOMEM);
13 }
14
15 static char *hexchar = "0123456789ABCDEF";
16
17 void encodeQ(indata,n,outdata,fatal)
18 char *indata;
19 unsigned int n;
20 stralloc *outdata;
21 char *fatal;
22
23 /* converts any character with the high order bit set to */
24 /* quoted printable. In: n chars of indata, out: stralloc outdata*/
25
26 {
27 register char *cpout;
28 register char ch;
29 unsigned int i;
30 char *cpin;
31
32 cpin = indata;
33 i = 0;
34 /* max 3 outchars per inchar & 2 char newline per 72 chars */
35 if (!stralloc_copys(outdata,"")) die_nomem(fatal);
36 if (!stralloc_ready(outdata,n * 3 + n/36)) die_nomem(fatal); /* worst case */
37 cpout = outdata->s;
38 while (n--) {
39 ch = *cpin++;
40 if (ch != ' ' && ch != '\n' && ch != '\t' &&
41 (ch > 126 || ch < 33 || ch == 61)) {
42 *(cpout++) = '=';
43 *(cpout++) = hexchar[(ch >> 4) & 0xf];
44 *(cpout++) = hexchar[ch & 0xf];
45 i += 3;
46 } else {
47 if (ch == '\n')
48 i = 0;
49 *(cpout++) = ch;
50 }
51 if (i >= 72) {
52 *(cpout++) = '=';
53 *(cpout++) = '\n';
54 i = 0;
55 }
56 }
57 outdata->len = (unsigned int) (cpout - outdata->s);
58 }