vfs_acl_common: rename psd to psd_blob in get_nt_acl_internal()
[Samba.git] / source3 / lib / srprs.c
bloba3fd0c3e48a8e2b0378bb89f55e69b1f569fd362
1 /*
2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /**
21 * @file srprs.c
22 * @author Gregor Beck <gb@sernet.de>
23 * @date Aug 2010
24 * @brief A simple recursive parser.
27 #include "includes.h"
28 #include "srprs.h"
29 #include "cbuf.h"
31 bool srprs_skipws(const char** ptr) {
32 while (isspace(**ptr))
33 ++(*ptr);
34 return true;
37 bool srprs_char(const char** ptr, char c) {
38 if (**ptr == c) {
39 ++(*ptr);
40 return true;
42 return false;
45 bool srprs_str(const char** ptr, const char* str, size_t len)
47 if (len == -1)
48 len = strlen(str);
50 if (memcmp(*ptr, str, len) == 0) {
51 *ptr += len;
52 return true;
54 return false;
57 bool srprs_charset(const char** ptr, const char* set, cbuf* oss)
59 const char* p = strchr(set, **ptr);
60 if (p != NULL && *p != '\0') {
61 cbuf_putc(oss, **ptr);
62 ++(*ptr);
63 return true;
65 return false;
68 bool srprs_charsetinv(const char** ptr, const char* set, cbuf* oss)
70 if ((**ptr != '\0') && (strchr(set, **ptr) == NULL)) {
71 cbuf_putc(oss, **ptr);
72 ++(*ptr);
73 return true;
75 return false;
80 bool srprs_quoted_string(const char** ptr, cbuf* str, bool* cont)
82 const char* pos = *ptr;
83 const size_t spos = cbuf_getpos(str);
85 if (cont == NULL || *cont == false) {
86 if (!srprs_char(&pos, '\"'))
87 goto fail;
90 while (true) {
91 while (srprs_charsetinv(&pos, "\\\"", str))
94 switch (*pos) {
95 case '\0':
96 if (cont == NULL) {
97 goto fail;
98 } else {
99 *ptr = pos;
100 *cont = true;
101 return true;
103 case '\"':
104 *ptr = pos+1;
105 if (cont != NULL) {
106 *cont = false;
108 return true;
110 case '\\':
111 pos++;
112 if (!srprs_charset(&pos, "\\\"", str))
113 goto fail;
114 break;
116 default:
117 assert(false);
121 fail:
122 cbuf_setpos(str, spos);
123 return false;
126 bool srprs_hex(const char** ptr, size_t len, unsigned* u)
128 const char *str = *ptr;
129 const char *pos = *ptr;
130 int ret;
131 int i;
132 char buf[8+1] = {};
134 assert((len >= 1) && (len <= 8));
136 for (i=0; i<len; i++) {
137 if (!srprs_charset(&pos, "0123456789abcdefABCDEF", NULL)) {
138 break;
140 buf[i] = str[i];
143 ret = sscanf(buf, "%8x", u);
145 if ( ret != 1 ) {
146 return false;
149 *ptr = pos;
150 return true;
153 bool srprs_nl(const char** ptr, cbuf* nl)
155 static const char CRLF[] = "\r\n";
156 if (srprs_str(ptr, CRLF, sizeof(CRLF) - 1)) {
157 cbuf_puts(nl, CRLF, sizeof(CRLF) - 1);
158 return true;
160 return srprs_charset(ptr, "\n\r", nl);
163 bool srprs_eos(const char** ptr)
165 return (**ptr == '\0');
168 bool srprs_eol(const char** ptr, cbuf* nl)
170 return srprs_eos(ptr) || srprs_nl(ptr, nl);
173 bool srprs_line(const char** ptr, cbuf* str)
175 while (srprs_charsetinv(ptr, "\n\r", str))
177 return true;
180 bool srprs_quoted(const char** ptr, cbuf* str)
182 const char* pos = *ptr;
183 const size_t spos = cbuf_getpos(str);
185 if (!srprs_char(&pos, '"')) {
186 goto fail;
189 while (true) {
190 while (srprs_charsetinv(&pos, "\\\"", str))
193 switch (*pos) {
194 case '\0':
195 goto fail;
196 case '"':
197 *ptr = pos+1;
198 return true;
200 case '\\':
201 pos++;
202 if (!srprs_charset(&pos, "\\\"", str)) {
203 unsigned u;
204 if (!srprs_hex(&pos, 2, &u)) {
205 goto fail;
207 cbuf_putc(str, u);
209 break;
210 default:
211 assert(false);
215 fail:
216 cbuf_setpos(str, spos);
217 return false;