libs/web: fix sanitize_utf8(), passes all testcases now
[project/luci.git] / libs / web / src / template_utils.c
1 /*
2  * LuCI Template - Utility functions
3  *
4  *   Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #include "template_utils.h"
20
21 /* initialize a buffer object */
22 static struct template_buffer * buf_init(void)
23 {
24         struct template_buffer *buf;
25
26         buf = (struct template_buffer *)malloc(sizeof(struct template_buffer));
27
28         if (buf != NULL)
29         {
30                 buf->fill = 0;
31                 buf->size = 1024;
32                 buf->data = (unsigned char *)malloc(buf->size);
33
34                 if (buf->data != NULL)
35                 {
36                         buf->dptr = buf->data;
37                         buf->data[0] = 0;
38
39                         return buf;
40                 }
41
42                 free(buf);
43         }
44
45         return NULL;
46 }
47
48 /* grow buffer */
49 static int buf_grow(struct template_buffer *buf)
50 {
51         unsigned int off = (buf->dptr - buf->data);
52         unsigned char *data =
53                 (unsigned char *)realloc(buf->data, buf->size + 1024);
54
55         if (data != NULL)
56         {
57                 buf->data  = data;
58                 buf->dptr  = data + off;
59                 buf->size += 1024;
60
61                 return buf->size;
62         }
63
64         return 0;
65 }
66
67 /* put one char into buffer object */
68 static int buf_putchar(struct template_buffer *buf, unsigned char c)
69 {
70         if( ((buf->fill + 1) >= buf->size) && !buf_grow(buf) )
71                 return 0;
72
73         *(buf->dptr++) = c;
74         *(buf->dptr) = 0;
75
76         buf->fill++;
77         return 1;
78 }
79
80 /* append data to buffer */
81 static int buf_append(struct template_buffer *buf, unsigned char *s, int len)
82 {
83         while ((buf->fill + len + 1) >= buf->size)
84         {
85                 if (!buf_grow(buf))
86                         return 0;
87         }
88
89         memcpy(buf->dptr, s, len);
90         buf->fill += len;
91         buf->dptr += len;
92
93         *(buf->dptr) = 0;
94
95         return len;
96 }
97
98 /* destroy buffer object and return pointer to data */
99 static char * buf_destroy(struct template_buffer *buf)
100 {
101         unsigned char *data = buf->data;
102
103         free(buf);
104         return (char *)data;
105 }
106
107
108 /* calculate the number of expected continuation chars */
109 static inline int mb_num_chars(unsigned char c)
110 {
111         if ((c & 0xE0) == 0xC0)
112                 return 2;
113         else if ((c & 0xF0) == 0xE0)
114                 return 3;
115         else if ((c & 0xF8) == 0xF0)
116                 return 4;
117         else if ((c & 0xFC) == 0xF8)
118                 return 5;
119         else if ((c & 0xFE) == 0xFC)
120                 return 6;
121
122         return 1;
123 }
124
125 /* test whether the given byte is a valid continuation char */
126 static inline int mb_is_cont(unsigned char c)
127 {
128         return ((c >= 0x80) && (c <= 0xBF));
129 }
130
131 /* test whether the byte sequence at the given pointer with the given
132  * length is the shortest possible representation of the code point */
133 static inline int mb_is_shortest(unsigned char *s, int n)
134 {
135         switch (n)
136         {
137                 case 2:
138                         /* 1100000x (10xxxxxx) */
139                         return ((*s & 0x1E) > 0);
140
141                 case 3:
142                         /* 11100000 100xxxxx (10xxxxxx) */
143                         return ((*s & 0x1F) > 0) && ((*(s+1) & 0x60) > 0);
144
145                 case 4:
146                         /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
147                         return ((*s & 0x0F) > 0) && ((*(s+1) & 0x70) > 0);
148
149                 case 5:
150                         /* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) */
151                         return ((*s & 0x07) > 0) && ((*(s+1) & 0x78) > 0);
152
153                 case 6:
154                         /* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) */
155                         return ((*s & 0x03) > 0) && ((*(s+1) & 0x7C) > 0);
156         }
157
158         return 1;
159 }
160
161 /* test whether the byte sequence at the given pointer with the given
162  * length is an UTF-16 surrogate */
163 static inline int mb_is_surrogate(unsigned char *s, int n)
164 {
165         return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF));
166 }
167
168 /* test whether the byte sequence at the given pointer with the given
169  * length is an illegal UTF-8 code point */
170 static inline int mb_is_illegal(unsigned char *s, int n)
171 {
172         return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) &&
173                         (*(s+2) >= 0xBE) && (*(s+2) <= 0xBF));
174 }
175
176
177 /* scan given source string, validate UTF-8 sequence and store result
178  * in given buffer object */
179 static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf)
180 {
181         unsigned char *ptr = *s;
182         unsigned int o = 0, v, n;
183
184         /* ascii byte without null */
185         if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F))
186         {
187                 if (!buf_putchar(buf, *ptr++))
188                         return 0;
189
190                 o = 1;
191         }
192
193         /* multi byte sequence */
194         else if ((n = mb_num_chars(*ptr)) > 1)
195         {
196                 /* count valid chars */
197                 for (v = 1; (v <= n) && ((o+v) < l) && mb_is_cont(*(ptr+v)); v++);
198
199                 switch (n)
200                 {
201                         case 6:
202                         case 5:
203                                 /* five and six byte sequences are always invalid */
204                                 if (!buf_putchar(buf, '?'))
205                                         return 0;
206
207                                 break;
208
209                         default:
210                                 /* if the number of valid continuation bytes matches the
211                                  * expected number and if the sequence is legal, copy
212                                  * the bytes to the destination buffer */
213                                 if ((v == n) && mb_is_shortest(ptr, n) &&
214                                         !mb_is_surrogate(ptr, n) && !mb_is_illegal(ptr, n))
215                                 {
216                                         /* copy sequence */
217                                         if (!buf_append(buf, ptr, n))
218                                                 return 0;
219                                 }
220
221                                 /* the found sequence is illegal, skip it */
222                                 else
223                                 {
224                                         /* invalid sequence */
225                                         if (!buf_putchar(buf, '?'))
226                                                 return 0;
227                                 }
228
229                                 break;
230                 }
231
232                 /* advance beyound the last found valid continuation char */
233                 o = v;
234                 ptr += v;
235         }
236
237         /* invalid byte (0x00) */
238         else
239         {
240                 if (!buf_putchar(buf, '?')) /* or 0xEF, 0xBF, 0xBD */
241                         return 0;
242
243                 o = 1;
244                 ptr++;
245         }
246
247         *s = ptr;
248         return o;
249 }
250
251 /* sanitize given string and replace all invalid UTF-8 sequences with "?" */
252 char * sanitize_utf8(const char *s, unsigned int l)
253 {
254         struct template_buffer *buf = buf_init();
255         unsigned char *ptr = (unsigned char *)s;
256         unsigned int v, o;
257
258         if (!buf)
259                 return NULL;
260
261         for (o = 0; o < l; o++)
262         {
263                 /* ascii char */
264                 if ((*ptr >= 0x01) && (*ptr <= 0x7F))
265                 {
266                         if (!buf_putchar(buf, *ptr++))
267                                 break;
268                 }
269
270                 /* invalid byte or multi byte sequence */
271                 else
272                 {
273                         if (!(v = _validate_utf8(&ptr, l - o, buf)))
274                                 break;
275
276                         o += (v - 1);
277                 }
278         }
279
280         return buf_destroy(buf);
281 }
282
283 /* Sanitize given string and strip all invalid XML bytes
284  * Validate UTF-8 sequences
285  * Escape XML control chars */
286 char * sanitize_pcdata(const char *s, unsigned int l)
287 {
288         struct template_buffer *buf = buf_init();
289         unsigned char *ptr = (unsigned char *)s;
290         unsigned int o, v;
291         char esq[8];
292         int esl;
293
294         if (!buf)
295                 return NULL;
296
297         for (o = 0; o < l; o++)
298         {
299                 /* Invalid XML bytes */
300                 if (((*ptr >= 0x00) && (*ptr <= 0x08)) ||
301                     ((*ptr >= 0x0B) && (*ptr <= 0x0C)) ||
302                     ((*ptr >= 0x0E) && (*ptr <= 0x1F)) ||
303                     (*ptr == 0x7F))
304                 {
305                         ptr++;
306                 }
307
308                 /* Escapes */
309                 else if ((*ptr == 0x26) ||
310                          (*ptr == 0x27) ||
311                          (*ptr == 0x22) ||
312                          (*ptr == 0x3C) ||
313                          (*ptr == 0x3E))
314                 {
315                         esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
316
317                         if (!buf_append(buf, (unsigned char *)esq, esl))
318                                 break;
319
320                         ptr++;
321                 }
322
323                 /* ascii char */
324                 else if (*ptr <= 0x7F)
325                 {
326                         buf_putchar(buf, *ptr++);
327                 }
328
329                 /* multi byte sequence */
330                 else
331                 {
332                         if (!(v = _validate_utf8(&ptr, l - o, buf)))
333                                 break;
334
335                         o += (v - 1);
336                 }
337         }
338
339         return buf_destroy(buf);
340 }