Remove $Id$ tags from most of webcit
[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 void escputs1(const char *strbuf, int nbsp, int nolinebreaks)
132 {
133         StrEscAppend(WC->WBuf, NULL, strbuf, nbsp, nolinebreaks);
134 }
135
136 void StrEscputs1(const StrBuf *strbuf, int nbsp, int nolinebreaks)
137 {
138         StrEscAppend(WC->WBuf, strbuf, NULL, nbsp, nolinebreaks);
139 }
140
141 /* 
142  * static wrapper for ecsputs1
143  */
144 void escputs(const char *strbuf)
145 {
146         escputs1(strbuf, 0, 0);
147 }
148
149
150 /* 
151  * static wrapper for ecsputs1
152  */
153 void StrEscPuts(const StrBuf *strbuf)
154 {
155         StrEscputs1(strbuf, 0, 0);
156 }
157
158
159 /*
160  * urlescape buffer and print it to the client
161  */
162 void urlescputs(const char *strbuf)
163 {
164         StrBufUrlescAppend(WC->WBuf, NULL, strbuf);
165 }
166
167 /*
168  * urlescape buffer and print it to the client
169  */
170 void UrlescPutStrBuf(const StrBuf *strbuf)
171 {
172         StrBufUrlescAppend(WC->WBuf, strbuf, NULL);
173 }
174
175 /**
176  * urlescape buffer and print it as header 
177  */
178 void hurlescputs(const char *strbuf) 
179 {
180         StrBufUrlescAppend(WC->HBuf, NULL, strbuf);
181 }
182
183
184 /*
185  * Copy a string, escaping characters for JavaScript strings.
186  */
187 void jsesc(char *target, size_t tlen, char *strbuf)
188 {
189         int len;
190         char *tend;
191         char *send;
192         char *tptr;
193         char *sptr;
194
195         target[0]='\0';
196         len = strlen (strbuf);
197         send = strbuf + len;
198         tend = target + tlen;
199         sptr = strbuf;
200         tptr = target;
201         
202         while (!IsEmptyStr(sptr) && 
203                (sptr < send) &&
204                (tptr < tend)) {
205                
206                 if (*sptr == '<')
207                         *tptr = '[';
208                 else if (*sptr == '>')
209                         *tptr = ']';
210                 else if (*sptr == '\'') {
211                         if (tend - tptr < 3)
212                                 return;
213                         *(tptr++) = '\\';
214                         *tptr = '\'';
215                 }
216                 else if (*sptr == '"') {
217                         if (tend - tptr < 8)
218                                 return;
219                         *(tptr++) = '&';
220                         *(tptr++) = 'q';
221                         *(tptr++) = 'u';
222                         *(tptr++) = 'o';
223                         *(tptr++) = 't';
224                         *tptr = ';';
225                 }
226                 else if (*sptr == '&') {
227                         if (tend - tptr < 7)
228                                 return;
229                         *(tptr++) = '&';
230                         *(tptr++) = 'a';
231                         *(tptr++) = 'm';
232                         *(tptr++) = 'p';
233                         *tptr = ';';
234                 } else {
235                         *tptr = *sptr;
236                 }
237                 tptr++; sptr++;
238         }
239         *tptr = '\0';
240 }
241
242 /*
243  * escape and print javascript
244  */
245 void jsescputs(char *strbuf)
246 {
247         char outbuf[SIZ];
248         
249         jsesc(outbuf, SIZ, strbuf);
250         wc_printf("%s", outbuf);
251 }
252
253 /*
254  * print a string to the client after cleaning it with msgesc() and stresc()
255  */
256 void msgescputs1( char *strbuf)
257 {
258         StrBuf *OutBuf;
259
260         if ((strbuf == NULL) || IsEmptyStr(strbuf))
261                 return;
262         OutBuf = NewStrBuf();
263         StrMsgEscAppend(OutBuf, NULL, strbuf);
264         StrEscAppend(WC->WBuf, OutBuf, NULL, 0, 0);
265         FreeStrBuf(&OutBuf);
266 }
267
268 /*
269  * print a string to the client after cleaning it with msgesc()
270  */
271 void msgescputs(char *strbuf) {
272         if ((strbuf != NULL) && !IsEmptyStr(strbuf))
273                 StrMsgEscAppend(WC->WBuf, NULL, strbuf);
274 }
275
276
277