grepwood

f1_unlzss.c

Feb 17th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #define N 4096
  2. #define F 18
  3. #define THRESHOLD 2
  4. #define GETBYTE() ((dwInputCurrent < dwInputLength) ? buffer[dwInputCurrent++] & 0x00FF : 0xffff)
  5. /* Special thanks to Anchorite from TeamX.ru */
  6. void LZSSDecode(char* buffer, int dwInputLength, char* arOut) {
  7.     uint16_t i;
  8.     uint16_t j;
  9.     uint16_t k;
  10.     int16_t c;
  11.     int dictionaryIndex = N - F;
  12.     unsigned int flags = 0;
  13.     int dwInputCurrent = 0;
  14.     int dwOutputIndex = 0;
  15.     char * text_buf = malloc(N+F-1);
  16.     memset(text_buf,' ',dictionaryIndex);
  17.     for ( ; ; ) {
  18.         if (((flags >>= 1) & 256) == 0) {
  19.             if ((c = GETBYTE()) == 0xffff) break;
  20.             flags = c | 0xff00;     /* uses higher byte to count 8 */
  21.         }
  22.         if (flags & 1) {
  23.             if ((c = GETBYTE()) == 0xffff) break;
  24. //          arOut[dwOutputIndex++] = c;
  25.             dwOutputIndex++;
  26.             text_buf[dictionaryIndex++] = c;
  27.             dictionaryIndex &= (N - 1);
  28.         }
  29.         else {
  30.             if ((i = GETBYTE()) == 0xffff) break;
  31.             if ((j = GETBYTE()) == 0xffff) break;
  32.             i |= ((j & 0xf0) << 4);
  33.             j = (j & 0x0f) + THRESHOLD;
  34.             for (k = 0; k <= j; k++) {
  35.                 c = text_buf[(i + k) & (N - 1)];
  36. //              arOut[dwOutputIndex++] = c;
  37.                 dwOutputIndex++;
  38.                 text_buf[dictionaryIndex++] = c;
  39.                 dictionaryIndex &= (N - 1);
  40.             }
  41.         }
  42.     }
  43.     printf("Output would be %i\n",dwOutputIndex);
  44.     free(text_buf);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment