Samba Patch - Denial of service - CPU loop and memory allocation.
[tomato.git] / release / src / router / nettle / sexp.h
blob7b68358d3653bc3b201b0f14a50bba1be878ea51
1 /* sexp.h
3 * Parsing s-expressions.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 Niels Möller
9 *
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,
23 * MA 02111-1301, USA.
26 #ifndef NETTLE_SEXP_H_INCLUDED
27 #define NETTLE_SEXP_H_INCLUDED
29 #include <stdarg.h>
30 #include "nettle-types.h"
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
36 /* Name mangling */
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
54 { SEXP_ATOM, SEXP_LIST, SEXP_END };
56 struct sexp_iterator
58 unsigned length;
59 const uint8_t *buffer;
61 /* Points at the start of the current sub expression. */
62 unsigned start;
63 /* If type is SEXP_LIST, pos points at the start of the current
64 * element. Otherwise, it points at the end. */
65 unsigned pos;
66 unsigned level;
68 enum sexp_type type;
70 unsigned display_length;
71 const uint8_t *display;
73 unsigned atom_length;
74 const uint8_t *atom;
78 /* All these functions return 1 on success, 0 on failure */
80 /* Initializes the iterator. */
81 int
82 sexp_iterator_first(struct sexp_iterator *iterator,
83 unsigned length, const uint8_t *input);
85 /* NOTE: Decodes the input string in place */
86 int
87 sexp_transport_iterator_first(struct sexp_iterator *iterator,
88 unsigned length, uint8_t *input);
90 int
91 sexp_iterator_next(struct sexp_iterator *iterator);
93 /* Current element must be a list. */
94 int
95 sexp_iterator_enter_list(struct sexp_iterator *iterator);
97 /* Skips the rest of the current list */
98 int
99 sexp_iterator_exit_list(struct sexp_iterator *iterator);
101 #if 0
102 /* Skips out of as many lists as necessary to get back to the given
103 * level. */
105 sexp_iterator_exit_lists(struct sexp_iterator *iterator,
106 unsigned level);
107 #endif
109 /* Gets start and length of the current subexpression. Implies
110 * sexp_iterator_next. */
111 const uint8_t *
112 sexp_iterator_subexpr(struct sexp_iterator *iterator,
113 unsigned *length);
116 sexp_iterator_get_uint32(struct sexp_iterator *iterator,
117 uint32_t *x);
120 /* Checks the type of the current expression, which should be a list
122 * (<type> ...)
125 sexp_iterator_check_type(struct sexp_iterator *iterator,
126 const uint8_t *type);
128 const uint8_t *
129 sexp_iterator_check_types(struct sexp_iterator *iterator,
130 unsigned ntypes,
131 const uint8_t * const *types);
133 /* Current element must be a list. Looks up element of type
135 * (key rest...)
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,
144 unsigned nkeys,
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
175 * *data.
177 * %(, %) Allows insertion of unbalanced parenthesis.
179 * Modifiers:
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.
186 unsigned
187 sexp_format(struct nettle_buffer *buffer,
188 const char *format, ...);
190 unsigned
191 sexp_vformat(struct nettle_buffer *buffer,
192 const char *format, va_list args);
194 unsigned
195 sexp_transport_format(struct nettle_buffer *buffer,
196 const char *format, ...);
198 unsigned
199 sexp_transport_vformat(struct nettle_buffer *buffer,
200 const char *format, va_list args);
202 /* Classification for advanced syntax. */
203 extern const char
204 sexp_token_chars[0x80];
206 #define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
208 #ifdef __cplusplus
210 #endif
212 #endif /* NETTLE_SEXP_H_INCLUDED */