/[pcre]/code/trunk/pcre16_valid_utf16.c
ViewVC logotype

Contents of /code/trunk/pcre16_valid_utf16.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 763 - (show annotations) (download)
Tue Nov 22 21:46:22 2011 UTC (2 years, 8 months ago) by zherczeg
Original Path: code/branches/pcre16/pcre16_valid_utf16.c
File MIME type: text/plain
File size: 4618 byte(s)
string utilities added
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
9 Copyright (c) 1997-2009 University of Cambridge
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 #include "pcre_internal.h"
50
51
52 /*************************************************
53 * Validate a UTF-16 string *
54 *************************************************/
55
56 /* This function is called (optionally) at the start of compile or match, to
57 check that a supposed UTF-16 string is actually valid. The early check means
58 that subsequent code can assume it is dealing with a valid string. The check
59 can be turned off for maximum performance, but the consequences of supplying an
60 invalid string are then undefined.
61
62 From release 8.21 more information about the details of the error are passed
63 back in the returned value:
64
65 PCRE_UTF16_ERR0 No error
66 PCRE_UTF16_ERR1 Missing low surrogate at the end of the string
67 PCRE_UTF16_ERR2 Invalid low surrogate
68 PCRE_UTF16_ERR3 Isolated low surrogate
69 PCRE_UTF16_ERR4 Not allowed character.
70
71 Arguments:
72 string points to the string
73 length length of string, or -1 if the string is zero-terminated
74 errp pointer to an error position offset variable
75
76 Returns: = 0 if the string is a valid UTF-16 string
77 > 0 otherwise, setting the offset of the bad character
78 */
79
80 int
81 _pcre16_valid_utf16(PCRE_PUCHAR string, int length, int *erroroffset)
82 {
83 #ifdef SUPPORT_UTF16
84 register PCRE_PUCHAR p;
85 register uschar c;
86
87 if (length < 0)
88 {
89 for (p = string; *p != 0; p++);
90 length = p - string;
91 }
92
93 for (p = string; length-- > 0; p++)
94 {
95 c = *p;
96
97 if ((c & 0xf800) != 0xd800)
98 {
99 /* Normal UTF-16 code point. Neither high nor low surrogate. */
100
101 /* This is probably a BOM from a different byte-order.
102 Regardless, the string is rejected. */
103 if (c == 0xfffe)
104 {
105 *erroroffset = p - string;
106 return PCRE_UTF16_ERR4;
107 }
108 }
109 else if ((c & 0x0400) == 0)
110 {
111 /* High surrogate. */
112
113 /* Must be a followed by a low surrogate. */
114 if (length == 0)
115 {
116 *erroroffset = p - string;
117 return PCRE_UTF16_ERR1;
118 }
119 p++;
120 length--;
121 if ((*p & 0xfc00) != 0xdc00)
122 {
123 *erroroffset = p - string;
124 return PCRE_UTF16_ERR2;
125 }
126 }
127 else
128 {
129 /* Isolated low surrogate. Always an error. */
130 *erroroffset = p - string;
131 return PCRE_UTF16_ERR3;
132 }
133 }
134
135 #else /* SUPPORT_UTF16 */
136 (void)(string); /* Keep picky compilers happy */
137 (void)(length);
138 #endif
139
140 return PCRE_UTF16_ERR0; /* This indicates success */
141 }
142
143 /* End of pcre16_valid_utf16.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12