Code Monkey home page Code Monkey logo

lz4dec's Introduction

lz4dec

LZ4 block decompressor written in arm64 assembly.

Overview

lz4dec was written with a focus on small code size and is primarily intended for self-extracting firmware images. It has no dependencies, doesn't use the stack, and only does single-byte memory accesses, so it should work in any environment, with or without MMU, with any memory alignment. It also supports overlapping input and output buffers, so long as the output buffer doesn't "catch up" (i.e. output is never written into parts of the input buffer that haven't been parsed yet). Thus when in doubt, align the input to the end of available buffer.

It has two configuration:

  • "Safe"
    This is the default and should be used whenever feasible. In this configuration, the input is considered untrusted and proper bounds checks are employed.
  • "Unsafe"
    This configuration can be enabled by defining the UNSAFE_LZ4 macro and will eliminate all bounds checks. This must only be used with trusted input, e.g. when combined with a "known-good" compressed firmware image.

At the time of writing, the code size of the safe configuration is 252 bytes while that of the unsafe configuration is 144 bytes.

Usage

The lz4dec.S file exports a single function called lz4dec.

If UNSAFE_LZ4 is not defined, then the prototype of that function is:

extern uint64_t lz4dec(const void *src, void *dst, uint64_t srcsz, uint64_t dstsz);

If UNSAFE_LZ4 is defined, then the prototype becomes:

extern uint64_t lz4dec(const void *src, void *dst, uint64_t srcsz);

The return value is the number of bytes written to the output buffer, or 0 if an error occurred.

srcsz must only be the size of compressed data, trailing bytes that are not LZ4 block data are not allowed. This is required because the size must be known in order to determine where an LZ4 block ends.

Testing

There is a test binary written in C that wraps around lz4dec.

On arm64 hosts, it can be built like so:

make                        # for the safe configuration
CFLAGS='-DUNSAFE_LZ4' make  # for the unsafe configuration

And used like so:

./lz4dec <file.lz4 >file

There are no command-line options.

I also wrote a companion binary to generate single-block LZ4 data as used by this test binary.

License

MIT.

lz4dec's People

Contributors

siguza avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.