s3:ctdb library: fix the build against older ctdb versions
[kai/samba.git] / source3 / lib / srprs.c
1 /*
2  * Samba Unix/Linux SMB client library
3  *
4  * Copyright (C) Gregor Beck 2010
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @file   srprs.c
22  * @author Gregor Beck <gb@sernet.de>
23  * @date   Aug 2010
24  * @brief  A simple recursive parser.
25  */
26
27 #include "includes.h"
28 #include "srprs.h"
29 #include "cbuf.h"
30
31 bool srprs_skipws(const char** ptr) {
32         while (isspace(**ptr))
33                 ++(*ptr);
34         return true;
35 }
36
37 bool srprs_char(const char** ptr, char c) {
38         if (**ptr == c) {
39                 ++(*ptr);
40                 return true;
41         }
42         return false;
43 }
44
45 bool srprs_str(const char** ptr, const char* str, size_t len)
46 {
47         if (len == -1)
48                 len = strlen(str);
49
50         if (memcmp(*ptr, str, len) == 0) {
51                 *ptr += len;
52                 return true;
53         }
54         return false;
55 }
56
57 bool srprs_charset(const char** ptr, const char* set, cbuf* oss)
58 {
59         const char* p = strchr(set, **ptr);
60         if (p != NULL && *p != '\0') {
61                 cbuf_putc(oss, **ptr);
62                 ++(*ptr);
63                 return true;
64         }
65         return false;
66 }
67
68 bool srprs_charsetinv(const char** ptr, const char* set, cbuf* oss)
69 {
70         if ((**ptr != '\0') && (strchr(set, **ptr) == NULL)) {
71                 cbuf_putc(oss, **ptr);
72                 ++(*ptr);
73                 return true;
74         }
75         return false;
76 }
77
78
79
80 bool srprs_quoted_string(const char** ptr, cbuf* str, bool* cont)
81 {
82         const char* pos = *ptr;
83         const size_t spos = cbuf_getpos(str);
84
85         if (cont == NULL || *cont == false) {
86                 if (!srprs_char(&pos, '\"'))
87                         goto fail;
88         }
89
90         while (true) {
91                 while (srprs_charsetinv(&pos, "\\\"", str))
92                         ;
93
94                 switch (*pos) {
95                 case '\0':
96                         if (cont == NULL) {
97                                 goto fail;
98                         } else {
99                                 *ptr = pos;
100                                 *cont = true;
101                                 return true;
102                         }
103                 case '\"':
104                         *ptr  = pos+1;
105                         if (cont != NULL) {
106                                 *cont = false;
107                         }
108                         return true;
109
110                 case '\\':
111                         pos++;
112                         if (!srprs_charset(&pos, "\\\"", str))
113                                 goto fail;
114                         break;
115
116                 default:
117                         assert(false);
118                 }
119         }
120
121 fail:
122         cbuf_setpos(str, spos);
123         return false;
124 }
125
126 bool srprs_hex(const char** ptr, size_t len, unsigned* u)
127 {
128         static const char* FMT[] = {
129                 "%1x","%2x","%3x","%4x","%5x","%6x","%7x","%8x",
130                 "%9x","%10x","%11x","%12x","%13x","%14x","%15x","%16x"
131         };
132
133         const char* pos = *ptr;
134         int ret;
135         int i;
136
137         assert((len > 0)
138                && (len <= 2*sizeof(unsigned))
139                && (len <= sizeof(FMT)/sizeof(const char*)));
140
141         for (i=0; i<len; i++) {
142                 if (!srprs_charset(&pos, "0123456789abcdefABCDEF", NULL)) {
143                         break;
144                 }
145         }
146
147         ret = sscanf(*ptr, FMT[len-1], u);
148
149         if ( ret != 1 ) {
150                 return false;
151         }
152
153         *ptr = pos;
154         return true;
155 }
156
157 bool srprs_nl(const char** ptr, cbuf* nl)
158 {
159         static const char CRLF[] = "\r\n";
160         if (srprs_str(ptr, CRLF, sizeof(CRLF) - 1)) {
161                 cbuf_puts(nl, CRLF, sizeof(CRLF) - 1);
162                 return true;
163         }
164         return srprs_charset(ptr, "\n\r", nl);
165 }
166
167 bool srprs_eos(const char** ptr)
168 {
169         return (**ptr == '\0');
170 }
171
172 bool srprs_eol(const char** ptr, cbuf* nl)
173 {
174         return  srprs_eos(ptr) || srprs_nl(ptr, nl);
175 }
176
177 bool srprs_line(const char** ptr, cbuf* str)
178 {
179         while (srprs_charsetinv(ptr, "\n\r", str))
180                 ;
181         return true;
182 }
183
184 bool srprs_quoted(const char** ptr, cbuf* str)
185 {
186         const char* pos = *ptr;
187         const size_t spos = cbuf_getpos(str);
188
189         if (!srprs_char(&pos, '"')) {
190                 goto fail;
191         }
192
193         while (true) {
194                 while (srprs_charsetinv(&pos, "\\\"", str))
195                         ;
196
197                 switch (*pos) {
198                 case '\0':
199                         goto fail;
200                 case '"':
201                         *ptr  = pos+1;
202                         return true;
203
204                 case '\\':
205                         pos++;
206                         if (!srprs_charset(&pos, "\\\"", str)) {
207                                 unsigned u;
208                                 if (!srprs_hex(&pos, 2, &u)) {
209                                         goto fail;
210                                 }
211                                 cbuf_putc(str, u);
212                         }
213                         break;
214                 default:
215                         assert(false);
216                 }
217         }
218
219 fail:
220         cbuf_setpos(str, spos);
221         return false;
222 }