diff options
-rw-r--r-- | include/mupdf/fitz/stream.h | 10 | ||||
-rw-r--r-- | source/fitz/stream-read.c | 17 | ||||
-rw-r--r-- | source/gprf/gprf-doc.c | 2 |
3 files changed, 28 insertions, 1 deletions
diff --git a/include/mupdf/fitz/stream.h b/include/mupdf/fitz/stream.h index 896b5846..53d5a256 100644 --- a/include/mupdf/fitz/stream.h +++ b/include/mupdf/fitz/stream.h @@ -173,6 +173,16 @@ int16_t fz_read_int16_le(fz_context *ctx, fz_stream *stm); int32_t fz_read_int32_le(fz_context *ctx, fz_stream *stm); int64_t fz_read_int64_le(fz_context *ctx, fz_stream *stm); +/* + fz_read_string: Read a null terminated string from the stream into + the a buffer of a given length. The buffer will be null terminated. + Throws on failure (including the failure to fit the entire string + including the terminator into the buffer). +*/ +void fz_read_string(fz_context *ctx, fz_stream *stm, char *buffer, int len); + + + enum { FZ_STREAM_META_PROGRESSIVE = 1, diff --git a/source/fitz/stream-read.c b/source/fitz/stream-read.c index f85b7612..927addb0 100644 --- a/source/fitz/stream-read.c +++ b/source/fitz/stream-read.c @@ -291,3 +291,20 @@ int64_t fz_read_int64(fz_context *ctx, fz_stream *stm) { return (int64_t)fz_read int16_t fz_read_int16_le(fz_context *ctx, fz_stream *stm) { return (int16_t)fz_read_uint16_le(ctx, stm); } int32_t fz_read_int32_le(fz_context *ctx, fz_stream *stm) { return (int32_t)fz_read_uint32_le(ctx, stm); } int64_t fz_read_int64_le(fz_context *ctx, fz_stream *stm) { return (int64_t)fz_read_uint64_le(ctx, stm); } + +void fz_read_string(fz_context *ctx, fz_stream *stm, char *buffer, int len) +{ + int c; + do + { + if (len <= 0) + fz_throw(ctx, FZ_ERROR_GENERIC, "Buffer overrun reading null terminated string"); + + c = fz_read_byte(ctx, stm); + if (c == EOF) + fz_throw(ctx, FZ_ERROR_GENERIC, "EOF reading null terminated string"); + *buffer++ = c; + len--; + } + while (c != 0); +} diff --git a/source/gprf/gprf-doc.c b/source/gprf/gprf-doc.c index 02562ca7..d0551816 100644 --- a/source/gprf/gprf-doc.c +++ b/source/gprf/gprf-doc.c @@ -556,7 +556,7 @@ read_tiles(fz_context *ctx, gprf_page *page) char blatter[4096]; int32_t rgba = fz_read_int32_le(ctx, file); int32_t cmyk = fz_read_int32_le(ctx, file); - fz_read_line(ctx, file, blatter, sizeof(blatter)); + fz_read_string(ctx, file, blatter, sizeof(blatter)); fz_add_separation(ctx, page->separations, rgba, cmyk, blatter); } |