tgupdate: merge pcreposix-compat base into pcreposix-compat
[pcreposix-compat.git] / pcre16_valid_utf16.c
blob09076539d09060945544ff6f151f2b7be0c824a7
1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
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.
8 Written by Philip Hazel
9 Copyright (c) 1997-2013 University of Cambridge
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
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.
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.
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 -----------------------------------------------------------------------------
41 /* This module contains an internal function for validating UTF-16 character
42 strings. */
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
49 /* Generate code with 16 bit character support. */
50 #define COMPILE_PCRE16
52 #include "pcre_internal.h"
55 /*************************************************
56 * Validate a UTF-16 string *
57 *************************************************/
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.
65 From release 8.21 more information about the details of the error are passed
66 back in the returned value:
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
72 PCRE_UTF16_ERR4 Unused (was non-character)
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
79 Returns: = 0 if the string is a valid UTF-16 string
80 > 0 otherwise, setting the offset of the bad character
83 int
84 PRIV(valid_utf)(PCRE_PUCHAR string, int length, int *erroroffset)
86 #ifdef SUPPORT_UTF
87 register PCRE_PUCHAR p;
88 register pcre_uint32 c;
90 if (length < 0)
92 for (p = string; *p != 0; p++);
93 length = p - string;
96 for (p = string; length-- > 0; p++)
98 c = *p;
100 if ((c & 0xf800) != 0xd800)
102 /* Normal UTF-16 code point. Neither high nor low surrogate. */
104 else if ((c & 0x0400) == 0)
106 /* High surrogate. Must be a followed by a low surrogate. */
107 if (length == 0)
109 *erroroffset = p - string;
110 return PCRE_UTF16_ERR1;
112 p++;
113 length--;
114 if ((*p & 0xfc00) != 0xdc00)
116 *erroroffset = p - string;
117 return PCRE_UTF16_ERR2;
120 else
122 /* Isolated low surrogate. Always an error. */
123 *erroroffset = p - string;
124 return PCRE_UTF16_ERR3;
128 #else /* SUPPORT_UTF */
129 (void)(string); /* Keep picky compilers happy */
130 (void)(length);
131 (void)(erroroffset);
132 #endif /* SUPPORT_UTF */
134 return PCRE_UTF16_ERR0; /* This indicates success */
137 /* End of pcre16_valid_utf16.c */