[Scummvm-cvs-logs] SF.net SVN: scummvm: [32646] scummvm/trunk/engines/cine

buddha_ at users.sourceforge.net buddha_ at users.sourceforge.net
Tue Jun 10 21:08:53 CEST 2008


Revision: 32646
          http://scummvm.svn.sourceforge.net/scummvm/?rev=32646&view=rev
Author:   buddha_
Date:     2008-06-10 12:08:53 -0700 (Tue, 10 Jun 2008)

Log Message:
-----------
Objectified the Delphine decompression routine (i.e. delphineUnpack).

Modified Paths:
--------------
    scummvm/trunk/engines/cine/part.cpp
    scummvm/trunk/engines/cine/unpack.cpp
    scummvm/trunk/engines/cine/unpack.h

Modified: scummvm/trunk/engines/cine/part.cpp
===================================================================
--- scummvm/trunk/engines/cine/part.cpp	2008-06-10 18:56:59 UTC (rev 32645)
+++ scummvm/trunk/engines/cine/part.cpp	2008-06-10 19:08:53 UTC (rev 32646)
@@ -127,7 +127,8 @@
 	uint8 *buf = new uint8[unpackedSize];
 	f.read(buf, packedSize);
 	if (packedSize != unpackedSize) {
-		if (!delphineUnpack(buf, buf, packedSize)) {
+		CineUnpacker cineUnpacker;
+		if (!cineUnpacker.unpack(buf, buf, packedSize)) {
 			error("Error while unpacking 'vol.cnf' data");
 		}
 	}
@@ -224,7 +225,8 @@
 	if (partBuffer[foundFileIdx].unpackedSize != partBuffer[foundFileIdx].packedSize) {
 		byte *unpackBuffer = (byte *)malloc(partBuffer[foundFileIdx].packedSize);
 		readFromPart(foundFileIdx, unpackBuffer);
-		delphineUnpack(dataPtr, unpackBuffer, partBuffer[foundFileIdx].packedSize);
+		CineUnpacker cineUnpacker;
+		cineUnpacker.unpack(dataPtr, unpackBuffer, partBuffer[foundFileIdx].packedSize);
 		free(unpackBuffer);
 	} else {
 		readFromPart(foundFileIdx, dataPtr);

Modified: scummvm/trunk/engines/cine/unpack.cpp
===================================================================
--- scummvm/trunk/engines/cine/unpack.cpp	2008-06-10 18:56:59 UTC (rev 32645)
+++ scummvm/trunk/engines/cine/unpack.cpp	2008-06-10 19:08:53 UTC (rev 32646)
@@ -30,94 +30,91 @@
 
 namespace Cine {
 
-struct UnpackCtx {
-	int size, datasize;
-	uint32 crc;
-	uint32 chk;
-	byte *dst;
-	const byte *src;
-};
+uint32 CineUnpacker::readSource() {
+	uint32 value = READ_BE_UINT32(_src);
+	_src -= 4;
+	return value;
+}
 
-static int rcr(UnpackCtx *uc, int CF) {
-	int rCF = (uc->chk & 1);
-	uc->chk >>= 1;
+int CineUnpacker::rcr(int CF) {
+	int rCF = (_chk & 1);
+	_chk >>= 1;
 	if (CF) {
-		uc->chk |= 0x80000000;
+		_chk |= 0x80000000;
 	}
 	return rCF;
 }
 
-static int nextChunk(UnpackCtx *uc) {
-	int CF = rcr(uc, 0);
-	if (uc->chk == 0) {
-		uc->chk = READ_BE_UINT32(uc->src); uc->src -= 4;
-		uc->crc ^= uc->chk;
-		CF = rcr(uc, 1);
+int CineUnpacker::nextChunk() {
+	int CF = rcr(0);
+	if (_chk == 0) {
+		_chk = readSource();
+		_crc ^= _chk;
+		CF = rcr(1);
 	}
 	return CF;
 }
 
-static uint16 getCode(UnpackCtx *uc, byte numChunks) {
+uint16 CineUnpacker::getCode(byte numChunks) {
 	uint16 c = 0;
 	while (numChunks--) {
 		c <<= 1;
-		if (nextChunk(uc)) {
+		if (nextChunk()) {
 			c |= 1;
 		}
 	}
 	return c;
 }
 
-static void unpackHelper1(UnpackCtx *uc, byte numChunks, byte addCount) {
-	uint16 count = getCode(uc, numChunks) + addCount + 1;
-	uc->datasize -= count;
+void CineUnpacker::unpackHelper1(byte numChunks, byte addCount) {
+	uint16 count = getCode(numChunks) + addCount + 1;
+	_datasize -= count;
 	while (count--) {
-		*uc->dst = (byte)getCode(uc, 8);
-		--uc->dst;
+		*_dst = (byte)getCode(8);
+		--_dst;
 	}
 }
 
-static void unpackHelper2(UnpackCtx *uc, byte numChunks) {
-	uint16 i = getCode(uc, numChunks);
-	uint16 count = uc->size + 1;
-	uc->datasize -= count;
+void CineUnpacker::unpackHelper2(byte numChunks) {
+	uint16 i = getCode(numChunks);
+	uint16 count = _size + 1;
+	_datasize -= count;
 	while (count--) {
-		*uc->dst = *(uc->dst + i);
-		--uc->dst;
+		*_dst = *(_dst + i);
+		--_dst;
 	}
 }
 
-bool delphineUnpack(byte *dst, const byte *src, int len) {
-	UnpackCtx uc;
-	uc.src = src + len - 4;
-	uc.datasize = READ_BE_UINT32(uc.src); uc.src -= 4;
-	uc.dst = dst + uc.datasize - 1;
-	uc.size = 0;
-	uc.crc = READ_BE_UINT32(uc.src); uc.src -= 4;
-	uc.chk = READ_BE_UINT32(uc.src); uc.src -= 4;
-	uc.crc ^= uc.chk;
+bool CineUnpacker::unpack(byte *dst, const byte *src, int srcLen) {
+	_src = src + srcLen - 4;
+	_datasize = readSource();
+	_dst = dst + _datasize - 1;
+	_size = 0;
+	_crc = readSource();
+	_chk = readSource();
+	_crc ^= _chk;
 	do {
-		if (!nextChunk(&uc)) {
-			uc.size = 1;
-			if (!nextChunk(&uc)) {
-				unpackHelper1(&uc, 3, 0);
+		if (!nextChunk()) {
+			_size = 1;
+			if (!nextChunk()) {
+				unpackHelper1(3, 0);
 			} else {
-				unpackHelper2(&uc, 8);
+				unpackHelper2(8);
 			}
 		} else {
-			uint16 c = getCode(&uc, 2);
+			uint16 c = getCode(2);
 			if (c == 3) {
-				unpackHelper1(&uc, 8, 8);
+				unpackHelper1(8, 8);
 			} else if (c < 2) {
-				uc.size = c + 2;
-				unpackHelper2(&uc, c + 9);
+				_size = c + 2;
+				unpackHelper2(c + 9);
 			} else {
-				uc.size = getCode(&uc, 8);
-				unpackHelper2(&uc, 12);
+				_size = getCode(8);
+				unpackHelper2(12);
 			}
 		}
-	} while (uc.datasize > 0 && uc.src >= src - 4);
-	return uc.crc == 0;
+	} while (_datasize > 0 && _src >= src - 4);
+	return _crc == 0;
 }
 
 } // End of namespace Cine

Modified: scummvm/trunk/engines/cine/unpack.h
===================================================================
--- scummvm/trunk/engines/cine/unpack.h	2008-06-10 18:56:59 UTC (rev 32645)
+++ scummvm/trunk/engines/cine/unpack.h	2008-06-10 19:08:53 UTC (rev 32646)
@@ -31,7 +31,25 @@
 
 namespace Cine {
 
-bool delphineUnpack(byte *dst, const byte *src, int len);
+class CineUnpacker {
+public:
+	/** Returns true if unpacking was successful, otherwise false. */
+	bool unpack(byte *dst, const byte *src, int srcLen);
+private:
+	/** Reads a single big endian 32-bit integer from the source and goes backwards 4 bytes. */
+	uint32 readSource();
+	int rcr(int CF);
+	int nextChunk();
+	uint16 getCode(byte numChunks);
+	void unpackHelper1(byte numChunks, byte addCount);
+	void unpackHelper2(byte numChunks);
+private:
+	int _size, _datasize;
+	uint32 _crc;
+	uint32 _chk;
+	byte *_dst;
+	const byte *_src;
+};
 
 } // End of namespace Cine
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list