mirror of
https://github.com/Ralim/IronOS.git
synced 2025-02-26 07:53:55 +00:00
Reformat lzfx sources
This commit is contained in:
@@ -5,10 +5,8 @@
|
||||
|
||||
/* Program to demonstrate file compression. Don't use it in the real world! */
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc == 3)
|
||||
{
|
||||
int main(int argc, char **argv) {
|
||||
if (argc == 3) {
|
||||
/* open input */
|
||||
FILE *f = fopen(argv[1], "rb");
|
||||
if (!f) {
|
||||
@@ -43,9 +41,7 @@ int main(int argc, char **argv)
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
printf("Compresses a file.\n\nUsage: lzfx-raw input output\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -63,8 +63,7 @@ typedef const u8 *LZSTATE[LZFX_HSIZE];
|
||||
#define LZFX_MAX_OFF (1 << 13)
|
||||
#define LZFX_MAX_REF ((1 << 8) + (1 << 3) - 2)
|
||||
|
||||
static
|
||||
int lzfx_getsize(const void* ibuf, unsigned int ilen, unsigned int *olen);
|
||||
static int lzfx_getsize(const void *ibuf, unsigned int ilen, unsigned int *olen);
|
||||
|
||||
/* Compressed format
|
||||
|
||||
@@ -83,8 +82,7 @@ int lzfx_getsize(const void* ibuf, unsigned int ilen, unsigned int *olen);
|
||||
oooooLLL oooooooo for backrefs of real length < 7 (1 <= L < 7)
|
||||
ooooo111 LLLLLLLL oooooooo for backrefs of real length >= 7 (L >= 7)
|
||||
*/
|
||||
int lzfx_compress(const void *const ibuf, const unsigned int ilen,
|
||||
void *obuf, unsigned int *const olen){
|
||||
int lzfx_compress(const void *const ibuf, const unsigned int ilen, void *obuf, unsigned int *const olen) {
|
||||
|
||||
/* Hash table; an array of u8*'s which point
|
||||
to various locations in the input buffer */
|
||||
@@ -108,19 +106,23 @@ int lzfx_compress(const void *const ibuf, const unsigned int ilen,
|
||||
unsigned long off;
|
||||
#endif
|
||||
|
||||
if(olen == NULL) return LZFX_EARGS;
|
||||
if (olen == NULL)
|
||||
return LZFX_EARGS;
|
||||
if (ibuf == NULL) {
|
||||
if(ilen != 0) return LZFX_EARGS;
|
||||
if (ilen != 0)
|
||||
return LZFX_EARGS;
|
||||
*olen = 0;
|
||||
return 0;
|
||||
}
|
||||
if(obuf == NULL) return LZFX_EARGS;
|
||||
if (obuf == NULL)
|
||||
return LZFX_EARGS;
|
||||
|
||||
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++;
|
||||
lit = 0;
|
||||
op++;
|
||||
|
||||
hval = LZFX_FRST(ip);
|
||||
|
||||
@@ -129,19 +131,14 @@ int lzfx_compress(const void *const ibuf, const unsigned int ilen,
|
||||
hval = LZFX_NEXT(hval, ip);
|
||||
hslot = htab + LZFX_IDX(hval);
|
||||
|
||||
ref = *hslot; *hslot = ip;
|
||||
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] ) {
|
||||
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;
|
||||
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 */
|
||||
@@ -167,7 +164,8 @@ int lzfx_compress(const void *const ibuf, const unsigned int ilen,
|
||||
*op++ = off;
|
||||
}
|
||||
|
||||
lit = 0; op++;
|
||||
lit = 0;
|
||||
op++;
|
||||
|
||||
ip += len - 1; /* ip = initial ip + #octets - 1 */
|
||||
|
||||
@@ -185,13 +183,16 @@ int lzfx_compress(const void *const ibuf, const unsigned int ilen,
|
||||
} else {
|
||||
/* Keep copying literal bytes */
|
||||
|
||||
if (fx_expect_false (op >= out_end)) return LZFX_ESIZE;
|
||||
if (fx_expect_false(op >= out_end))
|
||||
return LZFX_ESIZE;
|
||||
|
||||
lit++; *op++ = *ip++;
|
||||
lit++;
|
||||
*op++ = *ip++;
|
||||
|
||||
if (fx_expect_false(lit == LZFX_MAX_LIT)) {
|
||||
op[-lit - 1] = lit << 3; /* stop run */
|
||||
lit = 0; op++; /* start run */
|
||||
lit = 0;
|
||||
op++; /* start run */
|
||||
}
|
||||
|
||||
} /* if() found match in htab */
|
||||
@@ -200,15 +201,18 @@ int lzfx_compress(const void *const ibuf, const unsigned int ilen,
|
||||
|
||||
/* 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) return LZFX_ESIZE;
|
||||
if (op + 3 > out_end)
|
||||
return LZFX_ESIZE;
|
||||
|
||||
while (ip < in_end) {
|
||||
|
||||
lit++; *op++ = *ip++;
|
||||
lit++;
|
||||
*op++ = *ip++;
|
||||
|
||||
if (fx_expect_false(lit == LZFX_MAX_LIT)) {
|
||||
op[-lit - 1] = lit << 3;
|
||||
lit = 0; op++;
|
||||
lit = 0;
|
||||
op++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,8 +224,7 @@ int lzfx_compress(const void *const ibuf, const unsigned int ilen,
|
||||
}
|
||||
|
||||
/* Decompressor */
|
||||
int lzfx_decompress(const void* ibuf, unsigned int ilen,
|
||||
void* obuf, unsigned int *olen){
|
||||
int lzfx_decompress(const void *ibuf, unsigned int ilen, void *obuf, unsigned int *olen) {
|
||||
|
||||
u8 const * ip = (const u8 *)ibuf;
|
||||
u8 const *const in_end = ip + ilen;
|
||||
@@ -231,14 +234,17 @@ int lzfx_decompress(const void* ibuf, unsigned int ilen,
|
||||
unsigned int remain_len = 0;
|
||||
int rc;
|
||||
|
||||
if(olen == NULL) return LZFX_EARGS;
|
||||
if (olen == NULL)
|
||||
return LZFX_EARGS;
|
||||
if (ibuf == NULL) {
|
||||
if(ilen != 0) return LZFX_EARGS;
|
||||
if (ilen != 0)
|
||||
return LZFX_EARGS;
|
||||
*olen = 0;
|
||||
return 0;
|
||||
}
|
||||
if (obuf == NULL) {
|
||||
if(olen != 0) return LZFX_EARGS;
|
||||
if (olen != 0)
|
||||
return LZFX_EARGS;
|
||||
return lzfx_getsize(ibuf, ilen, olen);
|
||||
}
|
||||
|
||||
@@ -253,7 +259,8 @@ int lzfx_decompress(const void* ibuf, unsigned int ilen,
|
||||
--ip; /* Rewind to control byte */
|
||||
goto guess;
|
||||
}
|
||||
if(fx_expect_false(ip + len > in_end)) return LZFX_ECORRUPT;
|
||||
if (fx_expect_false(ip + len > in_end))
|
||||
return LZFX_ECORRUPT;
|
||||
|
||||
do
|
||||
*op++ = *ip++;
|
||||
@@ -275,17 +282,20 @@ int lzfx_decompress(const void* ibuf, unsigned int ilen,
|
||||
unsigned int len = ctrl & 0x7;
|
||||
u8 * ref = op - ((ctrl >> 3) << 8) - 1;
|
||||
|
||||
if(len==7) len += *ip++; /* i.e. format #2 */
|
||||
if (len == 7)
|
||||
len += *ip++; /* i.e. format #2 */
|
||||
|
||||
if (fx_expect_false(op + len > out_end)) {
|
||||
ip -= (len >= 7) ? 2 : 1; /* Rewind to control byte */
|
||||
goto guess;
|
||||
}
|
||||
if(fx_expect_false(ip >= in_end)) return LZFX_ECORRUPT;
|
||||
if (fx_expect_false(ip >= in_end))
|
||||
return LZFX_ECORRUPT;
|
||||
|
||||
ref -= *ip++;
|
||||
|
||||
if(fx_expect_false(ref < (u8*)obuf)) return LZFX_ECORRUPT;
|
||||
if (fx_expect_false(ref < (u8 *)obuf))
|
||||
return LZFX_ECORRUPT;
|
||||
|
||||
do
|
||||
*op++ = *ref++;
|
||||
@@ -300,13 +310,13 @@ int lzfx_decompress(const void* ibuf, unsigned int ilen,
|
||||
|
||||
guess:
|
||||
rc = lzfx_getsize(ip, ilen - (ip - (u8 *)ibuf), &remain_len);
|
||||
if(rc>=0) *olen = remain_len + (op - (u8*)obuf);
|
||||
if (rc >= 0)
|
||||
*olen = remain_len + (op - (u8 *)obuf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Guess len. No parameters may be NULL; this is not checked. */
|
||||
static
|
||||
int lzfx_getsize(const void* ibuf, unsigned int ilen, unsigned int *olen){
|
||||
static int lzfx_getsize(const void *ibuf, unsigned int ilen, unsigned int *olen) {
|
||||
|
||||
u8 const * ip = (const u8 *)ibuf;
|
||||
u8 const *const in_end = ip + ilen;
|
||||
@@ -332,21 +342,16 @@ int lzfx_getsize(const void* ibuf, unsigned int ilen, unsigned int *olen){
|
||||
len += *ip++;
|
||||
}
|
||||
|
||||
if(ip >= in_end) return LZFX_ECORRUPT;
|
||||
if (ip >= in_end)
|
||||
return LZFX_ECORRUPT;
|
||||
|
||||
ip++; /* skip the ref byte */
|
||||
|
||||
tot_len += len;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*olen = tot_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -67,8 +67,7 @@ extern "C" {
|
||||
olen contains the compressed size in bytes. On failure, a negative
|
||||
value is returned and olen is not modified.
|
||||
*/
|
||||
int lzfx_compress(const void* ibuf, unsigned int ilen,
|
||||
void* obuf, unsigned int *olen);
|
||||
int lzfx_compress(const void *ibuf, unsigned int ilen, void *obuf, unsigned int *olen);
|
||||
|
||||
/* Buffer-to-buffer decompression.
|
||||
|
||||
@@ -87,9 +86,7 @@ int lzfx_compress(const void* ibuf, unsigned int ilen,
|
||||
stream and is consequently very fast. Argument obuf may be NULL in
|
||||
this case only.
|
||||
*/
|
||||
int lzfx_decompress(const void* ibuf, unsigned int ilen,
|
||||
void* obuf, unsigned int *olen);
|
||||
|
||||
int lzfx_decompress(const void *ibuf, unsigned int ilen, void *obuf, unsigned int *olen);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
|
||||
Reference in New Issue
Block a user