sexp.c 7.35 KB
Newer Older
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
/* sexp.c
 *
 * Parsing s-expressions.
 */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2002 Niels Mller
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include "sexp.h"

#include <stdlib.h>
#include <string.h>

31 32
/* Initializes the iterator, but one has to call next to get to the
 * first element. */
33
static void
34 35 36 37 38 39 40
sexp_iterator_init(struct sexp_iterator *iterator,
		   unsigned length, const uint8_t *input)
{
  iterator->length = length;
  iterator->buffer = input;
  iterator->pos = 0;
  iterator->level = 0;
41
  iterator->type = SEXP_END; /* Value doesn't matter */
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  iterator->display_length = 0;
  iterator->display = NULL;
  iterator->atom_length = 0;
  iterator->atom = NULL;

  /* FIXME: For other than canonical syntax,
   * skip white space here. */
}

#define EMPTY(i) ((i)->pos == (i)->length)
#define NEXT(i) ((i)->buffer[(i)->pos++])

static int
sexp_iterator_simple(struct sexp_iterator *iterator,
		     unsigned *size,
		     const uint8_t **string)
{
  unsigned length = 0;
  uint8_t c;
  
  if (EMPTY(iterator)) return 0;
  c = NEXT(iterator);
64
  if (EMPTY(iterator)) return 0;
65 66 67 68 69

  if (c >= '1' && c <= '9')
    do
      {
	length = length * 10 + (c - '0');
70 71
	if (length > (iterator->length - iterator->pos))
	  return 0;
72 73 74 75

	if (EMPTY(iterator)) return 0;
	c = NEXT(iterator);
      }
76
    while (c >= '0' && c <= '9');
77

78 79 80 81
  else if (c == '0')
    /* There can be only one */
    c = NEXT(iterator);
  else 
82 83
    return 0;

84
  if (c != ':')
85 86 87 88 89 90 91 92 93 94
    return 0;

  *size = length;
  *string = iterator->buffer + iterator->pos;
  iterator->pos += length;

  return 1;
}

/* All these functions return 1 on success, 0 on failure */
95 96 97 98 99 100

/* Look at the current position in the data. Sets iterator->type, and
 * ignores the old value. */

static int
sexp_iterator_parse(struct sexp_iterator *iterator)
101
{
102 103
  iterator->start = iterator->pos;
  
104 105 106 107 108 109 110 111 112 113 114
  if (EMPTY(iterator))
    {
      if (iterator->level)
	return 0;
      
      iterator->type = SEXP_END;
      return 1;
    }
  switch (iterator->buffer[iterator->pos])
    {
    case '(': /* A list */
115 116 117
      iterator->type = SEXP_LIST;
      return 1;

118
    case ')':
119 120 121
      if (!iterator->level)
	return 0;
      
122
      iterator->pos++;
123
      iterator->type = SEXP_END;      
124 125
      return 1;
      
126 127 128 129 130 131 132 133 134 135 136 137
    case '[': /* Atom with display type */
      iterator->pos++;
      if (!sexp_iterator_simple(iterator,
				&iterator->display_length,
				&iterator->display))
	return 0;
      if (EMPTY(iterator) || NEXT(iterator) != ']')
	return 0;

      break;

    default:
138 139
      /* Must be either a decimal digit or a syntax error.
       * Errors are detected by sexp_iterator_simple. */
140 141 142 143 144 145 146 147 148 149 150 151 152
      iterator->display_length = 0;
      iterator->display = NULL;

      break;
    }

  iterator->type = SEXP_ATOM;
      
  return sexp_iterator_simple(iterator,
			      &iterator->atom_length,
			      &iterator->atom);
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
int
sexp_iterator_first(struct sexp_iterator *iterator,
		    unsigned length, const uint8_t *input)
{
  sexp_iterator_init(iterator, length, input);
  return sexp_iterator_parse(iterator);
}

int
sexp_iterator_next(struct sexp_iterator *iterator)
{
  switch (iterator->type)
    {
    case SEXP_END:
      return 1;
    case SEXP_LIST:
      /* Skip this list */
      return sexp_iterator_enter_list(iterator)
	&& sexp_iterator_exit_list(iterator);
    case SEXP_ATOM:
      /* iterator->pos should already point at the start of the next
       * element. */
      return sexp_iterator_parse(iterator);
    }
177 178
  /* If we get here, we have a bug. */
  abort();
179 180
}

181 182 183 184 185 186 187 188 189 190 191 192
/* Current element must be a list. */
int
sexp_iterator_enter_list(struct sexp_iterator *iterator)
{
  if (iterator->type != SEXP_LIST)
    return 0;

  if (EMPTY(iterator) || NEXT(iterator) != '(')
    /* Internal error */
    abort();

  iterator->level++;
193 194

  return sexp_iterator_parse(iterator);
195 196 197 198 199 200 201 202 203
}

/* Skips the rest of the current list */
int
sexp_iterator_exit_list(struct sexp_iterator *iterator)
{
  if (!iterator->level)
    return 0;

204 205 206
  while(iterator->type != SEXP_END)
    if (!sexp_iterator_next(iterator))
      return 0;
207
      
208 209
  iterator->level--;

210
  return sexp_iterator_parse(iterator);
211 212
}

213 214 215 216 217 218 219 220 221 222 223 224 225
const uint8_t *
sexp_iterator_subexpr(struct sexp_iterator *iterator,
		      unsigned *length)
{
  unsigned start = iterator->start;
  if (!sexp_iterator_next(iterator))
    return 0;

  *length = iterator->start - start;
  return iterator->buffer + start;
}


226 227 228 229 230 231 232 233
int
sexp_iterator_check_type(struct sexp_iterator *iterator,
			 const uint8_t *type)
{
  return (sexp_iterator_enter_list(iterator)
	  && iterator->type == SEXP_ATOM
	  && !iterator->display
	  && strlen(type) == iterator->atom_length
234 235
	  && !memcmp(type, iterator->atom, iterator->atom_length)
	  && sexp_iterator_next(iterator));
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

const uint8_t *
sexp_iterator_check_types(struct sexp_iterator *iterator,
			  unsigned ntypes,
			  const uint8_t **types)
{
  if (sexp_iterator_enter_list(iterator)
      && iterator->type == SEXP_ATOM
      && !iterator->display)
    {
      unsigned i;
      for (i = 0; i<ntypes; i++)
	if (strlen(types[i]) == iterator->atom_length
	    && !memcmp(types[i], iterator->atom,
		       iterator->atom_length))
252
	  return sexp_iterator_next(iterator) ? types[i] : NULL;
253
    }
254
  return NULL;
255 256
}

257 258 259
int
sexp_iterator_assoc(struct sexp_iterator *iterator,
		    unsigned nkeys,
260
		    const uint8_t **keys,
261 262
		    struct sexp_iterator *values)
{
263 264 265 266 267 268 269 270 271 272
  int *found;
  unsigned nfound;
  unsigned i;
  
  found = alloca(nkeys * sizeof(*found));
  for (i = 0; i<nkeys; i++)
    found[i] = 0;

  nfound = 0;
  
273 274 275 276 277
  for (;;)
    {
      switch (iterator->type)
	{
	case SEXP_LIST:
278

279 280 281
	  /* FIXME: Use sexp_iterator_check_type? Problem is to
	   * distinguish syntax errors from unkown keys (which we want
	   * to just ignore). */
282
	  if (!sexp_iterator_enter_list(iterator))
283 284 285 286 287 288 289 290
	    return 0;
	  
	  if (iterator->type == SEXP_ATOM
	      && !iterator->display)
	    {
	      /* Compare to the given keys */
	      for (i = 0; i<nkeys; i++)
		{
291 292 293 294 295
		  /* NOTE: The strlen could be put outside of the
		   * loop */
		  if (strlen(keys[i]) == iterator->atom_length
		      && !memcmp(keys[i], iterator->atom,
				 iterator->atom_length))
296
		    {
297 298 299 300
		      if (found[i])
			/* We don't allow duplicates */
			return 0;

301 302 303 304
		      /* Advance to point to value */
		      if (!sexp_iterator_next(iterator))
			return 0;

305 306 307
		      found[i] = 1;
		      nfound++;
		      
308 309
		      /* Record this position. */
		      values[i] = *iterator;
310
		      
311 312 313 314 315 316 317 318 319
		      break;
		    }
		}
	    }
	  if (!sexp_iterator_exit_list(iterator))
	    return 0;
	  break;
	case SEXP_ATOM:
	  /* Just ignore */
320 321
	  if (!sexp_iterator_next(iterator))
	    return 0;
322 323 324
	  break;
	  
	case SEXP_END:
325 326
	  return sexp_iterator_exit_list(iterator)
	    && (nfound == nkeys);
327 328 329 330 331 332

	default:
	  abort();
	}
    }
}