From c680270bdeae2f9eea93b604b63330b48cf4442e Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Sun, 24 May 2015 11:45:01 +0200 Subject: Support reading stream bits in reverse order. --- include/mupdf/fitz/stream.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'include') diff --git a/include/mupdf/fitz/stream.h b/include/mupdf/fitz/stream.h index 21958b8a..64f3d704 100644 --- a/include/mupdf/fitz/stream.h +++ b/include/mupdf/fitz/stream.h @@ -340,6 +340,46 @@ static inline unsigned int fz_read_bits(fz_context *ctx, fz_stream *stm, int n) return x; } +static inline unsigned int fz_read_rbits(fz_context *ctx, fz_stream *stm, int n) +{ + unsigned int x; + + if (n <= stm->avail) + { + x = stm->bits & ((1 << n) - 1); + stm->avail -= n; + stm->bits = stm->bits >> n; + + } + else + { + unsigned int used = 0; + + x = stm->bits & ((1 << stm->avail) - 1); + n -= stm->avail; + used = stm->avail; + stm->avail = 0; + + while (n > 8) + { + + x = (fz_read_byte(ctx, stm) << used) | x; + n -= 8; + used += 8; + } + + if (n > 0) + { + stm->bits = fz_read_byte(ctx, stm); + x = ((stm->bits & ((1 << n) - 1)) << used) | x; + stm->avail = 8 - n; + stm->bits = stm->bits >> n; + } + } + + return x; +} + static inline void fz_sync_bits(fz_context *ctx, fz_stream *stm) { stm->avail = 0; -- cgit v1.2.3