libs/web: introduce recursive expression support for datatypes, introduce "or" and...
[project/luci.git] / libs / web / src / template_utils.c
index c560b7a..36f08aa 100644 (file)
@@ -136,23 +136,38 @@ static inline int mb_is_shortest(unsigned char *s, int n)
        {
                case 2:
                        /* 1100000x (10xxxxxx) */
-                       return ((*s & 0x1E) > 0);
+                       return !(((*s >> 1) == 0x60) &&
+                                        ((*(s+1) >> 6) == 0x02));
 
                case 3:
                        /* 11100000 100xxxxx (10xxxxxx) */
-                       return ((*s & 0x1F) > 0) && ((*(s+1) & 0x60) > 0);
+                       return !((*s == 0xE0) &&
+                                        ((*(s+1) >> 5) == 0x04) &&
+                                        ((*(s+2) >> 6) == 0x02));
 
                case 4:
                        /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
-                       return ((*s & 0x0F) > 0) && ((*(s+1) & 0x70) > 0);
+                       return !((*s == 0xF0) &&
+                                        ((*(s+1) >> 4) == 0x08) &&
+                                        ((*(s+2) >> 6) == 0x02) &&
+                                        ((*(s+3) >> 6) == 0x02));
 
                case 5:
                        /* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) */
-                       return ((*s & 0x07) > 0) && ((*(s+1) & 0x78) > 0);
+                       return !((*s == 0xF8) &&
+                                        ((*(s+1) >> 3) == 0x10) &&
+                                        ((*(s+2) >> 6) == 0x02) &&
+                                        ((*(s+3) >> 6) == 0x02) &&
+                                        ((*(s+4) >> 6) == 0x02));
 
                case 6:
                        /* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) */
-                       return ((*s & 0x03) > 0) && ((*(s+1) & 0x7C) > 0);
+                       return !((*s == 0xF8) &&
+                                        ((*(s+1) >> 2) == 0x20) &&
+                                        ((*(s+2) >> 6) == 0x02) &&
+                                        ((*(s+3) >> 6) == 0x02) &&
+                                        ((*(s+4) >> 6) == 0x02) &&
+                                        ((*(s+5) >> 6) == 0x02));
        }
 
        return 1;
@@ -181,70 +196,67 @@ static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf)
        unsigned char *ptr = *s;
        unsigned int o = 0, v, n;
 
-       //for (o = 0; o < l; o++)
+       /* ascii byte without null */
+       if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F))
        {
-               /* ascii byte without null */
-               if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F))
-               {
-                       if (!buf_putchar(buf, *ptr++))
-                               return 0;
+               if (!buf_putchar(buf, *ptr++))
+                       return 0;
 
-                       o = 1;
-               }
+               o = 1;
+       }
 
-               /* multi byte sequence */
-               else if ((n = mb_num_chars(*ptr)) > 1)
+       /* multi byte sequence */
+       else if ((n = mb_num_chars(*ptr)) > 1)
+       {
+               /* count valid chars */
+               for (v = 1; (v <= n) && ((o+v) < l) && mb_is_cont(*(ptr+v)); v++);
+
+               switch (n)
                {
-                       /* count valid chars */
-                       for (v = 1; (v <= n) && ((o+v) < l) && mb_is_cont(*(ptr+v)); v++);
-
-                       switch (n)
-                       {
-                               case 6:
-                               case 5:
-                                       /* five and six byte sequences are always invalid */
+                       case 6:
+                       case 5:
+                               /* five and six byte sequences are always invalid */
+                               if (!buf_putchar(buf, '?'))
+                                       return 0;
+
+                               break;
+
+                       default:
+                               /* if the number of valid continuation bytes matches the
+                                * expected number and if the sequence is legal, copy
+                                * the bytes to the destination buffer */
+                               if ((v == n) && mb_is_shortest(ptr, n) &&
+                                       !mb_is_surrogate(ptr, n) && !mb_is_illegal(ptr, n))
+                               {
+                                       /* copy sequence */
+                                       if (!buf_append(buf, ptr, n))
+                                               return 0;
+                               }
+
+                               /* the found sequence is illegal, skip it */
+                               else
+                               {
+                                       /* invalid sequence */
                                        if (!buf_putchar(buf, '?'))
                                                return 0;
+                               }
 
-                                       break;
-
-                               default:
-                                       /* if the number of valid continuation bytes matches the
-                                        * expected number and if the sequence is legal, copy
-                                        * the bytes to the destination buffer */
-                                       if ((v == n) && mb_is_shortest(ptr, n) &&
-                                               !mb_is_surrogate(ptr, n) && !mb_is_illegal(ptr, n))
-                                       {
-                                               /* copy sequence */
-                                               if (!buf_append(buf, ptr, n))
-                                                       return 0;
-                                       }
-
-                                       /* the found sequence is illegal, skip it */
-                                       else
-                                       {
-                                               /* invalid sequence */
-                                               if (!buf_putchar(buf, '?'))
-                                                       return 0;
-                                       }
-
-                                       break;
-                       }
-
-                       /* advance beyound the last found valid continuation char */
-                       o = v;
-                       ptr += v;
+                               break;
                }
 
-               /* invalid byte (0x00) */
-               else
-               {
-                       if (!buf_putchar(buf, '?')) /* or 0xEF, 0xBF, 0xBD */
-                               return 0;
+               /* advance beyound the last found valid continuation char */
+               o = v;
+               ptr += v;
+       }
 
-                       o = 1;
-                       ptr++;
-               }
+       /* invalid byte (0x00) */
+       else
+       {
+               if (!buf_putchar(buf, '?')) /* or 0xEF, 0xBF, 0xBD */
+                       return 0;
+
+               o = 1;
+               ptr++;
        }
 
        *s = ptr;
@@ -256,15 +268,28 @@ char * sanitize_utf8(const char *s, unsigned int l)
 {
        struct template_buffer *buf = buf_init();
        unsigned char *ptr = (unsigned char *)s;
+       unsigned int v, o;
 
        if (!buf)
                return NULL;
 
-       if (!_validate_utf8(&ptr, l, buf))
+       for (o = 0; o < l; o++)
        {
-               free(buf->data);
-               free(buf);
-               return NULL;
+               /* ascii char */
+               if ((*ptr >= 0x01) && (*ptr <= 0x7F))
+               {
+                       if (!buf_putchar(buf, *ptr++))
+                               break;
+               }
+
+               /* invalid byte or multi byte sequence */
+               else
+               {
+                       if (!(v = _validate_utf8(&ptr, l - o, buf)))
+                               break;
+
+                       o += (v - 1);
+               }
        }
 
        return buf_destroy(buf);