Skip to content

Commit

Permalink
Add LZFSE plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
nemequ committed Jun 16, 2016
1 parent d703faf commit c849fcc
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 1 deletion.
1 change: 1 addition & 0 deletions .configure-custom.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ for plugin in \
libdeflate \
lz4 \
lzf \
lzfse \
lzg \
lzham \
lzjb \
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Squash currently contains plugins for the following libraries:
- [heatshrink](@ref md_plugins_heatshrink_heatshrink)
- [LZ4](@ref md_plugins_lz4_lz4)
- [LZF](@ref md_plugins_lzf_lzf)
- [LZFSE](@ref md_plugins_lzf_lzfse)
- [libdeflate](@ref md_plugins_libdeflate_libdeflate)
- [liblzg](@ref md_plugins_lzg_lzg)
- [LZHAM](@ref md_plugins_lzham_lzham)
Expand Down
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set (plugins_available
libdeflate
lz4
lzf
lzfse
lzg
lzham
lzjb
Expand Down
15 changes: 15 additions & 0 deletions plugins/lzfse/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include (SquashPlugin)

squash_plugin (
NAME lzfse
SOURCES squash-lzfse.c
C_STANDARD c99
EMBED_SOURCES
lzfse/src/lzfse_decode_base.c
lzfse/src/lzfse_decode.c
lzfse/src/lzfse_encode_base.c
lzfse/src/lzfse_encode.c
lzfse/src/lzfse_fse.c
lzfse/src/lzfse_main.c
lzfse/src/lzvn_decode_base.c
lzfse/src/lzvn_encode_base.c)
2 changes: 1 addition & 1 deletion plugins/lzfse/lzfse
Submodule lzfse updated 1 files
+1 −1 src/lzfse_encode.c
15 changes: 15 additions & 0 deletions plugins/lzfse/lzfse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# lzf Plugin #

LZFSE is a compression codec developed by Apple intended to replace zlib.

For more information about LZFSE, see https://github.com/lzfse/lzfse/

## Codecs ##

- **lzfse** — LZFSE data.

## License ##

The lzfse plugin is licensed under the
[MIT License](http://opensource.org/licenses/MIT), and LZFSE is under
a [3-clause BSD License](http://opensource.org/licenses/BSD-3-Clause).
132 changes: 132 additions & 0 deletions plugins/lzfse/squash-lzfse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* Copyright (c) 2016 The Squash Authors
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Authors:
* Evan Nemerson <evan@nemerson.com>
*/

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#include <squash/squash.h>


#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wpacked"
#pragma GCC diagnostic ignored "-Wpointer-arith"
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif /* defined(__GNUC__) */

#include "lzfse/src/lzfse.h"
#include "lzfse/src/lzfse_internal.h"

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif /* defined(__GNUC__) */

SQUASH_PLUGIN_EXPORT
SquashStatus squash_plugin_init_codec (SquashCodec* codec, SquashCodecImpl* impl);

static size_t
squash_lzfse_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_size) {
return uncompressed_size + 12;
}

static SquashStatus
squash_lzfse_decompress_buffer (SquashCodec* codec,
size_t* decompressed_size,
uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_size)],
size_t compressed_size,
const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)],
SquashOptions* options) {
const size_t workmem_size = lzfse_decode_scratch_size ();
lzfse_decoder_state* ctx = squash_calloc (workmem_size, 1);
if (SQUASH_UNLIKELY(ctx == NULL))
return squash_error (SQUASH_FAILED);

ctx->src_begin = ctx->src = compressed;
ctx->src_end = compressed + compressed_size;
ctx->dst_begin = ctx->dst = decompressed;
ctx->dst_end = decompressed + *decompressed_size;

const int lret = lzfse_decode (ctx);
const size_t written = (size_t) (ctx->dst - decompressed);

squash_free (ctx);

switch (lret) {
case LZFSE_STATUS_OK:
*decompressed_size = written;
return SQUASH_OK;
case LZFSE_STATUS_DST_FULL:
return squash_error (SQUASH_BUFFER_FULL);
case LZFSE_STATUS_ERROR:
default:
return squash_error (SQUASH_FAILED);
}
}

static SquashStatus
squash_lzfse_compress_buffer (SquashCodec* codec,
size_t* compressed_size,
uint8_t compressed[SQUASH_ARRAY_PARAM(*compressed_size)],
size_t uncompressed_size,
const uint8_t uncompressed[SQUASH_ARRAY_PARAM(uncompressed_size)],
SquashOptions* options) {
void* workmem = squash_calloc (lzfse_encode_scratch_size (), 1);
if (SQUASH_UNLIKELY(workmem == NULL))
return squash_error (SQUASH_FAILED);

const size_t r = lzfse_encode_buffer (compressed, *compressed_size,
uncompressed, uncompressed_size,
workmem);

squash_free (workmem);

if (SQUASH_UNLIKELY(r == 0))
return squash_error (SQUASH_BUFFER_FULL);

*compressed_size = r;
return SQUASH_OK;
}

SquashStatus
squash_plugin_init_codec (SquashCodec* codec, SquashCodecImpl* impl) {
const char* name = squash_codec_get_name (codec);

if (SQUASH_LIKELY(strcmp ("lzfse", name) == 0)) {
impl->get_max_compressed_size = squash_lzfse_get_max_compressed_size;
impl->decompress_buffer = squash_lzfse_decompress_buffer;
impl->compress_buffer = squash_lzfse_compress_buffer;
} else {
return SQUASH_UNABLE_TO_LOAD;
}

return SQUASH_OK;
}
3 changes: 3 additions & 0 deletions plugins/lzfse/squash.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
license=BSD3

[lzfse]

0 comments on commit c849fcc

Please sign in to comment.