123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- #ifndef STATIC
- #include <linux/module.h>
- #include <linux/kernel.h>
- #endif
- #include <linux/lz4.h>
- #include <asm/unaligned.h>
- #include "lz4defs.h"
- static const int dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
- #if LZ4_ARCH64
- static const int dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
- #endif
- static int lz4_uncompress(const char *source, char *dest, int osize)
- {
- const BYTE *ip = (const BYTE *) source;
- const BYTE *ref;
- BYTE *op = (BYTE *) dest;
- BYTE * const oend = op + osize;
- BYTE *cpy;
- unsigned token;
- size_t length;
- while (1) {
-
- token = *ip++;
- length = (token >> ML_BITS);
- if (length == RUN_MASK) {
- size_t len;
- len = *ip++;
- for (; len == 255; length += 255)
- len = *ip++;
- if (unlikely(length > (size_t)(length + len)))
- goto _output_error;
- length += len;
- }
-
- cpy = op + length;
- if (unlikely(cpy > oend - COPYLENGTH)) {
-
- if (cpy != oend)
- goto _output_error;
- memcpy(op, ip, length);
- ip += length;
- break;
- }
- LZ4_WILDCOPY(ip, op, cpy);
- ip -= (op - cpy);
- op = cpy;
-
- LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
- ip += 2;
-
- if (unlikely(ref < (BYTE *const) dest))
- goto _output_error;
-
- length = token & ML_MASK;
- if (length == ML_MASK) {
- for (; *ip == 255; length += 255)
- ip++;
- if (unlikely(length > (size_t)(length + *ip)))
- goto _output_error;
- length += *ip++;
- }
-
- if (unlikely((op - ref) < STEPSIZE)) {
- #if LZ4_ARCH64
- int dec64 = dec64table[op - ref];
- #else
- const int dec64 = 0;
- #endif
- op[0] = ref[0];
- op[1] = ref[1];
- op[2] = ref[2];
- op[3] = ref[3];
- op += 4;
- ref += 4;
- ref -= dec32table[op-ref];
- PUT4(ref, op);
- op += STEPSIZE - 4;
- ref -= dec64;
- } else {
- LZ4_COPYSTEP(ref, op);
- }
- cpy = op + length - (STEPSIZE - 4);
- if (cpy > (oend - COPYLENGTH)) {
-
- if (cpy > oend)
- goto _output_error;
- #if LZ4_ARCH64
- if ((ref + COPYLENGTH) > oend)
- #else
- if ((ref + COPYLENGTH) > oend ||
- (op + COPYLENGTH) > oend)
- #endif
- goto _output_error;
- LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
- while (op < cpy)
- *op++ = *ref++;
- op = cpy;
-
- if (op == oend)
- goto _output_error;
- continue;
- }
- LZ4_SECURECOPY(ref, op, cpy);
- op = cpy;
- }
-
- return (int) (((char *)ip) - source);
-
- _output_error:
- return -1;
- }
- static int lz4_uncompress_unknownoutputsize(const char *source, char *dest,
- int isize, size_t maxoutputsize)
- {
- const BYTE *ip = (const BYTE *) source;
- const BYTE *const iend = ip + isize;
- const BYTE *ref;
- BYTE *op = (BYTE *) dest;
- BYTE * const oend = op + maxoutputsize;
- BYTE *cpy;
-
- while (ip < iend) {
- unsigned token;
- size_t length;
-
- token = *ip++;
- length = (token >> ML_BITS);
- if (length == RUN_MASK) {
- int s = 255;
- while ((ip < iend) && (s == 255)) {
- s = *ip++;
- if (unlikely(length > (size_t)(length + s)))
- goto _output_error;
- length += s;
- }
- }
-
- cpy = op + length;
- if ((cpy > oend - COPYLENGTH) ||
- (ip + length > iend - COPYLENGTH)) {
- if (cpy > oend)
- goto _output_error;
- if (ip + length != iend)
- goto _output_error;
- memcpy(op, ip, length);
- op += length;
- break;
- }
- LZ4_WILDCOPY(ip, op, cpy);
- ip -= (op - cpy);
- op = cpy;
-
- LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
- ip += 2;
- if (ref < (BYTE * const) dest)
- goto _output_error;
-
-
- length = (token & ML_MASK);
- if (length == ML_MASK) {
- while (ip < iend) {
- int s = *ip++;
- if (unlikely(length > (size_t)(length + s)))
- goto _output_error;
- length += s;
- if (s == 255)
- continue;
- break;
- }
- }
-
- if (unlikely((op - ref) < STEPSIZE)) {
- #if LZ4_ARCH64
- int dec64 = dec64table[op - ref];
- #else
- const int dec64 = 0;
- #endif
- op[0] = ref[0];
- op[1] = ref[1];
- op[2] = ref[2];
- op[3] = ref[3];
- op += 4;
- ref += 4;
- ref -= dec32table[op - ref];
- PUT4(ref, op);
- op += STEPSIZE - 4;
- ref -= dec64;
- } else {
- LZ4_COPYSTEP(ref, op);
- }
- cpy = op + length - (STEPSIZE-4);
- if (cpy > oend - COPYLENGTH) {
- if (cpy > oend)
- goto _output_error;
- #if LZ4_ARCH64
- if ((ref + COPYLENGTH) > oend)
- #else
- if ((ref + COPYLENGTH) > oend ||
- (op + COPYLENGTH) > oend)
- #endif
- goto _output_error;
- LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
- while (op < cpy)
- *op++ = *ref++;
- op = cpy;
-
- if (op == oend)
- goto _output_error;
- continue;
- }
- LZ4_SECURECOPY(ref, op, cpy);
- op = cpy;
- }
-
- return (int) (((char *) op) - dest);
-
- _output_error:
- return -1;
- }
- int lz4_decompress(const unsigned char *src, size_t *src_len,
- unsigned char *dest, size_t actual_dest_len)
- {
- int ret = -1;
- int input_len = 0;
- input_len = lz4_uncompress(src, dest, actual_dest_len);
- if (input_len < 0)
- goto exit_0;
- *src_len = input_len;
- return 0;
- exit_0:
- return ret;
- }
- #ifndef STATIC
- EXPORT_SYMBOL(lz4_decompress);
- #endif
- int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
- unsigned char *dest, size_t *dest_len)
- {
- int ret = -1;
- int out_len = 0;
- out_len = lz4_uncompress_unknownoutputsize(src, dest, src_len,
- *dest_len);
- if (out_len < 0)
- goto exit_0;
- *dest_len = out_len;
- return 0;
- exit_0:
- return ret;
- }
- #ifndef STATIC
- EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_DESCRIPTION("LZ4 Decompressor");
- #endif
|