Back to index

php5  5.3.10
pcre_valid_utf8.c
Go to the documentation of this file.
00001 /*************************************************
00002 *      Perl-Compatible Regular Expressions       *
00003 *************************************************/
00004 
00005 /* PCRE is a library of functions to support regular expressions whose syntax
00006 and semantics are as close as possible to those of the Perl 5 language.
00007 
00008                        Written by Philip Hazel
00009            Copyright (c) 1997-2009 University of Cambridge
00010 
00011 -----------------------------------------------------------------------------
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014 
00015     * Redistributions of source code must retain the above copyright notice,
00016       this list of conditions and the following disclaimer.
00017 
00018     * Redistributions in binary form must reproduce the above copyright
00019       notice, this list of conditions and the following disclaimer in the
00020       documentation and/or other materials provided with the distribution.
00021 
00022     * Neither the name of the University of Cambridge nor the names of its
00023       contributors may be used to endorse or promote products derived from
00024       this software without specific prior written permission.
00025 
00026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036 POSSIBILITY OF SUCH DAMAGE.
00037 -----------------------------------------------------------------------------
00038 */
00039 
00040 
00041 /* This module contains an internal function for validating UTF-8 character
00042 strings. */
00043 
00044 
00045 #include "config.h"
00046 
00047 #include "pcre_internal.h"
00048 
00049 
00050 /*************************************************
00051 *         Validate a UTF-8 string                *
00052 *************************************************/
00053 
00054 /* This function is called (optionally) at the start of compile or match, to
00055 validate that a supposed UTF-8 string is actually valid. The early check means
00056 that subsequent code can assume it is dealing with a valid string. The check
00057 can be turned off for maximum performance, but the consequences of supplying
00058 an invalid string are then undefined.
00059 
00060 Originally, this function checked according to RFC 2279, allowing for values in
00061 the range 0 to 0x7fffffff, up to 6 bytes long, but ensuring that they were in
00062 the canonical format. Once somebody had pointed out RFC 3629 to me (it
00063 obsoletes 2279), additional restrictions were applied. The values are now
00064 limited to be between 0 and 0x0010ffff, no more than 4 bytes long, and the
00065 subrange 0xd000 to 0xdfff is excluded.
00066 
00067 Arguments:
00068   string       points to the string
00069   length       length of string, or -1 if the string is zero-terminated
00070 
00071 Returns:       < 0    if the string is a valid UTF-8 string
00072                >= 0   otherwise; the value is the offset of the bad byte
00073 
00074 Bad bytes can be:
00075 
00076   . An isolated byte whose most significant bits are 0x80, because this
00077     can only correctly appear within a UTF-8 character;
00078 
00079   . A byte whose most significant bits are 0xc0, but whose other bits indicate
00080     that there are more than 3 additional bytes (i.e. an RFC 2279 starting
00081     byte, which is no longer valid under RFC 3629);
00082 
00083   .
00084 
00085 The returned offset may also be equal to the length of the string; this means
00086 that one or more bytes is missing from the final UTF-8 character.
00087 */
00088 
00089 int
00090 _pcre_valid_utf8(USPTR string, int length)
00091 {
00092 #ifdef SUPPORT_UTF8
00093 register USPTR p;
00094 
00095 if (length < 0)
00096   {
00097   for (p = string; *p != 0; p++);
00098   length = p - string;
00099   }
00100 
00101 for (p = string; length-- > 0; p++)
00102   {
00103   register int ab;
00104   register int c = *p;
00105   if (c < 128) continue;
00106   if (c < 0xc0) return p - string;
00107   ab = _pcre_utf8_table4[c & 0x3f];     /* Number of additional bytes */
00108   if (ab > 3) return p - string;        /* Too many for RFC 3629 */
00109   if (length < ab) return p + 1 + length - string;   /* Missing bytes */
00110   length -= ab;
00111 
00112   /* Check top bits in the second byte */
00113   if ((*(++p) & 0xc0) != 0x80) return p - string;
00114 
00115   /* Check for overlong sequences for each different length, and for the
00116   excluded range 0xd000 to 0xdfff.  */
00117 
00118   switch (ab)
00119     {
00120     /* Check for xx00 000x (overlong sequence) */
00121 
00122     case 1:
00123     if ((c & 0x3e) == 0) return p - string;
00124     continue;   /* We know there aren't any more bytes to check */
00125 
00126     /* Check for 1110 0000, xx0x xxxx (overlong sequence) or
00127                  1110 1101, 1010 xxxx (0xd000 - 0xdfff) */
00128 
00129     case 2:
00130     if ((c == 0xe0 && (*p & 0x20) == 0) ||
00131         (c == 0xed && *p >= 0xa0))
00132       return p - string;
00133     break;
00134 
00135     /* Check for 1111 0000, xx00 xxxx (overlong sequence) or
00136        greater than 0x0010ffff (f4 8f bf bf) */
00137 
00138     case 3:
00139     if ((c == 0xf0 && (*p & 0x30) == 0) ||
00140         (c > 0xf4 ) ||
00141         (c == 0xf4 && *p > 0x8f))
00142       return p - string;
00143     break;
00144 
00145 #if 0
00146     /* These cases can no longer occur, as we restrict to a maximum of four
00147     bytes nowadays. Leave the code here in case we ever want to add an option
00148     for longer sequences. */
00149 
00150     /* Check for 1111 1000, xx00 0xxx */
00151     case 4:
00152     if (c == 0xf8 && (*p & 0x38) == 0) return p - string;
00153     break;
00154 
00155     /* Check for leading 0xfe or 0xff, and then for 1111 1100, xx00 00xx */
00156     case 5:
00157     if (c == 0xfe || c == 0xff ||
00158        (c == 0xfc && (*p & 0x3c) == 0)) return p - string;
00159     break;
00160 #endif
00161 
00162     }
00163 
00164   /* Check for valid bytes after the 2nd, if any; all must start 10 */
00165   while (--ab > 0)
00166     {
00167     if ((*(++p) & 0xc0) != 0x80) return p - string;
00168     }
00169   }
00170 #else
00171 (void)(string);  /* Keep picky compilers happy */
00172 (void)(length);
00173 #endif
00174 
00175 return -1;
00176 }
00177 
00178 /* End of pcre_valid_utf8.c */