sexp-conv.c 20.1 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
/* sexp-conv.c
 *
 * Conversion tool for handling the different flavours of sexp
 * syntax. */

#include "base64.h"
7
8
9
#include "buffer.h"

#include <assert.h>
Niels Möller's avatar
Niels Möller committed
10
11
#include <errno.h>
#include <stdarg.h>
12
13
#include <stdio.h>
#include <stdlib.h>
Niels Möller's avatar
Niels Möller committed
14
#include <string.h>
15

16
17
18
/* For getopt */
#include <unistd.h>

Niels Möller's avatar
Niels Möller committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void
die(const char *format, ...)
#if __GNUC___
     __attribute__((__format__ (__printf__,1, 2)))
     __attribute__((__noreturn__))
#endif
     ;

void
die(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  exit(EXIT_FAILURE);
}
Niels Möller's avatar
Niels Möller committed
37
38
39
40
41
42

enum sexp_mode
  {
    SEXP_CANONICAL = 0,
    SEXP_ADVANCED = 1,
    /* OR:ed with SEXP_CANONICAL or SEXP_ADVANCED when reading
43
     * transport data */
Niels Möller's avatar
Niels Möller committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    SEXP_TRANSPORT = 2,
  };

enum sexp_token
  {
    SEXP_STRING,
    SEXP_DISPLAY_START,
    SEXP_DISPLAY_END,
    SEXP_LIST_START,
    SEXP_LIST_END,
    SEXP_TRANSPORT_START,
    SEXP_TRANSPORT_END,
    SEXP_EOF,
  };

59
60
61
62
63
64
enum sexp_coding
  {
    SEXP_PLAIN,
    SEXP_BASE64,
    SEXP_HEX,
  };
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


/* Hex functions should move out, eventually */
struct hex_decode_ctx
{
  unsigned word;
  unsigned count;
};

static void
hex_decode_init(struct hex_decode_ctx *ctx)
{
  ctx->word = ctx->count = 0;
}

enum { HEX_INVALID = -1, HEX_SPACE=-2 };

/* Returns -1 on error. */
static int
hex_decode_single(struct hex_decode_ctx *ctx,
		  uint8_t *dst,
		  uint8_t src)
{
  static const signed char hex_decode_table[0x80] =
    {
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1, 
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
      -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    };
  
  int digit;
  if (src >= 0x80)
    return -1;

  /* FIXME: This code could use more clever choices of constants. */
  digit = hex_decode_table[src];
  switch (digit)
    {
    case -1:
      return -1;
    case -2:
      return 0;
    default:
      assert(digit >= 0);
      assert(digit < 0x10);

      if (ctx->count)
	{
	  *dst = (ctx->word << 4) | digit;
	  ctx->count = 0;
	  return 1;
	}
      else
	{
	  ctx->word = digit;
	  ctx->count = 1;
	  return 0;
	}
    }
}

static int
hex_decode_status(struct hex_decode_ctx *ctx)
{
  return !ctx->count;
}

Niels Möller's avatar
Niels Möller committed
137
138
139
140
struct sexp_input
{
  FILE *f;
  
141
  enum sexp_coding coding;
Niels Möller's avatar
Niels Möller committed
142
  /* Used in transport mode */
143
144
145
146
  union {
    struct base64_decode_ctx base64;
    struct hex_decode_ctx hex;
  };
Niels Möller's avatar
Niels Möller committed
147

148
149
150
  /* Terminator for current coding */
  uint8_t terminator;
  
Niels Möller's avatar
Niels Möller committed
151
152
153
154
155
156
157
158
159
160
  /* Type of current token */
  enum sexp_token token;

  /* Current token */
  struct nettle_buffer string;

  /* Nesting level */
  unsigned level;
};

161
static void
162
sexp_input_init(struct sexp_input *input, FILE *f)
163
164
{
  input->f = f;
165
  input->coding = SEXP_PLAIN;
166
  input->level = 0;
167
168

  nettle_buffer_init(&input->string);
169
170
}

Niels Möller's avatar
Niels Möller committed
171
172
173
174
struct sexp_output
{
  FILE *f;

175
  enum sexp_coding coding;
Niels Möller's avatar
Niels Möller committed
176
  enum sexp_mode mode;
177
  struct base64_encode_ctx base64;
Niels Möller's avatar
Niels Möller committed
178
179
180
181

  unsigned pos;
};

182
183
184
185
static void
sexp_output_init(struct sexp_output *output, FILE *f, enum sexp_mode mode)
{
  output->f = f;
186
  output->coding = SEXP_PLAIN;
187
188
189
190
191
192
193
  output->mode = mode;
  output->pos = 0;
}


/* Input */

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Returns zero at EOF */
static int
sexp_get_raw_char(struct sexp_input *input, uint8_t *out)
{
  int c = getc(input->f);
  
  if (c < 0)
    {
      if (ferror(input->f))
	die("Read error: %s\n", strerror(errno));

      return 0;
    }
  *out = c;
  return 1;
}

Niels Möller's avatar
Niels Möller committed
211
/* Returns 1 on success. For special tokens,
Niels Möller's avatar
Niels Möller committed
212
213
214
215
 * return 0 and set input->token accordingly. */
static int
sexp_get_char(struct sexp_input *input, uint8_t *out)
{
216
  switch (input->coding)
Niels Möller's avatar
Niels Möller committed
217
    {
218
    case SEXP_BASE64:
Niels Möller's avatar
Niels Möller committed
219
220
221
      for (;;)
	{
	  int done;
222
223
224
	  uint8_t c;
	  
	  if (!sexp_get_raw_char(input, &c))
Niels Möller's avatar
Niels Möller committed
225
	    die("Unexpected end of file in base64 data.\n");
Niels Möller's avatar
Niels Möller committed
226
227
228

	  if (c == '}')
	    {
229
	      if (base64_decode_status(&input->base64))
Niels Möller's avatar
Niels Möller committed
230
231
232
233
234
		{
		  input->token = SEXP_TRANSPORT_END;
		  return 0;
		}
	      else
Niels Möller's avatar
Niels Möller committed
235
		die("Invalid base64 data.\n");
Niels Möller's avatar
Niels Möller committed
236
237
	    }

238
	  done = base64_decode_single(&input->base64, out, c);
Niels Möller's avatar
Niels Möller committed
239
240
241
	  if (done)
	    return 1;
	}
242
    case SEXP_PLAIN:
243
244
      if (sexp_get_raw_char(input, out))
	return 1;
Niels Möller's avatar
Niels Möller committed
245
      
246
247
      input->token = SEXP_EOF;
      return 0;
248

249
250
251
    case SEXP_HEX:
      /* Not yet implemented */
      abort();
252
253
254

    default:
      abort();
255
    }
Niels Möller's avatar
Niels Möller committed
256
257
258
259
260
261
262
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
}

static const char
token_chars[0x80] =
  {
    /* 0, ... 0x1f */
    0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    /* SPC ! " # $ % & '  ( ) * + , - . / */
    0,0,0,0,0,0,0,0, 0,0,1,1,0,1,1,1,
    /* 0 1 2 3 4 5 6 7  8 9 : ; < = > ? */
    1,1,1,1,1,1,1,1, 1,1,1,0,0,1,0,0,
    /* @ A ... O */
    0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,	
    /* P ...             Z [ \] ^ _ */
    1,1,1,1,1,1,1,1, 1,1,1,0,0,0,0,1,
    /* ` a, ... o */
    0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,	
    /* p ...             z { | } ~ DEL */
    1,1,1,1,1,1,1,1, 1,1,1,0,0,0,0,0,
  };

#define TOKEN_CHAR(c) ((c) < 0x80 && token_chars[(c)])

/* Returns 0 at end of token */
static uint8_t
sexp_get_token_char(struct sexp_input *input)
{
  int c = getc(input->f);
  if (c >= 0 && TOKEN_CHAR(c))
    return c;

288
  ungetc(c, input->f);
Niels Möller's avatar
Niels Möller committed
289
290
291
  return 0;
}
     
Niels Möller's avatar
Niels Möller committed
292
/* Return 0 at end-of-string */
Niels Möller's avatar
Niels Möller committed
293
294
295
static int
sexp_get_quoted_char(struct sexp_input *input, uint8_t *c)
{
296
  if (!sexp_get_char(input, c))
Niels Möller's avatar
Niels Möller committed
297
    die("Unexpected end of file in quoted string.\n");
Niels Möller's avatar
Niels Möller committed
298
299
300
301

  for (;;)
    switch (*c)
      {
302
303
      default:
	return 1;
Niels Möller's avatar
Niels Möller committed
304
305
306
      case '\"':
	return 0;
      case '\\':
Niels Möller's avatar
Niels Möller committed
307
308
309
	if (!sexp_get_char(input, c))
	  die("Unexpected end of file in quoted string.\n");

Niels Möller's avatar
Niels Möller committed
310
311
312
313
314
315
316
317
318
319
320
321
322
	switch (*c)
	  {
	  case 'b': *c = '\b'; return 1;
	  case 't': *c = '\t'; return 1;
	  case 'n': *c = '\n'; return 1;
	  case 'f': *c = '\f'; return 1;
	  case 'r': *c = '\r'; return 1;
	  case '\\': *c = '\\'; return 1;
	  case 'o':
	  case 'x':
	    /* Not implemnted */
	    abort();
	  case '\n':
Niels Möller's avatar
Niels Möller committed
323
324
325
326
	    if (!sexp_get_char(input, c))
	      die("Unexpected end of file in quoted string.\n");
	    if (*c == '\r' && !sexp_get_char(input, c))
	      die("Unexpected end of file in quoted string.\n");
Niels Möller's avatar
Niels Möller committed
327
328
	    break;
	  case '\r':
Niels Möller's avatar
Niels Möller committed
329
330
331
332
	    if (!sexp_get_char(input, c))
	      die("Unexpected end of file in quoted string.\n");
	    if (*c == '\n' && !sexp_get_char(input, c))
	      die("Unexpected end of file in quoted string.\n");
Niels Möller's avatar
Niels Möller committed
333
334
335
336
337
	    break;
	  }
      }
}

Niels Möller's avatar
Niels Möller committed
338
static void
Niels Möller's avatar
Niels Möller committed
339
340
sexp_get_quoted_string(struct sexp_input *input)
{
Niels Möller's avatar
Niels Möller committed
341
342
  uint8_t c;

343
344
  assert(input->coding == SEXP_PLAIN);
  
Niels Möller's avatar
Niels Möller committed
345
346
347
  while (sexp_get_quoted_char(input, &c))
    if (!NETTLE_BUFFER_PUTC(&input->string, c))
      die("Virtual memory exhasuted.\n");
Niels Möller's avatar
Niels Möller committed
348
349
}

Niels Möller's avatar
Niels Möller committed
350
static void
Niels Möller's avatar
Niels Möller committed
351
352
353
354
355
356
sexp_get_hex_string(struct sexp_input *input)
{
  /* Not implemented */
  abort();
}

Niels Möller's avatar
Niels Möller committed
357
static void
Niels Möller's avatar
Niels Möller committed
358
359
360
361
sexp_get_base64_string(struct sexp_input *input)
{
  struct base64_decode_ctx ctx;

362
  assert(input->coding == SEXP_PLAIN);
Niels Möller's avatar
Niels Möller committed
363
364
365
366
367
368
369
370

  base64_decode_init(&ctx);
  
  for (;;)
    {
      uint8_t c;
      uint8_t decoded;
      
Niels Möller's avatar
Niels Möller committed
371
372
      if (!sexp_get_char(input, &c))
	die("Unexpected end of file in base64 string.\n");
Niels Möller's avatar
Niels Möller committed
373
374

      if (c == '|')
Niels Möller's avatar
Niels Möller committed
375
376
377
378
379
	{
	  if (!base64_decode_status(&ctx))
	    die("Invalid base64 string.\n");
	  return;
	}
Niels Möller's avatar
Niels Möller committed
380
      
Niels Möller's avatar
Niels Möller committed
381
382
383
      if (base64_decode_single(&ctx, &decoded, c)
	  && !NETTLE_BUFFER_PUTC(&input->string, decoded))
	die("Virtual memory exhasuted.\n");	
Niels Möller's avatar
Niels Möller committed
384
385
386
    }
}

Niels Möller's avatar
Niels Möller committed
387
static void
388
sexp_get_token_string(struct sexp_input *input, uint8_t c)
Niels Möller's avatar
Niels Möller committed
389
{
390
  assert(input->coding == SEXP_PLAIN);
Niels Möller's avatar
Niels Möller committed
391
392

  if (!TOKEN_CHAR(c) || ! NETTLE_BUFFER_PUTC(&input->string, c))
Niels Möller's avatar
Niels Möller committed
393
    die("Invalid token.\n");
Niels Möller's avatar
Niels Möller committed
394
395
396
397
  
  while ( (c = sexp_get_token_char(input)) > 0)
    {
      if (!NETTLE_BUFFER_PUTC(&input->string, c))
Niels Möller's avatar
Niels Möller committed
398
	die("Virtual memory exhasuted.\n");	
Niels Möller's avatar
Niels Möller committed
399
400
401
402
403
    }

  assert (input->string.size);
}

Niels Möller's avatar
Niels Möller committed
404
static void
Niels Möller's avatar
Niels Möller committed
405
406
407
408
409
410
411
412
sexp_get_string(struct sexp_input *input, uint8_t c)
{
  input->string.size = 0;
  input->token = SEXP_STRING;
  
  switch (c)
    {
    case '\"':
Niels Möller's avatar
Niels Möller committed
413
414
415
      sexp_get_quoted_string(input);
      break;
      
Niels Möller's avatar
Niels Möller committed
416
    case '#':
Niels Möller's avatar
Niels Möller committed
417
418
      sexp_get_hex_string(input);
      break;;
Niels Möller's avatar
Niels Möller committed
419
420

    case '|':
Niels Möller's avatar
Niels Möller committed
421
422
      sexp_get_base64_string(input);
      break;
423

Niels Möller's avatar
Niels Möller committed
424
    default:
Niels Möller's avatar
Niels Möller committed
425
426
      sexp_get_token_string(input, c);
      break;
Niels Möller's avatar
Niels Möller committed
427
428
429
    }
}

Niels Möller's avatar
Niels Möller committed
430
static void
431
432
sexp_get_string_length(struct sexp_input *input, enum sexp_mode mode,
		       unsigned length)
Niels Möller's avatar
Niels Möller committed
433
434
435
436
437
438
439
440
441
{
  uint8_t c;
	
  input->string.size = 0;
  input->token = SEXP_STRING;
  
  if (!length)
    {
      /* There must ne no more digits */
Niels Möller's avatar
Niels Möller committed
442
443
      if (!sexp_get_char(input, &c))
	die("Unexpected end of file in string.\n");
Niels Möller's avatar
Niels Möller committed
444
445
446
447
448
    }
  else
    /* Get rest of digits */
    for (;;)
      {
Niels Möller's avatar
Niels Möller committed
449
450
	if (!sexp_get_char(input, &c))
	  die("Unexpected end of file in string.\n");
Niels Möller's avatar
Niels Möller committed
451

452
	if (c < '0' || c > '9')
Niels Möller's avatar
Niels Möller committed
453
454
455
456
457
458
459
460
461
462
463
	  break;
	
	/* FIXME: Check for overflow? */
	length = length * 10 + c - '0';
      }

  switch(c)
    {
    case ':':
      /* Verbatim */
      for (; length; length--)
Niels Möller's avatar
Niels Möller committed
464
	if (!sexp_get_char(input, &c)
Niels Möller's avatar
Niels Möller committed
465
	    || !NETTLE_BUFFER_PUTC(&input->string, c))
Niels Möller's avatar
Niels Möller committed
466
	  die("Unexpected end of file in string.\n");
Niels Möller's avatar
Niels Möller committed
467
      
Niels Möller's avatar
Niels Möller committed
468
      return;
Niels Möller's avatar
Niels Möller committed
469
470

    case '"':
471
      if (mode != SEXP_ADVANCED)
Niels Möller's avatar
Niels Möller committed
472
	die("Encountered quoted string in canonical mode.\n");
Niels Möller's avatar
Niels Möller committed
473
474

      for (; length; length--)
Niels Möller's avatar
Niels Möller committed
475
	if (!sexp_get_quoted_char(input, &c)
Niels Möller's avatar
Niels Möller committed
476
	    || !NETTLE_BUFFER_PUTC(&input->string, c))
Niels Möller's avatar
Niels Möller committed
477
	  die("Unexpected end of string.\n");
Niels Möller's avatar
Niels Möller committed
478

Niels Möller's avatar
Niels Möller committed
479
480
      if (sexp_get_quoted_char(input, &c))
	die("Quoted string longer than expected.\n");
Niels Möller's avatar
Niels Möller committed
481
482
483
484
485
486
487
      
    case '#':
    case '|':
      /* Not yet implemented */
      abort();

    default:
Niels Möller's avatar
Niels Möller committed
488
      die("Invalid string.\n");
Niels Möller's avatar
Niels Möller committed
489
490
491
    }
}

Niels Möller's avatar
Niels Möller committed
492
static void
493
sexp_get_token(struct sexp_input *input, enum sexp_mode mode)
Niels Möller's avatar
Niels Möller committed
494
495
{
  uint8_t c;
496

497
  for(;;)
Niels Möller's avatar
Niels Möller committed
498
499
500
501
502
503
504
    if (!sexp_get_char(input, &c))
      return;
    else
      switch(c)
	{
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
505
	  sexp_get_string_length(input, mode, c - '0');
Niels Möller's avatar
Niels Möller committed
506
	  return;
507
	  
Niels Möller's avatar
Niels Möller committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
	case '(':
	  input->token = SEXP_LIST_START;
	  return;
	  
	case ')':
	  input->token = SEXP_LIST_END;
	  return;

	case '[':
	  input->token = SEXP_DISPLAY_START;
	  return;

	case ']':
	  input->token = SEXP_DISPLAY_END;
	  return;

	case ' ':  /* SPC, TAB, LF, CR */
	case '\t':
	case '\n':
	case '\r':
528
	  if (mode == SEXP_CANONICAL)
Niels Möller's avatar
Niels Möller committed
529
530
	    die("Whitespace encountered in canonical mode.\n");
	  break;
Niels Möller's avatar
Niels Möller committed
531

Niels Möller's avatar
Niels Möller committed
532
	case ';': /* Comments */
533
	  if (mode == SEXP_CANONICAL)
Niels Möller's avatar
Niels Möller committed
534
	    die("Comment encountered in canonical mode.\n");
Niels Möller's avatar
Niels Möller committed
535

Niels Möller's avatar
Niels Möller committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
	  for (;;)
	    {
	      int c = getc(input->f);
	      if (c < 0)
		{
		  if (ferror(input->f))
		    die("Read failed: %s.\n", strerror(errno));
		  else
		    {
		      input->token = SEXP_EOF;
		      return;
		    }
		}
	      if (c == '\n')
		break;
	    }
	  break;
Niels Möller's avatar
Niels Möller committed
553
	  
Niels Möller's avatar
Niels Möller committed
554
555
	default:
	  /* Ought to be a string */
556
	  if (mode != SEXP_ADVANCED)
Niels Möller's avatar
Niels Möller committed
557
558
559
560
561
	    die("Encountered advanced string in canonical mode.\n");

	  sexp_get_string(input, c);
	  return;
	}
Niels Möller's avatar
Niels Möller committed
562
563
}

564
565
566
567
568
569


/* Output routines */

#define LINE_WIDTH 60

570
static void 
571
572
sexp_put_newline(struct sexp_output *output,
		 unsigned indent)
Niels Möller's avatar
Niels Möller committed
573
574
575
{
  unsigned i;
  if (putc('\n', output->f) < 0)
576
    die("Write failed: %s\n", strerror(errno));
Niels Möller's avatar
Niels Möller committed
577

578
  for(i = 0; i < indent; i++)
Niels Möller's avatar
Niels Möller committed
579
    if (putc(' ', output->f) < 0)
580
      die("Write failed: %s\n", strerror(errno));
Niels Möller's avatar
Niels Möller committed
581

582
  output->pos = indent;
Niels Möller's avatar
Niels Möller committed
583
584
}

585
static void
586
sexp_put_char(struct sexp_output *output, unsigned indent,
Niels Möller's avatar
Niels Möller committed
587
588
	      uint8_t c)
{
589
  switch (output->coding)
Niels Möller's avatar
Niels Möller committed
590
    {
591
592
593
594
595
    case SEXP_BASE64:
      {
	uint8_t encoded[2];
	unsigned done;
	unsigned i;
596
      
597
	done = base64_encode_single(&output->base64, encoded, c);
598

599
	assert(done <= sizeof(encoded));
600

601
602
603
604
605
606
	for (i = 0; i<done; i++)
	  {
	    if (indent &&
		output->pos > LINE_WIDTH
		&& output->pos > (indent + 10))
	      sexp_put_newline(output, indent);
607
	  
608
609
	    if (putc(encoded[i], output->f) < 0)
	      die("Write failed: %s\n", strerror(errno));
610

611
612
613
614
615
	    output->pos++;
	  }
	break;
      }
    case SEXP_PLAIN:
616
      output->pos++;
617
618
      if (putc(c, output->f) < 0)
	die("Write failed: %s\n", strerror(errno));
619
620
621
622
623
      break;

    case SEXP_HEX:
      /* Not implemented */
      abort();
624
625
626
    }
}

627
static void
628
629
630
631
632
633
sexp_put_data(struct sexp_output *output, unsigned indent,
	      unsigned length, const uint8_t *data)
{
  unsigned i;

  for (i = 0; i<length; i++)
634
    sexp_put_char(output, indent, data[i]);
635
636
}

637
static void
638
639
640
641
sexp_puts(struct sexp_output *output, unsigned indent,
	  const uint8_t *s)
{
  while (*s)
642
    sexp_put_char(output, indent, *s++);
643
644
}

645
static void
646
647
648
649
650
sexp_put_length(struct sexp_output *output, unsigned indent,
		unsigned length)
{
  unsigned digit = 1;

651
652
653
654
655
656
657
  for (;;)
    {
      unsigned next = digit * 10;
      if (next > length)
	break;
      digit = next;
    }
658
659

  for (; digit; length %= digit, digit /= 10)
660
    sexp_put_char(output, indent, '0' + length / digit);
661
662
}

663
static void
664
665
sexp_put_base64_start(struct sexp_output *output, uint8_t c)
{
666
  assert(output->coding == SEXP_PLAIN);
667
  
668
  sexp_put_char(output, 0, c);
669
670

  base64_encode_init(&output->base64);
671
  output->coding = SEXP_BASE64;
Niels Möller's avatar
Niels Möller committed
672
673
}

674
static void
675
676
677
678
679
sexp_put_base64_end(struct sexp_output *output, uint8_t c)
{
  uint8_t encoded[BASE64_ENCODE_FINAL_LENGTH];
  unsigned done;

680
  assert(output->coding = SEXP_BASE64);
681
682
683

  done = base64_encode_final(&output->base64, encoded);

684
  assert(done <= sizeof(encoded));
685
  
686
  output->coding = SEXP_PLAIN;
687

688
689
  sexp_put_data(output, 0, done, encoded);
  sexp_put_char(output, 0, c);
690
}
Niels Möller's avatar
Niels Möller committed
691

692
static void
693
sexp_put_string(struct sexp_output *output, unsigned indent,
Niels Möller's avatar
Niels Möller committed
694
695
696
		struct nettle_buffer *string)
{
  if (!string->size)
697
698
699
700
    sexp_puts(output, indent,
	      (output->mode == SEXP_ADVANCED) ? "\"\"": "0:");

  else if (output->mode == SEXP_ADVANCED)
Niels Möller's avatar
Niels Möller committed
701
702
    {
      unsigned i;
703
      int token = (string->contents[0] < '0' || string->contents[0] > '9');
Niels Möller's avatar
Niels Möller committed
704
      int quote_friendly = 1;
705
706
707
      static const char escape_names[0x10] =
	{ 0,0,0,0,0,0,0,0, 'b','t','n',0,'f','r',0,0 };

708
      for (i = 0; i<string->size; i++)
Niels Möller's avatar
Niels Möller committed
709
	{
710
	  uint8_t c = string->contents[i];
Niels Möller's avatar
Niels Möller committed
711
712
713
714
	  
	  if (token & !TOKEN_CHAR(c))
	    token = 0;
	  
715
716
717
718
719
720
721
	  if (quote_friendly)
	    {
	      if (c >= 0x7f)
		quote_friendly = 0;
	      else if (c < 0x20 && !escape_names[c])
		quote_friendly = 0;
	    }
Niels Möller's avatar
Niels Möller committed
722
723
724
	}
      
      if (token)
725
	sexp_put_data(output, indent, string->size, string->contents);
Niels Möller's avatar
Niels Möller committed
726
727
728

      else if (quote_friendly)
	{
729
730
	  sexp_put_char(output, indent, '"');

731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
	  for (i = 0; i<string->size; i++)
	    {
	      int escape = 0;
	      uint8_t c = string->contents[i];

	      assert(c < 0x7f);
	      
	      if (c == '\\' || c == '"')
		escape = 1;
	      else if (c < 0x20)
		{
		  escape = 1;
		  c = escape_names[c];
		  assert(c);
		}
746
747
	      if (escape)
		sexp_put_char(output, indent, '\\');
748

749
	      sexp_put_char(output, indent, c);
750
	    }
751
752
	  
	  sexp_put_char(output, indent, '"');
Niels Möller's avatar
Niels Möller committed
753
754
	}
      else
755
756
757
758
759
760
	{
	  sexp_put_base64_start(output, '|');
	  sexp_put_data(output, output->pos,
			string->size, string->contents);
	  sexp_put_base64_end(output, '|');
	}
761
762
    }
  else
763
764
765
766
767
    {
      sexp_put_length(output, indent, string->size);
      sexp_put_char(output, indent, ':');
      sexp_put_data(output, indent, string->size, string->contents);
    }
768
}
Niels Möller's avatar
Niels Möller committed
769

770
static void
771
772
sexp_put_list_start(struct sexp_output *output, unsigned indent)
{
773
  sexp_put_char(output, indent, '(');
774
775
}

776
static void
777
778
sexp_put_list_end(struct sexp_output *output, unsigned indent)
{
779
  sexp_put_char(output, indent, ')');
780
781
}

782
static void
783
784
sexp_put_display_start(struct sexp_output *output, unsigned indent)
{
785
  sexp_put_char(output, indent, '[');
786
787
}

788
static void
789
790
sexp_put_display_end(struct sexp_output *output, unsigned indent)
{
791
  sexp_put_char(output, indent, ']');
792
793
}

794
static void
795
796
sexp_convert_string(struct sexp_input *input, enum sexp_mode mode,
		    struct sexp_output *output, unsigned indent)
797
{
798
  sexp_get_token(input, mode);
799
800
801
802
  if (input->token == SEXP_STRING)
    sexp_put_string(output, indent, &input->string);
  else
    die("Invalid string.\n");
803
804
805
}

static int
806
807
sexp_convert_item(struct sexp_input *input, enum sexp_mode mode,
		  struct sexp_output *output, unsigned indent);
808

809
static void
810
sexp_convert_list(struct sexp_input *input, enum sexp_mode mode,
811
		  struct sexp_output *output, 
812
813
		  unsigned indent)
{
814
  unsigned item;
Niels Möller's avatar
Niels Möller committed
815

816
  for (item = 0;; item++)
817
    {
818
      sexp_get_token(input, mode);
Niels Möller's avatar
Niels Möller committed
819
  
820
      /* Check for end of list */
821
822
823
      if (input->token == SEXP_LIST_END
	  || input->token == SEXP_EOF
	  || input->token == SEXP_TRANSPORT_END)
824
	return;
825

826
827
828
829
830
831
      if (output->mode == SEXP_ADVANCED)
	{
	  /* FIXME: Adapt pretty printing to handle a big first
	   * element. */
	  if (item == 1)
	    {
832
	      sexp_put_char(output, indent, ' ');
833
834
	      indent = output->pos;
	    }
835
836
	  else if (item > 1)
	    sexp_put_newline(output, indent);
837
838
	}
      
839
      if (!sexp_convert_item(input, mode, output, indent))
840
841
	/* Should be detected above */
	abort();
842
843
844
    }
}

Niels Möller's avatar
Niels Möller committed
845
846
847
static void
sexp_convert_file(struct sexp_input *input, struct sexp_output *output)
{
848
  sexp_get_token(input, SEXP_ADVANCED);
Niels Möller's avatar
Niels Möller committed
849
850
851

  while (input->token != SEXP_EOF)
    {
852
      sexp_convert_item(input, SEXP_ADVANCED, output, 0);
Niels Möller's avatar
Niels Möller committed
853
      if (output->mode == SEXP_ADVANCED)
854
855
	sexp_put_newline(output, 0);
	  
856
      sexp_get_token(input, SEXP_ADVANCED);
Niels Möller's avatar
Niels Möller committed
857
858
859
860
861
862
863
864
    }

  if (fflush(output->f) < 0)
    die("Final fflush failed: %s.\n", strerror(errno));
}



865
static void
866
867
sexp_skip_token(struct sexp_input *input, enum sexp_mode mode,
		enum sexp_token token)
868
{
869
  sexp_get_token(input, mode);
870
871
  if (input->token != token)
    die("Syntax error.\n");
872
873
}

874
/* Returns 1 on success  and 0 at end of list/file.
875
876
877
 *
 * Should be called after getting the first token. */
static int
878
879
sexp_convert_item(struct sexp_input *input, enum sexp_mode mode,
		  struct sexp_output *output, unsigned indent)
880
881
882
883
884
885
{
  switch(input->token)
    {
    case SEXP_LIST_START:
      input->level++;
      
886
      sexp_put_list_start(output, indent);
887
      sexp_convert_list(input, mode, output, indent);
888
889
890
      sexp_put_list_end(output, indent);

      if (input->level)
891
	{
892
893
894
	  input->level--;
	  if (input->token == SEXP_LIST_END)
	    break;
895
	}
896
897
898
899
      else if (input->token == SEXP_EOF)
	break;

      die("Invalid list.\n");
900
901
902
      
    case SEXP_LIST_END:
      if (!input->level)
903
	die("Unexpected end of list.\n");
904
905
      
      input->level--;
906
      return 0;
Niels Möller's avatar
Niels Möller committed
907

908
    case SEXP_EOF:
909
910
911
      if (input->level)
	die("Unexpected end of file.\n");
      break;
912
913

    case SEXP_STRING:
914
915
      sexp_put_string(output, indent, &input->string);
      break;
916
    case SEXP_DISPLAY_START:
917
      sexp_put_display_start(output, indent);
918
919
      sexp_convert_string(input, mode, output, indent);
      sexp_skip_token(input, mode, SEXP_DISPLAY_END);
920
      sexp_put_display_end(output, indent);
921
      sexp_convert_string(input, mode, output, indent);
922
923
      break;
      
924
    case SEXP_TRANSPORT_START:
925
      if (mode != SEXP_ADVANCED)
926
	die("Base64 not allowed in canonical mode.\n");
927
928
929
      else
	{
	  unsigned old_level = input->level;
930
931
932
	  assert(input->coding == SEXP_PLAIN);
	  
	  input->coding = SEXP_BASE64;
933
934
935
	  input->level = 0;

	  base64_decode_init(&input->base64);
936
937
938

	  /* FIXME: sexp_convert_list is wrong. */
	  sexp_convert_list(input, SEXP_CANONICAL, output, indent);
939
	  
940
	  input->coding = SEXP_PLAIN;
941
	  input->level = old_level;
942
	  break;
Niels Möller's avatar
Niels Möller committed
943
	}
944
    case SEXP_TRANSPORT_END:
945
946
947
      /* FIXME: Should be moved do sexp_convert_transport */
      if ( (input->coding != SEXP_BASE64)
	   || input->level || !base64_decode_status(&input->base64))
948
	die("Invalid base64.\n");
949

950
      break;
951
    default:
952
      die("Syntax error.\n");
Niels Möller's avatar
Niels Möller committed
953
    }
954
  return 1;
Niels Möller's avatar
Niels Möller committed
955
956
}

957
958
959
960
961
static int
match_argument(const char *given, const char *name)
{
}

962
963
int
main(int argc, char **argv)
964
{  
965
966
  struct sexp_input input;
  struct sexp_output output;
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
  enum sexp_mode mode = SEXP_ADVANCED;
  unsigned width;
  
  int c;
  while ( (c = getopt(argc, argv, "s:w:")) != -1)
    switch (c)
      {
      case 's':
	/* FIXME: Allow abbreviations */
	if (!strcmp(optarg, "advanced"))
	  mode = SEXP_ADVANCED;
	else if (!strcmp(optarg, "transport"))
	  mode = SEXP_TRANSPORT;
	else if (!strcmp(optarg, "canonical"))
	  mode = SEXP_CANONICAL;
	else
	  die("Available syntax variants: advanced, transport, canonical\n");
	break;

      case 'w':
	die("Option -w not yet implemented.\n");
	
      case '?':
	printf("Usage: sexp-conv [-m syntax]\n"
	       "Available syntax variants: advanced, transport, canonical\n");
	return EXIT_FAILURE;
Niels Möller's avatar
Niels Möller committed
993

994
995
996
      default: abort();
      }
  
997
  sexp_input_init(&input, stdin);
998
  sexp_output_init(&output, stdout, mode);
Niels Möller's avatar
Niels Möller committed
999

Niels Möller's avatar
Niels Möller committed
1000
1001
1002
  sexp_convert_file(&input, &output);

  return EXIT_SUCCESS;
1003
}