diff options
author | Brad King <brad.king@kitware.com> | 2014-07-29 12:52:08 (GMT) |
---|---|---|
committer | CMake Topic Stage <kwrobot@kitware.com> | 2014-07-29 12:52:08 (GMT) |
commit | 4d574ee3a1a8dcc54835d90d7eab55677deb4cfb (patch) | |
tree | ad01ad56e2f09ac3173d11bba18af79501b7df27 /Utilities/cmliblzma/liblzma/delta | |
parent | b6acd96f7fbe4df80e3735b101271cb801ba375a (diff) | |
parent | 65a0ea456d5d92644fe67bbbcbe973df3f111822 (diff) | |
download | CMake-4d574ee3a1a8dcc54835d90d7eab55677deb4cfb.zip CMake-4d574ee3a1a8dcc54835d90d7eab55677deb4cfb.tar.gz CMake-4d574ee3a1a8dcc54835d90d7eab55677deb4cfb.tar.bz2 |
Merge topic 'add-liblzma'
65a0ea45 Help: Add notes for topic 'add-liblzma'
8436d181 CMake: Enable use of liblzma in libarchive (#14504)
73eab246 liblzma: Avoid defining a 'restrict' macro
90e7a4d4 liblzma: Disable warnings to avoid changing 3rd party code
c20b4502 liblzma: Port to VS 6, 7.0
7a92eddb liblzma: Port from C99 to C89/90
b2a07ca4 liblzma: Add CMake build system
d44ad161 liblzma: Remove unused Makefile.* files
a53caea3 liblzma: Add README-CMake.txt
133d42fe Merge branch 'liblzma-upstream' into add-liblzma
c289e634 liblzma 5.0.5-gb69900ed (reduced)
8510533f liblzma: Add .gitattributes to ignore whitespace checks
Diffstat (limited to 'Utilities/cmliblzma/liblzma/delta')
-rw-r--r-- | Utilities/cmliblzma/liblzma/delta/delta_common.c | 72 | ||||
-rw-r--r-- | Utilities/cmliblzma/liblzma/delta/delta_common.h | 20 | ||||
-rw-r--r-- | Utilities/cmliblzma/liblzma/delta/delta_decoder.c | 79 | ||||
-rw-r--r-- | Utilities/cmliblzma/liblzma/delta/delta_decoder.h | 25 | ||||
-rw-r--r-- | Utilities/cmliblzma/liblzma/delta/delta_encoder.c | 124 | ||||
-rw-r--r-- | Utilities/cmliblzma/liblzma/delta/delta_encoder.h | 23 | ||||
-rw-r--r-- | Utilities/cmliblzma/liblzma/delta/delta_private.h | 37 |
7 files changed, 380 insertions, 0 deletions
diff --git a/Utilities/cmliblzma/liblzma/delta/delta_common.c b/Utilities/cmliblzma/liblzma/delta/delta_common.c new file mode 100644 index 0000000..803e674 --- /dev/null +++ b/Utilities/cmliblzma/liblzma/delta/delta_common.c @@ -0,0 +1,72 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file delta_common.c +/// \brief Common stuff for Delta encoder and decoder +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "delta_common.h" +#include "delta_private.h" + + +static void +delta_coder_end(lzma_coder *coder, lzma_allocator *allocator) +{ + lzma_next_end(&coder->next, allocator); + lzma_free(coder, allocator); + return; +} + + +extern lzma_ret +lzma_delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator, + const lzma_filter_info *filters) +{ + const lzma_options_delta *opt; + + // Allocate memory for the decoder if needed. + if (next->coder == NULL) { + next->coder = lzma_alloc(sizeof(lzma_coder), allocator); + if (next->coder == NULL) + return LZMA_MEM_ERROR; + + // End function is the same for encoder and decoder. + next->end = &delta_coder_end; + next->coder->next = LZMA_NEXT_CODER_INIT; + } + + // Validate the options. + if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX) + return LZMA_OPTIONS_ERROR; + + // Set the delta distance. + opt = filters[0].options; + next->coder->distance = opt->dist; + + // Initialize the rest of the variables. + next->coder->pos = 0; + memzero(next->coder->history, LZMA_DELTA_DIST_MAX); + + // Initialize the next decoder in the chain, if any. + return lzma_next_filter_init(&next->coder->next, + allocator, filters + 1); +} + + +extern uint64_t +lzma_delta_coder_memusage(const void *options) +{ + const lzma_options_delta *opt = options; + + if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE + || opt->dist < LZMA_DELTA_DIST_MIN + || opt->dist > LZMA_DELTA_DIST_MAX) + return UINT64_MAX; + + return sizeof(lzma_coder); +} diff --git a/Utilities/cmliblzma/liblzma/delta/delta_common.h b/Utilities/cmliblzma/liblzma/delta/delta_common.h new file mode 100644 index 0000000..7e7e1ba --- /dev/null +++ b/Utilities/cmliblzma/liblzma/delta/delta_common.h @@ -0,0 +1,20 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file delta_common.h +/// \brief Common stuff for Delta encoder and decoder +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef LZMA_DELTA_COMMON_H +#define LZMA_DELTA_COMMON_H + +#include "common.h" + +extern uint64_t lzma_delta_coder_memusage(const void *options); + +#endif diff --git a/Utilities/cmliblzma/liblzma/delta/delta_decoder.c b/Utilities/cmliblzma/liblzma/delta/delta_decoder.c new file mode 100644 index 0000000..28df727 --- /dev/null +++ b/Utilities/cmliblzma/liblzma/delta/delta_decoder.c @@ -0,0 +1,79 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file delta_decoder.c +/// \brief Delta filter decoder +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "delta_decoder.h" +#include "delta_private.h" + + +static void +decode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size) +{ + size_t i; + const size_t distance = coder->distance; + + for (i = 0; i < size; ++i) { + buffer[i] += coder->history[(distance + coder->pos) & 0xFF]; + coder->history[coder->pos-- & 0xFF] = buffer[i]; + } +} + + +static lzma_ret +delta_decode(lzma_coder *coder, lzma_allocator *allocator, + const uint8_t *LZMA_RESTRICT in, size_t *LZMA_RESTRICT in_pos, + size_t in_size, uint8_t *LZMA_RESTRICT out, + size_t *LZMA_RESTRICT out_pos, size_t out_size, lzma_action action) +{ + const size_t out_start = *out_pos; + lzma_ret ret; + + assert(coder->next.code != NULL); + + ret = coder->next.code(coder->next.coder, allocator, + in, in_pos, in_size, out, out_pos, out_size, + action); + + decode_buffer(coder, out + out_start, *out_pos - out_start); + + return ret; +} + + +extern lzma_ret +lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator, + const lzma_filter_info *filters) +{ + next->code = &delta_decode; + return lzma_delta_coder_init(next, allocator, filters); +} + + +extern lzma_ret +lzma_delta_props_decode(void **options, lzma_allocator *allocator, + const uint8_t *props, size_t props_size) +{ + lzma_options_delta *opt; + + if (props_size != 1) + return LZMA_OPTIONS_ERROR; + + opt = lzma_alloc(sizeof(lzma_options_delta), allocator); + if (opt == NULL) + return LZMA_MEM_ERROR; + + opt->type = LZMA_DELTA_TYPE_BYTE; + opt->dist = props[0] + 1; + + *options = opt; + + return LZMA_OK; +} diff --git a/Utilities/cmliblzma/liblzma/delta/delta_decoder.h b/Utilities/cmliblzma/liblzma/delta/delta_decoder.h new file mode 100644 index 0000000..ae89acc --- /dev/null +++ b/Utilities/cmliblzma/liblzma/delta/delta_decoder.h @@ -0,0 +1,25 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file delta_decoder.h +/// \brief Delta filter decoder +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef LZMA_DELTA_DECODER_H +#define LZMA_DELTA_DECODER_H + +#include "delta_common.h" + +extern lzma_ret lzma_delta_decoder_init(lzma_next_coder *next, + lzma_allocator *allocator, const lzma_filter_info *filters); + +extern lzma_ret lzma_delta_props_decode( + void **options, lzma_allocator *allocator, + const uint8_t *props, size_t props_size); + +#endif diff --git a/Utilities/cmliblzma/liblzma/delta/delta_encoder.c b/Utilities/cmliblzma/liblzma/delta/delta_encoder.c new file mode 100644 index 0000000..a39c154 --- /dev/null +++ b/Utilities/cmliblzma/liblzma/delta/delta_encoder.c @@ -0,0 +1,124 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file delta_encoder.c +/// \brief Delta filter encoder +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "delta_encoder.h" +#include "delta_private.h" + + +/// Copies and encodes the data at the same time. This is used when Delta +/// is the first filter in the chain (and thus the last filter in the +/// encoder's filter stack). +static void +copy_and_encode(lzma_coder *coder, + const uint8_t *LZMA_RESTRICT in, uint8_t *LZMA_RESTRICT out, size_t size) +{ + size_t i; + const size_t distance = coder->distance; + + for (i = 0; i < size; ++i) { + const uint8_t tmp = coder->history[ + (distance + coder->pos) & 0xFF]; + coder->history[coder->pos-- & 0xFF] = in[i]; + out[i] = in[i] - tmp; + } +} + + +/// Encodes the data in place. This is used when we are the last filter +/// in the chain (and thus non-last filter in the encoder's filter stack). +static void +encode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size) +{ + size_t i; + const size_t distance = coder->distance; + + for (i = 0; i < size; ++i) { + const uint8_t tmp = coder->history[ + (distance + coder->pos) & 0xFF]; + coder->history[coder->pos-- & 0xFF] = buffer[i]; + buffer[i] -= tmp; + } +} + + +static lzma_ret +delta_encode(lzma_coder *coder, lzma_allocator *allocator, + const uint8_t *LZMA_RESTRICT in, size_t *LZMA_RESTRICT in_pos, + size_t in_size, uint8_t *LZMA_RESTRICT out, + size_t *LZMA_RESTRICT out_pos, size_t out_size, lzma_action action) +{ + lzma_ret ret; + + if (coder->next.code == NULL) { + const size_t in_avail = in_size - *in_pos; + const size_t out_avail = out_size - *out_pos; + const size_t size = my_min(in_avail, out_avail); + + copy_and_encode(coder, in + *in_pos, out + *out_pos, size); + + *in_pos += size; + *out_pos += size; + + ret = action != LZMA_RUN && *in_pos == in_size + ? LZMA_STREAM_END : LZMA_OK; + + } else { + const size_t out_start = *out_pos; + + ret = coder->next.code(coder->next.coder, allocator, + in, in_pos, in_size, out, out_pos, out_size, + action); + + encode_in_place(coder, out + out_start, *out_pos - out_start); + } + + return ret; +} + + +static lzma_ret +delta_encoder_update(lzma_coder *coder, lzma_allocator *allocator, + const lzma_filter *filters_null lzma_attribute((__unused__)), + const lzma_filter *reversed_filters) +{ + // Delta doesn't and will never support changing the options in + // the middle of encoding. If the app tries to change them, we + // simply ignore them. + return lzma_next_filter_update( + &coder->next, allocator, reversed_filters + 1); +} + + +extern lzma_ret +lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, + const lzma_filter_info *filters) +{ + next->code = &delta_encode; + next->update = &delta_encoder_update; + return lzma_delta_coder_init(next, allocator, filters); +} + + +extern lzma_ret +lzma_delta_props_encode(const void *options, uint8_t *out) +{ + const lzma_options_delta *opt = options; + + // The caller must have already validated the options, so it's + // LZMA_PROG_ERROR if they are invalid. + if (lzma_delta_coder_memusage(options) == UINT64_MAX) + return LZMA_PROG_ERROR; + + out[0] = opt->dist - LZMA_DELTA_DIST_MIN; + + return LZMA_OK; +} diff --git a/Utilities/cmliblzma/liblzma/delta/delta_encoder.h b/Utilities/cmliblzma/liblzma/delta/delta_encoder.h new file mode 100644 index 0000000..a447862 --- /dev/null +++ b/Utilities/cmliblzma/liblzma/delta/delta_encoder.h @@ -0,0 +1,23 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file delta_encoder.h +/// \brief Delta filter encoder +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef LZMA_DELTA_ENCODER_H +#define LZMA_DELTA_ENCODER_H + +#include "delta_common.h" + +extern lzma_ret lzma_delta_encoder_init(lzma_next_coder *next, + lzma_allocator *allocator, const lzma_filter_info *filters); + +extern lzma_ret lzma_delta_props_encode(const void *options, uint8_t *out); + +#endif diff --git a/Utilities/cmliblzma/liblzma/delta/delta_private.h b/Utilities/cmliblzma/liblzma/delta/delta_private.h new file mode 100644 index 0000000..62b7fed --- /dev/null +++ b/Utilities/cmliblzma/liblzma/delta/delta_private.h @@ -0,0 +1,37 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file delta_private.h +/// \brief Private common stuff for Delta encoder and decoder +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef LZMA_DELTA_PRIVATE_H +#define LZMA_DELTA_PRIVATE_H + +#include "delta_common.h" + +struct lzma_coder_s { + /// Next coder in the chain + lzma_next_coder next; + + /// Delta distance + size_t distance; + + /// Position in history[] + uint8_t pos; + + /// Buffer to hold history of the original data + uint8_t history[LZMA_DELTA_DIST_MAX]; +}; + + +extern lzma_ret lzma_delta_coder_init( + lzma_next_coder *next, lzma_allocator *allocator, + const lzma_filter_info *filters); + +#endif |