chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / math / group-parse.c
1 /* -*-c-*-
2  *
3  * Parse group description strings
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
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.
16  *
17  * Catacomb 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 Library General Public License for more details.
21  *
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.
26  */
27
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
47 group *group_parse(qd_parse *qd)
48 {
49   group *g = 0;
50
51   switch (qd_enum(qd, "prime,bin,ec")) {
52     case 0: {
53       dh_param dp;
54       qd_delim(qd, '{');
55       if (dh_parse(qd, &dp)) goto ouch;
56       qd_delim(qd, '}');
57       g = group_prime(&dp);
58       dh_paramfree(&dp);
59     } break;
60     case 1: {
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: {
69       ec_info ei;
70       qd_delim(qd, '{');
71       if (ec_infoparse(qd, &ei)) goto ouch;
72       qd_delim(qd, '}');
73       g = group_ec(&ei);
74     } break;
75   }
76   if (!g) qd->e = "bad group parameters";
77 ouch:
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
91 const 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 -------------------------------------------------*/