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