Sync with trunk revision 64099.
[reactos.git] / include / reactos / libs / gnutls / nettle / sexp.h
1 /* sexp.h
2 *
3 * Parsing s-expressions.
4 */
5
6 /* nettle, low-level cryptographics library
7 *
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.
14 *
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.
19 *
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.
24 */
25
26 #ifndef NETTLE_SEXP_H_INCLUDED
27 #define NETTLE_SEXP_H_INCLUDED
28
29 #include <stdarg.h>
30 #include "nettle-types.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
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
52
53 enum sexp_type { SEXP_ATOM, SEXP_LIST, SEXP_END };
54
55 struct sexp_iterator {
56 unsigned length;
57 const uint8_t *buffer;
58
59 /* Points at the start of the current sub expression. */
60 unsigned start;
61 /* If type is SEXP_LIST, pos points at the start of the current
62 * element. Otherwise, it points at the end. */
63 unsigned pos;
64 unsigned level;
65
66 enum sexp_type type;
67
68 unsigned display_length;
69 const uint8_t *display;
70
71 unsigned atom_length;
72 const uint8_t *atom;
73 };
74
75
76 /* All these functions return 1 on success, 0 on failure */
77
78 /* Initializes the iterator. */
79 int
80 sexp_iterator_first(struct sexp_iterator *iterator,
81 unsigned length, const uint8_t * input);
82
83 /* NOTE: Decodes the input string in place */
84 int
85 sexp_transport_iterator_first(struct sexp_iterator *iterator,
86 unsigned length, uint8_t * input);
87
88 int
89 sexp_iterator_next(struct sexp_iterator *iterator);
90
91 /* Current element must be a list. */
92 int
93 sexp_iterator_enter_list(struct sexp_iterator *iterator);
94
95 /* Skips the rest of the current list */
96 int
97 sexp_iterator_exit_list(struct sexp_iterator *iterator);
98
99 #if 0
100 /* Skips out of as many lists as necessary to get back to the given
101 * level. */
102 int
103 sexp_iterator_exit_lists(struct sexp_iterator *iterator,
104 unsigned level);
105 #endif
106
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);
111
112 int
113 sexp_iterator_get_uint32(struct sexp_iterator *iterator,
114 uint32_t * x);
115 \f
116
117 /* Checks the type of the current expression, which should be a list
118 *
119 * (<type> ...)
120 */
121 int
122 sexp_iterator_check_type(struct sexp_iterator *iterator,
123 const uint8_t * type);
124
125 const uint8_t *sexp_iterator_check_types(struct sexp_iterator
126 *iterator,
127 unsigned ntypes,
128 const uint8_t *
129 const *types);
130
131 /* Current element must be a list. Looks up element of type
132 *
133 * (key rest...)
134 *
135 * For a matching key, the corresponding iterator is initialized
136 * pointing at the start of REST.
137 *
138 * On success, exits the current list.
139 */
140 int
141 sexp_iterator_assoc(struct sexp_iterator *iterator,
142 unsigned nkeys,
143 const uint8_t * const *keys,
144 struct sexp_iterator *values);
145 \f
146
147 /* Output functions. What is a reasonable API for this? It seems
148 * ugly to have to reimplement string streams. */
149
150 /* Declared for real in buffer.h */
151 struct nettle_buffer;
152
153 /* Returns the number of output characters, or 0 on out of memory. If
154 * buffer == NULL, just compute length.
155 *
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:
160 *
161 * %s String represented as unsigned length, const uint8_t *data.
162 *
163 * %t Optional display type, represented as
164 * unsigned display_length, const uint8_t *display,
165 * display == NULL means no display type.
166 *
167 * %i Non-negative small integer, uint32_t.
168 *
169 * %b Non-negative bignum, mpz_t.
170 *
171 * %l Literal string (no length added), typically a balanced
172 * subexpression. Represented as unsigned length, const uint8_t
173 * *data.
174 *
175 * %(, %) Allows insertion of unbalanced parenthesis.
176 *
177 * Modifiers:
178 *
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.
182 */
183
184 unsigned
185 sexp_format(struct nettle_buffer *buffer,
186 const char *format, ...);
187
188 unsigned
189 sexp_vformat(struct nettle_buffer *buffer,
190 const char *format, va_list args);
191
192 unsigned
193 sexp_transport_format(struct nettle_buffer *buffer,
194 const char *format, ...);
195
196 unsigned
197 sexp_transport_vformat(struct nettle_buffer *buffer,
198 const char *format, va_list args);
199
200 /* Classification for advanced syntax. */
201 extern const char
202 sexp_token_chars[0x80];
203
204 #define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
205
206 #ifdef __cplusplus
207 }
208 #endif
209 #endif /* NETTLE_SEXP_H_INCLUDED */