Blame support/regex_internal.h

Packit 575503
/* Extended regular expression matching and search library.
Packit 575503
   Copyright (C) 2002-2017 Free Software Foundation, Inc.
Packit 575503
   This file is part of the GNU C Library.
Packit 575503
   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
Packit 575503
Packit 575503
   The GNU C Library is free software; you can redistribute it and/or
Packit 575503
   modify it under the terms of the GNU Lesser General Public
Packit 575503
   License as published by the Free Software Foundation; either
Packit 575503
   version 2.1 of the License, or (at your option) any later version.
Packit 575503
Packit 575503
   The GNU C Library is distributed in the hope that it will be useful,
Packit 575503
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 575503
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 575503
   Lesser General Public License for more details.
Packit 575503
Packit 575503
   You should have received a copy of the GNU Lesser General Public
Packit 575503
   License along with the GNU C Library; if not, see
Packit 575503
   <http://www.gnu.org/licenses/>.  */
Packit 575503
Packit 575503
#ifndef _REGEX_INTERNAL_H
Packit 575503
#define _REGEX_INTERNAL_H 1
Packit 575503
Packit 575503
#include <assert.h>
Packit 575503
#include <ctype.h>
Packit 575503
#include <stdio.h>
Packit 575503
#include <stdlib.h>
Packit 575503
#include <string.h>
Packit 575503
Packit 575503
#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
Packit 575503
# include <langinfo.h>
Packit 575503
#endif
Packit 575503
#if defined HAVE_LOCALE_H || defined _LIBC
Packit 575503
# include <locale.h>
Packit 575503
#endif
Packit 575503
#if defined HAVE_WCHAR_H || defined _LIBC
Packit 575503
# include <wchar.h>
Packit 575503
#endif /* HAVE_WCHAR_H || _LIBC */
Packit 575503
#if defined HAVE_WCTYPE_H || defined _LIBC
Packit 575503
# include <wctype.h>
Packit 575503
#endif /* HAVE_WCTYPE_H || _LIBC */
Packit 575503
#if defined HAVE_STDBOOL_H || defined _LIBC
Packit 575503
# include <stdbool.h>
Packit 575503
#endif /* HAVE_STDBOOL_H || _LIBC */
Packit 575503
#if defined HAVE_STDINT_H || defined _LIBC
Packit 575503
# include <stdint.h>
Packit 575503
#endif /* HAVE_STDINT_H || _LIBC */
Packit 575503
#if defined _LIBC
Packit 575503
# include <libc-lock.h>
Packit 575503
#else
Packit 575503
# define __libc_lock_init(NAME) do { } while (0)
Packit 575503
# define __libc_lock_lock(NAME) do { } while (0)
Packit 575503
# define __libc_lock_unlock(NAME) do { } while (0)
Packit 575503
#endif
Packit 575503
Packit 575503
#ifndef GAWK
Packit 575503
/* In case that the system doesn't have isblank().  */
Packit 575503
#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
Packit 575503
# define isblank(ch) ((ch) == ' ' || (ch) == '\t')
Packit 575503
#endif
Packit 575503
#else /* GAWK */
Packit 575503
/*
Packit 575503
 * This is a freaking mess. On glibc systems you have to define
Packit 575503
 * a magic constant to get isblank() out of <ctype.h>, since it's
Packit 575503
 * a C99 function.  To heck with all that and borrow a page from
Packit 575503
 * dfa.c's book.
Packit 575503
 */
Packit 575503
Packit 575503
static int
Packit 575503
is_blank (int c)
Packit 575503
{
Packit 575503
   return (c == ' ' || c == '\t');
Packit 575503
}
Packit 575503
#endif /* GAWK */
Packit 575503
Packit 575503
#ifdef _LIBC
Packit 575503
# ifndef _RE_DEFINE_LOCALE_FUNCTIONS
Packit 575503
#  define _RE_DEFINE_LOCALE_FUNCTIONS 1
Packit 575503
#   include <locale/localeinfo.h>
Packit 575503
#   include <locale/coll-lookup.h>
Packit 575503
# endif
Packit 575503
#endif
Packit 575503
Packit 575503
/* This is for other GNU distributions with internationalized messages.  */
Packit 575503
#if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
Packit 575503
# include <libintl.h>
Packit 575503
# ifdef _LIBC
Packit 575503
#  undef gettext
Packit 575503
#  define gettext(msgid) \
Packit 575503
  __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES)
Packit 575503
# endif
Packit 575503
#else
Packit 575503
# define gettext(msgid) (msgid)
Packit 575503
#endif
Packit 575503
Packit 575503
#ifndef gettext_noop
Packit 575503
/* This define is so xgettext can find the internationalizable
Packit 575503
   strings.  */
Packit 575503
# define gettext_noop(String) String
Packit 575503
#endif
Packit 575503
Packit 575503
/* For loser systems without the definition.  */
Packit 575503
#ifndef SIZE_MAX
Packit 575503
# define SIZE_MAX ((size_t) -1)
Packit 575503
#endif
Packit 575503
Packit 575503
#if ! defined(__DJGPP__) && (defined(GAWK) || _LIBC)
Packit 575503
# define RE_ENABLE_I18N
Packit 575503
#endif
Packit 575503
Packit 575503
#if __GNUC__ >= 3
Packit 575503
# define BE(expr, val) __builtin_expect (expr, val)
Packit 575503
#else
Packit 575503
# define BE(expr, val) (expr)
Packit 575503
# ifdef inline
Packit 575503
# undef inline
Packit 575503
# endif
Packit 575503
# define inline
Packit 575503
#endif
Packit 575503
Packit 575503
/* Number of single byte character.  */
Packit 575503
#define SBC_MAX 256
Packit 575503
Packit 575503
#define COLL_ELEM_LEN_MAX 8
Packit 575503
Packit 575503
/* The character which represents newline.  */
Packit 575503
#define NEWLINE_CHAR '\n'
Packit 575503
#define WIDE_NEWLINE_CHAR L'\n'
Packit 575503
Packit 575503
/* Rename to standard API for using out of glibc.  */
Packit 575503
#ifndef _LIBC
Packit 575503
# ifdef __wctype
Packit 575503
# undef __wctype
Packit 575503
# endif
Packit 575503
# define __wctype wctype
Packit 575503
# ifdef __iswctype
Packit 575503
# undef __iswctype
Packit 575503
# endif
Packit 575503
# define __iswctype iswctype
Packit 575503
# define __btowc btowc
Packit 575503
# define __mbrtowc mbrtowc
Packit 575503
#undef __mempcpy	/* GAWK */
Packit 575503
# define __mempcpy mempcpy
Packit 575503
# define __wcrtomb wcrtomb
Packit 575503
# define __regfree regfree
Packit 575503
#endif /* not _LIBC */
Packit 575503
Packit 575503
#if __GNUC__ < 3 + (__GNUC_MINOR__ < 1)
Packit 575503
# define __attribute__(arg)
Packit 575503
#endif
Packit 575503
Packit 575503
#ifdef GAWK
Packit 575503
/*
Packit 575503
 * Instead of trying to figure out which GCC version introduced
Packit 575503
 * this symbol, just define it out and be done.
Packit 575503
 */
Packit 575503
# undef __attribute_warn_unused_result__
Packit 575503
# define __attribute_warn_unused_result__
Packit 575503
#endif
Packit 575503
Packit 575503
/* An integer used to represent a set of bits.  It must be unsigned,
Packit 575503
   and must be at least as wide as unsigned int.  */
Packit 575503
typedef unsigned long int bitset_word_t;
Packit 575503
/* All bits set in a bitset_word_t.  */
Packit 575503
#define BITSET_WORD_MAX ULONG_MAX
Packit 575503
/* Number of bits in a bitset_word_t.  */
Packit 575503
#define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
Packit 575503
/* Number of bitset_word_t in a bit_set.  */
Packit 575503
#define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
Packit 575503
typedef bitset_word_t bitset_t[BITSET_WORDS];
Packit 575503
typedef bitset_word_t *re_bitset_ptr_t;
Packit 575503
typedef const bitset_word_t *re_const_bitset_ptr_t;
Packit 575503
Packit 575503
#define bitset_set(set,i) \
Packit 575503
  (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
Packit 575503
#define bitset_clear(set,i) \
Packit 575503
  (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
Packit 575503
#define bitset_contain(set,i) \
Packit 575503
  (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
Packit 575503
#define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
Packit 575503
#define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
Packit 575503
#define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
Packit 575503
Packit 575503
#define PREV_WORD_CONSTRAINT 0x0001
Packit 575503
#define PREV_NOTWORD_CONSTRAINT 0x0002
Packit 575503
#define NEXT_WORD_CONSTRAINT 0x0004
Packit 575503
#define NEXT_NOTWORD_CONSTRAINT 0x0008
Packit 575503
#define PREV_NEWLINE_CONSTRAINT 0x0010
Packit 575503
#define NEXT_NEWLINE_CONSTRAINT 0x0020
Packit 575503
#define PREV_BEGBUF_CONSTRAINT 0x0040
Packit 575503
#define NEXT_ENDBUF_CONSTRAINT 0x0080
Packit 575503
#define WORD_DELIM_CONSTRAINT 0x0100
Packit 575503
#define NOT_WORD_DELIM_CONSTRAINT 0x0200
Packit 575503
Packit 575503
typedef enum
Packit 575503
{
Packit 575503
  INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
Packit 575503
  WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
Packit 575503
  WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
Packit 575503
  INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
Packit 575503
  LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
Packit 575503
  LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
Packit 575503
  BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
Packit 575503
  BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
Packit 575503
  WORD_DELIM = WORD_DELIM_CONSTRAINT,
Packit 575503
  NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
Packit 575503
} re_context_type;
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  int alloc;
Packit 575503
  int nelem;
Packit 575503
  int *elems;
Packit 575503
} re_node_set;
Packit 575503
Packit 575503
typedef enum
Packit 575503
{
Packit 575503
  NON_TYPE = 0,
Packit 575503
Packit 575503
  /* Node type, These are used by token, node, tree.  */
Packit 575503
  CHARACTER = 1,
Packit 575503
  END_OF_RE = 2,
Packit 575503
  SIMPLE_BRACKET = 3,
Packit 575503
  OP_BACK_REF = 4,
Packit 575503
  OP_PERIOD = 5,
Packit 575503
#ifdef RE_ENABLE_I18N
Packit 575503
  COMPLEX_BRACKET = 6,
Packit 575503
  OP_UTF8_PERIOD = 7,
Packit 575503
#endif /* RE_ENABLE_I18N */
Packit 575503
Packit 575503
  /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
Packit 575503
     when the debugger shows values of this enum type.  */
Packit 575503
#define EPSILON_BIT 8
Packit 575503
  OP_OPEN_SUBEXP = EPSILON_BIT | 0,
Packit 575503
  OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
Packit 575503
  OP_ALT = EPSILON_BIT | 2,
Packit 575503
  OP_DUP_ASTERISK = EPSILON_BIT | 3,
Packit 575503
  ANCHOR = EPSILON_BIT | 4,
Packit 575503
Packit 575503
  /* Tree type, these are used only by tree. */
Packit 575503
  CONCAT = 16,
Packit 575503
  SUBEXP = 17,
Packit 575503
Packit 575503
  /* Token type, these are used only by token.  */
Packit 575503
  OP_DUP_PLUS = 18,
Packit 575503
  OP_DUP_QUESTION,
Packit 575503
  OP_OPEN_BRACKET,
Packit 575503
  OP_CLOSE_BRACKET,
Packit 575503
  OP_CHARSET_RANGE,
Packit 575503
  OP_OPEN_DUP_NUM,
Packit 575503
  OP_CLOSE_DUP_NUM,
Packit 575503
  OP_NON_MATCH_LIST,
Packit 575503
  OP_OPEN_COLL_ELEM,
Packit 575503
  OP_CLOSE_COLL_ELEM,
Packit 575503
  OP_OPEN_EQUIV_CLASS,
Packit 575503
  OP_CLOSE_EQUIV_CLASS,
Packit 575503
  OP_OPEN_CHAR_CLASS,
Packit 575503
  OP_CLOSE_CHAR_CLASS,
Packit 575503
  OP_WORD,
Packit 575503
  OP_NOTWORD,
Packit 575503
  OP_SPACE,
Packit 575503
  OP_NOTSPACE,
Packit 575503
  BACK_SLASH
Packit 575503
Packit 575503
} re_token_type_t;
Packit 575503
Packit 575503
#ifdef RE_ENABLE_I18N
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  /* Multibyte characters.  */
Packit 575503
  wchar_t *mbchars;
Packit 575503
Packit 575503
  /* Collating symbols.  */
Packit 575503
# ifdef _LIBC
Packit 575503
  int32_t *coll_syms;
Packit 575503
# endif
Packit 575503
Packit 575503
  /* Equivalence classes. */
Packit 575503
# ifdef _LIBC
Packit 575503
  int32_t *equiv_classes;
Packit 575503
# endif
Packit 575503
Packit 575503
  /* Range expressions. */
Packit 575503
# ifdef _LIBC
Packit 575503
  uint32_t *range_starts;
Packit 575503
  uint32_t *range_ends;
Packit 575503
# else /* not _LIBC */
Packit 575503
  wchar_t *range_starts;
Packit 575503
  wchar_t *range_ends;
Packit 575503
# endif /* not _LIBC */
Packit 575503
Packit 575503
  /* Character classes. */
Packit 575503
  wctype_t *char_classes;
Packit 575503
Packit 575503
  /* If this character set is the non-matching list.  */
Packit 575503
  unsigned int non_match : 1;
Packit 575503
Packit 575503
  /* # of multibyte characters.  */
Packit 575503
  int nmbchars;
Packit 575503
Packit 575503
  /* # of collating symbols.  */
Packit 575503
  int ncoll_syms;
Packit 575503
Packit 575503
  /* # of equivalence classes. */
Packit 575503
  int nequiv_classes;
Packit 575503
Packit 575503
  /* # of range expressions. */
Packit 575503
  int nranges;
Packit 575503
Packit 575503
  /* # of character classes. */
Packit 575503
  int nchar_classes;
Packit 575503
} re_charset_t;
Packit 575503
#endif /* RE_ENABLE_I18N */
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  union
Packit 575503
  {
Packit 575503
    unsigned char c;		/* for CHARACTER */
Packit 575503
    re_bitset_ptr_t sbcset;	/* for SIMPLE_BRACKET */
Packit 575503
#ifdef RE_ENABLE_I18N
Packit 575503
    re_charset_t *mbcset;	/* for COMPLEX_BRACKET */
Packit 575503
#endif /* RE_ENABLE_I18N */
Packit 575503
    int idx;			/* for BACK_REF */
Packit 575503
    re_context_type ctx_type;	/* for ANCHOR */
Packit 575503
  } opr;
Packit 575503
#if __GNUC__ >= 2
Packit 575503
  re_token_type_t type : 8;
Packit 575503
#else
Packit 575503
  re_token_type_t type;
Packit 575503
#endif
Packit 575503
  unsigned int constraint : 10;	/* context constraint */
Packit 575503
  unsigned int duplicated : 1;
Packit 575503
  unsigned int opt_subexp : 1;
Packit 575503
#ifdef RE_ENABLE_I18N
Packit 575503
  unsigned int accept_mb : 1;
Packit 575503
  /* These 2 bits can be moved into the union if needed (e.g. if running out
Packit 575503
     of bits; move opr.c to opr.c.c and move the flags to opr.c.flags).  */
Packit 575503
  unsigned int mb_partial : 1;
Packit 575503
#endif
Packit 575503
  unsigned int word_char : 1;
Packit 575503
} re_token_t;
Packit 575503
Packit 575503
#define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
Packit 575503
Packit 575503
struct re_string_t
Packit 575503
{
Packit 575503
  /* Indicate the raw buffer which is the original string passed as an
Packit 575503
     argument of regexec(), re_search(), etc..  */
Packit 575503
  const unsigned char *raw_mbs;
Packit 575503
  /* Store the multibyte string.  In case of "case insensitive mode" like
Packit 575503
     REG_ICASE, upper cases of the string are stored, otherwise MBS points
Packit 575503
     the same address that RAW_MBS points.  */
Packit 575503
  unsigned char *mbs;
Packit 575503
#ifdef RE_ENABLE_I18N
Packit 575503
  /* Store the wide character string which is corresponding to MBS.  */
Packit 575503
  wint_t *wcs;
Packit 575503
  int *offsets;
Packit 575503
  mbstate_t cur_state;
Packit 575503
#endif
Packit 575503
  /* Index in RAW_MBS.  Each character mbs[i] corresponds to
Packit 575503
     raw_mbs[raw_mbs_idx + i].  */
Packit 575503
  int raw_mbs_idx;
Packit 575503
  /* The length of the valid characters in the buffers.  */
Packit 575503
  int valid_len;
Packit 575503
  /* The corresponding number of bytes in raw_mbs array.  */
Packit 575503
  int valid_raw_len;
Packit 575503
  /* The length of the buffers MBS and WCS.  */
Packit 575503
  int bufs_len;
Packit 575503
  /* The index in MBS, which is updated by re_string_fetch_byte.  */
Packit 575503
  int cur_idx;
Packit 575503
  /* length of RAW_MBS array.  */
Packit 575503
  int raw_len;
Packit 575503
  /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN.  */
Packit 575503
  int len;
Packit 575503
  /* End of the buffer may be shorter than its length in the cases such
Packit 575503
     as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
Packit 575503
     instead of LEN.  */
Packit 575503
  int raw_stop;
Packit 575503
  /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS.  */
Packit 575503
  int stop;
Packit 575503
Packit 575503
  /* The context of mbs[0].  We store the context independently, since
Packit 575503
     the context of mbs[0] may be different from raw_mbs[0], which is
Packit 575503
     the beginning of the input string.  */
Packit 575503
  unsigned int tip_context;
Packit 575503
  /* The translation passed as a part of an argument of re_compile_pattern.  */
Packit 575503
  RE_TRANSLATE_TYPE trans;
Packit 575503
  /* Copy of re_dfa_t's word_char.  */
Packit 575503
  re_const_bitset_ptr_t word_char;
Packit 575503
  /* 1 if REG_ICASE.  */
Packit 575503
  unsigned char icase;
Packit 575503
  unsigned char is_utf8;
Packit 575503
  unsigned char map_notascii;
Packit 575503
  unsigned char mbs_allocated;
Packit 575503
  unsigned char offsets_needed;
Packit 575503
  unsigned char newline_anchor;
Packit 575503
  unsigned char word_ops_used;
Packit 575503
  int mb_cur_max;
Packit 575503
};
Packit 575503
typedef struct re_string_t re_string_t;
Packit 575503
Packit 575503
Packit 575503
struct re_dfa_t;
Packit 575503
typedef struct re_dfa_t re_dfa_t;
Packit 575503
Packit 575503
#ifndef NOT_IN_libc
Packit 575503
static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
Packit 575503
						int new_buf_len);
Packit 575503
# ifdef RE_ENABLE_I18N
Packit 575503
static void build_wcs_buffer (re_string_t *pstr);
Packit 575503
static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr);
Packit 575503
# endif /* RE_ENABLE_I18N */
Packit 575503
static void build_upper_buffer (re_string_t *pstr);
Packit 575503
static void re_string_translate_buffer (re_string_t *pstr);
Packit 575503
static unsigned int re_string_context_at (const re_string_t *input, int idx,
Packit 575503
					  int eflags) __attribute__ ((pure));
Packit 575503
#endif
Packit 575503
#define re_string_peek_byte(pstr, offset) \
Packit 575503
  ((pstr)->mbs[(pstr)->cur_idx + offset])
Packit 575503
#define re_string_fetch_byte(pstr) \
Packit 575503
  ((pstr)->mbs[(pstr)->cur_idx++])
Packit 575503
#define re_string_first_byte(pstr, idx) \
Packit 575503
  ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
Packit 575503
#define re_string_is_single_byte_char(pstr, idx) \
Packit 575503
  ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
Packit 575503
				|| (pstr)->wcs[(idx) + 1] != WEOF))
Packit 575503
#define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
Packit 575503
#define re_string_cur_idx(pstr) ((pstr)->cur_idx)
Packit 575503
#define re_string_get_buffer(pstr) ((pstr)->mbs)
Packit 575503
#define re_string_length(pstr) ((pstr)->len)
Packit 575503
#define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
Packit 575503
#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
Packit 575503
#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
Packit 575503
Packit 575503
#ifndef _LIBC
Packit 575503
# if HAVE_ALLOCA
Packit 575503
#  include <alloca.h>
Packit 575503
/* The OS usually guarantees only one guard page at the bottom of the stack,
Packit 575503
   and a page size can be as small as 4096 bytes.  So we cannot safely
Packit 575503
   allocate anything larger than 4096 bytes.  Also care for the possibility
Packit 575503
   of a few compiler-allocated temporary stack slots.  */
Packit 575503
#  define __libc_use_alloca(n) ((n) < 4032)
Packit 575503
# else
Packit 575503
/* alloca is implemented with malloc, so just use malloc.  */
Packit 575503
#  define __libc_use_alloca(n) 0
Packit 575503
# endif
Packit 575503
#endif
Packit 575503
Packit 575503
/*
Packit 575503
 * GAWK checks for zero-size allocations everywhere else,
Packit 575503
 * do it here too.
Packit 575503
 */
Packit 575503
#ifndef GAWK
Packit 575503
#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
Packit 575503
#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
Packit 575503
#else
Packit 575503
static void *
Packit 575503
test_malloc(size_t count, const char *file, size_t line)
Packit 575503
{
Packit 575503
	if (count == 0) {
Packit 575503
		fprintf(stderr, "%s:%lu: allocation of zero bytes\n",
Packit 575503
				file, (unsigned long) line);
Packit 575503
		exit(1);
Packit 575503
	}
Packit 575503
	return malloc(count);
Packit 575503
}
Packit 575503
Packit 575503
static void *
Packit 575503
test_realloc(void *p, size_t count, const char *file, size_t line)
Packit 575503
{
Packit 575503
	if (count == 0) {
Packit 575503
		fprintf(stderr, "%s:%lu: reallocation of zero bytes\n",
Packit 575503
				file, (unsigned long) line);
Packit 575503
		exit(1);
Packit 575503
	}
Packit 575503
	return realloc(p, count);
Packit 575503
}
Packit 575503
#define re_malloc(t,n) ((t *) test_malloc (((n) * sizeof (t)), __FILE__, __LINE__))
Packit 575503
#define re_realloc(p,t,n) ((t *) test_realloc (p, (n) * sizeof (t), __FILE__, __LINE__))
Packit 575503
#endif
Packit 575503
#define re_free(p) free (p)
Packit 575503
Packit 575503
struct bin_tree_t
Packit 575503
{
Packit 575503
  struct bin_tree_t *parent;
Packit 575503
  struct bin_tree_t *left;
Packit 575503
  struct bin_tree_t *right;
Packit 575503
  struct bin_tree_t *first;
Packit 575503
  struct bin_tree_t *next;
Packit 575503
Packit 575503
  re_token_t token;
Packit 575503
Packit 575503
  /* `node_idx' is the index in dfa->nodes, if `type' == 0.
Packit 575503
     Otherwise `type' indicate the type of this node.  */
Packit 575503
  int node_idx;
Packit 575503
};
Packit 575503
typedef struct bin_tree_t bin_tree_t;
Packit 575503
Packit 575503
#define BIN_TREE_STORAGE_SIZE \
Packit 575503
  ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
Packit 575503
Packit 575503
struct bin_tree_storage_t
Packit 575503
{
Packit 575503
  struct bin_tree_storage_t *next;
Packit 575503
  bin_tree_t data[BIN_TREE_STORAGE_SIZE];
Packit 575503
};
Packit 575503
typedef struct bin_tree_storage_t bin_tree_storage_t;
Packit 575503
Packit 575503
#define CONTEXT_WORD 1
Packit 575503
#define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
Packit 575503
#define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
Packit 575503
#define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
Packit 575503
Packit 575503
#define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
Packit 575503
#define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
Packit 575503
#define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
Packit 575503
#define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
Packit 575503
#define IS_ORDINARY_CONTEXT(c) ((c) == 0)
Packit 575503
Packit 575503
#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
Packit 575503
#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
Packit 575503
#define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
Packit 575503
#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
Packit 575503
Packit 575503
#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
Packit 575503
 ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
Packit 575503
  || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
Packit 575503
  || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
Packit 575503
  || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
Packit 575503
Packit 575503
#define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
Packit 575503
 ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
Packit 575503
  || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
Packit 575503
  || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
Packit 575503
  || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
Packit 575503
Packit 575503
struct re_dfastate_t
Packit 575503
{
Packit 575503
  unsigned int hash;
Packit 575503
  re_node_set nodes;
Packit 575503
  re_node_set non_eps_nodes;
Packit 575503
  re_node_set inveclosure;
Packit 575503
  re_node_set *entrance_nodes;
Packit 575503
  struct re_dfastate_t **trtable, **word_trtable;
Packit 575503
  unsigned int context : 4;
Packit 575503
  unsigned int halt : 1;
Packit 575503
  /* If this state can accept `multi byte'.
Packit 575503
     Note that we refer to multibyte characters, and multi character
Packit 575503
     collating elements as `multi byte'.  */
Packit 575503
  unsigned int accept_mb : 1;
Packit 575503
  /* If this state has backreference node(s).  */
Packit 575503
  unsigned int has_backref : 1;
Packit 575503
  unsigned int has_constraint : 1;
Packit 575503
};
Packit 575503
typedef struct re_dfastate_t re_dfastate_t;
Packit 575503
Packit 575503
struct re_state_table_entry
Packit 575503
{
Packit 575503
  int num;
Packit 575503
  int alloc;
Packit 575503
  re_dfastate_t **array;
Packit 575503
};
Packit 575503
Packit 575503
/* Array type used in re_sub_match_last_t and re_sub_match_top_t.  */
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  int next_idx;
Packit 575503
  int alloc;
Packit 575503
  re_dfastate_t **array;
Packit 575503
} state_array_t;
Packit 575503
Packit 575503
/* Store information about the node NODE whose type is OP_CLOSE_SUBEXP.  */
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  int node;
Packit 575503
  int str_idx; /* The position NODE match at.  */
Packit 575503
  state_array_t path;
Packit 575503
} re_sub_match_last_t;
Packit 575503
Packit 575503
/* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
Packit 575503
   And information about the node, whose type is OP_CLOSE_SUBEXP,
Packit 575503
   corresponding to NODE is stored in LASTS.  */
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  int str_idx;
Packit 575503
  int node;
Packit 575503
  state_array_t *path;
Packit 575503
  int alasts; /* Allocation size of LASTS.  */
Packit 575503
  int nlasts; /* The number of LASTS.  */
Packit 575503
  re_sub_match_last_t **lasts;
Packit 575503
} re_sub_match_top_t;
Packit 575503
Packit 575503
struct re_backref_cache_entry
Packit 575503
{
Packit 575503
  int node;
Packit 575503
  int str_idx;
Packit 575503
  int subexp_from;
Packit 575503
  int subexp_to;
Packit 575503
  char more;
Packit 575503
  char unused;
Packit 575503
  unsigned short int eps_reachable_subexps_map;
Packit 575503
};
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  /* The string object corresponding to the input string.  */
Packit 575503
  re_string_t input;
Packit 575503
#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
Packit 575503
  const re_dfa_t *const dfa;
Packit 575503
#else
Packit 575503
  const re_dfa_t *dfa;
Packit 575503
#endif
Packit 575503
  /* EFLAGS of the argument of regexec.  */
Packit 575503
  int eflags;
Packit 575503
  /* Where the matching ends.  */
Packit 575503
  int match_last;
Packit 575503
  int last_node;
Packit 575503
  /* The state log used by the matcher.  */
Packit 575503
  re_dfastate_t **state_log;
Packit 575503
  int state_log_top;
Packit 575503
  /* Back reference cache.  */
Packit 575503
  int nbkref_ents;
Packit 575503
  int abkref_ents;
Packit 575503
  struct re_backref_cache_entry *bkref_ents;
Packit 575503
  int max_mb_elem_len;
Packit 575503
  int nsub_tops;
Packit 575503
  int asub_tops;
Packit 575503
  re_sub_match_top_t **sub_tops;
Packit 575503
} re_match_context_t;
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  re_dfastate_t **sifted_states;
Packit 575503
  re_dfastate_t **limited_states;
Packit 575503
  int last_node;
Packit 575503
  int last_str_idx;
Packit 575503
  re_node_set limits;
Packit 575503
} re_sift_context_t;
Packit 575503
Packit 575503
struct re_fail_stack_ent_t
Packit 575503
{
Packit 575503
  int idx;
Packit 575503
  int node;
Packit 575503
  regmatch_t *regs;
Packit 575503
  re_node_set eps_via_nodes;
Packit 575503
};
Packit 575503
Packit 575503
struct re_fail_stack_t
Packit 575503
{
Packit 575503
  int num;
Packit 575503
  int alloc;
Packit 575503
  struct re_fail_stack_ent_t *stack;
Packit 575503
};
Packit 575503
Packit 575503
struct re_dfa_t
Packit 575503
{
Packit 575503
  re_token_t *nodes;
Packit 575503
  size_t nodes_alloc;
Packit 575503
  size_t nodes_len;
Packit 575503
  int *nexts;
Packit 575503
  int *org_indices;
Packit 575503
  re_node_set *edests;
Packit 575503
  re_node_set *eclosures;
Packit 575503
  re_node_set *inveclosures;
Packit 575503
  struct re_state_table_entry *state_table;
Packit 575503
  re_dfastate_t *init_state;
Packit 575503
  re_dfastate_t *init_state_word;
Packit 575503
  re_dfastate_t *init_state_nl;
Packit 575503
  re_dfastate_t *init_state_begbuf;
Packit 575503
  bin_tree_t *str_tree;
Packit 575503
  bin_tree_storage_t *str_tree_storage;
Packit 575503
  re_bitset_ptr_t sb_char;
Packit 575503
  int str_tree_storage_idx;
Packit 575503
Packit 575503
  /* number of subexpressions `re_nsub' is in regex_t.  */
Packit 575503
  unsigned int state_hash_mask;
Packit 575503
  int init_node;
Packit 575503
  int nbackref; /* The number of backreference in this dfa.  */
Packit 575503
Packit 575503
  /* Bitmap expressing which backreference is used.  */
Packit 575503
  bitset_word_t used_bkref_map;
Packit 575503
  bitset_word_t completed_bkref_map;
Packit 575503
Packit 575503
  unsigned int has_plural_match : 1;
Packit 575503
  /* If this dfa has "multibyte node", which is a backreference or
Packit 575503
     a node which can accept multibyte character or multi character
Packit 575503
     collating element.  */
Packit 575503
  unsigned int has_mb_node : 1;
Packit 575503
  unsigned int is_utf8 : 1;
Packit 575503
  unsigned int map_notascii : 1;
Packit 575503
  unsigned int word_ops_used : 1;
Packit 575503
  int mb_cur_max;
Packit 575503
  bitset_t word_char;
Packit 575503
  reg_syntax_t syntax;
Packit 575503
  int *subexp_map;
Packit 575503
#ifdef DEBUG
Packit 575503
  char* re_str;
Packit 575503
#endif
Packit 575503
#ifdef _LIBC
Packit 575503
  __libc_lock_define (, lock)
Packit 575503
#endif
Packit 575503
};
Packit 575503
Packit 575503
#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
Packit 575503
#define re_node_set_remove(set,id) \
Packit 575503
  (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
Packit 575503
#define re_node_set_empty(p) ((p)->nelem = 0)
Packit 575503
#define re_node_set_free(set) re_free ((set)->elems)
Packit 575503

Packit 575503
Packit 575503
typedef enum
Packit 575503
{
Packit 575503
  SB_CHAR,
Packit 575503
  MB_CHAR,
Packit 575503
  EQUIV_CLASS,
Packit 575503
  COLL_SYM,
Packit 575503
  CHAR_CLASS
Packit 575503
} bracket_elem_type;
Packit 575503
Packit 575503
typedef struct
Packit 575503
{
Packit 575503
  bracket_elem_type type;
Packit 575503
  union
Packit 575503
  {
Packit 575503
    unsigned char ch;
Packit 575503
    unsigned char *name;
Packit 575503
    wchar_t wch;
Packit 575503
  } opr;
Packit 575503
} bracket_elem_t;
Packit 575503
Packit 575503
Packit 575503
/* Inline functions for bitset operation.  */
Packit 575503
static void __attribute__ ((unused))
Packit 575503
bitset_not (bitset_t set)
Packit 575503
{
Packit 575503
  int bitset_i;
Packit 575503
  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
Packit 575503
    set[bitset_i] = ~set[bitset_i];
Packit 575503
}
Packit 575503
Packit 575503
static void __attribute__ ((unused))
Packit 575503
bitset_merge (bitset_t dest, const bitset_t src)
Packit 575503
{
Packit 575503
  int bitset_i;
Packit 575503
  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
Packit 575503
    dest[bitset_i] |= src[bitset_i];
Packit 575503
}
Packit 575503
Packit 575503
static void __attribute__ ((unused))
Packit 575503
bitset_mask (bitset_t dest, const bitset_t src)
Packit 575503
{
Packit 575503
  int bitset_i;
Packit 575503
  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
Packit 575503
    dest[bitset_i] &= src[bitset_i];
Packit 575503
}
Packit 575503
Packit 575503
#ifdef RE_ENABLE_I18N
Packit 575503
/* Inline functions for re_string.  */
Packit 575503
static int
Packit 575503
__attribute__ ((pure, unused))
Packit 575503
re_string_char_size_at (const re_string_t *pstr, int idx)
Packit 575503
{
Packit 575503
  int byte_idx;
Packit 575503
  if (pstr->mb_cur_max == 1)
Packit 575503
    return 1;
Packit 575503
  for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
Packit 575503
    if (pstr->wcs[idx + byte_idx] != WEOF)
Packit 575503
      break;
Packit 575503
  return byte_idx;
Packit 575503
}
Packit 575503
Packit 575503
static wint_t
Packit 575503
__attribute__ ((pure, unused))
Packit 575503
re_string_wchar_at (const re_string_t *pstr, int idx)
Packit 575503
{
Packit 575503
  if (pstr->mb_cur_max == 1)
Packit 575503
    return (wint_t) pstr->mbs[idx];
Packit 575503
  return (wint_t) pstr->wcs[idx];
Packit 575503
}
Packit 575503
Packit 575503
# ifndef NOT_IN_libc
Packit 575503
#  ifdef _LIBC
Packit 575503
#   include <locale/weight.h>
Packit 575503
#  endif
Packit 575503
Packit 575503
static int
Packit 575503
__attribute__ ((pure, unused))
Packit 575503
re_string_elem_size_at (const re_string_t *pstr, int idx)
Packit 575503
{
Packit 575503
#  ifdef _LIBC
Packit 575503
  const unsigned char *p, *extra;
Packit 575503
  const int32_t *table, *indirect;
Packit 575503
  uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
Packit 575503
Packit 575503
  if (nrules != 0)
Packit 575503
    {
Packit 575503
      table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
Packit 575503
      extra = (const unsigned char *)
Packit 575503
	_NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
Packit 575503
      indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
Packit 575503
						_NL_COLLATE_INDIRECTMB);
Packit 575503
      p = pstr->mbs + idx;
Packit 575503
      findidx (table, indirect, extra, &p, pstr->len - idx);
Packit 575503
      return p - pstr->mbs - idx;
Packit 575503
    }
Packit 575503
  else
Packit 575503
#  endif /* _LIBC */
Packit 575503
    return 1;
Packit 575503
}
Packit 575503
# endif
Packit 575503
#endif /* RE_ENABLE_I18N */
Packit 575503
Packit 575503
#endif /*  _REGEX_INTERNAL_H */