s3-lib: add srprs, primitives to build simple recursive parsers
authorGregor Beck <gbeck@sernet.de>
Wed, 29 Sep 2010 12:50:33 +0000 (14:50 +0200)
committerGregor Beck <gbeck@sernet.de>
Wed, 29 Sep 2010 13:18:27 +0000 (15:18 +0200)
source3/Makefile.in
source3/lib/srprs.c [new file with mode: 0644]
source3/lib/srprs.h [new file with mode: 0644]

index 005e4c21fa2a1d8dc7a545bc9b4af8b5da8a62ab..42cf85612040f2cdb32fd96de2c6ba5ca9ee9201 100644 (file)
@@ -942,7 +942,7 @@ NET_OBJ1 = utils/net.o utils/net_ads.o utils/net_help.o \
           utils/net_g_lock.o \
           utils/net_serverid.o \
           utils/net_eventlog.o \
-          lib/cbuf.o
+          lib/cbuf.o lib/srprs.o
 
 # these are not processed by make proto
 NET_OBJ2 = utils/net_registry_util.o utils/net_help_common.o
diff --git a/source3/lib/srprs.c b/source3/lib/srprs.c
new file mode 100644 (file)
index 0000000..77464f5
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * Samba Unix/Linux SMB client library
+ *
+ * Copyright (C) Gregor Beck 2010
+ *
+ * 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; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * @file   srprs.c
+ * @author Gregor Beck <gb@sernet.de>
+ * @date   Aug 2010
+ * @brief  A simple recursive parser.
+ */
+
+#include "srprs.h"
+#include "cbuf.h"
+#include <ctype.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+bool srprs_skipws(const char** ptr) {
+       while (isspace(**ptr))
+               ++(*ptr);
+       return true;
+}
+
+bool srprs_char(const char** ptr, char c) {
+       if (**ptr == c) {
+               ++(*ptr);
+               return true;
+       }
+       return false;
+}
+
+bool srprs_str(const char** ptr, const char* str, size_t len)
+{
+       if (len == -1)
+               len = strlen(str);
+
+       if (memcmp(*ptr, str, len) == 0) {
+               *ptr += len;
+               return true;
+       }
+       return false;
+}
+
+bool srprs_charset(const char** ptr, const char* set, cbuf* oss)
+{
+       const char* p = strchr(set, **ptr);
+       if (p != NULL && *p != '\0') {
+               cbuf_putc(oss, **ptr);
+               ++(*ptr);
+               return true;
+       }
+       return false;
+}
+
+bool srprs_charsetinv(const char** ptr, const char* set, cbuf* oss)
+{
+       if ((**ptr != '\0') && (strchr(set, **ptr) == NULL)) {
+               cbuf_putc(oss, **ptr);
+               ++(*ptr);
+               return true;
+       }
+       return false;
+}
+
+
+
+bool srprs_quoted_string(const char** ptr, cbuf* str, bool* cont)
+{
+       const char* pos = *ptr;
+       const size_t spos = cbuf_getpos(str);
+
+       if (cont == NULL || *cont == false) {
+               if (!srprs_char(&pos, '\"'))
+                       goto fail;
+       }
+
+       while (true) {
+               while (srprs_charsetinv(&pos, "\\\"", str))
+                       ;
+
+               switch (*pos) {
+               case '\0':
+                       if (cont == NULL) {
+                               goto fail;
+                       } else {
+                               *ptr = pos;
+                               *cont = true;
+                               return true;
+                       }
+               case '\"':
+                       *ptr  = pos+1;
+                       if (cont != NULL) {
+                               *cont = false;
+                       }
+                       return true;
+
+               case '\\':
+                       pos++;
+                       if (!srprs_charset(&pos, "\\\"", str))
+                               goto fail;
+                       break;
+
+               default:
+                       assert(false);
+               }
+       }
+
+fail:
+       cbuf_setpos(str, spos);
+       return false;
+}
+
+bool srprs_hex(const char** ptr, size_t len, unsigned* u)
+{
+       static const char* FMT[] = {
+               "%1x","%2x","%3x","%4x","%5x","%6x","%7x","%8x",
+               "%9x","%10x","%11x","%12x","%13x","%14x","%15x","%16x"
+       };
+
+       const char* pos = *ptr;
+       int ret;
+       int i;
+
+       assert((len > 0)
+              && (len <= 2*sizeof(unsigned))
+              && (len <= sizeof(FMT)/sizeof(const char*)));
+
+       for (i=0; i<len; i++) {
+               if (!srprs_charset(&pos, "0123456789abcdefABCDEF", NULL)) {
+                       break;
+               }
+       }
+
+       ret = sscanf(*ptr, FMT[len-1], u);
+
+       if ( ret != 1 ) {
+               return false;
+       }
+
+       *ptr = pos;
+       return true;
+}
+
+bool srprs_nl(const char** ptr, cbuf* nl)
+{
+       static const char CRLF[] = "\r\n";
+       if (srprs_str(ptr, CRLF, sizeof(CRLF) - 1)) {
+               cbuf_puts(nl, CRLF, sizeof(CRLF) - 1);
+               return true;
+       }
+       return srprs_charset(ptr, "\n\r", nl);
+}
+
+bool srprs_eos(const char** ptr)
+{
+       return (**ptr == '\0');
+}
+
+bool srprs_eol(const char** ptr, cbuf* nl)
+{
+       return  srprs_eos(ptr) || srprs_nl(ptr, nl);
+}
+
+bool srprs_line(const char** ptr, cbuf* str)
+{
+       while (srprs_charsetinv(ptr, "\n\r", str))
+               ;
+       return true;
+}
diff --git a/source3/lib/srprs.h b/source3/lib/srprs.h
new file mode 100644 (file)
index 0000000..bbbcdc7
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * Samba Unix/Linux SMB client library
+ *
+ * Copyright (C) Gregor Beck 2010
+ *
+ * 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; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * @file   srprs.h
+ * @author Gregor Beck <gb@sernet.de>
+ * @date   Aug 2010
+ *
+ * @brief  A simple recursive parser.
+ *
+ * This file contains functions which may be used to build a simple recursive
+ * parser. They all take the parse position as their first argument. If they
+ * match the parse position and the output arguments are updated accordingly and
+ * true is returned else no argument is altered. For arguments of type @ref cbuf
+ * this may hold only up to the current write position.
+ */
+
+#ifndef __SRPRS_H
+#define __SRPRS_H
+
+#include <stddef.h>
+#include <stdbool.h>
+#include <stdint.h>
+struct cbuf;
+
+/**
+ * Matches any amount of whitespace.
+ *
+ * @see isspace
+ * @param ptr parse position
+ *
+ * @return true
+ */
+bool srprs_skipws(const char** ptr);
+
+/**
+ * Match a single character.
+ *
+ * @param[in,out] ptr parse position
+ * @param         c   the character to match
+ *
+ * @return true if matched
+ */
+bool srprs_char(const char** ptr, char c);
+
+/**
+ * Match a string.
+ *
+ * @param[in,out] ptr parse position
+ * @param str string to match
+ * @param len number of bytes to compare, -1 means strlen(str)
+ *
+ * @return true if matched
+ */
+bool srprs_str(const char** ptr, const char* str, size_t len);
+
+/**
+ * Match a single character from a set.
+ * Didn't match '\\0'
+ *
+ * @param[in,out] ptr parse position
+ * @param[in] set the character set to look for
+ * @param[out] oss output buffer where to put the match, may be NULL
+ *
+ * @return true if matched
+ */
+bool srprs_charset(const char** ptr, const char* set, struct cbuf* oss);
+
+/**
+ * Match a single character not in set.
+ * Didn't match '\\0'
+ *
+ * @param[in,out] ptr parse position
+ * @param[in] set the character set to look for
+ * @param[out] oss output buffer where to put the match, may be NULL
+ *
+ * @return true if matched
+ */
+bool srprs_charsetinv(const char** ptr, const char* set, struct cbuf* oss);
+
+/**
+ * Match a quoted string.
+ *
+ *
+ * If cont is not NULL the match may span multiple invocations.
+ * @code
+ * const char* start = "\"start...";
+ * const char* cont  = "continued...";
+ * const char* end   = "end\"";
+ * bool  cont = false;
+ * cbuf* out  = cbuf_new(talloc_tos());
+ * srprs_quoted_string(&start, out, &cont);
+ * assert(*cont == true);
+ * srprs_quoted_string(&cont, out, &cont);
+ * assert(*cont == true);
+ * srprs_quoted_string(&end, out, &cont);
+ * assert(*cont == false);
+ * assert(strcmp(cbuf_gets(out, 0), "start...continued...end")==0);
+ * @endcode
+ *
+ * @param[in,out] ptr parse position
+ * @param[out] str output buffer where to put the match, may be NULL
+ * @param[in,out] cont
+ *
+ * @return true if matched
+ */
+bool srprs_quoted_string(const char** ptr, struct cbuf* str, bool* cont);
+
+/**
+ * Match a hex string.
+ *
+ * @param[in,out] ptr parse position
+ * @param         len maximum number of diggits to match
+ * @param[out]    u   value of the match
+ *
+ * @return true if matched
+ */
+bool srprs_hex(const char** ptr, size_t len, unsigned* u);
+
+/**
+ * Match the empty string at End Of String.
+ * It doesn't consume the '\\0' unlike
+ * @code
+ *   srprs_char(ptr, '\0', NULL);
+ * @endcode
+ *
+ * @param[in,out] ptr parse position
+ *
+ * @return true if **ptr == '\\0'
+ */
+bool srprs_eos(const char** ptr);
+
+/**
+ * Match a newline.
+ * A newline is either '\\n' (LF), '\\r' (CR), or "\r\n" (CRLF)
+ *
+ * @param[in,out] ptr parse position
+ * @param[out]    nl  output buffer where to put the match, may be NULL
+ *
+ * @return true if matched
+ */
+bool srprs_nl(const char** ptr, struct cbuf* nl);
+
+/**
+ * Match a newline or eos.
+ *
+ * @param ptr parse position
+ * @param nl  output buffer where to put the match, may be NULL
+ *
+ * @return true if matched
+ */
+bool srprs_eol(const char** ptr, struct cbuf* nl);
+
+/**
+ * Match a line up to but not including the newline.
+ *
+ * @param[in,out] ptr parse position
+ * @param[out]    str output buffer where to put the match, may be NULL
+ *
+ * @return true
+ */
+bool srprs_line(const char** ptr, struct cbuf* str);
+
+
+#endif /* __SRPRS_H */