1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32:48 -05:00

Refactor the key-components mechanism a bit.

Having recently pulled it out into its own file, I think it could also
do with a bit of tidying. In this rework:

 - the substructure for a single component now has a globally visible
   struct tag, so you can make a variable pointing at it, saving
   verbiage in every piece of code looping over a key_components

 - the 'is_mp_int' flag has been replaced with a type enum, so that
   more types can be added without further upheaval

 - the printing loop in cmdgen.c for puttygen --dump has factored out
   the initial 'name=' prefix on each line so that it isn't repeated
   per component type

 - the storage format for text components is now a strbuf rather than
   a plain char *, which I think is generally more useful.
This commit is contained in:
Simon Tatham
2022-04-18 10:10:57 +01:00
parent cf36b9215f
commit 68514ac8a1
5 changed files with 48 additions and 30 deletions

View File

@ -16,8 +16,8 @@ void key_components_add_text(key_components *kc,
sgrowarray(kc->components, kc->componentsize, kc->ncomponents);
size_t n = kc->ncomponents++;
kc->components[n].name = dupstr(name);
kc->components[n].is_mp_int = false;
kc->components[n].text = dupstr(value);
kc->components[n].type = KCT_TEXT;
kc->components[n].str = strbuf_dup_nm(ptrlen_from_asciz(value));
}
void key_components_add_mp(key_components *kc,
@ -26,19 +26,24 @@ void key_components_add_mp(key_components *kc,
sgrowarray(kc->components, kc->componentsize, kc->ncomponents);
size_t n = kc->ncomponents++;
kc->components[n].name = dupstr(name);
kc->components[n].is_mp_int = true;
kc->components[n].type = KCT_MPINT;
kc->components[n].mp = mp_copy(value);
}
void key_components_free(key_components *kc)
{
for (size_t i = 0; i < kc->ncomponents; i++) {
sfree(kc->components[i].name);
if (kc->components[i].is_mp_int) {
mp_free(kc->components[i].mp);
} else {
smemclr(kc->components[i].text, strlen(kc->components[i].text));
sfree(kc->components[i].text);
key_component *comp = &kc->components[i];
sfree(comp->name);
switch (comp->type) {
case KCT_MPINT:
mp_free(comp->mp);
break;
case KCT_TEXT:
strbuf_free(comp->str);
break;
default:
unreachable("bad key component type");
}
}
sfree(kc->components);