1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/*
* 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 "includes.h"
#include "srprs.h"
#include "cbuf.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;
}
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;
}
|