chiark / gitweb /
Merge branch '2.4.x' into 2.5.x
[catacomb] / math / group-parse.c
CommitLineData
34e4f738 1/* -*-c-*-
34e4f738 2 *
3 * Parse group description strings
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
34e4f738 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
45c0fd36 16 *
34e4f738 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
45c0fd36 21 *
34e4f738 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
34e4f738 27/*----- Header files ------------------------------------------------------*/
28
29#include "group.h"
30#include "dh.h"
31
32/*----- Main code ---------------------------------------------------------*/
33
34/* --- @group_parse@ --- *
35 *
36 * Arguments: @qd_parse *qd@ = quick-and-dirty parser
37 *
38 * Returns: Group pointer, or null for failure.
39 *
40 * Use: Parses a group description and returns the group. This has
41 * the form `TYPE { SPEC }' where TYPE is `prime' or `ec', and
42 * SPEC is the appropriate kind of group specification of the
43 * given type.
44 */
45
46group *group_parse(qd_parse *qd)
47{
48 group *g = 0;
49
50 switch (qd_enum(qd, "prime,ec")) {
51 case 0: {
52 dh_param dp;
53 qd_delim(qd, '{');
f923fb18 54 if (dh_parse(qd, &dp)) goto ouch;
34e4f738 55 qd_delim(qd, '}');
56 g = group_prime(&dp);
57 dh_paramfree(&dp);
58 } break;
59 case 1: {
60 ec_info ei;
61 qd_delim(qd, '{');
f923fb18 62 if (ec_infoparse(qd, &ei)) goto ouch;
34e4f738 63 qd_delim(qd, '}');
64 g = group_ec(&ei);
65 } break;
66 }
02d7884d 67 if (!g) qd->e = "bad group parameters";
f923fb18 68ouch:
34e4f738 69 return (g);
70}
71
72/* --- @group_fromstring@ --- *
73 *
74 * Arguments: @const char *p@ = pointer to string to read
75 * @group **gg@ = where to put the group pointer
76 *
77 * Returns: Null if OK, or an error string.
78 *
79 * Use: Parses a group spec from a string, and returns the group.
80 */
81
82const char *group_fromstring(const char *p, group **gg)
83{
84 group *g;
85 qd_parse qd;
86
87 qd.p = p;
88 qd.e = 0;
89 if ((g = group_parse(&qd)) == 0) return (qd.e);
90 if (!qd_eofp(&qd)) { G_DESTROYGROUP(g); return ("junk at end of string"); }
91 *gg = g;
92 return (0);
93}
94
95/*----- That's all, folks -------------------------------------------------*/