123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include "pcre_internal.h"
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre_get_stringnumber(const pcre *code, const char *stringname)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre32_get_stringnumber(const pcre32 *code, PCRE_SPTR32 stringname)
- #endif
- {
- int rc;
- int entrysize;
- int top, bot;
- pcre_uchar *nametable;
- #ifdef COMPILE_PCRE8
- if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
- return rc;
- if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
- if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
- return rc;
- if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
- return rc;
- #endif
- #ifdef COMPILE_PCRE16
- if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
- return rc;
- if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
- if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
- return rc;
- if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
- return rc;
- #endif
- #ifdef COMPILE_PCRE32
- if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
- return rc;
- if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
- if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
- return rc;
- if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
- return rc;
- #endif
- bot = 0;
- while (top > bot)
- {
- int mid = (top + bot) / 2;
- pcre_uchar *entry = nametable + entrysize*mid;
- int c = STRCMP_UC_UC((pcre_uchar *)stringname,
- (pcre_uchar *)(entry + IMM2_SIZE));
- if (c == 0) return GET2(entry, 0);
- if (c > 0) bot = mid + 1; else top = mid;
- }
- return PCRE_ERROR_NOSUBSTRING;
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre_get_stringtable_entries(const pcre *code, const char *stringname,
- char **firstptr, char **lastptr)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname,
- PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre32_get_stringtable_entries(const pcre32 *code, PCRE_SPTR32 stringname,
- PCRE_UCHAR32 **firstptr, PCRE_UCHAR32 **lastptr)
- #endif
- {
- int rc;
- int entrysize;
- int top, bot;
- pcre_uchar *nametable, *lastentry;
- #ifdef COMPILE_PCRE8
- if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
- return rc;
- if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
- if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
- return rc;
- if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
- return rc;
- #endif
- #ifdef COMPILE_PCRE16
- if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
- return rc;
- if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
- if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
- return rc;
- if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
- return rc;
- #endif
- #ifdef COMPILE_PCRE32
- if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
- return rc;
- if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
- if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
- return rc;
- if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
- return rc;
- #endif
- lastentry = nametable + entrysize * (top - 1);
- bot = 0;
- while (top > bot)
- {
- int mid = (top + bot) / 2;
- pcre_uchar *entry = nametable + entrysize*mid;
- int c = STRCMP_UC_UC((pcre_uchar *)stringname,
- (pcre_uchar *)(entry + IMM2_SIZE));
- if (c == 0)
- {
- pcre_uchar *first = entry;
- pcre_uchar *last = entry;
- while (first > nametable)
- {
- if (STRCMP_UC_UC((pcre_uchar *)stringname,
- (pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break;
- first -= entrysize;
- }
- while (last < lastentry)
- {
- if (STRCMP_UC_UC((pcre_uchar *)stringname,
- (pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break;
- last += entrysize;
- }
- #if defined COMPILE_PCRE8
- *firstptr = (char *)first;
- *lastptr = (char *)last;
- #elif defined COMPILE_PCRE16
- *firstptr = (PCRE_UCHAR16 *)first;
- *lastptr = (PCRE_UCHAR16 *)last;
- #elif defined COMPILE_PCRE32
- *firstptr = (PCRE_UCHAR32 *)first;
- *lastptr = (PCRE_UCHAR32 *)last;
- #endif
- return entrysize;
- }
- if (c > 0) bot = mid + 1; else top = mid;
- }
- return PCRE_ERROR_NOSUBSTRING;
- }
- #if defined COMPILE_PCRE8
- static int
- get_first_set(const pcre *code, const char *stringname, int *ovector,
- int stringcount)
- #elif defined COMPILE_PCRE16
- static int
- get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector,
- int stringcount)
- #elif defined COMPILE_PCRE32
- static int
- get_first_set(const pcre32 *code, PCRE_SPTR32 stringname, int *ovector,
- int stringcount)
- #endif
- {
- const REAL_PCRE *re = (const REAL_PCRE *)code;
- int entrysize;
- pcre_uchar *entry;
- #if defined COMPILE_PCRE8
- char *first, *last;
- #elif defined COMPILE_PCRE16
- PCRE_UCHAR16 *first, *last;
- #elif defined COMPILE_PCRE32
- PCRE_UCHAR32 *first, *last;
- #endif
- #if defined COMPILE_PCRE8
- if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
- return pcre_get_stringnumber(code, stringname);
- entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
- #elif defined COMPILE_PCRE16
- if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
- return pcre16_get_stringnumber(code, stringname);
- entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last);
- #elif defined COMPILE_PCRE32
- if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
- return pcre32_get_stringnumber(code, stringname);
- entrysize = pcre32_get_stringtable_entries(code, stringname, &first, &last);
- #endif
- if (entrysize <= 0) return entrysize;
- for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize)
- {
- int n = GET2(entry, 0);
- if (n < stringcount && ovector[n*2] >= 0) return n;
- }
- return GET2(entry, 0);
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre_copy_substring(const char *subject, int *ovector, int stringcount,
- int stringnumber, char *buffer, int size)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
- int stringnumber, PCRE_UCHAR16 *buffer, int size)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre32_copy_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
- int stringnumber, PCRE_UCHAR32 *buffer, int size)
- #endif
- {
- int yield;
- if (stringnumber < 0 || stringnumber >= stringcount)
- return PCRE_ERROR_NOSUBSTRING;
- stringnumber *= 2;
- yield = ovector[stringnumber+1] - ovector[stringnumber];
- if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
- memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield));
- buffer[yield] = 0;
- return yield;
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre_copy_named_substring(const pcre *code, const char *subject,
- int *ovector, int stringcount, const char *stringname,
- char *buffer, int size)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
- int *ovector, int stringcount, PCRE_SPTR16 stringname,
- PCRE_UCHAR16 *buffer, int size)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre32_copy_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
- int *ovector, int stringcount, PCRE_SPTR32 stringname,
- PCRE_UCHAR32 *buffer, int size)
- #endif
- {
- int n = get_first_set(code, stringname, ovector, stringcount);
- if (n <= 0) return n;
- #if defined COMPILE_PCRE8
- return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
- #elif defined COMPILE_PCRE16
- return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size);
- #elif defined COMPILE_PCRE32
- return pcre32_copy_substring(subject, ovector, stringcount, n, buffer, size);
- #endif
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
- const char ***listptr)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount,
- PCRE_SPTR16 **listptr)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre32_get_substring_list(PCRE_SPTR32 subject, int *ovector, int stringcount,
- PCRE_SPTR32 **listptr)
- #endif
- {
- int i;
- int size = sizeof(pcre_uchar *);
- int double_count = stringcount * 2;
- pcre_uchar **stringlist;
- pcre_uchar *p;
- for (i = 0; i < double_count; i += 2)
- {
- size += sizeof(pcre_uchar *) + IN_UCHARS(1);
- if (ovector[i+1] > ovector[i]) size += IN_UCHARS(ovector[i+1] - ovector[i]);
- }
- stringlist = (pcre_uchar **)(PUBL(malloc))(size);
- if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
- #if defined COMPILE_PCRE8
- *listptr = (const char **)stringlist;
- #elif defined COMPILE_PCRE16
- *listptr = (PCRE_SPTR16 *)stringlist;
- #elif defined COMPILE_PCRE32
- *listptr = (PCRE_SPTR32 *)stringlist;
- #endif
- p = (pcre_uchar *)(stringlist + stringcount + 1);
- for (i = 0; i < double_count; i += 2)
- {
- int len = (ovector[i+1] > ovector[i])? (ovector[i+1] - ovector[i]) : 0;
- memcpy(p, subject + ovector[i], IN_UCHARS(len));
- *stringlist++ = p;
- p += len;
- *p++ = 0;
- }
- *stringlist = NULL;
- return 0;
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
- pcre_free_substring_list(const char **pointer)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
- pcre16_free_substring_list(PCRE_SPTR16 *pointer)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
- pcre32_free_substring_list(PCRE_SPTR32 *pointer)
- #endif
- {
- (PUBL(free))((void *)pointer);
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre_get_substring(const char *subject, int *ovector, int stringcount,
- int stringnumber, const char **stringptr)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
- int stringnumber, PCRE_SPTR16 *stringptr)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre32_get_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
- int stringnumber, PCRE_SPTR32 *stringptr)
- #endif
- {
- int yield;
- pcre_uchar *substring;
- if (stringnumber < 0 || stringnumber >= stringcount)
- return PCRE_ERROR_NOSUBSTRING;
- stringnumber *= 2;
- yield = ovector[stringnumber+1] - ovector[stringnumber];
- substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1));
- if (substring == NULL) return PCRE_ERROR_NOMEMORY;
- memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield));
- substring[yield] = 0;
- #if defined COMPILE_PCRE8
- *stringptr = (const char *)substring;
- #elif defined COMPILE_PCRE16
- *stringptr = (PCRE_SPTR16)substring;
- #elif defined COMPILE_PCRE32
- *stringptr = (PCRE_SPTR32)substring;
- #endif
- return yield;
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre_get_named_substring(const pcre *code, const char *subject,
- int *ovector, int stringcount, const char *stringname,
- const char **stringptr)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
- int *ovector, int stringcount, PCRE_SPTR16 stringname,
- PCRE_SPTR16 *stringptr)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
- pcre32_get_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
- int *ovector, int stringcount, PCRE_SPTR32 stringname,
- PCRE_SPTR32 *stringptr)
- #endif
- {
- int n = get_first_set(code, stringname, ovector, stringcount);
- if (n <= 0) return n;
- #if defined COMPILE_PCRE8
- return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
- #elif defined COMPILE_PCRE16
- return pcre16_get_substring(subject, ovector, stringcount, n, stringptr);
- #elif defined COMPILE_PCRE32
- return pcre32_get_substring(subject, ovector, stringcount, n, stringptr);
- #endif
- }
- #if defined COMPILE_PCRE8
- PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
- pcre_free_substring(const char *pointer)
- #elif defined COMPILE_PCRE16
- PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
- pcre16_free_substring(PCRE_SPTR16 pointer)
- #elif defined COMPILE_PCRE32
- PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
- pcre32_free_substring(PCRE_SPTR32 pointer)
- #endif
- {
- (PUBL(free))((void *)pointer);
- }
|