ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
quote.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "str.h"
3#include "quote.h"
4
5/*
6quote() encodes a box as per rfc 821 and rfc 822,
7while trying to do as little quoting as possible.
8no, 821 and 822 don't have the same encoding. they're not even close.
9no special encoding here for bytes above 127.
10*/
11
12static const char ok[128] = {
13 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
14,0,7,0,7,7,7,7,7,0,0,7,7,0,7,7,7 ,7,7,7,7,7,7,7,7,7,7,0,0,0,7,0,7
15,0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,0,0,0,7,7
16,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,0
17} ;
18
19static int doit(stralloc *saout,const stralloc *sain)
20{
21 char ch;
22 int i;
23 int j;
24
25 if (!stralloc_ready(saout,sain->len * 2 + 2)) return 0;
26 j = 0;
27 saout->s[j++] = '"';
28 for (i = 0;i < sain->len;++i)
29 {
30 ch = sain->s[i];
31 if ((ch == '\r') || (ch == '\n') || (ch == '"') || (ch == '\\'))
32 saout->s[j++] = '\\';
33 saout->s[j++] = ch;
34 }
35 saout->s[j++] = '"';
36 saout->len = j;
37 return 1;
38}
39
40int quote_need(const char *s,unsigned int n)
41{
42 unsigned char uch;
43 int i;
44 if (!n) return 0;
45 for (i = 0;i < n;++i)
46 {
47 uch = s[i];
48 if (uch >= 128) return 1;
49 if (!ok[uch]) return 1;
50 }
51 if (s[0] == '.') return 1;
52 if (s[n - 1] == '.') return 1;
53 for (i = 0;i < n - 1;++i) if (s[i] == '.') if (s[i + 1] == '.') return 1;
54 return 0;
55}
56
57int quote(stralloc *saout,const stralloc *sain)
58{
59 if (quote_need(sain->s,sain->len)) return doit(saout,sain);
60 return stralloc_copy(saout,sain);
61}
62
63static stralloc foo = {0};
64
65int quote2(stralloc *sa,const char *s)
66{
67 int j;
68 j = str_rchr(s,'@');
69 if (!stralloc_copys(&foo,s)) return 0;
70 if (!s[j]) return quote(sa,&foo);
71 foo.len = j;
72 if (!quote(sa,&foo)) return 0;
73 return stralloc_cats(sa,s + j);
74}
void doit(char *addr, unsigned long msgnum, unsigned long when, stralloc *bounce)
int quote2(stralloc *sa, const char *s)
Definition quote.c:65
int quote(stralloc *saout, const stralloc *sain)
Definition quote.c:57
int quote_need(const char *s, unsigned int n)
Definition quote.c:40