rendered paste body#include <iostream>#include <fstream>#include <cstdlib>#include <string.h>using namespace std;extern fstream out;extern fstream in;ifstream inputfile;ifstream::pos_type file_position;struct slz{ int signature; // "SLZ*" * - rozne wartosci int data_size; // total data size (-header) int unpacked_size; // unpack size int offset; // next file offset (or total current file size)} __attribute__ ((__packed__));intslz_triace_blah (int x, int n, int a, unsigned char **o, unsigned char *outl){ unsigned char * t = *o; printf (" slz_triace_blah; x = %x n = %x a = %x \n", x, n, a); if (x < n) { printf (" x<n \n"); n = (((n - x) - 1) >> 1) + 1; x += n * 2; printf (" .. slz_triace x %X n %X a %X\n", x, n, a); if ((t + (n * 2)) > outl) return (-1); printf (" .. slz_triace ->> "); while (n--) { *t++ = a; *t++ = a >> 8; printf (" .. slz_triace %X %X", a, a >> 8); } printf ("\n"); *o = t; } return (x);}int slzDecompress ( unsigned char *bufferin, unsigned char *bufferout, unsigned int insize, unsigned int outsize, int type){ unsigned char * src = bufferin; unsigned char * dst = bufferout; cout << "preparation" << endl; int flag, n, x, a; for (flag = 0; dst < bufferout + outsize; flag >>= 1) { printf (" -- at position %x \n", dst - bufferout); if (!(flag & 0xff00)) { cout << " -- reading the control byte" << endl; if (src >= bufferin + insize) break; flag = 0xff00 | *src++; printf (" -- we have %2X as a control byte \n ", (unsigned int)(flag & 0xFF)); } if (flag & 1) { //bit = 1 printf( " -- current bit is 1 - copying 1 byte of input \n"); if (src >= bufferin + insize) break; //za malo danych, potrzebny chociaz 1 bajt if (dst >= bufferout + outsize) return (-1); //za duzo danych wygenerowane printf(" copy: copying %X to output \n", (unsigned int) (*src)); *dst++ = *src++; //kopiujemy 1 bajt i do przodu } else { //bit = 0 - lzss cout << " -- 0 bit found; doing the lzss " << endl; if (src + 2 > bufferin + insize) break; //do we have 2 more bytes of input? n = *src++; //grab 1 byte and go forth n |= (*src++ << 8); //grab another byte and put it "over" the previous byte x = ((n >> 12) & 0x0f) + 3; //x - howMany , n - offset n &= 0x0fff; //we only need the lower 12 bits of n printf (" lzss : n = %x x = %x \n", n, x); if ((x < 0x12) || (type != 2)) { printf( " lzss : x< 0x12 or compression type != 2 \n"); if ((dst - n) < bufferout) //we cannot reference before the beginning of the data return (-1); if ((dst + x) > bufferout + outsize) //if we have more bytes than the output can hold return (-1); //we bail out printf (" lzss: copy %X bytes from position %X in output ", x, (unsigned int) (dst - bufferin - n)); while (x--) { printf (" %X ", (unsigned int) *(dst - n)); *dst = *(dst - n); //we copy a byte from n bytes earlier dst++; //we do so x times } printf ("\n"); } else { cout << " lzss : x >= 0x12 and type = 2 " << endl; if (n < 0x100) { cout << " lzss: n < 0x100" << endl; if (src >= bufferin + insize) //if there is no more input break; //bail a = *src++; n += 0x13; printf (" . lzss : x %x n %x a %x \n", x, n, a); } else { cout << " lzss : n>= 0x100" << endl; a = n & 0xff; n = (((n >> 8) & 0xf) + 3); printf (" lzss: x %x n %x a %x \n", x, n, a); } // n< -0x100 a |= (a << 8); if ((dst - bufferout) & 1) { //odd amount of bytes left? if (dst > bufferout + outsize) //are we past last byte? return (-1); printf (" copying %X to output \n", a); *dst++ = a; if (n & 1) { printf (" lzss : n &1 \n"); printf (" lzss : doing n = ((n - 2) >> 1) + 1; \n"); n = ((n - 2) >> 1) + 1; printf (" lzss : n = %x \n", n); if ((dst + (n * 2)) > bufferout + outsize) return (-1); //can we fit n*2 bytes? printf (" lzss: copy %x bytes from %X \n", n, a); while (n--) { printf (" %X ", (unsigned int) a); *dst++ = a; *dst++ = a >> 8; } printf (" copied \n"); } else if ((n - 1) > 1) { printf (" (n - 1) >1 \n"); x =slz_triace_blah (1, n - 1, a, &dst, bufferout + outsize); if (x < 0) return (-1); if (x < n) { if (dst >= bufferout + outsize) return (-1); *dst++ = a; } } else if (n > 1) { if (dst >= bufferout + outsize) return (-1); *dst++ = a; } else { x = slz_triace_blah (0, n - 1, a, &dst, bufferout + outsize); if (x < 0) return (-1); if (x < n) { if (dst >= bufferout + outsize) return (-1); *dst++ = a; } } } else { printf(" x %X n %X a %X \n", x,n,a); printf( " .. slz_triace_blah(0, n - 1, a, &dst, dst+outsize) \n"); x = slz_triace_blah(0, n - 1, a, &dst, dst+outsize); printf(" . x %X n %X a %X \n", x,n,a); printf(" is x > 0 ? \n"); if(x < 0) return(-1); printf(" is x < n ?\n"); if(x < n) { if(dst >= dst+outsize) return(-1); printf(" copying %X to output \n" , a); *dst++ = a; } } } } } return (dst - bufferout); //did we finish?}boolverify_header (ifstream & input){ cout << " verify header" << endl; slz naglowek; memset (&naglowek, 0, sizeof (naglowek)); input.read (reinterpret_cast < char *>(&naglowek), sizeof (naglowek)); printf (" signature %x \n", naglowek.signature & 0x00FFFFFF); printf (" flag %x \n", naglowek.signature >> 24); printf (" unpacked %x \n", naglowek.unpacked_size); printf (" offset %x \n", naglowek.offset); char * tmp = reinterpret_cast < char *>(&naglowek.signature); char type[4]; strncpy (type, tmp, sizeof (type) - 1); type[sizeof (type) - 1] = '\0'; printf (" so we have %s file with compression %d \n", type, naglowek.signature >> 24); char * data; char * data_out; data = new char[naglowek.data_size]; data_out = new char[naglowek.unpacked_size]; memset (data, 0, sizeof (data)); memset (data_out, 0, sizeof (data_out)); input.seekg (0x10, ios::beg); input.read (data, naglowek.data_size); cout << "unpack" << endl; if (!(slzDecompress ((unsigned char *) data, (unsigned char *) data_out, naglowek.data_size, naglowek.unpacked_size, naglowek.signature >> 24))) //naglowek.unpacked_size); cout << " unpack ok " << endl; ofstream output; output.open ("output.dat"); if (output.is_open ()) { output.write (data_out, naglowek.unpacked_size); //sizeof (&data_out)); output.close (); printf ("wrote output file\n"); } return true;}intmain (int argc, char *argv[]){ if (!(argc == 2)) { cout << "specify one input file" << endl; } inputfile.open (argv[1], ios::in | ios::binary); if (!inputfile.is_open ()) { cout << "failed to open file" << endl; return 1; } cout << "file open" << endl; file_position = 0; inputfile.seekg (file_position, ios::beg); if (verify_header (inputfile)) cout << " looks good" << endl; inputfile.close (); return 0;}