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., 59 Temple Place - Suite 330, Boston,
26
#ifndef NETTLE_SEXP_H_INCLUDED
27
#define NETTLE_SEXP_H_INCLUDED
30
#include "nettle-types.h"
33
#define sexp_iterator_first nettle_sexp_iterator_first
34
#define sexp_transport_iterator_first nettle_sexp_transport_iterator_first
35
#define sexp_iterator_next nettle_sexp_iterator_next
36
#define sexp_iterator_enter_list nettle_sexp_iterator_enter_list
37
#define sexp_iterator_exit_list nettle_sexp_iterator_exit_list
38
#define sexp_iterator_subexpr nettle_sexp_iterator_subexpr
39
#define sexp_iterator_get_uint32 nettle_sexp_iterator_get_uint32
40
#define sexp_iterator_check_type nettle_sexp_iterator_check_type
41
#define sexp_iterator_check_types nettle_sexp_iterator_check_types
42
#define sexp_iterator_assoc nettle_sexp_iterator_assoc
43
#define sexp_format nettle_sexp_format
44
#define sexp_vformat nettle_sexp_vformat
45
#define sexp_transport_format nettle_sexp_transport_format
46
#define sexp_transport_vformat nettle_sexp_transport_vformat
47
#define sexp_token_chars nettle_sexp_token_chars
50
{ SEXP_ATOM, SEXP_LIST, SEXP_END };
55
const uint8_t *buffer;
57
/* Points at the start of the current sub expression. */
59
/* If type is SEXP_LIST, pos points at the start of the current
60
* element. Otherwise, it points at the end. */
66
unsigned display_length;
67
const uint8_t *display;
74
/* All these functions return 1 on success, 0 on failure */
76
/* Initializes the iterator. */
78
sexp_iterator_first(struct sexp_iterator *iterator,
79
unsigned length, const uint8_t *input);
81
/* NOTE: Decodes the input string in place */
83
sexp_transport_iterator_first(struct sexp_iterator *iterator,
84
unsigned length, uint8_t *input);
87
sexp_iterator_next(struct sexp_iterator *iterator);
89
/* Current element must be a list. */
91
sexp_iterator_enter_list(struct sexp_iterator *iterator);
93
/* Skips the rest of the current list */
95
sexp_iterator_exit_list(struct sexp_iterator *iterator);
98
/* Skips out of as many lists as necessary to get back to the given
101
sexp_iterator_exit_lists(struct sexp_iterator *iterator,
105
/* Gets start and length of the current subexpression. Implies
106
* sexp_iterator_next. */
108
sexp_iterator_subexpr(struct sexp_iterator *iterator,
112
sexp_iterator_get_uint32(struct sexp_iterator *iterator,
116
/* Checks the type of the current expression, which should be a list
121
sexp_iterator_check_type(struct sexp_iterator *iterator,
122
const uint8_t *type);
125
sexp_iterator_check_types(struct sexp_iterator *iterator,
127
const uint8_t **types);
129
/* Current element must be a list. Looks up element of type
133
* For a matching key, the corresponding iterator is initialized
134
* pointing at the start of REST.
136
* On success, exits the current list.
139
sexp_iterator_assoc(struct sexp_iterator *iterator,
141
const uint8_t **keys,
142
struct sexp_iterator *values);
145
/* Output functions. What is a reasonable API for this? It seems
146
* ugly to have to reimplement string streams. */
148
/* Declared for real in buffer.h */
149
struct nettle_buffer;
151
/* Returns the number of output characters, or 0 on out of memory. If
152
* buffer == NULL, just compute length.
154
* Format strings can contained matched parentheses, tokens ("foo" in
155
* the format string is formatted as "3:foo"), whitespace (which
156
* separates tokens but is otherwise ignored) and the following
157
* formatting specifiers:
159
* %s String represented as unsigned length, const uint8_t *data.
161
* %t Optional display type, represented as
162
* unsigned display_length, const uint8_t *display,
163
* display == NULL means no display type.
165
* %i Non-negative small integer, uint32_t.
167
* %b Non-negative bignum, mpz_t.
169
* %l Literal string (no length added), typically a balanced
170
* subexpression. Represented as unsigned length, const uint8_t
173
* %(, %) Allows insertion of unbalanced parenthesis.
177
* %0 For %s, %t and %l, says that there's no length argument,
178
* instead the string is NUL-terminated, and there's only one
179
* const uint8_t * argument.
183
sexp_format(struct nettle_buffer *buffer,
184
const char *format, ...);
187
sexp_vformat(struct nettle_buffer *buffer,
188
const char *format, va_list args);
190
/* FIXME: Add argument LINE_WIDTH. If non-zero, break lines to at most
194
sexp_transport_format(struct nettle_buffer *buffer,
195
const char *format, ...);
198
sexp_transport_vformat(struct nettle_buffer *buffer,
199
const char *format, va_list args);
201
/* Classification for advanced syntax. */
203
sexp_token_chars[0x80];
205
#define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
207
#endif /* NETTLE_SEXP_H_INCLUDED */