First public contribution.
2 * LIBOIL - Library of Optimized Inner Loops
3 * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
32 #include <liboil/liboil.h>
33 #include <liboil/liboiltest.h>
34 #include <liboil/liboilrandom.h>
35 #include "liboil/utf8/utf8.h"
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)
48 utf8_validate_test (OilTest *test)
52 uint8_t *ptr = oil_test_get_source_data (test, OIL_ARG_SRC1);
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 */
64 /* this might not actually be a bad char */
69 ptr[i] = 0x80 | (x&0x3f);
71 ptr[i] = 0xf8 | (x&0x07);
77 if (extra_chars > 0) {
78 ptr[i] = 0x80 | (oil_rand_u8() & 0x3f);
81 /* otherwise, we'll do a low probability of a multibyte char */
82 x = oil_rand_u8() & 0xf;
84 ptr[i] = 0xc0 | (oil_rand_u8() & 0x1f);
87 ptr[i] = 0xe0 | (oil_rand_u8() & 0x0f);
90 ptr[i] = 0xf0 | (oil_rand_u8() & 0x07);
93 ptr[i] = oil_rand_u8() & 0x7f;
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.
111 OIL_DEFINE_CLASS_FULL (utf8_validate, "int32_t *d_1, uint8_t *s, int n",
116 utf8_validate_ref (int32_t *d_1, uint8_t *s, int n)
124 if (s[i] < 128) continue;
125 if ((s[i] & 0xe0) == 0xc0) {
128 } else if ((s[i] & 0xf0) == 0xe0) {
131 } else if ((s[i] & 0xf8) == 0xf0) {
137 if (i + extra_bytes >= n) goto error;
138 while(extra_bytes--) {
140 if ((s[i] & 0xc0) != 0x80) goto error;
148 OIL_DEFINE_IMPL_REF (utf8_validate_ref, utf8_validate);