6 #include <boost/format.hpp>
11 #include "decrunchqadzstub.inc"
24 #define MAX_PLAIN_LEN 127
29 int decrunch_main(
int argc,
char **argv) {
40 if((inpf = fopen(argv[1],
"r")) == NULL) {
41 perror(
"Can not open file");
46 fprintf(stderr,
"Usage: qadd [FILENAME]\n");
49 while((c = fgetc(inpf)) != EOF) {
54 buf[bufidx++ %
sizeof(buf)] = c;
58 unsigned char pos = fgetc(inpf);
59 for(i = code; i < 0; ++i) {
60 c = buf[(bufidx - pos) %
sizeof(buf)];
62 buf[bufidx++ %
sizeof(buf)] = c;
67 if(inpf != stdin)
if(fclose(inpf) == EOF) perror(
"Error closing file");
72 std::ostream &
write_qadz_stub(std::ostream &out, uint16_t size, uint16_t loadaddr, uint16_t jmp) {
74 std::vector<uint8_t> stub(decrunchqadzstub, decrunchqadzstub + decrunchqadzstub_len);
89 const int POS_OF_DEST_LOW = 0x28;
90 const int POS_OF_DEST_HIGH = 0x2c;
103 stub.at(POS_OF_DEST_LOW) = loadaddr & 0xFF;
104 stub.at(POS_OF_DEST_HIGH) = (loadaddr >> 8) & 0xFF;
108 std::copy(stub.begin(), stub.end(), std::ostream_iterator<unsigned char>(out));
115 long datasize =
static_cast<long>(data.
size());
117 std::vector<uint8_t> buf;
118 std::vector<uint8_t> out;
127 out.push_back(
static_cast<uint8_t
>(buf.size()));
128 copy(buf.begin(), buf.end(), back_inserter(out));
132 void putc(uint8_t c) {
134 if(buf.size() == MAX_PLAIN_LEN) {
138 void puttoken(
int pos,
int len) {
140 out.push_back(
static_cast<uint8_t
>(-len));
141 out.push_back(
static_cast<uint8_t
>(pos));
147 for(
long pos = 0; pos < datasize; ++pos) {
148 long posi = LONG_MIN;
154 if(pos >= LOOK_BACK) {
156 max_look_back = LOOK_BACK;
162 std::cout <<
"pos: " << pos <<
" max_look_back: " << max_look_back << std::endl;
164 for(cmpj = max_look_back; cmpj > 0; --cmpj) {
166 for(cmpi = 0; cmpi < (cmpj < MAX_LEN ? cmpj : MAX_LEN); ++cmpi) {
167 if(pos + cmpi >= datasize) {
171 if(data[pos - cmpj + cmpi] != data[pos + cmpi]) {
184 outclass.putc(data[pos]);
186 outclass.puttoken(posi, match);