[Scummvm-cvs-logs] SF.net SVN: scummvm:[34922] scummvm/trunk/common

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Fri Nov 7 10:46:12 CET 2008


Revision: 34922
          http://scummvm.svn.sourceforge.net/scummvm/?rev=34922&view=rev
Author:   fingolfin
Date:     2008-11-07 09:46:12 +0000 (Fri, 07 Nov 2008)

Log Message:
-----------
Refactoring the Arj decoder code, phase 1

Modified Paths:
--------------
    scummvm/trunk/common/unarj.cpp
    scummvm/trunk/common/unarj.h

Modified: scummvm/trunk/common/unarj.cpp
===================================================================
--- scummvm/trunk/common/unarj.cpp	2008-11-07 08:47:59 UTC (rev 34921)
+++ scummvm/trunk/common/unarj.cpp	2008-11-07 09:46:12 UTC (rev 34922)
@@ -33,6 +33,109 @@
 
 namespace Common {
 
+
+#define ARJ_UCHAR_MAX 255
+#define ARJ_CHAR_BIT 8
+
+#define ARJ_COMMENT_MAX 2048
+#define ARJ_FILENAME_MAX 512
+
+#define ARJ_CODE_BIT 16
+#define ARJ_THRESHOLD 3
+#define ARJ_DICSIZ 26624
+#define ARJ_FDICSIZ ARJ_DICSIZ
+#define ARJ_MAXDICBIT   16
+#define ARJ_MAXMATCH   256
+#define ARJ_NC (ARJ_UCHAR_MAX + ARJ_MAXMATCH + 2 - ARJ_THRESHOLD)
+#define ARJ_NP (ARJ_MAXDICBIT + 1)
+#define ARJ_NT (ARJ_CODE_BIT + 3)
+
+#if ARJ_NT > ARJ_NP
+#define ARJ_NPT ARJ_NT
+#else
+#define ARJ_NPT ARJ_NP
+#endif
+
+#define ARJ_CTABLESIZE 4096
+#define ARJ_PTABLESIZE 256
+
+
+struct ArjHeader {
+	int32 pos;
+	uint16 id;
+	uint16 headerSize;
+	//
+	byte firstHdrSize;
+	byte nbr;
+	byte xNbr;
+	byte hostOs;
+	byte flags;
+	byte method;
+	byte fileType;
+	byte pad;
+	uint32 timeStamp;
+	int32 compSize;
+	int32 origSize;
+	uint32 fileCRC;
+	uint16 entryPos;
+	uint16 fileMode;
+	uint16 hostData;
+	char   filename[ARJ_FILENAME_MAX];
+	char   comment[ARJ_COMMENT_MAX];
+
+	uint32 headerCrc;
+};
+
+class ArjDecoder {
+public:
+	int32 findHeader(SeekableReadStream &stream);
+	ArjHeader *readHeader(SeekableReadStream &stream);
+
+	void decode();
+	void decode_f();
+
+	MemoryReadStream *_compressed;
+	MemoryWriteStream *_outstream;
+
+//protected:
+	uint16 _bitbuf;
+	uint16 _bytebuf;
+	int32 _compsize;
+	int32 _origsize;
+	byte _subbitbuf;
+	int _bitcount;
+
+	void init_getbits();
+	void fillbuf(int n);
+	uint16 getbits(int n);
+	
+
+	void make_table(int nchar, byte *bitlen, int tablebits, uint16 *table, int tablesize);
+	void read_pt_len(int nn, int nbit, int i_special);
+	void read_c_len(void);
+	uint16 decode_c(void);
+	uint16 decode_p(void);
+	void decode_start(void);
+	int16 decode_ptr(void);
+	int16 decode_len(void);
+
+private:
+	byte  _ntext[ARJ_FDICSIZ];
+
+	int16  _getlen;
+	int16  _getbuf;
+
+	uint16 _left[2 * ARJ_NC - 1];
+	uint16 _right[2 * ARJ_NC - 1];
+	byte  _c_len[ARJ_NC];
+	byte  _pt_len[ARJ_NPT];
+
+	uint16 _c_table[ARJ_CTABLESIZE];
+	uint16 _pt_table[ARJ_PTABLESIZE];
+	uint16 _blocksize;
+};
+
+
 #define HEADER_ID     0xEA60
 #define HEADER_ID_HI    0xEA
 #define HEADER_ID_LO    0x60
@@ -77,6 +180,7 @@
 }
 
 ArjFile::ArjFile() : _uncompressedData(NULL) {
+	_decoder = new ArjDecoder;
 	InitCRC();
 	_isOpen = false;
 	_fallBack = false;
@@ -87,6 +191,8 @@
 
 	for (uint i = 0; i < _headers.size(); i++)
 		delete _headers[i];
+	
+	delete _decoder;
 }
 
 void ArjFile::registerArchive(const String &filename) {
@@ -96,7 +202,7 @@
 	if (!_currArchive.open(filename))
 		return;
 
-	first_hdr_pos = findHeader();
+	first_hdr_pos = _decoder->findHeader(_currArchive);
 
 	if (first_hdr_pos < 0) {
 		warning("ArjFile::registerArchive(): Could not find a valid header");
@@ -104,10 +210,10 @@
 	}
 
 	_currArchive.seek(first_hdr_pos, SEEK_SET);
-	if (readHeader() == NULL)
+	if (_decoder->readHeader(_currArchive) == NULL)
 		return;
 
-	while ((header = readHeader()) != NULL) {
+	while ((header = _decoder->readHeader(_currArchive)) != NULL) {
 		_headers.push_back(header);
 
 		_currArchive.seek(header->compSize, SEEK_CUR);
@@ -125,37 +231,37 @@
 // Source for findHeader and readHeader: arj_arcv.c
 //
 
-int32 ArjFile::findHeader(void) {
+int32 ArjDecoder::findHeader(SeekableReadStream &stream) {
 	long end_pos, tmp_pos;
 	int id;
 	byte header[HEADERSIZE_MAX];
 	uint32 crc;
 	uint16 basic_hdr_size;
 
-	tmp_pos = _currArchive.pos();
-	_currArchive.seek(0L, SEEK_END);
-	end_pos = _currArchive.pos() - 2;
+	tmp_pos = stream.pos();
+	stream.seek(0L, SEEK_END);
+	end_pos = stream.pos() - 2;
 	if (end_pos >= tmp_pos + HSLIMIT_ARJ)
 		end_pos = tmp_pos + HSLIMIT_ARJ;
 
 	while (tmp_pos < end_pos) {
-		_currArchive.seek(tmp_pos, SEEK_SET);
-		id = _currArchive.readByte();
+		stream.seek(tmp_pos, SEEK_SET);
+		id = stream.readByte();
 		while (tmp_pos < end_pos) {
 			if (id == HEADER_ID_LO) {
-				if ((id = _currArchive.readByte()) == HEADER_ID_HI)
+				if ((id = stream.readByte()) == HEADER_ID_HI)
 					break;
 			} else
-				id = _currArchive.readByte();
+				id = stream.readByte();
 			tmp_pos++;
 		}
 		if (tmp_pos >= end_pos)
 			return -1;
-		if ((basic_hdr_size = _currArchive.readUint16LE()) <= HEADERSIZE_MAX) {
-			_currArchive.read(header, basic_hdr_size);
+		if ((basic_hdr_size = stream.readUint16LE()) <= HEADERSIZE_MAX) {
+			stream.read(header, basic_hdr_size);
 			crc = GetCRC(header, basic_hdr_size);
-			if (crc == _currArchive.readUint32LE()) {
-				_currArchive.seek(tmp_pos, SEEK_SET);
+			if (crc == stream.readUint32LE()) {
+				stream.seek(tmp_pos, SEEK_SET);
 				return tmp_pos;
 			}
 		}
@@ -164,20 +270,20 @@
 	return -1;
 }
 
-ArjHeader *ArjFile::readHeader() {
+ArjHeader *ArjDecoder::readHeader(SeekableReadStream &stream) {
 	ArjHeader header;
 	ArjHeader *head;
 	byte headData[HEADERSIZE_MAX];
 
 	// Strictly check the header ID
-	header.id = _currArchive.readUint16LE();
+	header.id = stream.readUint16LE();
 	if (header.id != HEADER_ID) {
 		warning("ArjFile::readHeader(): Bad header ID (%x)", header.id);
 
 		return NULL;
 	}
 
-	header.headerSize = _currArchive.readUint16LE();
+	header.headerSize = stream.readUint16LE();
 	if (header.headerSize == 0)
 		return NULL;			// end of archive
 	if (header.headerSize > HEADERSIZE_MAX) {
@@ -186,11 +292,11 @@
 		return NULL;
 	}
 
-	int rSize = _currArchive.read(headData, header.headerSize);
+	int rSize = stream.read(headData, header.headerSize);
 
 	MemoryReadStream readS(headData, rSize);
 
-	header.headerCrc = _currArchive.readUint32LE();
+	header.headerCrc = stream.readUint32LE();
 	if (GetCRC(headData, header.headerSize) != header.headerCrc) {
 		warning("ArjFile::readHeader(): Bad header CRC");
 		return NULL;
@@ -224,10 +330,10 @@
 
     // Process extended headers, if any
     uint16 extHeaderSize;
-	while ((extHeaderSize = _currArchive.readUint16LE()) != 0)
-		_currArchive.seek((long)(extHeaderSize + 4), SEEK_CUR);
+	while ((extHeaderSize = stream.readUint16LE()) != 0)
+		stream.seek((long)(extHeaderSize + 4), SEEK_CUR);
 
-	header.pos = _currArchive.pos();
+	header.pos = stream.pos();
 
 	head = new ArjHeader(header);
 
@@ -257,42 +363,43 @@
 
 	ArjHeader *hdr = _headers[_fileMap[filename]];
 
-	_compsize = hdr->compSize;
-	_origsize = hdr->origSize;
+	_decoder->_compsize = hdr->compSize;
+	_decoder->_origsize = hdr->origSize;
 
 	// FIXME: This hotfix prevents Drascula from leaking memory.
 	// As far as sanity checks go this is not bad, but the engine should be fixed.
-	if (_uncompressedData)
-		free(_uncompressedData);
+	free(_uncompressedData);
 
-	_uncompressedData = (byte *)malloc(_origsize);
-	_outstream = new MemoryWriteStream(_uncompressedData, _origsize);
+	_uncompressedData = (byte *)malloc(_decoder->_origsize);
+	_decoder->_outstream = new MemoryWriteStream(_uncompressedData, _decoder->_origsize);
 
 	_currArchive.open(_archMap[filename]);
 	_currArchive.seek(hdr->pos, SEEK_SET);
 
+printf("Arj archive method %d, file '%s'\n", hdr->method, filename.c_str());
+
 	if (hdr->method == 0) { // store
-        _currArchive.read(_uncompressedData, _origsize);
+        _currArchive.read(_uncompressedData, _decoder->	_origsize);
 	} else {
-		_compressedData = (byte *)malloc(_compsize);
-		_currArchive.read(_compressedData, _compsize);
+		byte *_compressedData = (byte *)malloc(_decoder->_compsize);
+		_currArchive.read(_compressedData, _decoder->_compsize);
 
-		_compressed = new MemoryReadStream(_compressedData, _compsize);
+		_decoder->_compressed = new MemoryReadStream(_compressedData, _decoder->_compsize);
 
 		if (hdr->method == 1 || hdr->method == 2 || hdr->method == 3)
-			decode();
+			_decoder->decode();
 		else if (hdr->method == 4)
-			decode_f();
+			_decoder->decode_f();
 
-		delete _compressed;
+		delete _decoder->_compressed;
 		free(_compressedData);
 	}
 
 	_currArchive.close();
-	delete _outstream;
-	_outstream = NULL;
+	delete _decoder->_outstream;
+	_decoder->_outstream = NULL;
 
-	_uncompressed = new MemoryReadStream(_uncompressedData, _origsize);
+	_uncompressed = new MemoryReadStream(_uncompressedData, _decoder->_origsize);
 
 	return true;
 }
@@ -340,7 +447,7 @@
 // Source for init_getbits: arj_file.c (decode_start_stub)
 //
 
-void ArjFile::init_getbits() {
+void ArjDecoder::init_getbits() {
 	_bitbuf = 0;
 	_bytebuf = 0;
 	_bitcount = 0;
@@ -351,7 +458,7 @@
 // Source for fillbuf, getbits: decode.c 
 //
 
-void ArjFile::fillbuf(int n) {
+void ArjDecoder::fillbuf(int n) {
 	while (_bitcount < n) {
 		_bitbuf = (_bitbuf << _bitcount) | (_bytebuf >> (8 - _bitcount));
 		n -= _bitcount;
@@ -369,7 +476,7 @@
 }
 
 // Reads a series of bits into the input buffer */
-uint16 ArjFile::getbits(int n) {
+uint16 ArjDecoder::getbits(int n) {
 	uint16 rc;
 
 	rc = _bitbuf >> (ARJ_CODE_BIT - n);
@@ -385,7 +492,7 @@
 //
 
 // Creates a table for decoding
-void ArjFile::make_table(int nchar, byte *bitlen, int tablebits, uint16 *table, int tablesize) {
+void ArjDecoder::make_table(int nchar, byte *bitlen, int tablebits, uint16 *table, int tablesize) {
 	uint16 count[17], weight[17], start[18];
 	uint16 *p;
 	uint i, k, len, ch, jutbits, avail, nextcode, mask;
@@ -399,7 +506,7 @@
 	for (i = 1; i <= 16; i++)
 		start[i + 1] = start[i] + (count[i] << (16 - i));
 	if (start[17] != (uint16) (1 << 16))
-		error("ArjFile::make_table(): bad file data");
+		error("ArjDecoder::make_table(): bad file data");
 
 	jutbits = 16 - tablebits;
 	for (i = 1; (int)i <= tablebits; i++) {
@@ -427,7 +534,7 @@
 		nextcode = k + weight[len];
 		if ((int)len <= tablebits) {
 			if (nextcode > (uint)tablesize)
-				error("ArjFile::make_table(): bad file data");
+				error("ArjDecoder::make_table(): bad file data");
 			for (i = start[len]; i < nextcode; i++)
 				table[i] = ch;
 		} else {
@@ -453,7 +560,7 @@
 }
 
 // Reads length of data pending
-void ArjFile::read_pt_len(int nn, int nbit, int i_special) {
+void ArjDecoder::read_pt_len(int nn, int nbit, int i_special) {
 	int i, n;
 	int16 c;
 	uint16 mask;
@@ -491,7 +598,7 @@
 }
 
 // Reads a character table
-void ArjFile::read_c_len() {
+void ArjDecoder::read_c_len() {
 	int16 i, c, n;
 	uint16 mask;
 
@@ -540,7 +647,7 @@
 }
 
 // Decodes a single character
-uint16 ArjFile::decode_c() {
+uint16 ArjDecoder::decode_c() {
 	uint16 j, mask;
 
 	if (_blocksize == 0) {
@@ -566,7 +673,7 @@
 }
 
 // Decodes a control character
-uint16 ArjFile::decode_p() {
+uint16 ArjDecoder::decode_p() {
 	uint16 j, mask;
 
 	j = _pt_table[_bitbuf >> 8];
@@ -589,13 +696,13 @@
 }
 
 // Initializes memory for decoding
-void ArjFile::decode_start() {
+void ArjDecoder::decode_start() {
 	_blocksize = 0;
 	init_getbits();
 }
 
 // Decodes the entire file
-void ArjFile::decode() {
+void ArjDecoder::decode() {
 	int16 i;
 	int16 r;
 	int16 c;
@@ -641,7 +748,7 @@
 }
 
 // Backward pointer decoding
-int16 ArjFile::decode_ptr() {
+int16 ArjDecoder::decode_ptr() {
 	int16 c = 0;
 	int16 width;
 	int16 plus;
@@ -663,7 +770,7 @@
 }
 
 // Reference length decoding
-int16 ArjFile::decode_len() {
+int16 ArjDecoder::decode_len() {
 	int16 c = 0;
 	int16 width;
 	int16 plus;
@@ -685,7 +792,7 @@
 }
 
 // Decodes the entire file, using method 4
-void ArjFile::decode_f() {
+void ArjDecoder::decode_f() {
 	int16 i;
 	int16 j;
 	int16 c;

Modified: scummvm/trunk/common/unarj.h
===================================================================
--- scummvm/trunk/common/unarj.h	2008-11-07 08:47:59 UTC (rev 34921)
+++ scummvm/trunk/common/unarj.h	2008-11-07 09:46:12 UTC (rev 34922)
@@ -31,58 +31,9 @@
 
 namespace Common {
 
-#define ARJ_UCHAR_MAX 255
-#define ARJ_CHAR_BIT 8
+struct ArjHeader;
+class ArjDecoder;
 
-#define ARJ_COMMENT_MAX 2048
-#define ARJ_FILENAME_MAX 512
-
-#define ARJ_CODE_BIT 16
-#define ARJ_THRESHOLD 3
-#define ARJ_DICSIZ 26624
-#define ARJ_FDICSIZ ARJ_DICSIZ
-#define ARJ_MAXDICBIT   16
-#define ARJ_MAXMATCH   256
-#define ARJ_NC (ARJ_UCHAR_MAX + ARJ_MAXMATCH + 2 - ARJ_THRESHOLD)
-#define ARJ_NP (ARJ_MAXDICBIT + 1)
-#define ARJ_NT (ARJ_CODE_BIT + 3)
-
-#if ARJ_NT > ARJ_NP
-#define ARJ_NPT ARJ_NT
-#else
-#define ARJ_NPT ARJ_NP
-#endif
-
-#define ARJ_CTABLESIZE 4096
-#define ARJ_PTABLESIZE 256
-
-
-struct ArjHeader {
-	int32 pos;
-	uint16 id;
-	uint16 headerSize;
-	//
-	byte firstHdrSize;
-	byte nbr;
-	byte xNbr;
-	byte hostOs;
-	byte flags;
-	byte method;
-	byte fileType;
-	byte pad;
-	uint32 timeStamp;
-	int32 compSize;
-	int32 origSize;
-	uint32 fileCRC;
-	uint16 entryPos;
-	uint16 fileMode;
-	uint16 hostData;
-	char   filename[ARJ_FILENAME_MAX];
-	char   comment[ARJ_COMMENT_MAX];
-
-	uint32 headerCrc;
-};
-
 typedef HashMap<String, int, IgnoreCase_Hash, IgnoreCase_EqualTo> ArjFilesMap;
 
 class ArjFile : public SeekableReadStream, public NonCopyable {
@@ -111,58 +62,13 @@
 	Array<ArjHeader *> _headers;
 	ArjFilesMap _fileMap;
 	StringMap _archMap;
-	ReadStream *_stream;
+
 	byte *_uncompressedData;
-	byte *_compressedData;
-	MemoryWriteStream *_outstream;
-	MemoryReadStream *_compressed;
 	SeekableReadStream *_uncompressed;
 
 	bool _isOpen;
-
-	int32 findHeader(void);
-	ArjHeader *readHeader();
-
-	void decode();
-	void decode_f();
-
-	uint16 _bitbuf;
-	uint16 _bytebuf;
-	int32 _compsize;
-	int32 _origsize;
-	byte _subbitbuf;
-	int _bitcount;
-
-	void init_getbits();
-	void fillbuf(int n);
-	uint16 getbits(int n);
 	
-
-	void make_table(int nchar, byte *bitlen, int tablebits, uint16 *table, int tablesize);
-	void read_pt_len(int nn, int nbit, int i_special);
-	void read_c_len(void);
-	uint16 decode_c(void);
-	uint16 decode_p(void);
-	void decode_start(void);
-	int16 decode_ptr(void);
-	int16 decode_len(void);
-
-private:
-	byte  _ntext[ARJ_FDICSIZ];
-
-	int16  _getlen;
-	int16  _getbuf;
-
-	uint16 _left[2 * ARJ_NC - 1];
-	uint16 _right[2 * ARJ_NC - 1];
-	byte  _c_len[ARJ_NC];
-	byte  _pt_len[ARJ_NPT];
-
-	uint16 _c_table[ARJ_CTABLESIZE];
-	uint16 _pt_table[ARJ_PTABLESIZE];
-	uint16 _blocksize;
-
-
+	ArjDecoder *_decoder;
 };
 
 } // End of namespace Common


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