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-2013 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-16 character 42strings. */ 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 60check that a supposed UTF-16 string is actually valid. The early check means 61that subsequent code can assume it is dealing with a valid string. The check 62can be turned off for maximum performance, but the consequences of supplying an 63invalid string are then undefined. 64 65From release 8.21 more information about the details of the error are passed 66back in the returned value: 67 68PCRE_UTF16_ERR0 No error 69PCRE_UTF16_ERR1 Missing low surrogate at the end of the string 70PCRE_UTF16_ERR2 Invalid low surrogate 71PCRE_UTF16_ERR3 Isolated low surrogate 72PCRE_UTF16_ERR4 Unused (was non-character) 73 74Arguments: 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 79Returns: = 0 if the string is a valid UTF-16 string 80 > 0 otherwise, setting the offset of the bad character 81*/ 82 83int 84PRIV(valid_utf)(PCRE_PUCHAR string, int length, int *erroroffset) 85{ 86#ifdef SUPPORT_UTF 87register PCRE_PUCHAR p; 88register pcre_uint32 c; 89 90if (length < 0) 91 { 92 for (p = string; *p != 0; p++); 93 length = p - string; 94 } 95 96for (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 { 106 /* High surrogate. Must be a followed by a low surrogate. */ 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); 131(void)(erroroffset); 132#endif /* SUPPORT_UTF */ 133 134return PCRE_UTF16_ERR0; /* This indicates success */ 135} 136 137/* End of pcre16_valid_utf16.c */ 138