1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/liboil/src/utf8/utf8.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,176 @@
1.4 +/*
1.5 + * LIBOIL - Library of Optimized Inner Loops
1.6 + * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
1.7 + * All rights reserved.
1.8 + *
1.9 + * Redistribution and use in source and binary forms, with or without
1.10 + * modification, are permitted provided that the following conditions
1.11 + * are met:
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + * 2. Redistributions in binary form must reproduce the above copyright
1.15 + * notice, this list of conditions and the following disclaimer in the
1.16 + * documentation and/or other materials provided with the distribution.
1.17 + *
1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1.28 + * POSSIBILITY OF SUCH DAMAGE.
1.29 + */
1.30 +//Portions Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.31 +
1.32 +#ifdef HAVE_CONFIG_H
1.33 +#include "config.h"
1.34 +#endif
1.35 +
1.36 +#include <liboil/liboil.h>
1.37 +#include <liboil/liboiltest.h>
1.38 +#include <liboil/liboilrandom.h>
1.39 +#include "utf8.h"
1.40 +
1.41 +
1.42 +/*
1.43 + * Little explanation:
1.44 + * 0x00-0x7f ASCII, one byte character
1.45 + * 0x80-0xbf continuation byte, not a valid start byte
1.46 + * 0xc0-0xdf 2-byte character
1.47 + * 0xe0-0xef 3-byte character
1.48 + * 0xf0-0xf7 4-byte character
1.49 + * 0xf8-0xff reserved (illegal at the present time)
1.50 + */
1.51 +static void
1.52 +utf8_validate_test (OilTest *test)
1.53 +{
1.54 + int i;
1.55 + int n = test->n;
1.56 + uint8_t *ptr = oil_test_get_source_data (test, OIL_ARG_SRC1);
1.57 + int x;
1.58 + int extra_chars = 0;
1.59 +
1.60 + for (i=0;i<n;i++){
1.61 + if (i >= n-16) {
1.62 + /* if it's close to the end, we'll randomly drop in a bad
1.63 + * byte from either the 0x80-0xbf or 0xf8-0xff segments */
1.64 + x = oil_rand_u8();
1.65 + if (x < 16) {
1.66 + x = oil_rand_u8();
1.67 + if (extra_chars>0) {
1.68 + /* this might not actually be a bad char */
1.69 + ptr[i] = x;
1.70 + extra_chars--;
1.71 + } else {
1.72 + if (x & 0x80) {
1.73 + ptr[i] = 0x80 | (x&0x3f);
1.74 + } else {
1.75 + ptr[i] = 0xf8 | (x&0x07);
1.76 + }
1.77 + }
1.78 + continue;
1.79 + }
1.80 + }
1.81 + if (extra_chars > 0) {
1.82 + ptr[i] = 0x80 | (oil_rand_u8() & 0x3f);
1.83 + extra_chars--;
1.84 + } else {
1.85 + /* otherwise, we'll do a low probability of a multibyte char */
1.86 + x = oil_rand_u8() & 0xf;
1.87 + if (x == 0) {
1.88 + ptr[i] = 0xc0 | (oil_rand_u8() & 0x1f);
1.89 + extra_chars = 1;
1.90 + } else if (x == 1) {
1.91 + ptr[i] = 0xe0 | (oil_rand_u8() & 0x0f);
1.92 + extra_chars = 2;
1.93 + } else if (x == 2) {
1.94 + ptr[i] = 0xf0 | (oil_rand_u8() & 0x07);
1.95 + extra_chars = 3;
1.96 + } else {
1.97 + ptr[i] = oil_rand_u8() & 0x7f;
1.98 + }
1.99 + }
1.100 + }
1.101 +
1.102 +}
1.103 +
1.104 +/**
1.105 + * oil_utf8_validate:
1.106 + * @d_1:
1.107 + * @s:
1.108 + * @n:
1.109 + *
1.110 + * Checks @s for valid UTF-8 characters. If the entire @s array
1.111 + * represents valid UTF-8 characters, @n is written to @d_1.
1.112 + * Otherwise, the index in the array of the beginning of the first
1.113 + * invalid UTF-8 character is written to @d_1.
1.114 + */
1.115 +OIL_DEFINE_CLASS_FULL (utf8_validate, "int32_t *d_1, uint8_t *s, int n",
1.116 + utf8_validate_test);
1.117 +
1.118 +
1.119 +static void
1.120 +utf8_validate_ref (int32_t *d_1, uint8_t *s, int n)
1.121 +{
1.122 + int i;
1.123 + int extra_bytes;
1.124 + int mask;
1.125 +
1.126 + for(i=0;i<n;i++){
1.127 + extra_bytes = 0;
1.128 + if (s[i] < 128) continue;
1.129 + if ((s[i] & 0xe0) == 0xc0) {
1.130 + extra_bytes = 1;
1.131 + mask = 0x7f;
1.132 + } else if ((s[i] & 0xf0) == 0xe0) {
1.133 + extra_bytes = 2;
1.134 + mask = 0x1f;
1.135 + } else if ((s[i] & 0xf8) == 0xf0) {
1.136 + extra_bytes = 3;
1.137 + mask = 0x0f;
1.138 + } else {
1.139 + goto error;
1.140 + }
1.141 + if (i + extra_bytes >= n) goto error;
1.142 + while(extra_bytes--) {
1.143 + i++;
1.144 + if ((s[i] & 0xc0) != 0x80) goto error;
1.145 + }
1.146 + }
1.147 +
1.148 +error:
1.149 + d_1[0] = i;
1.150 +}
1.151 +
1.152 +OIL_DEFINE_IMPL_REF (utf8_validate_ref, utf8_validate);
1.153 +
1.154 +#ifdef __SYMBIAN32__
1.155 +
1.156 +OilFunctionImpl* __oil_function_impl_utf8_validate_ref() {
1.157 + return &_oil_function_impl_utf8_validate_ref;
1.158 +}
1.159 +#endif
1.160 +
1.161 +
1.162 +#ifdef __SYMBIAN32__
1.163 +
1.164 +OilFunctionClass* __oil_function_class_utf8_validate() {
1.165 + return &_oil_function_class_utf8_validate;
1.166 +}
1.167 +#endif
1.168 +
1.169 +
1.170 +
1.171 +
1.172 +#ifdef __SYMBIAN32__
1.173 +
1.174 +EXPORT_C void** _oil_function_class_ptr_utf8_validate () {
1.175 + oil_function_class_ptr_utf8_validate = __oil_function_class_utf8_validate();
1.176 + return &oil_function_class_ptr_utf8_validate->func;
1.177 + }
1.178 +#endif
1.179 +