Imported new improved free regex implementation. Added (again)
[crypto.git] / lib / silcutil / silcregex.h
index 7fac2ac26a27b1b42ba12aaecc2665105591f6e1..b44456f1f726257e84c1453852bdf36ccbfd40a6 100644 (file)
@@ -1,25 +1,19 @@
 /*
 
-  regexpr.h
+  silcregex.h
 
-  Author: Tatu Ylonen <ylo@ngs.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
+  Copyright (C) 2007 Pekka Riikonen
 
-  Permission to use, copy, modify, distribute, and sell this software
-  and its documentation is hereby granted without fee, provided that the
-  above copyright notice appears in all source code copies, the name of
-  Tatu Ylonen is not used to advertise products containing this software
-  or a derivation thereof, and all modified versions are clearly marked
-  as such.
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
 
-  This software is provided "as is" without express or implied warranty.
-
-  Created: Thu Sep 26 17:15:36 1991 ylo
-  Last modified: Fri Jan  3 12:05:45 1992 ylo
-
-  The SILC Regex API by Pekka Riikonen, under the same license as the original
-  code.
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
 
 */
 
  * DESCRIPTION
  *
  * SILC regular expression interface provides Unix and POSIX compliant
- * regular expression compilation and matching.
+ * regular expression compilation and matching.  The syntax is compliant
+ * with Unix and POSIX regular expression syntax.
+ *
+ * The interface also provides many convenience functions to make the use
+ * of regular expressions easier.
  *
  * EXAMPLE
  *
  *
  ***/
 typedef struct SilcRegexObject {
-  char *buffer;                       /* compiled pattern */
-  int allocated;              /* allocated size of compiled pattern */
-  int used;                   /* actual length of compiled pattern */
+  SilcStack rstack;           /* Stack for fast allocations */
+  unsigned char *buffer;       /* compiled pattern */
   char *fastmap;               /* fastmap[ch] is true if ch can start pattern */
   char *translate;            /* translation to apply during comp/match */
+  int allocated;              /* allocated size of compiled pattern */
+  int used;                   /* actual length of compiled pattern */
+  int num_registers;          /* number of registers used */
   char fastmap_accurate;       /* true if fastmap is valid */
   char can_be_null;           /* true if can match empty string */
   char uses_registers;         /* registers used and need to be initialized */
@@ -108,7 +108,17 @@ typedef struct SilcRegexMatchObject {
  * SOURCE
  */
 typedef enum {
-  SILC_REGEX_FLAG_DEFAULT            = 0,
+  SILC_REGEX_DEFAULT   = 0x00000000,
+
+  /* The following flags can be used with silc_regex_match */
+
+  /* The beginning-of-line (^) always fails to match.  This can be useful
+     when beginning of a string should not be interpreted as the beginning
+     of line. */
+  SILC_REGEX_NOTBOL    = 0x00010000,
+
+  /* The end-of-line ($) always fails to match. */
+  SILC_REGEX_NOTEOL    = 0x00020000,
 } SilcRegexFlags;
 /***/
 
@@ -138,8 +148,8 @@ SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
  * SYNOPSIS
  *
  *    SilcBool silc_regex_match(SilcRegex regexp, const char *string,
- *                              SilcUInt32 num_match, SilcRegexMatch match,
- *                              SilcRegexFlags flags);
+ *                              SilcUInt32 string_len, SilcUInt32 num_match,
+ *                              SilcRegexMatch match, SilcRegexFlags flags);
  *
  * DESCRIPTION
  *
@@ -164,13 +174,13 @@ SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
  * EXAMPLE
  *
  *    // Find first match (check if string matches)
- *    if (!silc_regex_match(&reg, "foo20", 0, NULL, 0))
+ *    if (!silc_regex_match(&reg, "foo20", 5, 0, NULL, 0))
  *      no_match;
  *
  *    // Find multiple matches, one by one
  *    SilcRegexMatchStruct match;
  *
- *    while (silc_regex_match(&reg, string, 1, &match, 0)) {
+ *    while (silc_regex_match(&reg, string, len, 1, &match, 0)) {
  *      match_string = silc_memdup(string + match.start,
  *                                 match.end - match.start);
  *      string += match.end;
@@ -181,12 +191,12 @@ SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
  *    SilcRegexMatchStruct match[7];
  *
  *    silc_regex_compile(&reg, "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)", 0);
- *    silc_regex_match(&reg, "http://example.com/page.html", 7, match, 0);
+ *    silc_regex_match(&reg, "http://example.com/page.html", len, 7, match, 0);
  *
  ***/
 SilcBool silc_regex_match(SilcRegex regexp, const char *string,
-                         SilcUInt32 num_match, SilcRegexMatch match,
-                         SilcRegexFlags flags);
+                         SilcUInt32 string_len, SilcUInt32 num_match,
+                         SilcRegexMatch match, SilcRegexFlags flags);
 
 /****f* silcutil/SilcRegexAPI/silc_regex_free
  *
@@ -203,4 +213,66 @@ SilcBool silc_regex_match(SilcRegex regexp, const char *string,
  ***/
 void silc_regex_free(SilcRegex regexp);
 
+/****f* silcutil/SilcRegexAPI/silc_regex
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_regex(const char *string, const char *regex,
+ *                        SilcBuffer match, ...);
+ *
+ * DESCRIPTION
+ *
+ *    Matches the `string' to the regular expression `regex'.  Returns TRUE
+ *    if the `string' matches the regular expression or FALSE if it does not
+ *    match.  The silc_errno is also set to SILC_ERR_NOT_FOUND.
+ *
+ *    The first (whole) match is returned to `match' buffer if it is non-NULL.
+ *    The variable argument list are buffers where multiple matches are
+ *    returned in case of group (parenthesized) regular expression.  The caller
+ *    needs to know how many pointers to provide, in order to get all matches.
+ *    If `match' is non-NULL the variable argument list must be ended with
+ *    NULL.  The data in the `match' and in any other buffer is from `string'
+ *    and must not be freed by the caller.
+ *
+ * EXAMPLE
+ *
+ *    // Simple match
+ *    if (!silc_regex("foobar", "foo.", NULL))
+ *      no_match;
+ *
+ *    // Get the pointer to the first match
+ *    if (!silc_regex("foobar", ".bar", &match, NULL))
+ *      no_match;
+ *
+ *    // Group match
+ *    SilcBufferStruct match, sub1, sub2;
+ *
+ *    if (!silc_regex("Hello World", "(H..).(o..)", &match, &sub1, &sub2, NULL))
+ *      no_match;
+ *
+ ***/
+SilcBool silc_regex(const char *string, const char *regex,
+                   SilcBuffer match, ...);
+
+/****f* silcutil/SilcRegexAPI/silc_regex_buffer
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_regex_buffer(SilcBuffer buffer, const char *regex,
+ *                               SilcBuffer match, ...);
+ *
+ * DESCRIPTION
+ *
+ *    Same as silc_regex but the string to match is in `buffer'.  Returns
+ *    TRUE if the string matches and FALSE if it doesn't.  See examples and
+ *    other information in silc_regex.  The `buffer' and `match' may be the
+ *    same buffer.
+ *
+ ***/
+SilcBool silc_regex_buffer(SilcBuffer buffer, const char *regex,
+                          SilcBuffer match, ...);
+
+/* Backwards support */
+#define silc_string_regex_match(regex, string) silc_regex(string, regex, NULL)
+
 #endif /* SILCREGEX_H */