chiark / gitweb /
math/group-parse.c: Fix copyright notice.
[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,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       ec_info ei;
62       qd_delim(qd, '{');
63       if (ec_infoparse(qd, &ei)) goto ouch;
64       qd_delim(qd, '}');
65       g = group_ec(&ei);
66     } break;
67   }
68   if (!g) qd->e = "bad group parameters";
69 ouch:
70   return (g);
71 }
72
73 /* --- @group_fromstring@ --- *
74  *
75  * Arguments:   @const char *p@ = pointer to string to read
76  *              @group **gg@ = where to put the group pointer
77  *
78  * Returns:     Null if OK, or an error string.
79  *
80  * Use:         Parses a group spec from a string, and returns the group.
81  */
82
83 const char *group_fromstring(const char *p, group **gg)
84 {
85   group *g;
86   qd_parse qd;
87
88   qd.p = p;
89   qd.e = 0;
90   if ((g = group_parse(&qd)) == 0) return (qd.e);
91   if (!qd_eofp(&qd)) { G_DESTROYGROUP(g); return ("junk at end of string"); }
92   *gg = g;
93   return (0);
94 }
95
96 /*----- That's all, folks -------------------------------------------------*/