replace lzf with lzfx

this is from google before the snappy compression library, supposedly 100% compatible with existing lzf data both in compressing and decompressing
but supposedly alot faster doing both
This commit is contained in:
Alug committed 2025-09-15 20:31:12 +02:00
1 parent 88f1aab86f
commit 4591e6ed87
13 files changed
+545 -721

No files matched your search

+2 -2
View File
@@ -22,13 +22,13 @@ LOCAL_SRC_FILES := am_map.c \
hu_stuff.c \
i_tcp.c \
info.c \
lzf.c \
lzfx.c \
m_argv.c \
m_bbox.c \
m_cheat.c \
m_fixed.c \
m_menu.c \
m_textinput.c \
m_textinput.c \
m_misc.c \
m_queue.c \
m_random.c \
+2 -2
View File
@@ -22,7 +22,7 @@ set(SRB2_CORE_SOURCES
hu_stuff.c
i_tcp.c
info.c
lzf.c
lzfx.c
m_aatree.c
m_anigif.c
m_argv.c
@@ -90,7 +90,7 @@ set(SRB2_CORE_HEADERS
i_video.h
info.h
keys.h
lzf.h
lzfx.h
m_aatree.h
m_anigif.h
m_argv.h
+1 -1
View File
@@ -19,7 +19,7 @@ hu_stuff.c
i_tcp.c
i_time.c
info.c
lzf.c
lzfx.c
m_aatree.c
m_anigif.c
m_argv.c
+1 -1
View File
@@ -45,7 +45,7 @@
#include "r_local.h"
#include "m_argv.h"
#include "p_setup.h"
#include "lzf.h"
#include "lzfx.h"
#include "lua_script.h"
#include "lua_hook.h"
#include "k_kart.h"
-1
View File
@@ -19,7 +19,6 @@
#include "m_misc.h"
#include "z_zone.h"
#include "d_player.h"
#include "lzf.h"
char sprnames[NUMSPRITES + 1][5] =
{
-619
View File
@@ -1,619 +0,0 @@
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 2000-2005 by Marc Alexander Lehmann <schmorp@schmorp.de>
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file lzf.c
/// \brief LZF de/compression routines
/* LZF decompression routines copied from lzf_d.c from liblzf 1.7 */
/* LZF compression routines copied from lzf_c.c from liblzf 1.7 */
/*
* lzfP.h included here by Graue.
*/
#ifndef LZFP_h
#define LZFP_h
#include "lzf.h"
#include "doomdef.h"
/*
* Size of hashtable is (1 << HLOG) * sizeof (char *)
* decompression is independent of the hash table size
* the difference between 15 and 14 is very small
* for small blocks (and 14 is usually a bit faster).
* For a low-memory/faster configuration, use HLOG == 13;
* For best compression, use 15 or 16 (or more).
*/
#ifndef HLOG
# define HLOG 15
#endif
/*
* Sacrifice very little compression quality in favour of compression speed.
* This gives almost the same compression as the default code, and is
* (very roughly) 15% faster. This is the preferred mode of operation.
*/
#ifndef VERY_FAST
# define VERY_FAST 1
#endif
/*
* Sacrifice some more compression quality in favour of compression speed.
* (roughly 1-2% worse compression for large blocks and
* 9-10% for small, redundant, blocks and >>20% better speed in both cases)
* In short: when in need for speed, enable this for binary data,
* possibly disable this for text data.
*/
#ifndef ULTRA_FAST
# define ULTRA_FAST 0
#endif
/*
* Unconditionally aligning does not cost very much, so do it if unsure
*/
#ifndef STRICT_ALIGN
#if !(defined(__i386) || defined (__amd64)) || defined (__clang__)
#define STRICT_ALIGN 1
#else
#define STRICT_ALIGN 0
#endif
#endif
/*
* You may choose to pre-set the hash table (might be faster on some
* modern cpus and large (>>64k) blocks, and also makes compression
* deterministic/repeatable when the configuration otherwise is the same).
*/
#ifndef INIT_HTAB
# define INIT_HTAB 1
#endif
/*
* Avoid assigning values to errno variable? for some embedding purposes
* (linux kernel for example), this is necessary. NOTE: this breaks
* the documentation in lzf.h. Avoiding errno has no speed impact.
*/
#ifndef AVOID_ERRNO
# define AVOID_ERRNO 0
#endif
/*
* Whether to pass the LZF_STATE variable as argument, or allocate it
* on the stack. For small-stack environments, define this to 1.
* NOTE: this breaks the prototype in lzf.h.
*/
#ifndef LZF_STATE_ARG
# define LZF_STATE_ARG 0
#endif
/*
* Whether to add extra checks for input validity in lzf_decompress
* and return EINVAL if the input stream has been corrupted. This
* only shields against overflowing the input buffer and will not
* detect most corrupted streams.
* This check is not normally noticeable on modern hardware
* (<1% slowdown), but might slow down older cpus considerably.
*/
#ifndef CHECK_INPUT
# define CHECK_INPUT 1
#endif
/*
* Whether the target CPU has a slow multiplication. This affects
* the default hash function for the compressor, and enables a slightly
* worse hash function that needs only shifts.
*/
#ifndef MULTIPLICATION_IS_SLOW
# define MULTIPLICATION_IS_SLOW 0
#endif
/*
* If defined, then this data type will be used for storing offsets.
* This can be useful if you want to use a huge hashtable, want to
* conserve memory, or both, and your data fits into e.g. 64kb.
* If instead you want to compress data > 4GB, then it's better to
* to "#define LZF_USE_OFFSETS 0" instead.
*/
/*#define LZF_HSLOT unsigned short*/
/*
* Whether to store pointers or offsets inside the hash table. On
* 64 bit architetcures, pointers take up twice as much space,
* and might also be slower. Default is to autodetect.
*/
/*#define LZF_USE_OFFSETS autodetect */
/*
* Whether to optimise code for size, at the expense of speed. Use
* this when you are extremely tight on memory, perhaps in combination
* with AVOID_ERRNO 1 and CHECK_INPUT 0.
*/
#ifndef OPTIMISE_SIZE
# ifdef __OPTIMIZE_SIZE__
# define OPTIMISE_SIZE 1
# else
# define OPTIMISE_SIZE 0
# endif
#endif
/*****************************************************************************/
/* nothing should be changed below */
# include <string.h>
# include <limits.h>
#if ULTRA_FAST
# undef VERY_FAST
#endif
#ifndef LZF_USE_OFFSETS
# ifdef _WIN32
# define LZF_USE_OFFSETS defined(_M_X64)
# else
# include <stdint.h>
# define LZF_USE_OFFSETS (UINTPTR_MAX > 0xffffffffU)
# endif
#endif
typedef unsigned char u8;
#ifdef LZF_HSLOT
# define LZF_HSLOT_BIAS ((const u8 *)in_data)
#else
# ifdef LZF_USE_OFFSETS
# define LZF_HSLOT_BIAS ((const u8 *)in_data)
typedef unsigned int LZF_HSLOT;
# else
# define LZF_HSLOT_BIAS 0
typedef const u8 *LZF_HSLOT;
# endif
#endif
#if USHRT_MAX == 65535
typedef unsigned short u16;
#elif UINT_MAX == 65535
typedef unsigned int u16;
#else
# undef STRICT_ALIGN
# define STRICT_ALIGN 1
#endif
#define LZF_MAX_LIT (1 << 5)
#define LZF_MAX_OFF (1 << 13)
#define LZF_MAX_REF ((1 << 8) + (1 << 3))
typedef LZF_HSLOT LZF_STATE[1 << (HLOG)];
typedef struct
{
const u8 *first [1 << (6+8)]; /* most recent occurance of a match */
u16 prev [LZF_MAX_OFF]; /* how many bytes to go backwards for the next match */
} LZF_STATE_BEST[1];
#endif
/*
* lzfP.h ends here. lzf_d.c follows.
*/
#if AVOID_ERRNO
# define SET_ERRNO(n)
#else
# include <errno.h>
# define SET_ERRNO(n) errno = (n)
#endif
#ifndef USE_REP_MOVSB
#if (defined (__i386) || defined (__amd64)) && __GNUC__ >= 3
#define USE_REP_MOVSB 1
#else
#define USE_REP_MOVSB 0 // Default to 0 if not defined elsewhere
#endif
#endif
#if USE_REP_MOVSB /* small win on amd, big loss on intel */
#if (defined (__i386) || defined (__amd64)) && __GNUC__ >= 3
# define lzf_movsb(dst, src, len) \
asm ("rep movsb" \
: "=D" (dst), "=S" (src), "=c" (len) \
: "0" (dst), "1" (src), "2" (len));
#endif
#endif
size_t
lzf_decompress (const void *const in_data, size_t in_len,
void *out_data, size_t out_len)
{
u8 const *ip = (const u8 *)in_data;
u8 *op = (u8 *)out_data;
u8 const *const in_end = ip + in_len;
u8 *const out_end = op + out_len;
do
{
unsigned int ctrl = *ip++;
if (ctrl < (1 << 5)) /* literal run */
{
ctrl++;
if (op + ctrl > out_end)
{
SET_ERRNO (E2BIG);
return 0;
}
#if CHECK_INPUT
if (ip + ctrl > in_end)
{
SET_ERRNO (EINVAL);
return 0;
}
#endif
#ifdef lzf_movsb
lzf_movsb (op, ip, ctrl);
#elif OPTIMISE_SIZE
while (ctrl--)
*op++ = *ip++;
#else
switch (ctrl)
{
case 32: *op++ = *ip++; case 31: *op++ = *ip++; case 30: *op++ = *ip++; case 29: *op++ = *ip++;
case 28: *op++ = *ip++; case 27: *op++ = *ip++; case 26: *op++ = *ip++; case 25: *op++ = *ip++;
case 24: *op++ = *ip++; case 23: *op++ = *ip++; case 22: *op++ = *ip++; case 21: *op++ = *ip++;
case 20: *op++ = *ip++; case 19: *op++ = *ip++; case 18: *op++ = *ip++; case 17: *op++ = *ip++;
case 16: *op++ = *ip++; case 15: *op++ = *ip++; case 14: *op++ = *ip++; case 13: *op++ = *ip++;
case 12: *op++ = *ip++; case 11: *op++ = *ip++; case 10: *op++ = *ip++; case 9: *op++ = *ip++;
case 8: *op++ = *ip++; case 7: *op++ = *ip++; case 6: *op++ = *ip++; case 5: *op++ = *ip++;
case 4: *op++ = *ip++; case 3: *op++ = *ip++; case 2: *op++ = *ip++; case 1: *op++ = *ip++;
}
#endif
}
else /* back reference */
{
unsigned int len = ctrl >> 5;
u8 *ref = op - ((ctrl & 0x1f) << 8) - 1;
#if CHECK_INPUT
if (ip >= in_end)
{
SET_ERRNO (EINVAL);
return 0;
}
#endif
if (len == 7)
{
len += *ip++;
#if CHECK_INPUT
if (ip >= in_end)
{
SET_ERRNO (EINVAL);
return 0;
}
#endif
}
ref -= *ip++;
if (op + len + 2 > out_end)
{
SET_ERRNO (E2BIG);
return 0;
}
if (ref < (u8 *)out_data)
{
SET_ERRNO (EINVAL);
return 0;
}
#ifdef lzf_movsb
len += 2;
lzf_movsb (op, ref, len);
#elif OPTIMISE_SIZE
len += 2;
do
*op++ = *ref++;
while (--len);
#else
switch (len)
{
default:
len += 2;
if (op >= ref + len)
{
/* disjunct areas */
memcpy (op, ref, len);
op += len;
}
else
{
/* overlapping, use octet by octet copying */
do
*op++ = *ref++;
while (--len);
}
break;
case 9: *op++ = *ref++;
case 8: *op++ = *ref++;
case 7: *op++ = *ref++;
case 6: *op++ = *ref++;
case 5: *op++ = *ref++;
case 4: *op++ = *ref++;
case 3: *op++ = *ref++;
case 2: *op++ = *ref++;
case 1: *op++ = *ref++;
case 0: *op++ = *ref++; /* two octets more */
*op++ = *ref++;
}
#endif
}
}
while (ip < in_end);
return op - (u8 *)out_data;
}
/*
* lzf_d.c ends here. lzf_c.c follows.
*/
#define HSIZE (1 << (HLOG))
/*
* don't play with this unless you benchmark!
* the data format is not dependent on the hash function.
* the hash function might seem strange, just believe me,
* it works ;)
*/
#ifndef FRST
# define FRST(p) (((p[0]) << 8) | p[1])
# define NEXT(v,p) (((v) << 8) | p[2])
# if MULTIPLICATION_IS_SLOW
# if ULTRA_FAST
# define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1))
# elif VERY_FAST
# define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
# else
# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
# endif
# else
/* this one was developed with sesse,
* and is very similar to the one in snappy.
* it does need a modern enough cpu with a fast multiplication.
*/
# define IDX(h) (((h * 0x1e35a7bdU) >> (32 - HLOG - 8)) & (HSIZE - 1))
# endif
#endif
#if 0
/* original lzv-like hash function, much worse and thus slower */
# define FRST(p) (p[0] << 5) ^ p[1]
# define NEXT(v,p) ((v) << 5) ^ p[2]
# define IDX(h) ((h) & (HSIZE - 1))
#endif
#if __GNUC__ >= 3
# define expect(expr,value) __builtin_expect ((expr),(value))
# define inline inline
#else
# define expect(expr,value) (expr)
# define inline static
#endif
#define expect_false(expr) expect ((expr) != 0, 0)
#define expect_true(expr) expect ((expr) != 0, 1)
/*
* compressed format
*
* 000LLLLL <L+1> ; literal, L+1=1..33 octets
* LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
* 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
*
*/
size_t
lzf_compress (const void *const in_data, size_t in_len,
void *out_data, size_t out_len
#if LZF_STATE_ARG
, LZF_STATE htab
#endif
)
{
#if !LZF_STATE_ARG
LZF_STATE htab;
#endif
const u8 *ip = (const u8 *)in_data;
u8 *op = (u8 *)out_data;
const u8 *in_end = ip + in_len;
u8 *out_end = op + out_len;
const u8 *ref;
/*
* off requires a type wide enough to hold a general pointer difference.
* ISO C doesn't have that (size_t might not be enough and ptrdiff_t only
* works for differences within a single object). We also assume that
* no bit pattern traps. Since the only platform that is both non-POSIX
* and fails to support both assumptions is windows 64 bit, we make a
* special workaround for it.
*/
size_t off;
unsigned int hval;
int lit;
if (!in_len || !out_len)
return 0;
#if INIT_HTAB
memset (htab, 0, sizeof (htab));
#endif
lit = 0; op++; /* start run */
hval = FRST (ip);
while (ip < in_end - 2)
{
LZF_HSLOT *hslot;
hval = NEXT (hval, ip);
hslot = htab + IDX (hval);
ref = *hslot + LZF_HSLOT_BIAS; *hslot = ip - LZF_HSLOT_BIAS;
if (1
#if INIT_HTAB
&& ref < ip /* the next test will actually take care of this, but this is faster */
#endif
&& (off = ip - ref - 1) < LZF_MAX_OFF
&& ref > (const u8 *)in_data
&& ref[2] == ip[2]
#if STRICT_ALIGN
&& ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
#else
&& *(const u16 *)ref == *(const u16 *)ip
#endif
)
{
/* match found at *ref++ */
unsigned int len = 2;
size_t maxlen = in_end - ip - len;
maxlen = maxlen > LZF_MAX_REF ? LZF_MAX_REF : maxlen;
if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
return 0;
op [- lit - 1] = lit - 1; /* stop run */
op -= !lit; /* undo run if length is zero */
for (;;)
{
if (expect_true (maxlen > 16))
{
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
}
do
len++;
while (len < maxlen && ref[len] == ip[len]);
break;
}
len -= 2; /* len is now #octets - 1 */
ip++;
if (len < 7)
{
*op++ = (off >> 8) + (len << 5);
}
else
{
*op++ = (off >> 8) + ( 7 << 5);
*op++ = len - 7;
}
*op++ = off;
lit = 0; op++; /* start run */
ip += len + 1;
if (expect_false (ip >= in_end - 2))
break;
#if ULTRA_FAST || VERY_FAST
--ip;
# if VERY_FAST && !ULTRA_FAST
--ip;
# endif
hval = FRST (ip);
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
# if VERY_FAST && !ULTRA_FAST
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
# endif
#else
ip -= len + 1;
do
{
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
}
while (len--);
#endif
}
else
{
/* one more literal byte we must copy */
if (expect_false (op >= out_end))
return 0;
lit++; *op++ = *ip++;
if (expect_false (lit == LZF_MAX_LIT))
{
op [- lit - 1] = lit - 1; /* stop run */
lit = 0; op++; /* start run */
}
}
}
if (op + 3 > out_end) /* at most 3 bytes can be missing here */
return 0;
while (ip < in_end)
{
lit++; *op++ = *ip++;
if (expect_false (lit == LZF_MAX_LIT))
{
op [- lit - 1] = lit - 1; /* stop run */
lit = 0; op++; /* start run */
}
}
op [- lit - 1] = lit - 1; /* end run */
op -= !lit; /* undo run if length is zero */
return op - (u8 *)out_data;
}
-75
View File
@@ -1,75 +0,0 @@
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (c) 2000-2010 Marc Alexander Lehmann <schmorp@schmorp.de>
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file lzf.h
/// \brief LZF de/compression routines
#ifndef LZF_H
#define LZF_H
#include <stdio.h>
/***********************************************************************
**
** lzf -- an extremely fast/free compression/decompression-method
** http://liblzf.plan9.de/
**
** This algorithm is believed to be patent-free.
**
***********************************************************************/
#define LZF_VERSION 0x0106 /* 1.6 */
/*
* Compress in_len bytes stored at the memory block starting at
* in_data and write the result to out_data, up to a maximum length
* of out_len bytes.
*
* If the output buffer is not large enough or any error occurs
* return 0, otherwise return the number of bytes used (which might
* be considerably larger than in_len, so it makes sense to always
* use out_len == in_len - 1), to ensure _some_ compression, and store
* the data uncompressed otherwise.
*
* lzf_compress might use different algorithms on different systems and
* even diferent runs, thus might result in different compressed strings
* depending on the phase of the moon or similar factors. However, all
* these strings are architecture-independent and will result in the
* original data when decompressed using lzf_decompress.
*
* The buffers must not be overlapping.
*
* If the option LZF_STATE_ARG is enabled, an extra argument must be
* supplied which is not reflected in this header file. Refer to lzfP.h
* and lzf_c.c.
*
*/
size_t
lzf_compress (const void *const in_data, size_t in_len,
void *out_data, size_t out_len);
/*
* Decompress data compressed with some version of the lzf_compress
* function and stored at location in_data and length in_len. The result
* will be stored at out_data up to a maximum of out_len characters.
*
* If the output buffer is not large enough to hold the decompressed
* data, a 0 is returned and errno is set to E2BIG. Otherwise the number
* of decompressed bytes (i.e. the original length of the data) is
* returned.
*
* If an error in the compressed data is detected, a zero is returned and
* errno is set to EINVAL.
*
* This function is very fast, about as fast as a copying loop.
*/
size_t
lzf_decompress (const void *const in_data, size_t in_len,
void *out_data, size_t out_len);
#endif
+425
View File
@@ -0,0 +1,425 @@
/*
* Copyright (c) 2009 Andrew Collette <andrew.collette at gmail.com>
* http://lzfx.googlecode.com
*
* Implements an LZF-compatible compressor/decompressor based on the liblzf
* codebase written by Marc Lehmann. This code is released under the BSD
* license. License and original copyright statement follow.
*
*
* Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LZFP_h
#define LZFP_h
#include "lzfx.h"
#define LZFX_HSIZE (1 << (LZFX_HLOG))
/* We need this for memset */
#ifdef __cplusplus
# include <cstring>
#else
# include <string.h>
#endif
#if __GNUC__ >= 3
# define fx_expect_false(expr) __builtin_expect((expr) != 0, 0)
# define fx_expect_true(expr) __builtin_expect((expr) != 0, 1)
#else
# define fx_expect_false(expr) (expr)
# define fx_expect_true(expr) (expr)
#endif
typedef unsigned char u8;
typedef const u8 *LZSTATE[LZFX_HSIZE];
/* Define the hash function */
#define LZFX_FRST(p) (((p[0]) << 8) | p[1])
#define LZFX_NEXT(v,p) (((v) << 8) | p[2])
#define LZFX_IDX(h) ((( h >> (3*8 - LZFX_HLOG)) - h ) & (LZFX_HSIZE - 1))
/* These cannot be changed, as they are related to the compressed format. */
#define LZFX_MAX_LIT (1 << 5)
#define LZFX_MAX_OFF (1 << 13)
#define LZFX_MAX_REF ((1 << 8) + (1 << 3))
#include <errno.h>
#define SET_ERRNO(n) errno = (n)
// lug: Changed to return the output lenght instead of of modifying the pointer directly
// Changed return values aswell, 0 on error, and output lenght on success to accomidate for kart
static size_t lzfx_getsize(const void* ibuf, size_t ilen);
/* Compressed format
There are two kinds of structures in LZF/LZFX: literal runs and back
references. The length of a literal run is encoded as L - 1, as it must
contain at least one byte. Literals are encoded as follows:
000LLLLL <L+1 bytes>
Back references are encoded as follows. The smallest possible encoded
length value is 1, as otherwise the control byte would be recognized as
a literal run. Since at least three bytes must match for a back reference
to be inserted, the length is encoded as L - 2 instead of L - 1. The
offset (distance to the desired data in the output buffer) is encoded as
o - 1, as all offsets are at least 1. The binary format is:
LLLooooo oooooooo for backrefs of real length < 9 (1 <= L < 7)
111ooooo LLLLLLLL oooooooo for backrefs of real length >= 9 (L > 7)
*/
size_t lzfx_compress(const void *const ibuf, const size_t ilen, void *obuf, size_t const olen)
{
/* Hash table; an array of u8*'s which point
to various locations in the input buffer */
const u8 *htab[LZFX_HSIZE];
const u8 **hslot; /* Pointer to entry in hash table */
unsigned int hval; /* Hash value generated by macros above */
const u8 *ref; /* Pointer to candidate match location in input */
const u8 *ip = (const u8 *)ibuf;
const u8 *const in_end = ip + ilen;
u8 *op = (u8 *)obuf;
const u8 *const out_end = (op + olen);
int lit; /* # of bytes in current literal run */
#if defined (WIN32) && defined (_M_X64)
unsigned _int64 off; /* workaround for missing POSIX compliance */
#else
unsigned long off;
#endif
if (olen == 0)
{
SET_ERRNO(EINVAL);
return 0;
}
if (ibuf == NULL)
{
if (ilen != 0)
SET_ERRNO(EINVAL);
return 0;
}
if (obuf == NULL)
{
SET_ERRNO(EINVAL);
return 0;
}
memset(htab, 0, sizeof(htab));
/* Start a literal run. Whenever we do this the output pointer is
advanced because the current byte will hold the encoded length. */
lit = 0; op++;
hval = LZFX_FRST(ip);
while (ip + 2 < in_end) /* The NEXT macro reads 2 bytes ahead */
{
hval = LZFX_NEXT(hval, ip);
hslot = htab + LZFX_IDX(hval);
ref = *hslot; *hslot = ip;
if (ref < ip
&& (off = ip - ref - 1) < LZFX_MAX_OFF
&& ip + 4 < in_end /* Backref takes up to 3 bytes, so don't bother */
&& ref > (u8 *)ibuf
&& ref[0] == ip[0]
&& ref[1] == ip[1]
&& ref[2] == ip[2] )
{
unsigned int len = 3; /* We already know 3 bytes match */
const unsigned int maxlen = in_end - ip - 2 > LZFX_MAX_REF ?
LZFX_MAX_REF : in_end - ip - 2;
/* lit == 0: op + 3 must be < out_end (because we undo the run)
lit != 0: op + 3 + 1 must be < out_end */
if (fx_expect_false(op - !lit + 3 + 1 >= out_end))
{
SET_ERRNO(E2BIG);
return 0;
}
op [- lit - 1] = lit - 1; /* Terminate literal run */
op -= !lit; /* Undo run if length is zero */
/* Start checking at the fourth byte */
while (len < maxlen && ref[len] == ip[len])
len++;
len -= 2; /* We encode the length as #octets - 2 */
/* Format 1: [LLLooooo oooooooo] */
if (len < 7)
{
*op++ = (off >> 8) + (len << 5);
*op++ = off;
/* Format 2: [111ooooo LLLLLLLL oooooooo] */
}
else
{
*op++ = (off >> 8) + (7 << 5);
*op++ = len - 7;
*op++ = off;
}
lit = 0; op++;
ip += len + 1; /* ip = initial ip + #octets -1 */
if (fx_expect_false (ip + 3 >= in_end))
{
ip++; /* Code following expects exit at bottom of loop */
break;
}
hval = LZFX_FRST (ip);
hval = LZFX_NEXT (hval, ip);
htab[LZFX_IDX (hval)] = ip;
ip++; /* ip = initial ip + #octets */
}
else
{
/* Keep copying literal bytes */
if (fx_expect_false (op >= out_end))
{
SET_ERRNO(E2BIG);
return 0;
}
lit++; *op++ = *ip++;
if (fx_expect_false (lit == LZFX_MAX_LIT))
{
op [- lit - 1] = lit - 1; /* stop run */
lit = 0; op++; /* start run */
}
} /* if() found match in htab */
} /* while(ip < ilen -2) */
/* At most 3 bytes remain in input. We therefore need 4 bytes available
in the output buffer to store them (3 data + ctrl byte).*/
if (op + 3 > out_end)
{
SET_ERRNO(E2BIG);
return 0;
}
while (ip < in_end)
{
lit++; *op++ = *ip++;
if (fx_expect_false (lit == LZFX_MAX_LIT))
{
op [- lit - 1] = lit - 1;
lit = 0; op++;
}
}
op [- lit - 1] = lit - 1;
op -= !lit;
return op - (u8 *)obuf;
}
/* Decompressor */
size_t lzfx_decompress(const void* ibuf, size_t ilen, void* obuf, size_t olen)
{
u8 const *ip = (const u8 *)ibuf;
u8 const *const in_end = ip + ilen;
u8 *op = (u8 *)obuf;
u8 const *const out_end = (op + olen);
size_t remain_len = 0;
if (olen == 0)
{
SET_ERRNO(EINVAL);
return 0;
}
if (ibuf == NULL)
{
if (ilen != 0)
SET_ERRNO(EINVAL);
return 0;
}
if (obuf == NULL)
{
if (olen != 0)
{
SET_ERRNO(EINVAL);
return 0;
}
return lzfx_getsize(ibuf, ilen);
}
do {
unsigned int ctrl = *ip++;
/* Format 000LLLLL: a literal byte string follows, of length L+1 */
if (ctrl < (1 << 5))
{
ctrl++;
if (fx_expect_false(op + ctrl > out_end))
{
--ip; /* Rewind to control byte */
goto guess;
}
if (fx_expect_false(ip + ctrl > in_end))
{
SET_ERRNO(EINVAL);
return 0;
}
do
*op++ = *ip++;
while(--ctrl);
/* Format #1 [LLLooooo oooooooo]: backref of length L+1+2
^^^^^ ^^^^^^^^
A B
#2 [111ooooo LLLLLLLL oooooooo] backref of length L+7+2
^^^^^ ^^^^^^^^
A B
In both cases the location of the backref is computed from the
remaining part of the data as follows:
location = op - A*256 - B - 1
*/
}
else
{
unsigned int len = (ctrl >> 5);
u8 *ref = op - ((ctrl & 0x1f) << 8) -1;
if (len==7) /* i.e. format #2 */
len += *ip++;
len += 2; /* len is now #octets */
if (fx_expect_false(op + len > out_end))
{
ip -= (len >= 9) ? 2 : 1; /* Rewind to control byte */
goto guess;
}
if (fx_expect_false(ip >= in_end))
{
SET_ERRNO(EINVAL);
return 0;
}
ref -= *ip++;
if (fx_expect_false(ref < (u8*)obuf))
{
SET_ERRNO(EINVAL);
return 0;
}
do
*op++ = *ref++;
while (--len);
}
} while (ip < in_end);
return op - (u8 *)obuf;
guess:
remain_len = lzfx_getsize(ip, ilen - (ip-(u8*)ibuf));
if (remain_len > 0)
return remain_len + (op - (u8*)obuf);
return 0;
}
/* Guess len. No parameters may be NULL; this is not checked. */
static size_t lzfx_getsize(const void* ibuf, size_t ilen)
{
u8 const *ip = (const u8 *)ibuf;
u8 const *const in_end = ip + ilen;
int tot_len = 0;
while (ip < in_end)
{
unsigned int ctrl = *ip++;
if (ctrl < (1 << 5))
{
ctrl++;
if (ip + ctrl > in_end)
{
SET_ERRNO(EINVAL);
return 0;
}
tot_len += ctrl;
ip += ctrl;
}
else
{
unsigned int len = (ctrl >> 5);
if (len == 7) /* i.e. format #2 */
{
len += *ip++;
}
len += 2; /* len is now #octets */
if (ip >= in_end)
return 0;
ip++; /* skip the ref byte */
tot_len += len;
}
}
return tot_len;
}
#endif
+101
View File
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2009 Andrew Collette <andrew.collette at gmail.com>
* http://lzfx.googlecode.com
*
* Implements an LZF-compatible compressor/decompressor based on the liblzf
* codebase written by Marc Lehmann. This code is released under the BSD
* license. License and original copyright statement follow.
*
*
* Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LZFX_H
#define LZFX_H
#ifdef __cplusplus
extern "C" {
#endif
/* Documented behavior, including function signatures and error codes,
is guaranteed to remain unchanged for releases with the same major
version number. Releases of the same major version are also able
to read each other's output, although the output itself is not
guaranteed to be byte-for-byte identical.
*/
#define LZFX_VERSION_MAJOR 0
#define LZFX_VERSION_MINOR 1
#define LZFX_VERSION_STRING "0.1"
/* Hashtable size (2**LZFX_HLOG entries) */
#ifndef LZFX_HLOG
# define LZFX_HLOG 16
#endif
// lug: gotta let errno take care of those as i dont feel like rewriting a bunch of crap in karts handling of this
/* Predefined errors. */
//#define LZFX_ESIZE -1 /* Output buffer too small */
//#define LZFX_ECORRUPT -2 /* Invalid data for decompression */
//#define LZFX_EARGS -3 /* Arguments invalid (NULL) */
#include "doomdef.h"
/* Buffer-to buffer compression.
Supply pre-allocated input and output buffers via ibuf and obuf, and
their size in bytes via ilen and olen. Buffers may not overlap.
On success, the function returns a non-negative value and the argument
olen contains the compressed size in bytes. On failure, a negative
value is returned and olen is not modified.
*/
size_t lzfx_compress(const void* ibuf, const size_t ilen, void* obuf, size_t olen);
#define lzf_compress(ibuf, ilen, obuf, olen) lzfx_compress(ibuf, ilen, obuf, olen)
/* Buffer-to-buffer decompression.
Supply pre-allocated input and output buffers via ibuf and obuf, and
their size in bytes via ilen and olen. Buffers may not overlap.
On success, the function returns a non-negative value and the argument
olen contains the uncompressed size in bytes. On failure, a negative
value is returned.
If the failure code is LZFX_ESIZE, olen contains the minimum buffer size
required to hold the decompressed data. Otherwise, olen is not modified.
Supplying a zero *olen is a valid and supported strategy to determine the
required buffer size. This does not require decompression of the entire
stream and is consequently very fast. Argument obuf may be NULL in
this case only.
*/
size_t lzfx_decompress(const void* ibuf, const size_t ilen, void* obuf, size_t olen);
#define lzf_decompress(ibuf, ilen, obuf, olen) lzfx_decompress(ibuf, ilen, obuf, olen)
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
+2 -2
View File
@@ -984,11 +984,11 @@ SOURCE=..\sounds.h
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\lzf.c
SOURCE=..\lzfx.c
# End Source File
# Begin Source File
SOURCE=..\lzf.h
SOURCE=..\lzfx.h
# End Source File
# Begin Source File
+4 -11
View File
@@ -39,22 +39,15 @@
6755C7A91017FE2500A80195 = {
fileEncoding = 30;
isa = PBXFileReference;
name = lzf.c;
path = ../../lzf.c;
name = lzfx.c;
path = ../../lzfx.c;
refType = 2;
};
6755C7AA1017FE2500A80195 = {
fileEncoding = 30;
isa = PBXFileReference;
name = lzf.h;
path = ../../lzf.h;
refType = 2;
};
6755C7AB1017FE2500A80195 = {
fileEncoding = 30;
isa = PBXFileReference;
name = m_dllist.h;
path = ../../m_dllist.h;
name = lzfx.h;
path = ../../lzfx.h;
refType = 2;
};
6755C7AC1017FE2500A80195 = {
@@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */
002F39FA09D0881F00EBEB88 /* SDL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 002F39F909D0881F00EBEB88 /* SDL.framework */; };
002F3A0009D0884600EBEB88 /* SDL.framework in Copy Frameworks into .app bundle */ = {isa = PBXBuildFile; fileRef = 002F39F909D0881F00EBEB88 /* SDL.framework */; };
1E308E720B71172D0015728C /* lzf.c in Sources */ = {isa = PBXBuildFile; fileRef = 1E44B2240B67EADE00BAD059 /* lzf.c */; };
1E308E720B71172D0015728C /* lzfx.c in Sources */ = {isa = PBXBuildFile; fileRef = 1E44B2240B67EADE00BAD059 /* lzfx.c */; };
1E32C4290B6E6D5D0029E058 /* libpng.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E32C4140B6E6D5D0029E058 /* libpng.framework */; };
1E32C42B0B6E6D6E0029E058 /* libpng.framework in Copy Frameworks into .app bundle */ = {isa = PBXBuildFile; fileRef = 1E32C4140B6E6D5D0029E058 /* libpng.framework */; };
1E44AE750B67CC2B00BAD059 /* hw_bsp.c in Sources */ = {isa = PBXBuildFile; fileRef = 1E44AE610B67CC2B00BAD059 /* hw_bsp.c */; };
@@ -351,8 +351,8 @@
1E44AFEC0B67D0AB00BAD059 /* r_opengl.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = r_opengl.c; path = ../../hardware/r_opengl/r_opengl.c; sourceTree = SOURCE_ROOT; };
1E44B0560B67D81E00BAD059 /* SDL_macosx_main.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = SDL_macosx_main.h; path = ../SDL_main/SDL_macosx_main.h; sourceTree = SOURCE_ROOT; };
1E44B0570B67D81E00BAD059 /* SDL_macosx_main.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = SDL_macosx_main.m; path = ../SDL_main/SDL_macosx_main.m; sourceTree = SOURCE_ROOT; };
1E44B2240B67EADE00BAD059 /* lzf.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = lzf.c; path = ../../lzf.c; sourceTree = SOURCE_ROOT; };
1E44B2250B67EADE00BAD059 /* lzf.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = lzf.h; path = ../../lzf.h; sourceTree = SOURCE_ROOT; };
1E44B2240B67EADE00BAD059 /* lzfx.c */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.c; name = lzfx.c; path = ../../lzfx.c; sourceTree = SOURCE_ROOT; };
1E44B2250B67EADE00BAD059 /* lzfx.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = lzfx.h; path = ../../lzfx.h; sourceTree = SOURCE_ROOT; };
1E66921B0B690C5B00B7313A /* SDL_mixer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL_mixer.framework; path = /Library/Frameworks/SDL_mixer.framework; sourceTree = "<absolute>"; };
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
@@ -713,8 +713,8 @@
1E44AF170B67CE1000BAD059 /* W_Wad */ = {
isa = PBXGroup;
children = (
1E44B2240B67EADE00BAD059 /* lzf.c */,
1E44B2250B67EADE00BAD059 /* lzf.h */,
1E44B2240B67EADE00BAD059 /* lzfx.c */,
1E44B2250B67EADE00BAD059 /* lzfx.h */,
1E44AF1C0B67CE3600BAD059 /* md5.c */,
1E44AF1D0B67CE3600BAD059 /* md5.h */,
1E44AFA70B67CF6400BAD059 /* w_wad.c */,
@@ -1128,7 +1128,7 @@
1E44AFEB0B67D06200BAD059 /* mac_alert.c in Sources */,
1E44AFED0B67D0AB00BAD059 /* r_opengl.c in Sources */,
1E44B0590B67D81E00BAD059 /* SDL_macosx_main.m in Sources */,
1E308E720B71172D0015728C /* lzf.c in Sources */,
1E308E720B71172D0015728C /* lzfx.c in Sources */,
676BB5200E0DE06100C95963 /* m_queue.c in Sources */,
676BB5220E0DE06100C95963 /* p_polyobj.c in Sources */,
67B83BFA14F57EAB00AAAE4E /* lapi.c in Sources */,
+1 -1
View File
@@ -37,7 +37,7 @@
#ifdef ZWAD
#include <errno.h>
#include "lzf.h"
#include "lzfx.h"
#endif
#include "doomdef.h"