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.
This commit is contained in:
Simon Tatham 2020-02-09 21:53:11 +00:00
Родитель 86ebc37783
Коммит e0e133b4b0
3 изменённых файлов: 25 добавлений и 19 удалений

21
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

11
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,

Просмотреть файл

@ -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)