Annotation of embedaddon/pcre/pcre16_valid_utf16.c, revision 1.1.1.2
1.1 misho 1: /*************************************************
2: * Perl-Compatible Regular Expressions *
3: *************************************************/
4:
5: /* PCRE is a library of functions to support regular expressions whose syntax
6: and semantics are as close as possible to those of the Perl 5 language.
7:
8: Written by Philip Hazel
1.1.1.2 ! misho 9: Copyright (c) 1997-2013 University of Cambridge
1.1 misho 10:
11: -----------------------------------------------------------------------------
12: Redistribution and use in source and binary forms, with or without
13: modification, are permitted provided that the following conditions are met:
14:
15: * Redistributions of source code must retain the above copyright notice,
16: this list of conditions and the following disclaimer.
17:
18: * Redistributions in binary form must reproduce the above copyright
19: notice, this list of conditions and the following disclaimer in the
20: documentation and/or other materials provided with the distribution.
21:
22: * Neither the name of the University of Cambridge nor the names of its
23: contributors may be used to endorse or promote products derived from
24: this software without specific prior written permission.
25:
26: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: POSSIBILITY OF SUCH DAMAGE.
37: -----------------------------------------------------------------------------
38: */
39:
40:
41: /* This module contains an internal function for validating UTF-16 character
42: strings. */
43:
44:
45: #ifdef HAVE_CONFIG_H
46: #include "config.h"
47: #endif
48:
49: /* Generate code with 16 bit character support. */
50: #define COMPILE_PCRE16
51:
52: #include "pcre_internal.h"
53:
54:
55: /*************************************************
56: * Validate a UTF-16 string *
57: *************************************************/
58:
59: /* This function is called (optionally) at the start of compile or match, to
60: check that a supposed UTF-16 string is actually valid. The early check means
61: that subsequent code can assume it is dealing with a valid string. The check
62: can be turned off for maximum performance, but the consequences of supplying an
63: invalid string are then undefined.
64:
65: From release 8.21 more information about the details of the error are passed
66: back in the returned value:
67:
68: PCRE_UTF16_ERR0 No error
69: PCRE_UTF16_ERR1 Missing low surrogate at the end of the string
70: PCRE_UTF16_ERR2 Invalid low surrogate
71: PCRE_UTF16_ERR3 Isolated low surrogate
1.1.1.2 ! misho 72: PCRE_UTF16_ERR4 Unused (was non-character)
1.1 misho 73:
74: Arguments:
75: string points to the string
76: length length of string, or -1 if the string is zero-terminated
77: errp pointer to an error position offset variable
78:
79: Returns: = 0 if the string is a valid UTF-16 string
80: > 0 otherwise, setting the offset of the bad character
81: */
82:
83: int
84: PRIV(valid_utf)(PCRE_PUCHAR string, int length, int *erroroffset)
85: {
86: #ifdef SUPPORT_UTF
87: register PCRE_PUCHAR p;
1.1.1.2 ! misho 88: register pcre_uint32 c;
1.1 misho 89:
90: if (length < 0)
91: {
92: for (p = string; *p != 0; p++);
93: length = p - string;
94: }
95:
96: for (p = string; length-- > 0; p++)
97: {
98: c = *p;
99:
100: if ((c & 0xf800) != 0xd800)
101: {
102: /* Normal UTF-16 code point. Neither high nor low surrogate. */
103: }
104: else if ((c & 0x0400) == 0)
105: {
1.1.1.2 ! misho 106: /* High surrogate. Must be a followed by a low surrogate. */
1.1 misho 107: if (length == 0)
108: {
109: *erroroffset = p - string;
110: return PCRE_UTF16_ERR1;
111: }
112: p++;
113: length--;
114: if ((*p & 0xfc00) != 0xdc00)
115: {
116: *erroroffset = p - string;
117: return PCRE_UTF16_ERR2;
118: }
119: }
120: else
121: {
122: /* Isolated low surrogate. Always an error. */
123: *erroroffset = p - string;
124: return PCRE_UTF16_ERR3;
125: }
126: }
127:
128: #else /* SUPPORT_UTF */
129: (void)(string); /* Keep picky compilers happy */
130: (void)(length);
1.1.1.2 ! misho 131: (void)(erroroffset);
1.1 misho 132: #endif /* SUPPORT_UTF */
133:
134: return PCRE_UTF16_ERR0; /* This indicates success */
135: }
136:
137: /* End of pcre16_valid_utf16.c */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>