Skip to content

Commit

Permalink
Merge branch 'AIOOBE_in_Tree.d_code' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ymnk committed Oct 2, 2013
2 parents 0fa43af + e2fee8f commit dd3a62e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/main/java/com/jcraft/jzlib/Deflate.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ static class Config{
// Depth of each subtree used as tie breaker for trees of equal frequency
byte[] depth=new byte[2*L_CODES+1];

int l_buf; // index for literals or lengths */
byte[] l_buf; // index for literals or lengths */

// Size of match buffer for literals/lengths. There are 4 reasons for
// limiting lit_bufsize to 64K:
Expand Down Expand Up @@ -632,7 +632,7 @@ boolean _tr_tally (int dist, // distance of matched string
pending_buf[d_buf+last_lit*2] = (byte)(dist>>>8);
pending_buf[d_buf+last_lit*2+1] = (byte)dist;

pending_buf[l_buf+last_lit] = (byte)lc; last_lit++;
l_buf[last_lit] = (byte)lc; last_lit++;

if (dist == 0) {
// lc is the unmatched char
Expand Down Expand Up @@ -677,7 +677,7 @@ void compress_block(short[] ltree, short[] dtree){
do{
dist=((pending_buf[d_buf+lx*2]<<8)&0xff00)|
(pending_buf[d_buf+lx*2+1]&0xff);
lc=(pending_buf[l_buf+lx])&0xff; lx++;
lc=(l_buf[lx])&0xff; lx++;

if(dist == 0){
send_code(lc, ltree); // send a literal byte
Expand Down Expand Up @@ -1381,11 +1381,11 @@ else if(windowBits > 15){

// We overlay pending_buf and d_buf+l_buf. This works since the average
// output size for (length,distance) codes is <= 24 bits.
pending_buf = new byte[lit_bufsize*4];
pending_buf_size = lit_bufsize*4;
pending_buf = new byte[lit_bufsize*3];
pending_buf_size = lit_bufsize*3;

d_buf = lit_bufsize/2;
l_buf = (1+2)*lit_bufsize;
d_buf = lit_bufsize;
l_buf = new byte[lit_bufsize];

this.level = level;

Expand Down Expand Up @@ -1422,6 +1422,7 @@ int deflateEnd(){
}
// Deallocate in reverse order of allocations:
pending_buf=null;
l_buf=null;
head=null;
prev=null;
window=null;
Expand Down Expand Up @@ -1699,6 +1700,8 @@ public Object clone() throws CloneNotSupportedException {
Deflate dest = (Deflate)super.clone();

dest.pending_buf = dup(dest.pending_buf);
dest.d_buf = dest.d_buf;
dest.l_buf = dup(dest.l_buf);
dest.window = dup(dest.window);

dest.prev = dup(dest.prev);
Expand Down
Binary file added src/test/resources/jzlib.fail.gz
Binary file not shown.
36 changes: 35 additions & 1 deletion src/test/scala/GZIPIOStreamTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import org.scalatest._
import org.scalatest.matchers.ShouldMatchers

import java.io._
import java.util.zip.CheckedOutputStream
import java.util.zip.CheckedInputStream
import java.util.zip.{GZIPInputStream => juzGZIPInputStream}
import java.util.zip.{CRC32 => juzCRC32}

import JZlib._

Expand All @@ -16,7 +20,7 @@ class GZIPIOStreamTest extends FlatSpec with BeforeAndAfter with ShouldMatchers
after {
}

behavior of "GZipOutputStream and GZipInputStream"
behavior of "GZIPOutputStream and GZIPInputStream"

it can "deflate and infate data." in {

Expand Down Expand Up @@ -53,4 +57,34 @@ class GZIPIOStreamTest extends FlatSpec with BeforeAndAfter with ShouldMatchers

crc32.getValue should equal(gis.getCRC.asInstanceOf[Long])
}

behavior of "GZIPOutputStream"

// https://github.com/ymnk/jzlib/issues/9
// https://github.com/jglick/jzlib-9-demo
it can "deflate some file without AIOOBE." in {
val pos = new PipedOutputStream()
val pis = new PipedInputStream(pos)
val csOut = new juzCRC32()
val gos = new GZIPOutputStream(pos)
val cos = new CheckedOutputStream(gos, csOut)

val t = new Thread() {
override def run = {
val fail = "/jzlib.fail.gz".fromResource
val fis = new juzGZIPInputStream(new ByteArrayInputStream(fail))
fis -> cos
cos.close()
}
}
t.start();

val gis = new GZIPInputStream(pis)
val csIn = new juzCRC32();
new CheckedInputStream(gis, csIn) -> new ByteArrayOutputStream()

t.join()

csIn.getValue() should equal(csOut.getValue)
}
}
18 changes: 15 additions & 3 deletions src/test/scala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import java.io._

package object jzlib {
implicit def readIS(is: InputStream) = new {
def ->(out: OutputStream)(implicit buf: Array[Byte]) = {
Stream.continually(is.read(buf)).
takeWhile(-1 !=).foreach(i => out.write(buf, 0, i))
def ->(out: OutputStream)
(implicit buf: Array[Byte] = new Array[Byte](1024)) = {
Stream.
continually(is.read(buf)).
takeWhile(-1 !=).
foreach(i => out.write(buf, 0, i))
is.close
}
}

// reading a resource file
implicit def fromResource(str: String ) = new {
def fromResource: Array[Byte] =
io.Source.
fromURL(getClass.getResource(str))(io.Codec.ISO8859).
map(_.toByte).
toArray
}

implicit def readArray(is: Array[Byte]) = new {
def ->(out: OutputStream)(implicit buf: Array[Byte]) = {
new ByteArrayInputStream(is) -> (out)
Expand Down

0 comments on commit dd3a62e

Please sign in to comment.