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
54 { SEXP_ATOM
, SEXP_LIST
, SEXP_END
};
59 const uint8_t *buffer
;
61 /* Points at the start of the current sub expression. */
63 /* If type is SEXP_LIST, pos points at the start of the current
64 * element. Otherwise, it points at the end. */
70 unsigned display_length
;
71 const uint8_t *display
;
78 /* All these functions return 1 on success, 0 on failure */
80 /* Initializes the iterator. */
82 sexp_iterator_first(struct sexp_iterator
*iterator
,
83 unsigned length
, const uint8_t *input
);
85 /* NOTE: Decodes the input string in place */
87 sexp_transport_iterator_first(struct sexp_iterator
*iterator
,
88 unsigned length
, uint8_t *input
);
91 sexp_iterator_next(struct sexp_iterator
*iterator
);
93 /* Current element must be a list. */
95 sexp_iterator_enter_list(struct sexp_iterator
*iterator
);
97 /* Skips the rest of the current list */
99 sexp_iterator_exit_list(struct sexp_iterator
*iterator
);
102 /* Skips out of as many lists as necessary to get back to the given
105 sexp_iterator_exit_lists(struct sexp_iterator
*iterator
,
109 /* Gets start and length of the current subexpression. Implies
110 * sexp_iterator_next. */
112 sexp_iterator_subexpr(struct sexp_iterator
*iterator
,
116 sexp_iterator_get_uint32(struct sexp_iterator
*iterator
,
120 /* Checks the type of the current expression, which should be a list
125 sexp_iterator_check_type(struct sexp_iterator
*iterator
,
126 const uint8_t *type
);
129 sexp_iterator_check_types(struct sexp_iterator
*iterator
,
131 const uint8_t * const *types
);
133 /* Current element must be a list. Looks up element of type
137 * For a matching key, the corresponding iterator is initialized
138 * pointing at the start of REST.
140 * On success, exits the current list.
143 sexp_iterator_assoc(struct sexp_iterator
*iterator
,
145 const uint8_t * const *keys
,
146 struct sexp_iterator
*values
);
149 /* Output functions. What is a reasonable API for this? It seems
150 * ugly to have to reimplement string streams. */
152 /* Declared for real in buffer.h */
153 struct nettle_buffer
;
155 /* Returns the number of output characters, or 0 on out of memory. If
156 * buffer == NULL, just compute length.
158 * Format strings can contained matched parentheses, tokens ("foo" in
159 * the format string is formatted as "3:foo"), whitespace (which
160 * separates tokens but is otherwise ignored) and the following
161 * formatting specifiers:
163 * %s String represented as unsigned length, const uint8_t *data.
165 * %t Optional display type, represented as
166 * unsigned display_length, const uint8_t *display,
167 * display == NULL means no display type.
169 * %i Non-negative small integer, uint32_t.
171 * %b Non-negative bignum, mpz_t.
173 * %l Literal string (no length added), typically a balanced
174 * subexpression. Represented as unsigned length, const uint8_t
177 * %(, %) Allows insertion of unbalanced parenthesis.
181 * %0 For %s, %t and %l, says that there's no length argument,
182 * instead the string is NUL-terminated, and there's only one
183 * const uint8_t * argument.
187 sexp_format(struct nettle_buffer
*buffer
,
188 const char *format
, ...);
191 sexp_vformat(struct nettle_buffer
*buffer
,
192 const char *format
, va_list args
);
195 sexp_transport_format(struct nettle_buffer
*buffer
,
196 const char *format
, ...);
199 sexp_transport_vformat(struct nettle_buffer
*buffer
,
200 const char *format
, va_list args
);
202 /* Classification for advanced syntax. */
204 sexp_token_chars
[0x80];
206 #define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
212 #endif /* NETTLE_SEXP_H_INCLUDED */