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

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

#include "sexp.h"
#include "buffer.h"

33
#include <assert.h>
34
35
36
37
38
39
40
41
42
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if HAVE_LIBGMP
# include "bignum.h"
#endif

43
44
/* Code copied from sexp-conv.c: sexp_put_length */
static unsigned
Niels Möller's avatar
Niels Möller committed
45
format_prefix(struct nettle_buffer *buffer,
46
47
	      unsigned length)
{
48
49
50
51
52
53
54
55
  unsigned digit = 1;
  unsigned prefix_length = 1;
  
  for (;;)
    {
      unsigned next = digit * 10;
      if (next > length)
	break;
56

57
58
59
      prefix_length++;
      digit = next;
    }
60

61
62
63
64
65
66
67
68
69
70
71
  if (buffer)
    {
      for (; digit; length %= digit, digit /= 10)
	if (!NETTLE_BUFFER_PUTC(buffer, '0' + length / digit))
	  return 0;
      
      if (!NETTLE_BUFFER_PUTC(buffer, ':'))
	return 0;
    }

  return prefix_length + 1;
72
73
}

Niels Möller's avatar
Niels Möller committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
static unsigned
format_string(struct nettle_buffer *buffer,
	      unsigned length, const uint8_t *s)
{
  unsigned prefix_length = format_prefix(buffer, length);
  if (!prefix_length)
    return 0;

  if (buffer && !nettle_buffer_write(buffer, length, s))
    return 0;

  return prefix_length + length;
}

88
89
unsigned
sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
90
91
{
  unsigned nesting = 0;
92
  unsigned done = 0;
93

94
95
96
  for (;;)
    switch (*format++)
      {
97
      default:
Niels Möller's avatar
Niels Möller committed
98
99
	{
	  const char *start = format - 1;
100
	  unsigned length = 1 + strcspn(format, "()% \t");
Niels Möller's avatar
Niels Möller committed
101
102
103
104
105
106
107
108
109
	  unsigned output_length = format_string(buffer, length, start);
	  if (!output_length)
	    return 0;
	  
	  done += output_length;
	  format = start + length;

	  break;
	}
110
111
112
      case ' ': case '\t':
	break;
	
113
      case '\0':
114
115
116
117
	assert(!nesting);
	    
	return done;

118
      case '(':
119
120
	if (buffer && !NETTLE_BUFFER_PUTC(buffer, '('))
	  return 0;
121

122
	done++;
123
124
125
126
	nesting++;
	break;

      case ')':
127
128
129
130
131
	assert (nesting);
	if (buffer && !NETTLE_BUFFER_PUTC(buffer, ')'))
	  return 0;

	done++;
132
133
134
135
	nesting--;
	break;

      case '%':
136
137
	{
	  int nul_flag = 0;
138

139
140
141
142
	  if (*format == '0')
	    {
	      format++;
	      nul_flag = 1;
143
	    }
144
	  switch (*format++)
145
	    {
146
147
	    default:
	      abort();
148

149
150
151
152
	    case 's':
	      {
		const char *s;
		unsigned length;
Niels Möller's avatar
Niels Möller committed
153
		unsigned output_length;
154
155
156
157
158
159
160
161
162
163
164
165
		
		if (nul_flag)
		  {
		    s = va_arg(args, const char *);
		    length = strlen(s);
		  }
		else
		  {
		    length = va_arg(args, unsigned);
		    s = va_arg(args, const char *);
		  }
		
Niels Möller's avatar
Niels Möller committed
166
167
		output_length = format_string(buffer, length, s);
		if (!output_length)
168
169
		  return 0;

Niels Möller's avatar
Niels Möller committed
170
		done += output_length;
171
172
173
174
175
176
		break;
	      }
	    case 't':
	      {
		const char *s;
		unsigned length;
Niels Möller's avatar
Niels Möller committed
177
		unsigned output_length;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
		
		if (nul_flag)
		  {
		    s = va_arg(args, const char *);
		    if (!s)
		      break;
		    
		    length = strlen(s);
		  }
		else
		  {
		    length = va_arg(args, unsigned);
		    s = va_arg(args, const char *);
		    if (!s)
		      break;
		  }
		
		if (buffer && !NETTLE_BUFFER_PUTC(buffer, '['))
		  return 0;
		done++;
		
Niels Möller's avatar
Niels Möller committed
199
		output_length = format_string(buffer, length, s);
200
	      
Niels Möller's avatar
Niels Möller committed
201
		if (!output_length)
202
		  return 0;
203

Niels Möller's avatar
Niels Möller committed
204
205
		done += output_length;
		
206
207
208
209
210
211
		if (buffer && !NETTLE_BUFFER_PUTC(buffer, ']'))
		  return 0;
		done++;
		
		break;
	      }
212
	      
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	    case 'l':
	      {
		const char *s;
		unsigned length;
		
		if (nul_flag)
		  {
		    s = va_arg(args, const char *);
		    length = strlen(s);
		  }
		else
		  {
		    length = va_arg(args, unsigned);
		    s = va_arg(args, const char *);
		  }

		if (buffer && !nettle_buffer_write(buffer, length, s))
		  return 0;
231
	      
232
233
234
235
236
237
238
		done += length;
		break;
	      }
	    case 'i':
	      {
		uint32_t x = va_arg(args, uint32_t);
		unsigned length;
239
	      
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
		if (x < 0x80)
		  length = 1;
		else if (x < 0x8000L)
		  length = 2;
		else if (x < 0x800000L)
		  length = 3;
		else if (x < 0x80000000L)
		  length = 4;
		else
		  length = 5;
	      
		if (buffer && !(NETTLE_BUFFER_PUTC(buffer, '0' + length)
				&& NETTLE_BUFFER_PUTC(buffer, ':')))
		  return 0;

		done += (2 + length);

		if (buffer)
		  switch(length)
		    {
		    case 5:
		      /* Leading byte needed for the sign. */
		      if (!NETTLE_BUFFER_PUTC(buffer, 0))
			return 0;
		      /* Fall through */
		    case 4:
		      if (!NETTLE_BUFFER_PUTC(buffer, x >> 24))
			return 0;
		      /* Fall through */
		    case 3:
		      if (!NETTLE_BUFFER_PUTC(buffer, (x >> 16) & 0xff))
			return 0;
		      /* Fall through */
		    case 2:
		      if (!NETTLE_BUFFER_PUTC(buffer, (x >> 8) & 0xff))
			return 0;
		      /* Fall through */
		    case 1:
		      if (!NETTLE_BUFFER_PUTC(buffer, x & 0xff))
			return 0;
		      break;
		    default:
		      abort();
		    }
		break;
	      }
	    case 'b':
	      {
288
#if HAVE_LIBGMP
289
290
291
		const MP_INT *n = va_arg(args, const MP_INT *);
		unsigned length;
		unsigned prefix_length;
292
	      
293
294
295
296
297
298
299
300
301
302
303
304
		length = nettle_mpz_sizeinbase_256_s(n);
		prefix_length = format_prefix(buffer, length);
		if (!prefix_length)
		  return 0;

		done += prefix_length;

		if (buffer)
		  {
		    uint8_t *space = nettle_buffer_space(buffer, length);
		    if (!space)
		      return 0;
305
		  
306
307
		    nettle_mpz_get_str_256(length, space, n);
		  }
308

309
		done += length;
310
	      
311
#else /* ! HAVE_LIBGMP */
312
		abort();
313
#endif /* ! HAVE_LIBGMP */
314
315
		break;
	      }
316
	    }
317
	}
318
319
      }
}
320
321
322
323
324
325
326
327
328
329
330
331
332

unsigned
sexp_format(struct nettle_buffer *buffer, const char *format, ...)
{
  va_list args;
  unsigned done;
  
  va_start(args, format);
  done = sexp_vformat(buffer, format, args);
  va_end(args);

  return done;
}