int windowSize = 4096; char *dict = new char[4096]; // Fill dictionary with spaces memset(dict,'\0x20',4096); int offset = 0; unsigned int O = 0; unsigned char L = 0; unsigned char FLAGS = 0; unsigned char byte = 0; short N = 0; // 0. Set dictionary offset to D-18 offset = windowSize - 18; // 1. If at the end of file, exit. while(filesize > 0) { // 2. Read N (2 bytes == word) from input. // The absolute value of N is how many bytes of data to read (if N=0, exit) DAT1Stream.read((char *)&N,sizeof(N)); filesize-=sizeof(N); // Flip endianness N = (N>>8) | (N<<8); if (N == 0) return; // 2.1. If N < 0, read the absolute value // of N bytes from input and write to output (do not put into dictionary). Go to 1. if (N < 0) { char *output = new char[N*-1]; DAT1Stream.read((char *)output,N*-1); filesize -= N*-1; fout.write((char *)output,N*-1); continue; } // 2.2. If N > 0, clear dictionary, repeat below until N bytes have been read from input, // and then go to 1. if (N > 0) { // Clear dictionary (with spaces, not null) memset(dict,'\0x20',4096); while (N > 0) { // 2.2.1. Read FLAGS (1 byte) from input. Repeat below 8 times. DAT1Stream.read((char *)&FLAGS,sizeof(FLAGS)); filesize -= sizeof(FLAGS); N-=sizeof(FLAGS); // Repeat 8 times for (int i=0;i<8;i++,filesize--) { // 2.2.1.1. If FLAGS *is not* a modulus of 2 (& 1 != 0), //read 1 byte from input, write to output, then go to 2.2.1.3. if ((FLAGS & 1) != 0) { DAT1Stream.read((char *)&byte, sizeof(byte)); filesize--; N--; cout << byte; dict[offset] = byte; if (offset == (windowSize-1)) { offset = 0; } else offset++; FLAGS = FLAGS>>1; } else { // 2.2.1.2. If FLAGS *is* a modulus of 2 (& 1 == 0) O = L = 0; // 2.2.1.2.1. Read O (1 byte) and L (1 byte) from input. DAT1Stream.read((char *)&O,1); DAT1Stream.read((char *)&L,1); filesize -= 2; N-=2; // 2.2.1.2.2. Take away the High-nibble (first 4 bits) from L and prepend it to O; O = (unsigned int)((((unsigned int)L>>4)<<8) | (unsigned int)O); L <<= 4; L >>= 4; printf("O = %d, L = %d dict[O] = %d\n",O,L,dict[O]); for (int m=0;m<(L+3);m++) { cout << dict[O]; O++; } //return; FLAGS = FLAGS>>1; } } } } }