os/ossrv/genericopenlibs/liboil/src/utf8_s.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/liboil/src/utf8_s.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,151 @@
     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 +
    1.31 +#ifdef HAVE_CONFIG_H
    1.32 +#include "config.h"
    1.33 +#endif
    1.34 +
    1.35 +#include <liboil/liboil.h>
    1.36 +#include <liboil/liboiltest.h>
    1.37 +#include <liboil/liboilrandom.h>
    1.38 +#include "liboil/utf8/utf8.h"
    1.39 +
    1.40 +
    1.41 +/*
    1.42 + * Little explanation:
    1.43 + *  0x00-0x7f  ASCII, one byte character
    1.44 + *  0x80-0xbf  continuation byte, not a valid start byte
    1.45 + *  0xc0-0xdf  2-byte character
    1.46 + *  0xe0-0xef  3-byte character
    1.47 + *  0xf0-0xf7  4-byte character
    1.48 + *  0xf8-0xff  reserved (illegal at the present time)
    1.49 + */
    1.50 +static void
    1.51 +utf8_validate_test (OilTest *test)
    1.52 +{
    1.53 +  int i;
    1.54 +  int n = test->n;
    1.55 +  uint8_t *ptr = oil_test_get_source_data (test, OIL_ARG_SRC1);
    1.56 +  int x;
    1.57 +  int extra_chars = 0;
    1.58 +
    1.59 +  for (i=0;i<n;i++){
    1.60 +    if (i >= n-16) {
    1.61 +      /* if it's close to the end, we'll randomly drop in a bad
    1.62 +       * byte from either the 0x80-0xbf or 0xf8-0xff segments */
    1.63 +      x = oil_rand_u8();
    1.64 +      if (x < 16) {
    1.65 +        x = oil_rand_u8();
    1.66 +        if (extra_chars>0) {
    1.67 +          /* this might not actually be a bad char */
    1.68 +          ptr[i] = x;
    1.69 +          extra_chars--;
    1.70 +        } else {
    1.71 +          if (x & 0x80) {
    1.72 +            ptr[i] = 0x80 | (x&0x3f);
    1.73 +          } else {
    1.74 +            ptr[i] = 0xf8 | (x&0x07);
    1.75 +          }
    1.76 +        }
    1.77 +        continue;
    1.78 +      }
    1.79 +    }
    1.80 +    if (extra_chars > 0) {
    1.81 +      ptr[i] = 0x80 | (oil_rand_u8() & 0x3f);
    1.82 +      extra_chars--;
    1.83 +    } else {
    1.84 +      /* otherwise, we'll do a low probability of a multibyte char */
    1.85 +      x = oil_rand_u8() & 0xf;
    1.86 +      if (x == 0) {
    1.87 +        ptr[i] = 0xc0 | (oil_rand_u8() & 0x1f);
    1.88 +        extra_chars = 1;
    1.89 +      } else if (x == 1) {
    1.90 +        ptr[i] = 0xe0 | (oil_rand_u8() & 0x0f);
    1.91 +        extra_chars = 2;
    1.92 +      } else if (x == 2) {
    1.93 +        ptr[i] = 0xf0 | (oil_rand_u8() & 0x07);
    1.94 +        extra_chars = 3;
    1.95 +      } else {
    1.96 +        ptr[i] = oil_rand_u8() & 0x7f;
    1.97 +      }
    1.98 +    }
    1.99 +  }
   1.100 +
   1.101 +}
   1.102 +
   1.103 +/**
   1.104 + * oil_utf8_validate:
   1.105 + * @d_1:
   1.106 + * @s:
   1.107 + * @n:
   1.108 + *
   1.109 + * Checks @s for valid UTF-8 characters.  If the entire @s array
   1.110 + * represents valid UTF-8 characters, @n is written to @d_1.
   1.111 + * Otherwise, the index in the array of the beginning of the first
   1.112 + * invalid UTF-8 character is written to @d_1.
   1.113 + */
   1.114 +OIL_DEFINE_CLASS_FULL (utf8_validate, "int32_t *d_1, uint8_t *s, int n",
   1.115 +    utf8_validate_test);
   1.116 +
   1.117 +
   1.118 +static void
   1.119 +utf8_validate_ref (int32_t *d_1, uint8_t *s, int n)
   1.120 +{
   1.121 +  int i;
   1.122 +  int extra_bytes;
   1.123 +  int mask;
   1.124 +
   1.125 +  for(i=0;i<n;i++){
   1.126 +    extra_bytes = 0;
   1.127 +    if (s[i] < 128) continue;
   1.128 +    if ((s[i] & 0xe0) == 0xc0) {
   1.129 +      extra_bytes = 1;
   1.130 +      mask = 0x7f;
   1.131 +    } else if ((s[i] & 0xf0) == 0xe0) {
   1.132 +      extra_bytes = 2;
   1.133 +      mask = 0x1f;
   1.134 +    } else if ((s[i] & 0xf8) == 0xf0) {
   1.135 +      extra_bytes = 3;
   1.136 +      mask = 0x0f;
   1.137 +    } else {
   1.138 +      goto error;
   1.139 +    }
   1.140 +    if (i + extra_bytes >= n) goto error;
   1.141 +    while(extra_bytes--) {
   1.142 +      i++;
   1.143 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.144 +    }
   1.145 +  }
   1.146 +
   1.147 +error:
   1.148 +  d_1[0] = i;
   1.149 +}
   1.150 +
   1.151 +OIL_DEFINE_IMPL_REF (utf8_validate_ref, utf8_validate);
   1.152 +
   1.153 +
   1.154 +