summaryrefslogtreecommitdiff
path: root/sexp-transport.c
blob: 1a34db7129b0d0ef251ddd5f1e9834c753e6546e (plain)
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
/* sexp-transport.c

   Parsing s-expressions in transport format.

   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/.
*/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <string.h>

#include "sexp.h"

#include "base64.h"

/* NOTE: Decodes the input string in place */
int
sexp_transport_iterator_first(struct sexp_iterator *iterator,
			      size_t length, uint8_t *input)
{
  /* We first base64 decode any transport encoded sexp at the start of
   * the input. */

  size_t in = 0;
  size_t out = 0;

  while (in < length)
    switch(input[in])
      {
      case ' ':  /* SPC, TAB, LF, CR */
      case '\t':
      case '\n':
      case '\r':
	in++;
	break;
	  
      case ';':  /* Comments */
	while (++in < length && input[in] != '\n')
	  ;
	break;
	  
      case '{':
	{
	  /* Found transport encoding */
	  struct base64_decode_ctx ctx;
	  size_t coded_length;
	  size_t end;

	  for (end = ++in; end < length && input[end] != '}'; end++)
	    ;

	  if (end == length)
	    return 0;
	    
	  base64_decode_init(&ctx);
	  
	  if (base64_decode_update(&ctx, &coded_length, input + out,
				   end - in, (const char*) (input + in))
	      && base64_decode_final(&ctx))
	    {	  
	      out += coded_length;
	      in = end + 1;
	    }
	  else
	    return 0;
	  
	  break;
	}
      default:
	/* Expression isn't in transport encoding. Rest of the input
	 * should be in canonical encoding. */
	goto transport_done;
      }
  
 transport_done:

  /* Here, we have two, possibly empty, input parts in canonical
   * encoding:
   *
   * 0...out-1,  in...length -1
   *
   * If the input was already in canonical encoding, out = 0 and in =
   * amount of leading space.
   *
   * If all input was in transport encoding, in == length.
   */
  if (!out)
    {
      input += in;
      length -= in;
    }
  else if (in == length)
    length = out;
  else if (out == in)
    /* Unusual case, nothing happens */
    ;
  else
    {
      /* Both parts non-empty */
      assert(out < in);
      memmove(input + out, input + in, length - in);
      length -= (in - out);
    }

  return sexp_iterator_first(iterator, length, input);
}