diff --git a/misc.h b/misc.h index a8eb7a19..8573098d 100644 --- a/misc.h +++ b/misc.h @@ -97,6 +97,7 @@ static inline int toint(unsigned u) } char *fgetline(FILE *fp); +bool read_file_into(BinarySink *bs, FILE *fp); char *chomp(char *str); bool strstartswith(const char *s, const char *t); bool strendswith(const char *s, const char *t); diff --git a/utils.c b/utils.c index 168a4a4f..17f67587 100644 --- a/utils.c +++ b/utils.c @@ -501,6 +501,20 @@ char *fgetline(FILE *fp) return ret; } +/* + * Read an entire file into a BinarySink. + */ +bool read_file_into(BinarySink *bs, FILE *fp) +{ + char buf[4096]; + while (1) { + size_t retd = fread(buf, 1, sizeof(buf), fp); + if (retd == 0) + return !ferror(fp); + put_data(bs, buf, retd); + } +} + /* * Perl-style 'chomp', for a line we just read with fgetline. Unlike * Perl chomp, however, we're deliberately forgiving of strange