3 /* nettle, low-level cryptographics library
5 * Copyright (C) 2002, 2003 Niels Möller
7 * The nettle library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at your
10 * option) any later version.
12 * The nettle library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the nettle library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
35 sexp_compound_token_init(struct sexp_compound_token *token)
38 nettle_buffer_init(&token->display);
39 nettle_buffer_init(&token->string);
43 sexp_compound_token_clear(struct sexp_compound_token *token)
45 nettle_buffer_clear(&token->display);
46 nettle_buffer_clear(&token->string);
50 sexp_parse_init(struct sexp_parser *parser,
51 struct sexp_input *input,
54 parser->input = input;
57 /* Start counting with 1 for the top level, to make comparisons
58 * between transport and level simpler.
60 * FIXME: Is that trick ugly? */
62 parser->transport = 0;
65 /* Get next token, and check that it is of the expected kind. */
67 sexp_check_token(struct sexp_parser *parser,
68 enum sexp_token token,
69 struct nettle_buffer *string)
71 sexp_get_token(parser->input,
72 parser->transport ? SEXP_CANONICAL : parser->mode,
75 if (parser->input->token != token)
76 die("Syntax error.\n");
79 /* Performs further processing of the input, in particular display
80 * types and transport decoding.
82 * This is complicated a little by the requirement that a
83 * transport-encoded block, {xxxxx}, must include exactly one
84 * expression. We check at the end of strings and list whether or not
85 * we should expect a SEXP_CODING_END as the next token. */
87 sexp_parse(struct sexp_parser *parser,
88 struct sexp_compound_token *token)
92 sexp_get_token(parser->input,
93 parser->transport ? SEXP_CANONICAL : parser->mode,
96 switch(parser->input->token)
99 if (parser->level == parser->transport)
100 die("Unmatched end of list in transport encoded data.\n");
104 die("Unmatched end of list.\n");
106 token->type = SEXP_LIST_END;
109 if (parser->level == parser->transport)
111 sexp_check_token(parser, SEXP_CODING_END, &token->string);
112 assert(parser->transport);
113 assert(parser->level == parser->transport);
116 parser->transport = 0;
121 if (parser->level > 1)
122 die("Unexpected end of file.\n");
124 token->type = SEXP_EOF;
127 case SEXP_LIST_START:
129 token->type = SEXP_LIST_START;
132 case SEXP_DISPLAY_START:
133 sexp_check_token(parser, SEXP_STRING, &token->display);
134 sexp_check_token(parser, SEXP_DISPLAY_END, &token->display);
135 sexp_check_token(parser, SEXP_STRING, &token->string);
137 token->type = SEXP_DISPLAY;
138 goto check_transport_end;
141 token->type = SEXP_STRING;
142 goto check_transport_end;
145 token->type = SEXP_COMMENT;
148 case SEXP_TRANSPORT_START:
149 if (parser->mode == SEXP_CANONICAL)
150 die("Base64 not allowed in canonical mode.\n");
152 parser->transport = parser->level;
156 case SEXP_CODING_END:
157 die("Unexpected end of transport encoding.\n");
160 /* Internal error. */