indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / utils.c
1
2 /*
3  * de/encoding stuff. hopefully mostly to be depricated in favour of subst.c + strbuf
4  */
5
6 #define SHOW_ME_VAPPEND_PRINTF
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include "webcit.h"
10
11
12 /*   
13  * remove escaped strings from i.e. the url string (like %20 for blanks)
14  */
15 long unescape_input(char *buf) {
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         char *aptr, *bptr, *eptr;
67
68         *target = '\0';
69         aptr = strbuf;
70         bptr = target;
71         eptr = target + tSize - 6;      /* our biggest unit to put in...  */
72
73
74         while ((bptr < eptr) && !IsEmptyStr(aptr)) {
75                 if (*aptr == '<') {
76                         memcpy(bptr, "&lt;", 4);
77                         bptr += 4;
78                 }
79                 else if (*aptr == '>') {
80                         memcpy(bptr, "&gt;", 4);
81                         bptr += 4;
82                 }
83                 else if (*aptr == '&') {
84                         memcpy(bptr, "&amp;", 5);
85                         bptr += 5;
86                 }
87                 else if (*aptr == '\"') {
88                         memcpy(bptr, "&quot;", 6);
89                         bptr += 6;
90                 }
91                 else if (*aptr == '\'') {
92                         memcpy(bptr, "&#39;", 5);
93                         bptr += 5;
94                 }
95                 else if (*aptr == LB) {
96                         *bptr = '<';
97                         bptr++;
98                 }
99                 else if (*aptr == RB) {
100                         *bptr = '>';
101                         bptr++;
102                 }
103                 else if (*aptr == QU) {
104                         *bptr = '"';
105                         bptr++;
106                 }
107                 else if ((*aptr == 32) && (nbsp == 1)) {
108                         memcpy(bptr, "&nbsp;", 6);
109                         bptr += 6;
110                 }
111                 else if ((*aptr == '\n') && (nolinebreaks)) {
112                         *bptr = '\0';   /* nothing */
113                 }
114                 else if ((*aptr == '\r') && (nolinebreaks)) {
115                         *bptr = '\0';   /* nothing */
116                 }
117                 else {
118                         *bptr = *aptr;
119                         bptr++;
120                 }
121                 aptr++;
122         }
123         *bptr = '\0';
124         if ((bptr = eptr - 1) && !IsEmptyStr(aptr))
125                 return -1;
126         return (bptr - target);
127 }
128
129 /* 
130  * static wrapper for ecsputs1
131  */
132 void escputs(const char *strbuf) {
133         StrEscAppend(WC->WBuf, NULL, strbuf, 0, 0);
134 }
135
136 /*
137  * urlescape buffer and print it to the client
138  */
139 void urlescputs(const char *strbuf) {
140         StrBufUrlescAppend(WC->WBuf, NULL, strbuf);
141 }
142
143
144 /**
145  * urlescape buffer and print it as header 
146  */
147 void hurlescputs(const char *strbuf) {
148         StrBufUrlescAppend(WC->HBuf, NULL, strbuf);
149 }
150
151
152 /*
153  * Output a string to the client as a CDATA block
154  */
155 void cdataout(char *rawdata) {
156         char *ptr = rawdata;
157         wc_printf("<![CDATA[");
158
159         while ((ptr != NULL) && (ptr[0] != 0)) {
160                 if (!strncmp(ptr, "]]>", 3)) {
161                         wc_printf("]]]]><![CDATA[>");
162                         ++ptr;
163                         ++ptr;
164                         ++ptr;
165                 }
166                 else {
167                         wc_printf("%c", ptr[0]);
168                         ++ptr;
169                 }
170         }
171
172         wc_printf("]]>");
173 }