genericopenlibs/liboil/src/utf8_s.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /*
       
     2  * LIBOIL - Library of Optimized Inner Loops
       
     3  * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
       
     4  * All rights reserved.
       
     5  *
       
     6  * Redistribution and use in source and binary forms, with or without
       
     7  * modification, are permitted provided that the following conditions
       
     8  * are met:
       
     9  * 1. Redistributions of source code must retain the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer.
       
    11  * 2. Redistributions in binary form must reproduce the above copyright
       
    12  *    notice, this list of conditions and the following disclaimer in the
       
    13  *    documentation and/or other materials provided with the distribution.
       
    14  * 
       
    15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
       
    16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
       
    19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
       
    20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
       
    21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
       
    24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    25  * POSSIBILITY OF SUCH DAMAGE.
       
    26  */
       
    27 
       
    28 #ifdef HAVE_CONFIG_H
       
    29 #include "config.h"
       
    30 #endif
       
    31 
       
    32 #include <liboil/liboil.h>
       
    33 #include <liboil/liboiltest.h>
       
    34 #include <liboil/liboilrandom.h>
       
    35 #include "liboil/utf8/utf8.h"
       
    36 
       
    37 
       
    38 /*
       
    39  * Little explanation:
       
    40  *  0x00-0x7f  ASCII, one byte character
       
    41  *  0x80-0xbf  continuation byte, not a valid start byte
       
    42  *  0xc0-0xdf  2-byte character
       
    43  *  0xe0-0xef  3-byte character
       
    44  *  0xf0-0xf7  4-byte character
       
    45  *  0xf8-0xff  reserved (illegal at the present time)
       
    46  */
       
    47 static void
       
    48 utf8_validate_test (OilTest *test)
       
    49 {
       
    50   int i;
       
    51   int n = test->n;
       
    52   uint8_t *ptr = oil_test_get_source_data (test, OIL_ARG_SRC1);
       
    53   int x;
       
    54   int extra_chars = 0;
       
    55 
       
    56   for (i=0;i<n;i++){
       
    57     if (i >= n-16) {
       
    58       /* if it's close to the end, we'll randomly drop in a bad
       
    59        * byte from either the 0x80-0xbf or 0xf8-0xff segments */
       
    60       x = oil_rand_u8();
       
    61       if (x < 16) {
       
    62         x = oil_rand_u8();
       
    63         if (extra_chars>0) {
       
    64           /* this might not actually be a bad char */
       
    65           ptr[i] = x;
       
    66           extra_chars--;
       
    67         } else {
       
    68           if (x & 0x80) {
       
    69             ptr[i] = 0x80 | (x&0x3f);
       
    70           } else {
       
    71             ptr[i] = 0xf8 | (x&0x07);
       
    72           }
       
    73         }
       
    74         continue;
       
    75       }
       
    76     }
       
    77     if (extra_chars > 0) {
       
    78       ptr[i] = 0x80 | (oil_rand_u8() & 0x3f);
       
    79       extra_chars--;
       
    80     } else {
       
    81       /* otherwise, we'll do a low probability of a multibyte char */
       
    82       x = oil_rand_u8() & 0xf;
       
    83       if (x == 0) {
       
    84         ptr[i] = 0xc0 | (oil_rand_u8() & 0x1f);
       
    85         extra_chars = 1;
       
    86       } else if (x == 1) {
       
    87         ptr[i] = 0xe0 | (oil_rand_u8() & 0x0f);
       
    88         extra_chars = 2;
       
    89       } else if (x == 2) {
       
    90         ptr[i] = 0xf0 | (oil_rand_u8() & 0x07);
       
    91         extra_chars = 3;
       
    92       } else {
       
    93         ptr[i] = oil_rand_u8() & 0x7f;
       
    94       }
       
    95     }
       
    96   }
       
    97 
       
    98 }
       
    99 
       
   100 /**
       
   101  * oil_utf8_validate:
       
   102  * @d_1:
       
   103  * @s:
       
   104  * @n:
       
   105  *
       
   106  * Checks @s for valid UTF-8 characters.  If the entire @s array
       
   107  * represents valid UTF-8 characters, @n is written to @d_1.
       
   108  * Otherwise, the index in the array of the beginning of the first
       
   109  * invalid UTF-8 character is written to @d_1.
       
   110  */
       
   111 OIL_DEFINE_CLASS_FULL (utf8_validate, "int32_t *d_1, uint8_t *s, int n",
       
   112     utf8_validate_test);
       
   113 
       
   114 
       
   115 static void
       
   116 utf8_validate_ref (int32_t *d_1, uint8_t *s, int n)
       
   117 {
       
   118   int i;
       
   119   int extra_bytes;
       
   120   int mask;
       
   121 
       
   122   for(i=0;i<n;i++){
       
   123     extra_bytes = 0;
       
   124     if (s[i] < 128) continue;
       
   125     if ((s[i] & 0xe0) == 0xc0) {
       
   126       extra_bytes = 1;
       
   127       mask = 0x7f;
       
   128     } else if ((s[i] & 0xf0) == 0xe0) {
       
   129       extra_bytes = 2;
       
   130       mask = 0x1f;
       
   131     } else if ((s[i] & 0xf8) == 0xf0) {
       
   132       extra_bytes = 3;
       
   133       mask = 0x0f;
       
   134     } else {
       
   135       goto error;
       
   136     }
       
   137     if (i + extra_bytes >= n) goto error;
       
   138     while(extra_bytes--) {
       
   139       i++;
       
   140       if ((s[i] & 0xc0) != 0x80) goto error;
       
   141     }
       
   142   }
       
   143 
       
   144 error:
       
   145   d_1[0] = i;
       
   146 }
       
   147 
       
   148 OIL_DEFINE_IMPL_REF (utf8_validate_ref, utf8_validate);
       
   149 
       
   150 
       
   151