genericopenlibs/liboil/src/utf8/utf8.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 //Portions Copyright (c)  2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
       
    28 
       
    29 #ifdef HAVE_CONFIG_H
       
    30 #include "config.h"
       
    31 #endif
       
    32 
       
    33 #include <liboil/liboil.h>
       
    34 #include <liboil/liboiltest.h>
       
    35 #include <liboil/liboilrandom.h>
       
    36 #include "utf8.h"
       
    37 
       
    38 
       
    39 /*
       
    40  * Little explanation:
       
    41  *  0x00-0x7f  ASCII, one byte character
       
    42  *  0x80-0xbf  continuation byte, not a valid start byte
       
    43  *  0xc0-0xdf  2-byte character
       
    44  *  0xe0-0xef  3-byte character
       
    45  *  0xf0-0xf7  4-byte character
       
    46  *  0xf8-0xff  reserved (illegal at the present time)
       
    47  */
       
    48 static void
       
    49 utf8_validate_test (OilTest *test)
       
    50 {
       
    51   int i;
       
    52   int n = test->n;
       
    53   uint8_t *ptr = oil_test_get_source_data (test, OIL_ARG_SRC1);
       
    54   int x;
       
    55   int extra_chars = 0;
       
    56 
       
    57   for (i=0;i<n;i++){
       
    58     if (i >= n-16) {
       
    59       /* if it's close to the end, we'll randomly drop in a bad
       
    60        * byte from either the 0x80-0xbf or 0xf8-0xff segments */
       
    61       x = oil_rand_u8();
       
    62       if (x < 16) {
       
    63         x = oil_rand_u8();
       
    64         if (extra_chars>0) {
       
    65           /* this might not actually be a bad char */
       
    66           ptr[i] = x;
       
    67           extra_chars--;
       
    68         } else {
       
    69           if (x & 0x80) {
       
    70             ptr[i] = 0x80 | (x&0x3f);
       
    71           } else {
       
    72             ptr[i] = 0xf8 | (x&0x07);
       
    73           }
       
    74         }
       
    75         continue;
       
    76       }
       
    77     }
       
    78     if (extra_chars > 0) {
       
    79       ptr[i] = 0x80 | (oil_rand_u8() & 0x3f);
       
    80       extra_chars--;
       
    81     } else {
       
    82       /* otherwise, we'll do a low probability of a multibyte char */
       
    83       x = oil_rand_u8() & 0xf;
       
    84       if (x == 0) {
       
    85         ptr[i] = 0xc0 | (oil_rand_u8() & 0x1f);
       
    86         extra_chars = 1;
       
    87       } else if (x == 1) {
       
    88         ptr[i] = 0xe0 | (oil_rand_u8() & 0x0f);
       
    89         extra_chars = 2;
       
    90       } else if (x == 2) {
       
    91         ptr[i] = 0xf0 | (oil_rand_u8() & 0x07);
       
    92         extra_chars = 3;
       
    93       } else {
       
    94         ptr[i] = oil_rand_u8() & 0x7f;
       
    95       }
       
    96     }
       
    97   }
       
    98 
       
    99 }
       
   100 
       
   101 /**
       
   102  * oil_utf8_validate:
       
   103  * @d_1:
       
   104  * @s:
       
   105  * @n:
       
   106  *
       
   107  * Checks @s for valid UTF-8 characters.  If the entire @s array
       
   108  * represents valid UTF-8 characters, @n is written to @d_1.
       
   109  * Otherwise, the index in the array of the beginning of the first
       
   110  * invalid UTF-8 character is written to @d_1.
       
   111  */
       
   112 OIL_DEFINE_CLASS_FULL (utf8_validate, "int32_t *d_1, uint8_t *s, int n",
       
   113     utf8_validate_test);
       
   114 
       
   115 
       
   116 static void
       
   117 utf8_validate_ref (int32_t *d_1, uint8_t *s, int n)
       
   118 {
       
   119   int i;
       
   120   int extra_bytes;
       
   121   int mask;
       
   122 
       
   123   for(i=0;i<n;i++){
       
   124     extra_bytes = 0;
       
   125     if (s[i] < 128) continue;
       
   126     if ((s[i] & 0xe0) == 0xc0) {
       
   127       extra_bytes = 1;
       
   128       mask = 0x7f;
       
   129     } else if ((s[i] & 0xf0) == 0xe0) {
       
   130       extra_bytes = 2;
       
   131       mask = 0x1f;
       
   132     } else if ((s[i] & 0xf8) == 0xf0) {
       
   133       extra_bytes = 3;
       
   134       mask = 0x0f;
       
   135     } else {
       
   136       goto error;
       
   137     }
       
   138     if (i + extra_bytes >= n) goto error;
       
   139     while(extra_bytes--) {
       
   140       i++;
       
   141       if ((s[i] & 0xc0) != 0x80) goto error;
       
   142     }
       
   143   }
       
   144 
       
   145 error:
       
   146   d_1[0] = i;
       
   147 }
       
   148 
       
   149 OIL_DEFINE_IMPL_REF (utf8_validate_ref, utf8_validate);
       
   150 
       
   151 #ifdef	__SYMBIAN32__
       
   152  
       
   153 OilFunctionImpl* __oil_function_impl_utf8_validate_ref() {
       
   154         return &_oil_function_impl_utf8_validate_ref;
       
   155 }
       
   156 #endif
       
   157 
       
   158 
       
   159 #ifdef	__SYMBIAN32__
       
   160  
       
   161 OilFunctionClass* __oil_function_class_utf8_validate() {
       
   162         return &_oil_function_class_utf8_validate;
       
   163 }
       
   164 #endif
       
   165 
       
   166 
       
   167 
       
   168 
       
   169 #ifdef	__SYMBIAN32__
       
   170  
       
   171 EXPORT_C void** _oil_function_class_ptr_utf8_validate ()	{
       
   172 	oil_function_class_ptr_utf8_validate = __oil_function_class_utf8_validate();
       
   173 	return &oil_function_class_ptr_utf8_validate->func;
       
   174 	}
       
   175 #endif
       
   176