s3: add function srprs_quoted to parse strings written with cbuf_print_quoted
authorGregor Beck <gbeck@sernet.de>
Thu, 17 Mar 2011 09:22:25 +0000 (10:22 +0100)
committerMichael Adam <obnox@samba.org>
Mon, 4 Apr 2011 22:01:20 +0000 (00:01 +0200)
source3/lib/srprs.c
source3/lib/srprs.h

index 77464f5f3adebddf04ab58b30f1c076d5624beb0..5a032d27b6e24a24b2403ef0dba53043182a3975 100644 (file)
@@ -183,3 +183,43 @@ bool srprs_line(const char** ptr, cbuf* str)
                ;
        return true;
 }
+
+bool srprs_quoted(const char** ptr, cbuf* str)
+{
+       const char* pos = *ptr;
+       const size_t spos = cbuf_getpos(str);
+
+       if (!srprs_char(&pos, '"')) {
+               goto fail;
+       }
+
+       while (true) {
+               while (srprs_charsetinv(&pos, "\\\"", str))
+                       ;
+
+               switch (*pos) {
+               case '\0':
+                       goto fail;
+               case '"':
+                       *ptr  = pos+1;
+                       return true;
+
+               case '\\':
+                       pos++;
+                       if (!srprs_charset(&pos, "\\\"", str)) {
+                               unsigned u;
+                               if (!srprs_hex(&pos, 2, &u)) {
+                                       goto fail;
+                               }
+                               cbuf_putc(str, u);
+                       }
+                       break;
+               default:
+                       assert(false);
+               }
+       }
+
+fail:
+       cbuf_setpos(str, spos);
+       return false;
+}
index bbbcdc776afcb4c1239d401d268b96dfaed26c56..350e08c2c1810aee02309f8a62400083338262e7 100644 (file)
@@ -113,9 +113,10 @@ bool srprs_charsetinv(const char** ptr, const char* set, struct cbuf* oss);
  * assert(*cont == false);
  * assert(strcmp(cbuf_gets(out, 0), "start...continued...end")==0);
  * @endcode
+ * @see cbuf_print_quoted_string
  *
  * @param[in,out] ptr parse position
- * @param[out] str output buffer where to put the match, may be NULL
+ * @param[out] str output buffer where to put the match
  * @param[in,out] cont
  *
  * @return true if matched
@@ -177,5 +178,15 @@ bool srprs_eol(const char** ptr, struct cbuf* nl);
  */
 bool srprs_line(const char** ptr, struct cbuf* str);
 
+/**
+ * Match a quoted string with escaped characters.
+ * @see cbuf_print_quoted
+ *
+ * @param[in,out] ptr parse position
+ * @param[out] str output buffer where to put the match
+ *
+ * @return true if matched
+ */
+bool srprs_quoted(const char** ptr, struct cbuf* str);
 
 #endif /* __SRPRS_H */