sexp.c 8.3 KB
Newer Older
1
/* sexp.c
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

   Parsing s-expressions.

   Copyright (C) 2002 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle 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 copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
33

34 35 36
#if HAVE_CONFIG_H
# include "config.h"
#endif
37

38 39 40
#include <stdlib.h>
#include <string.h>

41 42 43
#include "sexp.h"

#include "macros.h"
44
#include "nettle-internal.h"
45

46 47
/* Initializes the iterator, but one has to call next to get to the
 * first element. */
48
static void
49 50 51 52 53 54 55
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;
56
  iterator->type = SEXP_END; /* Value doesn't matter */
57 58 59 60 61 62 63 64 65 66 67
  iterator->display_length = 0;
  iterator->display = NULL;
  iterator->atom_length = 0;
  iterator->atom = NULL;
}

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

static int
sexp_iterator_simple(struct sexp_iterator *iterator,
68
		     size_t *size,
69 70 71 72 73 74 75
		     const uint8_t **string)
{
  unsigned length = 0;
  uint8_t c;
  
  if (EMPTY(iterator)) return 0;
  c = NEXT(iterator);
76
  if (EMPTY(iterator)) return 0;
77 78 79 80 81

  if (c >= '1' && c <= '9')
    do
      {
	length = length * 10 + (c - '0');
82 83
	if (length > (iterator->length - iterator->pos))
	  return 0;
84 85 86 87

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

90 91 92 93
  else if (c == '0')
    /* There can be only one */
    c = NEXT(iterator);
  else 
94 95
    return 0;

96
  if (c != ':')
97 98 99 100 101 102 103 104 105 106
    return 0;

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

  return 1;
}

/* All these functions return 1 on success, 0 on failure */
107 108 109 110 111 112

/* 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)
113
{
114 115
  iterator->start = iterator->pos;
  
116 117 118 119 120 121 122 123 124 125 126
  if (EMPTY(iterator))
    {
      if (iterator->level)
	return 0;
      
      iterator->type = SEXP_END;
      return 1;
    }
  switch (iterator->buffer[iterator->pos])
    {
    case '(': /* A list */
127 128 129
      iterator->type = SEXP_LIST;
      return 1;

130
    case ')':
131 132 133
      if (!iterator->level)
	return 0;
      
134
      iterator->pos++;
135
      iterator->type = SEXP_END;      
136 137
      return 1;
      
138 139 140 141 142 143 144 145 146 147 148 149
    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:
150 151
      /* Must be either a decimal digit or a syntax error.
       * Errors are detected by sexp_iterator_simple. */
152 153 154 155 156 157 158 159 160 161 162 163 164
      iterator->display_length = 0;
      iterator->display = NULL;

      break;
    }

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

165 166
int
sexp_iterator_first(struct sexp_iterator *iterator,
167
		    size_t length, const uint8_t *input)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
  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);
    }
189 190
  /* If we get here, we have a bug. */
  abort();
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204
/* 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++;
205 206

  return sexp_iterator_parse(iterator);
207 208 209 210 211 212 213 214 215
}

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

216 217 218
  while(iterator->type != SEXP_END)
    if (!sexp_iterator_next(iterator))
      return 0;
219
      
220 221
  iterator->level--;

222
  return sexp_iterator_parse(iterator);
223 224
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
#if 0
/* What's a reasonable interface for this? */
int
sexp_iterator_exit_lists(struct sexp_iterator *iterator,
			 unsigned level)
{
  assert(iterator->level >= level);

  while (iterator->level > level)
    if (!sexp_iterator_exit_list(iterator))
      return 0;

  return 1;
}
#endif

241 242
const uint8_t *
sexp_iterator_subexpr(struct sexp_iterator *iterator,
243
		      size_t *length)
244
{
245
  size_t start = iterator->start;
246 247 248 249 250 251 252
  if (!sexp_iterator_next(iterator))
    return 0;

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

253 254 255 256 257 258 259 260 261
int
sexp_iterator_get_uint32(struct sexp_iterator *iterator,
			 uint32_t *x)
{
  if (iterator->type == SEXP_ATOM
      && !iterator->display
      && iterator->atom_length
      && iterator->atom[0] < 0x80)
    {
262
      size_t length = iterator->atom_length;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
      const uint8_t *p = iterator->atom;

      /* Skip leading zeros. */
      while(length && !*p)
	{
	  length--; p++;
	}

      switch(length)
	{
	case 0:
	  *x = 0;
	  break;
	case 1:
	  *x = p[0];
	  break;
	case 2:
	  *x = READ_UINT16(p);
	  break;
	case 3:
	  *x = READ_UINT24(p);
	  break;
	case 4:
	  *x = READ_UINT32(p);
	  break;
	default:
	  return 0;
	}
      return sexp_iterator_next(iterator);
    }
  return 0;
}
295

296 297
int
sexp_iterator_check_type(struct sexp_iterator *iterator,
298
			 const char *type)
299 300 301 302 303
{
  return (sexp_iterator_enter_list(iterator)
	  && iterator->type == SEXP_ATOM
	  && !iterator->display
	  && strlen(type) == iterator->atom_length
304 305
	  && !memcmp(type, iterator->atom, iterator->atom_length)
	  && sexp_iterator_next(iterator));
306 307
}

308
const char *
309 310
sexp_iterator_check_types(struct sexp_iterator *iterator,
			  unsigned ntypes,
311
			  const char * const *types)
312 313 314 315 316 317 318 319 320 321
{
  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))
322
	  return sexp_iterator_next(iterator) ? types[i] : NULL;
323
    }
324
  return NULL;
325 326
}

327 328 329
int
sexp_iterator_assoc(struct sexp_iterator *iterator,
		    unsigned nkeys,
330
		    const char * const *keys,
331 332
		    struct sexp_iterator *values)
{
333
  TMP_DECL(found, int, NETTLE_MAX_SEXP_ASSOC);
334 335
  unsigned nfound;
  unsigned i;
336 337

  TMP_ALLOC(found, nkeys);
338 339 340 341 342
  for (i = 0; i<nkeys; i++)
    found[i] = 0;

  nfound = 0;
  
343 344 345 346 347
  for (;;)
    {
      switch (iterator->type)
	{
	case SEXP_LIST:
348

349
	  if (!sexp_iterator_enter_list(iterator))
350 351 352 353 354 355 356 357
	    return 0;
	  
	  if (iterator->type == SEXP_ATOM
	      && !iterator->display)
	    {
	      /* Compare to the given keys */
	      for (i = 0; i<nkeys; i++)
		{
358 359 360 361 362
		  /* 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))
363
		    {
364 365 366 367
		      if (found[i])
			/* We don't allow duplicates */
			return 0;

368 369 370 371
		      /* Advance to point to value */
		      if (!sexp_iterator_next(iterator))
			return 0;

372 373 374
		      found[i] = 1;
		      nfound++;
		      
375 376
		      /* Record this position. */
		      values[i] = *iterator;
377
		      
378 379 380 381 382 383 384 385 386
		      break;
		    }
		}
	    }
	  if (!sexp_iterator_exit_list(iterator))
	    return 0;
	  break;
	case SEXP_ATOM:
	  /* Just ignore */
387 388
	  if (!sexp_iterator_next(iterator))
	    return 0;
389 390 391
	  break;
	  
	case SEXP_END:
392 393
	  return sexp_iterator_exit_list(iterator)
	    && (nfound == nkeys);
394 395 396 397 398 399

	default:
	  abort();
	}
    }
}