unsigned unpack_lzh(u8 *src, unsigned size, u8 *buf) { packed_ptr = src; packed_end = src+size; int i, j, k, c; unsigned count = 0; StartHuff(); // while (count < textsize) // textsize - sizeof unpacked data while (packed_ptr < packed_end) { c = DecodeChar(); if (c < 256) { *buf++ = c; text_buf[r++] = c; r &= (N - 1); count++; } else { i = (r - DecodePosition() - 1) & (N - 1); j = c - 255 + THRESHOLD; for (k = 0; k < j; k++) { c = text_buf[(i + k) & (N - 1)]; *buf++ = c; text_buf[r++] = c; r &= (N - 1); count++; } } } return count; }
void melt2 () { register short i, j, k, r, c; /* Huffman-dependent part */ if(read_header() == EOF) return; StartHuff(N_CHAR2); init(Table2); /* end of Huffman-dependent part */ InitIO(); for (i = 0; i < N2 - F2; i++) text_buf[i] = ' '; r = N2 - F2; for (in_count = 0;; ) { c = DecodeChar(); if (c == ENDOF) break; if (c < 256) { #ifdef DEBUG if (debug) fprintf(stderr, "'%s'\n", pr_char((uc_t)c)); else #endif /* DEBUG */ putchar (c); text_buf[r++] = c; r &= N2 - 1; in_count++; } else { i = (r - DecodePosition() - 1) & (N2 - 1); j = c - 256 + THRESHOLD; #ifdef DEBUG if (debug) fputc('"', stderr); #endif for (k = 0; k < j; k++) { c = text_buf[(i + k) & (N2 - 1)]; #ifdef DEBUG if (debug) fprintf(stderr, "%s", pr_char((uc_t)c)); else #endif putchar (c); text_buf[r++] = c; r &= (N2 - 1); in_count++; } #ifdef DEBUG if (debug) fprintf(stderr, "\"\n"); #endif } INDICATOR } }
int unlzh(unsigned char *in, int insz, unsigned char *out, int outsz) { int i, j, k, r, c; unsigned long int count; infile = in; infilel = in + insz; outfile = out; outfilel = out + outsz; /*textsize = (xgetc(infile)); textsize |= (xgetc(infile) << 8); textsize |= (xgetc(infile) << 16); textsize |= (xgetc(infile) << 24); if (textsize == 0) return(-1);*/ textsize = outsz; StartHuff(); for (i = 0; i < N - F; i++) text_buf[i] = 0x20; r = N - F; for (count = 0; count < textsize; ) { c = DecodeChar(); if (c < 256) { if (xputc(c, outfile) == -1) { return(-1); } text_buf[r++] = (unsigned char)c; r &= (N - 1); count++; } else { i = (r - DecodePosition() - 1) & (N - 1); j = c - 255 + THRESHOLD; for (k = 0; k < j; k++) { c = text_buf[(i + k) & (N - 1)]; if (xputc(c, outfile) == -1) { return(-1); } text_buf[r++] = (unsigned char)c; r &= (N - 1); count++; } } } return(outfile - out); }
int td0dsk_t::Decode(UINT8 *buf, int len) /* Decoding/Uncompressing */ { INT16 c,pos; int count; // was an unsigned long, seems unnecessary for (count = 0; count < len; ) { if(tdctl.bufcnt == 0) { if((c = DecodeChar()) < 0) return(count); // fatal error if (c < 256) { *(buf++) = c; text_buf[tdctl.r++] = c; tdctl.r &= (N - 1); count++; } else { if((pos = DecodePosition()) < 0) return(count); // fatal error tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1); tdctl.bufcnt = c - 255 + THRESHOLD; tdctl.bufndx = 0; } } else { // still chars from last string while( tdctl.bufndx < tdctl.bufcnt && count < len ) { c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)]; *(buf++) = c; tdctl.bufndx++; text_buf[tdctl.r++] = c; tdctl.r &= (N - 1); count++; } // reset bufcnt after copy string from text_buf[] if(tdctl.bufndx >= tdctl.bufcnt) tdctl.bufndx = tdctl.bufcnt = 0; } } return(count); // count == len, success }
USHORT Unpack_DEEP(UCHAR *in, UCHAR *out, USHORT origsize){ USHORT i, j, c; UCHAR *outend; initbitbuf(in); if (dms_init_deep_tabs) Init_DEEP_Tabs(); outend = out+origsize; while (out < outend) { c = DecodeChar(); if (c < 256) { *out++ = dms_text[dms_deep_text_loc++ & DBITMASK] = (UCHAR)c; } else { j = (USHORT) (c - 255 + THRESHOLD); i = (USHORT) (dms_deep_text_loc - DecodePosition() - 1); while (j--) *out++ = dms_text[dms_deep_text_loc++ & DBITMASK] = dms_text[i++ & DBITMASK]; } } dms_deep_text_loc = (USHORT)((dms_deep_text_loc+60) & DBITMASK); return 0; }
COUNT cread (void *buf, COUNT size, COUNT count, PLZHCODE_DESC lpCodeDesc) { COUNT r, j, i; BYTE *lpStr; if ((_lpCurCodeDesc = lpCodeDesc) == 0) return (0); size *= count; if (lpCodeDesc->StreamIndex + size > lpCodeDesc->StreamLength) { size /= count; count = (COUNT)((lpCodeDesc->StreamLength - lpCodeDesc->StreamIndex) / size); size *= count; } if (size == 0) return (0); lpStr = (BYTE*)buf; _StreamType = lpCodeDesc->StreamType; _Stream = lpCodeDesc->Stream; _workbuf = lpCodeDesc->workbuf; _workbuflen = lpCodeDesc->workbuflen; lpCodeDesc->StreamIndex += size; r = lpCodeDesc->buf_index; j = lpCodeDesc->bytes_left; if (j) { lpCodeDesc->bytes_left = 0; i = lpCodeDesc->restart_index; goto ReenterRun; } do { COUNT c; DecodeChar (&c); if (c < 256) { size--; *lpStr++ = lpCodeDesc->text_buf[r++ & (N - 1)] = (BYTE)c; } else { COUNT copy_size; //i is a COUNT; DecodePosition(&i); i = r - i - 1; j = c - 255 + THRESHOLD; ReenterRun: if (j > size) { lpCodeDesc->bytes_left = j - size; lpCodeDesc->restart_index = i + size; j = size; } size -= j; do { COUNT loc_size; i &= (N - 1); r &= (N - 1); if ((i < r && i + j > r) || (i > r && i + j > r + N)) copy_size = (r - i) & (N - 1); else if ((copy_size = j) > N) copy_size = N; loc_size = copy_size; if (i + loc_size > N) { COUNT k; k = N - i; memcpy (lpStr, &lpCodeDesc->text_buf[i], k); lpStr += k; loc_size -= k; i = 0; } memcpy (lpStr, &lpCodeDesc->text_buf[i], loc_size); lpStr += loc_size; i += loc_size; lpStr -= copy_size; loc_size = copy_size; if (r + loc_size > N) { COUNT k; k = N - r; memcpy (&lpCodeDesc->text_buf[r], lpStr, k); lpStr += k; loc_size -= k; r = 0; } memcpy (&lpCodeDesc->text_buf[r], lpStr, loc_size); lpStr += loc_size; r += loc_size; } while (j -= copy_size); } } while (size); lpCodeDesc->buf_index = r; lpCodeDesc->Stream = _Stream; lpCodeDesc->workbuf = _workbuf; lpCodeDesc->workbuflen = _workbuflen; return (count); }