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