source: XMLIO_V2/external/src/POCO/Foundation.save/pcre_valid_utf8.c @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1/*************************************************
2*      Perl-Compatible Regular Expressions       *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and semantics are as close as possible to those of the Perl 5 language.
7
8                       Written by Philip Hazel
9           Copyright (c) 1997-2008 University of Cambridge
10
11-----------------------------------------------------------------------------
12Redistribution and use in source and binary forms, with or without
13modification, 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
26THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36POSSIBILITY OF SUCH DAMAGE.
37-----------------------------------------------------------------------------
38*/
39
40
41/* This module contains an internal function for validating UTF-8 character
42strings. */
43
44
45#include "pcre_config.h"
46#include "pcre_internal.h"
47
48
49/*************************************************
50*         Validate a UTF-8 string                *
51*************************************************/
52
53/* This function is called (optionally) at the start of compile or match, to
54validate that a supposed UTF-8 string is actually valid. The early check means
55that subsequent code can assume it is dealing with a valid string. The check
56can be turned off for maximum performance, but the consequences of supplying
57an invalid string are then undefined.
58
59Originally, this function checked according to RFC 2279, allowing for values in
60the range 0 to 0x7fffffff, up to 6 bytes long, but ensuring that they were in
61the canonical format. Once somebody had pointed out RFC 3629 to me (it
62obsoletes 2279), additional restrictions were applied. The values are now
63limited to be between 0 and 0x0010ffff, no more than 4 bytes long, and the
64subrange 0xd000 to 0xdfff is excluded.
65
66Arguments:
67  string       points to the string
68  length       length of string, or -1 if the string is zero-terminated
69
70Returns:       < 0    if the string is a valid UTF-8 string
71               >= 0   otherwise; the value is the offset of the bad byte
72*/
73
74int
75_pcre_valid_utf8(const uschar *string, int length)
76{
77#ifdef SUPPORT_UTF8
78register const uschar *p;
79
80if (length < 0)
81  {
82  for (p = string; *p != 0; p++);
83  length = p - string;
84  }
85
86for (p = string; length-- > 0; p++)
87  {
88  register int ab;
89  register int c = *p;
90  if (c < 128) continue;
91  if (c < 0xc0) return p - string;
92  ab = _pcre_utf8_table4[c & 0x3f];     /* Number of additional bytes */
93  if (length < ab || ab > 3) return p - string;
94  length -= ab;
95
96  /* Check top bits in the second byte */
97  if ((*(++p) & 0xc0) != 0x80) return p - string;
98
99  /* Check for overlong sequences for each different length, and for the
100  excluded range 0xd000 to 0xdfff.  */
101
102  switch (ab)
103    {
104    /* Check for xx00 000x (overlong sequence) */
105
106    case 1:
107    if ((c & 0x3e) == 0) return p - string;
108    continue;   /* We know there aren't any more bytes to check */
109
110    /* Check for 1110 0000, xx0x xxxx (overlong sequence) or
111                 1110 1101, 1010 xxxx (0xd000 - 0xdfff) */
112
113    case 2:
114    if ((c == 0xe0 && (*p & 0x20) == 0) ||
115        (c == 0xed && *p >= 0xa0))
116      return p - string;
117    break;
118
119    /* Check for 1111 0000, xx00 xxxx (overlong sequence) or
120       greater than 0x0010ffff (f4 8f bf bf) */
121
122    case 3:
123    if ((c == 0xf0 && (*p & 0x30) == 0) ||
124        (c > 0xf4 ) ||
125        (c == 0xf4 && *p > 0x8f))
126      return p - string;
127    break;
128
129#if 0
130    /* These cases can no longer occur, as we restrict to a maximum of four
131    bytes nowadays. Leave the code here in case we ever want to add an option
132    for longer sequences. */
133
134    /* Check for 1111 1000, xx00 0xxx */
135    case 4:
136    if (c == 0xf8 && (*p & 0x38) == 0) return p - string;
137    break;
138
139    /* Check for leading 0xfe or 0xff, and then for 1111 1100, xx00 00xx */
140    case 5:
141    if (c == 0xfe || c == 0xff ||
142       (c == 0xfc && (*p & 0x3c) == 0)) return p - string;
143    break;
144#endif
145
146    }
147
148  /* Check for valid bytes after the 2nd, if any; all must start 10 */
149  while (--ab > 0)
150    {
151    if ((*(++p) & 0xc0) != 0x80) return p - string;
152    }
153  }
154#else
155(void)(string);  /* Keep picky compilers happy */
156(void)(length);
157#endif
158
159return -1;
160}
161
162/* End of pcre_valid_utf8.c */
Note: See TracBrowser for help on using the repository browser.