X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/b817bfc642225b8c3c0b6a7e42d1fb949b61a606..813390c45f438f411662b1a55678e63f11681eb4:/key-text.c diff --git a/key-text.c b/key-text.c index dcfe354..ff9e705 100644 --- a/key-text.c +++ b/key-text.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: key-text.c,v 1.6 2004/04/08 01:36:15 mdw Exp $ + * $Id$ * * Key textual encoding * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,12 +15,12 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, @@ -49,17 +49,17 @@ /* --- @key_read@ --- * * * Arguments: @const char *p@ = pointer to textual key representation - * @key_data *k@ = pointer to output block for key data * @char **pp@ = where to store the end pointer * - * Returns: Zero if all went well, nonzero if there was a problem. + * Returns: The newly-read key data, or null if it failed. * * Use: Parses a textual key description. */ -int key_read(const char *p, key_data *k, char **pp) +key_data *key_read(const char *p, char **pp) { unsigned e; + key_data *kd; /* --- Read the encoding type --- * * @@ -72,13 +72,12 @@ int key_read(const char *p, key_data *k, char **pp) else { char *q; if (key_readflags(p, &q, &e, 0)) - return (-1); + return (0); p = q + 1; } /* --- Now scan the data based on the encoding type --- */ - k->e = e; switch (e & KF_ENCMASK) { /* --- Binary encoding --- * @@ -97,9 +96,7 @@ int key_read(const char *p, key_data *k, char **pp) base64_init(&b); base64_decode(&b, p, sz, &d); base64_decode(&b, 0, 0, &d); - k->u.k.k = sub_alloc(d.len); - k->u.k.sz = d.len; - memcpy(k->u.k.k, d.buf, d.len); + kd = key_newbinary(e, d.buf, d.len); dstr_destroy(&d); p += sz; } break; @@ -111,10 +108,11 @@ int key_read(const char *p, key_data *k, char **pp) case KENC_MP: { char *q; - mp *m = mp_readstring(k->e & KF_BURN ? MP_NEWSEC : MP_NEW, p, &q, 0); + mp *m = mp_readstring(e & KF_BURN ? MP_NEWSEC : MP_NEW, p, &q, 0); if (!m) - return (-1); - k->u.m = m; + return (0); + kd = key_newmp(e, m); + MP_DROP(m); p = q; } break; @@ -143,7 +141,7 @@ int key_read(const char *p, key_data *k, char **pp) p++; } DPUTZ(&d); - k->u.p = xstrdup(d.buf); + kd = key_newstring(e, d.buf); dstr_destroy(&d); } break; @@ -154,12 +152,14 @@ int key_read(const char *p, key_data *k, char **pp) */ case KENC_EC: { + ec pt = EC_INIT; qd_parse qd; qd.p = p; qd.e = 0; - EC_CREATE(&k->u.e); - if (!ec_ptparse(&qd, &k->u.e)) - return (-1); + if (!ec_ptparse(&qd, &pt)) + return (0); + kd = key_newec(e, &pt); + EC_DESTROY(&pt); p = qd.p; } break; @@ -175,21 +175,20 @@ int key_read(const char *p, key_data *k, char **pp) case KENC_STRUCT: { dstr d = DSTR_INIT; + key_data *nkd; char *q; /* --- Read the opening bracket --- */ - k->e &= KF_ENCMASK; + kd = key_newstruct(); if (*p != '[') - return (-1); + return (0); p++; - sym_create(&k->u.s); /* --- Read named key subparts --- */ for (;;) { size_t sz; - key_struct *ks; /* --- Stop if there's a close-bracket --- * * @@ -212,24 +211,11 @@ int key_read(const char *p, key_data *k, char **pp) DPUTM(&d, p, sz); DPUTZ(&d); - /* --- Add an appropriate block to the key table --- * - * - * Simply destroy old data if there's already a match. - */ - - { - unsigned f; - ks = sym_find(&k->u.s, d.buf, d.len, sizeof(*ks), &f); - if (f) - key_destroy(&ks->k); - } - /* --- Read the key data for the subkey --- */ - if (key_read(q + 1, &ks->k, &q)) { - sym_remove(&k->u.s, ks); + if ((nkd = key_read(q + 1, &q)) == 0) goto fail; - } + key_structsteal(kd, d.buf, nkd); p = q; /* --- Read the comma or close-bracket --- */ @@ -252,21 +238,21 @@ int key_read(const char *p, key_data *k, char **pp) fail: dstr_destroy(&d); - key_destroy(k); - return (-1); + return (0); } break; /* --- Anything else is unknown --- */ default: - return (-1); + return (0); } /* --- Return the end pointer --- */ + kd->e = e; if (pp) *pp = (char *)p; - return (0); + return (kd); } /* --- @key_write@ --- * @@ -279,7 +265,7 @@ int key_read(const char *p, key_data *k, char **pp) * * Use: Writes a key in a textual encoding. */ - + int key_write(key_data *k, dstr *d, const key_filter *kf) { int rc = 0; @@ -327,19 +313,19 @@ int key_write(key_data *k, dstr *d, const key_filter *kf) rc = 1; break; case KENC_STRUCT: { - sym_iter i; - key_struct *ks; + key_subkeyiter i; + const char *tag; char del = 0; size_t n = d->len; DPUTS(d, "struct:["); - for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) { + for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) { size_t o = d->len; if (del) DPUTC(d, del); - DPUTS(d, SYM_NAME(ks)); + DPUTS(d, tag); DPUTC(d, '='); - if (!key_write(&ks->k, d, kf)) + if (!key_write(k, d, kf)) d->len = o; else { del = ',';