| 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-2005 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-8 character
|
| 42 |
strings. */
|
| 43 |
|
| 44 |
|
| 45 |
#include "pcre_internal.h"
|
| 46 |
|
| 47 |
|
| 48 |
/*************************************************
|
| 49 |
* Validate a UTF-8 string *
|
| 50 |
*************************************************/
|
| 51 |
|
| 52 |
/* This function is called (optionally) at the start of compile or match, to
|
| 53 |
validate that a supposed UTF-8 string is actually valid. The early check means
|
| 54 |
that subsequent code can assume it is dealing with a valid string. The check
|
| 55 |
can be turned off for maximum performance, but the consequences of supplying
|
| 56 |
an invalid string are then undefined.
|
| 57 |
|
| 58 |
Arguments:
|
| 59 |
string points to the string
|
| 60 |
length length of string, or -1 if the string is zero-terminated
|
| 61 |
|
| 62 |
Returns: < 0 if the string is a valid UTF-8 string
|
| 63 |
>= 0 otherwise; the value is the offset of the bad byte
|
| 64 |
*/
|
| 65 |
|
| 66 |
EXPORT int
|
| 67 |
_pcre_valid_utf8(const uschar *string, int length)
|
| 68 |
{
|
| 69 |
register const uschar *p;
|
| 70 |
|
| 71 |
if (length < 0)
|
| 72 |
{
|
| 73 |
for (p = string; *p != 0; p++);
|
| 74 |
length = p - string;
|
| 75 |
}
|
| 76 |
|
| 77 |
for (p = string; length-- > 0; p++)
|
| 78 |
{
|
| 79 |
register int ab;
|
| 80 |
register int c = *p;
|
| 81 |
if (c < 128) continue;
|
| 82 |
if ((c & 0xc0) != 0xc0) return p - string;
|
| 83 |
ab = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */
|
| 84 |
if (length < ab) return p - string;
|
| 85 |
length -= ab;
|
| 86 |
|
| 87 |
/* Check top bits in the second byte */
|
| 88 |
if ((*(++p) & 0xc0) != 0x80) return p - string;
|
| 89 |
|
| 90 |
/* Check for overlong sequences for each different length */
|
| 91 |
switch (ab)
|
| 92 |
{
|
| 93 |
/* Check for xx00 000x */
|
| 94 |
case 1:
|
| 95 |
if ((c & 0x3e) == 0) return p - string;
|
| 96 |
continue; /* We know there aren't any more bytes to check */
|
| 97 |
|
| 98 |
/* Check for 1110 0000, xx0x xxxx */
|
| 99 |
case 2:
|
| 100 |
if (c == 0xe0 && (*p & 0x20) == 0) return p - string;
|
| 101 |
break;
|
| 102 |
|
| 103 |
/* Check for 1111 0000, xx00 xxxx */
|
| 104 |
case 3:
|
| 105 |
if (c == 0xf0 && (*p & 0x30) == 0) return p - string;
|
| 106 |
break;
|
| 107 |
|
| 108 |
/* Check for 1111 1000, xx00 0xxx */
|
| 109 |
case 4:
|
| 110 |
if (c == 0xf8 && (*p & 0x38) == 0) return p - string;
|
| 111 |
break;
|
| 112 |
|
| 113 |
/* Check for leading 0xfe or 0xff, and then for 1111 1100, xx00 00xx */
|
| 114 |
case 5:
|
| 115 |
if (c == 0xfe || c == 0xff ||
|
| 116 |
(c == 0xfc && (*p & 0x3c) == 0)) return p - string;
|
| 117 |
break;
|
| 118 |
}
|
| 119 |
|
| 120 |
/* Check for valid bytes after the 2nd, if any; all must start 10 */
|
| 121 |
while (--ab > 0)
|
| 122 |
{
|
| 123 |
if ((*(++p) & 0xc0) != 0x80) return p - string;
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
return -1;
|
| 128 |
}
|
| 129 |
|
| 130 |
/* End of pcre_valid_utf8.c */
|