contrib: Import FastLZ 0.1.0

URL
https://github.com/ariya/FastLZ/

Hash
f1217348a868bdb9ee0730244475aee05ab329c5
This commit is contained in:
Amar Takhar
2025-07-31 17:51:28 -05:00
committed by Kinsey Moore
parent d509bbd57e
commit f00a819274
6 changed files with 1864 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+478
View File
@@ -0,0 +1,478 @@
/*
6PACK - file compressor using FastLZ (lightning-fast compression library)
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIXPACK_VERSION_MAJOR 0
#define SIXPACK_VERSION_MINOR 1
#define SIXPACK_VERSION_REVISION 0
#define SIXPACK_VERSION_STRING "0.1.0"
#include "fastlz.h"
#if defined(WIN32) || defined(__NT__) || defined(_WIN32) || defined(__WIN32__)
#if defined(__BORLANDC__) || defined(_MSC_VER)
#define inline __inline
#endif
#endif
/* magic identifier for 6pack file */
static unsigned char sixpack_magic[8] = {137, '6', 'P', 'K', 13, 10, 26, 10};
#define BLOCK_SIZE 65536
/* prototypes */
static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len);
void usage(void);
int detect_magic(FILE *f);
static inline unsigned long readU16(const unsigned char* ptr);
static inline unsigned long readU32(const unsigned char* ptr);
void read_chunk_header(FILE* f, int* id, int* options, unsigned long* size,
unsigned long* checksum, unsigned long* extra);
int unpack_file(const char* archive_file);
/* for Adler-32 checksum algorithm, see RFC 1950 Section 8.2 */
#define ADLER32_BASE 65521
static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len)
{
const unsigned char* ptr = (const unsigned char*)buf;
unsigned long s1 = checksum & 0xffff;
unsigned long s2 = (checksum >> 16) & 0xffff;
while(len>0)
{
unsigned k = len < 5552 ? len : 5552;
len -= k;
while(k >= 8)
{
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
k -= 8;
}
while(k-- > 0)
{
s1 += *ptr++; s2 += s1;
}
s1 = s1 % ADLER32_BASE;
s2 = s2 % ADLER32_BASE;
}
return (s2 << 16) + s1;
}
void usage(void)
{
printf("6unpack: uncompress 6pack archive\n");
printf("Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)\n");
printf("\n");
printf("Usage: 6unpack archive-file\n");
printf("\n");
}
/* return non-zero if magic sequence is detected */
/* warning: reset the read pointer to the beginning of the file */
int detect_magic(FILE *f)
{
unsigned char buffer[8];
size_t bytes_read;
int c;
fseek(f, SEEK_SET, 0);
bytes_read = fread(buffer, 1, 8, f);
fseek(f, SEEK_SET, 0);
if(bytes_read < 8)
return 0;
for(c = 0; c < 8; c++)
if(buffer[c] != sixpack_magic[c])
return 0;
return -1;
}
static inline unsigned long readU16( const unsigned char* ptr )
{
return ptr[0]+(ptr[1]<<8);
}
static inline unsigned long readU32( const unsigned char* ptr )
{
return ptr[0]+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24);
}
void read_chunk_header(FILE* f, int* id, int* options, unsigned long* size,
unsigned long* checksum, unsigned long* extra)
{
unsigned char buffer[16];
fread(buffer, 1, 16, f);
*id = readU16(buffer) & 0xffff;
*options = readU16(buffer+2) & 0xffff;
*size = readU32(buffer+4) & 0xffffffff;
*checksum = readU32(buffer+8) & 0xffffffff;
*extra = readU32(buffer+12) & 0xffffffff;
}
int unpack_file(const char* input_file)
{
FILE* in;
unsigned long fsize;
int c;
unsigned long percent;
unsigned char progress[20];
int chunk_id;
int chunk_options;
unsigned long chunk_size;
unsigned long chunk_checksum;
unsigned long chunk_extra;
unsigned char buffer[BLOCK_SIZE];
unsigned long checksum;
unsigned long decompressed_size;
unsigned long total_extracted;
int name_length;
char* output_file;
FILE* f;
unsigned char* compressed_buffer;
unsigned char* decompressed_buffer;
unsigned long compressed_bufsize;
unsigned long decompressed_bufsize;
/* sanity check */
in = fopen(input_file, "rb");
if(!in)
{
printf("Error: could not open %s\n", input_file);
return -1;
}
/* find size of the file */
fseek(in, 0, SEEK_END);
fsize = ftell(in);
fseek(in, 0, SEEK_SET);
/* not a 6pack archive? */
if(!detect_magic(in))
{
fclose(in);
printf("Error: file %s is not a 6pack archive!\n", input_file);
return -1;
}
printf("Archive: %s", input_file);
/* position of first chunk */
fseek(in, 8, SEEK_SET);
/* initialize */
output_file = 0;
f = 0;
total_extracted = 0;
decompressed_size = 0;
percent = 0;
compressed_buffer = 0;
decompressed_buffer = 0;
compressed_bufsize = 0;
decompressed_bufsize = 0;
/* main loop */
for(;;)
{
/* end of file? */
size_t pos = ftell(in);
if(pos >= fsize)
break;
read_chunk_header(in, &chunk_id, &chunk_options,
&chunk_size, &chunk_checksum, &chunk_extra);
if((chunk_id == 1) && (chunk_size > 10) && (chunk_size < BLOCK_SIZE))
{
/* close current file, if any */
printf("\n");
free(output_file);
output_file = 0;
if(f)
fclose(f);
/* file entry */
fread(buffer, 1, chunk_size, in);
checksum = update_adler32(1L, buffer, chunk_size);
if(checksum != chunk_checksum)
{
free(output_file);
output_file = 0;
fclose(in);
printf("\nError: checksum mismatch!\n");
printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
return -1;
}
decompressed_size = readU32(buffer);
total_extracted = 0;
percent = 0;
/* get file to extract */
name_length = (int)readU16(buffer+8);
if(name_length > (int)chunk_size - 10)
name_length = chunk_size - 10;
output_file = (char*)malloc(name_length+1);
memset(output_file, 0, name_length+1);
for(c = 0; c < name_length; c++)
output_file[c] = buffer[10+c];
/* check if already exists */
f = fopen(output_file, "rb");
if(f)
{
fclose(f);
printf("File %s already exists. Skipped.\n", output_file);
free(output_file);
output_file = 0;
f = 0;
}
else
{
/* create the file */
f = fopen(output_file, "wb");
if(!f)
{
printf("Can't create file %s. Skipped.\n", output_file);
free(output_file);
output_file = 0;
f = 0;
}
else
{
/* for progress status */
printf("\n");
memset(progress, ' ', 20);
if(strlen(output_file) < 16)
for(c = 0; c < (int)strlen(output_file); c++)
progress[c] = output_file[c];
else
{
for(c = 0; c < 13; c++)
progress[c] = output_file[c];
progress[13] = '.';
progress[14] = '.';
progress[15] = ' ';
}
progress[16] = '[';
progress[17] = 0;
printf("%s", progress);
for(c = 0; c < 50; c++)
printf(".");
printf("]\r");
printf("%s", progress);
}
}
}
if((chunk_id == 17) && f && output_file && decompressed_size)
{
unsigned long remaining;
/* uncompressed */
switch(chunk_options)
{
/* stored, simply copy to output */
case 0:
/* read one block at at time, write and update checksum */
total_extracted += chunk_size;
remaining = chunk_size;
checksum = 1L;
for(;;)
{
unsigned long r = (BLOCK_SIZE < remaining) ? BLOCK_SIZE: remaining;
size_t bytes_read = fread(buffer, 1, r, in);
if(bytes_read == 0)
break;
fwrite(buffer, 1, bytes_read, f);
checksum = update_adler32(checksum, buffer, bytes_read);
remaining -= bytes_read;
}
/* verify everything is written correctly */
if(checksum != chunk_checksum)
{
fclose(f);
f = 0;
free(output_file);
output_file = 0;
printf("\nError: checksum mismatch. Aborted.\n");
printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
}
break;
/* compressed using FastLZ */
case 1:
/* enlarge input buffer if necessary */
if(chunk_size > compressed_bufsize)
{
compressed_bufsize = chunk_size;
free(compressed_buffer);
compressed_buffer = (unsigned char*)malloc(compressed_bufsize);
}
/* enlarge output buffer if necessary */
if(chunk_extra > decompressed_bufsize)
{
decompressed_bufsize = chunk_extra;
free(decompressed_buffer);
decompressed_buffer = (unsigned char*)malloc(decompressed_bufsize);
}
/* read and check checksum */
fread(compressed_buffer, 1, chunk_size, in);
checksum = update_adler32(1L, compressed_buffer, chunk_size);
total_extracted += chunk_extra;
/* verify that the chunk data is correct */
if(checksum != chunk_checksum)
{
fclose(f);
f = 0;
free(output_file);
output_file = 0;
printf("\nError: checksum mismatch. Skipped.\n");
printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
}
else
{
/* decompress and verify */
remaining = fastlz_decompress(compressed_buffer, chunk_size, decompressed_buffer, chunk_extra);
if(remaining != chunk_extra)
{
fclose(f);
f = 0;
free(output_file);
output_file = 0;
printf("\nError: decompression failed. Skipped.\n");
}
else
fwrite(decompressed_buffer, 1, chunk_extra, f);
}
break;
default:
printf("\nError: unknown compression method (%d)\n", chunk_options);
fclose(f);
f = 0;
free(output_file);
output_file = 0;
break;
}
/* for progress, if everything is fine */
if(f)
{
int last_percent = (int)percent;
if(decompressed_size < (1<<24))
percent = total_extracted * 100 / decompressed_size;
else
percent = total_extracted / 256 * 100 / (decompressed_size >>8);
percent >>= 1;
while(last_percent < (int)percent)
{
printf("#");
last_percent++;
}
}
}
/* position of next chunk */
fseek(in, pos + 16 + chunk_size, SEEK_SET);
}
printf("\n\n");
/* free allocated stuff */
free(compressed_buffer);
free(decompressed_buffer);
free(output_file);
/* close working files */
if(f)
fclose(f);
fclose(in);
/* so far so good */
return 0;
}
int main(int argc, char** argv)
{
int i;
const char* archive_file;
/* show help with no argument at all*/
if(argc == 1)
{
usage();
return 0;
}
/* check for help on usage */
for(i = 1; i <= argc; i++)
if(argv[i])
if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
{
usage();
return 0;
}
/* check for version information */
for(i = 1; i <= argc; i++)
if(argv[i])
if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
{
printf("6unpack: high-speed file compression tool\n");
printf("Version %s (using FastLZ %s)\n",
SIXPACK_VERSION_STRING, FASTLZ_VERSION_STRING);
printf("Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)\n");
printf("\n");
return 0;
}
/* needs at least two arguments */
if(argc <= 1)
{
usage();
return 0;
}
archive_file = argv[1];
return unpack_file(archive_file);
}
+24
View File
@@ -0,0 +1,24 @@
FastLZ - lightning-fast lossless compression library
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
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.
+75
View File
@@ -0,0 +1,75 @@
FastLZ - lightning-fast lossless compression library
Author: Ariya Hidayat
Official website: http://www.fastlz.org
FastLZ is distributed using the MIT license, see file LICENSE
for details.
FastLZ consists of two files: fastlz.h and fastlz.c. Just add these
files to your project in order to use FastLZ. For information on
compression and decompression routines, see fastlz.h.
A simple file compressor called 6pack is included as an example
on how to use FastLZ. The corresponding decompressor is 6unpack.
To compile using GCC:
gcc -o 6pack 6pack.c fastlz.c
gcc -o 6unpack 6unpack.c fastlz.c
To compile using MinGW:
mingw32-gcc -o 6pack 6pack.c fastlz.c
mingw32-gcc -o 6unpack 6unpack.c fastlz.c
To compile using Microsoft Visual C++:
cl 6pack.c fastlz.c
cl 6unpack.c fastlz.c
To compile using Borland C++:
bcc32 6pack.c fastlz.c
bcc32 6unpack.c fastlz.c
To compile using OpenWatcom C/C++:
cl386 6pack.c fastlz.c
cl386 6unpack.c fastlz.c
To compile using Intel C++ compiler for Windows:
icl 6pack.c fastlz.c
icl 6unpack.c fastlz.c
To compile using Intel C++ compiler for Linux:
icc -o 6pack 6pack.c fastlz.c
icc -o 6unpack 6unpack.c fastlz.c
To compile 6pack using LCC-Win32:
lc 6pack.c fastlz.c
lc 6unpack.c fastlz.c
To compile 6pack using Pelles C:
pocc 6pack.c
pocc 6unpack.c
pocc fastlz.c
polink 6pack.obj fastlz.obj
polink 6unpack.obj fastlz.obj
For speed optimization, always use proper compile flags for optimization options.
Typical compiler flags are given below:
* GCC (pre 4.2): -march=pentium -O3 -fomit-frame-pointer -mtune=pentium
* GCC 4.2 or later: -march=pentium -O3 -fomit-frame-pointer -mtune=generic
* Digital Mars C/C++: -o+all -5
* Intel C++ (Windows): /O3 /Qipo
* Intel C++ (Linux): -O2 -march=pentium -mtune=pentium
* Borland C++: -O2 -5
* LCC-Win32: -O
* Pelles C: /O2
File diff suppressed because it is too large Load Diff
+100
View File
@@ -0,0 +1,100 @@
/*
FastLZ - lightning-fast lossless compression library
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
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.
*/
#ifndef FASTLZ_H
#define FASTLZ_H
#define FASTLZ_VERSION 0x000100
#define FASTLZ_VERSION_MAJOR 0
#define FASTLZ_VERSION_MINOR 0
#define FASTLZ_VERSION_REVISION 0
#define FASTLZ_VERSION_STRING "0.1.0"
#if defined (__cplusplus)
extern "C" {
#endif
/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
*/
int fastlz_compress(const void* input, int length, void* output);
/**
Decompress a block of compressed data and returns the size of the
decompressed block. If error occurs, e.g. the compressed data is
corrupted or the output buffer is not large enough, then 0 (zero)
will be returned instead.
The input buffer and the output buffer can not overlap.
Decompression is memory safe and guaranteed not to write the output buffer
more than what is specified in maxout.
*/
int fastlz_decompress(const void* input, int length, void* output, int maxout);
/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
Compression level can be specified in parameter level. At the moment,
only level 1 and level 2 are supported.
Level 1 is the fastest compression and generally useful for short data.
Level 2 is slightly slower but it gives better compression ratio.
Note that the compressed data, regardless of the level, can always be
decompressed using the function fastlz_decompress above.
*/
int fastlz_compress_level(int level, const void* input, int length, void* output);
#if defined (__cplusplus)
}
#endif
#endif /* FASTLZ_H */