s/qmail 4.2.29a
Next generation secure email transport
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 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,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
29 for (i = 0; i < sain->len; ++i) {
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
38 return 1;
39}
40
41int quote_need(char *s,unsigned int n)
42{
43 unsigned char uch;
44 int i;
45 if (!n) return 1;
46
47 for (i = 0; i < n; ++i) {
48 uch = s[i];
49 if (uch >= 128) return 1;
50 if (!ok[uch]) return 1;
51 }
52 if (s[0] == '.') return 1;
53 if (s[n - 1] == '.') return 1;
54
55 for (i = 0; i < n - 1; ++i)
56 if (s[i] == '.')
57 if (s[i + 1] == '.') return 1;
58
59 return 0;
60}
61
62int quote(stralloc *saout,stralloc *sain)
63{
64 if (quote_need(sain->s,sain->len)) return doit(saout,sain);
65 return stralloc_copy(saout,sain);
66}
67
68static stralloc foo = {0};
69
70int quote2(stralloc *sa,char *s)
71{
72 int j;
73 if (!*s) return stralloc_copys(sa,s);
74 j = str_rchr(s,'@');
75 if (!stralloc_copys(&foo,s)) return 0;
76 if (!s[j]) return quote(sa,&foo);
77 foo.len = j;
78 if (!quote(sa,&foo)) return 0;
79
80 return stralloc_cats(sa,s + j);
81}
int stralloc_copys(stralloc *, char const *)
stralloc sa
Definition: dnscname.c:11
void doit()
Definition: newaliases.c:256
stralloc foo
Definition: qmail-local.c:74
int j
Definition: qmail-send.c:920
int quote_need(char *s, unsigned int n)
Definition: quote.c:41
int quote(stralloc *saout, stralloc *sain)
Definition: quote.c:62
int quote2(stralloc *sa, char *s)
Definition: quote.c:70