From decwrl!labrea!eos!ames!mailrus!csd4.milw.wisc.edu!leah!itsgw!steinmetz!uunet!allbery Sun Jan 29 16:45:48 PST 1989 Article 788 of comp.sources.misc: Path: granite!decwrl!labrea!eos!ames!mailrus!csd4.milw.wisc.edu!leah!itsgw!steinmetz!uunet!allbery From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) Newsgroups: comp.sources.misc Subject: v06i013: u16 patch 1 Message-ID: <47279@uunet.UU.NET> Date: 24 Jan 89 03:07:23 GMT Sender: allbery@uunet.UU.NET Reply-To: tom@SSD.HARRIS.COM (Tom Horsley) Lines: 157 Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) Posting-number: Volume 6, Issue 13 Submitted-by: tom@SSD.HARRIS.COM (Tom Horsley) Archive-name: u16.patch1 [This just about clears out the comp.sources.misc queue -- there are 4 more submissions to go, but they require further thought. ++bsa] This is patch number 1 (and hopefully patch number last) to the u16 16 bit uncompress for the PC. ----------------------------cut here------------------------------- *** u16.c.orig Tue Jan 3 15:34:51 1989 --- u16.c Tue Jan 3 15:34:28 1989 *************** *** 6,11 **** --- 6,23 ---- * Parts written (other parts plagarized) by Tom Horsley * (tahorsley@ssd.harris.com) * Dec 1988. + * + * Version 1.1 + * + * Bug1 - fixed Dec 22, 1988 + * The initial check for end of file that attempted to just use + * the current byte pointer was not good enough. Because the byte + * pointer might look within bounds, but a large code might eat + * some garbage bits beyond the end as it is assembled. + * + * The fix was to add a new variable to keep track of the count + * of bits in the buffer and always check to see that there are + * enough bits for at least one new code to be extracted. */ #include #include *************** *** 82,88 **** * message you get with the -H option. */ unsigned char buf[MAXBUF] = "\ ! u16 - 16 bit LZW uncompress for the IBM PC\n\ u16 [-H] [files...]\n\ \n\ -H\tPrint this message and exit.\n\ --- 94,100 ---- * message you get with the -H option. */ unsigned char buf[MAXBUF] = "\ ! u16 - 16 bit LZW uncompress for the IBM PC, version 1.1\n\ u16 [-H] [files...]\n\ \n\ -H\tPrint this message and exit.\n\ *************** *** 97,102 **** --- 109,119 ---- on the ends of file names.\n" ; + /* Number of bits in the buffer. + */ + long bitsinbuf = 0; + long n_bits; + /* Number of bytes of file data resident in buf. */ int bufsize = 0; *************** *** 167,172 **** --- 184,190 ---- eofmark = &buf[bufsize]; } else { bufsize += cursize; + bitsinbuf += (((long)cursize) << 3); } } if (eofmark == NULL) { *************** *** 203,210 **** * have recieved a clear code then flush the current size code * and advance to next size. */ ! while (cb.codep != vartab[curvartab].origp) xcode(&cb); ! if (cb.bufp >= endbuf) return(-1L); if (clear_flg > 0) { curvartab = 0; clear_flg = 0; --- 221,230 ---- * have recieved a clear code then flush the current size code * and advance to next size. */ ! while (cb.codep != vartab[curvartab].origp) { ! xcode(&cb); ! bitsinbuf -= n_bits; ! } if (clear_flg > 0) { curvartab = 0; clear_flg = 0; *************** *** 220,232 **** (*vartab[curvartab].initp)(&cb); vartab[curvartab].origp = cb.codep; maxcode = vartab[curvartab].maxcode; #ifdef DEBUG fprintf(stderr, "switching to %d bit codes, bytes_out = %ld, free_ent = %ld\n", ! vartab[curvartab].n_bits,bytes_out, free_ent); #endif } ! return (long)(xcode(&cb)); } /* Decompress stdin to stdout. This routine adapts to the codes in --- 240,258 ---- (*vartab[curvartab].initp)(&cb); vartab[curvartab].origp = cb.codep; maxcode = vartab[curvartab].maxcode; + n_bits = (long)vartab[curvartab].n_bits; #ifdef DEBUG fprintf(stderr, "switching to %d bit codes, bytes_out = %ld, free_ent = %ld\n", ! n_bits,bytes_out, free_ent); #endif } ! bitsinbuf -= n_bits; ! if (bitsinbuf < 0) { ! return -1L; ! } else { ! return (long) xcode(&cb); ! } } /* Decompress stdin to stdout. This routine adapts to the codes in *************** *** 270,276 **** --- 296,304 ---- /* Read the iniital buffer worth of data and check magic numbers * and flags. */ + bitsinbuf = 0; ReadBuf(); + bitsinbuf -= 3 * 8; if (bufsize < 3) { fputs("u16: Missing file header.\n",stderr); return 1; *************** *** 298,303 **** --- 326,332 ---- /* * initialize the first 256 entries in the table. */ + n_bits = vartab[0].n_bits; maxcode = vartab[0].maxcode; for ( code = 255; code >= 0; code-- ) { tab_prefixof(code) = 0; --------------------------- cut here -------------------------------- ===================================================================== usenet: tahorsley@ssd.harris.com USMail: Tom Horsley compuserve: 76505,364 511 Kingbird Circle genie: T.HORSLEY Delray Beach, FL 33444