diff options
author | Chris Johns <chrisj@rtems.org> | 2012-11-22 16:22:22 +1100 |
---|---|---|
committer | Chris Johns <chrisj@rtems.org> | 2012-11-22 16:22:22 +1100 |
commit | bee2397a6ebbb539d5e961dbcf77655f9ce0b88c (patch) | |
tree | cdd0703d0283442e66a4bd1e60fdcd618702dee1 /rld-compression.cpp | |
parent | 81a8394daa9995b9c8f2ce6b4e3918d79bc1f630 (diff) |
Split out the compression code for reuse.
Diffstat (limited to 'rld-compression.cpp')
-rw-r--r-- | rld-compression.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/rld-compression.cpp b/rld-compression.cpp new file mode 100644 index 0000000..3309371 --- /dev/null +++ b/rld-compression.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2012, Chris Johns <chrisj@rtems.org> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/** + * @file + * + * @ingroup rtems_ld + * + * @brief RTEMS Linker. + * + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <fstream> +#include <iostream> + +#include <errno.h> +#include <string.h> + +#include <rld.h> +#include <rld-compression.h> + +#include "fastlz.h" + +namespace rld +{ + namespace compress + { + compressor::compressor (files::image& image, + size_t size, + bool compress) + : image (image), + size (size), + compress (compress), + buffer (0), + io (0), + level (0), + total (0), + total_compressed (0) + { + buffer = new uint8_t[size]; + io = new uint8_t[size + (size / 10)]; + } + + compressor::~compressor () + { + flush (); + delete [] buffer; + delete [] io; + } + + void + compressor::write (const void* data_, size_t length) + { + const uint8_t* data = static_cast <const uint8_t*> (data_); + + if (compress) + { + while (length) + { + size_t appending; + + if (length > (size - level)) + appending = size - level; + else + appending = length; + + ::memcpy ((void*) (buffer + level), data, appending); + + level += appending; + length -= appending; + total += appending; + + if (level >= size) + { + int writing = + ::fastlz_compress (buffer, level, io); + + image.write (io, writing); + + total_compressed += writing; + level = 0; + } + } + } + else + { + image.write (data, length); + total += length; + } + } + + void + compressor::flush () + { + if (level) + { + int writing = + ::fastlz_compress (buffer, level, io); + + image.write (io, writing); + + total_compressed += writing; + level = 0; + } + } + + size_t + compressor::transferred () const + { + return total; + } + + size_t + compressor::compressed () const + { + return total_compressed; + } + + } +} |