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

Diff of /code/trunk/pcre32_valid_utf32.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

code/trunk/pcre_refcount.c revision 836 by ph10, Wed Dec 28 17:16:11 2011 UTC code/trunk/pcre32_valid_utf32.c revision 1118 by chpe, Tue Oct 16 15:57:31 2012 UTC
# Line 38  POSSIBILITY OF SUCH DAMAGE. Line 38  POSSIBILITY OF SUCH DAMAGE.
38  */  */
39    
40    
41  /* This module contains the external function pcre_refcount(), which is an  /* This module contains an internal function for validating UTF-32 character
42  auxiliary function that can be used to maintain a reference count in a compiled  strings. */
 pattern data block. This might be helpful in applications where the block is  
 shared by different users. */  
43    
44    
45  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
46  #include "config.h"  #include "config.h"
47  #endif  #endif
48    
49  #include "pcre_internal.h"  /* Generate code with 32 bit character support. */
50    #define COMPILE_PCRE32
51    
52    #include "pcre_internal.h"
53    
54  /*************************************************  /*************************************************
55  *           Maintain reference count             *  *         Validate a UTF-32 string                *
56  *************************************************/  *************************************************/
57    
58  /* The reference count is a 16-bit field, initialized to zero. It is not  /* This function is called (optionally) at the start of compile or match, to
59  possible to transfer a non-zero count from one host to a different host that  check that a supposed UTF-32 string is actually valid. The early check means
60  has a different byte order - though I can't see why anyone in their right mind  that subsequent code can assume it is dealing with a valid string. The check
61  would ever want to do that!  can be turned off for maximum performance, but the consequences of supplying an
62    invalid string are then undefined.
63    
64    More information about the details of the error are passed
65    back in the returned value:
66    
67    PCRE_UTF32_ERR0  No error
68    PCRE_UTF32_ERR1  Surrogate character
69    PCRE_UTF32_ERR2  Non-character
70    PCRE_UTF32_ERR3  Character > 0x10ffff
71    
72  Arguments:  Arguments:
73    argument_re   points to compiled code    string       points to the string
74    adjust        value to add to the count    length       length of string, or -1 if the string is zero-terminated
75      errp         pointer to an error position offset variable
76    
77  Returns:        the (possibly updated) count value (a non-negative number), or  Returns:       = 0    if the string is a valid UTF-32 string
78                  a negative error number                 > 0    otherwise, setting the offset of the bad character
79  */  */
80    
81  #ifdef COMPILE_PCRE8  int
82  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PRIV(valid_utf)(PCRE_PUCHAR string, int length, int *erroroffset)
 pcre_refcount(pcre *argument_re, int adjust)  
 #else  
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  
 pcre_refcount(pcre *argument_re, int adjust)  
 #endif  
83  {  {
84  real_pcre *re = (real_pcre *)argument_re;  #ifdef SUPPORT_UTF
85  if (re == NULL) return PCRE_ERROR_NULL;  register PCRE_PUCHAR p;
86  if (re->magic_number != MAGIC_NUMBER) return PCRE_ERROR_BADMAGIC;  register pcre_uchar c;
87  if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;  
88  re->ref_count = (-adjust > re->ref_count)? 0 :  if (length < 0)
89                  (adjust + re->ref_count > 65535)? 65535 :    {
90                  re->ref_count + adjust;    for (p = string; *p != 0; p++);
91  return re->ref_count;    length = p - string;
92      }
93    
94    for (p = string; length-- > 0; p++)
95      {
96      c = *p;
97    
98      if ((c & 0xfffff800u) != 0xd800u)
99        {
100        /* Normal UTF-32 code point. Neither high nor low surrogate. */
101    
102        /* Check for non-characters */
103        if ((c & 0xfffeu) == 0xfffeu ||
104            c >= 0xfdd0u && c <= 0xfdefu)
105          {
106          *erroroffset = p - string;
107          return PCRE_UTF32_ERR2;
108          }
109        else if (c > 0x10ffffu)
110          {
111          *erroroffset = p - string;
112          return PCRE_UTF32_ERR3;
113          }
114        }
115      else
116        {
117        /* A surrogate */
118        *erroroffset = p - string;
119        return PCRE_UTF32_ERR1;
120        }
121      }
122    
123    #else  /* SUPPORT_UTF */
124    (void)(string);  /* Keep picky compilers happy */
125    (void)(length);
126    #endif /* SUPPORT_UTF */
127    
128    return PCRE_UTF32_ERR0;   /* This indicates success */
129  }  }
130    
131  /* End of pcre_refcount.c */  /* End of pcre32_valid_utf32.c */

Legend:
Removed from v.836  
changed lines
  Added in v.1118

  ViewVC Help
Powered by ViewVC 1.1.5