template_utils.c 7.6 KB

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