]>
git.ipfire.org Git - thirdparty/nettle.git/blob - sexp-format.c
5 Copyright (C) 2002 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
50 format_prefix(struct nettle_buffer
*buffer
,
54 unsigned prefix_length
= 1;
58 size_t next
= digit
* 10;
68 for (; digit
; length
%= digit
, digit
/= 10)
69 if (!NETTLE_BUFFER_PUTC(buffer
, '0' + length
/ digit
))
72 if (!NETTLE_BUFFER_PUTC(buffer
, ':'))
76 return prefix_length
+ 1;
80 format_string(struct nettle_buffer
*buffer
,
81 size_t length
, const uint8_t *s
)
83 unsigned prefix_length
= format_prefix(buffer
, length
);
87 if (buffer
&& !nettle_buffer_write(buffer
, length
, s
))
90 return prefix_length
+ length
;
94 strlen_u8 (const uint8_t *s
)
96 return strlen((const char*) s
);
100 sexp_vformat(struct nettle_buffer
*buffer
, const char *format
, va_list args
)
102 unsigned nesting
= 0;
110 const char *start
= format
- 1;
111 size_t length
= 1 + strcspn(format
, "()% \t");
113 = format_string(buffer
, length
, (const uint8_t *) start
);
117 done
+= output_length
;
118 format
= start
+ length
;
131 if (buffer
&& !NETTLE_BUFFER_PUTC(buffer
, '('))
140 if (buffer
&& !NETTLE_BUFFER_PUTC(buffer
, ')'))
163 /* Allow unbalanced parenthesis */
164 if (buffer
&& !NETTLE_BUFFER_PUTC(buffer
, format
[-1]))
173 size_t output_length
;
177 s
= va_arg(args
, const uint8_t *);
178 length
= strlen_u8(s
);
182 length
= va_arg(args
, size_t);
183 s
= va_arg(args
, const uint8_t *);
186 output_length
= format_string(buffer
, length
, s
);
190 done
+= output_length
;
197 size_t output_length
;
201 s
= va_arg(args
, const uint8_t *);
205 length
= strlen_u8(s
);
209 length
= va_arg(args
, size_t);
210 s
= va_arg(args
, const uint8_t *);
215 if (buffer
&& !NETTLE_BUFFER_PUTC(buffer
, '['))
219 output_length
= format_string(buffer
, length
, s
);
224 done
+= output_length
;
226 if (buffer
&& !NETTLE_BUFFER_PUTC(buffer
, ']'))
240 s
= va_arg(args
, const uint8_t *);
241 length
= strlen_u8(s
);
245 length
= va_arg(args
, size_t);
246 s
= va_arg(args
, const uint8_t *);
249 if (buffer
&& !nettle_buffer_write(buffer
, length
, s
))
257 uint32_t x
= va_arg(args
, uint32_t);
262 else if (x
< 0x8000L
)
264 else if (x
< 0x800000L
)
266 else if (x
< 0x80000000L
)
271 if (buffer
&& !(NETTLE_BUFFER_PUTC(buffer
, '0' + length
)
272 && NETTLE_BUFFER_PUTC(buffer
, ':')))
275 done
+= (2 + length
);
281 /* Leading byte needed for the sign. */
282 if (!NETTLE_BUFFER_PUTC(buffer
, 0))
286 if (!NETTLE_BUFFER_PUTC(buffer
, x
>> 24))
290 if (!NETTLE_BUFFER_PUTC(buffer
, (x
>> 16) & 0xff))
294 if (!NETTLE_BUFFER_PUTC(buffer
, (x
>> 8) & 0xff))
298 if (!NETTLE_BUFFER_PUTC(buffer
, x
& 0xff))
308 mpz_srcptr n
= va_arg(args
, mpz_srcptr
);
310 unsigned prefix_length
;
312 length
= nettle_mpz_sizeinbase_256_s(n
);
313 prefix_length
= format_prefix(buffer
, length
);
317 done
+= prefix_length
;
321 uint8_t *space
= nettle_buffer_space(buffer
, length
);
325 nettle_mpz_get_str_256(length
, space
, n
);
338 sexp_format(struct nettle_buffer
*buffer
, const char *format
, ...)
343 va_start(args
, format
);
344 done
= sexp_vformat(buffer
, format
, args
);