From e0e133b4b068d93bd1d0a3af2dbb13d451ed2346 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 9 Feb 2020 21:53:11 +0000 Subject: [PATCH] Expose the rest of LoadedFile in headers. This will allow it to be used more conveniently for things other than key files. For the moment, the implementation still lives in sshpubk.c. Moving it out into utils.c or misc.c would be nicer, but it has awkward dependencies on marshal.c and the per-platform f_open function. Perhaps another time. --- misc.h | 21 +++++++++++++++++++++ ssh.h | 11 +---------- sshpubk.c | 12 +++--------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/misc.h b/misc.h index eff7e08c..e31a3ec7 100644 --- a/misc.h +++ b/misc.h @@ -406,4 +406,25 @@ static inline char *stripctrl_string(StripCtrlChars *sccpub, const char *str) return stripctrl_string_ptrlen(sccpub, ptrlen_from_asciz(str)); } +/* + * A mechanism for loading a file from disk into a memory buffer where + * it can be picked apart as a BinarySource. + */ +struct LoadedFile { + char *data; + size_t len, max_size; + BinarySource_IMPLEMENTATION; +}; +typedef enum { + LF_OK, /* file loaded successfully */ + LF_TOO_BIG, /* file didn't fit in buffer */ + LF_ERROR, /* error from stdio layer */ +} LoadFileStatus; +LoadedFile *lf_new(size_t max_size); +void lf_free(LoadedFile *lf); +LoadFileStatus lf_load_fp(LoadedFile *lf, FILE *fp); +LoadFileStatus lf_load(LoadedFile *lf, const Filename *filename); +static inline ptrlen ptrlen_from_lf(LoadedFile *lf) +{ return make_ptrlen(lf->data, lf->len); } + #endif diff --git a/ssh.h b/ssh.h index ec972e05..590f4cd2 100644 --- a/ssh.h +++ b/ssh.h @@ -1197,18 +1197,9 @@ int rsa1_loadpub_f(const Filename *filename, BinarySink *bs, const ssh_keyalg *find_pubkey_alg(const char *name); const ssh_keyalg *find_pubkey_alg_len(ptrlen name); -/* - * A mechanism for loading a key file from disk into a memory buffer - * where it can be picked apart as a BinarySource. - */ -struct LoadedFile { - char *data; - size_t len, max_size; - BinarySource_IMPLEMENTATION; -}; +/* Convenient wrappers on the LoadedFile mechanism suitable for key files */ LoadedFile *lf_load_keyfile(const Filename *filename, const char **errptr); LoadedFile *lf_load_keyfile_fp(FILE *fp, const char **errptr); -void lf_free(LoadedFile *lf); enum { SSH_KEYTYPE_UNOPENABLE, diff --git a/sshpubk.c b/sshpubk.c index a06d0c01..ac995b69 100644 --- a/sshpubk.c +++ b/sshpubk.c @@ -44,13 +44,7 @@ static const ptrlen rsa1_signature = (x)=='+' ? 62 : \ (x)=='/' ? 63 : 0 ) -typedef enum { - LF_OK, /* file loaded successfully */ - LF_TOO_BIG, /* file didn't fit in buffer */ - LF_ERROR, /* error from stdio layer */ -} LoadFileStatus; - -static LoadedFile *lf_new(size_t max_size) +LoadedFile *lf_new(size_t max_size) { LoadedFile *lf = snew_plus(LoadedFile, max_size); lf->data = snew_plus_get_aux(lf); @@ -66,7 +60,7 @@ void lf_free(LoadedFile *lf) sfree(lf); } -static LoadFileStatus lf_load_fp(LoadedFile *lf, FILE *fp) +LoadFileStatus lf_load_fp(LoadedFile *lf, FILE *fp) { lf->len = 0; while (lf->len < lf->max_size) { @@ -94,7 +88,7 @@ static LoadFileStatus lf_load_fp(LoadedFile *lf, FILE *fp) return status; } -static LoadFileStatus lf_load(LoadedFile *lf, const Filename *filename) +LoadFileStatus lf_load(LoadedFile *lf, const Filename *filename) { FILE *fp = f_open(filename, "rb", false); if (!fp)