summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2015-07-30 14:48:27 +0200
committerTor Andersson <tor.andersson@artifex.com>2015-07-31 15:07:30 +0200
commit81aa73b272f350642e490a7c528f06e28a7eb117 (patch)
tree97a724f1215f22f7d94f876256f94b82af04f3d8
parentefb5a38ca0bac3537ceaf3383681a518df133143 (diff)
downloadmupdf-81aa73b272f350642e490a7c528f06e28a7eb117.tar.xz
Clean up fz_read_[u]intNN() functions and add big-endian versions.
Use an endian-ness independent method of reading, instead of byte swapping.
-rw-r--r--include/mupdf/fitz/stream.h34
-rw-r--r--source/fitz/stream-read.c111
-rw-r--r--source/gprf/gprf-doc.c36
3 files changed, 126 insertions, 55 deletions
diff --git a/include/mupdf/fitz/stream.h b/include/mupdf/fitz/stream.h
index 64f3d704..896b5846 100644
--- a/include/mupdf/fitz/stream.h
+++ b/include/mupdf/fitz/stream.h
@@ -148,22 +148,30 @@ fz_buffer *fz_read_all(fz_context *ctx, fz_stream *stm, int initial);
fz_buffer *fz_read_file(fz_context *ctx, const char *filename);
/*
- fz_read_int16le: Read a 16bit little endian value from the stream.
- Throws on failure.
-*/
-int16_t fz_read_int16le(fz_context *ctx, fz_stream *stm);
+ fz_read_[u]int(16|24|32|64)(_le)?
-/*
- fz_read_int32le: Read a 32bit little endian value from the stream.
- Throws on failure.
-*/
-int32_t fz_read_int32le(fz_context *ctx, fz_stream *stm);
+ Read a 16/32/64 bit signed/unsigned integer from stream,
+ in big or little-endian byte orders.
-/*
- fz_read_int64le: Read a 64bit little endian value from the stream.
- Throws on failure.
+ Throws an exception if EOF is encountered.
*/
-int64_t fz_read_int64le(fz_context *ctx, fz_stream *stm);
+uint16_t fz_read_uint16(fz_context *ctx, fz_stream *stm);
+uint32_t fz_read_uint24(fz_context *ctx, fz_stream *stm);
+uint32_t fz_read_uint32(fz_context *ctx, fz_stream *stm);
+uint64_t fz_read_uint64(fz_context *ctx, fz_stream *stm);
+
+uint16_t fz_read_uint16_le(fz_context *ctx, fz_stream *stm);
+uint32_t fz_read_uint24_le(fz_context *ctx, fz_stream *stm);
+uint32_t fz_read_uint32_le(fz_context *ctx, fz_stream *stm);
+uint64_t fz_read_uint64_le(fz_context *ctx, fz_stream *stm);
+
+int16_t fz_read_int16(fz_context *ctx, fz_stream *stm);
+int32_t fz_read_int32(fz_context *ctx, fz_stream *stm);
+int64_t fz_read_int64(fz_context *ctx, fz_stream *stm);
+
+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);
enum
{
diff --git a/source/fitz/stream-read.c b/source/fitz/stream-read.c
index c228b5d0..f85b7612 100644
--- a/source/fitz/stream-read.c
+++ b/source/fitz/stream-read.c
@@ -186,45 +186,108 @@ fz_read_file(fz_context *ctx, const char *filename)
return buf;
}
-static inline int isbigendian(void)
+uint16_t fz_read_uint16(fz_context *ctx, fz_stream *stm)
{
- static const int one = 1;
- return *(char*)&one == 0;
+ uint16_t a = fz_read_byte(ctx, stm);
+ uint16_t b = fz_read_byte(ctx, stm);
+ uint16_t x = (a<<8) | (b);
+ if (a == EOF || b == EOF)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int16");
+ return x;
}
-static inline int32_t rev32(int32_t val)
+uint32_t fz_read_uint24(fz_context *ctx, fz_stream *stm)
{
- return ((val>>24) & 0xff) || ((val>>8) & 0xFF00) || ((val<<8) & 0xFF0000) || (val<<24);
+ uint32_t a = fz_read_byte(ctx, stm);
+ uint32_t b = fz_read_byte(ctx, stm);
+ uint32_t c = fz_read_byte(ctx, stm);
+ uint32_t x = (a<<16) | (b<<8) | (c);
+ if (a == EOF || b == EOF || c == EOF)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int24");
+ return x;
}
-int32_t fz_read_int32le(fz_context *ctx, fz_stream *stm)
+uint32_t fz_read_uint32(fz_context *ctx, fz_stream *stm)
{
- int32_t val;
-
- if (fz_read(ctx, stm, (unsigned char *)&val, sizeof(val)) != sizeof(val))
- fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to read int32le from file");
-
- if (isbigendian())
- val = rev32(val);
-
- return val;
+ uint32_t a = fz_read_byte(ctx, stm);
+ uint32_t b = fz_read_byte(ctx, stm);
+ uint32_t c = fz_read_byte(ctx, stm);
+ uint32_t d = fz_read_byte(ctx, stm);
+ uint32_t x = (a<<24) | (b<<16) | (c<<8) | (d);
+ if (a == EOF || b == EOF || c == EOF || d == EOF)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int32");
+ return x;
}
-int16_t fz_read_int16le(fz_context *ctx, fz_stream *stm)
+uint64_t fz_read_uint64(fz_context *ctx, fz_stream *stm)
{
- int a = fz_read_byte(ctx, stm);
- int b = fz_read_byte(ctx, stm);
+ uint64_t a = fz_read_byte(ctx, stm);
+ uint64_t b = fz_read_byte(ctx, stm);
+ uint64_t c = fz_read_byte(ctx, stm);
+ uint64_t d = fz_read_byte(ctx, stm);
+ uint64_t e = fz_read_byte(ctx, stm);
+ uint64_t f = fz_read_byte(ctx, stm);
+ uint64_t g = fz_read_byte(ctx, stm);
+ uint64_t h = fz_read_byte(ctx, stm);
+ uint64_t x = (a<<56) | (b<<48) | (c<<40) | (d<<32) | (e<<24) | (f<<16) | (g<<8) | (h);
+ if (a == EOF || b == EOF || c == EOF || d == EOF || e == EOF || f == EOF || g == EOF || h == EOF)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int64");
+ return x;
+}
+uint16_t fz_read_uint16_le(fz_context *ctx, fz_stream *stm)
+{
+ uint16_t a = fz_read_byte(ctx, stm);
+ uint16_t b = fz_read_byte(ctx, stm);
+ uint16_t x = (a) | (b<<8);
if (a == EOF || b == EOF)
- fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to read int16le from file");
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int16");
+ return x;
+}
- return a | (b<<8);
+uint32_t fz_read_uint24_le(fz_context *ctx, fz_stream *stm)
+{
+ uint32_t a = fz_read_byte(ctx, stm);
+ uint32_t b = fz_read_byte(ctx, stm);
+ uint32_t c = fz_read_byte(ctx, stm);
+ uint32_t x = (a) | (b<<8) | (c<<16);
+ if (a == EOF || b == EOF || c == EOF)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int24");
+ return x;
}
-int64_t fz_read_int64le(fz_context *ctx, fz_stream *stm)
+uint32_t fz_read_uint32_le(fz_context *ctx, fz_stream *stm)
{
- uint32_t v0 = (uint32_t)fz_read_int32le(ctx, stm);
- uint32_t v1 = (uint32_t)fz_read_int32le(ctx, stm);
+ uint32_t a = fz_read_byte(ctx, stm);
+ uint32_t b = fz_read_byte(ctx, stm);
+ uint32_t c = fz_read_byte(ctx, stm);
+ uint32_t d = fz_read_byte(ctx, stm);
+ uint32_t x = (a) | (b<<8) | (c<<16) | (d<<24);
+ if (a == EOF || b == EOF || c == EOF || d == EOF)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int32");
+ return x;
+}
- return v0 | (((int64_t)v1)<<32);
+uint64_t fz_read_uint64_le(fz_context *ctx, fz_stream *stm)
+{
+ uint64_t a = fz_read_byte(ctx, stm);
+ uint64_t b = fz_read_byte(ctx, stm);
+ uint64_t c = fz_read_byte(ctx, stm);
+ uint64_t d = fz_read_byte(ctx, stm);
+ uint64_t e = fz_read_byte(ctx, stm);
+ uint64_t f = fz_read_byte(ctx, stm);
+ uint64_t g = fz_read_byte(ctx, stm);
+ uint64_t h = fz_read_byte(ctx, stm);
+ uint64_t x = (a) | (b<<8) | (c<<16) | (d<<24) | (e<<32) | (f<<40) | (g<<48) | (h<<56);
+ if (a == EOF || b == EOF || c == EOF || d == EOF || e == EOF || f == EOF || g == EOF || h == EOF)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of file in int64");
+ return x;
}
+
+int16_t fz_read_int16(fz_context *ctx, fz_stream *stm) { return (int16_t)fz_read_uint16(ctx, stm); }
+int32_t fz_read_int32(fz_context *ctx, fz_stream *stm) { return (int32_t)fz_read_uint32(ctx, stm); }
+int64_t fz_read_int64(fz_context *ctx, fz_stream *stm) { return (int64_t)fz_read_uint64(ctx, stm); }
+
+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); }
diff --git a/source/gprf/gprf-doc.c b/source/gprf/gprf-doc.c
index 103b8e1e..07714b8a 100644
--- a/source/gprf/gprf-doc.c
+++ b/source/gprf/gprf-doc.c
@@ -515,38 +515,38 @@ read_tiles(fz_context *ctx, gprf_page *page)
fz_try(ctx)
{
- val = fz_read_int32le(ctx, file);
+ val = fz_read_int32_le(ctx, file);
if (val != 0x46505347)
fz_throw(ctx, FZ_ERROR_GENERIC, "Unexpected signature in GSPF file");
- val = fz_read_int16le(ctx, file);
+ val = fz_read_int16_le(ctx, file);
if (val != 1)
fz_throw(ctx, FZ_ERROR_GENERIC, "Unexpected version in GSPF file");
- val = fz_read_int16le(ctx, file);
+ val = fz_read_int16_le(ctx, file);
if (val != 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "Unexpected compression in GSPF file");
- val = fz_read_int32le(ctx, file);
+ val = fz_read_int32_le(ctx, file);
if (val != page->width)
fz_throw(ctx, FZ_ERROR_GENERIC, "Unexpected width in GSPF file");
- val = fz_read_int32le(ctx, file);
+ val = fz_read_int32_le(ctx, file);
if (val != page->height)
fz_throw(ctx, FZ_ERROR_GENERIC, "Unexpected height in GSPF file");
- val = fz_read_int16le(ctx, file);
+ val = fz_read_int16_le(ctx, file);
if (val != 8)
fz_throw(ctx, FZ_ERROR_GENERIC, "Unexpected bpc in GSPF file");
- num_seps = fz_read_int16le(ctx, file);
+ num_seps = fz_read_int16_le(ctx, file);
if (num_seps < 0 || num_seps > FZ_MAX_SEPARATIONS)
fz_throw(ctx, FZ_ERROR_GENERIC, "Unexpected number of separations in GSPF file");
- offset = fz_read_int64le(ctx, file); /* Ignore the ICC for now */
+ offset = fz_read_int64_le(ctx, file); /* Ignore the ICC for now */
/* Read the table offset */
- offset = fz_read_int64le(ctx, file);
+ offset = fz_read_int64_le(ctx, file);
/* Skip to the separations */
fz_seek(ctx, file, 64, SEEK_SET);
@@ -554,8 +554,8 @@ read_tiles(fz_context *ctx, gprf_page *page)
for (i = 0; i < num_seps; i++)
{
char blatter[4096];
- int32_t rgba = fz_read_int32le(ctx, file);
- int32_t cmyk = fz_read_int32le(ctx, file);
+ 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_add_separation(ctx, page->separations, rgba, cmyk, blatter);
}
@@ -567,7 +567,7 @@ read_tiles(fz_context *ctx, gprf_page *page)
page->tiles = fz_calloc(ctx, num_tiles, sizeof(fz_image *));
i = 0;
- off = fz_read_int64le(ctx, file);
+ off = fz_read_int64_le(ctx, file);
for (y = 0; y < page->tile_height; y++)
{
for (x = 0; x < page->tile_width; x++)
@@ -578,7 +578,7 @@ read_tiles(fz_context *ctx, gprf_page *page)
for (j = 0; j < num_seps+3; j++)
{
offsets[j] = (fz_off_t)off;
- off = fz_read_int64le(ctx, file);
+ off = fz_read_int64_le(ctx, file);
}
page->tiles[i] = fz_new_gprf_image(ctx, page, i, offsets, (fz_off_t)off);
@@ -712,25 +712,25 @@ gprf_open_document_with_stream(fz_context *ctx, fz_stream *file)
int i;
char buf[4096];
- val = fz_read_int32le(ctx, file);
+ val = fz_read_int32_le(ctx, file);
if (val != 0x4f525047)
fz_throw(ctx, FZ_ERROR_GENERIC, "Invalid file signature in gproof file");
val = fz_read_byte(ctx, file);
val |= fz_read_byte(ctx, file)<<8;
if (val != 1)
fz_throw(ctx, FZ_ERROR_GENERIC, "Invalid version in gproof file");
- doc->res = fz_read_int32le(ctx, file);
+ doc->res = fz_read_int32_le(ctx, file);
if (doc->res < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "Invalid resolution in gproof file");
- doc->num_pages = fz_read_int32le(ctx, file);
+ doc->num_pages = fz_read_int32_le(ctx, file);
if (doc->num_pages < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "Invalid resolution in gproof file");
doc->page_dims = fz_calloc(ctx, doc->num_pages, sizeof(*doc->page_dims));
for (i = 0; i < doc->num_pages; i++)
{
- doc->page_dims[i].w = fz_read_int32le(ctx, file);
- doc->page_dims[i].h = fz_read_int32le(ctx, file);
+ doc->page_dims[i].w = fz_read_int32_le(ctx, file);
+ doc->page_dims[i].h = fz_read_int32_le(ctx, file);
}
fz_read_line(ctx, file, buf, sizeof(buf));
doc->pdf_filename = fz_strdup(ctx, buf);