Mailing list header changes (fuck you Google)
[citadel.git] / webcit / utils.c
1 /*
2  * de/encoding stuff. hopefully mostly to be depricated in favour of subst.c + strbuf
3  */
4
5 #define SHOW_ME_VAPPEND_PRINTF
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include "webcit.h"
9
10
11 /*   
12  * remove escaped strings from i.e. the url string (like %20 for blanks)
13  */
14 long unescape_input(char *buf)
15 {
16         unsigned int a, b;
17         char hex[3];
18         long buflen;
19         long len;
20
21         buflen = strlen(buf);
22
23         while ((buflen > 0) && (isspace(buf[buflen - 1]))){
24                 buf[buflen - 1] = 0;
25                 buflen --;
26         }
27
28         a = 0; 
29         while (a < buflen) {
30                 if (buf[a] == '+')
31                         buf[a] = ' ';
32                 if (buf[a] == '%') {
33                         /* don't let % chars through, rather truncate the input. */
34                         if (a + 2 > buflen) {
35                                 buf[a] = '\0';
36                                 buflen = a;
37                         }
38                         else {                  
39                                 hex[0] = buf[a + 1];
40                                 hex[1] = buf[a + 2];
41                                 hex[2] = 0;
42                                 b = 0;
43                                 b = decode_hex(hex);
44                                 buf[a] = (char) b;
45                                 len = buflen - a - 2;
46                                 if (len > 0)
47                                         memmove(&buf[a + 1], &buf[a + 3], len);
48                         
49                                 buflen -=2;
50                         }
51                 }
52                 a++;
53         }
54         return a;
55 }
56
57 /*
58  * Copy a string, escaping characters which have meaning in HTML.  
59  *
60  * target              target buffer
61  * strbuf              source buffer
62  * nbsp                        If nonzero, spaces are converted to non-breaking spaces.
63  * nolinebreaks                if set, linebreaks are removed from the string.
64  */
65 long stresc(char *target, long tSize, char *strbuf, int nbsp, int nolinebreaks)
66 {
67         char *aptr, *bptr, *eptr;
68  
69         *target = '\0';
70         aptr = strbuf;
71         bptr = target;
72         eptr = target + tSize - 6; /* our biggest unit to put in...  */
73  
74  
75         while ((bptr < eptr) && !IsEmptyStr(aptr) ){
76                 if (*aptr == '<') {
77                         memcpy(bptr, "&lt;", 4);
78                         bptr += 4;
79                 }
80                 else if (*aptr == '>') {
81                         memcpy(bptr, "&gt;", 4);
82                         bptr += 4;
83                 }
84                 else if (*aptr == '&') {
85                         memcpy(bptr, "&amp;", 5);
86                         bptr += 5;
87                 }
88                 else if (*aptr == '\"') {
89                         memcpy(bptr, "&quot;", 6);
90                         bptr += 6;
91                 }
92                 else if (*aptr == '\'') {
93                         memcpy(bptr, "&#39;", 5);
94                         bptr += 5;
95                 }
96                 else if (*aptr == LB) {
97                         *bptr = '<';
98                         bptr ++;
99                 }
100                 else if (*aptr == RB) {
101                         *bptr = '>';
102                         bptr ++;
103                 }
104                 else if (*aptr == QU) {
105                         *bptr ='"';
106                         bptr ++;
107                 }
108                 else if ((*aptr == 32) && (nbsp == 1)) {
109                         memcpy(bptr, "&nbsp;", 6);
110                         bptr += 6;
111                 }
112                 else if ((*aptr == '\n') && (nolinebreaks)) {
113                         *bptr='\0';     /* nothing */
114                 }
115                 else if ((*aptr == '\r') && (nolinebreaks)) {
116                         *bptr='\0';     /* nothing */
117                 }
118                 else{
119                         *bptr = *aptr;
120                         bptr++;
121                 }
122                 aptr ++;
123         }
124         *bptr = '\0';
125         if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
126                 return -1;
127         return (bptr - target);
128 }
129
130 /* 
131  * static wrapper for ecsputs1
132  */
133 void escputs(const char *strbuf)
134 {
135         StrEscAppend(WC->WBuf, NULL, strbuf, 0, 0);
136 }
137
138 /*
139  * urlescape buffer and print it to the client
140  */
141 void urlescputs(const char *strbuf)
142 {
143         StrBufUrlescAppend(WC->WBuf, NULL, strbuf);
144 }
145
146
147 /**
148  * urlescape buffer and print it as header 
149  */
150 void hurlescputs(const char *strbuf) 
151 {
152         StrBufUrlescAppend(WC->HBuf, NULL, strbuf);
153 }
154
155
156 /*
157  * Output a string to the client as a CDATA block
158  */
159 void cdataout(char *rawdata)
160 {
161         char *ptr = rawdata;
162         wc_printf("<![CDATA[");
163
164         while ((ptr != NULL) && (ptr[0] != 0))
165         {
166                 if (!strncmp(ptr, "]]>", 3)) {
167                         wc_printf("]]]]><![CDATA[>");
168                         ++ptr; ++ptr; ++ptr;
169                 }
170                 else {
171                         wc_printf("%c", ptr[0]);
172                         ++ptr;
173                 }
174         }
175
176         wc_printf("]]>");
177 }
178