3 * Parsing s-expressions.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 #ifndef NETTLE_SEXP_H_INCLUDED
27 #define NETTLE_SEXP_H_INCLUDED
30 #include "nettle-types.h"
37 #define sexp_iterator_first nettle_sexp_iterator_first
38 #define sexp_transport_iterator_first nettle_sexp_transport_iterator_first
39 #define sexp_iterator_next nettle_sexp_iterator_next
40 #define sexp_iterator_enter_list nettle_sexp_iterator_enter_list
41 #define sexp_iterator_exit_list nettle_sexp_iterator_exit_list
42 #define sexp_iterator_subexpr nettle_sexp_iterator_subexpr
43 #define sexp_iterator_get_uint32 nettle_sexp_iterator_get_uint32
44 #define sexp_iterator_check_type nettle_sexp_iterator_check_type
45 #define sexp_iterator_check_types nettle_sexp_iterator_check_types
46 #define sexp_iterator_assoc nettle_sexp_iterator_assoc
47 #define sexp_format nettle_sexp_format
48 #define sexp_vformat nettle_sexp_vformat
49 #define sexp_transport_format nettle_sexp_transport_format
50 #define sexp_transport_vformat nettle_sexp_transport_vformat
51 #define sexp_token_chars nettle_sexp_token_chars
53 enum sexp_type
{ SEXP_ATOM
, SEXP_LIST
, SEXP_END
};
55 struct sexp_iterator
{
57 const uint8_t *buffer
;
59 /* Points at the start of the current sub expression. */
61 /* If type is SEXP_LIST, pos points at the start of the current
62 * element. Otherwise, it points at the end. */
68 unsigned display_length
;
69 const uint8_t *display
;
76 /* All these functions return 1 on success, 0 on failure */
78 /* Initializes the iterator. */
80 sexp_iterator_first(struct sexp_iterator
*iterator
,
81 unsigned length
, const uint8_t * input
);
83 /* NOTE: Decodes the input string in place */
85 sexp_transport_iterator_first(struct sexp_iterator
*iterator
,
86 unsigned length
, uint8_t * input
);
89 sexp_iterator_next(struct sexp_iterator
*iterator
);
91 /* Current element must be a list. */
93 sexp_iterator_enter_list(struct sexp_iterator
*iterator
);
95 /* Skips the rest of the current list */
97 sexp_iterator_exit_list(struct sexp_iterator
*iterator
);
100 /* Skips out of as many lists as necessary to get back to the given
103 sexp_iterator_exit_lists(struct sexp_iterator
*iterator
,
107 /* Gets start and length of the current subexpression. Implies
108 * sexp_iterator_next. */
109 const uint8_t *sexp_iterator_subexpr(struct sexp_iterator
110 *iterator
, unsigned *length
);
113 sexp_iterator_get_uint32(struct sexp_iterator
*iterator
,
117 /* Checks the type of the current expression, which should be a list
122 sexp_iterator_check_type(struct sexp_iterator
*iterator
,
123 const uint8_t * type
);
125 const uint8_t *sexp_iterator_check_types(struct sexp_iterator
131 /* Current element must be a list. Looks up element of type
135 * For a matching key, the corresponding iterator is initialized
136 * pointing at the start of REST.
138 * On success, exits the current list.
141 sexp_iterator_assoc(struct sexp_iterator
*iterator
,
143 const uint8_t * const *keys
,
144 struct sexp_iterator
*values
);
147 /* Output functions. What is a reasonable API for this? It seems
148 * ugly to have to reimplement string streams. */
150 /* Declared for real in buffer.h */
151 struct nettle_buffer
;
153 /* Returns the number of output characters, or 0 on out of memory. If
154 * buffer == NULL, just compute length.
156 * Format strings can contained matched parentheses, tokens ("foo" in
157 * the format string is formatted as "3:foo"), whitespace (which
158 * separates tokens but is otherwise ignored) and the following
159 * formatting specifiers:
161 * %s String represented as unsigned length, const uint8_t *data.
163 * %t Optional display type, represented as
164 * unsigned display_length, const uint8_t *display,
165 * display == NULL means no display type.
167 * %i Non-negative small integer, uint32_t.
169 * %b Non-negative bignum, mpz_t.
171 * %l Literal string (no length added), typically a balanced
172 * subexpression. Represented as unsigned length, const uint8_t
175 * %(, %) Allows insertion of unbalanced parenthesis.
179 * %0 For %s, %t and %l, says that there's no length argument,
180 * instead the string is NUL-terminated, and there's only one
181 * const uint8_t * argument.
185 sexp_format(struct nettle_buffer
*buffer
,
186 const char *format
, ...);
189 sexp_vformat(struct nettle_buffer
*buffer
,
190 const char *format
, va_list args
);
193 sexp_transport_format(struct nettle_buffer
*buffer
,
194 const char *format
, ...);
197 sexp_transport_vformat(struct nettle_buffer
*buffer
,
198 const char *format
, va_list args
);
200 /* Classification for advanced syntax. */
202 sexp_token_chars
[0x80];
204 #define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
209 #endif /* NETTLE_SEXP_H_INCLUDED */