Prepar fastlz integration

This commit is contained in:
optixx 2016-07-16 17:16:04 +02:00
parent d5560260b3
commit 2532119248
17 changed files with 3322 additions and 4227 deletions

View File

@ -24,7 +24,7 @@ TARGET = main
AVRDUDE = avrdude -c usbasp -p $(DEVICE) AVRDUDE = avrdude -c usbasp -p $(DEVICE)
SIZE = avr-size SIZE = avr-size
BOOT_LOADER = 2 BOOT_LOADER = 2
BOOT_COMPRESS = rle BOOT_COMPRESS = fastlz
BOOT_ROM01 = ../../roms/qd16boot01.smc BOOT_ROM01 = ../../roms/qd16boot01.smc
BOOT_ROM02 = ../../roms/qd16boot02.smc BOOT_ROM02 = ../../roms/qd16boot02.smc

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,16 @@
/* /*
File: qd16boot02.smc File: qd16boot02.smc
Time: Wed, 18 May 2016 15:44:45 Time: Sat, 16 Jul 2016 17:06:14
*/ */
#ifndef __FIFO_H__ #ifndef __FIFO_H__
#define __FIFO_H__ #define __FIFO_H__
#define LOADER_NAME "qd16boot02.smc" #define LOADER_NAME "qd16boot02.smc"
#define LOADER_COMPRESS "ZIP" #define LOADER_COMPRESS "FASTLZ"
#define ROM_ZIP_SIZE 33654 #define ROM_FASTLZ_SIZE 48271
#define ROM_BUFFER_CNT 2 #define ROM_BUFFER_CNT 2
#define ROM_BUFFER_SIZE01 32767 #define ROM_BUFFER_SIZE01 32767
#define ROM_BUFFER_SIZE02 887 #define ROM_BUFFER_SIZE02 15504
#endif #endif

View File

@ -1,636 +0,0 @@
/*
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 "snapshot 20070615"
#include "fastlz.h"
#undef PATH_SEPARATOR
#if defined(MSDOS) || defined(__MSDOS__) || defined(MSDOS)
#define PATH_SEPARATOR '\\'
#endif
#if defined(WIN32) || defined(__NT__) || defined(_WIN32) || defined(__WIN32__)
#define PATH_SEPARATOR '\\'
#if defined(__BORLANDC__) || defined(_MSC_VER)
#define inline __inline
#endif
#endif
#ifndef PATH_SEPARATOR
#define PATH_SEPARATOR '/'
#endif
#undef SIXPACK_BENCHMARK_WIN32
#if defined(WIN32) || defined(__NT__) || defined(_WIN32) || defined(__WIN32__)
#if defined(_MSC_VER) || defined(__GNUC__)
#define SIXPACK_BENCHMARK_WIN32
#include <windows.h>
#endif
#endif
/* magic identifier for 6pack file */
static unsigned char sixpack_magic[8] = {137, '6', 'P', 'K', 13, 10, 26, 10};
#define BLOCK_SIZE (2*64*1024)
/* prototypes */
static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len);
void usage(void);
int detect_magic(FILE *f);
void write_magic(FILE *f);
void write_chunk_header(FILE* f, int id, int options, unsigned long size,
unsigned long checksum, unsigned long extra);
unsigned long block_compress(const unsigned char* input, unsigned long length, unsigned char* output);
int pack_file_compressed(const char* input_file, int method, int level, FILE* f);
int pack_file(int compress_level, const char* input_file, const char* output_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("6pack: high-speed file compression tool\n");
printf("Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)\n");
printf("\n");
printf("Usage: 6pack [options] input-file output-file\n");
printf("\n");
printf("Options:\n");
printf(" -1 compress faster\n");
printf(" -2 compress better\n");
printf(" -v show program version\n");
#ifdef SIXPACK_BENCHMARK_WIN32
printf(" -mem check in-memory compression speed\n");
#endif
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;
}
void write_magic(FILE *f)
{
fwrite(sixpack_magic, 8, 1, f);
}
void write_chunk_header(FILE* f, int id, int options, unsigned long size,
unsigned long checksum, unsigned long extra)
{
unsigned char buffer[16];
buffer[0] = id & 255;
buffer[1] = id >> 8;
buffer[2] = options & 255;
buffer[3] = options >> 8;
buffer[4] = size & 255;
buffer[5] = (size >> 8) & 255;
buffer[6] = (size >> 16) & 255;
buffer[7] = (size >> 24) & 255;
buffer[8] = checksum & 255;
buffer[9] = (checksum >> 8) & 255;
buffer[10] = (checksum >> 16) & 255;
buffer[11] = (checksum >> 24) & 255;
buffer[12] = extra & 255;
buffer[13] = (extra >> 8) & 255;
buffer[14] = (extra >> 16) & 255;
buffer[15] = (extra >> 24) & 255;
fwrite(buffer, 16, 1, f);
}
int pack_file_compressed(const char* input_file, int method, int level, FILE* f)
{
FILE* in;
unsigned long fsize;
unsigned long checksum;
const char* shown_name;
unsigned char buffer[BLOCK_SIZE];
unsigned char result[BLOCK_SIZE*2]; /* FIXME twice is too large */
unsigned char progress[20];
int c;
unsigned long percent;
unsigned long total_read;
unsigned long total_compressed;
int chunk_size;
/* 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);
/* already a 6pack archive? */
if(detect_magic(in))
{
printf("Error: file %s is already a 6pack archive!\n", input_file);
fclose(in);
return -1;
}
/* truncate directory prefix, e.g. "foo/bar/FILE.txt" becomes "FILE.txt" */
shown_name = input_file + strlen(input_file) - 1;
while(shown_name > input_file)
if(*(shown_name-1) == PATH_SEPARATOR)
break;
else
shown_name--;
/* chunk for File Entry */
buffer[0] = fsize & 255;
buffer[1] = (fsize >> 8) & 255;
buffer[2] = (fsize >> 16) & 255;
buffer[3] = (fsize >> 24) & 255;
#if 0
buffer[4] = (fsize >> 32) & 255;
buffer[5] = (fsize >> 40) & 255;
buffer[6] = (fsize >> 48) & 255;
buffer[7] = (fsize >> 56) & 255;
#else
/* because fsize is only 32-bit */
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0;
buffer[7] = 0;
#endif
buffer[8] = (strlen(shown_name)+1) & 255;
buffer[9] = (strlen(shown_name)+1) >> 8;
checksum = 1L;
checksum = update_adler32(checksum, buffer, 10);
checksum = update_adler32(checksum, shown_name, strlen(shown_name)+1);
write_chunk_header(f, 1, 0, 10+strlen(shown_name)+1, checksum, 0);
fwrite(buffer, 10, 1, f);
fwrite(shown_name, strlen(shown_name)+1, 1, f);
total_compressed = 16 + 10 + strlen(shown_name)+1;
/* for progress status */
memset(progress, ' ', 20);
if(strlen(shown_name) < 16)
for(c = 0; c < (int)strlen(shown_name); c++)
progress[c] = shown_name[c];
else
{
for(c = 0; c < 13; c++)
progress[c] = shown_name[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);
/* read file and place in archive */
total_read = 0;
percent = 0;
for(;;)
{
int compress_method = method;
int last_percent = (int)percent;
size_t bytes_read = fread(buffer, 1, BLOCK_SIZE, in);
if(bytes_read == 0)
break;
total_read += bytes_read;
/* for progress */
if(fsize < (1<<24))
percent = total_read * 100 / fsize;
else
percent = total_read/256 * 100 / (fsize >>8);
percent >>= 1;
while(last_percent < (int)percent)
{
printf("#");
last_percent++;
}
/* too small, don't bother to compress */
if(bytes_read < 32)
compress_method = 0;
/* write to output */
switch(compress_method)
{
/* FastLZ */
case 1:
chunk_size = fastlz_compress_level(level, buffer, bytes_read, result);
checksum = update_adler32(1L, result, chunk_size);
write_chunk_header(f, 17, 1, chunk_size, checksum, bytes_read);
fwrite(result, 1, chunk_size, f);
total_compressed += 16;
total_compressed += chunk_size;
break;
/* uncompressed, also fallback method */
case 0:
default:
checksum = 1L;
checksum = update_adler32(checksum, buffer, bytes_read);
write_chunk_header(f, 17, 0, bytes_read, checksum, bytes_read);
fwrite(buffer, 1, bytes_read, f);
total_compressed += 16;
total_compressed += bytes_read;
break;
}
}
fclose(in);
if(total_read != fsize)
{
printf("\n");
printf("Error: reading %s failed!\n", input_file);
return -1;
}
else
{
printf("] ");
if(total_compressed < fsize)
{
if(fsize < (1<<20))
percent = total_compressed * 1000 / fsize;
else
percent = total_compressed/256 * 1000 / (fsize >>8);
percent = 1000 - percent;
printf("%2d.%d%% saved", (int)percent/10, (int)percent%10);
}
printf("\n");
}
return 0;
}
int pack_file(int compress_level, const char* input_file, const char* output_file)
{
FILE* f;
int result;
f = fopen(output_file, "rb");
if(f)
{
fclose(f);
printf("Error: file %s already exists. Aborted.\n\n", output_file);
return -1;
}
f = fopen(output_file, "wb");
if(!f)
{
printf("Error: could not create %s. Aborted.\n\n", output_file);
return -1;
}
write_magic(f);
result = pack_file_compressed(input_file, 1, compress_level, f);
fclose(f);
return result;
}
#ifdef SIXPACK_BENCHMARK_WIN32
int benchmark_speed(int compress_level, const char* input_file);
int benchmark_speed(int compress_level, const char* input_file)
{
FILE* in;
unsigned long fsize;
unsigned long maxout;
const char* shown_name;
unsigned char* buffer;
unsigned char* result;
size_t bytes_read;
/* 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);
/* already a 6pack archive? */
if(detect_magic(in))
{
printf("Error: no benchmark for 6pack archive!\n");
fclose(in);
return -1;
}
/* truncate directory prefix, e.g. "foo/bar/FILE.txt" becomes "FILE.txt" */
shown_name = input_file + strlen(input_file) - 1;
while(shown_name > input_file)
if(*(shown_name-1) == PATH_SEPARATOR)
break;
else
shown_name--;
maxout = 1.05 * fsize;
maxout = (maxout < 66) ? 66 : maxout;
buffer = (unsigned char*)malloc(fsize);
result = (unsigned char*)malloc(maxout);
if(!buffer || !result)
{
printf("Error: not enough memory!\n");
free(buffer);
free(result);
fclose(in);
return -1;
}
printf("Reading source file....\n");
bytes_read = fread(buffer, 1, fsize, in);
if(bytes_read != fsize)
{
printf("Error reading file %s!\n", shown_name);
printf("Read %d bytes, expecting %d bytes\n", bytes_read, fsize);
free(buffer);
free(result);
fclose(in);
return -1;
}
/* shamelessly copied from QuickLZ 1.20 test program */
{
unsigned int j, y;
size_t i, u = 0;
double mbs, fastest;
unsigned long compressed_size;
printf("Setting HIGH_PRIORITY_CLASS...\n");
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
printf("Benchmarking FastLZ Level %d, please wait...\n", compress_level);
i = bytes_read;
fastest = 0.0;
for (j = 0; j < 3; j++)
{
y = 0;
mbs = GetTickCount();
while(GetTickCount() == mbs);
mbs = GetTickCount();
while(GetTickCount() - mbs < 3000) /* 1% accuracy with 18.2 timer */
{
u = fastlz_compress_level(compress_level, buffer, bytes_read, result);
y++;
}
mbs = ((double)i*(double)y)/((double)(GetTickCount() - mbs)/1000.)/1000000.;
/*printf(" %.1f Mbyte/s ", mbs);*/
if (fastest < mbs)
fastest = mbs;
}
printf("\nCompressed %d bytes into %d bytes (%.1f%%) at %.1f Mbyte/s.\n", (unsigned int)i, (unsigned int)u, (double)u/(double)i*100., fastest);
#if 1
fastest = 0.0;
compressed_size = u;
for (j = 0; j < 3; j++)
{
y = 0;
mbs = GetTickCount();
while(GetTickCount() == mbs);
mbs = GetTickCount();
while(GetTickCount() - mbs < 3000) /* 1% accuracy with 18.2 timer */
{
u = fastlz_decompress(result, compressed_size, buffer, bytes_read);
y++;
}
mbs = ((double)i*(double)y)/((double)(GetTickCount() - mbs)/1000.)/1000000.;
/*printf(" %.1f Mbyte/s ", mbs);*/
if (fastest < mbs)
fastest = mbs;
}
printf("\nDecompressed at %.1f Mbyte/s.\n\n(1 MB = 1000000 byte)\n", fastest);
#endif
}
fclose(in);
return 0;
}
#endif /* SIXPACK_BENCHMARK_WIN32 */
int main(int argc, char** argv)
{
int i;
int compress_level;
int benchmark;
char* input_file;
char* output_file;
/* show help with no argument at all*/
if(argc == 1)
{
usage();
return 0;
}
/* default compression level, not the fastest */
compress_level = 2;
/* do benchmark only when explicitly specified */
benchmark = 0;
/* no file is specified */
input_file = 0;
output_file = 0;
for(i = 1; i <= argc; i++)
{
char* argument = argv[i];
if(!argument)
continue;
/* display help on usage */
if(!strcmp(argument, "-h") || !strcmp(argument, "--help"))
{
usage();
return 0;
}
/* check for version information */
if(!strcmp(argument, "-v") || !strcmp(argument, "--version"))
{
printf("6pack: 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;
}
/* test compression speed? */
if(!strcmp(argument, "-mem"))
{
benchmark = 1;
continue;
}
/* compression level */
if(!strcmp(argument, "-1") || !strcmp(argument, "--fastest"))
{
compress_level = 1;
continue;
}
if(!strcmp(argument, "-2"))
{
compress_level = 2;
continue;
}
/* unknown option */
if(argument[0] == '-')
{
printf("Error: unknown option %s\n\n", argument);
printf("To get help on usage:\n");
printf(" 6pack --help\n\n");
return -1;
}
/* first specified file is input */
if(!input_file)
{
input_file = argument;
continue;
}
/* next specified file is output */
if(!output_file)
{
output_file = argument;
continue;
}
/* files are already specified */
printf("Error: unknown option %s\n\n", argument);
printf("To get help on usage:\n");
printf(" 6pack --help\n\n");
return -1;
}
if(!input_file)
{
printf("Error: input file is not specified.\n\n");
printf("To get help on usage:\n");
printf(" 6pack --help\n\n");
return -1;
}
if(!output_file && !benchmark)
{
printf("Error: output file is not specified.\n\n");
printf("To get help on usage:\n");
printf(" 6pack --help\n\n");
return -1;
}
#ifdef SIXPACK_BENCHMARK_WIN32
if(benchmark)
return benchmark_speed(compress_level, input_file);
else
#endif
return pack_file(compress_level, input_file, output_file);
/* unreachable */
return 0;
}

View File

@ -1,478 +0,0 @@
/*
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);
}

View File

@ -1,5 +0,0 @@
all:
gcc -o poc poc.c fastlz.c -I/usr/local/Cellar/openssl/1.0.2g/include -L/usr/local/Cellar/openssl/1.0.2g/lib -lssl -lcrypto

View File

@ -1,439 +0,0 @@
/*
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.
*/
/*
* Give hints to the compiler for branch prediction optimization.
*/
#if defined(__GNUC__) && (__GNUC__ > 2)
#define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
#else
#define FASTLZ_EXPECT_CONDITIONAL(c) (c)
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c)
#endif
/*
* Use inlined functions for supported systems.
*/
#if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C)
#define FASTLZ_INLINE inline
#elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__)
#define FASTLZ_INLINE __inline
#else
#define FASTLZ_INLINE
#endif
typedef unsigned char flzuint8;
typedef unsigned short flzuint16;
typedef unsigned int flzuint32;
/* prototypes */
int fastlz_compress(const void* input, int length, void* output);
int fastlz_decompress(const void* input, int length, void* output);
#define MAX_COPY 32
#define MAX_LEN 264 /* 256 + 8 */
#define MAX_DISTANCE 256
#define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8)
#define HASH_LOG 13
#define HASH_SIZE (1<< HASH_LOG)
#define HASH_MASK (HASH_SIZE-1)
#define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; }
FASTLZ_INLINE int fastlz_compress(const void* input, int length, void* output)
{
const flzuint8* ip = (const flzuint8*) input;
const flzuint8* ip_bound = ip + length - 2;
const flzuint8* ip_limit = ip + length - 12;
flzuint8* op = (flzuint8*) output;
const flzuint8* htab[HASH_SIZE];
const flzuint8** hslot;
flzuint32 hval;
flzuint32 copy;
/* sanity check */
if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4))
{
if(length)
{
/* create literal copy only */
*op++ = length-1;
ip_bound++;
while(ip <= ip_bound)
*op++ = *ip++;
return length+1;
}
else
return 0;
}
/* initializes hash table */
for (hslot = htab; hslot < htab + HASH_SIZE; hslot++)
*hslot = ip;
/* we start with literal copy */
copy = 2;
*op++ = MAX_COPY-1;
*op++ = *ip++;
*op++ = *ip++;
/* main loop */
while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
{
const flzuint8* ref;
flzuint32 distance;
/* minimum match length */
flzuint32 len = 3;
/* comparison starting-point */
const flzuint8* anchor = ip;
/* check for a run */
/* find potential match */
HASH_FUNCTION(hval,ip);
hslot = htab + hval;
ref = htab[hval];
/* calculate distance to the match */
distance = anchor - ref;
/* update hash table */
*hslot = anchor;
/* is this a match? check the first 3 bytes */
if(distance==0 ||
(distance >= MAX_DISTANCE) ||
*ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++)
goto literal;
/* last matched byte */
ip = anchor + len;
/* distance is biased */
distance--;
if(!distance)
{
/* zero distance means a run */
flzuint8 x = ip[-1];
while(ip < ip_bound)
if(*ref++ != x) break; else ip++;
}
else
for(;;)
{
/* safe because the outer check against ip limit */
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
while(ip < ip_bound)
if(*ref++ != *ip++) break;
break;
}
/* if we have copied something, adjust the copy count */
if(copy)
/* copy is biased, '0' means 1 byte copy */
*(op-copy-1) = copy-1;
else
/* back, to overwrite the copy count */
op--;
/* reset literal counter */
copy = 0;
/* length is biased, '1' means a match of 3 bytes */
ip -= 3;
len = ip - anchor;
/* encode the match */
if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2))
while(len > MAX_LEN-2)
{
*op++ = (7 << 5) + (distance >> 8);
*op++ = MAX_LEN - 2 - 7 -2;
*op++ = (distance & 255);
len -= MAX_LEN-2;
}
if(len < 7)
{
*op++ = (len << 5) + (distance >> 8);
*op++ = (distance & 255);
}
else
{
*op++ = (7 << 5) + (distance >> 8);
*op++ = len - 7;
*op++ = (distance & 255);
}
/* update the hash at match boundary */
HASH_FUNCTION(hval,ip);
htab[hval] = ip++;
HASH_FUNCTION(hval,ip);
htab[hval] = ip++;
/* assuming literal copy */
*op++ = MAX_COPY-1;
continue;
literal:
*op++ = *anchor++;
ip = anchor;
copy++;
if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY))
{
copy = 0;
*op++ = MAX_COPY-1;
}
}
/* left-over as literal copy */
ip_bound++;
while(ip <= ip_bound)
{
*op++ = *ip++;
copy++;
if(copy == MAX_COPY)
{
copy = 0;
*op++ = MAX_COPY-1;
}
}
/* if we have copied something, adjust the copy length */
if(copy)
*(op-copy-1) = copy-1;
else
op--;
return op - (flzuint8*)output;
}
#include <stdio.h>
#define log1(NUM) printf("%i op=%i(%x) ip=%i(%x) len=%i ctrl=%i ofs=%i(%i) limit=%i\n",NUM, (((int)op - (int)output)), *op, (((int)ip - (int)input)),*ip, len, ctrl, ofs, (ofs >> 8),ip < ip_limit);
int fastlz_decompress(const void* input, int length, void* output)
{
const flzuint8* ip = (const flzuint8*) input;
const flzuint8* ip_limit = ip + length;
flzuint8* op = (flzuint8*) output;
flzuint32 ctrl = (*ip++) & 31;
int loop = 1;
do
{
const flzuint8* ref = op;
flzuint32 len = ctrl >> 5;
flzuint32 ofs = (ctrl & 31) << 8;
printf("-------------------\n");
log1(1)
if(ctrl >= 32)
{
len--;
ref -= ofs;
if (len == 7-1)
len += *ip++;
ref -= *ip++;
log1(1)
if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
ctrl = *ip++;
else
loop = 0;
log1(1)
if(ref == op)
{
log1(2)
/* optimize copy for a run */
flzuint8 b = ref[-1];
*op++ = b;
*op++ = b;
*op++ = b;
for(; len; --len)
*op++ = b;
}
else
{
log1(3)
/* copy from reference */
ref--;
*op++ = *ref++;
*op++ = *ref++;
*op++ = *ref++;
for(; len; --len)
*op++ = *ref++;
}
}
else
{
ctrl++;
log1(4)
*op++ = *ip++;
for(--ctrl; ctrl; ctrl--){
log1(5)
*op++ = *ip++;
}
loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit);
if(loop){
ctrl = *ip++;
}
log1(6)
}
}
while(FASTLZ_EXPECT_CONDITIONAL(loop));
return op - (flzuint8*)output;
}
#include <stdlib.h>
#include <assert.h>
#include "ringbuffer.h"
#define log2(NUM) printf("%i op=%i(%x) ip=%i(%x) ref=%i(%x) dist=%i buf->end=%i len=%i ctrl=%i ofs=%i(%i) limit=%i\n",NUM, output_addr,output[output_addr], input_addr,input[input_addr],ref_addr,input[ref_addr],output_addr - ref_addr,buffer_ptr->end, len, ctrl, ofs, ofs>>6,input_addr < ip_limit);
#define OUTPUT_INC(B) do { \
flzuint8 __b = B;\
output[output_addr] = __b;\
bufferWrite(buffer_ptr, __b);\
output_addr++;\
} while (0)
#define OUTPUT_INC_FROM_REFINC() do { \
flzuint8 __b = output[ref_addr]; \
flzuint16 __dist = (output_addr-ref_addr); \
flzuint8 __c = bufferGet(buffer_ptr, __dist); \
printf("output_addr=%i ref_addr=%i(%x) dist=%i(%x) buf->end=%i buf->size=%i position=%i\n", output_addr, ref_addr, __b, __dist, __c, buffer_ptr->end, buffer_ptr->size, __mod(buffer_ptr->end - __dist, buffer_ptr->size)); \
assert(__c == __b); \
output[output_addr] = __c;\
bufferWrite(buffer_ptr, __c);\
output_addr++;\
ref_addr++;\
} while (0)
ringBuffer_typedef(unsigned char, charBuffer);
int fastlz_decompress2(unsigned char* input, int length, unsigned char* output)
{
flzuint32 input_addr = 0;
flzuint32 ip_limit = length;
flzuint32 output_addr = 0;
flzuint32 ref_addr = 0;
flzuint32 ctrl = (input[input_addr++]) & 31;
int loop = 1;
charBuffer buffer;
bufferInit(buffer, MAX_DISTANCE, unsigned char);
charBuffer* buffer_ptr;
buffer_ptr = &buffer;
do
{
ref_addr = output_addr;
flzuint32 len = ctrl >> 5;
flzuint32 ofs = (ctrl & 31) << 6;
printf("-------------------\n");
log2(1)
if(ctrl >= 32)
{
len--;
ref_addr -= ofs;
if (len == 7-1)
len += input[input_addr++];
ref_addr -= input[input_addr++];
log2(1)
if(FASTLZ_EXPECT_CONDITIONAL( input_addr < ip_limit))
ctrl = input[input_addr++];
else
loop = 0;
log2(1)
if(ref_addr == output_addr)
{
log2(2)
flzuint8 b = output[ref_addr-1];
OUTPUT_INC(b);
OUTPUT_INC(b);
OUTPUT_INC(b);
for(; len; --len)
OUTPUT_INC(b);
}
else
{
log2(3)
ref_addr--;
OUTPUT_INC_FROM_REFINC();
OUTPUT_INC_FROM_REFINC();
OUTPUT_INC_FROM_REFINC();
for(; len; --len)
OUTPUT_INC_FROM_REFINC();
}
}
else
{
ctrl++;
log2(4)
OUTPUT_INC(input[input_addr++]);
for(--ctrl; ctrl; ctrl--){
log2(5)
OUTPUT_INC(input[input_addr++]);
}
loop = FASTLZ_EXPECT_CONDITIONAL(input_addr < ip_limit);
if (loop){
ctrl = input[input_addr++];
}
log2(6)
}
}
while(FASTLZ_EXPECT_CONDITIONAL(loop));
return 0;
}

View File

@ -1,99 +0,0 @@
/*
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);
/**
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.
*/
#if defined (__cplusplus)
}
#endif
#endif /* FASTLZ_H */

View File

@ -1,88 +0,0 @@
/* Philip Thrasher's Crazy Awesome Ring Buffer Macros!
*
* Below you will find some naughty macros for easy owning and manipulating
* generic ring buffers. Yes, they are slightly evil in readability, but they
* are really fast, and they work great.
*
* Example usage:
*
* #include <stdio.h>
*
* // So we can use this in any method, this gives us a typedef
* // named 'intBuffer'.
* ringBuffer_typedef(int, intBuffer);
*
* int main() {
* // Declare vars.
* intBuffer myBuffer;
*
* bufferInit(myBuffer,1024,int);
*
* // We must have the pointer. All of the macros deal with the pointer.
* // (except for init.)
* intBuffer* myBuffer_ptr;
* myBuffer_ptr = &myBuffer;
*
* // Write two values.
* bufferWrite(myBuffer_ptr,37);
* bufferWrite(myBuffer_ptr,72);
*
* // Read a value into a local variable.
* int first;
* bufferRead(myBuffer_ptr,first);
* assert(first == 37); // true
*
* int second;
* bufferRead(myBuffer_ptr,second);
* assert(second == 72); // true
*
* return 0;
* }
*
*/
#ifndef _ringbuffer_h
#define _ringbuffer_h
int __mod(int a, int b)
{
int r = a % b;
return r < 0 ? r + b : r;
}
#define ringBuffer_typedef(T, NAME) \
typedef struct { \
int size; \
int start; \
int end; \
T* elems; \
} NAME
#define bufferInit(BUF, S, T) \
BUF.size = S+1; \
BUF.start = 0; \
BUF.end = 0; \
BUF.elems = (T*)calloc(BUF.size, sizeof(T))
#define bufferDestroy(BUF) free(BUF->elems)
#define nextStartIndex(BUF) ((BUF->start + 1) % BUF->size)
#define nextEndIndex(BUF) ((BUF->end + 1) % BUF->size)
#define isBufferEmpty(BUF) (BUF->end == BUF->start)
#define isBufferFull(BUF) (nextEndIndex(BUF) == BUF->start)
#define bufferGet(BUF, INDEX) (BUF->elems[__mod(BUF->end - INDEX, BUF->size)])
#define bufferWrite(BUF, ELEM) \
BUF->elems[BUF->end] = ELEM; \
BUF->end = (BUF->end + 1) % BUF->size; \
if (isBufferEmpty(BUF)) { \
BUF->start = nextStartIndex(BUF); \
}
#define bufferRead(BUF, ELEM) \
ELEM = BUF->elems[BUF->start]; \
BUF->start = nextStartIndex(BUF);
#endif

View File

@ -1,24 +0,0 @@
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.

View File

@ -1,5 +1,8 @@
all: poc pack
all: poc: poc.c fastlz.c
gcc 6pack.c fastlz.c -o pack gcc -o poc poc.c fastlz.c -I/usr/local/Cellar/openssl/1.0.2g/include -L/usr/local/Cellar/openssl/1.0.2g/lib -lssl -lcrypto
gcc 6unpack.c fastlz.c -o unpack
pack: pack.c fastlz.c
gcc -o pack pack.c fastlz.c -I/usr/local/Cellar/openssl/1.0.2g/include -L/usr/local/Cellar/openssl/1.0.2g/lib -lssl -lcrypto

View File

@ -1,75 +0,0 @@
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

View File

@ -24,14 +24,6 @@
THE SOFTWARE. THE SOFTWARE.
*/ */
#if !defined(FASTLZ__COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR)
/*
* Always check for bound when decompressing.
* Generally it is best to leave it defined.
*/
#define FASTLZ_SAFE
/* /*
* Give hints to the compiler for branch prediction optimization. * Give hints to the compiler for branch prediction optimization.
*/ */
@ -54,116 +46,27 @@
#define FASTLZ_INLINE #define FASTLZ_INLINE
#endif #endif
/*
* Prevent accessing more than 8-bit at once, except on x86 architectures.
*/
#if !defined(FASTLZ_STRICT_ALIGN)
#define FASTLZ_STRICT_ALIGN
#if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */
#undef FASTLZ_STRICT_ALIGN
#elif defined(__i486__) || defined(__i586__) || defined(__i686__) /* GNU C */
#undef FASTLZ_STRICT_ALIGN
#elif defined(_M_IX86) /* Intel, MSVC */
#undef FASTLZ_STRICT_ALIGN
#elif defined(__386)
#undef FASTLZ_STRICT_ALIGN
#elif defined(_X86_) /* MinGW */
#undef FASTLZ_STRICT_ALIGN
#elif defined(__I86__) /* Digital Mars */
#undef FASTLZ_STRICT_ALIGN
#endif
#endif
/*
* FIXME: use preprocessor magic to set this on different platforms!
*/
typedef unsigned char flzuint8; typedef unsigned char flzuint8;
typedef unsigned short flzuint16; typedef unsigned short flzuint16;
typedef unsigned int flzuint32; typedef unsigned int flzuint32;
/* prototypes */ /* prototypes */
int fastlz_compress(const void* input, int length, void* output); int fastlz_compress(const void* input, int length, void* output);
int fastlz_compress_level(int level, const void* input, int length, void* output); int fastlz_decompress(const void* input, int length, void* output);
int fastlz_decompress(const void* input, int length, void* output, int maxout);
#define MAX_COPY 32 #define MAX_COPY 32
#define MAX_LEN 264 /* 256 + 8 */ #define MAX_LEN 264 /* 256 + 8 */
#define MAX_DISTANCE 8192 #define MAX_DISTANCE 256
#if !defined(FASTLZ_STRICT_ALIGN)
#define FASTLZ_READU16(p) *((const flzuint16*)(p))
#else
#define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8) #define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8)
#endif
#define HASH_LOG 13 #define HASH_LOG 13
#define HASH_SIZE (1<< HASH_LOG) #define HASH_SIZE (1<< HASH_LOG)
#define HASH_MASK (HASH_SIZE-1) #define HASH_MASK (HASH_SIZE-1)
#define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; } #define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; }
#undef FASTLZ_LEVEL
#define FASTLZ_LEVEL 1
#undef FASTLZ_COMPRESSOR FASTLZ_INLINE int fastlz_compress(const void* input, int length, void* output)
#undef FASTLZ_DECOMPRESSOR
#define FASTLZ_COMPRESSOR fastlz1_compress
#define FASTLZ_DECOMPRESSOR fastlz1_decompress
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
#include "fastlz.c"
#undef FASTLZ_LEVEL
#define FASTLZ_LEVEL 2
#undef MAX_DISTANCE
#define MAX_DISTANCE 8191
#define MAX_FARDISTANCE (65535+MAX_DISTANCE-1)
#undef FASTLZ_COMPRESSOR
#undef FASTLZ_DECOMPRESSOR
#define FASTLZ_COMPRESSOR fastlz2_compress
#define FASTLZ_DECOMPRESSOR fastlz2_decompress
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
#include "fastlz.c"
int fastlz_compress(const void* input, int length, void* output)
{
/* for short block, choose fastlz1 */
if(length < 65536)
return fastlz1_compress(input, length, output);
/* else... */
return fastlz2_compress(input, length, output);
}
int fastlz_decompress(const void* input, int length, void* output, int maxout)
{
/* magic identifier for compression level */
int level = ((*(const flzuint8*)input) >> 5) + 1;
if(level == 1)
return fastlz1_decompress(input, length, output, maxout);
if(level == 2)
return fastlz2_decompress(input, length, output, maxout);
/* unknown level, trigger error */
return 0;
}
int fastlz_compress_level(int level, const void* input, int length, void* output)
{
if(level == 1)
return fastlz1_compress(input, length, output);
if(level == 2)
return fastlz2_compress(input, length, output);
return 0;
}
#else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output)
{ {
const flzuint8* ip = (const flzuint8*) input; const flzuint8* ip = (const flzuint8*) input;
const flzuint8* ip_bound = ip + length - 2; const flzuint8* ip_bound = ip + length - 2;
@ -215,15 +118,6 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void*
const flzuint8* anchor = ip; const flzuint8* anchor = ip;
/* check for a run */ /* check for a run */
#if FASTLZ_LEVEL==2
if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1))
{
distance = 1;
ip += 3;
ref = anchor - 1 + 3;
goto match;
}
#endif
/* find potential match */ /* find potential match */
HASH_FUNCTION(hval,ip); HASH_FUNCTION(hval,ip);
@ -238,25 +132,10 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void*
/* is this a match? check the first 3 bytes */ /* is this a match? check the first 3 bytes */
if(distance==0 || if(distance==0 ||
#if FASTLZ_LEVEL==1
(distance >= MAX_DISTANCE) || (distance >= MAX_DISTANCE) ||
#else
(distance >= MAX_FARDISTANCE) ||
#endif
*ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++) *ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++)
goto literal; goto literal;
#if FASTLZ_LEVEL==2
/* far, needs at least 5-byte match */
if(distance >= MAX_DISTANCE)
{
if(*ip++ != *ref++ || *ip++!= *ref++)
goto literal;
len += 2;
}
match:
#endif
/* last matched byte */ /* last matched byte */
ip = anchor + len; ip = anchor + len;
@ -304,48 +183,6 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void*
len = ip - anchor; len = ip - anchor;
/* encode the match */ /* encode the match */
#if FASTLZ_LEVEL==2
if(distance < MAX_DISTANCE)
{
if(len < 7)
{
*op++ = (len << 5) + (distance >> 8);
*op++ = (distance & 255);
}
else
{
*op++ = (7 << 5) + (distance >> 8);
for(len-=7; len >= 255; len-= 255)
*op++ = 255;
*op++ = len;
*op++ = (distance & 255);
}
}
else
{
/* far away, but not yet in the another galaxy... */
if(len < 7)
{
distance -= MAX_DISTANCE;
*op++ = (len << 5) + 31;
*op++ = 255;
*op++ = distance >> 8;
*op++ = distance & 255;
}
else
{
distance -= MAX_DISTANCE;
*op++ = (7 << 5) + 31;
for(len-=7; len >= 255; len-= 255)
*op++ = 255;
*op++ = len;
*op++ = 255;
*op++ = distance >> 8;
*op++ = distance & 255;
}
}
#else
if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2)) if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2))
while(len > MAX_LEN-2) while(len > MAX_LEN-2)
{ {
@ -366,7 +203,6 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void*
*op++ = len - 7; *op++ = len - 7;
*op++ = (distance & 255); *op++ = (distance & 255);
} }
#endif
/* update the hash at match boundary */ /* update the hash at match boundary */
HASH_FUNCTION(hval,ip); HASH_FUNCTION(hval,ip);
@ -409,74 +245,46 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void*
else else
op--; op--;
#if FASTLZ_LEVEL==2
/* marker for fastlz2 */
*(flzuint8*)output |= (1 << 5);
#endif
return op - (flzuint8*)output; return op - (flzuint8*)output;
} }
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout)
#include <stdio.h>
#define log1(NUM) printf("%i op=%i(%x) ip=%i(%x) len=%i ctrl=%i ofs=%i(%i) limit=%i\n",NUM, (((int)op - (int)output)), *op, (((int)ip - (int)input)),*ip, len, ctrl, ofs, (ofs >> 8),ip < ip_limit);
int fastlz_decompress(const void* input, int length, void* output)
{ {
const flzuint8* ip = (const flzuint8*) input; const flzuint8* ip = (const flzuint8*) input;
const flzuint8* ip_limit = ip + length; const flzuint8* ip_limit = ip + length;
flzuint8* op = (flzuint8*) output; flzuint8* op = (flzuint8*) output;
flzuint8* op_limit = op + maxout;
flzuint32 ctrl = (*ip++) & 31; flzuint32 ctrl = (*ip++) & 31;
int loop = 1; int loop = 1;
do do
{ {
const flzuint8* ref = op; const flzuint8* ref = op;
flzuint32 len = ctrl >> 5; flzuint32 len = ctrl >> 5;
flzuint32 ofs = (ctrl & 31) << 8; flzuint32 ofs = (ctrl & 31) << 8;
printf("-------------------\n");
log1(1)
if(ctrl >= 32) if(ctrl >= 32)
{ {
#if FASTLZ_LEVEL==2
flzuint8 code;
#endif
len--; len--;
ref -= ofs; ref -= ofs;
if (len == 7-1) if (len == 7-1)
#if FASTLZ_LEVEL==1
len += *ip++; len += *ip++;
ref -= *ip++; ref -= *ip++;
#else
do
{
code = *ip++;
len += code;
} while (code==255);
code = *ip++;
ref -= code;
/* match from 16-bit distance */
if(FASTLZ_UNEXPECT_CONDITIONAL(code==255))
if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8)))
{
ofs = (*ip++) << 8;
ofs += *ip++;
ref = op - ofs - MAX_DISTANCE;
}
#endif
#ifdef FASTLZ_SAFE
if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit))
return 0;
if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output))
return 0;
#endif
log1(1)
if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
ctrl = *ip++; ctrl = *ip++;
else else
loop = 0; loop = 0;
log1(1)
if(ref == op) if(ref == op)
{ {
log1(2)
/* optimize copy for a run */ /* optimize copy for a run */
flzuint8 b = ref[-1]; flzuint8 b = ref[-1];
*op++ = b; *op++ = b;
@ -487,65 +295,185 @@ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void
} }
else else
{ {
#if !defined(FASTLZ_STRICT_ALIGN) log1(3)
const flzuint16* p;
flzuint16* q;
#endif
/* copy from reference */ /* copy from reference */
ref--; ref--;
*op++ = *ref++; *op++ = *ref++;
*op++ = *ref++; *op++ = *ref++;
*op++ = *ref++; *op++ = *ref++;
#if !defined(FASTLZ_STRICT_ALIGN)
/* copy a byte, so that now it's word aligned */
if(len & 1)
{
*op++ = *ref++;
len--;
}
/* copy 16-bit at once */
q = (flzuint16*) op;
op += len;
p = (const flzuint16*) ref;
for(len>>=1; len > 4; len-=4)
{
*q++ = *p++;
*q++ = *p++;
*q++ = *p++;
*q++ = *p++;
}
for(; len; --len)
*q++ = *p++;
#else
for(; len; --len) for(; len; --len)
*op++ = *ref++; *op++ = *ref++;
#endif
} }
} }
else else
{ {
ctrl++; ctrl++;
#ifdef FASTLZ_SAFE log1(4)
if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit))
return 0;
if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit))
return 0;
#endif
*op++ = *ip++; *op++ = *ip++;
for(--ctrl; ctrl; ctrl--) for(--ctrl; ctrl; ctrl--){
log1(5)
*op++ = *ip++; *op++ = *ip++;
}
loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit); loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit);
if(loop) if(loop){
ctrl = *ip++; ctrl = *ip++;
} }
log1(6)
}
} }
while(FASTLZ_EXPECT_CONDITIONAL(loop)); while(FASTLZ_EXPECT_CONDITIONAL(loop));
return op - (flzuint8*)output; return op - (flzuint8*)output;
} }
#endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */ #include <stdlib.h>
#include <assert.h>
#include "ringbuffer.h"
//#define log2(NUM) printf("%i op=%i(%x) ip=%i(%x) ref=%i(%x) dist=%i buf->end=%i len=%i ctrl=%i ofs=%i(%i) limit=%i\n",NUM, output_index,output[output_index], input_index,input[input_index],ref_index,input[ref_index],output_index - ref_index,ref_buffer_ptr->end, len, ctrl, ofs, ofs>>6,input_index < ip_limit);
#define log2(NUM) printf("%i op=%i(%x) ip=%i ref=%i dist=%i buf->end=%i len=%i ctrl=%i ofs=%i(%i) limit=%i\n",NUM, output_index,output[output_index], input_index,ref_index,output_index - ref_index,ref_buffer_ptr->end, len, ctrl, ofs, ofs>>6,input_index < ip_limit);
#define OUTPUT_INC(B) do { \
__b = B;\
output[output_index] = __b;\
bufferWrite(ref_buffer_ptr, __b);\
output_index++;\
} while (0)
#define OUTPUT_INC_FROM_REFINC() do { \
__dist = (output_index-ref_index); \
__c = buffer_get(ref_buffer_ptr, __dist); \
printf("output_index=%i ref_index=%i(%x) dist=%i(%x) buf->end=%i buf->size=%i position=%i\n", output_index, ref_index, __c, __dist, __c, ref_buffer_ptr->end, ref_buffer_ptr->size, __mod(ref_buffer_ptr->end - __dist, ref_buffer_ptr->size)); \
output[output_index] = __c;\
bufferWrite(ref_buffer_ptr, __c);\
output_index++;\
ref_index++;\
} while (0)
#define FROM_REF(OUT) do { \
flzuint16 __dist = (output_index-ref_index+1); \
OUT = buffer_get(ref_buffer_ptr, __dist); \
} while (0)
#define INPUT_INC(OUT) do { \
if (input_index<32768) { \
OUT = input1[input_index++]; \
} else { \
OUT = input2[input_index-32768]; \
input_index++; \
}\
} while (0)
ring_buffer_typedef(unsigned char, byte_buffer);
int fastlz_decompress2(unsigned char* input1, unsigned char* input2, int length, unsigned char* output)
{
flzuint32 input_index = 0;
flzuint32 ip_limit = length;
flzuint32 output_index = 0;
flzuint32 ref_index = 0;
//flzuint32 ctrl = (input[input_index++]) & 31;
flzuint32 ctrl;
INPUT_INC(ctrl);
ctrl = ctrl & 31;
int loop = 1;
byte_buffer ref_buffer;
buffer_init(ref_buffer, MAX_DISTANCE, unsigned char);
byte_buffer* ref_buffer_ptr;
ref_buffer_ptr = &ref_buffer;
do
{
flzuint8 __b;
flzuint16 __dist;
flzuint8 __c;
flzuint8 tmp;
ref_index = output_index;
flzuint32 len = ctrl >> 5;
flzuint32 ofs = (ctrl & 31) << 6;
printf("-------------------\n");
log2(1)
if(ctrl >= 32)
{
len--;
ref_index -= ofs;
if (len == 7-1){
INPUT_INC(tmp);
len += tmp;
//len += input[input_index++];
}
INPUT_INC(tmp);
ref_index -= tmp;
//ref_index -= input[input_index++];
log2(1)
if(FASTLZ_EXPECT_CONDITIONAL( input_index < ip_limit))
INPUT_INC(ctrl);
//ctrl = input[input_index++];
else
loop = 0;
log2(1)
if(ref_index == output_index)
{
log2(2)
//flzuint8 b = output[ref_index-1];
flzuint8 b;
FROM_REF(b);
OUTPUT_INC(b);
OUTPUT_INC(b);
OUTPUT_INC(b);
for(; len; --len)
OUTPUT_INC(b);
}
else
{
log2(3)
ref_index--;
OUTPUT_INC_FROM_REFINC();
OUTPUT_INC_FROM_REFINC();
OUTPUT_INC_FROM_REFINC();
for(; len; --len)
OUTPUT_INC_FROM_REFINC();
}
}
else
{
ctrl++;
log2(4)
INPUT_INC(tmp);
OUTPUT_INC(tmp);
//OUTPUT_INC(input[input_index++]);
for(--ctrl; ctrl; ctrl--){
log2(5)
INPUT_INC(tmp);
OUTPUT_INC(tmp);
//OUTPUT_INC(input[input_index++]);
}
loop = FASTLZ_EXPECT_CONDITIONAL(input_index < ip_limit);
if (loop){
INPUT_INC(ctrl);
//ctrl = input[input_index++];
}
log2(6)
}
}
while(FASTLZ_EXPECT_CONDITIONAL(loop));
buffer_destroy(ref_buffer_ptr);
return 0;
}

View File

@ -67,7 +67,7 @@ int fastlz_compress(const void* input, int length, void* output);
more than what is specified in maxout. more than what is specified in maxout.
*/ */
int fastlz_decompress(const void* input, int length, void* output, int maxout); int fastlz_decompress(const void* input, int length, void* output);
/** /**
Compress a block of data in the input buffer and returns the size of Compress a block of data in the input buffer and returns the size of
@ -91,7 +91,6 @@ int fastlz_decompress(const void* input, int length, void* output, int maxout);
decompressed using the function fastlz_decompress above. decompressed using the function fastlz_decompress above.
*/ */
int fastlz_compress_level(int level, const void* input, int length, void* output);
#if defined (__cplusplus) #if defined (__cplusplus)
} }

62
tools/fastlz/pack.c Normal file
View File

@ -0,0 +1,62 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "fastlz.h"
#include <openssl/md5.h>
#define HEXDUMP_COLS 8
int main(int argc, char** argv){
char *source = NULL;
int len, rlen, i;
FILE *fp = fopen(argv[1], "r");
if (fp != NULL) {
/* Go to the end of the file. */
if (fseek(fp, 0L, SEEK_END) == 0) {
/* Get the size of the file. */
len = ftell(fp);
if (len == -1) { /* Error */ }
/* Allocate our buffer to that size. */
source = malloc(sizeof(char) * (len + 1));
/* Go back to the start of the file. */
if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ }
/* Read the entire file into memory. */
size_t newLen = fread(source, sizeof(char), len, fp);
if (newLen == 0) {
fputs("Error reading file", stderr);
} else {
printf("Reading file with size=%i\n", len);
source[newLen++] = '\0'; /* Just to be safe. */
}
}
fclose(fp);
}
MD5_CTX md5_context;
unsigned char c[MD5_DIGEST_LENGTH];
unsigned char *packed;
packed = (char*)malloc(len);
MD5_Init (&md5_context);
MD5_Update (&md5_context, source, len);
MD5_Final (c,&md5_context);
printf("unpacked len=%i md5=", len);
for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
printf("\n");
rlen = fastlz_compress(source, len, packed);
printf("packed len=%i md5=", rlen);
MD5_Init (&md5_context);
MD5_Update (&md5_context, packed, rlen);
MD5_Final (c,&md5_context);
for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
printf("\n");
fp = fopen(argv[2], "wb");
fwrite(packed, rlen, 1, fp);
printf("Wrote %s %l bytes\n", argv[2], len);
fclose(fp);
}

View File

@ -23,7 +23,9 @@ void init(long len){
int main(int argc, char** argv){ int main(int argc, char** argv){
char *source = NULL; char *source = NULL;
int len, rlen, i; char *source_part1 = NULL;
char *source_part2 = NULL;
int len, len1, len2, rlen, rlen1, rlen2, i;
FILE *fp = fopen("/Users/david/Devel/arch/avr/code/quickdev16/roms/qd16boot02.smc", "r"); FILE *fp = fopen("/Users/david/Devel/arch/avr/code/quickdev16/roms/qd16boot02.smc", "r");
if (fp != NULL) { if (fp != NULL) {
/* Go to the end of the file. */ /* Go to the end of the file. */
@ -54,15 +56,9 @@ int main(int argc, char** argv){
MD5_CTX md5_context; MD5_CTX md5_context;
unsigned char c[MD5_DIGEST_LENGTH]; unsigned char c[MD5_DIGEST_LENGTH];
#if 0
len = strlen(sample);
init(len);
memcpy(unpacked, sample, len);
#else
init(len); init(len);
memcpy(unpacked, source, len); memcpy(unpacked, source, len);
#endif
MD5_Init (&md5_context); MD5_Init (&md5_context);
MD5_Update (&md5_context, unpacked, len); MD5_Update (&md5_context, unpacked, len);
@ -72,25 +68,26 @@ int main(int argc, char** argv){
printf("\n"); printf("\n");
rlen = fastlz_compress(unpacked, len, packed); rlen = fastlz_compress(unpacked, len, packed);
//hexdump(packed, rlen); //hexdump(packed, rlen);
printf("packed len=%i\n", rlen); printf("packed len=%i md5=", rlen);
memset(unpacked, 0, len);
printf("-----\n");
#if 0
fastlz_decompress(packed, rlen, unpacked);
MD5_Init (&md5_context); MD5_Init (&md5_context);
MD5_Update (&md5_context, unpacked, len); MD5_Update (&md5_context, packed, rlen);
MD5_Final (c,&md5_context); MD5_Final (c,&md5_context);
printf("unpacked len=%i md5=", len);
for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]); for(i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
printf("\n"); printf("\n");
fp = fopen("out01.smc", "wb");
fwrite(unpacked, len, 1, fp);
printf("Wrote out01.smc %l bytes\n", len);
fclose(fp);
memset(unpacked, 0, len); memset(unpacked, 0, len);
#endif
printf("-----\n"); printf("-----\n");
fastlz_decompress2(packed, rlen, unpacked);
unsigned char *packed1;
unsigned char *packed2;
rlen1 = 32768;
packed1 = malloc(rlen1);
packed2 = malloc(rlen1);
memcpy(packed1, packed, rlen1);
memcpy(packed2, packed + rlen1, (rlen - rlen1));
fastlz_decompress2(packed1, packed2, rlen, unpacked);
MD5_Init (&md5_context); MD5_Init (&md5_context);
MD5_Update (&md5_context, unpacked, len); MD5_Update (&md5_context, unpacked, len);
MD5_Final (c,&md5_context); MD5_Final (c,&md5_context);
@ -101,11 +98,6 @@ int main(int argc, char** argv){
fwrite(unpacked, len, 1, fp); fwrite(unpacked, len, 1, fp);
printf("Wrote out02.smc %l bytes\n", len); printf("Wrote out02.smc %l bytes\n", len);
fclose(fp); fclose(fp);
printf("s1=%s\n\n", sample);
printf("s2=%s\n", unpacked);
return 0;
} }
void hexdump(void *mem, unsigned int len){ void hexdump(void *mem, unsigned int len){

42
tools/fastlz/ringbuffer.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef _ringbuffer_h
#define _ringbuffer_h
int __mod(int a, int b)
{
int r = a % b;
return r < 0 ? r + b : r;
}
#define ring_buffer_typedef(T, NAME) \
typedef struct { \
int size; \
int start; \
int end; \
T* elems; \
} NAME
#define buffer_init(BUF, S, T) \
BUF.size = S+1; \
BUF.start = 0; \
BUF.end = 0; \
BUF.elems = (T*)calloc(BUF.size, sizeof(T))
#define buffer_destroy(BUF) free(BUF->elems)
#define nex_start_index(BUF) ((BUF->start + 1) % BUF->size)
#define is_buffer_empty(BUF) (BUF->end == BUF->start)
#define buffer_get(BUF, INDEX) (BUF->elems[__mod(BUF->end - INDEX, BUF->size)])
#define bufferWrite(BUF, ELEM) \
BUF->elems[BUF->end] = ELEM; \
BUF->end = (BUF->end + 1) % BUF->size; \
if (is_buffer_empty(BUF)) { \
BUF->start = nex_start_index(BUF); \
}
#define bufferRead(BUF, ELEM) \
ELEM = BUF->elems[BUF->start]; \
BUF->start = nex_start_index(BUF);
#endif