3 * $Id: key-fetch.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
5 * Higher-level key unpacking
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/dstr.h>
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @key_fetchinit@ --- *
41 * Arguments: @const key_fetchdef *kf@ = pointer to base definition
42 * @key_packstruct *kps@ = pointer to destination packing def
43 * @void *p@ = pointer to destination block
45 * Returns: Pointer to packing definition.
47 * Use: Initializes a packing definition (@key_packdef@ structure).
48 * If @kps@ is null on entry, an appropriately sized block is
49 * allocated automatically. Otherwise it must be large enough.
52 static size_t kfcount(const key_fetchdef
*kf
)
64 key_packdef
*key_fetchinit(const key_fetchdef
*kf
,
65 key_packstruct
*kp
, void *p
)
67 size_t n
= 1 + kfcount(kf
);
72 /* --- If @kps@ is null, count the entries and allocate --- */
77 kp
= xmalloc(n
* sizeof(*kp
));
78 kp
->name
= (char *)kp
;
81 /* --- Fill in the top part --- */
83 kp
->kp
.kd
.e
= KENC_STRUCT
;
87 /* --- Initialize for the main loop --- */
93 /* --- Iterate over the entries in the table --- *
95 * The end of the target block is used as a stack to record where
96 * substructure is meant to occur. The integer @n@ is the depth of the
97 * stack; @kps@ is a full descending stack pointer. The @kp.p@ member of a
98 * stack element points back to an entry with substructure, the @kp.p@
99 * member of which refers to the @kf@ table for the substructure.
101 * This should all be about as clear as mud.
106 /* --- Blat out a level's worth --- */
111 if ((kf
->e
& KF_ENCMASK
) != KENC_STRUCT
)
112 kp
->kp
.p
= cp
+ kf
->off
;
115 kp
->kp
.p
= (void *)kf
->kf
;
125 /* --- Pop an entry from the stack --- */
128 key_packstruct
*kkp
= (kps
++)->kp
.p
;
135 /* --- We're done --- */
140 /* --- @key_fetch@ --- *
142 * Arguments: @key_packdef *kp@ = pointer to packing structure
143 * @key *k@ = key file containing desired key
145 * Returns: Error code, or zero.
147 * Use: Fetches an unpacked key from a packed one.
150 int key_fetch(key_packdef
*kp
, key
*k
)
156 e
= key_unpack(kp
, &k
->k
, &d
);
161 /* --- @key_fetchbyname@ --- *
163 * Arguments: @key_packdef *kp@ = pointer to packing structure
164 * @key_file *kf@ = key file containing desired key
165 * @const char *tag@ = user's tag describing the key
167 * Returns: Error code, or zero.
169 * Use: Fetches a named key from a key file and unpacks it
173 int key_fetchbyname(key_packdef
*kp
, key_file
*kf
, const char *tag
)
179 if (key_qtag(kf
, tag
, &d
, 0, &kd
))
182 e
= key_unpack(kp
, kd
, &d
);
187 /* --- @key_fetchdone@ --- *
189 * Arguments: @key_packdef *kp@ = pointer to packing structure
193 * Use: Frees a packing structure. If the structure was allocated by
194 * @key_fetchinit@ then it is freed.
197 void key_fetchdone(key_packdef
*kp
)
199 key_packstruct
*kps
= (key_packstruct
*)(((char *)kp
) -
200 offsetof(key_packstruct
, kp
));
206 /*----- That's all, folks -------------------------------------------------*/