cvs-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Cvs-cvs] ccvs/lib ChangeLog regcomp.c regex.c regex.h re...


From: Derek Robert Price
Subject: [Cvs-cvs] ccvs/lib ChangeLog regcomp.c regex.c regex.h re...
Date: Tue, 11 Apr 2006 20:21:31 +0000

CVSROOT:        /cvsroot/cvs
Module name:    ccvs
Branch:         
Changes by:     Derek Robert Price <address@hidden>     06/04/11 20:21:31

Modified files:
        lib            : ChangeLog regcomp.c regex.c regex.h 
                         regex_internal.c regex_internal.h regexec.c 

Log message:
        * regcomp.c, regex.c, regex.h, regex_internal.c, regex_internal.h,
        regexec.c: Update from GNULIB.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/cvs/ccvs/lib/ChangeLog.diff?tr1=1.497&tr2=1.498&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/cvs/ccvs/lib/regcomp.c.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/cvs/ccvs/lib/regex.c.diff?tr1=1.22&tr2=1.23&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/cvs/ccvs/lib/regex.h.diff?tr1=1.11&tr2=1.12&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/cvs/ccvs/lib/regex_internal.c.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/cvs/ccvs/lib/regex_internal.h.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/cvs/ccvs/lib/regexec.c.diff?tr1=1.7&tr2=1.8&r1=text&r2=text

Patches:
Index: ccvs/lib/ChangeLog
diff -u ccvs/lib/ChangeLog:1.497 ccvs/lib/ChangeLog:1.498
--- ccvs/lib/ChangeLog:1.497    Mon Apr 10 17:41:07 2006
+++ ccvs/lib/ChangeLog  Tue Apr 11 20:21:31 2006
@@ -1,3 +1,8 @@
+2006-04-11  Derek Price  <address@hidden>
+
+       * regcomp.c, regex.c, regex.h, regex_internal.c, regex_internal.h,
+       regexec.c: Update from GNULIB.
+
 2006-04-10  Derek Price  <address@hidden>
 
        * system.h: Assume <stdint.h> since GNULIB provides a substitute.
Index: ccvs/lib/regcomp.c
diff -u ccvs/lib/regcomp.c:1.7 ccvs/lib/regcomp.c:1.8
--- ccvs/lib/regcomp.c:1.7      Tue Sep 20 16:47:52 2005
+++ ccvs/lib/regcomp.c  Tue Apr 11 20:21:31 2006
@@ -1,5 +1,5 @@
 /* Extended regular expression matching and search library.
-   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2002,2003,2004,2005,2006 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Isamu Hasegawa <address@hidden>.
 
@@ -18,11 +18,11 @@
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
 
 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
-                                         Idx length, reg_syntax_t syntax);
+                                         size_t length, reg_syntax_t syntax);
 static void re_compile_fastmap_iter (regex_t *bufp,
                                     const re_dfastate_t *init_state,
                                     char *fastmap);
-static reg_errcode_t init_dfa (re_dfa_t *dfa, Idx pat_len);
+static reg_errcode_t init_dfa (re_dfa_t *dfa, size_t pat_len);
 #ifdef RE_ENABLE_I18N
 static void free_charset (re_charset_t *cset);
 #endif /* RE_ENABLE_I18N */
@@ -55,7 +55,7 @@
 static Idx fetch_number (re_string_t *input, re_token_t *token,
                         reg_syntax_t syntax);
 static int peek_token (re_token_t *token, re_string_t *input,
-                       reg_syntax_t syntax);
+                       reg_syntax_t syntax) internal_function;
 static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
                          reg_syntax_t syntax, reg_errcode_t *err);
 static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
@@ -86,26 +86,26 @@
                                          re_string_t *regexp,
                                          re_token_t *token);
 #ifdef RE_ENABLE_I18N
-static reg_errcode_t build_equiv_class (bitset sbcset,
+static reg_errcode_t build_equiv_class (bitset_t sbcset,
                                        re_charset_t *mbcset,
                                        Idx *equiv_class_alloc,
                                        const unsigned char *name);
-static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
-                                     bitset sbcset,
+static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
+                                     bitset_t sbcset,
                                      re_charset_t *mbcset,
                                      Idx *char_class_alloc,
                                      const unsigned char *class_name,
                                      reg_syntax_t syntax);
 #else  /* not RE_ENABLE_I18N */
-static reg_errcode_t build_equiv_class (bitset sbcset,
+static reg_errcode_t build_equiv_class (bitset_t sbcset,
                                        const unsigned char *name);
-static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
-                                     bitset sbcset,
+static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
+                                     bitset_t sbcset,
                                      const unsigned char *class_name,
                                      reg_syntax_t syntax);
 #endif /* not RE_ENABLE_I18N */
 static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
-                                      unsigned REG_TRANSLATE_TYPE trans,
+                                      RE_TRANSLATE_TYPE trans,
                                       const unsigned char *class_name,
                                       const unsigned char *extra,
                                       bool non_match, reg_errcode_t *err);
@@ -125,7 +125,7 @@
    POSIX doesn't require that we do anything for REG_NOERROR,
    but why not be nice?  */
 
-const char __re_error_msgid[] attribute_hidden =
+static const char __re_error_msgid[] =
   {
 #define REG_NOERROR_IDX        0
     gettext_noop ("Success")   /* REG_NOERROR */
@@ -179,7 +179,7 @@
     gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
   };
 
-const size_t __re_error_msgid_idx[] attribute_hidden =
+static const size_t __re_error_msgid_idx[] =
   {
     REG_NOERROR_IDX,
     REG_NOMATCH_IDX,
@@ -206,22 +206,30 @@
    compiles PATTERN (of length LENGTH) and puts the result in BUFP.
    Returns 0 if the pattern was valid, otherwise an error string.
 
-   Assumes the `re_allocated' (and perhaps `re_buffer') and `translate' fields
+   Assumes the `allocated' (and perhaps `buffer') and `translate' fields
    are set in BUFP on entry.  */
 
+#ifdef _LIBC
+const char *
+re_compile_pattern (pattern, length, bufp)
+    const char *pattern;
+    size_t length;
+    struct re_pattern_buffer *bufp;
+#else /* size_t might promote */
 const char *
 re_compile_pattern (const char *pattern, size_t length,
                    struct re_pattern_buffer *bufp)
+#endif
 {
   reg_errcode_t ret;
 
   /* And GNU code determines whether or not to get register information
      by passing null for the REGS argument to re_match, etc., not by
-     setting re_no_sub, unless REG_NO_SUB is set.  */
-  bufp->re_no_sub = !!(re_syntax_options & REG_NO_SUB);
+     setting no_sub, unless RE_NO_SUB is set.  */
+  bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
 
   /* Match anchors at newline.  */
-  bufp->re_newline_anchor = 1;
+  bufp->newline_anchor = 1;
 
   ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
 
@@ -249,7 +257,8 @@
    defined in regex.h.  We return the old syntax.  */
 
 reg_syntax_t
-re_set_syntax (reg_syntax_t syntax)
+re_set_syntax (syntax)
+    reg_syntax_t syntax;
 {
   reg_syntax_t ret = re_syntax_options;
 
@@ -261,10 +270,11 @@
 #endif
 
 int
-re_compile_fastmap (struct re_pattern_buffer *bufp)
+re_compile_fastmap (bufp)
+    struct re_pattern_buffer *bufp;
 {
-  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
-  char *fastmap = bufp->re_fastmap;
+  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
+  char *fastmap = bufp->fastmap;
 
   memset (fastmap, '\0', sizeof (char) * SBC_MAX);
   re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
@@ -274,7 +284,7 @@
     re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
   if (dfa->init_state != dfa->init_state_begbuf)
     re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
-  bufp->re_fastmap_accurate = 1;
+  bufp->fastmap_accurate = 1;
   return 0;
 }
 #ifdef _LIBC
@@ -297,9 +307,9 @@
 re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
                         char *fastmap)
 {
-  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
   Idx node_cnt;
-  bool icase = (dfa->mb_cur_max == 1 && (bufp->re_syntax & REG_IGNORE_CASE));
+  bool icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
   for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
     {
       Idx node = init_state->nodes.elems[node_cnt];
@@ -309,7 +319,7 @@
        {
          re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
 #ifdef RE_ENABLE_I18N
-         if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
+         if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
            {
              unsigned char buf[MB_LEN_MAX];
              unsigned char *p;
@@ -322,7 +332,7 @@
                     && dfa->nodes[node].type == CHARACTER
                     && dfa->nodes[node].mb_partial)
                *p++ = dfa->nodes[node].opr.c;
-             memset (&state, 0, sizeof (state));
+             memset (&state, '\0', sizeof (state));
              if (mbrtowc (&wc, (const char *) buf, p - buf,
                           &state) == p - buf
                  && (__wcrtomb ((char *) buf, towlower (wc), &state)
@@ -333,11 +343,15 @@
        }
       else if (type == SIMPLE_BRACKET)
        {
-         int i, j, ch;
+         int i, ch;
          for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
-           for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
-             if (dfa->nodes[node].opr.sbcset[i] & ((bitset_word) 1 << j))
-               re_set_fastmap (fastmap, icase, ch);
+           {
+             int j;
+             bitset_word_t w = dfa->nodes[node].opr.sbcset[i];
+             for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
+               if (w & ((bitset_word_t) 1 << j))
+                 re_set_fastmap (fastmap, icase, ch);
+           }
        }
 #ifdef RE_ENABLE_I18N
       else if (type == COMPLEX_BRACKET)
@@ -376,7 +390,7 @@
              memset (&state, '\0', sizeof (state));
              if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
                re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
-             if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
+             if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
                {
                  if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
                      != (size_t) -1)
@@ -393,7 +407,7 @@
        {
          memset (fastmap, '\1', sizeof (char) * SBC_MAX);
          if (type == END_OF_RE)
-           bufp->re_can_be_null = 1;
+           bufp->can_be_null = 1;
          return;
        }
     }
@@ -405,14 +419,14 @@
    PREG is a regex_t *.  We do not expect any fields to be initialized,
    since POSIX says we shouldn't.  Thus, we set
 
-     `re_buffer' to the compiled pattern;
-     `re_used' to the length of the compiled pattern;
-     `re_syntax' to REG_SYNTAX_POSIX_EXTENDED if the
+     `buffer' to the compiled pattern;
+     `used' to the length of the compiled pattern;
+     `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
        REG_EXTENDED bit in CFLAGS is set; otherwise, to
-       REG_SYNTAX_POSIX_BASIC;
-     `re_newline_anchor' to REG_NEWLINE being set in CFLAGS;
-     `re_fastmap' to an allocated space for the fastmap;
-     `re_fastmap_accurate' to zero;
+       RE_SYNTAX_POSIX_BASIC;
+     `newline_anchor' to REG_NEWLINE being set in CFLAGS;
+     `fastmap' to an allocated space for the fastmap;
+     `fastmap_accurate' to zero;
      `re_nsub' to the number of subexpressions in PATTERN.
 
    PATTERN is the address of the pattern string.
@@ -436,35 +450,38 @@
    the return codes and their meanings.)  */
 
 int
-regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
+regcomp (preg, pattern, cflags)
+    regex_t *__restrict preg;
+    const char *__restrict pattern;
+    int cflags;
 {
   reg_errcode_t ret;
-  reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? REG_SYNTAX_POSIX_EXTENDED
-                       : REG_SYNTAX_POSIX_BASIC);
+  reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
+                        : RE_SYNTAX_POSIX_BASIC);
 
-  preg->re_buffer = NULL;
-  preg->re_allocated = 0;
-  preg->re_used = 0;
+  preg->buffer = NULL;
+  preg->allocated = 0;
+  preg->used = 0;
 
   /* Try to allocate space for the fastmap.  */
-  preg->re_fastmap = re_malloc (char, SBC_MAX);
-  if (BE (preg->re_fastmap == NULL, 0))
+  preg->fastmap = re_malloc (char, SBC_MAX);
+  if (BE (preg->fastmap == NULL, 0))
     return REG_ESPACE;
 
-  syntax |= (cflags & REG_ICASE) ? REG_IGNORE_CASE : 0;
+  syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
 
   /* If REG_NEWLINE is set, newlines are treated differently.  */
   if (cflags & REG_NEWLINE)
     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
-      syntax &= ~REG_DOT_NEWLINE;
-      syntax |= REG_HAT_LISTS_NOT_NEWLINE;
+      syntax &= ~RE_DOT_NEWLINE;
+      syntax |= RE_HAT_LISTS_NOT_NEWLINE;
       /* It also changes the matching behavior.  */
-      preg->re_newline_anchor = 1;
+      preg->newline_anchor = 1;
     }
   else
-    preg->re_newline_anchor = 0;
-  preg->re_no_sub = !!(cflags & REG_NOSUB);
-  preg->re_translate = NULL;
+    preg->newline_anchor = 0;
+  preg->no_sub = !!(cflags & REG_NOSUB);
+  preg->translate = NULL;
 
   ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
 
@@ -473,7 +490,7 @@
   if (ret == REG_ERPAREN)
     ret = REG_EPAREN;
 
-  /* We have already checked preg->re_fastmap != NULL.  */
+  /* We have already checked preg->fastmap != NULL.  */
   if (BE (ret == REG_NOERROR, 1))
     /* Compute the fastmap now, since regexec cannot modify the pattern
        buffer.  This function never fails in this implementation.  */
@@ -481,8 +498,8 @@
   else
     {
       /* Some error occurred while compiling the expression.  */
-      re_free (preg->re_fastmap);
-      preg->re_fastmap = NULL;
+      re_free (preg->fastmap);
+      preg->fastmap = NULL;
     }
 
   return (int) ret;
@@ -494,9 +511,18 @@
 /* Returns a message corresponding to an error code, ERRCODE, returned
    from either regcomp or regexec.   We don't use PREG here.  */
 
+#ifdef _LIBC
+size_t
+regerror (errcode, preg, errbuf, errbuf_size)
+    int errcode;
+    const regex_t *__restrict preg;
+    char *__restrict errbuf;
+    size_t errbuf_size;
+#else /* size_t might promote */
 size_t
 regerror (int errcode, const regex_t *__restrict preg,
          char *__restrict errbuf, size_t errbuf_size)
+#endif
 {
   const char *msg;
   size_t msg_size;
@@ -541,21 +567,18 @@
    UTF-8 is used.  Otherwise we would allocate memory just to initialize
    it the same all the time.  UTF-8 is the preferred encoding so this is
    a worthwhile optimization.  */
-static const bitset utf8_sb_map =
+static const bitset_t utf8_sb_map =
 {
   /* Set the first 128 bits.  */
-# if 2 < BITSET_WORDS
+# if 4 * BITSET_WORD_BITS < ASCII_CHARS
+#  error "bitset_word_t is narrower than 32 bits"
+# elif 3 * BITSET_WORD_BITS < ASCII_CHARS
+  BITSET_WORD_MAX, BITSET_WORD_MAX, BITSET_WORD_MAX,
+# elif 2 * BITSET_WORD_BITS < ASCII_CHARS
+  BITSET_WORD_MAX, BITSET_WORD_MAX,
+# elif 1 * BITSET_WORD_BITS < ASCII_CHARS
   BITSET_WORD_MAX,
 # endif
-# if 4 < BITSET_WORDS
-  BITSET_WORD_MAX,
-# endif
-# if 6 < BITSET_WORDS
-  BITSET_WORD_MAX,
-# endif
-# if 8 < BITSET_WORDS
-#  error "Invalid BITSET_WORDS"
-# endif
   (BITSET_WORD_MAX
    >> (SBC_MAX % BITSET_WORD_BITS == 0
        ? 0
@@ -615,19 +638,20 @@
 /* Free dynamically allocated space used by PREG.  */
 
 void
-regfree (regex_t *preg)
+regfree (preg)
+    regex_t *preg;
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   if (BE (dfa != NULL, 1))
     free_dfa_content (dfa);
-  preg->re_buffer = NULL;
-  preg->re_allocated = 0;
+  preg->buffer = NULL;
+  preg->allocated = 0;
 
-  re_free (preg->re_fastmap);
-  preg->re_fastmap = NULL;
+  re_free (preg->fastmap);
+  preg->fastmap = NULL;
 
-  re_free (preg->re_translate);
-  preg->re_translate = NULL;
+  re_free (preg->translate);
+  preg->translate = NULL;
 }
 #ifdef _LIBC
 weak_alias (__regfree, regfree)
@@ -648,31 +672,32 @@
    regcomp/regexec above without link errors.  */
 weak_function
 # endif
-re_comp (const char *s)
+re_comp (s)
+     const char *s;
 {
   reg_errcode_t ret;
   char *fastmap;
 
   if (!s)
     {
-      if (!re_comp_buf.re_buffer)
+      if (!re_comp_buf.buffer)
        return gettext ("No previous regular expression");
       return 0;
     }
 
-  if (re_comp_buf.re_buffer)
+  if (re_comp_buf.buffer)
     {
-      fastmap = re_comp_buf.re_fastmap;
-      re_comp_buf.re_fastmap = NULL;
+      fastmap = re_comp_buf.fastmap;
+      re_comp_buf.fastmap = NULL;
       __regfree (&re_comp_buf);
       memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
-      re_comp_buf.re_fastmap = fastmap;
+      re_comp_buf.fastmap = fastmap;
     }
 
-  if (re_comp_buf.re_fastmap == NULL)
+  if (re_comp_buf.fastmap == NULL)
     {
-      re_comp_buf.re_fastmap = (char *) malloc (SBC_MAX);
-      if (re_comp_buf.re_fastmap == NULL)
+      re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
+      if (re_comp_buf.fastmap == NULL)
        return (char *) gettext (__re_error_msgid
                                 + __re_error_msgid_idx[(int) REG_ESPACE]);
     }
@@ -681,7 +706,7 @@
      don't need to initialize the pattern buffer fields which affect it.  */
 
   /* Match anchors at newlines.  */
-  re_comp_buf.re_newline_anchor = 1;
+  re_comp_buf.newline_anchor = 1;
 
   ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
 
@@ -706,7 +731,7 @@
    SYNTAX indicate regular expression's syntax.  */
 
 static reg_errcode_t
-re_compile_internal (regex_t *preg, const char *pattern, Idx length,
+re_compile_internal (regex_t *preg, const char * pattern, size_t length,
                     reg_syntax_t syntax)
 {
   reg_errcode_t err = REG_NOERROR;
@@ -714,55 +739,56 @@
   re_string_t regexp;
 
   /* Initialize the pattern buffer.  */
-  preg->re_fastmap_accurate = 0;
-  preg->re_syntax = syntax;
-  preg->re_not_bol = preg->re_not_eol = 0;
-  preg->re_used = 0;
+  preg->fastmap_accurate = 0;
+  preg->syntax = syntax;
+  preg->not_bol = preg->not_eol = 0;
+  preg->used = 0;
   preg->re_nsub = 0;
-  preg->re_can_be_null = 0;
-  preg->re_regs_allocated = REG_UNALLOCATED;
+  preg->can_be_null = 0;
+  preg->regs_allocated = REGS_UNALLOCATED;
 
   /* Initialize the dfa.  */
-  dfa = (re_dfa_t *) preg->re_buffer;
-  if (BE (preg->re_allocated < sizeof (re_dfa_t), 0))
+  dfa = (re_dfa_t *) preg->buffer;
+  if (BE (preg->allocated < sizeof (re_dfa_t), 0))
     {
       /* If zero allocated, but buffer is non-null, try to realloc
         enough space.  This loses if buffer's address is bogus, but
-        that is the user's responsibility.  If buffer is null this
+        that is the user's responsibility.  If ->buffer is NULL this
         is a simple allocation.  */
-      dfa = re_realloc (preg->re_buffer, re_dfa_t, 1);
+      dfa = re_realloc (preg->buffer, re_dfa_t, 1);
       if (dfa == NULL)
        return REG_ESPACE;
-      preg->re_allocated = sizeof (re_dfa_t);
-      preg->re_buffer = (unsigned char *) dfa;
+      preg->allocated = sizeof (re_dfa_t);
+      preg->buffer = (unsigned char *) dfa;
     }
-  preg->re_used = sizeof (re_dfa_t);
-
-  __libc_lock_init (dfa->lock);
+  preg->used = sizeof (re_dfa_t);
 
   err = init_dfa (dfa, length);
   if (BE (err != REG_NOERROR, 0))
     {
       free_dfa_content (dfa);
-      preg->re_buffer = NULL;
-      preg->re_allocated = 0;
+      preg->buffer = NULL;
+      preg->allocated = 0;
       return err;
     }
 #ifdef DEBUG
+  /* Note: length+1 will not overflow since it is checked in init_dfa.  */
   dfa->re_str = re_malloc (char, length + 1);
   strncpy (dfa->re_str, pattern, length + 1);
 #endif
 
-  err = re_string_construct (&regexp, pattern, length, preg->re_translate,
-                            syntax & REG_IGNORE_CASE, dfa);
+  __libc_lock_init (dfa->lock);
+
+  err = re_string_construct (&regexp, pattern, length, preg->translate,
+                            syntax & RE_ICASE, dfa);
   if (BE (err != REG_NOERROR, 0))
     {
     re_compile_internal_free_return:
       free_workarea_compile (preg);
       re_string_destruct (&regexp);
       free_dfa_content (dfa);
-      preg->re_buffer = NULL;
-      preg->re_allocated = 0;
+      preg->buffer = NULL;
+      preg->allocated = 0;
       return err;
     }
 
@@ -779,7 +805,7 @@
 
 #ifdef RE_ENABLE_I18N
   /* If possible, do searching in single byte encoding to speed things up.  */
-  if (dfa->is_utf8 && !(syntax & REG_IGNORE_CASE) && preg->re_translate == 
NULL)
+  if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
     optimize_utf8 (dfa);
 #endif
 
@@ -793,8 +819,8 @@
   if (BE (err != REG_NOERROR, 0))
     {
       free_dfa_content (dfa);
-      preg->re_buffer = NULL;
-      preg->re_allocated = 0;
+      preg->buffer = NULL;
+      preg->allocated = 0;
     }
 
   return err;
@@ -804,27 +830,43 @@
    as the initial length of some arrays.  */
 
 static reg_errcode_t
-init_dfa (re_dfa_t *dfa, Idx pat_len)
+init_dfa (re_dfa_t *dfa, size_t pat_len)
 {
   __re_size_t table_size;
 #ifndef _LIBC
   char *codeset_name;
 #endif
+  size_t max_object_size =
+    MAX (sizeof (struct re_state_table_entry),
+        MAX (sizeof (re_token_t),
+             MAX (sizeof (re_node_set),
+                  MAX (sizeof (regmatch_t),
+                       MAX (sizeof (regoff_t),
+                            MAX (sizeof (wchar_t),
+                                 MAX (sizeof (wctype_t),
+                                      sizeof (Idx))))))));
 
   memset (dfa, '\0', sizeof (re_dfa_t));
 
   /* Force allocation of str_tree_storage the first time.  */
   dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
 
+  /* Avoid overflows.  The extra "/ 2" is for the table_size doubling
+     calculation below, and for similar doubling calculations
+     elsewhere.  And it's <= rather than <, because some of the
+     doubling calculations add 1 afterwards.  */
+  if (BE (SIZE_MAX / max_object_size / 2 <= pat_len, 0))
+    return REG_ESPACE;
+
   dfa->nodes_alloc = pat_len + 1;
-  dfa->nodes = re_xmalloc (re_token_t, dfa->nodes_alloc);
+  dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
 
   /*  table_size = 2 ^ ceil(log pat_len) */
-  for (table_size = 1; table_size <= pat_len; table_size <<= 1)
-    if (0 < (Idx) -1 && table_size == 0)
-      return REG_ESPACE;
+  for (table_size = 1; ; table_size <<= 1)
+    if (table_size > pat_len)
+      break;
 
-  dfa->state_table = re_calloc (struct re_state_table_entry, table_size);
+  dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
   dfa->state_hash_mask = table_size - 1;
 
   dfa->mb_cur_max = MB_CUR_MAX;
@@ -867,7 +909,7 @@
        {
          int i, j, ch;
 
-         dfa->sb_char = re_calloc (bitset_word, BITSET_WORDS);
+         dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
          if (BE (dfa->sb_char == NULL, 0))
            return REG_ESPACE;
 
@@ -877,7 +919,7 @@
              {
                wint_t wch = __btowc (ch);
                if (wch != WEOF)
-                 dfa->sb_char[i] |= (bitset_word) 1 << j;
+                 dfa->sb_char[i] |= (bitset_word_t) 1 << j;
 # ifndef _LIBC
                if (isascii (ch) && wch != ch)
                  dfa->map_notascii = 1;
@@ -897,6 +939,7 @@
    character used by some operators like "\<", "\>", etc.  */
 
 static void
+internal_function
 init_word_char (re_dfa_t *dfa)
 {
   int i, j, ch;
@@ -904,7 +947,7 @@
   for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
     for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
       if (isalnum (ch) || ch == '_')
-       dfa->word_char[i] |= (bitset_word) 1 << j;
+       dfa->word_char[i] |= (bitset_word_t) 1 << j;
 }
 
 /* Free the work area which are only used while compiling.  */
@@ -912,7 +955,7 @@
 static void
 free_workarea_compile (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   bin_tree_storage_t *storage, *next;
   for (storage = dfa->str_tree_storage; storage; storage = next)
     {
@@ -1022,7 +1065,7 @@
     switch (dfa->nodes[node].type)
       {
       case CHARACTER:
-       if (dfa->nodes[node].opr.c >= 0x80)
+       if (dfa->nodes[node].opr.c >= ASCII_CHARS)
          mb_chars = true;
        break;
       case ANCHOR:
@@ -1053,11 +1096,10 @@
       case SIMPLE_BRACKET:
        /* Just double check.  */
        {
-         int rshift =
-           (SBC_MAX / 2 % BITSET_WORD_BITS == 0
-            ? 0
-            : BITSET_WORD_BITS - SBC_MAX / 2 % BITSET_WORD_BITS);
-         for (i = SBC_MAX / 2 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
+         int rshift = (ASCII_CHARS % BITSET_WORD_BITS == 0
+                       ? 0
+                       : BITSET_WORD_BITS - ASCII_CHARS % BITSET_WORD_BITS);
+         for (i = ASCII_CHARS / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
            {
              if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
                return;
@@ -1073,7 +1115,7 @@
     for (node = 0; node < dfa->nodes_len; ++node)
       {
        if (dfa->nodes[node].type == CHARACTER
-           && dfa->nodes[node].opr.c >= 0x80)
+           && dfa->nodes[node].opr.c >= ASCII_CHARS)
          dfa->nodes[node].mb_partial = 0;
        else if (dfa->nodes[node].type == OP_PERIOD)
          dfa->nodes[node].type = OP_UTF8_PERIOD;
@@ -1092,19 +1134,19 @@
 static reg_errcode_t
 analyze (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   reg_errcode_t ret;
 
   /* Allocate arrays.  */
   dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
   dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
-  dfa->edests = re_xmalloc (re_node_set, dfa->nodes_alloc);
+  dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
   dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
   if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
          || dfa->eclosures == NULL, 0))
     return REG_ESPACE;
 
-  dfa->subexp_map = re_xmalloc (Idx, preg->re_nsub);
+  dfa->subexp_map = re_malloc (Idx, preg->re_nsub);
   if (dfa->subexp_map != NULL)
     {
       Idx i;
@@ -1137,10 +1179,10 @@
 
   /* We only need this during the prune_impossible_nodes pass in regexec.c;
      skip it if p_i_n will not run, as calc_inveclosure can be quadratic.  */
-  if ((!preg->re_no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
+  if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
       || dfa->nbackref)
     {
-      dfa->inveclosures = re_xmalloc (re_node_set, dfa->nodes_len);
+      dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
       if (BE (dfa->inveclosures == NULL, 0))
         return REG_ESPACE;
       ret = calc_inveclosure (dfa);
@@ -1240,7 +1282,7 @@
 
       dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
       if (other_idx < BITSET_WORD_BITS)
-       dfa->used_bkref_map &= ~ ((bitset_word) 1 << other_idx);
+       dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx);
     }
 
   return REG_NOERROR;
@@ -1273,18 +1315,19 @@
 static bin_tree_t *
 lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   bin_tree_t *body = node->left;
   bin_tree_t *op, *cls, *tree1, *tree;
 
-  if (preg->re_no_sub
+  if (preg->no_sub
       /* We do not optimize empty subexpressions, because otherwise we may
         have bad CONCAT nodes with NULL children.  This is obviously not
         very common, so we do not lose much.  An example that triggers
         this case is the sed "script" /\(\)/x.  */
       && node->left != NULL
-      && ! (node->token.opr.idx < BITSET_WORD_BITS
-           && dfa->used_bkref_map & ((bitset_word) 1 << node->token.opr.idx)))
+      && (node->token.opr.idx >= BITSET_WORD_BITS
+         || !(dfa->used_bkref_map
+              & ((bitset_word_t) 1 << node->token.opr.idx))))
     return node->left;
 
   /* Convert the SUBEXP node to the concatenation of an
@@ -1410,9 +1453,9 @@
    to their own constraint.  */
 
 static reg_errcode_t
-duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node,
-                       Idx top_clone_node, Idx root_node,
-                       unsigned int init_constraint)
+internal_function
+duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node, Idx top_clone_node,
+                       Idx root_node, unsigned int init_constraint)
 {
   Idx org_node, clone_node;
   bool ok;
@@ -1458,8 +1501,7 @@
                  /* ...but if the node is root_node itself, it means the
                     epsilon closure have a loop, then tie it to the
                     destination of the root_node.  */
-                 ok = re_node_set_insert (dfa->edests + clone_node,
-                                           org_dest);
+                 ok = re_node_set_insert (dfa->edests + clone_node, org_dest);
                  if (BE (! ok, 0))
                    return REG_ESPACE;
                  break;
@@ -1652,8 +1694,6 @@
       && dfa->edests[node].nelem
       && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
     {
-      Idx org_node, cur_node;
-      org_node = cur_node = node;
       err = duplicate_node_closure (dfa, node, node, node, constraint);
       if (BE (err != REG_NOERROR, 0))
        return err;
@@ -1711,6 +1751,7 @@
    We must not use this function inside bracket expressions.  */
 
 static void
+internal_function
 fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
 {
   re_string_skip_bytes (input, peek_token (result, input, syntax));
@@ -1720,6 +1761,7 @@
    We must not use this function inside bracket expressions.  */
 
 static int
+internal_function
 peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
 {
   unsigned char c;
@@ -1770,97 +1812,97 @@
       switch (c2)
        {
        case '|':
-         if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_NO_BK_VBAR))
+         if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
            token->type = OP_ALT;
          break;
        case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
-         if (!(syntax & REG_NO_BK_REFS))
+         if (!(syntax & RE_NO_BK_REFS))
            {
              token->type = OP_BACK_REF;
              token->opr.idx = c2 - '1';
            }
          break;
        case '<':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_FIRST;
            }
          break;
        case '>':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_LAST;
            }
          break;
        case 'b':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_DELIM;
            }
          break;
        case 'B':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = NOT_WORD_DELIM;
            }
          break;
        case 'w':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            token->type = OP_WORD;
          break;
        case 'W':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            token->type = OP_NOTWORD;
          break;
        case 's':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            token->type = OP_SPACE;
          break;
        case 'S':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            token->type = OP_NOTSPACE;
          break;
        case '`':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = BUF_FIRST;
            }
          break;
        case '\'':
-         if (!(syntax & REG_NO_GNU_OPS))
+         if (!(syntax & RE_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = BUF_LAST;
            }
          break;
        case '(':
-         if (!(syntax & REG_NO_BK_PARENS))
+         if (!(syntax & RE_NO_BK_PARENS))
            token->type = OP_OPEN_SUBEXP;
          break;
        case ')':
-         if (!(syntax & REG_NO_BK_PARENS))
+         if (!(syntax & RE_NO_BK_PARENS))
            token->type = OP_CLOSE_SUBEXP;
          break;
        case '+':
-         if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
+         if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
            token->type = OP_DUP_PLUS;
          break;
        case '?':
-         if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
+         if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
            token->type = OP_DUP_QUESTION;
          break;
        case '{':
-         if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
+         if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
            token->type = OP_OPEN_DUP_NUM;
          break;
        case '}':
-         if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
+         if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
            token->type = OP_CLOSE_DUP_NUM;
          break;
        default:
@@ -1883,38 +1925,38 @@
   switch (c)
     {
     case '\n':
-      if (syntax & REG_NEWLINE_ALT)
+      if (syntax & RE_NEWLINE_ALT)
        token->type = OP_ALT;
       break;
     case '|':
-      if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_NO_BK_VBAR))
+      if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
        token->type = OP_ALT;
       break;
     case '*':
       token->type = OP_DUP_ASTERISK;
       break;
     case '+':
-      if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
+      if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
        token->type = OP_DUP_PLUS;
       break;
     case '?':
-      if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
+      if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
        token->type = OP_DUP_QUESTION;
       break;
     case '{':
-      if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
+      if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
        token->type = OP_OPEN_DUP_NUM;
       break;
     case '}':
-      if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
+      if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
        token->type = OP_CLOSE_DUP_NUM;
       break;
     case '(':
-      if (syntax & REG_NO_BK_PARENS)
+      if (syntax & RE_NO_BK_PARENS)
        token->type = OP_OPEN_SUBEXP;
       break;
     case ')':
-      if (syntax & REG_NO_BK_PARENS)
+      if (syntax & RE_NO_BK_PARENS)
        token->type = OP_CLOSE_SUBEXP;
       break;
     case '[':
@@ -1924,18 +1966,18 @@
       token->type = OP_PERIOD;
       break;
     case '^':
-      if (!(syntax & (REG_CONTEXT_INDEP_ANCHORS | REG_CARET_ANCHORS_HERE)) &&
+      if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
          re_string_cur_idx (input) != 0)
        {
          char prev = re_string_peek_byte (input, -1);
-         if (!(syntax & REG_NEWLINE_ALT) || prev != '\n')
+         if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
            break;
        }
       token->type = ANCHOR;
       token->opr.ctx_type = LINE_FIRST;
       break;
     case '$':
-      if (!(syntax & REG_CONTEXT_INDEP_ANCHORS) &&
+      if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
          re_string_cur_idx (input) + 1 != re_string_length (input))
        {
          re_token_t next;
@@ -1958,6 +2000,7 @@
    We must not use this function out of bracket expressions.  */
 
 static int
+internal_function
 peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
 {
   unsigned char c;
@@ -1978,7 +2021,7 @@
     }
 #endif /* RE_ENABLE_I18N */
 
-  if (c == '\\' && (syntax & REG_BACKSLASH_ESCAPE_IN_LISTS)
+  if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
       && re_string_cur_idx (input) + 1 < re_string_length (input))
     {
       /* In this case, '\' escape a character.  */
@@ -2008,7 +2051,7 @@
          token->type = OP_OPEN_EQUIV_CLASS;
          break;
        case ':':
-         if (syntax & REG_CHAR_CLASSES)
+         if (syntax & RE_CHAR_CLASSES)
            {
              token->type = OP_OPEN_CHAR_CLASS;
              break;
@@ -2057,11 +2100,11 @@
 parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
        reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   bin_tree_t *tree, *eor, *root;
   re_token_t current_token;
   dfa->syntax = syntax;
-  fetch_token (&current_token, regexp, syntax | REG_CARET_ANCHORS_HERE);
+  fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
   tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     return NULL;
@@ -2091,7 +2134,7 @@
 parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
               reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   bin_tree_t *tree, *branch = NULL;
   tree = parse_branch (regexp, preg, token, syntax, nest, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
@@ -2099,7 +2142,7 @@
 
   while (token->type == OP_ALT)
     {
-      fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
+      fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
       if (token->type != OP_ALT && token->type != END_OF_RE
          && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
        {
@@ -2133,7 +2176,7 @@
              reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
   bin_tree_t *tree, *exp;
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   tree = parse_expression (regexp, preg, token, syntax, nest, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     return NULL;
@@ -2172,7 +2215,7 @@
 parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
                  reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   bin_tree_t *tree;
   switch (token->type)
     {
@@ -2229,7 +2272,7 @@
       dfa->has_mb_node = 1;
       break;
     case OP_OPEN_DUP_NUM:
-      if (syntax & REG_CONTEXT_INVALID_DUP)
+      if (syntax & RE_CONTEXT_INVALID_DUP)
        {
          *err = REG_BADRPT;
          return NULL;
@@ -2238,12 +2281,12 @@
     case OP_DUP_ASTERISK:
     case OP_DUP_PLUS:
     case OP_DUP_QUESTION:
-      if (syntax & REG_CONTEXT_INVALID_OPS)
+      if (syntax & RE_CONTEXT_INVALID_OPS)
        {
          *err = REG_BADRPT;
          return NULL;
        }
-      else if (syntax & REG_CONTEXT_INDEP_OPS)
+      else if (syntax & RE_CONTEXT_INDEP_OPS)
        {
          fetch_token (token, regexp, syntax);
          return parse_expression (regexp, preg, token, syntax, nest, err);
@@ -2251,7 +2294,7 @@
       /* else fall through  */
     case OP_CLOSE_SUBEXP:
       if ((token->type == OP_CLOSE_SUBEXP) &&
-         !(syntax & REG_UNMATCHED_RIGHT_PAREN_ORD))
+         !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
        {
          *err = REG_ERPAREN;
          return NULL;
@@ -2365,7 +2408,7 @@
       if (BE (*err != REG_NOERROR && tree == NULL, 0))
        return NULL;
       /* In BRE consecutive duplications are not allowed.  */
-      if ((syntax & REG_CONTEXT_INVALID_DUP)
+      if ((syntax & RE_CONTEXT_INVALID_DUP)
          && (token->type == OP_DUP_ASTERISK
              || token->type == OP_OPEN_DUP_NUM))
        {
@@ -2388,12 +2431,12 @@
 parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
               reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
   bin_tree_t *tree;
   size_t cur_nsub;
   cur_nsub = preg->re_nsub++;
 
-  fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
+  fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
 
   /* The subexpression may be a null string.  */
   if (token->type == OP_CLOSE_SUBEXP)
@@ -2454,7 +2497,7 @@
       if (BE (start == REG_ERROR || end == REG_ERROR, 0))
        {
          /* Invalid sequence.  */
-         if (BE (!(syntax & REG_INVALID_INTERVAL_ORD), 0))
+         if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
            {
              if (token->type == END_OF_RE)
                *err = REG_EBRACE;
@@ -2565,11 +2608,14 @@
      update it.  */
 
 static reg_errcode_t
-build_range_exp (bitset sbcset,
+internal_function
 # ifdef RE_ENABLE_I18N
-                re_charset_t *mbcset, Idx *range_alloc,
-# endif
+build_range_exp (bitset_t sbcset, re_charset_t *mbcset, Idx *range_alloc,
                 bracket_elem_t *start_elem, bracket_elem_t *end_elem)
+# else /* not RE_ENABLE_I18N */
+build_range_exp (bitset_t sbcset, bracket_elem_t *start_elem,
+                bracket_elem_t *end_elem)
+# endif /* not RE_ENABLE_I18N */
 {
   unsigned int start_ch, end_ch;
   /* Equivalence Classes and Character Classes can't be a range start/end.  */
@@ -2589,7 +2635,8 @@
 # ifdef RE_ENABLE_I18N
   {
     wchar_t wc;
-    wint_t start_wc, end_wc;
+    wint_t start_wc;
+    wint_t end_wc;
     wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
 
     start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
@@ -2623,11 +2670,12 @@
            wchar_t *new_array_start, *new_array_end;
            Idx new_nranges;
 
-           new_nranges = mbcset->nranges;
+           /* +1 in case of mbcset->nranges is 0.  */
+           new_nranges = 2 * mbcset->nranges + 1;
            /* Use realloc since mbcset->range_starts and mbcset->range_ends
               are NULL if *range_alloc == 0.  */
-           new_array_start = re_x2realloc (mbcset->range_starts, wchar_t,
-                                           &new_nranges);
+           new_array_start = re_realloc (mbcset->range_starts, wchar_t,
+                                         new_nranges);
            new_array_end = re_realloc (mbcset->range_ends, wchar_t,
                                        new_nranges);
 
@@ -2681,7 +2729,8 @@
    pointer argument since we may update it.  */
 
 static reg_errcode_t
-build_collating_symbol (bitset sbcset,
+internal_function
+build_collating_symbol (bitset_t sbcset,
 # ifdef RE_ENABLE_I18N
                        re_charset_t *mbcset, Idx *coll_sym_alloc,
 # endif
@@ -2719,27 +2768,34 @@
 
   auto inline int32_t
   __attribute ((always_inline))
-  seek_collating_symbol_entry (const unsigned char *name, size_t name_len)
+  seek_collating_symbol_entry (name, name_len)
+        const unsigned char *name;
+        size_t name_len;
     {
       int32_t hash = elem_hash ((const char *) name, name_len);
       int32_t elem = hash % table_size;
-      int32_t second = hash % (table_size - 2);
-      while (symb_table[2 * elem] != 0)
+      if (symb_table[2 * elem] != 0)
        {
-         /* First compare the hashing value.  */
-         if (symb_table[2 * elem] == hash
-             /* Compare the length of the name.  */
-             && name_len == extra[symb_table[2 * elem + 1]]
-             /* Compare the name.  */
-             && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
-                        name_len) == 0)
+         int32_t second = hash % (table_size - 2) + 1;
+
+         do
            {
-             /* Yep, this is the entry.  */
-             break;
-           }
+             /* First compare the hashing value.  */
+             if (symb_table[2 * elem] == hash
+                 /* Compare the length of the name.  */
+                 && name_len == extra[symb_table[2 * elem + 1]]
+                 /* Compare the name.  */
+                 && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
+                            name_len) == 0)
+               {
+                 /* Yep, this is the entry.  */
+                 break;
+               }
 
-         /* Next entry.  */
-         elem += second;
+             /* Next entry.  */
+             elem += second;
+           }
+         while (symb_table[2 * elem] != 0);
        }
       return elem;
     }
@@ -2750,7 +2806,8 @@
 
   auto inline unsigned int
   __attribute ((always_inline))
-  lookup_collation_sequence_value (bracket_elem_t *br_elem)
+  lookup_collation_sequence_value (br_elem)
+        bracket_elem_t *br_elem;
     {
       if (br_elem->type == SB_CHAR)
        {
@@ -2817,9 +2874,11 @@
 
   auto inline reg_errcode_t
   __attribute ((always_inline))
-  build_range_exp (bitset sbcset, re_charset_t *mbcset,
-                  Idx *range_alloc,
-                  bracket_elem_t *start_elem, bracket_elem_t *end_elem)
+  build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
+        re_charset_t *mbcset;
+        Idx *range_alloc;
+        bitset_t sbcset;
+        bracket_elem_t *start_elem, *end_elem;
     {
       unsigned int ch;
       uint32_t start_collseq;
@@ -2837,7 +2896,7 @@
       /* Check start/end collation sequence values.  */
       if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
        return REG_ECOLLATE;
-      if (BE ((syntax & REG_NO_EMPTY_RANGES) && start_collseq > end_collseq, 
0))
+      if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
        return REG_ERANGE;
 
       /* Got valid collation sequence values, add them as a new entry.
@@ -2854,9 +2913,10 @@
              uint32_t *new_array_end;
              Idx new_nranges;
 
-             new_nranges = mbcset->nranges;
-             new_array_start = re_x2realloc (mbcset->range_starts, uint32_t,
-                                             &new_nranges);
+             /* +1 in case of mbcset->nranges is 0.  */
+             new_nranges = 2 * mbcset->nranges + 1;
+             new_array_start = re_realloc (mbcset->range_starts, uint32_t,
+                                           new_nranges);
              new_array_end = re_realloc (mbcset->range_ends, uint32_t,
                                          new_nranges);
 
@@ -2897,8 +2957,11 @@
 
   auto inline reg_errcode_t
   __attribute ((always_inline))
-  build_collating_symbol (bitset sbcset, re_charset_t *mbcset,
-                         Idx *coll_sym_alloc, const unsigned char *name)
+  build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
+        re_charset_t *mbcset;
+        Idx *coll_sym_alloc;
+        bitset_t sbcset;
+        const unsigned char *name;
     {
       int32_t elem, idx;
       size_t name_len = strlen ((const char *) name);
@@ -2927,11 +2990,12 @@
          if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
            {
              /* Not enough, realloc it.  */
-             Idx new_coll_sym_alloc = mbcset->ncoll_syms;
+             /* +1 in case of mbcset->ncoll_syms is 0.  */
+             Idx new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
              /* Use realloc since mbcset->coll_syms is NULL
                 if *alloc == 0.  */
-             int32_t *new_coll_syms = re_x2realloc (mbcset->coll_syms, int32_t,
-                                                    &new_coll_sym_alloc);
+             int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
+                                                  new_coll_sym_alloc);
              if (BE (new_coll_syms == NULL, 0))
                return REG_ESPACE;
              mbcset->coll_syms = new_coll_syms;
@@ -2973,7 +3037,7 @@
       /*
       if (MB_CUR_MAX > 1)
       */
-       collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
+      collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
       table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
       symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
                                                  _NL_COLLATE_SYMB_TABLEMB);
@@ -2981,9 +3045,9 @@
                                                   _NL_COLLATE_SYMB_EXTRAMB);
     }
 #endif
-  sbcset = re_calloc (bitset_word, BITSET_WORDS);
+  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
 #ifdef RE_ENABLE_I18N
-  mbcset = re_calloc (re_charset_t, 1);
+  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
 #endif /* RE_ENABLE_I18N */
 #ifdef RE_ENABLE_I18N
   if (BE (sbcset == NULL || mbcset == NULL, 0))
@@ -3007,7 +3071,7 @@
       mbcset->non_match = 1;
 #endif /* not RE_ENABLE_I18N */
       non_match = true;
-      if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
+      if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
        bitset_set (sbcset, '\0');
       re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
       token_len = peek_token_bracket (token, regexp, syntax);
@@ -3115,10 +3179,11 @@
                {
                  wchar_t *new_mbchars;
                  /* Not enough, realloc it.  */
-                 mbchar_alloc = mbcset->nmbchars;
+                 /* +1 in case of mbcset->nmbchars is 0.  */
+                 mbchar_alloc = 2 * mbcset->nmbchars + 1;
                  /* Use realloc since array is NULL if *alloc == 0.  */
-                 new_mbchars = re_x2realloc (mbcset->mbchars, wchar_t,
-                                             &mbchar_alloc);
+                 new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
+                                           mbchar_alloc);
                  if (BE (new_mbchars == NULL, 0))
                    goto parse_bracket_exp_espace;
                  mbcset->mbchars = new_mbchars;
@@ -3331,13 +3396,14 @@
      is a pointer argument sinse we may update it.  */
 
 static reg_errcode_t
-build_equiv_class (bitset sbcset,
 #ifdef RE_ENABLE_I18N
-                  re_charset_t *mbcset, Idx *equiv_class_alloc,
-#endif
-                  const unsigned char *name)
+build_equiv_class (bitset_t sbcset, re_charset_t *mbcset,
+                  Idx *equiv_class_alloc, const unsigned char *name)
+#else /* not RE_ENABLE_I18N */
+build_equiv_class (bitset_t sbcset, const unsigned char *name)
+#endif /* not RE_ENABLE_I18N */
 {
-#if defined _LIBC
+#ifdef _LIBC
   uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
   if (nrules != 0)
     {
@@ -3392,11 +3458,12 @@
       if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
        {
          /* Not enough, realloc it.  */
-         Idx new_equiv_class_alloc = mbcset->nequiv_classes;
+         /* +1 in case of mbcset->nequiv_classes is 0.  */
+         Idx new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
          /* Use realloc since the array is NULL if *alloc == 0.  */
-         int32_t *new_equiv_classes = re_x2realloc (mbcset->equiv_classes,
-                                                    int32_t,
-                                                    &new_equiv_class_alloc);
+         int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
+                                                  int32_t,
+                                                  new_equiv_class_alloc);
          if (BE (new_equiv_classes == NULL, 0))
            return REG_ESPACE;
          mbcset->equiv_classes = new_equiv_classes;
@@ -3421,18 +3488,21 @@
      is a pointer argument sinse we may update it.  */
 
 static reg_errcode_t
-build_charclass (unsigned REG_TRANSLATE_TYPE trans, bitset sbcset,
 #ifdef RE_ENABLE_I18N
+build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
                 re_charset_t *mbcset, Idx *char_class_alloc,
-#endif
                 const unsigned char *class_name, reg_syntax_t syntax)
+#else /* not RE_ENABLE_I18N */
+build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
+                const unsigned char *class_name, reg_syntax_t syntax)
+#endif /* not RE_ENABLE_I18N */
 {
   int i;
   const char *name = (const char *) class_name;
 
   /* In case of REG_ICASE "upper" and "lower" match the both of
      upper and lower cases.  */
-  if ((syntax & REG_IGNORE_CASE)
+  if ((syntax & RE_ICASE)
       && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
     name = "alpha";
 
@@ -3441,10 +3511,11 @@
   if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
     {
       /* Not enough, realloc it.  */
-      Idx new_char_class_alloc = mbcset->nchar_classes;
+      /* +1 in case of mbcset->nchar_classes is 0.  */
+      Idx new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
       /* Use realloc since array is NULL if *alloc == 0.  */
-      wctype_t *new_char_classes = re_x2realloc (mbcset->char_classes, 
wctype_t,
-                                                &new_char_class_alloc);
+      wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
+                                              new_char_class_alloc);
       if (BE (new_char_classes == NULL, 0))
        return REG_ESPACE;
       mbcset->char_classes = new_char_classes;
@@ -3454,39 +3525,45 @@
 #endif /* RE_ENABLE_I18N */
 
 #define BUILD_CHARCLASS_LOOP(ctype_func)       \
-    for (i = 0; i < SBC_MAX; ++i)              \
+  do {                                         \
+    if (BE (trans != NULL, 0))                 \
       {                                                \
-       if (ctype_func (i))                     \
-         {                                     \
-           int ch = trans ? trans[i] : i;      \
-           bitset_set (sbcset, ch);            \
-         }                                     \
-      }
+       for (i = 0; i < SBC_MAX; ++i)           \
+         if (ctype_func (i))                   \
+           bitset_set (sbcset, trans[i]);      \
+      }                                                \
+    else                                       \
+      {                                                \
+       for (i = 0; i < SBC_MAX; ++i)           \
+         if (ctype_func (i))                   \
+           bitset_set (sbcset, i);             \
+      }                                                \
+  } while (0)
 
   if (strcmp (name, "alnum") == 0)
-    BUILD_CHARCLASS_LOOP (isalnum)
+    BUILD_CHARCLASS_LOOP (isalnum);
   else if (strcmp (name, "cntrl") == 0)
-    BUILD_CHARCLASS_LOOP (iscntrl)
+    BUILD_CHARCLASS_LOOP (iscntrl);
   else if (strcmp (name, "lower") == 0)
-    BUILD_CHARCLASS_LOOP (islower)
+    BUILD_CHARCLASS_LOOP (islower);
   else if (strcmp (name, "space") == 0)
-    BUILD_CHARCLASS_LOOP (isspace)
+    BUILD_CHARCLASS_LOOP (isspace);
   else if (strcmp (name, "alpha") == 0)
-    BUILD_CHARCLASS_LOOP (isalpha)
+    BUILD_CHARCLASS_LOOP (isalpha);
   else if (strcmp (name, "digit") == 0)
-    BUILD_CHARCLASS_LOOP (isdigit)
+    BUILD_CHARCLASS_LOOP (isdigit);
   else if (strcmp (name, "print") == 0)
-    BUILD_CHARCLASS_LOOP (isprint)
+    BUILD_CHARCLASS_LOOP (isprint);
   else if (strcmp (name, "upper") == 0)
-    BUILD_CHARCLASS_LOOP (isupper)
+    BUILD_CHARCLASS_LOOP (isupper);
   else if (strcmp (name, "blank") == 0)
-    BUILD_CHARCLASS_LOOP (isblank)
+    BUILD_CHARCLASS_LOOP (isblank);
   else if (strcmp (name, "graph") == 0)
-    BUILD_CHARCLASS_LOOP (isgraph)
+    BUILD_CHARCLASS_LOOP (isgraph);
   else if (strcmp (name, "punct") == 0)
-    BUILD_CHARCLASS_LOOP (ispunct)
+    BUILD_CHARCLASS_LOOP (ispunct);
   else if (strcmp (name, "xdigit") == 0)
-    BUILD_CHARCLASS_LOOP (isxdigit)
+    BUILD_CHARCLASS_LOOP (isxdigit);
   else
     return REG_ECTYPE;
 
@@ -3494,10 +3571,10 @@
 }
 
 static bin_tree_t *
-build_charclass_op (re_dfa_t *dfa, unsigned REG_TRANSLATE_TYPE trans,
+build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
                    const unsigned char *class_name,
-                   const unsigned char *extra,
-                   bool non_match, reg_errcode_t *err)
+                   const unsigned char *extra, bool non_match,
+                   reg_errcode_t *err)
 {
   re_bitset_ptr_t sbcset;
 #ifdef RE_ENABLE_I18N
@@ -3508,9 +3585,9 @@
   re_token_t br_token;
   bin_tree_t *tree;
 
-  sbcset = re_calloc (bitset_word, BITSET_WORDS);
+  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
 #ifdef RE_ENABLE_I18N
-  mbcset = re_calloc (re_charset_t, 1);
+  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
 #endif /* RE_ENABLE_I18N */
 
 #ifdef RE_ENABLE_I18N
@@ -3527,7 +3604,7 @@
     {
 #ifdef RE_ENABLE_I18N
       /*
-      if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
+      if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
        bitset_set(cset->sbcset, '\0');
       */
       mbcset->non_match = 1;
@@ -3627,7 +3704,7 @@
              || num == REG_ERROR)
             ? REG_ERROR
             : ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
-      num = (num > REG_DUP_MAX) ? REG_ERROR : num;
+      num = (num > RE_DUP_MAX) ? REG_ERROR : num;
     }
   return num;
 }
Index: ccvs/lib/regex.c
diff -u ccvs/lib/regex.c:1.22 ccvs/lib/regex.c:1.23
--- ccvs/lib/regex.c:1.22       Mon Sep 19 21:26:13 2005
+++ ccvs/lib/regex.c    Tue Apr 11 20:21:31 2006
@@ -1,5 +1,5 @@
 /* Extended regular expression matching and search library.
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Isamu Hasegawa <address@hidden>.
 
@@ -21,6 +21,11 @@
 # include <config.h>
 #endif
 
+/* Make sure noone compiles this code with a C++ compiler.  */
+#if defined __cplusplus && defined _LIBC
+# error "This is C code, use a C compiler"
+#endif
+
 #ifdef _LIBC
 /* We have to keep the namespace clean.  */
 # define regfree(preg) __regfree (preg)
Index: ccvs/lib/regex.h
diff -u ccvs/lib/regex.h:1.11 ccvs/lib/regex.h:1.12
--- ccvs/lib/regex.h:1.11       Mon Mar 20 02:21:56 2006
+++ ccvs/lib/regex.h    Tue Apr 11 20:21:31 2006
@@ -28,16 +28,16 @@
 extern "C" {
 #endif
 
-/* Define _REGEX_SOURCE to get definitions that are incompatible with
-   POSIX.  */
-#if (!defined _REGEX_SOURCE                                    \
-     && (defined _GNU_SOURCE                                   \
-        || (!defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE \
-            && !defined _XOPEN_SOURCE)))
-# define _REGEX_SOURCE 1
+/* Define __USE_GNU_REGEX to declare GNU extensions that violate the
+   POSIX name space rules.  */
+#undef __USE_GNU_REGEX
+#if (defined _GNU_SOURCE                                       \
+     || (!defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE    \
+        && !defined _XOPEN_SOURCE))
+# define __USE_GNU_REGEX 1
 #endif
 
-#if defined _REGEX_SOURCE && defined VMS
+#ifdef __VMS
 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
    should be there.  */
 # include <stddef.h>
@@ -58,8 +58,8 @@
    signalled its intention to change the requirement to be that
    regoff_t be at least as wide as ptrdiff_t and ssize_t; see XBD ERN
    60 (2005-08-25).  We don't know of any hosts where ssize_t or
-   ptrdiff_t is wider than long int, so long int is safe.  */
-typedef long int regoff_t;
+   ptrdiff_t is wider than ssize_t, so ssize_t is safe.  */
+typedef ssize_t regoff_t;
 
 /* The type of nonnegative object indexes.  Traditionally, GNU regex
    uses 'int' for these.  Code that uses __re_idx_t should work
@@ -99,20 +99,22 @@
    add or remove a bit, only one other definition need change.  */
 typedef unsigned long int reg_syntax_t;
 
+#ifdef __USE_GNU_REGEX
+
 /* If this bit is not set, then \ inside a bracket expression is literal.
    If set, then such a \ quotes the following character.  */
-#define REG_BACKSLASH_ESCAPE_IN_LISTS 1ul
+# define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
 
 /* If this bit is not set, then + and ? are operators, and \+ and \? are
      literals.
    If set, then \+ and \? are operators and + and ? are literals.  */
-#define REG_BK_PLUS_QM (1ul << 1)
+# define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
 
 /* If this bit is set, then character classes are supported.  They are:
      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
    If not set, then character classes are not supported.  */
-#define REG_CHAR_CLASSES (1ul << 2)
+# define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
 
 /* If this bit is set, then ^ and $ are always anchors (outside bracket
      expressions, of course).
@@ -122,11 +124,11 @@
         $  is an anchor if it is at the end of a regular expression, or
            before a close-group or an alternation operator.
 
-   This bit could be (re)combined with REG_CONTEXT_INDEP_OPS, because
+   This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
    POSIX draft 11.2 says that * etc. in leading positions is undefined.
    We already implemented a previous draft which made those constructs
    invalid, though, so we haven't changed the code back.  */
-#define REG_CONTEXT_INDEP_ANCHORS (1ul << 3)
+# define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
 
 /* If this bit is set, then special characters are always special
      regardless of where they are in the pattern.
@@ -134,70 +136,71 @@
      some contexts; otherwise they are ordinary.  Specifically,
      * + ? and intervals are only special when not after the beginning,
      open-group, or alternation operator.  */
-#define REG_CONTEXT_INDEP_OPS (1ul << 4)
+# define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
 
 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
      immediately after an alternation or begin-group operator.  */
-#define REG_CONTEXT_INVALID_OPS (1ul << 5)
+# define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
 
 /* If this bit is set, then . matches newline.
    If not set, then it doesn't.  */
-#define REG_DOT_NEWLINE (1ul << 6)
+# define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
 
 /* If this bit is set, then . doesn't match NUL.
    If not set, then it does.  */
-#define REG_DOT_NOT_NULL (1ul << 7)
+# define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
 
 /* If this bit is set, nonmatching lists [^...] do not match newline.
    If not set, they do.  */
-#define REG_HAT_LISTS_NOT_NEWLINE (1ul << 8)
+# define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
 
 /* If this bit is set, either \{...\} or {...} defines an
-     interval, depending on REG_NO_BK_BRACES.
+     interval, depending on RE_NO_BK_BRACES.
    If not set, \{, \}, {, and } are literals.  */
-#define REG_INTERVALS (1ul << 9)
+# define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
 
 /* If this bit is set, +, ? and | aren't recognized as operators.
    If not set, they are.  */
-#define REG_LIMITED_OPS (1ul << 10)
+# define RE_LIMITED_OPS (RE_INTERVALS << 1)
 
 /* If this bit is set, newline is an alternation operator.
    If not set, newline is literal.  */
-#define REG_NEWLINE_ALT (1ul << 11)
+# define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
 
 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
      are literals.
   If not set, then `\{...\}' defines an interval.  */
-#define REG_NO_BK_BRACES (1ul << 12)
+# define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
 
 /* If this bit is set, (...) defines a group, and \( and \) are literals.
    If not set, \(...\) defines a group, and ( and ) are literals.  */
-#define REG_NO_BK_PARENS (1ul << 13)
+# define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
 
 /* If this bit is set, then \<digit> matches <digit>.
    If not set, then \<digit> is a back-reference.  */
-#define REG_NO_BK_REFS (1ul << 14)
+# define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
 
 /* If this bit is set, then | is an alternation operator, and \| is literal.
    If not set, then \| is an alternation operator, and | is literal.  */
-#define REG_NO_BK_VBAR (1ul << 15)
+# define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
 
 /* If this bit is set, then an ending range point collating higher
      than the starting range point, as in [z-a], is invalid.
-   If not set, the containing range is empty and does not match any string.  */
-#define REG_NO_EMPTY_RANGES (1ul << 16)
+   If not set, then when ending range point collates higher than the
+     starting range point, the range is ignored.  */
+# define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
 
 /* If this bit is set, then an unmatched ) is ordinary.
    If not set, then an unmatched ) is invalid.  */
-#define REG_UNMATCHED_RIGHT_PAREN_ORD (1ul << 17)
+# define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
 
 /* If this bit is set, succeed as soon as we match the whole pattern,
    without further backtracking.  */
-#define REG_NO_POSIX_BACKTRACKING (1ul << 18)
+# define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
 
 /* If this bit is set, do not process the GNU regex operators.
    If not set, then the GNU regex operators are recognized. */
-#define REG_NO_GNU_OPS (1ul << 19)
+# define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
 
 /* If this bit is set, turn on internal regex debugging.
    If not set, and debugging was on, turn it off.
@@ -205,29 +208,31 @@
    We define this bit always, so that all that's needed to turn on
    debugging is to recompile regex.c; the calling code can always have
    this bit set, and it won't affect anything in the normal case. */
-#define REG_DEBUG (1ul << 20)
+# define RE_DEBUG (RE_NO_GNU_OPS << 1)
 
 /* If this bit is set, a syntactically invalid interval is treated as
    a string of ordinary characters.  For example, the ERE 'a{1' is
    treated as 'a\{1'.  */
-#define REG_INVALID_INTERVAL_ORD (1ul << 21)
+# define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
 
 /* If this bit is set, then ignore case when matching.
    If not set, then case is significant.  */
-#define REG_IGNORE_CASE (1ul << 22)
+# define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
 
-/* This bit is used internally like REG_CONTEXT_INDEP_ANCHORS but only
+/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only
    for ^, because it is difficult to scan the regex backwards to find
    whether ^ should be special.  */
-#define REG_CARET_ANCHORS_HERE (1ul << 23)
+# define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)
 
 /* If this bit is set, then \{ cannot be first in an bre or
    immediately after an alternation or begin-group operator.  */
-#define REG_CONTEXT_INVALID_DUP (1ul << 24)
+# define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)
 
 /* If this bit is set, then no_sub will be set to 1 during
    re_compile_pattern.  */
-#define REG_NO_SUB (1ul << 25)
+# define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)
+
+#endif /* defined __USE_GNU_REGEX */
 
 /* This global variable defines the particular regexp syntax to use (for
    some interfaces).  When a regexp is compiled, the syntax used is
@@ -235,82 +240,92 @@
    already-compiled regexps.  */
 extern reg_syntax_t re_syntax_options;
 
+#ifdef __USE_GNU_REGEX
 /* Define combinations of the above bits for the standard possibilities.
    (The [[[ comments delimit what gets put into the Texinfo file, so
    don't delete them!)  */
 /* [[[begin syntaxes]]] */
-#define REG_SYNTAX_EMACS 0
+# define RE_SYNTAX_EMACS 0
 
-#define REG_SYNTAX_AWK                                                 \
-  (REG_BACKSLASH_ESCAPE_IN_LISTS   | REG_DOT_NOT_NULL                  \
-   | REG_NO_BK_PARENS             | REG_NO_BK_REFS                     \
-   | REG_NO_BK_VBAR               | REG_NO_EMPTY_RANGES                \
-   | REG_DOT_NEWLINE              | REG_CONTEXT_INDEP_ANCHORS          \
-   | REG_UNMATCHED_RIGHT_PAREN_ORD | REG_NO_GNU_OPS)
-
-#define REG_SYNTAX_GNU_AWK                                             \
-  ((REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS          \
-    | REG_DEBUG)                                                       \
-   & ~(REG_DOT_NOT_NULL | REG_INTERVALS | REG_CONTEXT_INDEP_OPS                
\
-       | REG_CONTEXT_INVALID_OPS ))
-
-#define REG_SYNTAX_POSIX_AWK                                           \
-  (REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS           \
-   | REG_INTERVALS          | REG_NO_GNU_OPS)
-
-#define REG_SYNTAX_GREP                                                        
\
-  (REG_BK_PLUS_QM             | REG_CHAR_CLASSES                       \
-   | REG_HAT_LISTS_NOT_NEWLINE | REG_INTERVALS                         \
-   | REG_NEWLINE_ALT)
-
-#define REG_SYNTAX_EGREP                                               \
-  (REG_CHAR_CLASSES       | REG_CONTEXT_INDEP_ANCHORS                  \
-   | REG_CONTEXT_INDEP_OPS | REG_HAT_LISTS_NOT_NEWLINE                 \
-   | REG_NEWLINE_ALT      | REG_NO_BK_PARENS                           \
-   | REG_NO_BK_VBAR)
-
-#define REG_SYNTAX_POSIX_EGREP                                         \
-  (REG_SYNTAX_EGREP | REG_INTERVALS | REG_NO_BK_BRACES                 \
-   | REG_INVALID_INTERVAL_ORD)
+# define RE_SYNTAX_AWK                                                 \
+  (RE_BACKSLASH_ESCAPE_IN_LISTS   | RE_DOT_NOT_NULL                    \
+   | RE_NO_BK_PARENS              | RE_NO_BK_REFS                      \
+   | RE_NO_BK_VBAR                | RE_NO_EMPTY_RANGES                 \
+   | RE_DOT_NEWLINE              | RE_CONTEXT_INDEP_ANCHORS            \
+   | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
+
+# define RE_SYNTAX_GNU_AWK                                             \
+  ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG)        
\
+   & ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS           \
+       | RE_CONTEXT_INVALID_OPS ))
+
+# define RE_SYNTAX_POSIX_AWK                                           \
+  (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS             \
+   | RE_INTERVALS          | RE_NO_GNU_OPS)
+
+# define RE_SYNTAX_GREP                                                        
\
+  (RE_BK_PLUS_QM              | RE_CHAR_CLASSES                                
\
+   | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS                           \
+   | RE_NEWLINE_ALT)
+
+# define RE_SYNTAX_EGREP                                               \
+  (RE_CHAR_CLASSES        | RE_CONTEXT_INDEP_ANCHORS                   \
+   | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE                   \
+   | RE_NEWLINE_ALT       | RE_NO_BK_PARENS                            \
+   | RE_NO_BK_VBAR)
+
+# define RE_SYNTAX_POSIX_EGREP                                         \
+  (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES                    \
+   | RE_INVALID_INTERVAL_ORD)
 
 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff.  */
-#define REG_SYNTAX_ED REG_SYNTAX_POSIX_BASIC
+# define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
 
-#define REG_SYNTAX_SED REG_SYNTAX_POSIX_BASIC
+# define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
 
 /* Syntax bits common to both basic and extended POSIX regex syntax.  */
-#define _REG_SYNTAX_POSIX_COMMON                                       \
-  (REG_CHAR_CLASSES | REG_DOT_NEWLINE   | REG_DOT_NOT_NULL             \
-   | REG_INTERVALS  | REG_NO_EMPTY_RANGES)
+# define _RE_SYNTAX_POSIX_COMMON                                       \
+  (RE_CHAR_CLASSES | RE_DOT_NEWLINE      | RE_DOT_NOT_NULL             \
+   | RE_INTERVALS  | RE_NO_EMPTY_RANGES)
 
-#define REG_SYNTAX_POSIX_BASIC                                         \
-  (_REG_SYNTAX_POSIX_COMMON | REG_BK_PLUS_QM | REG_CONTEXT_INVALID_DUP)
+# define RE_SYNTAX_POSIX_BASIC                                         \
+  (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)
 
-/* Differs from ..._POSIX_BASIC only in that REG_BK_PLUS_QM becomes
-   REG_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
+/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
+   RE_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
    isn't minimal, since other operators, such as \`, aren't disabled.  */
-#define REG_SYNTAX_POSIX_MINIMAL_BASIC                                 \
-  (_REG_SYNTAX_POSIX_COMMON | REG_LIMITED_OPS)
+# define RE_SYNTAX_POSIX_MINIMAL_BASIC                                 \
+  (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
 
-#define REG_SYNTAX_POSIX_EXTENDED                                      \
-  (_REG_SYNTAX_POSIX_COMMON  | REG_CONTEXT_INDEP_ANCHORS               \
-   | REG_CONTEXT_INDEP_OPS   | REG_NO_BK_BRACES                                
\
-   | REG_NO_BK_PARENS       | REG_NO_BK_VBAR                           \
-   | REG_CONTEXT_INVALID_OPS | REG_UNMATCHED_RIGHT_PAREN_ORD)
-
-/* Differs from ..._POSIX_EXTENDED in that REG_CONTEXT_INDEP_OPS is
-   removed and REG_NO_BK_REFS is added.  */
-#define REG_SYNTAX_POSIX_MINIMAL_EXTENDED                              \
-  (_REG_SYNTAX_POSIX_COMMON  | REG_CONTEXT_INDEP_ANCHORS               \
-   | REG_CONTEXT_INVALID_OPS | REG_NO_BK_BRACES                                
\
-   | REG_NO_BK_PARENS       | REG_NO_BK_REFS                           \
-   | REG_NO_BK_VBAR         | REG_UNMATCHED_RIGHT_PAREN_ORD)
+# define RE_SYNTAX_POSIX_EXTENDED                                      \
+  (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS                 \
+   | RE_CONTEXT_INDEP_OPS   | RE_NO_BK_BRACES                          \
+   | RE_NO_BK_PARENS        | RE_NO_BK_VBAR                            \
+   | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
+
+/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
+   removed and RE_NO_BK_REFS is added.  */
+# define RE_SYNTAX_POSIX_MINIMAL_EXTENDED                              \
+  (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS                 \
+   | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES                          \
+   | RE_NO_BK_PARENS        | RE_NO_BK_REFS                            \
+   | RE_NO_BK_VBAR         | RE_UNMATCHED_RIGHT_PAREN_ORD)
 /* [[[end syntaxes]]] */
+
+#endif /* defined __USE_GNU_REGEX */
 
-/* Maximum number of duplicates an interval can allow.  This is
-   distinct from RE_DUP_MAX, to conform to POSIX name space rules and
-   to avoid collisions with <limits.h>.  */
-#define REG_DUP_MAX 32767
+#ifdef __USE_GNU_REGEX
+
+/* Maximum number of duplicates an interval can allow.  POSIX-conforming
+   systems might define this in <limits.h>, but we want our
+   value, so remove any previous define.  */
+# ifdef RE_DUP_MAX
+#  undef RE_DUP_MAX
+# endif
+/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows.  */
+# define RE_DUP_MAX (0x7fff)
+
+#endif /* defined __USE_GNU_REGEX */
 
 
 /* POSIX `cflags' bits (i.e., information for `regcomp').  */
@@ -356,72 +371,55 @@
 typedef enum
 {
   _REG_ENOSYS = -1,    /* This will never happen for this implementation.  */
-#define REG_ENOSYS     _REG_ENOSYS
-
-  _REG_NOERROR,                /* Success.  */
-#define REG_NOERROR    _REG_NOERROR
-
+  _REG_NOERROR = 0,    /* Success.  */
   _REG_NOMATCH,                /* Didn't find a match (for regexec).  */
-#define REG_NOMATCH    _REG_NOMATCH
 
   /* POSIX regcomp return error codes.  (In the order listed in the
      standard.)  */
-
   _REG_BADPAT,         /* Invalid pattern.  */
-#define REG_BADPAT     _REG_BADPAT
-
-  _REG_ECOLLATE,       /* Inalid collating element.  */
-#define REG_ECOLLATE   _REG_ECOLLATE
-
+  _REG_ECOLLATE,       /* Invalid collating element.  */
   _REG_ECTYPE,         /* Invalid character class name.  */
-#define REG_ECTYPE     _REG_ECTYPE
-
   _REG_EESCAPE,                /* Trailing backslash.  */
-#define REG_EESCAPE    _REG_EESCAPE
-
   _REG_ESUBREG,                /* Invalid back reference.  */
-#define REG_ESUBREG    _REG_ESUBREG
-
   _REG_EBRACK,         /* Unmatched left bracket.  */
-#define REG_EBRACK     _REG_EBRACK
-
   _REG_EPAREN,         /* Parenthesis imbalance.  */
-#define REG_EPAREN     _REG_EPAREN
-
   _REG_EBRACE,         /* Unmatched \{.  */
-#define REG_EBRACE     _REG_EBRACE
-
   _REG_BADBR,          /* Invalid contents of \{\}.  */
-#define REG_BADBR      _REG_BADBR
-
   _REG_ERANGE,         /* Invalid range end.  */
-#define REG_ERANGE     _REG_ERANGE
-
   _REG_ESPACE,         /* Ran out of memory.  */
-#define REG_ESPACE     _REG_ESPACE
-
   _REG_BADRPT,         /* No preceding re for repetition op.  */
-#define REG_BADRPT     _REG_BADRPT
 
   /* Error codes we've added.  */
-
   _REG_EEND,           /* Premature end.  */
-#define REG_EEND       _REG_EEND
-
   _REG_ESIZE,          /* Compiled pattern bigger than 2^16 bytes.  */
-#define REG_ESIZE      _REG_ESIZE
-
   _REG_ERPAREN         /* Unmatched ) or \); not returned from regcomp.  */
-#define REG_ERPAREN    _REG_ERPAREN
-
 } reg_errcode_t;
+
+#ifdef _XOPEN_SOURCE
+# define REG_ENOSYS    _REG_ENOSYS
+#endif
+#define REG_NOERROR    _REG_NOERROR
+#define REG_NOMATCH    _REG_NOMATCH
+#define REG_BADPAT     _REG_BADPAT
+#define REG_ECOLLATE   _REG_ECOLLATE
+#define REG_ECTYPE     _REG_ECTYPE
+#define REG_EESCAPE    _REG_EESCAPE
+#define REG_ESUBREG    _REG_ESUBREG
+#define REG_EBRACK     _REG_EBRACK
+#define REG_EPAREN     _REG_EPAREN
+#define REG_EBRACE     _REG_EBRACE
+#define REG_BADBR      _REG_BADBR
+#define REG_ERANGE     _REG_ERANGE
+#define REG_ESPACE     _REG_ESPACE
+#define REG_BADRPT     _REG_BADRPT
+#define REG_EEND       _REG_EEND
+#define REG_ESIZE      _REG_ESIZE
+#define REG_ERPAREN    _REG_ERPAREN
 
-/* In the traditional GNU implementation, regex.h defined member names
-   like `buffer' that POSIX does not allow.  These members now have
-   names with leading `re_' (e.g., `re_buffer').  Support the old
-   names only if _REGEX_SOURCE is defined.  New programs should use
-   the new names.  */
-#ifdef _REGEX_SOURCE
+/* struct re_pattern_buffer normally uses member names like `buffer'
+   that POSIX does not allow.  In POSIX mode these members have names
+   with leading `re_' (e.g., `re_buffer').  */
+#ifdef __USE_GNU_REGEX
 # define _REG_RE_NAME(id) id
 # define _REG_RM_NAME(id) id
 #else
@@ -430,88 +428,87 @@
 #endif
 
 /* The user can specify the type of the re_translate member by
-   defining the macro REG_TRANSLATE_TYPE.  In the traditional GNU
-   implementation, this macro was named RE_TRANSLATE_TYPE, but POSIX
-   does not allow this.  Support the old name only if _REGEX_SOURCE
-   and if the new name is not defined. New programs should use the new
-   name.  */
-#ifndef REG_TRANSLATE_TYPE
-# if defined _REGEX_SOURCE && defined RE_TRANSLATE_TYPE
-#  define REG_TRANSLATE_TYPE RE_TRANSLATE_TYPE
-# else
-#  define REG_TRANSLATE_TYPE char *
+   defining the macro RE_TRANSLATE_TYPE, which defaults to unsigned
+   char *.  This pollutes the POSIX name space, so in POSIX mode just
+   use unsigned char *.  */
+#ifdef __USE_GNU_REGEX
+# ifndef RE_TRANSLATE_TYPE
+#  define RE_TRANSLATE_TYPE unsigned char *
 # endif
+# define REG_TRANSLATE_TYPE RE_TRANSLATE_TYPE
+#else
+# define REG_TRANSLATE_TYPE unsigned char *
 #endif
 
 /* This data structure represents a compiled pattern.  Before calling
-   the pattern compiler), the fields `re_buffer', `re_allocated', `re_fastmap',
-   `re_translate', and `re_no_sub' can be set.  After the pattern has been
+   the pattern compiler, the fields `buffer', `allocated', `fastmap',
+   `translate', and `no_sub' can be set.  After the pattern has been
    compiled, the `re_nsub' field is available.  All other fields are
    private to the regex routines.  */
 
 struct re_pattern_buffer
 {
-/* [[[begin pattern_buffer]]] */
-       /* Space that holds the compiled pattern.  It is declared as
-          `unsigned char *' because its elements are
-           sometimes used as array indexes.  */
+  /* Space that holds the compiled pattern.  It is declared as
+     `unsigned char *' because its elements are sometimes used as
+     array indexes.  */
   unsigned char *_REG_RE_NAME (buffer);
 
-       /* Number of bytes to which `re_buffer' points.  */
+  /* Number of bytes to which `buffer' points.  */
   __re_long_size_t _REG_RE_NAME (allocated);
 
-       /* Number of bytes actually used in `re_buffer'.  */
+  /* Number of bytes actually used in `buffer'.  */
   __re_long_size_t _REG_RE_NAME (used);
 
-        /* Syntax setting with which the pattern was compiled.  */
+  /* Syntax setting with which the pattern was compiled.  */
   reg_syntax_t _REG_RE_NAME (syntax);
 
-        /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
-           the fastmap, if there is one, to skip over impossible
-           starting points for matches.  */
+  /* Pointer to a fastmap, if any, otherwise zero.  re_search uses the
+     fastmap, if there is one, to skip over impossible starting points
+     for matches.  */
   char *_REG_RE_NAME (fastmap);
 
-        /* Either a translate table to apply to all characters before
-           comparing them, or zero for no translation.  The translation
-           is applied to a pattern when it is compiled and to a string
-           when it is matched.  */
+  /* Either a translate table to apply to all characters before
+     comparing them, or zero for no translation.  The translation is
+     applied to a pattern when it is compiled and to a string when it
+     is matched.  */
   REG_TRANSLATE_TYPE _REG_RE_NAME (translate);
 
-       /* Number of subexpressions found by the compiler.  */
+  /* Number of subexpressions found by the compiler.  */
   size_t re_nsub;
 
-        /* Zero if this pattern cannot match the empty string, one else.
-           Well, in truth it's used only in `re_search_2', to see
-           whether or not we should use the fastmap, so we don't set
-           this absolutely perfectly; see `re_compile_fastmap' (the
-           `duplicate' case).  */
+  /* Zero if this pattern cannot match the empty string, one else.
+     Well, in truth it's used only in `re_search_2', to see whether or
+     not we should use the fastmap, so we don't set this absolutely
+     perfectly; see `re_compile_fastmap' (the `duplicate' case).  */
   unsigned int _REG_RE_NAME (can_be_null) : 1;
 
-        /* If REG_UNALLOCATED, allocate space in the `regs' structure
-             for `max (REG_NREGS, re_nsub + 1)' groups.
-           If REG_REALLOCATE, reallocate space if necessary.
-           If REG_FIXED, use what's there.  */
-#define REG_UNALLOCATED 0
-#define REG_REALLOCATE 1
-#define REG_FIXED 2
+  /* If REGS_UNALLOCATED, allocate space in the `regs' structure
+     for `max (RE_NREGS, re_nsub + 1)' groups.
+     If REGS_REALLOCATE, reallocate space if necessary.
+     If REGS_FIXED, use what's there.  */
+#ifdef __USE_GNU_REGEX
+# define REGS_UNALLOCATED 0
+# define REGS_REALLOCATE 1
+# define REGS_FIXED 2
+#endif
   unsigned int _REG_RE_NAME (regs_allocated) : 2;
 
-        /* Set to zero when `regex_compile' compiles a pattern; set to one
-           by `re_compile_fastmap' if it updates the fastmap.  */
+  /* Set to zero when `regex_compile' compiles a pattern; set to one
+     by `re_compile_fastmap' if it updates the fastmap.  */
   unsigned int _REG_RE_NAME (fastmap_accurate) : 1;
 
-        /* If set, `re_match_2' does not return information about
-           subexpressions.  */
+  /* If set, `re_match_2' does not return information about
+     subexpressions.  */
   unsigned int _REG_RE_NAME (no_sub) : 1;
 
-        /* If set, a beginning-of-line anchor doesn't match at the
-           beginning of the string.  */
+  /* If set, a beginning-of-line anchor doesn't match at the beginning
+     of the string.  */
   unsigned int _REG_RE_NAME (not_bol) : 1;
 
-        /* Similarly for an end-of-line anchor.  */
+  /* Similarly for an end-of-line anchor.  */
   unsigned int _REG_RE_NAME (not_eol) : 1;
 
-        /* If true, an anchor at a newline matches.  */
+  /* If true, an anchor at a newline matches.  */
   unsigned int _REG_RE_NAME (newline_anchor) : 1;
 
 /* [[[end pattern_buffer]]] */
@@ -529,11 +526,11 @@
 };
 
 
-/* If `regs_allocated' is REG_UNALLOCATED in the pattern buffer,
+/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
    `re_match_2' returns information about at least this many registers
    the first time a `regs' structure is passed.  */
-#ifndef REG_NREGS
-# define REG_NREGS 30
+#if !defined RE_NREGS && defined __USE_GNU_REGEX
+# define RE_NREGS 30
 #endif
 
 
@@ -569,7 +566,7 @@
    compiled into BUFFER.  Start searching at position START, for RANGE
    characters.  Return the starting position of the match, -1 for no
    match, or -2 for an internal error.  Also return register
-   information in REGS (if REGS and BUFFER->re_no_sub are nonzero).  */
+   information in REGS (if REGS and BUFFER->no_sub are nonzero).  */
 extern regoff_t re_search (struct re_pattern_buffer *__buffer,
                           const char *__string, __re_idx_t __length,
                           __re_idx_t __start, regoff_t __range,
@@ -662,110 +659,8 @@
 extern void regfree (regex_t *__preg);
 
 
-#ifdef _REGEX_SOURCE
-
-/* Define the POSIX-compatible member names in terms of the
-   incompatible (and deprecated) names established by _REG_RE_NAME.
-   New programs should use the re_* names.  */
-
-# define re_allocated allocated
-# define re_buffer buffer
-# define re_can_be_null can_be_null
-# define re_fastmap fastmap
-# define re_fastmap_accurate fastmap_accurate
-# define re_newline_anchor newline_anchor
-# define re_no_sub no_sub
-# define re_not_bol not_bol
-# define re_not_eol not_eol
-# define re_regs_allocated regs_allocated
-# define re_syntax syntax
-# define re_translate translate
-# define re_used used
-
-/* Similarly for _REG_RM_NAME.  */
-
-# define rm_end end
-# define rm_num_regs num_regs
-# define rm_start start
-
-/* Undef RE_DUP_MAX first, in case the user has already included a
-   <limits.h> with an incompatible definition.
-
-   On GNU systems, the most common spelling for RE_DUP_MAX's value in
-   <limits.h> is (0x7ffff), so define RE_DUP_MAX to that, not to
-   REG_DUP_MAX.  This avoid some duplicate-macro-definition warnings
-   with programs that include <limits.h> after this file.
-
-   New programs should not assume that regex.h defines RE_DUP_MAX; to
-   get the value of RE_DUP_MAX, they should instead include <limits.h>
-   and possibly invoke the sysconf function.  */
-
-# undef RE_DUP_MAX
-# define RE_DUP_MAX (0x7fff)
-
-/* Define the following symbols for backward source compatibility.
-   These symbols violate the POSIX name space rules, and new programs
-   should avoid them.  */
-
-# define REGS_FIXED REG_FIXED
-# define REGS_REALLOCATE REG_REALLOCATE
-# define REGS_UNALLOCATED REG_UNALLOCATED
-# define RE_BACKSLASH_ESCAPE_IN_LISTS REG_BACKSLASH_ESCAPE_IN_LISTS
-# define RE_BK_PLUS_QM REG_BK_PLUS_QM
-# define RE_CARET_ANCHORS_HERE REG_CARET_ANCHORS_HERE
-# define RE_CHAR_CLASSES REG_CHAR_CLASSES
-# define RE_CONTEXT_INDEP_ANCHORS REG_CONTEXT_INDEP_ANCHORS
-# define RE_CONTEXT_INDEP_OPS REG_CONTEXT_INDEP_OPS
-# define RE_CONTEXT_INVALID_DUP REG_CONTEXT_INVALID_DUP
-# define RE_CONTEXT_INVALID_OPS REG_CONTEXT_INVALID_OPS
-# define RE_DEBUG REG_DEBUG
-# define RE_DOT_NEWLINE REG_DOT_NEWLINE
-# define RE_DOT_NOT_NULL REG_DOT_NOT_NULL
-# define RE_HAT_LISTS_NOT_NEWLINE REG_HAT_LISTS_NOT_NEWLINE
-# define RE_ICASE REG_IGNORE_CASE /* avoid collision with REG_ICASE */
-# define RE_INTERVALS REG_INTERVALS
-# define RE_INVALID_INTERVAL_ORD REG_INVALID_INTERVAL_ORD
-# define RE_LIMITED_OPS REG_LIMITED_OPS
-# define RE_NEWLINE_ALT REG_NEWLINE_ALT
-# define RE_NO_BK_BRACES REG_NO_BK_BRACES
-# define RE_NO_BK_PARENS REG_NO_BK_PARENS
-# define RE_NO_BK_REFS REG_NO_BK_REFS
-# define RE_NO_BK_VBAR REG_NO_BK_VBAR
-# define RE_NO_EMPTY_RANGES REG_NO_EMPTY_RANGES
-# define RE_NO_GNU_OPS REG_NO_GNU_OPS
-# define RE_NO_POSIX_BACKTRACKING REG_NO_POSIX_BACKTRACKING
-# define RE_NO_SUB REG_NO_SUB
-# define RE_NREGS REG_NREGS
-# define RE_SYNTAX_AWK REG_SYNTAX_AWK
-# define RE_SYNTAX_ED REG_SYNTAX_ED
-# define RE_SYNTAX_EGREP REG_SYNTAX_EGREP
-# define RE_SYNTAX_EMACS REG_SYNTAX_EMACS
-# define RE_SYNTAX_GNU_AWK REG_SYNTAX_GNU_AWK
-# define RE_SYNTAX_GREP REG_SYNTAX_GREP
-# define RE_SYNTAX_POSIX_AWK REG_SYNTAX_POSIX_AWK
-# define RE_SYNTAX_POSIX_BASIC REG_SYNTAX_POSIX_BASIC
-# define RE_SYNTAX_POSIX_EGREP REG_SYNTAX_POSIX_EGREP
-# define RE_SYNTAX_POSIX_EXTENDED REG_SYNTAX_POSIX_EXTENDED
-# define RE_SYNTAX_POSIX_MINIMAL_BASIC REG_SYNTAX_POSIX_MINIMAL_BASIC
-# define RE_SYNTAX_POSIX_MINIMAL_EXTENDED REG_SYNTAX_POSIX_MINIMAL_EXTENDED
-# define RE_SYNTAX_SED REG_SYNTAX_SED
-# define RE_UNMATCHED_RIGHT_PAREN_ORD REG_UNMATCHED_RIGHT_PAREN_ORD
-# ifndef RE_TRANSLATE_TYPE
-#  define RE_TRANSLATE_TYPE REG_TRANSLATE_TYPE
-# endif
-
-#endif /* defined _REGEX_SOURCE */
-
 #ifdef __cplusplus
 }
 #endif /* C++ */
 
 #endif /* regex.h */
-
-/*
-Local variables:
-make-backup-files: t
-version-control: t
-trim-versions-without-asking: nil
-End:
-*/
Index: ccvs/lib/regex_internal.c
diff -u ccvs/lib/regex_internal.c:1.7 ccvs/lib/regex_internal.c:1.8
--- ccvs/lib/regex_internal.c:1.7       Tue Sep 20 16:47:52 2005
+++ ccvs/lib/regex_internal.c   Tue Apr 11 20:21:31 2006
@@ -1,5 +1,5 @@
 /* Extended regular expression matching and search library.
-   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Isamu Hasegawa <address@hidden>.
 
@@ -19,7 +19,7 @@
 
 static void re_string_construct_common (const char *str, Idx len,
                                        re_string_t *pstr,
-                                       REG_TRANSLATE_TYPE trans, bool icase,
+                                       RE_TRANSLATE_TYPE trans, bool icase,
                                        const re_dfa_t *dfa) internal_function;
 static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
                                          const re_node_set *nodes,
@@ -37,7 +37,7 @@
 static reg_errcode_t
 internal_function
 re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
-                   REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
+                   RE_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
 {
   reg_errcode_t ret;
   Idx init_buf_len;
@@ -65,7 +65,7 @@
 static reg_errcode_t
 internal_function
 re_string_construct (re_string_t *pstr, const char *str, Idx len,
-                    REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
+                    RE_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
 {
   reg_errcode_t ret;
   memset (pstr, '\0', sizeof (re_string_t));
@@ -132,13 +132,20 @@
 #ifdef RE_ENABLE_I18N
   if (pstr->mb_cur_max > 1)
     {
-      wint_t *new_wcs = re_xrealloc (pstr->wcs, wint_t, new_buf_len);
+      wint_t *new_wcs;
+
+      /* Avoid overflow.  */
+      size_t max_object_size = MAX (sizeof (wint_t), sizeof (Idx));
+      if (BE (SIZE_MAX / max_object_size < new_buf_len, 0))
+       return REG_ESPACE;
+
+      new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
       if (BE (new_wcs == NULL, 0))
        return REG_ESPACE;
       pstr->wcs = new_wcs;
       if (pstr->offsets != NULL)
        {
-         Idx *new_offsets = re_xrealloc (pstr->offsets, Idx, new_buf_len);
+         Idx *new_offsets = re_realloc (pstr->offsets, Idx, new_buf_len);
          if (BE (new_offsets == NULL, 0))
            return REG_ESPACE;
          pstr->offsets = new_offsets;
@@ -161,13 +168,13 @@
 static void
 internal_function
 re_string_construct_common (const char *str, Idx len, re_string_t *pstr,
-                           REG_TRANSLATE_TYPE trans, bool icase,
+                           RE_TRANSLATE_TYPE trans, bool icase,
                            const re_dfa_t *dfa)
 {
   pstr->raw_mbs = (const unsigned char *) str;
   pstr->len = len;
   pstr->raw_len = len;
-  pstr->trans = (unsigned REG_TRANSLATE_TYPE) trans;
+  pstr->trans = trans;
   pstr->icase = icase;
   pstr->mbs_allocated = (trans != NULL || icase);
   pstr->mb_cur_max = dfa->mb_cur_max;
@@ -301,7 +308,7 @@
          mbclen = mbrtowc (&wc,
                            ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
                             + byte_idx), remain_len, &pstr->cur_state);
-         if (BE ((size_t) (mbclen + 2) > 2, 1))
+         if (BE (mbclen < (size_t) -2, 1))
            {
              wchar_t wcu = wc;
              if (iswlower (wc))
@@ -369,7 +376,7 @@
        else
          p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
        mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
-       if (BE ((size_t) (mbclen + 2) > 2, 1))
+       if (BE (mbclen < (size_t) -2, 1))
          {
            wchar_t wcu = wc;
            if (iswlower (wc))
@@ -392,7 +399,7 @@
 
                    if (pstr->offsets == NULL)
                      {
-                       pstr->offsets = re_xmalloc (Idx, pstr->bufs_len);
+                       pstr->offsets = re_malloc (Idx, pstr->bufs_len);
 
                        if (pstr->offsets == NULL)
                          return REG_ESPACE;
@@ -635,37 +642,50 @@
                     byte other than 0x80 - 0xbf.  */
                  raw = pstr->raw_mbs + pstr->raw_mbs_idx;
                  end = raw + (offset - pstr->mb_cur_max);
-                 for (p = raw + offset - 1; p >= end; --p)
-                   if ((*p & 0xc0) != 0x80)
-                     {
-                       mbstate_t cur_state;
-                       wchar_t wc2;
-                       Idx mlen = raw + pstr->len - p;
-                       unsigned char buf[6];
-                       size_t mbclen;
-
-                       q = p;
-                       if (BE (pstr->trans != NULL, 0))
-                         {
-                           int i = mlen < 6 ? mlen : 6;
-                           while (--i >= 0)
-                             buf[i] = pstr->trans[p[i]];
-                           q = buf;
-                         }
-                       /* XXX Don't use mbrtowc, we know which conversion
-                          to use (UTF-8 -> UCS4).  */
-                       memset (&cur_state, 0, sizeof (cur_state));
-                       mbclen = mbrtowc (&wc2, (const char *) p, mlen,
-                                         &cur_state);
-                       if (raw + offset - p <= mbclen && mbclen < (size_t) -2)
-                         {
-                           memset (&pstr->cur_state, '\0',
-                                   sizeof (mbstate_t));
-                           pstr->valid_len = mbclen - (raw + offset - p);
-                           wc = wc2;
-                         }
-                       break;
-                     }
+                 p = raw + offset - 1;
+#ifdef _LIBC
+                 /* We know the wchar_t encoding is UCS4, so for the simple
+                    case, ASCII characters, skip the conversion step.  */
+                 if (isascii (*p) && BE (pstr->trans == NULL, 1))
+                   {
+                     memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
+                     pstr->valid_len = 0;
+                     wc = (wchar_t) *p;
+                   }
+                 else
+#endif
+                   for (; p >= end; --p)
+                     if ((*p & 0xc0) != 0x80)
+                       {
+                         mbstate_t cur_state;
+                         wchar_t wc2;
+                         Idx mlen = raw + pstr->len - p;
+                         unsigned char buf[6];
+                         size_t mbclen;
+
+                         q = p;
+                         if (BE (pstr->trans != NULL, 0))
+                           {
+                             int i = mlen < 6 ? mlen : 6;
+                             while (--i >= 0)
+                               buf[i] = pstr->trans[p[i]];
+                             q = buf;
+                           }
+                         /* XXX Don't use mbrtowc, we know which conversion
+                            to use (UTF-8 -> UCS4).  */
+                         memset (&cur_state, 0, sizeof (cur_state));
+                         mbclen = mbrtowc (&wc2, (const char *) p, mlen,
+                                           &cur_state);
+                         if (raw + offset - p <= mbclen
+                             && mbclen < (size_t) -2)
+                           {
+                             memset (&pstr->cur_state, '\0',
+                                     sizeof (mbstate_t));
+                             pstr->valid_len = mbclen - (raw + offset - p);
+                             wc = wc2;
+                           }
+                         break;
+                       }
                }
 
              if (wc == WEOF)
@@ -719,15 +739,15 @@
     }
   else
 #endif /* RE_ENABLE_I18N */
-  if (BE (pstr->mbs_allocated, 0))
-    {
-      if (pstr->icase)
-       build_upper_buffer (pstr);
-      else if (pstr->trans != NULL)
-       re_string_translate_buffer (pstr);
-    }
-  else
-    pstr->valid_len = pstr->len;
+    if (BE (pstr->mbs_allocated, 0))
+      {
+       if (pstr->icase)
+         build_upper_buffer (pstr);
+       else if (pstr->trans != NULL)
+         re_string_translate_buffer (pstr);
+      }
+    else
+      pstr->valid_len = pstr->len;
 
   pstr->cur_idx = 0;
   return REG_NOERROR;
@@ -873,7 +893,7 @@
 {
   set->alloc = size;
   set->nelem = 0;
-  set->elems = re_xmalloc (Idx, size);
+  set->elems = re_malloc (Idx, size);
   if (BE (set->elems == NULL, 0))
     return REG_ESPACE;
   return REG_NOERROR;
@@ -939,7 +959,7 @@
          dest->alloc = dest->nelem = 0;
          return REG_ESPACE;
        }
-      memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
+      memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
     }
   else
     re_node_set_init_empty (dest);
@@ -964,12 +984,7 @@
   if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
     {
       Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
-      Idx *new_elems;
-      if (sizeof (Idx) < 3
-         && (new_alloc < dest->alloc
-             || ((Idx) (src1->nelem + src2->nelem) < src1->nelem)))
-       return REG_ESPACE;
-      new_elems = re_xrealloc (dest->elems, Idx, new_alloc);
+      Idx *new_elems = re_realloc (dest->elems, Idx, new_alloc);
       if (BE (new_elems == NULL, 0))
         return REG_ESPACE;
       dest->elems = new_elems;
@@ -1038,7 +1053,7 @@
       }
 
   /* Copy remaining SRC elements.  */
-  memcpy (dest->elems, dest->elems + sbase, delta * sizeof dest->elems[0]);
+  memcpy (dest->elems, dest->elems + sbase, delta * sizeof (Idx));
 
   return REG_NOERROR;
 }
@@ -1055,9 +1070,7 @@
   if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
     {
       dest->alloc = src1->nelem + src2->nelem;
-      if (sizeof (Idx) < 2 && dest->alloc < src1->nelem)
-       return REG_ESPACE;
-      dest->elems = re_xmalloc (Idx, dest->alloc);
+      dest->elems = re_malloc (Idx, dest->alloc);
       if (BE (dest->elems == NULL, 0))
        return REG_ESPACE;
     }
@@ -1085,13 +1098,13 @@
   if (i1 < src1->nelem)
     {
       memcpy (dest->elems + id, src1->elems + i1,
-            (src1->nelem - i1) * sizeof dest->elems[0]);
+            (src1->nelem - i1) * sizeof (Idx));
       id += src1->nelem - i1;
     }
   else if (i2 < src2->nelem)
     {
       memcpy (dest->elems + id, src2->elems + i2,
-            (src2->nelem - i2) * sizeof dest->elems[0]);
+            (src2->nelem - i2) * sizeof (Idx));
       id += src2->nelem - i2;
     }
   dest->nelem = id;
@@ -1108,17 +1121,10 @@
   Idx is, id, sbase, delta;
   if (src == NULL || src->nelem == 0)
     return REG_NOERROR;
-  if (sizeof (Idx) < 3
-      && ((Idx) (2 * src->nelem) < src->nelem
-         || (Idx) (2 * src->nelem + dest->nelem) < dest->nelem))
-    return REG_ESPACE;
   if (dest->alloc < 2 * src->nelem + dest->nelem)
     {
-      Idx new_alloc = src->nelem + dest->alloc;
-      Idx *new_buffer;
-      if (sizeof (Idx) < 4 && new_alloc < dest->alloc)
-       return REG_ESPACE;
-      new_buffer = re_x2realloc (dest->elems, Idx, &new_alloc);
+      Idx new_alloc = 2 * (src->nelem + dest->alloc);
+      Idx *new_buffer = re_realloc (dest->elems, Idx, new_alloc);
       if (BE (new_buffer == NULL, 0))
        return REG_ESPACE;
       dest->elems = new_buffer;
@@ -1128,7 +1134,7 @@
   if (BE (dest->nelem == 0, 0))
     {
       dest->nelem = src->nelem;
-      memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
+      memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
       return REG_NOERROR;
     }
 
@@ -1150,8 +1156,7 @@
     {
       /* If DEST is exhausted, the remaining items of SRC must be unique.  */
       sbase -= is + 1;
-      memcpy (dest->elems + sbase, src->elems,
-             (is + 1) * sizeof dest->elems[0]);
+      memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (Idx));
     }
 
   id = dest->nelem - 1;
@@ -1180,7 +1185,7 @@
            {
              /* Copy remaining SRC elements.  */
              memcpy (dest->elems, dest->elems + sbase,
-                     delta * sizeof dest->elems[0]);
+                     delta * sizeof (Idx));
              break;
            }
        }
@@ -1200,7 +1205,7 @@
   Idx idx;
   /* In case the set is empty.  */
   if (set->alloc == 0)
-    return re_node_set_init_1 (set, elem) == REG_NOERROR;
+    return BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1);
 
   if (BE (set->nelem, 0) == 0)
     {
@@ -1213,7 +1218,9 @@
   /* Realloc if we need.  */
   if (set->alloc == set->nelem)
     {
-      Idx *new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
+      Idx *new_elems;
+      set->alloc = set->alloc * 2;
+      new_elems = re_realloc (set->elems, Idx, set->alloc);
       if (BE (new_elems == NULL, 0))
        return false;
       set->elems = new_elems;
@@ -1251,7 +1258,8 @@
   if (set->alloc == set->nelem)
     {
       Idx *new_elems;
-      new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
+      set->alloc = (set->alloc + 1) * 2;
+      new_elems = re_realloc (set->elems, Idx, set->alloc);
       if (BE (new_elems == NULL, 0))
        return false;
       set->elems = new_elems;
@@ -1324,18 +1332,26 @@
   int type = token.type;
   if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
     {
-      Idx new_nodes_alloc = dfa->nodes_alloc;
+      size_t new_nodes_alloc = dfa->nodes_alloc * 2;
       Idx *new_nexts, *new_indices;
       re_node_set *new_edests, *new_eclosures;
+      re_token_t *new_nodes;
+      size_t max_object_size =
+       MAX (sizeof (re_token_t),
+            MAX (sizeof (re_node_set),
+                 sizeof (Idx)));
 
-      re_token_t *new_nodes = re_x2realloc (dfa->nodes, re_token_t,
-                                           &new_nodes_alloc);
+      /* Avoid overflows.  */
+      if (BE (SIZE_MAX / 2 / max_object_size < dfa->nodes_alloc, 0))
+       return REG_MISSING;
+
+      new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
       if (BE (new_nodes == NULL, 0))
        return REG_MISSING;
       dfa->nodes = new_nodes;
       new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
       new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
-      new_edests = re_xrealloc (dfa->edests, re_node_set, new_nodes_alloc);
+      new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
       new_eclosures = re_realloc (dfa->eclosures, re_node_set, 
new_nodes_alloc);
       if (BE (new_nexts == NULL || new_indices == NULL
              || new_edests == NULL || new_eclosures == NULL, 0))
@@ -1378,9 +1394,10 @@
         - We never return non-NULL value in case of any errors, it is for
           optimization.  */
 
-static re_dfastate_t*
+static re_dfastate_t *
 internal_function
-re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa, const re_node_set *nodes)
+re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
+                 const re_node_set *nodes)
 {
   re_hashval_t hash;
   re_dfastate_t *new_state;
@@ -1409,13 +1426,10 @@
 
   /* There are no appropriate state in the dfa, create the new one.  */
   new_state = create_ci_newstate (dfa, nodes, hash);
-  if (BE (new_state != NULL, 1))
-    return new_state;
-  else
-    {
-      *err = REG_ESPACE;
-      return NULL;
-    }
+  if (BE (new_state == NULL, 0))
+    *err = REG_ESPACE;
+
+  return new_state;
 }
 
 /* Search for the state whose node_set is equivalent to NODES and
@@ -1428,9 +1442,9 @@
         - We never return non-NULL value in case of any errors, it is for
           optimization.  */
 
-static re_dfastate_t*
+static re_dfastate_t *
 internal_function
-re_acquire_state_context (reg_errcode_t *err, re_dfa_t *dfa,
+re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
                          const re_node_set *nodes, unsigned int context)
 {
   re_hashval_t hash;
@@ -1459,13 +1473,10 @@
     }
   /* There are no appropriate state in `dfa', create the new one.  */
   new_state = create_cd_newstate (dfa, nodes, context, hash);
-  if (BE (new_state != NULL, 1))
-    return new_state;
-  else
-    {
-      *err = REG_ESPACE;
-      return NULL;
-    }
+  if (BE (new_state == NULL, 0))
+    *err = REG_ESPACE;
+
+  return new_state;
 }
 
 /* Finish initialization of the new state NEWSTATE, and using its hash value
@@ -1473,8 +1484,8 @@
    indicates the error code if failed.  */
 
 static reg_errcode_t
-internal_function
-register_state (const re_dfa_t *dfa, re_dfastate_t *newstate, re_hashval_t 
hash)
+register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
+               re_hashval_t hash)
 {
   struct re_state_table_entry *spot;
   reg_errcode_t err;
@@ -1488,19 +1499,16 @@
     {
       Idx elem = newstate->nodes.elems[i];
       if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
-       {
-         bool ok = re_node_set_insert_last (&newstate->non_eps_nodes, elem);
-         if (BE (! ok, 0))
-           return REG_ESPACE;
-       }
+       if (BE (! re_node_set_insert_last (&newstate->non_eps_nodes, elem), 0))
+         return REG_ESPACE;
     }
 
   spot = dfa->state_table + (hash & dfa->state_hash_mask);
   if (BE (spot->alloc <= spot->num, 0))
     {
-      Idx new_alloc = spot->num;
-      re_dfastate_t **new_array = re_x2realloc (spot->array, re_dfastate_t *,
-                                               &new_alloc);
+      Idx new_alloc = 2 * spot->num + 2;
+      re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
+                                             new_alloc);
       if (BE (new_array == NULL, 0))
        return REG_ESPACE;
       spot->array = new_array;
@@ -1510,6 +1518,22 @@
   return REG_NOERROR;
 }
 
+static void
+free_state (re_dfastate_t *state)
+{
+  re_node_set_free (&state->non_eps_nodes);
+  re_node_set_free (&state->inveclosure);
+  if (state->entrance_nodes != &state->nodes)
+    {
+      re_node_set_free (state->entrance_nodes);
+      re_free (state->entrance_nodes);
+    }
+  re_node_set_free (&state->nodes);
+  re_free (state->word_trtable);
+  re_free (state->trtable);
+  re_free (state);
+}
+
 /* Create the new state which is independ of contexts.
    Return the new state if succeeded, otherwise return NULL.  */
 
@@ -1522,7 +1546,7 @@
   reg_errcode_t err;
   re_dfastate_t *newstate;
 
-  newstate = re_calloc (re_dfastate_t, 1);
+  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
   if (BE (newstate == NULL, 0))
     return NULL;
   err = re_node_set_init_copy (&newstate->nodes, nodes);
@@ -1572,7 +1596,7 @@
   reg_errcode_t err;
   re_dfastate_t *newstate;
 
-  newstate = re_calloc (re_dfastate_t, 1);
+  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
   if (BE (newstate == NULL, 0))
     return NULL;
   err = re_node_set_init_copy (&newstate->nodes, nodes);
@@ -1637,20 +1661,3 @@
     }
   return  newstate;
 }
-
-static void
-internal_function
-free_state (re_dfastate_t *state)
-{
-  re_node_set_free (&state->non_eps_nodes);
-  re_node_set_free (&state->inveclosure);
-  if (state->entrance_nodes != &state->nodes)
-    {
-      re_node_set_free (state->entrance_nodes);
-      re_free (state->entrance_nodes);
-    }
-  re_node_set_free (&state->nodes);
-  re_free (state->word_trtable);
-  re_free (state->trtable);
-  re_free (state);
-}
Index: ccvs/lib/regex_internal.h
diff -u ccvs/lib/regex_internal.h:1.10 ccvs/lib/regex_internal.h:1.11
--- ccvs/lib/regex_internal.h:1.10      Wed Jan 25 12:53:38 2006
+++ ccvs/lib/regex_internal.h   Tue Apr 11 20:21:31 2006
@@ -43,6 +43,9 @@
 #if defined HAVE_WCTYPE_H || defined _LIBC
 # include <wctype.h>
 #endif /* HAVE_WCTYPE_H || _LIBC */
+#if defined HAVE_STDINT_H || defined _LIBC
+# include <stdint.h>
+#endif /* HAVE_STDINT_H || _LIBC */
 #if defined _LIBC
 # include <bits/libc-lock.h>
 #else
@@ -84,6 +87,11 @@
 # define gettext_noop(String) String
 #endif
 
+/* For loser systems without the definition.  */
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
+
 #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && 
HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
 # define RE_ENABLE_I18N
 #endif
@@ -92,10 +100,16 @@
 # define BE(expr, val) __builtin_expect (expr, val)
 #else
 # define BE(expr, val) (expr)
+# ifdef _LIBC
+#  define inline
+# endif
 #endif
 
-/* Number of single byte character.  */
-#define SBC_MAX 256
+/* Number of ASCII characters.  */
+#define ASCII_CHARS 0x80
+
+/* Number of single byte characters.  */
+#define SBC_MAX (UCHAR_MAX + 1)
 
 #define COLL_ELEM_LEN_MAX 8
 
@@ -122,9 +136,6 @@
 # define __attribute(arg)
 #endif
 
-extern const char __re_error_msgid[] attribute_hidden;
-extern const size_t __re_error_msgid_idx[] attribute_hidden;
-
 typedef __re_idx_t Idx;
 
 /* Special return value for failure to match.  */
@@ -152,15 +163,15 @@
 
 /* An integer used to represent a set of bits.  It must be unsigned,
    and must be at least as wide as unsigned int.  */
-typedef unsigned long int bitset_word;
-
-/* Maximum value of a bitset word.  It must be useful in preprocessor
-   contexts, and must be consistent with bitset_word.  */
+typedef unsigned long int bitset_word_t;
+/* All bits set in a bitset_word_t.  */
 #define BITSET_WORD_MAX ULONG_MAX
 
-/* Number of bits in a bitset word.  Avoid greater-than-32-bit
-   integers and unconditional shifts by more than 31 bits, as they're
-   not portable.  */
+/* Number of bits in a bitset_word_t.  For portability to hosts with
+   padding bits, do not use '(sizeof (bitset_word_t) * CHAR_BIT)';
+   instead, deduce it directly from BITSET_WORD_MAX.  Avoid
+   greater-than-32-bit integers and unconditional shifts by more than
+   31 bits, as they're not portable.  */
 #if BITSET_WORD_MAX == 0xffffffff
 # define BITSET_WORD_BITS 32
 #elif BITSET_WORD_MAX >> 31 >> 5 == 1
@@ -182,20 +193,20 @@
 # if BITSET_WORD_BITS <= SBC_MAX
 #  error "Invalid SBC_MAX"
 # endif
- #elif BITSET_WORD_MAX == (0xffffffff + 2) * 0xffffffff
+#elif BITSET_WORD_MAX == (0xffffffff + 2) * 0xffffffff
 /* Work around a bug in 64-bit PGC (before version 6.1-2), where the
    preprocessor mishandles large unsigned values as if they were signed.  */
 # define BITSET_WORD_BITS 64
 #else
-# error "Add case for new bitset_word size"
+# error "Add case for new bitset_word_t size"
 #endif
 
-/* Number of bitset words in a bitset.  */
+/* Number of bitset_word_t values in a bitset_t.  */
 #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
 
-typedef bitset_word bitset[BITSET_WORDS];
-typedef bitset_word *re_bitset_ptr_t;
-typedef const bitset_word *re_const_bitset_ptr_t;
+typedef bitset_word_t bitset_t[BITSET_WORDS];
+typedef bitset_word_t *re_bitset_ptr_t;
+typedef const bitset_word_t *re_const_bitset_ptr_t;
 
 #define PREV_WORD_CONSTRAINT 0x0001
 #define PREV_NOTWORD_CONSTRAINT 0x0002
@@ -401,7 +412,7 @@
      the beginning of the input string.  */
   unsigned int tip_context;
   /* The translation passed as a part of an argument of re_compile_pattern.  */
-  unsigned REG_TRANSLATE_TYPE trans;
+  RE_TRANSLATE_TYPE trans;
   /* Copy of re_dfa_t's word_char.  */
   re_const_bitset_ptr_t word_char;
   /* true if REG_ICASE.  */
@@ -438,10 +449,9 @@
 #endif /* RE_ENABLE_I18N */
 static void build_upper_buffer (re_string_t *pstr) internal_function;
 static void re_string_translate_buffer (re_string_t *pstr) internal_function;
-static unsigned int re_string_context_at (const re_string_t *input,
-                                         Idx idx, int eflags)
+static unsigned int re_string_context_at (const re_string_t *input, Idx idx,
+                                         int eflags)
      internal_function __attribute ((pure));
-
 #define re_string_peek_byte(pstr, offset) \
   ((pstr)->mbs[(pstr)->cur_idx + offset])
 #define re_string_fetch_byte(pstr) \
@@ -474,71 +484,14 @@
 # endif
 #endif
 
+#ifndef MAX
+# define MAX(a,b) ((a) < (b) ? (b) : (a))
+#endif
+
 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
-#define re_xmalloc(t,n) ((t *) re_xnmalloc (n, sizeof (t)))
-#define re_calloc(t,n) ((t *) calloc (n, sizeof (t)))
 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
-#define re_xrealloc(p,t,n) ((t *) re_xnrealloc (p, n, sizeof (t)))
-#define re_x2realloc(p,t,pn) ((t *) re_x2nrealloc (p, pn, sizeof (t)))
 #define re_free(p) free (p)
 
-#ifndef SIZE_MAX
-# define SIZE_MAX ((size_t) -1)
-#endif
-
-/* Return true if an array of N objects, each of size S, cannot exist
-   due to size arithmetic overflow.  S must be nonzero.  */
-static inline bool
-re_alloc_oversized (size_t n, size_t s)
-{
-  return BE (SIZE_MAX / s < n, 0);
-}
-
-/* Return true if an array of (2 * N + 1) objects, each of size S,
-   cannot exist due to size arithmetic overflow.  S must be nonzero.  */
-static inline bool
-re_x2alloc_oversized (size_t n, size_t s)
-{
-  return BE ((SIZE_MAX / s - 1) / 2 < n, 0);
-}
-
-/* Allocate an array of N objects, each with S bytes of memory,
-   dynamically, with error checking.  S must be nonzero.  */
-static inline void *
-re_xnmalloc (size_t n, size_t s)
-{
-  return re_alloc_oversized (n, s) ? NULL : malloc (n * s);
-}
-
-/* Change the size of an allocated block of memory P to an array of N
-   objects each of S bytes, with error checking.  S must be nonzero.  */
-static inline void *
-re_xnrealloc (void *p, size_t n, size_t s)
-{
-  return re_alloc_oversized (n, s) ? NULL : realloc (p, n * s);
-}
-
-/* Reallocate a block of memory P to an array of (2 * (*PN) + 1)
-   objects each of S bytes, with error checking.  S must be nonzero.
-   If the allocation is successful, set *PN to the new allocation
-   count and return the resulting pointer.  Otherwise, return
-   NULL.  */
-static inline void *
-re_x2nrealloc (void *p, size_t *pn, size_t s)
-{
-  if (re_x2alloc_oversized (*pn, s))
-    return NULL;
-  else
-    {
-      /* Add 1 in case *PN is zero.  */
-      size_t n1 = 2 * *pn + 1;
-      p = realloc (p, n1 * s);
-      if (BE (p != NULL, 1))
-       *pn = n1;
-      return p;
-    }
-}
-
 struct bin_tree_t
 {
   struct bin_tree_t *parent;
@@ -668,9 +621,9 @@
   /* The string object corresponding to the input string.  */
   re_string_t input;
 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
-  re_dfa_t *const dfa;
+  const re_dfa_t *const dfa;
 #else
-  re_dfa_t *dfa;
+  const re_dfa_t *dfa;
 #endif
   /* EFLAGS of the argument of regexec.  */
   int eflags;
@@ -717,8 +670,8 @@
 struct re_dfa_t
 {
   re_token_t *nodes;
-  Idx nodes_alloc;
-  Idx nodes_len;
+  size_t nodes_alloc;
+  size_t nodes_len;
   Idx *nexts;
   Idx *org_indices;
   re_node_set *edests;
@@ -740,8 +693,8 @@
   Idx nbackref; /* The number of backreference in this dfa.  */
 
   /* Bitmap expressing which backreference is used.  */
-  bitset_word used_bkref_map;
-  bitset_word completed_bkref_map;
+  bitset_word_t used_bkref_map;
+  bitset_word_t completed_bkref_map;
 
   unsigned int has_plural_match : 1;
   /* If this dfa has "multibyte node", which is a backreference or
@@ -752,7 +705,7 @@
   unsigned int map_notascii : 1;
   unsigned int word_ops_used : 1;
   int mb_cur_max;
-  bitset word_char;
+  bitset_t word_char;
   reg_syntax_t syntax;
   Idx *subexp_map;
 #ifdef DEBUG
@@ -766,8 +719,6 @@
   (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
 #define re_node_set_empty(p) ((p)->nelem = 0)
 #define re_node_set_free(set) re_free ((set)->elems)
-
-static void free_state (re_dfastate_t *state) internal_function;
 
 
 typedef enum
@@ -791,76 +742,76 @@
 } bracket_elem_t;
 
 
-/* Inline functions for bitset operation.  */
+/* Inline functions for bitset_t operation.  */
 
 static inline void
-bitset_set (bitset set, Idx i)
+bitset_set (bitset_t set, Idx i)
 {
-  set[i / BITSET_WORD_BITS] |= (bitset_word) 1 << i % BITSET_WORD_BITS;
+  set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS;
 }
 
 static inline void
-bitset_clear (bitset set, Idx i)
+bitset_clear (bitset_t set, Idx i)
 {
-  set[i / BITSET_WORD_BITS] &= ~ ((bitset_word) 1 << i % BITSET_WORD_BITS);
+  set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS);
 }
 
 static inline bool
-bitset_contain (const bitset set, Idx i)
+bitset_contain (const bitset_t set, Idx i)
 {
   return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
 }
 
 static inline void
-bitset_empty (bitset set)
+bitset_empty (bitset_t set)
 {
-  memset (set, 0, sizeof (bitset));
+  memset (set, '\0', sizeof (bitset_t));
 }
 
 static inline void
-bitset_set_all (bitset set)
+bitset_set_all (bitset_t set)
 {
-  memset (set, -1, sizeof (bitset_word) * (SBC_MAX / BITSET_WORD_BITS));
+  memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS));
   if (SBC_MAX % BITSET_WORD_BITS != 0)
     set[BITSET_WORDS - 1] =
-      ((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
+      ((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
 }
 
 static inline void
-bitset_copy (bitset dest, const bitset src)
+bitset_copy (bitset_t dest, const bitset_t src)
 {
-  memcpy (dest, src, sizeof (bitset));
+  memcpy (dest, src, sizeof (bitset_t));
 }
 
 static inline void
-bitset_not (bitset set)
+bitset_not (bitset_t set)
 {
-  int i;
-  for (i = 0; i < SBC_MAX / BITSET_WORD_BITS; ++i)
-    set[i] = ~set[i];
+  int bitset_i;
+  for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i)
+    set[bitset_i] = ~set[bitset_i];
   if (SBC_MAX % BITSET_WORD_BITS != 0)
     set[BITSET_WORDS - 1] =
-      ((((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
+      ((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
        & ~set[BITSET_WORDS - 1]);
 }
 
 static inline void
-bitset_merge (bitset dest, const bitset src)
+bitset_merge (bitset_t dest, const bitset_t src)
 {
-  int i;
-  for (i = 0; i < BITSET_WORDS; ++i)
-    dest[i] |= src[i];
+  int bitset_i;
+  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
+    dest[bitset_i] |= src[bitset_i];
 }
 
 static inline void
-bitset_mask (bitset dest, const bitset src)
+bitset_mask (bitset_t dest, const bitset_t src)
 {
-  int i;
-  for (i = 0; i < BITSET_WORDS; ++i)
-    dest[i] &= src[i];
+  int bitset_i;
+  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
+    dest[bitset_i] &= src[bitset_i];
 }
 
-#if defined RE_ENABLE_I18N
+#ifdef RE_ENABLE_I18N
 /* Inline functions for re_string.  */
 static inline int
 internal_function __attribute ((pure))
@@ -888,11 +839,11 @@
 internal_function __attribute ((pure))
 re_string_elem_size_at (const re_string_t *pstr, Idx idx)
 {
-#ifdef _LIBC
+# ifdef _LIBC
   const unsigned char *p, *extra;
   const int32_t *table, *indirect;
   int32_t tmp;
-# include <locale/weight.h>
+#  include <locale/weight.h>
   uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
 
   if (nrules != 0)
@@ -907,7 +858,7 @@
       return p - pstr->mbs - idx;
     }
   else
-#endif /* _LIBC */
+# endif /* _LIBC */
     return 1;
 }
 #endif /* RE_ENABLE_I18N */
Index: ccvs/lib/regexec.c
diff -u ccvs/lib/regexec.c:1.7 ccvs/lib/regexec.c:1.8
--- ccvs/lib/regexec.c:1.7      Tue Sep 20 16:47:52 2005
+++ ccvs/lib/regexec.c  Tue Apr 11 20:21:31 2006
@@ -51,68 +51,79 @@
                                regoff_t range, Idx stop,
                                struct re_registers *regs,
                                bool ret_len) internal_function;
-static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
-                             Idx nregs, int regs_allocated) internal_function;
+static unsigned int re_copy_regs (struct re_registers *regs, regmatch_t 
*pmatch,
+                                 Idx nregs, int regs_allocated)
+     internal_function;
 static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
      internal_function;
 static Idx check_matching (re_match_context_t *mctx, bool fl_longest_match,
-                          Idx *p_match_first)
-     internal_function;
+                          Idx *p_match_first) internal_function;
 static Idx check_halt_state_context (const re_match_context_t *mctx,
                                     const re_dfastate_t *state, Idx idx)
      internal_function;
-static void update_regs (re_dfa_t *dfa, regmatch_t *pmatch,
+static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
                         regmatch_t *prev_idx_match, Idx cur_node,
                         Idx cur_idx, Idx nmatch) internal_function;
 static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
                                      Idx str_idx, Idx dest_node, Idx nregs,
                                      regmatch_t *regs,
-                                     re_node_set *eps_via_nodes) 
internal_function;
+                                     re_node_set *eps_via_nodes)
+     internal_function;
 static reg_errcode_t set_regs (const regex_t *preg,
                               const re_match_context_t *mctx,
                               size_t nmatch, regmatch_t *pmatch,
                               bool fl_backtrack) internal_function;
-static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs) 
internal_function;
+static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs)
+     internal_function;
 
 #ifdef RE_ENABLE_I18N
 static int sift_states_iter_mb (const re_match_context_t *mctx,
                                re_sift_context_t *sctx,
-                               Idx node_idx, Idx str_idx, Idx max_str_idx) 
internal_function;
+                               Idx node_idx, Idx str_idx, Idx max_str_idx)
+     internal_function;
 #endif /* RE_ENABLE_I18N */
-static reg_errcode_t sift_states_backward (re_match_context_t *mctx,
-                                          re_sift_context_t *sctx) 
internal_function;
-static reg_errcode_t build_sifted_states (re_match_context_t *mctx,
+static reg_errcode_t sift_states_backward (const re_match_context_t *mctx,
+                                          re_sift_context_t *sctx)
+     internal_function;
+static reg_errcode_t build_sifted_states (const re_match_context_t *mctx,
                                          re_sift_context_t *sctx, Idx str_idx,
-                                         re_node_set *cur_dest) 
internal_function;
-static reg_errcode_t update_cur_sifted_state (re_match_context_t *mctx,
+                                         re_node_set *cur_dest)
+     internal_function;
+static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx,
                                              re_sift_context_t *sctx,
                                              Idx str_idx,
-                                             re_node_set *dest_nodes) 
internal_function;
-static reg_errcode_t add_epsilon_src_nodes (re_dfa_t *dfa,
+                                             re_node_set *dest_nodes)
+     internal_function;
+static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa,
                                            re_node_set *dest_nodes,
-                                           const re_node_set *candidates) 
internal_function;
+                                           const re_node_set *candidates)
+     internal_function;
 static bool check_dst_limits (const re_match_context_t *mctx,
                              const re_node_set *limits,
                              Idx dst_node, Idx dst_idx, Idx src_node,
                              Idx src_idx) internal_function;
 static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx,
                                        int boundaries, Idx subexp_idx,
-                                       Idx from_node, Idx bkref_idx) 
internal_function;
+                                       Idx from_node, Idx bkref_idx)
+     internal_function;
 static int check_dst_limits_calc_pos (const re_match_context_t *mctx,
                                      Idx limit, Idx subexp_idx,
                                      Idx node, Idx str_idx,
                                      Idx bkref_idx) internal_function;
-static reg_errcode_t check_subexp_limits (re_dfa_t *dfa,
+static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa,
                                          re_node_set *dest_nodes,
                                          const re_node_set *candidates,
                                          re_node_set *limits,
                                          struct re_backref_cache_entry 
*bkref_ents,
                                          Idx str_idx) internal_function;
-static reg_errcode_t sift_states_bkref (re_match_context_t *mctx,
+static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx,
                                        re_sift_context_t *sctx,
-                                       Idx str_idx, const re_node_set 
*candidates) internal_function;
-static reg_errcode_t merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst,
-                                       re_dfastate_t **src, Idx num) 
internal_function;
+                                       Idx str_idx, const re_node_set 
*candidates)
+     internal_function;
+static reg_errcode_t merge_state_array (const re_dfa_t *dfa,
+                                       re_dfastate_t **dst,
+                                       re_dfastate_t **src, Idx num)
+     internal_function;
 static re_dfastate_t *find_recover_state (reg_errcode_t *err,
                                         re_match_context_t *mctx) 
internal_function;
 static re_dfastate_t *transit_state (reg_errcode_t *err,
@@ -120,27 +131,33 @@
                                     re_dfastate_t *state) internal_function;
 static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
                                            re_match_context_t *mctx,
-                                           re_dfastate_t *next_state) 
internal_function;
+                                           re_dfastate_t *next_state)
+     internal_function;
 static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
                                                re_node_set *cur_nodes,
                                                Idx str_idx) internal_function;
 #if 0
 static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
                                        re_match_context_t *mctx,
-                                       re_dfastate_t *pstate) 
internal_function;
+                                       re_dfastate_t *pstate)
+     internal_function;
 #endif
 #ifdef RE_ENABLE_I18N
 static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
-                                      re_dfastate_t *pstate) internal_function;
+                                      re_dfastate_t *pstate)
+     internal_function;
 #endif /* RE_ENABLE_I18N */
 static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
-                                         const re_node_set *nodes) 
internal_function;
+                                         const re_node_set *nodes)
+     internal_function;
 static reg_errcode_t get_subexp (re_match_context_t *mctx,
-                                Idx bkref_node, Idx bkref_str_idx) 
internal_function;
+                                Idx bkref_node, Idx bkref_str_idx)
+     internal_function;
 static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
                                     const re_sub_match_top_t *sub_top,
                                     re_sub_match_last_t *sub_last,
-                                    Idx bkref_node, Idx bkref_str) 
internal_function;
+                                    Idx bkref_node, Idx bkref_str)
+     internal_function;
 static Idx find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
                             Idx subexp_idx, int type) internal_function;
 static reg_errcode_t check_arrival (re_match_context_t *mctx,
@@ -150,35 +167,41 @@
 static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
                                                   Idx str_idx,
                                                   re_node_set *cur_nodes,
-                                                  re_node_set *next_nodes) 
internal_function;
-static reg_errcode_t check_arrival_expand_ecl (re_dfa_t *dfa,
+                                                  re_node_set *next_nodes)
+     internal_function;
+static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa,
                                               re_node_set *cur_nodes,
-                                              Idx ex_subexp, int type) 
internal_function;
-static reg_errcode_t check_arrival_expand_ecl_sub (re_dfa_t *dfa,
+                                              Idx ex_subexp, int type)
+     internal_function;
+static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa,
                                                   re_node_set *dst_nodes,
                                                   Idx target, Idx ex_subexp,
                                                   int type) internal_function;
 static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
                                         re_node_set *cur_nodes, Idx cur_str,
-                                        Idx subexp_num, int type) 
internal_function;
-static bool build_trtable (re_dfa_t *dfa,
+                                        Idx subexp_num, int type)
+     internal_function;
+static bool build_trtable (const re_dfa_t *dfa,
                           re_dfastate_t *state) internal_function;
 #ifdef RE_ENABLE_I18N
-static int check_node_accept_bytes (re_dfa_t *dfa, Idx node_idx,
-                                   const re_string_t *input, Idx idx) 
internal_function;
+static int check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
+                                   const re_string_t *input, Idx idx)
+     internal_function;
 # ifdef _LIBC
 static unsigned int find_collation_sequence_value (const unsigned char *mbs,
-                                                  size_t name_len) 
internal_function;
+                                                  size_t name_len)
+     internal_function;
 # endif /* _LIBC */
 #endif /* RE_ENABLE_I18N */
 static Idx group_nodes_into_DFAstates (const re_dfa_t *dfa,
                                       const re_dfastate_t *state,
                                       re_node_set *states_node,
-                                      bitset *states_ch) internal_function;
+                                      bitset_t *states_ch) internal_function;
 static bool check_node_accept (const re_match_context_t *mctx,
                               const re_token_t *node, Idx idx)
      internal_function;
-static reg_errcode_t extend_buffers (re_match_context_t *mctx) 
internal_function;
+static reg_errcode_t extend_buffers (re_match_context_t *mctx)
+     internal_function;
 
 /* Entry point for POSIX code.  */
 
@@ -197,13 +220,17 @@
    We return 0 if we find a match and REG_NOMATCH if not.  */
 
 int
-regexec (const regex_t *__restrict preg, const char *__restrict string,
-        size_t nmatch, regmatch_t pmatch[], int eflags)
+regexec (preg, string, nmatch, pmatch, eflags)
+    const regex_t *__restrict preg;
+    const char *__restrict string;
+    size_t nmatch;
+    regmatch_t pmatch[];
+    int eflags;
 {
   reg_errcode_t err;
   Idx start, length;
 #ifdef _LIBC
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
 #endif
 
   if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
@@ -221,7 +248,7 @@
     }
 
   __libc_lock_lock (dfa->lock);
-  if (preg->re_no_sub)
+  if (preg->no_sub)
     err = re_search_internal (preg, string, length, start, length,
                              length, 0, NULL, eflags);
   else
@@ -271,8 +298,8 @@
    the first STOP characters of the concatenation of the strings should be
    concerned.
 
-   If REGS is not NULL, and BUFP->re_no_sub is not set, the offsets of the 
match
-   and all groups is stroed in REGS.  (For the "_2" variants, the offsets are
+   If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
+   and all groups is stored in REGS.  (For the "_2" variants, the offsets are
    computed relative to the concatenation, not relative to the individual
    strings.)
 
@@ -281,8 +308,11 @@
    match was found and -2 indicates an internal error.  */
 
 regoff_t
-re_match (struct re_pattern_buffer *bufp, const char *string,
-         Idx length, Idx start, struct re_registers *regs)
+re_match (bufp, string, length, start, regs)
+    struct re_pattern_buffer *bufp;
+    const char *string;
+    Idx length, start;
+    struct re_registers *regs;
 {
   return re_search_stub (bufp, string, length, start, 0, length, regs, true);
 }
@@ -291,8 +321,12 @@
 #endif
 
 regoff_t
-re_search (struct re_pattern_buffer *bufp, const char *string,
-          Idx length, Idx start, regoff_t range, struct re_registers *regs)
+re_search (bufp, string, length, start, range, regs)
+    struct re_pattern_buffer *bufp;
+    const char *string;
+    Idx length, start;
+    regoff_t range;
+    struct re_registers *regs;
 {
   return re_search_stub (bufp, string, length, start, range, length, regs,
                         false);
@@ -302,10 +336,11 @@
 #endif
 
 regoff_t
-re_match_2 (struct re_pattern_buffer *bufp,
-           const char *string1, Idx length1,
-           const char *string2, Idx length2,
-           Idx start, struct re_registers *regs, Idx stop)
+re_match_2 (bufp, string1, length1, string2, length2, start, regs, stop)
+    struct re_pattern_buffer *bufp;
+    const char *string1, *string2;
+    Idx length1, length2, start, stop;
+    struct re_registers *regs;
 {
   return re_search_2_stub (bufp, string1, length1, string2, length2,
                           start, 0, regs, stop, true);
@@ -315,10 +350,12 @@
 #endif
 
 regoff_t
-re_search_2 (struct re_pattern_buffer *bufp,
-            const char *string1, Idx length1,
-            const char *string2, Idx length2,
-            Idx start, regoff_t range, struct re_registers *regs, Idx stop)
+re_search_2 (bufp, string1, length1, string2, length2, start, range, regs, 
stop)
+    struct re_pattern_buffer *bufp;
+    const char *string1, *string2;
+    Idx length1, length2, start, stop;
+    regoff_t range;
+    struct re_registers *regs;
 {
   return re_search_2_stub (bufp, string1, length1, string2, length2,
                           start, range, regs, stop, false);
@@ -351,8 +388,12 @@
 
        if (BE (s == NULL, 0))
          return -2;
+#ifdef _LIBC
+       memcpy (__mempcpy (s, string1, length1), string2, length2);
+#else
        memcpy (s, string1, length1);
        memcpy (s + length1, string2, length2);
+#endif
        str = s;
       }
     else
@@ -384,59 +425,37 @@
   regoff_t rval;
   int eflags = 0;
 #ifdef _LIBC
-  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
+  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
 #endif
   Idx last_start = start + range;
 
   /* Check for out-of-range.  */
   if (BE (start < 0 || start > length, 0))
     return -1;
-  if (sizeof start < sizeof range)
-    {
-      regoff_t length_offset = length;
-      regoff_t start_offset = start;
-      if (BE (length_offset - start_offset < range, 0))
-       last_start = length;
-      else if (BE (range < - start_offset, 0))
-       last_start = 0;
-    }
-  else
-    {
-      if (BE ((last_start < start) != (range < 0), 0))
-       {
-         /* Overflow occurred when computing last_start; substitute
-            the extreme value.  */
-         last_start = range < 0 ? 0 : length;
-       }
-      else
-       {
-         if (BE (length < last_start, 0))
-           last_start = length;
-         else if (BE (last_start < 0, 0))
-           last_start = 0;
-       }
-    }
+  if (BE (length < last_start || (0 <= range && last_start < start), 0))
+    last_start = length;
+  else if (BE (last_start < 0 || (range < 0 && start <= last_start), 0))
+    last_start = 0;
 
   __libc_lock_lock (dfa->lock);
 
-  eflags |= (bufp->re_not_bol) ? REG_NOTBOL : 0;
-  eflags |= (bufp->re_not_eol) ? REG_NOTEOL : 0;
+  eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
+  eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
 
   /* Compile fastmap if we haven't yet.  */
-  if (start < last_start && bufp->re_fastmap != NULL
-      && !bufp->re_fastmap_accurate)
+  if (start < last_start && bufp->fastmap != NULL && !bufp->fastmap_accurate)
     re_compile_fastmap (bufp);
 
-  if (BE (bufp->re_no_sub, 0))
+  if (BE (bufp->no_sub, 0))
     regs = NULL;
 
   /* We need at least 1 register.  */
   if (regs == NULL)
     nregs = 1;
-  else if (BE (bufp->re_regs_allocated == REG_FIXED
-              && regs->rm_num_regs <= bufp->re_nsub, 0))
+  else if (BE (bufp->regs_allocated == REGS_FIXED
+              && regs->num_regs <= bufp->re_nsub, 0))
     {
-      nregs = regs->rm_num_regs;
+      nregs = regs->num_regs;
       if (BE (nregs < 1, 0))
        {
          /* Nothing can be copied to regs.  */
@@ -446,7 +465,7 @@
     }
   else
     nregs = bufp->re_nsub + 1;
-  pmatch = re_xmalloc (regmatch_t, nregs);
+  pmatch = re_malloc (regmatch_t, nregs);
   if (BE (pmatch == NULL, 0))
     {
       rval = -2;
@@ -464,9 +483,9 @@
   else if (regs != NULL)
     {
       /* If caller wants register contents data back, copy them.  */
-      bufp->re_regs_allocated = re_copy_regs (regs, pmatch, nregs,
-                                             bufp->re_regs_allocated);
-      if (BE (bufp->re_regs_allocated == REG_UNALLOCATED, 0))
+      bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
+                                          bufp->regs_allocated);
+      if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0))
        rval = -2;
     }
 
@@ -486,58 +505,68 @@
   return rval;
 }
 
-static unsigned
+static unsigned int
 internal_function
 re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs,
              int regs_allocated)
 {
-  int rval = REG_REALLOCATE;
+  int rval = REGS_REALLOCATE;
   Idx i;
   Idx need_regs = nregs + 1;
-  /* We need one extra element beyond `rm_num_regs' for the `-1' marker GNU 
code
+  /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
      uses.  */
 
   /* Have the register data arrays been allocated?  */
-  if (regs_allocated == REG_UNALLOCATED)
+  if (regs_allocated == REGS_UNALLOCATED)
     { /* No.  So allocate them with malloc.  */
-      regs->rm_start = re_xmalloc (regoff_t, need_regs);
-      regs->rm_end = re_malloc (regoff_t, need_regs);
-      if (BE (regs->rm_start == NULL, 0) || BE (regs->rm_end == NULL, 0))
-       return REG_UNALLOCATED;
-      regs->rm_num_regs = need_regs;
+      regs->start = re_malloc (regoff_t, need_regs);
+      if (BE (regs->start == NULL, 0))
+       return REGS_UNALLOCATED;
+      regs->end = re_malloc (regoff_t, need_regs);
+      if (BE (regs->end == NULL, 0))
+       {
+         re_free (regs->start);
+         return REGS_UNALLOCATED;
+       }
+      regs->num_regs = need_regs;
     }
-  else if (regs_allocated == REG_REALLOCATE)
+  else if (regs_allocated == REGS_REALLOCATE)
     { /* Yes.  If we need more elements than were already
         allocated, reallocate them.  If we need fewer, just
         leave it alone.  */
-      if (BE (need_regs > regs->rm_num_regs, 0))
+      if (BE (need_regs > regs->num_regs, 0))
        {
-         regoff_t *new_start =
-           re_xrealloc (regs->rm_start, regoff_t, need_regs);
-         regoff_t *new_end = re_realloc (regs->rm_end, regoff_t, need_regs);
-         if (BE (new_start == NULL, 0) || BE (new_end == NULL, 0))
-           return REG_UNALLOCATED;
-         regs->rm_start = new_start;
-         regs->rm_end = new_end;
-         regs->rm_num_regs = need_regs;
+         regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
+         regoff_t *new_end;
+         if (BE (new_start == NULL, 0))
+           return REGS_UNALLOCATED;
+         new_end = re_realloc (regs->end, regoff_t, need_regs);
+         if (BE (new_end == NULL, 0))
+           {
+             re_free (new_start);
+             return REGS_UNALLOCATED;
+           }
+         regs->start = new_start;
+         regs->end = new_end;
+         regs->num_regs = need_regs;
        }
     }
   else
     {
-      assert (regs_allocated == REG_FIXED);
-      /* This function may not be called with REG_FIXED and nregs too big.  */
-      assert (regs->rm_num_regs >= nregs);
-      rval = REG_FIXED;
+      assert (regs_allocated == REGS_FIXED);
+      /* This function may not be called with REGS_FIXED and nregs too big.  */
+      assert (regs->num_regs >= nregs);
+      rval = REGS_FIXED;
     }
 
   /* Copy the regs.  */
   for (i = 0; i < nregs; ++i)
     {
-      regs->rm_start[i] = pmatch[i].rm_so;
-      regs->rm_end[i] = pmatch[i].rm_eo;
+      regs->start[i] = pmatch[i].rm_so;
+      regs->end[i] = pmatch[i].rm_eo;
     }
-  for ( ; i < regs->rm_num_regs; ++i)
-    regs->rm_start[i] = regs->rm_end[i] = -1;
+  for ( ; i < regs->num_regs; ++i)
+    regs->start[i] = regs->end[i] = -1;
 
   return rval;
 }
@@ -556,21 +585,24 @@
    freeing the old data.  */
 
 void
-re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
-                 __re_size_t num_regs, regoff_t *starts, regoff_t *ends)
+re_set_registers (bufp, regs, num_regs, starts, ends)
+    struct re_pattern_buffer *bufp;
+    struct re_registers *regs;
+    __re_size_t num_regs;
+    regoff_t *starts, *ends;
 {
   if (num_regs)
     {
-      bufp->re_regs_allocated = REG_REALLOCATE;
-      regs->rm_num_regs = num_regs;
-      regs->rm_start = starts;
-      regs->rm_end = ends;
+      bufp->regs_allocated = REGS_REALLOCATE;
+      regs->num_regs = num_regs;
+      regs->start = starts;
+      regs->end = ends;
     }
   else
     {
-      bufp->re_regs_allocated = REG_UNALLOCATED;
-      regs->rm_num_regs = 0;
-      regs->rm_start = regs->rm_end = NULL;
+      bufp->regs_allocated = REGS_UNALLOCATED;
+      regs->num_regs = 0;
+      regs->start = regs->end = NULL;
     }
 }
 #ifdef _LIBC
@@ -585,7 +617,8 @@
 # ifdef _LIBC
 weak_function
 # endif
-re_exec (const char *s)
+re_exec (s)
+     const char *s;
 {
   return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
 }
@@ -611,12 +644,13 @@
                    int eflags)
 {
   reg_errcode_t err;
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
   Idx left_lim, right_lim;
   int incr;
   bool fl_longest_match;
   int match_kind;
-  Idx match_first, match_last = REG_MISSING;
+  Idx match_first;
+  Idx match_last = REG_MISSING;
   Idx extra_nmatch;
   bool sb;
   int ch;
@@ -625,11 +659,10 @@
 #else
   re_match_context_t mctx;
 #endif
-  char *fastmap = ((preg->re_fastmap != NULL && preg->re_fastmap_accurate
-                   && start != last_start && !preg->re_can_be_null)
-                  ? preg->re_fastmap : NULL);
-  unsigned REG_TRANSLATE_TYPE t =
-    (unsigned REG_TRANSLATE_TYPE) preg->re_translate;
+  char *fastmap = ((preg->fastmap != NULL && preg->fastmap_accurate
+                   && start != last_start && !preg->can_be_null)
+                  ? preg->fastmap : NULL);
+  RE_TRANSLATE_TYPE t = preg->translate;
 
 #if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 
199901L))
   memset (&mctx, '\0', sizeof (re_match_context_t));
@@ -640,7 +673,7 @@
   nmatch -= extra_nmatch;
 
   /* Check if the DFA haven't been compiled.  */
-  if (BE (preg->re_used == 0 || dfa->init_state == NULL
+  if (BE (preg->used == 0 || dfa->init_state == NULL
          || dfa->init_state_word == NULL || dfa->init_state_nl == NULL
          || dfa->init_state_begbuf == NULL, 0))
     return REG_NOMATCH;
@@ -651,12 +684,12 @@
 #endif
 
   /* If initial states with non-begbuf contexts have no elements,
-     the regex must be anchored.  If preg->re_newline_anchor is set,
+     the regex must be anchored.  If preg->newline_anchor is set,
      we'll never use init_state_nl, so do not check it.  */
   if (dfa->init_state->nodes.nelem == 0
       && dfa->init_state_word->nodes.nelem == 0
       && (dfa->init_state_nl->nodes.nelem == 0
-         || !preg->re_newline_anchor))
+         || !preg->newline_anchor))
     {
       if (start != 0 && last_start != 0)
         return REG_NOMATCH;
@@ -667,13 +700,12 @@
   fl_longest_match = (nmatch != 0 || dfa->nbackref);
 
   err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
-                           preg->re_translate,
-                           preg->re_syntax & REG_IGNORE_CASE, dfa);
+                           preg->translate, preg->syntax & RE_ICASE, dfa);
   if (BE (err != REG_NOERROR, 0))
     goto free_return;
   mctx.input.stop = stop;
   mctx.input.raw_stop = stop;
-  mctx.input.newline_anchor = preg->re_newline_anchor;
+  mctx.input.newline_anchor = preg->newline_anchor;
 
   err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
   if (BE (err != REG_NOERROR, 0))
@@ -685,7 +717,14 @@
      multi character collating element.  */
   if (nmatch > 1 || dfa->has_mb_node)
     {
-      mctx.state_log = re_xmalloc (re_dfastate_t *, mctx.input.bufs_len + 1);
+      /* Avoid overflow.  */
+      if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= mctx.input.bufs_len, 0))
+       {
+         err = REG_ESPACE;
+         goto free_return;
+       }
+
+      mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
       if (BE (mctx.state_log == NULL, 0))
        {
          err = REG_ESPACE;
@@ -706,7 +745,7 @@
   sb = dfa->mb_cur_max == 1;
   match_kind =
     (fastmap
-     ? ((sb || !(preg->re_syntax & REG_IGNORE_CASE || t) ? 4 : 0)
+     ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
        | (start <= last_start ? 2 : 0)
        | (t != NULL ? 1 : 0))
      : 8);
@@ -828,13 +867,13 @@
          else
            {
              mctx.match_last = match_last;
-             if ((!preg->re_no_sub && nmatch > 1) || dfa->nbackref)
+             if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
                {
                  re_dfastate_t *pstate = mctx.state_log[match_last];
                  mctx.last_node = check_halt_state_context (&mctx, pstate,
                                                             match_last);
                }
-             if ((!preg->re_no_sub && nmatch > 1 && dfa->has_plural_match)
+             if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
                  || dfa->nbackref)
                {
                  err = prune_impossible_nodes (&mctx);
@@ -873,7 +912,7 @@
         the maximum possible regoff_t value.  We need a new error
         code REG_OVERFLOW.  */
 
-      if (!preg->re_no_sub && nmatch > 1)
+      if (!preg->no_sub && nmatch > 1)
        {
          err = set_regs (preg, &mctx, nmatch, pmatch,
                          dfa->has_plural_match && dfa->nbackref > 0);
@@ -934,7 +973,7 @@
 internal_function
 prune_impossible_nodes (re_match_context_t *mctx)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   Idx halt_node, match_last;
   reg_errcode_t ret;
   re_dfastate_t **sifted_states;
@@ -945,7 +984,12 @@
 #endif
   match_last = mctx->match_last;
   halt_node = mctx->last_node;
-  sifted_states = re_xmalloc (re_dfastate_t *, match_last + 1);
+
+  /* Avoid overflow.  */
+  if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= match_last, 0))
+    return REG_ESPACE;
+
+  sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
   if (BE (sifted_states == NULL, 0))
     {
       ret = REG_ESPACE;
@@ -953,7 +997,7 @@
     }
   if (dfa->nbackref)
     {
-      lim_states = re_xmalloc (re_dfastate_t *, match_last + 1);
+      lim_states = re_malloc (re_dfastate_t *, match_last + 1);
       if (BE (lim_states == NULL, 0))
        {
          ret = REG_ESPACE;
@@ -1021,7 +1065,7 @@
 acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
                            Idx idx)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   if (dfa->init_state->has_constraint)
     {
       unsigned int context;
@@ -1063,7 +1107,7 @@
 check_matching (re_match_context_t *mctx, bool fl_longest_match,
                Idx *p_match_first)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
   Idx match = 0;
   Idx match_last = REG_MISSING;
@@ -1233,11 +1277,11 @@
 
 static Idx
 internal_function
-proceed_next_node (const re_match_context_t *mctx,
-                  Idx nregs, regmatch_t *regs, Idx *pidx, Idx node,
-                  re_node_set *eps_via_nodes, struct re_fail_stack_t *fs)
+proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
+                  Idx *pidx, Idx node, re_node_set *eps_via_nodes,
+                  struct re_fail_stack_t *fs)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   Idx i;
   bool ok;
   if (IS_EPSILON_NODE (dfa->nodes[node].type))
@@ -1342,15 +1386,17 @@
   Idx num = fs->num++;
   if (fs->num == fs->alloc)
     {
-      struct re_fail_stack_ent_t *new_array =
-       re_x2realloc (fs->stack, struct re_fail_stack_ent_t, &fs->alloc);
+      struct re_fail_stack_ent_t *new_array;
+      new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t)
+                                      * fs->alloc * 2));
       if (new_array == NULL)
        return REG_ESPACE;
+      fs->alloc *= 2;
       fs->stack = new_array;
     }
   fs->stack[num].idx = str_idx;
   fs->stack[num].node = dest_node;
-  fs->stack[num].regs = re_xmalloc (regmatch_t, nregs);
+  fs->stack[num].regs = re_malloc (regmatch_t, nregs);
   if (fs->stack[num].regs == NULL)
     return REG_ESPACE;
   memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
@@ -1360,8 +1406,8 @@
 
 static Idx
 internal_function
-pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx,
-               Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
+pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs,
+               regmatch_t *regs, re_node_set *eps_via_nodes)
 {
   Idx num = --fs->num;
   assert (REG_VALID_INDEX (num));
@@ -1380,10 +1426,10 @@
 
 static reg_errcode_t
 internal_function
-set_regs (const regex_t *preg, const re_match_context_t *mctx,
-         size_t nmatch, regmatch_t *pmatch, bool fl_backtrack)
+set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
+         regmatch_t *pmatch, bool fl_backtrack)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
   Idx idx, cur_node;
   re_node_set eps_via_nodes;
   struct re_fail_stack_t *fs;
@@ -1398,7 +1444,7 @@
   if (fl_backtrack)
     {
       fs = &fs_body;
-      fs->stack = re_xmalloc (struct re_fail_stack_ent_t, fs->alloc);
+      fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc);
       if (fs->stack == NULL)
        return REG_ESPACE;
     }
@@ -1408,11 +1454,6 @@
   cur_node = dfa->init_node;
   re_node_set_init_empty (&eps_via_nodes);
 
-  if (re_alloc_oversized (nmatch, sizeof (regmatch_t)))
-    {
-      free_fail_stack_return (fs);
-      return REG_ESPACE;
-    }
   if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
     prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
   else
@@ -1509,8 +1550,8 @@
 
 static void
 internal_function
-update_regs (re_dfa_t *dfa, regmatch_t *pmatch, regmatch_t *prev_idx_match,
-            Idx cur_node, Idx cur_idx, Idx nmatch)
+update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
+            regmatch_t *prev_idx_match, Idx cur_node, Idx cur_idx, Idx nmatch)
 {
   int type = dfa->nodes[cur_node].type;
   if (type == OP_OPEN_SUBEXP)
@@ -1581,7 +1622,7 @@
 
 static reg_errcode_t
 internal_function
-sift_states_backward (re_match_context_t *mctx, re_sift_context_t *sctx)
+sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx)
 {
   reg_errcode_t err;
   int null_cnt = 0;
@@ -1639,11 +1680,11 @@
 
 static reg_errcode_t
 internal_function
-build_sifted_states (re_match_context_t *mctx, re_sift_context_t *sctx,
+build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx,
                     Idx str_idx, re_node_set *cur_dest)
 {
-  re_dfa_t *const dfa = mctx->dfa;
-  re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
+  const re_dfa_t *const dfa = mctx->dfa;
+  const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
   Idx i;
 
   /* Then build the next sifted state.
@@ -1726,8 +1767,8 @@
 
 static reg_errcode_t
 internal_function
-merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst, re_dfastate_t **src,
-                  Idx num)
+merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst,
+                  re_dfastate_t **src, Idx num)
 {
   Idx st_idx;
   reg_errcode_t err;
@@ -1753,11 +1794,12 @@
 
 static reg_errcode_t
 internal_function
-update_cur_sifted_state (re_match_context_t *mctx, re_sift_context_t *sctx,
-                        Idx str_idx, re_node_set *dest_nodes)
+update_cur_sifted_state (const re_match_context_t *mctx,
+                        re_sift_context_t *sctx, Idx str_idx,
+                        re_node_set *dest_nodes)
 {
-  re_dfa_t *const dfa = mctx->dfa;
-  reg_errcode_t err;
+  const re_dfa_t *const dfa = mctx->dfa;
+  reg_errcode_t err = REG_NOERROR;
   const re_node_set *candidates;
   candidates = ((mctx->state_log[str_idx] == NULL) ? NULL
                : &mctx->state_log[str_idx]->nodes);
@@ -1800,7 +1842,7 @@
 
 static reg_errcode_t
 internal_function
-add_epsilon_src_nodes (re_dfa_t *dfa, re_node_set *dest_nodes,
+add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes,
                       const re_node_set *candidates)
 {
   reg_errcode_t err = REG_NOERROR;
@@ -1825,7 +1867,7 @@
 
 static reg_errcode_t
 internal_function
-sub_epsilon_src_nodes (re_dfa_t *dfa, Idx node, re_node_set *dest_nodes,
+sub_epsilon_src_nodes (const re_dfa_t *dfa, Idx node, re_node_set *dest_nodes,
                       const re_node_set *candidates)
 {
     Idx ecl_idx;
@@ -1877,7 +1919,7 @@
 check_dst_limits (const re_match_context_t *mctx, const re_node_set *limits,
                  Idx dst_node, Idx dst_idx, Idx src_node, Idx src_idx)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   Idx lim_idx, src_pos, dst_pos;
 
   Idx dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
@@ -1913,8 +1955,8 @@
 check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries,
                             Idx subexp_idx, Idx from_node, Idx bkref_idx)
 {
-  re_dfa_t *const dfa = mctx->dfa;
-  re_node_set *eclosures = dfa->eclosures + from_node;
+  const re_dfa_t *const dfa = mctx->dfa;
+  const re_node_set *eclosures = dfa->eclosures + from_node;
   Idx node_idx;
 
   /* Else, we are on the boundary: examine the nodes on the epsilon
@@ -1938,7 +1980,7 @@
 
                  if (subexp_idx < BITSET_WORD_BITS
                      && !(ent->eps_reachable_subexps_map
-                          & ((bitset_word) 1 << subexp_idx)))
+                          & ((bitset_word_t) 1 << subexp_idx)))
                    continue;
 
                  /* Recurse trying to reach the OP_OPEN_SUBEXP and
@@ -1965,8 +2007,8 @@
                    return 0;
 
                  if (subexp_idx < BITSET_WORD_BITS)
-                   ent->eps_reachable_subexps_map &=
-                     ~ ((bitset_word) 1 << subexp_idx);
+                   ent->eps_reachable_subexps_map
+                     &= ~((bitset_word_t) 1 << subexp_idx);
                }
              while (ent++->more);
            }
@@ -1992,9 +2034,9 @@
 
 static int
 internal_function
-check_dst_limits_calc_pos (const re_match_context_t *mctx,
-                          Idx limit, Idx subexp_idx,
-                          Idx from_node, Idx str_idx, Idx bkref_idx)
+check_dst_limits_calc_pos (const re_match_context_t *mctx, Idx limit,
+                          Idx subexp_idx, Idx from_node, Idx str_idx,
+                          Idx bkref_idx)
 {
   struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
   int boundaries;
@@ -2022,7 +2064,7 @@
 
 static reg_errcode_t
 internal_function
-check_subexp_limits (re_dfa_t *dfa, re_node_set *dest_nodes,
+check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes,
                     const re_node_set *candidates, re_node_set *limits,
                     struct re_backref_cache_entry *bkref_ents, Idx str_idx)
 {
@@ -2110,10 +2152,10 @@
 
 static reg_errcode_t
 internal_function
-sift_states_bkref (re_match_context_t *mctx, re_sift_context_t *sctx,
+sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx,
                   Idx str_idx, const re_node_set *candidates)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
   Idx node_idx, node;
   re_sift_context_t local_sctx;
@@ -2141,8 +2183,10 @@
       enabled_idx = first_idx;
       do
        {
+         Idx subexp_len;
+         Idx to_idx;
+         Idx dst_node;
          bool ok;
-         Idx subexp_len, to_idx, dst_node;
          re_dfastate_t *cur_state;
 
          if (entry->node != node)
@@ -2211,7 +2255,7 @@
 sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
                     Idx node_idx, Idx str_idx, Idx max_str_idx)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   int naccepted;
   /* Check the node can accept `multi byte'.  */
   naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx);
@@ -2299,7 +2343,7 @@
 merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
                      re_dfastate_t *next_state)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   Idx cur_idx = re_string_cur_idx (&mctx->input);
 
   if (cur_idx > mctx->state_log_top)
@@ -2377,7 +2421,7 @@
 internal_function
 find_recover_state (reg_errcode_t *err, re_match_context_t *mctx)
 {
-  re_dfastate_t *cur_state = NULL;
+  re_dfastate_t *cur_state;
   do
     {
       Idx max = mctx->state_log_top;
@@ -2409,7 +2453,7 @@
 check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
                           Idx str_idx)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   Idx node_idx;
   reg_errcode_t err;
 
@@ -2424,7 +2468,7 @@
       if (dfa->nodes[node].type == OP_OPEN_SUBEXP
          && dfa->nodes[node].opr.idx < BITSET_WORD_BITS
          && (dfa->used_bkref_map
-             & ((bitset_word) 1 << dfa->nodes[node].opr.idx)))
+             & ((bitset_word_t) 1 << dfa->nodes[node].opr.idx)))
        {
          err = match_ctx_add_subtop (mctx, node, str_idx);
          if (BE (err != REG_NOERROR, 0))
@@ -2442,7 +2486,7 @@
 transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
                  re_dfastate_t *state)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   re_node_set next_nodes;
   re_dfastate_t *next_state;
   Idx node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
@@ -2481,7 +2525,7 @@
 internal_function
 transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
   Idx i;
 
@@ -2535,7 +2579,8 @@
          if (BE (err != REG_NOERROR, 0))
            return err;
        }
-      context = re_string_context_at (&mctx->input, dest_idx - 1, 
mctx->eflags);
+      context = re_string_context_at (&mctx->input, dest_idx - 1,
+                                     mctx->eflags);
       mctx->state_log[dest_idx]
        = re_acquire_state_context (&err, dfa, &dest_nodes, context);
       if (dest_state != NULL)
@@ -2551,7 +2596,7 @@
 internal_function
 transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
   Idx i;
   Idx cur_str_idx = re_string_cur_idx (&mctx->input);
@@ -2665,14 +2710,15 @@
 internal_function
 get_subexp (re_match_context_t *mctx, Idx bkref_node, Idx bkref_str_idx)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   Idx subexp_num, sub_top_idx;
   const char *buf = (const char *) re_string_get_buffer (&mctx->input);
   /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX.  */
   Idx cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
   if (cache_idx != REG_MISSING)
     {
-      const struct re_backref_cache_entry *entry = mctx->bkref_ents + 
cache_idx;
+      const struct re_backref_cache_entry *entry
+       = mctx->bkref_ents + cache_idx;
       do
         if (entry->node == bkref_node)
          return REG_NOERROR; /* We already checked it.  */
@@ -2719,7 +2765,8 @@
                  buf = (const char *) re_string_get_buffer (&mctx->input);
                }
              if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0)
-               break; /* We don't need to search this sub expression any more. 
 */
+               /* We don't need to search this sub expression any more.  */
+               break;
            }
          bkref_str_off += sl_str_diff;
          sl_str += sl_str_diff;
@@ -2771,20 +2818,22 @@
            continue;
          /* Does this state have a ')' of the sub expression?  */
          nodes = &mctx->state_log[sl_str]->nodes;
-         cls_node = find_subexp_node (dfa, nodes, subexp_num, OP_CLOSE_SUBEXP);
+         cls_node = find_subexp_node (dfa, nodes, subexp_num,
+                                      OP_CLOSE_SUBEXP);
          if (cls_node == REG_MISSING)
            continue; /* No.  */
          if (sub_top->path == NULL)
            {
-             sub_top->path = re_calloc (state_array_t,
-                                        sl_str - sub_top->str_idx + 1);
+             sub_top->path = calloc (sizeof (state_array_t),
+                                     sl_str - sub_top->str_idx + 1);
              if (sub_top->path == NULL)
                return REG_ESPACE;
            }
          /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node
             in the current context?  */
          err = check_arrival (mctx, sub_top->path, sub_top->node,
-                              sub_top->str_idx, cls_node, sl_str, 
OP_CLOSE_SUBEXP);
+                              sub_top->str_idx, cls_node, sl_str,
+                              OP_CLOSE_SUBEXP);
          if (err == REG_NOMATCH)
              continue;
          if (BE (err != REG_NOERROR, 0))
@@ -2816,7 +2865,8 @@
   Idx to_idx;
   /* Can the subexpression arrive the back reference?  */
   err = check_arrival (mctx, &sub_last->path, sub_last->node,
-                      sub_last->str_idx, bkref_node, bkref_str, 
OP_OPEN_SUBEXP);
+                      sub_last->str_idx, bkref_node, bkref_str,
+                      OP_OPEN_SUBEXP);
   if (err != REG_NOERROR)
     return err;
   err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx,
@@ -2859,12 +2909,11 @@
 
 static reg_errcode_t
 internal_function
-check_arrival (re_match_context_t *mctx, state_array_t *path,
-              Idx top_node, Idx top_str, Idx last_node, Idx last_str,
-              int type)
+check_arrival (re_match_context_t *mctx, state_array_t *path, Idx top_node,
+              Idx top_str, Idx last_node, Idx last_str, int type)
 {
-  re_dfa_t *const dfa = mctx->dfa;
-  reg_errcode_t err;
+  const re_dfa_t *const dfa = mctx->dfa;
+  reg_errcode_t err = REG_NOERROR;
   Idx subexp_num, backup_cur_idx, str_idx, null_cnt;
   re_dfastate_t *cur_state = NULL;
   re_node_set *cur_nodes, next_nodes;
@@ -2878,18 +2927,19 @@
       re_dfastate_t **new_array;
       Idx old_alloc = path->alloc;
       Idx new_alloc = old_alloc + last_str + mctx->max_mb_elem_len + 1;
-      if (BE (new_alloc < old_alloc, 0))
+      if (BE (new_alloc < old_alloc, 0)
+         || BE (SIZE_MAX / sizeof (re_dfastate_t *) < new_alloc, 0))
        return REG_ESPACE;
-      new_array = re_xrealloc (path->array, re_dfastate_t *, new_alloc);
+      new_array = re_realloc (path->array, re_dfastate_t *, new_alloc);
       if (BE (new_array == NULL, 0))
        return REG_ESPACE;
       path->array = new_array;
       path->alloc = new_alloc;
       memset (new_array + old_alloc, '\0',
-             sizeof (re_dfastate_t *) * (new_alloc - old_alloc));
+             sizeof (re_dfastate_t *) * (path->alloc - old_alloc));
     }
 
-  str_idx = path->next_idx == 0 ? top_str : path->next_idx;
+  str_idx = path->next_idx ? path->next_idx : top_str;
 
   /* Temporary modify MCTX.  */
   backup_state_log = mctx->state_log;
@@ -2917,7 +2967,7 @@
       if (cur_state && cur_state->has_backref)
        {
          err = re_node_set_init_copy (&next_nodes, &cur_state->nodes);
-         if (BE ( err != REG_NOERROR, 0))
+         if (BE (err != REG_NOERROR, 0))
            return err;
        }
       else
@@ -2929,7 +2979,7 @@
        {
          err = expand_bkref_cache (mctx, &next_nodes, str_idx,
                                    subexp_num, type);
-         if (BE ( err != REG_NOERROR, 0))
+         if (BE (err != REG_NOERROR, 0))
            {
              re_node_set_free (&next_nodes);
              return err;
@@ -2960,7 +3010,8 @@
       if (cur_state)
        {
          err = check_arrival_add_next_nodes (mctx, str_idx,
-                                             &cur_state->non_eps_nodes, 
&next_nodes);
+                                             &cur_state->non_eps_nodes,
+                                             &next_nodes);
          if (BE (err != REG_NOERROR, 0))
            {
              re_node_set_free (&next_nodes);
@@ -2978,7 +3029,7 @@
            }
          err = expand_bkref_cache (mctx, &next_nodes, str_idx,
                                    subexp_num, type);
-         if (BE ( err != REG_NOERROR, 0))
+         if (BE (err != REG_NOERROR, 0))
            {
              re_node_set_free (&next_nodes);
              return err;
@@ -3021,13 +3072,12 @@
 static reg_errcode_t
 internal_function
 check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx,
-                             re_node_set *cur_nodes,
-                             re_node_set *next_nodes)
+                             re_node_set *cur_nodes, re_node_set *next_nodes)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   bool ok;
   Idx cur_idx;
-  reg_errcode_t err;
+  reg_errcode_t err = REG_NOERROR;
   re_node_set union_set;
   re_node_set_init_empty (&union_set);
   for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx)
@@ -3100,7 +3150,7 @@
 
 static reg_errcode_t
 internal_function
-check_arrival_expand_ecl (re_dfa_t *dfa, re_node_set *cur_nodes,
+check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes,
                          Idx ex_subexp, int type)
 {
   reg_errcode_t err;
@@ -3118,7 +3168,7 @@
   for (idx = 0; idx < cur_nodes->nelem; ++idx)
     {
       Idx cur_node = cur_nodes->elems[idx];
-      re_node_set *eclosure = dfa->eclosures + cur_node;
+      const re_node_set *eclosure = dfa->eclosures + cur_node;
       outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
       if (outside_node == REG_MISSING)
        {
@@ -3153,7 +3203,7 @@
 
 static reg_errcode_t
 internal_function
-check_arrival_expand_ecl_sub (re_dfa_t *dfa, re_node_set *dst_nodes,
+check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes,
                              Idx target, Idx ex_subexp, int type)
 {
   Idx cur_node;
@@ -3179,12 +3229,12 @@
        break;
       if (dfa->edests[cur_node].nelem == 2)
        {
-         reg_errcode_t ret =
-           check_arrival_expand_ecl_sub (dfa, dst_nodes,
-                                         dfa->edests[cur_node].elems[1],
-                                         ex_subexp, type);
-         if (BE (ret != REG_NOERROR, 0))
-           return ret;
+         reg_errcode_t err;
+         err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
+                                             dfa->edests[cur_node].elems[1],
+                                             ex_subexp, type);
+         if (BE (err != REG_NOERROR, 0))
+           return err;
        }
       cur_node = dfa->edests[cur_node].elems[0];
     }
@@ -3201,7 +3251,7 @@
 expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
                    Idx cur_str, Idx subexp_num, int type)
 {
-  re_dfa_t *const dfa = mctx->dfa;
+  const re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
   Idx cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
   struct re_backref_cache_entry *ent;
@@ -3287,25 +3337,26 @@
 
 static bool
 internal_function
-build_trtable (re_dfa_t *dfa, re_dfastate_t *state)
+build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
 {
   reg_errcode_t err;
   Idx i, j;
   int ch;
   bool need_word_trtable = false;
-  bitset_word elem, mask;
-  bool dests_node_malloced = false, dest_states_malloced = false;
+  bitset_word_t elem, mask;
+  bool dests_node_malloced = false;
+  bool dest_states_malloced = false;
   Idx ndests; /* Number of the destination states from `state'.  */
   re_dfastate_t **trtable;
   re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
   re_node_set follows, *dests_node;
-  bitset *dests_ch;
-  bitset acceptable;
+  bitset_t *dests_ch;
+  bitset_t acceptable;
 
   struct dests_alloc
   {
     re_node_set dests_node[SBC_MAX];
-    bitset dests_ch[SBC_MAX];
+    bitset_t dests_ch[SBC_MAX];
   } *dests_alloc;
 
   /* We build DFA states which corresponds to the destination nodes
@@ -3313,7 +3364,7 @@
      destination state contains, and `dests_ch[i]' represents the
      characters which i-th destination state accepts.  */
   if (__libc_use_alloca (sizeof (struct dests_alloc)))
-    dests_alloc = (struct dests_alloc *) alloca (sizeof dests_alloc[0]);
+    dests_alloc = (struct dests_alloc *) alloca (sizeof (struct dests_alloc));
   else
     {
       dests_alloc = re_malloc (struct dests_alloc, 1);
@@ -3336,7 +3387,8 @@
        free (dests_alloc);
       if (ndests == 0)
        {
-         state->trtable = re_calloc (re_dfastate_t *, SBC_MAX);
+         state->trtable = (re_dfastate_t **)
+           calloc (sizeof (re_dfastate_t *), SBC_MAX);
          return true;
        }
       return false;
@@ -3347,12 +3399,13 @@
     goto out_free;
 
   /* Avoid arithmetic overflow in size calculation.  */
-  if (BE (((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX)
-          / (3 * sizeof (re_dfastate_t *)))
-         < ndests, 0))
+  if (BE ((((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX)
+           / (3 * sizeof (re_dfastate_t *)))
+          < ndests),
+         0))
     goto out_free;
 
-  if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX
+  if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX
                         + ndests * 3 * sizeof (re_dfastate_t *)))
     dest_states = (re_dfastate_t **)
       alloca (ndests * 3 * sizeof (re_dfastate_t *));
@@ -3428,7 +3481,8 @@
         character, or we are in a single-byte character set so we can
         discern by looking at the character code: allocate a
         256-entry transition table.  */
-      trtable = state->trtable = re_calloc (re_dfastate_t *, SBC_MAX);
+      trtable = state->trtable =
+       (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
       if (BE (trtable == NULL, 0))
        goto out_free;
 
@@ -3458,7 +3512,8 @@
         by looking at the character code: build two 256-entry
         transition tables, one starting at trtable[0] and one
         starting at trtable[SBC_MAX].  */
-      trtable = state->word_trtable = re_calloc (re_dfastate_t *, 2 * SBC_MAX);
+      trtable = state->word_trtable =
+       (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
       if (BE (trtable == NULL, 0))
        goto out_free;
 
@@ -3518,13 +3573,13 @@
 static Idx
 internal_function
 group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
-                           re_node_set *dests_node, bitset *dests_ch)
+                           re_node_set *dests_node, bitset_t *dests_ch)
 {
   reg_errcode_t err;
   bool ok;
   Idx i, j, k;
   Idx ndests; /* Number of the destinations from `state'.  */
-  bitset accepts; /* Characters a node can accept.  */
+  bitset_t accepts; /* Characters a node can accept.  */
   const re_node_set *cur_nodes = &state->nodes;
   bitset_empty (accepts);
   ndests = 0;
@@ -3551,21 +3606,21 @@
          else
 #endif
            bitset_set_all (accepts);
-         if (!(dfa->syntax & REG_DOT_NEWLINE))
+         if (!(dfa->syntax & RE_DOT_NEWLINE))
            bitset_clear (accepts, '\n');
-         if (dfa->syntax & REG_DOT_NOT_NULL)
+         if (dfa->syntax & RE_DOT_NOT_NULL)
            bitset_clear (accepts, '\0');
        }
 #ifdef RE_ENABLE_I18N
       else if (type == OP_UTF8_PERIOD)
         {
-         if (SBC_MAX / 2 % BITSET_WORD_BITS == 0)
-           memset (accepts, -1, sizeof accepts / 2);
+         if (ASCII_CHARS % BITSET_WORD_BITS == 0)
+           memset (accepts, -1, ASCII_CHARS);
          else
            bitset_merge (accepts, utf8_sb_map);
-         if (!(dfa->syntax & REG_DOT_NEWLINE))
+         if (!(dfa->syntax & RE_DOT_NEWLINE))
            bitset_clear (accepts, '\n');
-         if (dfa->syntax & REG_DOT_NOT_NULL)
+         if (dfa->syntax & RE_DOT_NOT_NULL)
            bitset_clear (accepts, '\0');
         }
 #endif
@@ -3593,7 +3648,7 @@
 
          if (constraint & NEXT_WORD_CONSTRAINT)
            {
-             bitset_word any_set = 0;
+             bitset_word_t any_set = 0;
              if (type == CHARACTER && !node->word_char)
                {
                  bitset_empty (accepts);
@@ -3612,7 +3667,7 @@
            }
          if (constraint & NEXT_NOTWORD_CONSTRAINT)
            {
-             bitset_word any_set = 0;
+             bitset_word_t any_set = 0;
              if (type == CHARACTER && node->word_char)
                {
                  bitset_empty (accepts);
@@ -3635,10 +3690,10 @@
         state.  Above, we make sure that accepts is not empty.  */
       for (j = 0; j < ndests; ++j)
        {
-         bitset intersec; /* Intersection sets, see below.  */
-         bitset remains;
+         bitset_t intersec; /* Intersection sets, see below.  */
+         bitset_t remains;
          /* Flags, see below.  */
-         bitset_word has_intersec, not_subset, not_consumed;
+         bitset_word_t has_intersec, not_subset, not_consumed;
 
          /* Optimization, skip if this state doesn't accept the character.  */
          if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c))
@@ -3710,7 +3765,7 @@
 
 static int
 internal_function
-check_node_accept_bytes (re_dfa_t *dfa, Idx node_idx,
+check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
                         const re_string_t *input, Idx str_idx)
 {
   const re_token_t *node = dfa->nodes + node_idx;
@@ -3776,9 +3831,9 @@
       /* FIXME: I don't think this if is needed, as both '\n'
         and '\0' are char_len == 1.  */
       /* '.' accepts any one character except the following two cases.  */
-      if ((!(dfa->syntax & REG_DOT_NEWLINE) &&
+      if ((!(dfa->syntax & RE_DOT_NEWLINE) &&
           re_string_byte_at (input, str_idx) == '\n') ||
-         ((dfa->syntax & REG_DOT_NOT_NULL) &&
+         ((dfa->syntax & RE_DOT_NOT_NULL) &&
           re_string_byte_at (input, str_idx) == '\0'))
        return 0;
       return char_len;
@@ -3945,6 +4000,7 @@
 
 # ifdef _LIBC
 static unsigned int
+internal_function
 find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
 {
   uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
@@ -4028,13 +4084,13 @@
 
 #ifdef RE_ENABLE_I18N
     case OP_UTF8_PERIOD:
-      if (ch >= 0x80)
+      if (ch >= ASCII_CHARS)
         return false;
       /* FALLTHROUGH */
 #endif
     case OP_PERIOD:
-      if ((ch == '\n' && !(mctx->dfa->syntax & REG_DOT_NEWLINE))
-         || (ch == '\0' && (mctx->dfa->syntax & REG_DOT_NOT_NULL)))
+      if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
+         || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
        return false;
       break;
 
@@ -4064,6 +4120,10 @@
   reg_errcode_t ret;
   re_string_t *pstr = &mctx->input;
 
+  /* Avoid overflow.  */
+  if (BE (SIZE_MAX / 2 / sizeof (re_dfastate_t *) <= pstr->bufs_len, 0))
+    return REG_ESPACE;
+
   /* Double the lengthes of the buffers.  */
   ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
   if (BE (ret != REG_NOERROR, 0))
@@ -4075,8 +4135,8 @@
       /* XXX We have no indication of the size of this buffer.  If this
         allocation fail we have no indication that the state_log array
         does not have the right size.  */
-      re_dfastate_t **new_array = re_xrealloc (mctx->state_log, re_dfastate_t 
*,
-                                              pstr->bufs_len + 1);
+      re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *,
+                                             pstr->bufs_len + 1);
       if (BE (new_array == NULL, 0))
        return REG_ESPACE;
       mctx->state_log = new_array;
@@ -4124,8 +4184,15 @@
   mctx->match_last = REG_MISSING;
   if (n > 0)
     {
-      mctx->bkref_ents = re_xmalloc (struct re_backref_cache_entry, n);
-      mctx->sub_tops = re_xmalloc (re_sub_match_top_t *, n);
+      /* Avoid overflow.  */
+      size_t max_object_size =
+       MAX (sizeof (struct re_backref_cache_entry),
+            sizeof (re_sub_match_top_t *));
+      if (BE (SIZE_MAX / max_object_size < n, 0))
+       return REG_ESPACE;
+
+      mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
+      mctx->sub_tops = re_malloc (re_sub_match_top_t *, n);
       if (BE (mctx->bkref_ents == NULL || mctx->sub_tops == NULL, 0))
        return REG_ESPACE;
     }
@@ -4191,14 +4258,14 @@
 
 static reg_errcode_t
 internal_function
-match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx,
-                    Idx from, Idx to)
+match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx, Idx from,
+                    Idx to)
 {
   if (mctx->nbkref_ents >= mctx->abkref_ents)
     {
       struct re_backref_cache_entry* new_entry;
-      new_entry = re_x2realloc (mctx->bkref_ents, struct 
re_backref_cache_entry,
-                               &mctx->abkref_ents);
+      new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry,
+                             mctx->abkref_ents * 2);
       if (BE (new_entry == NULL, 0))
        {
          re_free (mctx->bkref_ents);
@@ -4206,8 +4273,8 @@
        }
       mctx->bkref_ents = new_entry;
       memset (mctx->bkref_ents + mctx->nbkref_ents, '\0',
-             (sizeof (struct re_backref_cache_entry)
-              * (mctx->abkref_ents - mctx->nbkref_ents)));
+             sizeof (struct re_backref_cache_entry) * mctx->abkref_ents);
+      mctx->abkref_ents *= 2;
     }
   if (mctx->nbkref_ents > 0
       && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx)
@@ -4271,16 +4338,16 @@
 #endif
   if (BE (mctx->nsub_tops == mctx->asub_tops, 0))
     {
-      Idx new_asub_tops = mctx->asub_tops;
-      re_sub_match_top_t **new_array = re_x2realloc (mctx->sub_tops,
-                                                    re_sub_match_top_t *,
-                                                    &new_asub_tops);
+      Idx new_asub_tops = mctx->asub_tops * 2;
+      re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
+                                                  re_sub_match_top_t *,
+                                                  new_asub_tops);
       if (BE (new_array == NULL, 0))
        return REG_ESPACE;
       mctx->sub_tops = new_array;
       mctx->asub_tops = new_asub_tops;
     }
-  mctx->sub_tops[mctx->nsub_tops] = re_calloc (re_sub_match_top_t, 1);
+  mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
   if (BE (mctx->sub_tops[mctx->nsub_tops] == NULL, 0))
     return REG_ESPACE;
   mctx->sub_tops[mctx->nsub_tops]->node = node;
@@ -4298,16 +4365,16 @@
   re_sub_match_last_t *new_entry;
   if (BE (subtop->nlasts == subtop->alasts, 0))
     {
-      Idx new_alasts = subtop->alasts;
-      re_sub_match_last_t **new_array = re_x2realloc (subtop->lasts,
-                                                     re_sub_match_last_t *,
-                                                     &new_alasts);
+      Idx new_alasts = 2 * subtop->alasts + 1;
+      re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
+                                                   re_sub_match_last_t *,
+                                                   new_alasts);
       if (BE (new_array == NULL, 0))
        return NULL;
       subtop->lasts = new_array;
       subtop->alasts = new_alasts;
     }
-  new_entry = re_calloc (re_sub_match_last_t, 1);
+  new_entry = calloc (1, sizeof (re_sub_match_last_t));
   if (BE (new_entry != NULL, 1))
     {
       subtop->lasts[subtop->nlasts] = new_entry;
@@ -4320,10 +4387,8 @@
 
 static void
 internal_function
-sift_ctx_init (re_sift_context_t *sctx,
-              re_dfastate_t **sifted_sts,
-              re_dfastate_t **limited_sts,
-              Idx last_node, Idx last_str_idx)
+sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
+              re_dfastate_t **limited_sts, Idx last_node, Idx last_str_idx)
 {
   sctx->sifted_states = sifted_sts;
   sctx->limited_states = limited_sts;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]