2 * LuCI Template - Utility functions
4 * Copyright (C) 2010 Jo-Philipp Wich <jow@openwrt.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "template_utils.h"
20 #include "template_lmo.h"
22 /* initialize a buffer object */
23 struct template_buffer * buf_init(int size)
25 struct template_buffer *buf;
30 buf = (struct template_buffer *)malloc(sizeof(struct template_buffer));
36 buf->data = malloc(buf->size);
38 if (buf->data != NULL)
40 buf->dptr = buf->data;
53 int buf_grow(struct template_buffer *buf, int size)
55 unsigned int off = (buf->dptr - buf->data);
61 data = realloc(buf->data, buf->size + size);
66 buf->dptr = data + off;
75 /* put one char into buffer object */
76 int buf_putchar(struct template_buffer *buf, char c)
78 if( ((buf->fill + 1) >= buf->size) && !buf_grow(buf, 0) )
88 /* append data to buffer */
89 int buf_append(struct template_buffer *buf, const char *s, int len)
91 if ((buf->fill + len + 1) >= buf->size)
93 if (!buf_grow(buf, len + 1))
97 memcpy(buf->dptr, s, len);
106 /* read buffer length */
107 int buf_length(struct template_buffer *buf)
112 /* destroy buffer object and return pointer to data */
113 char * buf_destroy(struct template_buffer *buf)
115 char *data = buf->data;
122 /* calculate the number of expected continuation chars */
123 static inline int mb_num_chars(unsigned char c)
125 if ((c & 0xE0) == 0xC0)
127 else if ((c & 0xF0) == 0xE0)
129 else if ((c & 0xF8) == 0xF0)
131 else if ((c & 0xFC) == 0xF8)
133 else if ((c & 0xFE) == 0xFC)
139 /* test whether the given byte is a valid continuation char */
140 static inline int mb_is_cont(unsigned char c)
142 return ((c >= 0x80) && (c <= 0xBF));
145 /* test whether the byte sequence at the given pointer with the given
146 * length is the shortest possible representation of the code point */
147 static inline int mb_is_shortest(unsigned char *s, int n)
152 /* 1100000x (10xxxxxx) */
153 return !(((*s >> 1) == 0x60) &&
154 ((*(s+1) >> 6) == 0x02));
157 /* 11100000 100xxxxx (10xxxxxx) */
158 return !((*s == 0xE0) &&
159 ((*(s+1) >> 5) == 0x04) &&
160 ((*(s+2) >> 6) == 0x02));
163 /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
164 return !((*s == 0xF0) &&
165 ((*(s+1) >> 4) == 0x08) &&
166 ((*(s+2) >> 6) == 0x02) &&
167 ((*(s+3) >> 6) == 0x02));
170 /* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) */
171 return !((*s == 0xF8) &&
172 ((*(s+1) >> 3) == 0x10) &&
173 ((*(s+2) >> 6) == 0x02) &&
174 ((*(s+3) >> 6) == 0x02) &&
175 ((*(s+4) >> 6) == 0x02));
178 /* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) */
179 return !((*s == 0xF8) &&
180 ((*(s+1) >> 2) == 0x20) &&
181 ((*(s+2) >> 6) == 0x02) &&
182 ((*(s+3) >> 6) == 0x02) &&
183 ((*(s+4) >> 6) == 0x02) &&
184 ((*(s+5) >> 6) == 0x02));
190 /* test whether the byte sequence at the given pointer with the given
191 * length is an UTF-16 surrogate */
192 static inline int mb_is_surrogate(unsigned char *s, int n)
194 return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF));
197 /* test whether the byte sequence at the given pointer with the given
198 * length is an illegal UTF-8 code point */
199 static inline int mb_is_illegal(unsigned char *s, int n)
201 return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) &&
202 (*(s+2) >= 0xBE) && (*(s+2) <= 0xBF));
206 /* scan given source string, validate UTF-8 sequence and store result
207 * in given buffer object */
208 static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf)
210 unsigned char *ptr = *s;
211 unsigned int o = 0, v, n;
213 /* ascii byte without null */
214 if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F))
216 if (!buf_putchar(buf, *ptr++))
222 /* multi byte sequence */
223 else if ((n = mb_num_chars(*ptr)) > 1)
225 /* count valid chars */
226 for (v = 1; (v <= n) && ((o+v) < l) && mb_is_cont(*(ptr+v)); v++);
232 /* five and six byte sequences are always invalid */
233 if (!buf_putchar(buf, '?'))
239 /* if the number of valid continuation bytes matches the
240 * expected number and if the sequence is legal, copy
241 * the bytes to the destination buffer */
242 if ((v == n) && mb_is_shortest(ptr, n) &&
243 !mb_is_surrogate(ptr, n) && !mb_is_illegal(ptr, n))
246 if (!buf_append(buf, (char *)ptr, n))
250 /* the found sequence is illegal, skip it */
253 /* invalid sequence */
254 if (!buf_putchar(buf, '?'))
261 /* advance beyound the last found valid continuation char */
266 /* invalid byte (0x00) */
269 if (!buf_putchar(buf, '?')) /* or 0xEF, 0xBF, 0xBD */
280 /* sanitize given string and replace all invalid UTF-8 sequences with "?" */
281 char * utf8(const char *s, unsigned int l)
283 struct template_buffer *buf = buf_init(l);
284 unsigned char *ptr = (unsigned char *)s;
290 for (o = 0; o < l; o++)
293 if ((*ptr >= 0x01) && (*ptr <= 0x7F))
295 if (!buf_putchar(buf, (char)*ptr++))
299 /* invalid byte or multi byte sequence */
302 if (!(v = _validate_utf8(&ptr, l - o, buf)))
309 return buf_destroy(buf);
312 /* Sanitize given string and strip all invalid XML bytes
313 * Validate UTF-8 sequences
314 * Escape XML control chars */
315 char * pcdata(const char *s, unsigned int l)
317 struct template_buffer *buf = buf_init(l);
318 unsigned char *ptr = (unsigned char *)s;
326 for (o = 0; o < l; o++)
328 /* Invalid XML bytes */
329 if (((*ptr >= 0x00) && (*ptr <= 0x08)) ||
330 ((*ptr >= 0x0B) && (*ptr <= 0x0C)) ||
331 ((*ptr >= 0x0E) && (*ptr <= 0x1F)) ||
338 else if ((*ptr == 0x26) ||
344 esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
346 if (!buf_append(buf, esq, esl))
353 else if (*ptr <= 0x7F)
355 buf_putchar(buf, (char)*ptr++);
358 /* multi byte sequence */
361 if (!(v = _validate_utf8(&ptr, l - o, buf)))
368 return buf_destroy(buf);
371 char * striptags(const char *s, unsigned int l)
373 struct template_buffer *buf = buf_init(l);
374 unsigned char *ptr = (unsigned char *)s;
375 unsigned char *end = ptr + l;
381 for (prev = ' '; ptr < end; ptr++)
383 if ((*ptr == '<') && ((ptr + 2) < end) &&
384 ((*(ptr + 1) == '/') || isalpha(*(ptr + 1))))
386 for (tag = ptr; tag < end; tag++)
391 buf_putchar(buf, ' ');
399 else if (isspace(*ptr))
402 buf_putchar(buf, *ptr);
415 esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
416 buf_append(buf, esq, esl);
420 buf_putchar(buf, *ptr);
428 return buf_destroy(buf);
431 void luastr_escape(struct template_buffer *out, const char *s, unsigned int l,
438 for (ptr = (char *)s; ptr < (s + l); ptr++)
443 buf_append(out, "\\\\", 2);
448 buf_append(out, """, 5);
450 buf_append(out, "\\\"", 2);
454 buf_append(out, "\\n", 2);
463 esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
464 buf_append(out, esq, esl);
469 buf_putchar(out, *ptr);
474 void luastr_translate(struct template_buffer *out, const char *s, unsigned int l,
480 if (!lmo_translate(s, l, &tr, &trlen))
481 luastr_escape(out, tr, trlen, escape_xml);
483 luastr_escape(out, s, l, escape_xml);