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

Diff of /code/trunk/pcre_get.c

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

revision 236 by ph10, Tue Sep 11 12:57:06 2007 UTC revision 883 by zherczeg, Mon Jan 16 08:35:42 2012 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2007 University of Cambridge             Copyright (c) 1997-2012 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 65  Returns:      the number of the named pa Line 65  Returns:      the number of the named pa
65                  (PCRE_ERROR_NOSUBSTRING) if not found                  (PCRE_ERROR_NOSUBSTRING) if not found
66  */  */
67    
68  int  #ifdef COMPILE_PCRE8
69    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
70  pcre_get_stringnumber(const pcre *code, const char *stringname)  pcre_get_stringnumber(const pcre *code, const char *stringname)
71    #else
72    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
73    pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname)
74    #endif
75  {  {
76  int rc;  int rc;
77  int entrysize;  int entrysize;
78  int top, bot;  int top, bot;
79  uschar *nametable;  pcre_uchar *nametable;
80    
81    #ifdef COMPILE_PCRE8
82  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
83    return rc;    return rc;
84  if (top <= 0) return PCRE_ERROR_NOSUBSTRING;  if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
# Line 81  if ((rc = pcre_fullinfo(code, NULL, PCRE Line 87  if ((rc = pcre_fullinfo(code, NULL, PCRE
87    return rc;    return rc;
88  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
89    return rc;    return rc;
90    #endif
91    #ifdef COMPILE_PCRE16
92    if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
93      return rc;
94    if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
95    
96    if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
97      return rc;
98    if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
99      return rc;
100    #endif
101    
102  bot = 0;  bot = 0;
103  while (top > bot)  while (top > bot)
104    {    {
105    int mid = (top + bot) / 2;    int mid = (top + bot) / 2;
106    uschar *entry = nametable + entrysize*mid;    pcre_uchar *entry = nametable + entrysize*mid;
107    int c = strcmp(stringname, (char *)(entry + 2));    int c = STRCMP_UC_UC((pcre_uchar *)stringname,
108    if (c == 0) return (entry[0] << 8) + entry[1];      (pcre_uchar *)(entry + IMM2_SIZE));
109      if (c == 0) return GET2(entry, 0);
110    if (c > 0) bot = mid + 1; else top = mid;    if (c > 0) bot = mid + 1; else top = mid;
111    }    }
112    
# Line 114  Returns:      the length of each entry, Line 132  Returns:      the length of each entry,
132                  (PCRE_ERROR_NOSUBSTRING) if not found                  (PCRE_ERROR_NOSUBSTRING) if not found
133  */  */
134    
135  int  #ifdef COMPILE_PCRE8
136    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
137  pcre_get_stringtable_entries(const pcre *code, const char *stringname,  pcre_get_stringtable_entries(const pcre *code, const char *stringname,
138    char **firstptr, char **lastptr)    char **firstptr, char **lastptr)
139    #else
140    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
141    pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname,
142      PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr)
143    #endif
144  {  {
145  int rc;  int rc;
146  int entrysize;  int entrysize;
147  int top, bot;  int top, bot;
148  uschar *nametable, *lastentry;  pcre_uchar *nametable, *lastentry;
149    
150    #ifdef COMPILE_PCRE8
151  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
152    return rc;    return rc;
153  if (top <= 0) return PCRE_ERROR_NOSUBSTRING;  if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
# Line 131  if ((rc = pcre_fullinfo(code, NULL, PCRE Line 156  if ((rc = pcre_fullinfo(code, NULL, PCRE
156    return rc;    return rc;
157  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
158    return rc;    return rc;
159    #endif
160    #ifdef COMPILE_PCRE16
161    if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
162      return rc;
163    if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
164    
165    if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
166      return rc;
167    if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
168      return rc;
169    #endif
170    
171  lastentry = nametable + entrysize * (top - 1);  lastentry = nametable + entrysize * (top - 1);
172  bot = 0;  bot = 0;
173  while (top > bot)  while (top > bot)
174    {    {
175    int mid = (top + bot) / 2;    int mid = (top + bot) / 2;
176    uschar *entry = nametable + entrysize*mid;    pcre_uchar *entry = nametable + entrysize*mid;
177    int c = strcmp(stringname, (char *)(entry + 2));    int c = STRCMP_UC_UC((pcre_uchar *)stringname,
178        (pcre_uchar *)(entry + IMM2_SIZE));
179    if (c == 0)    if (c == 0)
180      {      {
181      uschar *first = entry;      pcre_uchar *first = entry;
182      uschar *last = entry;      pcre_uchar *last = entry;
183      while (first > nametable)      while (first > nametable)
184        {        {
185        if (strcmp(stringname, (char *)(first - entrysize + 2)) != 0) break;        if (STRCMP_UC_UC((pcre_uchar *)stringname,
186            (pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break;
187        first -= entrysize;        first -= entrysize;
188        }        }
189      while (last < lastentry)      while (last < lastentry)
190        {        {
191        if (strcmp(stringname, (char *)(last + entrysize + 2)) != 0) break;        if (STRCMP_UC_UC((pcre_uchar *)stringname,
192            (pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break;
193        last += entrysize;        last += entrysize;
194        }        }
195    #ifdef COMPILE_PCRE8
196      *firstptr = (char *)first;      *firstptr = (char *)first;
197      *lastptr = (char *)last;      *lastptr = (char *)last;
198    #else
199        *firstptr = (PCRE_UCHAR16 *)first;
200        *lastptr = (PCRE_UCHAR16 *)last;
201    #endif
202      return entrysize;      return entrysize;
203      }      }
204    if (c > 0) bot = mid + 1; else top = mid;    if (c > 0) bot = mid + 1; else top = mid;
# Line 182  Returns:       the number of the first t Line 226  Returns:       the number of the first t
226                 or a negative number on error                 or a negative number on error
227  */  */
228    
229    #ifdef COMPILE_PCRE8
230  static int  static int
231  get_first_set(const pcre *code, const char *stringname, int *ovector)  get_first_set(const pcre *code, const char *stringname, int *ovector)
232    #else
233    static int
234    get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector)
235    #endif
236  {  {
237  const real_pcre *re = (const real_pcre *)code;  const REAL_PCRE *re = (const REAL_PCRE *)code;
238  int entrysize;  int entrysize;
239    pcre_uchar *entry;
240    #ifdef COMPILE_PCRE8
241  char *first, *last;  char *first, *last;
242  uschar *entry;  #else
243    PCRE_UCHAR16 *first, *last;
244    #endif
245    
246    #ifdef COMPILE_PCRE8
247  if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)  if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
248    return pcre_get_stringnumber(code, stringname);    return pcre_get_stringnumber(code, stringname);
249  entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);  entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
250    #else
251    if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
252      return pcre16_get_stringnumber(code, stringname);
253    entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last);
254    #endif
255  if (entrysize <= 0) return entrysize;  if (entrysize <= 0) return entrysize;
256  for (entry = (uschar *)first; entry <= (uschar *)last; entry += entrysize)  for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize)
257    {    {
258    int n = (entry[0] << 8) + entry[1];    int n = GET2(entry, 0);
259    if (ovector[n*2] >= 0) return n;    if (ovector[n*2] >= 0) return n;
260    }    }
261  return (first[0] << 8) + first[1];  return GET2(entry, 0);
262  }  }
263    
264    
# Line 231  Returns:         if successful: Line 291  Returns:         if successful:
291                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
292  */  */
293    
294  int  #ifdef COMPILE_PCRE8
295    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
296  pcre_copy_substring(const char *subject, int *ovector, int stringcount,  pcre_copy_substring(const char *subject, int *ovector, int stringcount,
297    int stringnumber, char *buffer, int size)    int stringnumber, char *buffer, int size)
298    #else
299    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
300    pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
301      int stringnumber, PCRE_UCHAR16 *buffer, int size)
302    #endif
303  {  {
304  int yield;  int yield;
305  if (stringnumber < 0 || stringnumber >= stringcount)  if (stringnumber < 0 || stringnumber >= stringcount)
# Line 241  if (stringnumber < 0 || stringnumber >= Line 307  if (stringnumber < 0 || stringnumber >=
307  stringnumber *= 2;  stringnumber *= 2;
308  yield = ovector[stringnumber+1] - ovector[stringnumber];  yield = ovector[stringnumber+1] - ovector[stringnumber];
309  if (size < yield + 1) return PCRE_ERROR_NOMEMORY;  if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
310  memcpy(buffer, subject + ovector[stringnumber], yield);  memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield));
311  buffer[yield] = 0;  buffer[yield] = 0;
312  return yield;  return yield;
313  }  }
# Line 276  Returns:         if successful: Line 342  Returns:         if successful:
342                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
343  */  */
344    
345  int  #ifdef COMPILE_PCRE8
346  pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector,  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
347    int stringcount, const char *stringname, char *buffer, int size)  pcre_copy_named_substring(const pcre *code, const char *subject,
348      int *ovector, int stringcount, const char *stringname,
349      char *buffer, int size)
350    #else
351    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
352    pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
353      int *ovector, int stringcount, PCRE_SPTR16 stringname,
354      PCRE_UCHAR16 *buffer, int size)
355    #endif
356  {  {
357  int n = get_first_set(code, stringname, ovector);  int n = get_first_set(code, stringname, ovector);
358  if (n <= 0) return n;  if (n <= 0) return n;
359    #ifdef COMPILE_PCRE8
360  return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);  return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
361    #else
362    return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size);
363    #endif
364  }  }
365    
366    
# Line 308  Returns:         if successful: 0 Line 386  Returns:         if successful: 0
386                     PCRE_ERROR_NOMEMORY (-6) failed to get store                     PCRE_ERROR_NOMEMORY (-6) failed to get store
387  */  */
388    
389  int  #ifdef COMPILE_PCRE8
390    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
391  pcre_get_substring_list(const char *subject, int *ovector, int stringcount,  pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
392    const char ***listptr)    const char ***listptr)
393    #else
394    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
395    pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount,
396      PCRE_SPTR16 **listptr)
397    #endif
398  {  {
399  int i;  int i;
400  int size = sizeof(char *);  int size = sizeof(pcre_uchar *);
401  int double_count = stringcount * 2;  int double_count = stringcount * 2;
402  char **stringlist;  pcre_uchar **stringlist;
403  char *p;  pcre_uchar *p;
404    
405  for (i = 0; i < double_count; i += 2)  for (i = 0; i < double_count; i += 2)
406    size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;    size += sizeof(pcre_uchar *) + IN_UCHARS(ovector[i+1] - ovector[i] + 1);
407    
408  stringlist = (char **)(pcre_malloc)(size);  stringlist = (pcre_uchar **)(PUBL(malloc))(size);
409  if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;  if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
410    
411    #ifdef COMPILE_PCRE8
412  *listptr = (const char **)stringlist;  *listptr = (const char **)stringlist;
413  p = (char *)(stringlist + stringcount + 1);  #else
414    *listptr = (PCRE_SPTR16 *)stringlist;
415    #endif
416    p = (pcre_uchar *)(stringlist + stringcount + 1);
417    
418  for (i = 0; i < double_count; i += 2)  for (i = 0; i < double_count; i += 2)
419    {    {
420    int len = ovector[i+1] - ovector[i];    int len = ovector[i+1] - ovector[i];
421    memcpy(p, subject + ovector[i], len);    memcpy(p, subject + ovector[i], IN_UCHARS(len));
422    *stringlist++ = p;    *stringlist++ = p;
423    p += len;    p += len;
424    *p++ = 0;    *p++ = 0;
# Line 347  return 0; Line 435  return 0;
435  *************************************************/  *************************************************/
436    
437  /* This function exists for the benefit of people calling PCRE from non-C  /* This function exists for the benefit of people calling PCRE from non-C
438  programs that can call its functions, but not free() or (pcre_free)() directly.  programs that can call its functions, but not free() or (PUBL(free))()
439    directly.
440    
441  Argument:   the result of a previous pcre_get_substring_list()  Argument:   the result of a previous pcre_get_substring_list()
442  Returns:    nothing  Returns:    nothing
443  */  */
444    
445  void  #ifdef COMPILE_PCRE8
446    PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
447  pcre_free_substring_list(const char **pointer)  pcre_free_substring_list(const char **pointer)
448    #else
449    PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
450    pcre16_free_substring_list(PCRE_SPTR16 *pointer)
451    #endif
452  {  {
453  (pcre_free)((void *)pointer);  (PUBL(free))((void *)pointer);
454  }  }
455    
456    
# Line 386  Returns:         if successful: Line 480  Returns:         if successful:
480                     PCRE_ERROR_NOSUBSTRING (-7) substring not present                     PCRE_ERROR_NOSUBSTRING (-7) substring not present
481  */  */
482    
483  int  #ifdef COMPILE_PCRE8
484    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
485  pcre_get_substring(const char *subject, int *ovector, int stringcount,  pcre_get_substring(const char *subject, int *ovector, int stringcount,
486    int stringnumber, const char **stringptr)    int stringnumber, const char **stringptr)
487    #else
488    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
489    pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
490      int stringnumber, PCRE_SPTR16 *stringptr)
491    #endif
492  {  {
493  int yield;  int yield;
494  char *substring;  pcre_uchar *substring;
495  if (stringnumber < 0 || stringnumber >= stringcount)  if (stringnumber < 0 || stringnumber >= stringcount)
496    return PCRE_ERROR_NOSUBSTRING;    return PCRE_ERROR_NOSUBSTRING;
497  stringnumber *= 2;  stringnumber *= 2;
498  yield = ovector[stringnumber+1] - ovector[stringnumber];  yield = ovector[stringnumber+1] - ovector[stringnumber];
499  substring = (char *)(pcre_malloc)(yield + 1);  substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1));
500  if (substring == NULL) return PCRE_ERROR_NOMEMORY;  if (substring == NULL) return PCRE_ERROR_NOMEMORY;
501  memcpy(substring, subject + ovector[stringnumber], yield);  memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield));
502  substring[yield] = 0;  substring[yield] = 0;
503  *stringptr = substring;  #ifdef COMPILE_PCRE8
504    *stringptr = (const char *)substring;
505    #else
506    *stringptr = (PCRE_SPTR16)substring;
507    #endif
508  return yield;  return yield;
509  }  }
510    
# Line 433  Returns:         if successful: Line 537  Returns:         if successful:
537                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
538  */  */
539    
540  int  #ifdef COMPILE_PCRE8
541  pcre_get_named_substring(const pcre *code, const char *subject, int *ovector,  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
542    int stringcount, const char *stringname, const char **stringptr)  pcre_get_named_substring(const pcre *code, const char *subject,
543      int *ovector, int stringcount, const char *stringname,
544      const char **stringptr)
545    #else
546    PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
547    pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
548      int *ovector, int stringcount, PCRE_SPTR16 stringname,
549      PCRE_SPTR16 *stringptr)
550    #endif
551  {  {
552  int n = get_first_set(code, stringname, ovector);  int n = get_first_set(code, stringname, ovector);
553  if (n <= 0) return n;  if (n <= 0) return n;
554    #ifdef COMPILE_PCRE8
555  return pcre_get_substring(subject, ovector, stringcount, n, stringptr);  return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
556    #else
557    return pcre16_get_substring(subject, ovector, stringcount, n, stringptr);
558    #endif
559  }  }
560    
561    
# Line 450  return pcre_get_substring(subject, ovect Line 566  return pcre_get_substring(subject, ovect
566  *************************************************/  *************************************************/
567    
568  /* This function exists for the benefit of people calling PCRE from non-C  /* This function exists for the benefit of people calling PCRE from non-C
569  programs that can call its functions, but not free() or (pcre_free)() directly.  programs that can call its functions, but not free() or (PUBL(free))()
570    directly.
571    
572  Argument:   the result of a previous pcre_get_substring()  Argument:   the result of a previous pcre_get_substring()
573  Returns:    nothing  Returns:    nothing
574  */  */
575    
576  void  #ifdef COMPILE_PCRE8
577    PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
578  pcre_free_substring(const char *pointer)  pcre_free_substring(const char *pointer)
579    #else
580    PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
581    pcre16_free_substring(PCRE_SPTR16 pointer)
582    #endif
583  {  {
584  (pcre_free)((void *)pointer);  (PUBL(free))((void *)pointer);
585  }  }
586    
587  /* End of pcre_get.c */  /* End of pcre_get.c */

Legend:
Removed from v.236  
changed lines
  Added in v.883

  ViewVC Help
Powered by ViewVC 1.1.5