+htc introduce big endian read methods to ByteReader

This commit is contained in:
Johannes Rudolph 2015-04-21 14:29:30 +02:00
parent 4e3c1db4bb
commit 7b72fca3c2
2 changed files with 14 additions and 6 deletions

View file

@ -18,6 +18,8 @@ private[akka] class ByteReader(input: ByteString) {
private[this] var off = 0
def hasRemaining: Boolean = off < input.size
def currentOffset: Int = off
def remainingData: ByteString = input.drop(off)
def fromStartToHere: ByteString = input.take(currentOffset)
@ -28,8 +30,14 @@ private[akka] class ByteReader(input: ByteString) {
off += 1
x.toInt & 0xFF
} else throw NeedMoreData
def readShort(): Int = readByte() | (readByte() << 8)
def readInt(): Int = readShort() | (readShort() << 16)
def readShortLE(): Int = readByte() | (readByte() << 8)
def readIntLE(): Int = readShortLE() | (readShortLE() << 16)
def readLongLE(): Long = (readIntBE() & 0xffffffffL) | ((readIntLE() & 0xffffffffL) << 32)
def readShortBE(): Int = (readByte() << 8) | readByte()
def readIntBE(): Int = (readShortBE() << 16) | readShortBE()
def readLongBE(): Long = ((readIntBE() & 0xffffffffL) << 32) | (readIntBE() & 0xffffffffL)
def skip(numBytes: Int): Unit =
if (off + numBytes <= input.length) off += numBytes
else throw NeedMoreData

View file

@ -88,10 +88,10 @@ class GzipDecompressor(maxBytesPerChunk: Int = Decoder.MaxBytesPerChunkDefault)
if (readByte() != 8) fail("Unsupported GZIP compression method") // check compression method
val flags = readByte()
skip(6) // skip MTIME, XFL and OS fields
if ((flags & 4) > 0) skip(readShort()) // skip optional extra fields
if ((flags & 4) > 0) skip(readShortLE()) // skip optional extra fields
if ((flags & 8) > 0) skipZeroTerminatedString() // skip optional file name
if ((flags & 16) > 0) skipZeroTerminatedString() // skip optional file comment
if ((flags & 2) > 0 && crc16(fromStartToHere) != readShort()) fail("Corrupt GZIP header")
if ((flags & 2) > 0 && crc16(fromStartToHere) != readShortLE()) fail("Corrupt GZIP header")
inflater.reset()
crc32.reset()
@ -107,8 +107,8 @@ class GzipDecompressor(maxBytesPerChunk: Int = Decoder.MaxBytesPerChunkDefault)
def read(reader: ByteReader, ctx: Context[ByteString]): SyncDirective = {
import reader._
if (readInt() != crc32.getValue.toInt) fail("Corrupt data (CRC32 checksum error)")
if (readInt() != inflater.getBytesWritten.toInt /* truncated to 32bit */ ) fail("Corrupt GZIP trailer ISIZE")
if (readIntLE() != crc32.getValue.toInt) fail("Corrupt data (CRC32 checksum error)")
if (readIntLE() != inflater.getBytesWritten.toInt /* truncated to 32bit */ ) fail("Corrupt GZIP trailer ISIZE")
becomeWithRemaining(Initial, remainingData, ctx)
}