~ubuntu-branches/ubuntu/quantal/nettle/quantal

« back to all changes in this revision

Viewing changes to sexp.h

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place - Suite 330, Boston,
 
23
 * MA 02111-1307, 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
/* Name mangling */
 
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
 
48
 
 
49
enum sexp_type
 
50
  { SEXP_ATOM, SEXP_LIST, SEXP_END };
 
51
 
 
52
struct sexp_iterator
 
53
{
 
54
  unsigned length;
 
55
  const uint8_t *buffer;
 
56
 
 
57
  /* Points at the start of the current sub expression. */
 
58
  unsigned start;
 
59
  /* If type is SEXP_LIST, pos points at the start of the current
 
60
   * element. Otherwise, it points at the end. */
 
61
  unsigned pos;
 
62
  unsigned level;
 
63
 
 
64
  enum sexp_type type;
 
65
  
 
66
  unsigned display_length;
 
67
  const uint8_t *display;
 
68
 
 
69
  unsigned atom_length;
 
70
  const uint8_t *atom;
 
71
};
 
72
 
 
73
 
 
74
/* All these functions return 1 on success, 0 on failure */
 
75
 
 
76
/* Initializes the iterator. */
 
77
int
 
78
sexp_iterator_first(struct sexp_iterator *iterator,
 
79
                    unsigned length, const uint8_t *input);
 
80
 
 
81
/* NOTE: Decodes the input string in place */
 
82
int
 
83
sexp_transport_iterator_first(struct sexp_iterator *iterator,
 
84
                              unsigned length, uint8_t *input);
 
85
 
 
86
int
 
87
sexp_iterator_next(struct sexp_iterator *iterator);
 
88
 
 
89
/* Current element must be a list. */
 
90
int
 
91
sexp_iterator_enter_list(struct sexp_iterator *iterator);
 
92
 
 
93
/* Skips the rest of the current list */
 
94
int
 
95
sexp_iterator_exit_list(struct sexp_iterator *iterator);
 
96
 
 
97
#if 0
 
98
/* Skips out of as many lists as necessary to get back to the given
 
99
 * level. */
 
100
int
 
101
sexp_iterator_exit_lists(struct sexp_iterator *iterator,
 
102
                         unsigned level);
 
103
#endif
 
104
 
 
105
/* Gets start and length of the current subexpression. Implies
 
106
 * sexp_iterator_next. */
 
107
const uint8_t *
 
108
sexp_iterator_subexpr(struct sexp_iterator *iterator,
 
109
                      unsigned *length);
 
110
 
 
111
int
 
112
sexp_iterator_get_uint32(struct sexp_iterator *iterator,
 
113
                         uint32_t *x);
 
114
 
 
115
 
 
116
/* Checks the type of the current expression, which should be a list
 
117
 *
 
118
 *  (<type> ...)
 
119
 */
 
120
int
 
121
sexp_iterator_check_type(struct sexp_iterator *iterator,
 
122
                         const uint8_t *type);
 
123
 
 
124
const uint8_t *
 
125
sexp_iterator_check_types(struct sexp_iterator *iterator,
 
126
                          unsigned ntypes,
 
127
                          const uint8_t **types);
 
128
 
 
129
/* Current element must be a list. Looks up element of type
 
130
 *
 
131
 *   (key rest...)
 
132
 *
 
133
 * For a matching key, the corresponding iterator is initialized
 
134
 * pointing at the start of REST.
 
135
 *
 
136
 * On success, exits the current list.
 
137
 */
 
138
int
 
139
sexp_iterator_assoc(struct sexp_iterator *iterator,
 
140
                    unsigned nkeys,
 
141
                    const uint8_t **keys,
 
142
                    struct sexp_iterator *values);
 
143
 
 
144
 
 
145
/* Output functions. What is a reasonable API for this? It seems
 
146
 * ugly to have to reimplement string streams. */
 
147
 
 
148
/* Declared for real in buffer.h */
 
149
struct nettle_buffer;
 
150
 
 
151
/* Returns the number of output characters, or 0 on out of memory. If
 
152
 * buffer == NULL, just compute length.
 
153
 *
 
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:
 
158
 *
 
159
 *   %s   String represented as unsigned length, const uint8_t *data.
 
160
 *
 
161
 *   %t   Optional display type, represented as
 
162
 *        unsigned display_length, const uint8_t *display,
 
163
 *        display == NULL means no display type.
 
164
 *
 
165
 *   %i   Non-negative small integer, uint32_t.
 
166
 *
 
167
 *   %b   Non-negative bignum, mpz_t.
 
168
 *
 
169
 *   %l   Literal string (no length added), typically a balanced
 
170
 *        subexpression. Represented as unsigned length, const uint8_t
 
171
 *        *data.
 
172
 *
 
173
 *   %(, %)  Allows insertion of unbalanced parenthesis.
 
174
 *
 
175
 * Modifiers:
 
176
 *
 
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.
 
180
 */
 
181
 
 
182
unsigned
 
183
sexp_format(struct nettle_buffer *buffer,
 
184
            const char *format, ...);
 
185
 
 
186
unsigned
 
187
sexp_vformat(struct nettle_buffer *buffer,
 
188
             const char *format, va_list args);
 
189
 
 
190
/* FIXME: Add argument LINE_WIDTH. If non-zero, break lines to at most
 
191
 * that width. */
 
192
 
 
193
unsigned
 
194
sexp_transport_format(struct nettle_buffer *buffer,
 
195
                      const char *format, ...);
 
196
 
 
197
unsigned
 
198
sexp_transport_vformat(struct nettle_buffer *buffer,
 
199
                       const char *format, va_list args);
 
200
 
 
201
/* Classification for advanced syntax. */
 
202
extern const char
 
203
sexp_token_chars[0x80];
 
204
 
 
205
#define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
 
206
 
 
207
#endif /* NETTLE_SEXP_H_INCLUDED */