3 Copyright (C) 2002, 2003, 2008 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
44 sexp_compound_token_init(struct sexp_compound_token *token)
47 nettle_buffer_init(&token->display);
48 nettle_buffer_init(&token->string);
52 sexp_compound_token_clear(struct sexp_compound_token *token)
54 nettle_buffer_clear(&token->display);
55 nettle_buffer_clear(&token->string);
59 sexp_parse_init(struct sexp_parser *parser,
60 struct sexp_input *input,
63 parser->input = input;
66 /* Start counting with 1 for the top level, to make comparisons
67 * between transport and level simpler.
69 * FIXME: Is that trick ugly? */
71 parser->transport = 0;
74 /* Get next token, and check that it is of the expected kind. */
76 sexp_check_token(struct sexp_parser *parser,
77 enum sexp_token token,
78 struct nettle_buffer *string)
80 sexp_get_token(parser->input,
81 parser->transport ? SEXP_CANONICAL : parser->mode,
84 if (parser->input->token != token)
85 die("Syntax error.\n");
88 /* Performs further processing of the input, in particular display
89 * types and transport decoding.
91 * This is complicated a little by the requirement that a
92 * transport-encoded block, {xxxxx}, must include exactly one
93 * expression. We check at the end of strings and list whether or not
94 * we should expect a SEXP_CODING_END as the next token. */
96 sexp_parse(struct sexp_parser *parser,
97 struct sexp_compound_token *token)
101 sexp_get_token(parser->input,
102 parser->transport ? SEXP_CANONICAL : parser->mode,
105 switch(parser->input->token)
108 if (parser->level == parser->transport)
109 die("Unmatched end of list in transport encoded data.\n");
113 die("Unmatched end of list.\n");
115 token->type = SEXP_LIST_END;
118 if (parser->level == parser->transport)
120 sexp_check_token(parser, SEXP_CODING_END, &token->string);
121 assert(parser->transport);
122 assert(parser->level == parser->transport);
125 parser->transport = 0;
130 if (parser->level > 1)
131 die("Unexpected end of file.\n");
133 token->type = SEXP_EOF;
136 case SEXP_LIST_START:
138 token->type = SEXP_LIST_START;
141 case SEXP_DISPLAY_START:
142 sexp_check_token(parser, SEXP_STRING, &token->display);
143 sexp_check_token(parser, SEXP_DISPLAY_END, &token->display);
144 sexp_check_token(parser, SEXP_STRING, &token->string);
146 token->type = SEXP_DISPLAY;
147 goto check_transport_end;
150 token->type = SEXP_STRING;
151 goto check_transport_end;
154 token->type = SEXP_COMMENT;
157 case SEXP_TRANSPORT_START:
158 if (parser->mode == SEXP_CANONICAL)
159 die("Base64 not allowed in canonical mode.\n");
161 parser->transport = parser->level;
165 case SEXP_CODING_END:
166 die("Unexpected end of transport encoding.\n");
169 /* Internal error. */