XipZ
Mini packer ▶►▸ for small programs.
|
Source file containing the main function. More...
#include <inttypes.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <stdexcept>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <boost/format.hpp>
#include "cmdline.h"
#include "data.hh"
#include "qadz.hh"
#include "decrunchxipzstub.inc"
Go to the source code of this file.
Classes | |
struct | Bits |
class | HistEntry |
Histogram entry. More... | |
Macros | |
#define | POS_OF_END_OF_CDATA 0x38 |
Position in the stub where the end of the compressed data is stored. | |
#define | POS_OF_BEGIN_OF_CDATA 0x3d |
Position in the stub where the beginning of the compressed data is stored. | |
#define | POS_OF_N 0x46 |
Position in the stub where the end of the number of bits for the compression is stored. | |
#define | POS_OF_DEST 0x6f |
Position in the stub where the address is stored at which the data is decompressed. | |
#define | POS_OF_JMP 0x5c |
Position in the stub where the address is stored at which the final JMP is performed. | |
#define | POS_OF_STOPREADING 0x58 |
Position in the stub where the high byte of the destination address is stored at which the decompression process stops. | |
Typedefs | |
typedef std::vector< HistEntry > | HistorgramArray |
Convenience type for a histogram array. More... | |
typedef std::array< Bits, 256 > | CompressionBits |
Convenience type definition for an array of 256 byte counts. | |
Functions | |
std::ostream & | operator<< (std::ostream &o, const Bits &x) |
Output operator for Bits. More... | |
Data | read_data (const std::string &fname) |
Read data from a file. More... | |
HistorgramArray | calc_histo (const Data &data) |
calculate the histogram More... | |
std::ostream & | operator<< (std::ostream &o, const HistEntry &x) |
Output a histogram entry. More... | |
float | calc_comp (int n, const Data &data, const HistorgramArray &histarr) |
calculate compressed bytes More... | |
CompressionBits | create_compression_bits (const HistorgramArray &compressable, int n) |
create compression table More... | |
std::ostream & | write_stub (std::ostream &out, int n, uint16_t size, uint16_t loadaddr, uint16_t jmp) |
write the decrunch stub More... | |
std::ostream & | write_compression_table (std::ostream &out, int n, const HistorgramArray &histe) |
write compression table More... | |
std::ostream & | write_compressed_data (std::ostream &out, const std::vector< uint8_t > &data) |
write the compressed data More... | |
std::vector< uint8_t > | create_compressed_data (const Data &data, const CompressionBits &compbits) |
Create compressed data. More... | |
void | output_64_common (const HistorgramArray &shisto) |
int | choose_optimal_n (const Data &data, const HistorgramArray &shisto) |
choose optimal number of bits More... | |
int | main_xipz (const std::string &inputname, const std::string &outputname, bool raw, int jump) |
main function using xip More... | |
int | main_qadz (const std::string &inputname, const std::string &outputname, bool raw, int jump) |
main function using qadz More... | |
int | main (int argc, char **argv) |
main function using xip More... | |
Source file containing the main function.
Currently contains the main function of the program and the xipz compression algorithm.
Definition in file xipz.cc.
typedef std::vector<HistEntry> HistorgramArray |
float calc_comp | ( | int | n, |
const Data & | data, | ||
const HistorgramArray & | histarr | ||
) |
HistorgramArray calc_histo | ( | const Data & | data | ) |
calculate the histogram
For each byte in the input data the frequency is calculated.
The histogram entries are sorted according to their frequencies. The most common bytes are moved to teh beginning of the array.
data | binary data |
int choose_optimal_n | ( | const Data & | data, |
const HistorgramArray & | shisto | ||
) |
std::vector<uint8_t> create_compressed_data | ( | const Data & | data, |
const CompressionBits & | compbits | ||
) |
Create compressed data.
The table of compression bits (CompressionBits) is all that is needed to compress the data in data. Every output token is prepended with a '1' bit if it is a literal token (aka byte) or with a '0' bit if it is compressed data. In this case only the bits in the compbits table are written.
If not a whole byte is filled at the end, then zero bits are appended.
data | binary data to crunch |
compbits | compression table |
CompressionBits create_compression_bits | ( | const HistorgramArray & | compressable, |
int | n | ||
) |
create compression table
Generates a table of 256 entries where the n bits are stored according to their frequency or the eight bits of the byte value if not compressed.
compressable | histogram entries for the generation of the compression table |
n | how many bits to use |
int main | ( | int | argc, |
char ** | argv | ||
) |
int main_qadz | ( | const std::string & | inputname, |
const std::string & | outputname, | ||
bool | raw, | ||
int | jump | ||
) |
main function using qadz
Main function for compression using the qadz (LZ77-like) algorithm.
inputname | input filename |
outputname | outout filename |
raw | should the compressed data be written raw (without decompression stub) |
jump | jump address, -1 = equal to load address |
int main_xipz | ( | const std::string & | inputname, |
const std::string & | outputname, | ||
bool | raw, | ||
int | jump | ||
) |
std::ostream& operator<< | ( | std::ostream & | o, |
const Bits & | x | ||
) |
std::ostream& operator<< | ( | std::ostream & | o, |
const HistEntry & | x | ||
) |
void output_64_common | ( | const HistorgramArray & | shisto | ) |
Data read_data | ( | const std::string & | fname | ) |
std::ostream& write_compressed_data | ( | std::ostream & | out, |
const std::vector< uint8_t > & | data | ||
) |
std::ostream& write_compression_table | ( | std::ostream & | out, |
int | n, | ||
const HistorgramArray & | histe | ||
) |
std::ostream& write_stub | ( | std::ostream & | out, |
int | n, | ||
uint16_t | size, | ||
uint16_t | loadaddr, | ||
uint16_t | jmp | ||
) |
write the decrunch stub
The decrunching stub is written and all parameters like decrunching address and jump address are adjusted. Please take care to adjust all the defines above otherwise the code will probably just crash. It may also fry your cat so be careful!
out | output stream to write to |
n | number of bits |
size | number of compressed bytes |
loadaddr | original load address |
jmp | jump address to jump after decrunching |