diff options
author | Chris Johns <chrisj@rtems.org> | 2012-12-21 17:08:17 +1100 |
---|---|---|
committer | Chris Johns <chrisj@rtems.org> | 2012-12-21 17:08:17 +1100 |
commit | 437d1ff26efa1087407f260f4a4dffec4e849848 (patch) | |
tree | 2ef70152eda2bdcf70e54b24737997e20ff9f2de /rld-compression.h | |
parent | 9f45219a18a32f59c25bd78669c862c018911719 (diff) |
Decompressor fixes.
Make reading compressed files more robust returning the amount
of data that can be read. Also add >> operartors to get the
data. Add exceptions when a read fails.
Diffstat (limited to 'rld-compression.h')
-rw-r--r-- | rld-compression.h | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/rld-compression.h b/rld-compression.h index 20a1e44..9f06e0d 100644 --- a/rld-compression.h +++ b/rld-compression.h @@ -87,7 +87,7 @@ namespace rld * @param data Write the decompressed data here. * @param length The mount of data in bytes to read. */ - void read (void* data, size_t length); + size_t read (void* data, size_t length); /** * Read the decompressed data writing it to the image. @@ -96,7 +96,15 @@ namespace rld * @param offset The output image offset to write from. * @param length The mount of data in bytes to read. */ - void read (files::image& output_, off_t offset, size_t length); + size_t read (files::image& output_, off_t offset, size_t length); + + /** + * Read the decompressed data writing it to the image. + * + * @param output The output image. + * @param length The mount of data in bytes to read. + */ + size_t read (files::image& output_, size_t length); /** * The amount of uncompressed data transferred. @@ -112,6 +120,11 @@ namespace rld */ size_t compressed () const; + /** + * The current offset in the stream. + */ + off_t offset () const; + private: /** @@ -140,7 +153,7 @@ namespace rld }; /** - * Compressor template function for writing data to the compressor.. + * Compressor template function for writing data to the compressor. */ template < typename T > void write (compressor& comp, const T value) @@ -156,6 +169,24 @@ namespace rld comp.write (bytes, sizeof (T)); } + /** + * Compressor template function for reading data from the compressor. + */ + template < typename T > + T read (compressor& comp) + { + uint8_t bytes[sizeof (T)]; + T v = 0; + uint32_t b = 0; + if (comp.read (bytes, sizeof (T)) != sizeof (T)) + throw rld::error ("Reading of value failed", "compression"); + while (b < sizeof (T)) + { + v = (v << 8) | ((T) bytes[b++]); + } + return v; + } + } } @@ -177,4 +208,16 @@ static inline rld::compress::compressor& operator<< (rld::compress::compressor& return comp; } +static inline rld::compress::compressor& operator>> (rld::compress::compressor& comp, + uint64_t& value) { + value = rld::compress::read < uint64_t > (comp); + return comp; +} + +static inline rld::compress::compressor& operator>> (rld::compress::compressor& comp, + uint32_t& value) { + value = rld::compress::read < uint32_t > (comp); + return comp; +} + #endif |