sexp_parser.c 17.3 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* sexp_parser.c
Niels Möller's avatar
Niels Möller committed
2
3
4
5
6
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
Niels Möller's avatar
Niels Möller committed
7
 * Copyright (C) 1998, 1999, Ron Rivest, Niels Mller
Niels Möller's avatar
Niels Möller committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

Niels Möller's avatar
Hacked    
Niels Möller committed
24
25
#include "sexp_parser.h"

26
27
#include "format.h"
#include "parse_macros.h"
Niels Möller's avatar
Hacked    
Niels Möller committed
28
29
30
#include "werror.h"
#include "xalloc.h"

31
32
33
34
#include <assert.h>
#include <string.h>

/* Automatically generated files. */
Niels Möller's avatar
Hacked    
Niels Möller committed
35
#include "sexp_table.h"
36
#include "digit_table.h"
Niels Möller's avatar
Hacked    
Niels Möller committed
37

38
39
40
41
42
43
44
45
46
47
48
/* Returns the length of the segmant of characters of a class */
static UINT32 sexp_scan_class(struct simple_buffer *buffer, int class)
{
  UINT32 i;

  for (i=0; i<LEFT; i++)
    if (!(sexp_char_classes[HERE[i]] & class))
      break;

  return i;
}
Niels Möller's avatar
Niels Möller committed
49

50
static void sexp_skip_space(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
51
{
52
53
  ADVANCE(sexp_scan_class(buffer, CHAR_space));
}
Niels Möller's avatar
Niels Möller committed
54

55
56
57
58
59
60
/* Skip the following input character on input stream struct
 * simple_buffer *buffer, if it is equal to a given character. Return 1
 * on success, 0 on error. */
static int sexp_skip_char(struct simple_buffer *buffer, UINT8 expect)
{
  UINT8 c;
Niels Möller's avatar
Niels Möller committed
61
  
62
63
64
65
66
67
68
69
  if (!LEFT)
    {
      werror("sexp: Unexpected EOF when expecting character %x.\n",
	     expect);
      return 0;
    }
  c = GET();
  if (c != expect)
Niels Möller's avatar
Niels Möller committed
70
    {
71
72
      werror("sexp: Expected char %x, got %x.\n", expect, c);
      return 0;
Niels Möller's avatar
Niels Möller committed
73
    }
74
75

  return 1;
Niels Möller's avatar
Niels Möller committed
76
77
}

78
79
/* Parse one or more characters into a simple string as a token. */
static struct lsh_string *sexp_parse_token(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
80
{
81
82
83
84
85
86
87
  UINT32 length;
  struct lsh_string *token;
  
  assert(LEFT);
  assert(sexp_char_classes[*HERE] & CHAR_token_start);
  
  length = sexp_scan_class(buffer, CHAR_token);
Niels Möller's avatar
Niels Möller committed
88

89
90
91
92
93
  if (!length)
    {
      werror("sexp: Invalid token.\n");
      return NULL;
    }
Niels Möller's avatar
Niels Möller committed
94

95
96
  token = ssh_format("%ls", length, HERE);
  ADVANCE(length);
Niels Möller's avatar
Niels Möller committed
97

98
99
  return token;
}
Niels Möller's avatar
Niels Möller committed
100

101
102
/* Parse a decimal number */
static int sexp_parse_decimal(struct simple_buffer *buffer, UINT32 *value)
Niels Möller's avatar
Niels Möller committed
103
{
104
105
  unsigned length = sexp_scan_class(buffer, CHAR_digit);
  unsigned i;
Niels Möller's avatar
Niels Möller committed
106
  
107
108
109
  assert(length);
  
  if ((*HERE == '0') && (length != 1))
Niels Möller's avatar
Niels Möller committed
110
    {
111
112
113
      /* No leading zeros allowed */
      werror("sexp: Unexpected leading zeroes\n");
      return 0;
Niels Möller's avatar
Niels Möller committed
114
    }
115
  if (length > 8)
Niels Möller's avatar
Niels Möller committed
116
    {
117
118
119
      werror("sexp: Decimal number too long (%d digits, max is 8).\n",
	     length);
      return 0;
Niels Möller's avatar
Niels Möller committed
120
    }
121
122
  for (i = 0, *value = 0; i<length; i++)
    *value = *value * 10 + HERE[i] - '0';
Niels Möller's avatar
Niels Möller committed
123

124
125
  ADVANCE(length);
  return 1;
Niels Möller's avatar
Niels Möller committed
126
127
}

128
129
130
/* Reads a literal string of given length. */
static struct lsh_string *
sexp_parse_literal(struct simple_buffer *buffer, UINT32 length)
Niels Möller's avatar
Niels Möller committed
131
{
132
133
134
135
136
137
138
  struct lsh_string *res;
  
  if (LEFT < length)
    {
      werror("sexp: Unexpected EOF in literal.\n");
      return NULL;
    }
Niels Möller's avatar
Niels Möller committed
139

140
141
  res = ssh_format("%ls", length, HERE);
  ADVANCE(length);
Niels Möller's avatar
Niels Möller committed
142

143
  return res;
Niels Möller's avatar
Niels Möller committed
144
145
}

146
147
#define QUOTE_END -1
#define QUOTE_INVALID -2
Niels Möller's avatar
Niels Möller committed
148

149
static int sexp_dequote(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
150
{
151
  int c;
Niels Möller's avatar
Niels Möller committed
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  if (!LEFT)
    return QUOTE_INVALID;
  
  c = GET();

 loop:
  switch (c)
	{
	default:
	  return c;
	case '"':
	  return QUOTE_END;
	case '\\':
	  if (!LEFT)
	    return QUOTE_INVALID;

	  switch( (c = GET()) )
	    {
	    case '\\':
	    case '"':
	    case '\'':
	      return c;
	    case 'b':
	      return 0x8;
	    case 't':
	      return 0x9;
	    case 'n':
	      return 0xa;
	    case 'v':
	      return 0xb;
	    case 'f':
	      return 0xc;
	    case 'r':
	      return 0xd;
	    case '\r':
	      /* Ignore */
	      if (!LEFT)
		return QUOTE_INVALID;
	      c = GET();
	      if (c == '\n')
		{ /* Ignore this too */
		  if (!LEFT)
		    return QUOTE_INVALID;
		  c = GET();
		}
	      goto loop;
	    case '\n':
	      /* Ignore */
	      if (!LEFT)
		return QUOTE_INVALID;
	      c = GET();
	      if (c == '\r')
		{ /* Ignore this too */
		  if (!LEFT)
		    return QUOTE_INVALID;
		  c = GET();
		}
	      goto loop;
	      
	    default:
	      /* Octal escape sequence */
	      {
		int value;
		unsigned i;
	    
		if (!(sexp_char_classes[c] & CHAR_octal))
		  {
		    werror("sexp: Invalid escape character in"
			   " quoted string: %x.\n", c);
		    return QUOTE_INVALID;
		  }

		if (LEFT < 2)
		  {
		    werror("sexp: Unexpected eof in octal escape sequence.\n");
		    return QUOTE_INVALID;
		  }
	    	      
		value = c - '0';
		for (i = 1; i<3; i++)
		  {
		    c = GET();
		    if (!(sexp_char_classes[c] & CHAR_octal))
		      {
			werror("sexp: Invalid character %x in"
			       " octal escape sequence.\n", c);
			return QUOTE_INVALID;
		      }
		    value = (value << 3) + (c - '0');
		  }
		return value;
	      }
	    }
	}
}
	
/* Reads a quoted string of given length. Handles ordinary C escapes.
 * Assumes that the starting '"' have been skipped already. */
static struct lsh_string *
sexp_parse_quoted_length(struct simple_buffer *buffer, UINT32 length)
Niels Möller's avatar
Niels Möller committed
253
{
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  struct lsh_string *res;
  UINT32 i;
  
  res = lsh_string_alloc(length);
  
  for (i = 0; i < length; i++)
    {
      int c = sexp_dequote(buffer);

      if (c < 0)
	{
	  if (c == QUOTE_END)
	    werror("sexp: Quoted string is too short.\n");
	  lsh_string_free(res);
	  return NULL;
	}
      res->data[i] = (unsigned) c;
    }
  return res;
Niels Möller's avatar
Niels Möller committed
273
274
}

275
276
277
/* Reads a quoted string of indefinite length */
static struct lsh_string *
sexp_parse_quoted(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
278
{
279
280
281
282
283
284
285
  struct lsh_string *res;
  UINT32 length;
  UINT32 i;
  UINT8 *p;
  
  if (*HERE == '"')
    return lsh_string_alloc(0);
Niels Möller's avatar
Niels Möller committed
286

287
288
  /* We want a reasonable upper bound on the string to allocate.
   * Search for a double quote, not preceded by a backslash. */
Niels Möller's avatar
Niels Möller committed
289
  
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  for (p = HERE; p < HERE + LEFT; )
    {
      p = memchr(p, '"', (HERE + LEFT) - p);
      if (!p)
	{
	  werror("sexp: Unexpected EOF in quoted string.\n");
	  return NULL;
	}
      if (p[-1] != '\\')
	break;

      p++;
    }

  length = p - HERE;
  res = lsh_string_alloc(length);

  for (i = 0; i<length; i++)
Niels Möller's avatar
Hacked    
Niels Möller committed
308
    {
309
310
311
312
313
314
315
316
317
318
319
320
321
      int c = sexp_dequote(buffer);

      switch (c)
	{
	case QUOTE_INVALID:
	  lsh_string_free(res);
	  return NULL;
	case QUOTE_END:
	  res->length = i;
	  return res;
	default:
	  res->data[i] = (unsigned) c;
	}
Niels Möller's avatar
Hacked    
Niels Möller committed
322
    }
Niels Möller's avatar
Niels Möller committed
323

324
325
326
  /* We haven't seen the ending double quote yet. We must be looking at it now. */
  if (!sexp_skip_char(buffer, '"'))
    fatal("Internal error!\n");
Niels Möller's avatar
Hacked    
Niels Möller committed
327
  
328
  return res;
Niels Möller's avatar
Niels Möller committed
329
330
}

331
static int sexp_dehex(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
332
{
333
334
  unsigned i;
  int value = 0;
Niels Möller's avatar
Niels Möller committed
335

336
337
338
339
340
341
342
343
344
345
346
  for (i = 0; i<2; i++)
    {
      int c;
      
      sexp_skip_space(buffer);

      if (!LEFT)
	{
	  werror("sexp: Unexpected EOF in hex string.\n");
	  return HEX_INVALID;
	}
Niels Möller's avatar
Niels Möller committed
347

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
      c = hex_digits[GET()];
      
      switch (c)
	{
	case HEX_END:
	  if (!i)
	    return HEX_END;
	  /* Fall through */
	case  HEX_INVALID:
	  return HEX_INVALID;
	default:
	  value = (value << 4) | c;
	}
    }
  return value;
Niels Möller's avatar
Niels Möller committed
363
364
}

365
366
367
368
/* Reads a hex string of given length. Handles ordinary C escapes.
 * Assumes that the starting '#' have been skipped already. */
static struct lsh_string *
sexp_parse_hex_length(struct simple_buffer *buffer, UINT32 length)
Niels Möller's avatar
Niels Möller committed
369
{
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  struct lsh_string *res;
  UINT32 i;
  
  res = lsh_string_alloc(length);
  
  for (i = 0; i < length; i++)
    {
      int c =  sexp_dehex(buffer);

      if (c < 0)
	{
	  if (c == HEX_END)
	    werror("sexp: Hex string is too short.\n");
	  lsh_string_free(res);
	  return NULL;
	}
      res->data[i] = (unsigned) c;
    }
  return res;
}

/* Reads a hex string of indefinite length */
static struct lsh_string *
sexp_parse_hex(struct simple_buffer *buffer)
{
  struct lsh_string *res;
  
  UINT32 length = sexp_scan_class(buffer, CHAR_hex | CHAR_space);
  UINT32 terminator = buffer->pos + length;
Niels Möller's avatar
Niels Möller committed
399

400
401
402
403
  UINT32 i;
  
  if ( (length == LEFT)
       || (HERE[terminator] != '#'))
Niels Möller's avatar
Niels Möller committed
404
    {
405
406
407
      werror("sexp: Unexpected EOF in hex string.\n");
      return NULL;
    }
Niels Möller's avatar
Niels Möller committed
408

409
410
  /* The number of digits, divided by two, rounded upwards,
   * is an upper limit on the length. */
Niels Möller's avatar
Niels Möller committed
411

412
  length = (length + 1) / 2;
Niels Möller's avatar
Niels Möller committed
413

414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  res = lsh_string_alloc(length);

  for (i = 0; i < length; i++)
    {
      int c = sexp_dehex(buffer);

      switch (c)
	{
	case HEX_INVALID:
	  lsh_string_free(res);
	  return NULL;
	case HEX_END:
	  res->length = i;
	  return res;
	default:
	  res->data[i] = (unsigned) c;
	}
Niels Möller's avatar
Niels Möller committed
431
    }
432
433
434
435
436

  assert(sexp_scan_class(buffer, CHAR_space) == (terminator - buffer->pos));
  buffer->pos = terminator + 1;

  return res;
Niels Möller's avatar
Niels Möller committed
437
438
}

439
struct base64_state
Niels Möller's avatar
Niels Möller committed
440
{
441
442
443
444
  /* Bits are shifted into the buffer from the right, 6 at a time */
  unsigned buffer;
  /* Bits currently in the buffer */
  unsigned bits;
Niels Möller's avatar
Niels Möller committed
445

446
447
  UINT8 terminator;
};
Niels Möller's avatar
Hacked    
Niels Möller committed
448

449
#define BASE64_INIT(t) {0, 0, (t)}
Niels Möller's avatar
Niels Möller committed
450

451
452
453
454
455
456
457
458
459
460
461
462
/* Extracts one octet from the base64 encoded input. */
static int sexp_decode_base64(struct simple_buffer *buffer,
			      struct base64_state *state)
{
  int res;

  assert(state->bits <= 8);

  while (state->bits < 8)
    {
      UINT8 c;
      int digit;
Niels Möller's avatar
Niels Möller committed
463
      
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
      if (!LEFT)
	return BASE64_INVALID;
      
      c = GET();
      if (c == state->terminator)
	{
	  /* Check for unused bits */
	  if (state->bits && ((1<<state->bits) & state->buffer))
	    {
	      werror("sexp: Base64 terminated with %d leftover bits.\n",
		     state->bits);
	      return BASE64_INVALID;
	    }
	  return BASE64_END;
	}

      digit = base64_digits[c];

      switch (digit)
	{
	case BASE64_SPACE:
	  continue;
	case BASE64_INVALID:
	  return BASE64_INVALID;
	default:
	  state->buffer = (state->buffer << 6) | digit;
	  state->bits += 6;
	}
Niels Möller's avatar
Niels Möller committed
492
    }
493
494
495
496
  res = (state->buffer >> (state->bits - 8)) & 0xff;
  state->bits -= 8;

  return res;
Niels Möller's avatar
Niels Möller committed
497
498
}

499
500
501
502
503
504
505
/* Reads a base64-encoded string of given length. */
static struct lsh_string *
sexp_parse_base64_length(struct simple_buffer *buffer,
			UINT32 length,
			UINT8 terminator)
{
  struct base64_state state = BASE64_INIT(terminator);
Niels Möller's avatar
Hacked    
Niels Möller committed
506

507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  struct lsh_string *res;
  UINT32 i;
  
  res = lsh_string_alloc(length);
  
  for (i = 0; i < length; i++)
    {
      int c = sexp_decode_base64(buffer, &state);

      if (c < 0)
	{
	  if (c == BASE64_END)
	    werror("sexp: Base string is too short.\n");
	  lsh_string_free(res);
	  return NULL;
	}
      res->data[i] = (unsigned) c;
    }
  return res;
}

/* Reads a base64-encoded string of indefinite length. */
static struct lsh_string *
sexp_parse_base64(struct simple_buffer *buffer, UINT8 delimiter)
Niels Möller's avatar
Hacked    
Niels Möller committed
531
{
532
533
  struct base64_state state = BASE64_INIT(delimiter);
  struct lsh_string *res;
Niels Möller's avatar
Hacked    
Niels Möller committed
534

535
536
  UINT32 length = sexp_scan_class(buffer, CHAR_base64 | CHAR_space);
  UINT32 terminator = buffer->pos + length;
Niels Möller's avatar
Hacked    
Niels Möller committed
537

538
539
540
541
542
543
544
545
  UINT32 i;
  
  if ( (length == LEFT)
       || (HERE[terminator] != delimiter))
    {
      werror("sexp: Unexpected EOF in base64 string.\n");
      return NULL;
    }
Niels Möller's avatar
Hacked    
Niels Möller committed
546

547
548
  /* The number of digits, multiplied by 3/4, rounded upwards,
   * is an upper limit on the length. */
Niels Möller's avatar
Hacked    
Niels Möller committed
549

550
  length = ( (length + 1) * 3) / 4;
Niels Möller's avatar
Niels Möller committed
551

552
  res = lsh_string_alloc(length);
Niels Möller's avatar
Niels Möller committed
553

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  for (i = 0; i < length; i++)
    {
      int c = sexp_decode_base64(buffer, &state);

      switch (c)
	{
	case BASE64_INVALID:
	  lsh_string_free(res);
	  return NULL;
	case BASE64_END:
	  res->length = i;
	  return res;
	default:
	  res->data[i] = (unsigned) c;
	}
    }
Niels Möller's avatar
Niels Möller committed
570

571
572
  assert(sexp_scan_class(buffer, CHAR_base64_space) == (terminator - buffer->pos));
  buffer->pos = terminator + 1;
Niels Möller's avatar
Niels Möller committed
573

574
  return res;
Niels Möller's avatar
Niels Möller committed
575
}
576
577
578
579
580
  
/* Reads and returns a simple string from the input stream, using the
 * canonical encoding. */
static struct lsh_string *
sexp_parse_string_canonical(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
581
{
582
583
584
585
586
  UINT32 length;
      
  if (sexp_parse_decimal(buffer, &length)
      && sexp_skip_char(buffer, ':'))
    return sexp_parse_literal(buffer, length);
Niels Möller's avatar
Niels Möller committed
587

588
  return NULL;
Niels Möller's avatar
Niels Möller committed
589
}
590
591
592

static struct lsh_string *
sexp_parse_string_advanced(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
593
{
594
595
596
597
  int class;
  
  if (!LEFT)
    return NULL;
Niels Möller's avatar
Niels Möller committed
598

599
600
601
602
603
604
  class = sexp_char_classes[*HERE];

  if (class & CHAR_token_start)
    return sexp_parse_token(buffer);

  if (class & CHAR_digit)
Niels Möller's avatar
Hacked    
Niels Möller committed
605
    {
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
      UINT32 length;
      if (!sexp_parse_decimal(buffer, &length))
	return NULL;

      switch(GET())
	{
	case '|':
	  return sexp_parse_base64_length(buffer, length, '|');
	case '#':
	  return sexp_parse_hex_length(buffer, length);
	case '"':
	  return sexp_parse_quoted_length(buffer, length);
	case ':':
	  return sexp_parse_literal(buffer, length);
	default:
	  werror("sexp: Invalid prefixed string.\n");
	  return NULL;
	}
Niels Möller's avatar
Hacked    
Niels Möller committed
624
    }
625
626
      
  switch(GET())
Niels Möller's avatar
Hacked    
Niels Möller committed
627
    {
628
629
630
631
632
633
634
635
    case '|':
      return sexp_parse_base64(buffer, '|');
    case '#':
      return sexp_parse_hex(buffer);
    case '"':
      return sexp_parse_quoted(buffer);
    default:
      return NULL;
Niels Möller's avatar
Hacked    
Niels Möller committed
636
    }
Niels Möller's avatar
Niels Möller committed
637
638
}

639
640
static struct sexp *
sexp_parse_display_canonical(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
641
{
642
  struct lsh_string *display;
Niels Möller's avatar
Niels Möller committed
643

644
645
646
  sexp_skip_space(buffer);
  
  display = sexp_parse_string_canonical(buffer);
Niels Möller's avatar
Niels Möller committed
647

648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
  if (display)
    {
      sexp_skip_space(buffer);
      if (sexp_skip_char(buffer, ']'))
	{
	  struct lsh_string *contents;
	  
	  sexp_skip_space(buffer);
	  contents = sexp_parse_string_canonical(buffer);

	  if (contents)
	    return make_sexp_string(display, contents);
	}
      lsh_string_free(display);
    }

  return NULL;
Niels Möller's avatar
Niels Möller committed
665
666
}

667
668
static struct sexp *
sexp_parse_display_advanced(struct simple_buffer *buffer)
Niels Möller's avatar
Hacked    
Niels Möller committed
669
{
670
  struct lsh_string *display = sexp_parse_string_advanced(buffer);
Niels Möller's avatar
Hacked    
Niels Möller committed
671

672
673
674
  if (display)
    {
      struct lsh_string *contents;
Niels Möller's avatar
Hacked    
Niels Möller committed
675

676
677
678
679
680
681
682
683
684
      if (sexp_skip_char(buffer, ']')
     	  && ((contents = sexp_parse_string_advanced(buffer))))
	return make_sexp_string(display, contents);

      lsh_string_free(display);
    }
  
  return NULL;
}
Niels Möller's avatar
Niels Möller committed
685
686
687

struct parse_node
{
688
  struct parse_node *prev;
Niels Möller's avatar
Niels Möller committed
689
690
691
  struct sexp *item;
};

692
struct parse_list
Niels Möller's avatar
Niels Möller committed
693
{
694
695
696
697
698
  struct parse_node *tail;
  unsigned count;
};

#define PARSE_LIST_INIT { NULL, 0 }
Niels Möller's avatar
Niels Möller committed
699

700
static struct sexp *build_parse_vector(struct parse_list *p)
Niels Möller's avatar
Niels Möller committed
701
{
702
703
704
705
706
707
  struct object_list *l = alloc_object_list(p->count);
 
  unsigned i = p->count;
  struct parse_node *n = p->tail;
   
  while (n)
Niels Möller's avatar
Niels Möller committed
708
709
    {
      struct parse_node *old = n;
710
711
712
713
714
715
      
      assert(i);
      LIST(l)[--i] = &n->item->super;
      old = n;
      n = n->prev;
      
Niels Möller's avatar
Niels Möller committed
716
717
      lsh_space_free(old);
    }
718
719
720
  assert(!i);
  
  return sexp_v(l);
Niels Möller's avatar
Niels Möller committed
721
722
}

723
static void parse_list_free(struct parse_list *p)
Niels Möller's avatar
Niels Möller committed
724
{
725
  struct parse_node *n = p->tail;
Niels Möller's avatar
Hacked    
Niels Möller committed
726
  
727
728
729
  while (n)
    {
      struct parse_node *old = n;
Niels Möller's avatar
Niels Möller committed
730

731
732
733
734
      /* FIXME: Could we do KILL(n->item); here? */
      n = n->prev;
      lsh_space_free(old);
    }
Niels Möller's avatar
Niels Möller committed
735
736
}

737
static void parse_list_add(struct parse_list *p, struct sexp *e)
Niels Möller's avatar
Niels Möller committed
738
{
739
  struct parse_node *n;
Niels Möller's avatar
Niels Möller committed
740

741
742
743
744
  NEW_SPACE(n);
  n->prev = p->tail;
  n->item = e;
  p->tail = n;
Niels Möller's avatar
Niels Möller committed
745

746
  p->count++;
Niels Möller's avatar
Niels Möller committed
747
748
}

749
static struct sexp *sexp_parse_list_canonical(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
750
{
751
  struct parse_list p = PARSE_LIST_INIT;
Niels Möller's avatar
Niels Möller committed
752

753
  while (LEFT)
Niels Möller's avatar
Niels Möller committed
754
    {
755
756
757
758
759
760
761
762
763
764
765
766
      struct sexp *e;
      
      if (*HERE == ')')
	return build_parse_vector(&p);

      e = sexp_parse_canonical(buffer);
      if (!e)
	{
	  parse_list_free(&p);
	  return NULL;
	}
      parse_list_add(&p, e);
Niels Möller's avatar
Niels Möller committed
767
    }
768
  werror("sexp: Unexpected EOF (missing ')')\n");
Niels Möller's avatar
Niels Möller committed
769
  
770
771
  parse_list_free(&p);
  return NULL;
Niels Möller's avatar
Niels Möller committed
772
773
}

774
static struct sexp *sexp_parse_list_advanced(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
775
{
776
  struct parse_list p = PARSE_LIST_INIT;
Niels Möller's avatar
Niels Möller committed
777

778
  while (LEFT)
Niels Möller's avatar
Niels Möller committed
779
    {
780
      struct sexp *e;
Niels Möller's avatar
Hacked    
Niels Möller committed
781
      
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
      if (*HERE == ')')
	return build_parse_vector(&p);

      e = sexp_parse_advanced(buffer);
      if (!e)
	{
	  parse_list_free(&p);
	  return NULL;
	}
      parse_list_add(&p, e);

      sexp_skip_space(buffer);
    }
  werror("sexp: Unexpected EOF (missing ')')\n");
  
  parse_list_free(&p);
  return NULL;
Niels Möller's avatar
Niels Möller committed
799
800
}

801
struct sexp *sexp_parse_canonical(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
802
{
803
804
805
806
807
  if (!LEFT)
    {
      werror("sexp: Unexpected EOF.\n");
      return NULL;
    }
Niels Möller's avatar
Niels Möller committed
808

809
810
811
812
813
  if (sexp_char_classes[*HERE] & CHAR_digit)
    {
      struct lsh_string *s = sexp_parse_string_canonical(buffer);
      return s ? make_sexp_string(NULL, s) : NULL;
    }
Niels Möller's avatar
Niels Möller committed
814
  
815
  switch(GET())
Niels Möller's avatar
Niels Möller committed
816
817
    {
    case '(':
818
819
820
      return sexp_parse_list_canonical(buffer);
    case '[':
      return sexp_parse_display_canonical(buffer);
Niels Möller's avatar
Niels Möller committed
821
    default:
822
823
      werror("sexp: Syntax error.\n");
      return NULL;
Niels Möller's avatar
Niels Möller committed
824
825
826
    }
}

827
static struct sexp *sexp_decode_transport(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
828
{
829
830
831
832
833
834
  struct lsh_string *s = sexp_parse_base64(buffer, '}');
  struct simple_buffer inner;
  struct sexp *e;
  
  if (!s)
    return NULL;
Niels Möller's avatar
Niels Möller committed
835

836
  simple_buffer_init(&inner, s->length, s->data);
Niels Möller's avatar
Hacked    
Niels Möller committed
837

838
839
840
841
  e = sexp_parse_canonical(&inner);

  if (!parse_eod(&inner))
    e = NULL;
Niels Möller's avatar
Niels Möller committed
842
  
843
844
  lsh_string_free(s);
  return e;
Niels Möller's avatar
Hacked    
Niels Möller committed
845
846
}

847
struct sexp *sexp_parse_transport(struct simple_buffer *buffer)
Niels Möller's avatar
Hacked    
Niels Möller committed
848
{
849
850
851
852
853
  if (!LEFT)
    {
      werror("sexp: Unexpected EOF.\n");
      return NULL;
    }
Niels Möller's avatar
Hacked    
Niels Möller committed
854

855
856
857
858
859
860
861
  if (*HERE == '{')
    {
      ADVANCE(1);
      return sexp_decode_transport(buffer);
    }

  return sexp_parse_canonical(buffer);
Niels Möller's avatar
Hacked    
Niels Möller committed
862
863
}

864
struct sexp *sexp_parse_advanced(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
865
{
866
  if (!LEFT)
Niels Möller's avatar
Niels Möller committed
867
    {
868
869
      werror("sexp: Unexpected EOF.\n");
      return NULL;
Niels Möller's avatar
Niels Möller committed
870
871
    }

872
  switch (*HERE)
Niels Möller's avatar
Niels Möller committed
873
874
    {
    case '(':
875
876
      ADVANCE(1);
      return sexp_parse_list_advanced(buffer);
Niels Möller's avatar
Niels Möller committed
877
    case '{':
878
879
880
881
882
      ADVANCE(1);
      return sexp_decode_transport(buffer);
    case '[':
      ADVANCE(1);
      return sexp_parse_display_advanced(buffer);
Niels Möller's avatar
Niels Möller committed
883
    default:
884
885
886
887
      {
	struct lsh_string *s = sexp_parse_string_advanced(buffer);
	return s ? make_sexp_string(NULL, s) : NULL;
      }
Niels Möller's avatar
Niels Möller committed
888
889
    }
}
890