Initial commit
This commit is contained in:
83
engines/ultima/ultima4/core/lzw/hash.cpp
Normal file
83
engines/ultima/ultima4/core/lzw/hash.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ultima/ultima4/core/lzw/hash.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
namespace LZW {
|
||||
|
||||
int probe1(byte root, int codeword) {
|
||||
int newHashCode = ((root << 4) ^ codeword) & 0xfff;
|
||||
return (newHashCode);
|
||||
}
|
||||
|
||||
/* The secondary probe uses some assembler instructions that aren't easily translated to C. */
|
||||
int probe2(byte root, int codeword) {
|
||||
/* registers[0] == AX, registers[1] == DX */
|
||||
long registers[2], temp;
|
||||
long carry, oldCarry;
|
||||
int i, j;
|
||||
|
||||
/* the pre-mul part */
|
||||
registers[1] = 0;
|
||||
registers[0] = ((root << 1) + codeword) | 0x800;
|
||||
|
||||
/* the mul part (simulated mul instruction) */
|
||||
/* DX:AX = AX * AX */
|
||||
temp = (registers[0] & 0xff) * (registers[0] & 0xff);
|
||||
temp += 2 * (registers[0] & 0xff) * (registers[0] >> 8) * 0x100;
|
||||
registers[1] = (temp >> 16) + (registers[0] >> 8) * (registers[0] >> 8);
|
||||
registers[0] = temp & 0xffff;
|
||||
|
||||
/* if DX != 0, the mul instruction sets the carry flag */
|
||||
if (registers[1] == 00) {
|
||||
carry = 0;
|
||||
} else {
|
||||
carry = 1;
|
||||
}
|
||||
|
||||
/* the rcl part */
|
||||
for (i = 0; i < 2; i++) { /* 2 rcl's */
|
||||
for (j = 0; j < 2; j++) { /* rotate through 2 registers */
|
||||
oldCarry = carry;
|
||||
carry = (registers[j] >> 15) & 1;
|
||||
registers[j] = (registers[j] << 1) | oldCarry;
|
||||
registers[j] = registers[j] & 0xffff; /* make sure register stays 16 bit */
|
||||
}
|
||||
}
|
||||
|
||||
/* final touches */
|
||||
registers[0] = ((registers[0] >> 8) | (registers[1] << 8)) & 0xfff;
|
||||
|
||||
return ((int)registers[0]);
|
||||
}
|
||||
|
||||
int probe3(int hashCode) {
|
||||
const long probeOffset = 0x1fd; /* I think 0x1fd is prime */
|
||||
|
||||
long newHashCode = (hashCode + probeOffset) & 0xfff;
|
||||
return ((int)newHashCode);
|
||||
}
|
||||
|
||||
} // End of namespace LZW
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
39
engines/ultima/ultima4/core/lzw/hash.h
Normal file
39
engines/ultima/ultima4/core/lzw/hash.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ULTIMA4_CORE_LZW_HASH_H
|
||||
#define ULTIMA4_CORE_LZW_HASH_H
|
||||
|
||||
#include "common/scummsys.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
namespace LZW {
|
||||
|
||||
int probe1(byte root, int codeword);
|
||||
int probe2(byte root, int codeword);
|
||||
int probe3(int hashCode);
|
||||
|
||||
} // End of namespace LZW
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
|
||||
#endif
|
||||
315
engines/ultima/ultima4/core/lzw/lzw.cpp
Normal file
315
engines/ultima/ultima4/core/lzw/lzw.cpp
Normal file
@@ -0,0 +1,315 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* A few files from Ultima 4 (PC version) have been compressed with the LZW algorithm.
|
||||
* There are two things that make the U4 implementation of the LZW decoding algorithm special:
|
||||
* 1) It uses a fixed codeword length of 12 bits.
|
||||
* The advantages over variable-length codewords are faster decompression and simpler code.
|
||||
* 2) The dictionary is implemented as a hash table.
|
||||
* While the dictionary is supposed to implemented as a hash table in the LZW *en*coder (to speed up
|
||||
* string searches), there is no reason not to implement it as a simple array in the decoder.
|
||||
* But since U4 uses a hash table in the decoder, this C version must do the same (or it won't be
|
||||
* able to decode the U4 files).
|
||||
*
|
||||
* An explanation on LZW data (de)compression can be found here:
|
||||
* https://web.archive.org/web/20191231131544/https://marknelson.us/posts/1989/10/01/lzw-data-compression.html
|
||||
* https://web.archive.org/web/20191231131532/https://marknelson.us/posts/2011/11/08/lzw-revisited.html
|
||||
*/
|
||||
|
||||
#include "ultima/ultima4/core/lzw/lzw.h"
|
||||
#include "ultima/ultima4/core/lzw/hash.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
namespace LZW {
|
||||
|
||||
typedef void (*WRITE_DECOMP)(byte root, byte *destination, long *position);
|
||||
|
||||
struct lzwDictionaryEntry {
|
||||
byte root;
|
||||
int codeword;
|
||||
byte occupied;
|
||||
};
|
||||
|
||||
long generalizedDecompress(WRITE_DECOMP outFunc, byte *compressedMem, byte *decompressedMem, long compressedSize);
|
||||
int getNextCodeword(long *bitsRead, byte *compressedMem);
|
||||
void discardRoot(byte root, byte *destination, long *position);
|
||||
void outputRoot(byte root, byte *destination, long *position);
|
||||
|
||||
void getString(int codeword, lzwDictionaryEntry *lzwDictionary, byte *stack, int *elementsInStack);
|
||||
int getNewHashCode(byte root, int codeword, lzwDictionaryEntry *dictionary);
|
||||
byte hashPosFound(int hashCode, byte root, int codeword, lzwDictionaryEntry *dictionary);
|
||||
|
||||
/*
|
||||
* This function returns the decompressed size of a block of compressed data.
|
||||
* It doesn't decompress the data.
|
||||
* Use this function if you want to decompress a block of data, but don't know the decompressed size
|
||||
* in advance.
|
||||
*
|
||||
* There is some error checking to detect if the compressed data is corrupt, but it's only rudimentary.
|
||||
* Returns:
|
||||
* No errors: (long) decompressed size
|
||||
* Error: (long) -1
|
||||
*/
|
||||
long lzwGetDecompressedSize(byte *compressedMem, long compressedSize) {
|
||||
return (generalizedDecompress(&discardRoot, compressedMem, nullptr, compressedSize));
|
||||
}
|
||||
|
||||
/*
|
||||
* Decompresses a block of compressed data from memory to memory.
|
||||
* Use this function if you already know the decompressed size.
|
||||
*
|
||||
* This function assumes that *decompressed_mem is already allocated, and that the decompressed data
|
||||
* will fit into *decompressed_mem.
|
||||
* There is some error checking to detect if the compressed data is corrupt, but it's only rudimentary.
|
||||
* Returns:
|
||||
* No errors: (long) decompressed size
|
||||
* Error: (long) -1
|
||||
*/
|
||||
long lzwDecompress(byte *compressedMem, byte *decompressedMem, long compressedSize) {
|
||||
return (generalizedDecompress(&outputRoot, compressedMem, decompressedMem, compressedSize));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------
|
||||
Functions used only inside lzw.c
|
||||
-------------------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* This function does the actual decompression work.
|
||||
* Parameters:
|
||||
* perform_decompression: FALSE ==> return decompressed size, but discard decompressed data
|
||||
* compressed_mem: compressed data
|
||||
* decompressed_mem: this is where the compressed data will be decompressed to
|
||||
* compressed_size: size of the compressed data (in bytes)
|
||||
*/
|
||||
long generalizedDecompress(WRITE_DECOMP outFunc, byte *compressedMem, byte *decompressedMem, long compressedSize) {
|
||||
int i;
|
||||
|
||||
/* re-initialize the dictionary when there are more than 0xccc entries */
|
||||
const int maxDictEntries = 0xccc;
|
||||
|
||||
const int lzwStackSize = 0x8000;
|
||||
const int lzwDictionarySize = 0x1000;
|
||||
|
||||
int old_code;
|
||||
int new_code;
|
||||
byte character;
|
||||
|
||||
long bitsRead = 0;
|
||||
long bytesWritten = 0;
|
||||
|
||||
/* newpos: position in the dictionary where new codeword was added */
|
||||
/* must be equal to current codeword (if it isn't, the compressed data must be corrupt) */
|
||||
/* unknownCodeword: is the current codeword in the dictionary? */
|
||||
int newpos;
|
||||
byte unknownCodeword;
|
||||
|
||||
/* initialize the dictionary and the stack */
|
||||
lzwDictionaryEntry *lzwDictionary = (lzwDictionaryEntry *) malloc(sizeof(lzwDictionaryEntry) * lzwDictionarySize);
|
||||
int codewordsInDictionary = 0;
|
||||
byte *lzwStack = (byte *) malloc(sizeof(byte) * lzwStackSize);
|
||||
int elementsInStack = 0;
|
||||
|
||||
/* clear the dictionary */
|
||||
memset(lzwDictionary, 0, sizeof(lzwDictionaryEntry) * lzwDictionarySize);
|
||||
for (i = 0; i < 0x100; i++) {
|
||||
lzwDictionary[i].occupied = 1;
|
||||
}
|
||||
|
||||
if (bitsRead + 12 <= compressedSize * 8) {
|
||||
/* read OLD_CODE */
|
||||
old_code = getNextCodeword(&bitsRead, compressedMem);
|
||||
/* CHARACTER = OLD_CODE */
|
||||
character = (byte)old_code;
|
||||
/* output OLD_CODE */
|
||||
outFunc(character, decompressedMem, &bytesWritten);
|
||||
|
||||
while (bitsRead + 12 <= compressedSize * 8) { /* WHILE there are still input characters DO */
|
||||
/* read NEW_CODE */
|
||||
new_code = getNextCodeword(&bitsRead, compressedMem);
|
||||
|
||||
if (lzwDictionary[new_code].occupied) { /* is the codeword in the dictionary? */
|
||||
/* codeword is present in the dictionary */
|
||||
/* it must either be a root or a non-root that has already been added to the dicionary */
|
||||
unknownCodeword = 0;
|
||||
|
||||
/* STRING = get translation of NEW_CODE */
|
||||
getString(new_code, lzwDictionary, lzwStack, &elementsInStack);
|
||||
} else {
|
||||
/* codeword is yet to be defined */
|
||||
unknownCodeword = 1;
|
||||
|
||||
/* STRING = get translation of OLD_CODE */
|
||||
/* STRING = STRING+CHARACTER */
|
||||
lzwStack[elementsInStack] = character; /* push character on the stack */
|
||||
elementsInStack++;
|
||||
|
||||
getString(old_code, lzwDictionary, lzwStack, &elementsInStack);
|
||||
}
|
||||
|
||||
/* CHARACTER = first character in STRING */
|
||||
character = lzwStack[elementsInStack - 1]; /* element at top of stack */
|
||||
|
||||
/* output STRING */
|
||||
while (elementsInStack > 0) {
|
||||
outFunc(lzwStack[elementsInStack - 1], decompressedMem, &bytesWritten);
|
||||
elementsInStack--;
|
||||
}
|
||||
|
||||
/* add OLD_CODE + CHARACTER to the translation table */
|
||||
newpos = getNewHashCode(character, old_code, lzwDictionary);
|
||||
|
||||
lzwDictionary[newpos].root = character;
|
||||
lzwDictionary[newpos].codeword = old_code;
|
||||
lzwDictionary[newpos].occupied = 1;
|
||||
codewordsInDictionary++;
|
||||
|
||||
/* check for errors */
|
||||
if (unknownCodeword && (newpos != new_code)) {
|
||||
/* clean up */
|
||||
free(lzwStack);
|
||||
free(lzwDictionary);
|
||||
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (codewordsInDictionary > maxDictEntries) {
|
||||
/* wipe dictionary */
|
||||
codewordsInDictionary = 0;
|
||||
memset(lzwDictionary, 0, sizeof(lzwDictionaryEntry) * lzwDictionarySize);
|
||||
for (i = 0; i < 0x100; i++) {
|
||||
lzwDictionary[i].occupied = 1;
|
||||
}
|
||||
|
||||
if (bitsRead + 12 <= compressedSize * 8) {
|
||||
new_code = getNextCodeword(&bitsRead, compressedMem);
|
||||
character = (byte)new_code;
|
||||
|
||||
outFunc(character, decompressedMem, &bytesWritten);
|
||||
} else {
|
||||
/* clean up */
|
||||
free(lzwStack);
|
||||
free(lzwDictionary);
|
||||
|
||||
return (bytesWritten);
|
||||
}
|
||||
}
|
||||
|
||||
/* OLD_CODE = NEW_CODE */
|
||||
old_code = new_code;
|
||||
}
|
||||
}
|
||||
/* clean up */
|
||||
free(lzwStack);
|
||||
free(lzwDictionary);
|
||||
|
||||
return (bytesWritten);
|
||||
}
|
||||
|
||||
/* read the next 12-bit codeword from the compressed data */
|
||||
int getNextCodeword(long *bitsRead, byte *compressedMem) {
|
||||
int codeword = (compressedMem[(*bitsRead) / 8] << 8) + compressedMem[(*bitsRead) / 8 + 1];
|
||||
codeword = codeword >> (4 - ((*bitsRead) % 8));
|
||||
codeword = codeword & 0xfff;
|
||||
(*bitsRead) += 12;
|
||||
|
||||
return (codeword);
|
||||
}
|
||||
|
||||
/* increment position pointer, but do not write root to memory */
|
||||
void discardRoot(byte root, byte *destination, long *position) {
|
||||
(*position)++;
|
||||
}
|
||||
|
||||
/* output a root to memory */
|
||||
void outputRoot(byte root, byte *destination, long *position) {
|
||||
destination[*position] = root;
|
||||
(*position)++;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------
|
||||
Dictionary-related functions
|
||||
-------------------------------------------------------------------------------------- */
|
||||
|
||||
/* pushes the string associated with codeword onto the stack */
|
||||
void getString(int codeword, lzwDictionaryEntry *dictionary, byte *stack, int *elementsInStack) {
|
||||
byte root;
|
||||
int currentCodeword = codeword;
|
||||
|
||||
while (currentCodeword > 0xff) {
|
||||
root = dictionary[currentCodeword].root;
|
||||
currentCodeword = dictionary[currentCodeword].codeword;
|
||||
stack[*elementsInStack] = root;
|
||||
(*elementsInStack)++;
|
||||
}
|
||||
|
||||
/* push the root at the leaf */
|
||||
stack[*elementsInStack] = (byte)currentCodeword;
|
||||
(*elementsInStack)++;
|
||||
}
|
||||
|
||||
int getNewHashCode(byte root, int codeword, lzwDictionaryEntry *dictionary) {
|
||||
int hashCode;
|
||||
|
||||
/* probe 1 */
|
||||
hashCode = probe1(root, codeword);
|
||||
if (hashPosFound(hashCode, root, codeword, dictionary)) {
|
||||
return (hashCode);
|
||||
}
|
||||
/* probe 2 */
|
||||
hashCode = probe2(root, codeword);
|
||||
if (hashPosFound(hashCode, root, codeword, dictionary)) {
|
||||
return (hashCode);
|
||||
}
|
||||
/* probe 3 */
|
||||
do {
|
||||
hashCode = probe3(hashCode);
|
||||
} while (! hashPosFound(hashCode, root, codeword, dictionary));
|
||||
|
||||
return (hashCode);
|
||||
}
|
||||
|
||||
byte hashPosFound(int hashCode, byte root, int codeword, lzwDictionaryEntry *dictionary) {
|
||||
if (hashCode > 0xff) {
|
||||
// hash codes must not be roots
|
||||
byte c1 = 0, c2 = 0, c3 = 0;
|
||||
|
||||
if (dictionary[hashCode].occupied) {
|
||||
// hash table position is occupied
|
||||
c1 = 1;
|
||||
// is our (root,codeword) pair already in the hash table?
|
||||
c2 = dictionary[hashCode].root == root;
|
||||
c3 = dictionary[hashCode].codeword == codeword;
|
||||
} else {
|
||||
// hash table position is free
|
||||
c1 = 0;
|
||||
}
|
||||
|
||||
return (!c1) || (c1 && c2 && c3);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace LZW
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
38
engines/ultima/ultima4/core/lzw/lzw.h
Normal file
38
engines/ultima/ultima4/core/lzw/lzw.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ULTIMA4_CORE_LZW_LZW_H
|
||||
#define ULTIMA4_CORE_LZW_LZW_H
|
||||
|
||||
#include "common/scummsys.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
namespace LZW {
|
||||
|
||||
long lzwGetDecompressedSize(byte *compressedMem, long compressedSize);
|
||||
long lzwDecompress(byte *compressedMem, byte *decompressedMem, long compressedSize);
|
||||
|
||||
} // End of namespace LZW
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
|
||||
#endif
|
||||
155
engines/ultima/ultima4/core/lzw/u4decode.cpp
Normal file
155
engines/ultima/ultima4/core/lzw/u4decode.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ultima/ultima4/core/lzw/lzw.h"
|
||||
#include "ultima/ultima4/core/lzw/u4decode.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
namespace LZW {
|
||||
|
||||
/*
|
||||
* Loads a file, decompresses it (from memory to memory), and writes the decompressed data to another file
|
||||
* Returns:
|
||||
* -1 if there was an error
|
||||
* the decompressed file length, on success
|
||||
*/
|
||||
long decompress_u4_file(Common::SeekableReadStream *in, long filesize, void **out) {
|
||||
byte *compressed_mem, *decompressed_mem;
|
||||
long compressed_filesize, decompressed_filesize;
|
||||
long errorCode;
|
||||
|
||||
/* size of the compressed input file */
|
||||
compressed_filesize = filesize;
|
||||
|
||||
/* input file should be longer than 0 bytes */
|
||||
if (compressed_filesize == 0)
|
||||
return (-1);
|
||||
|
||||
/* check if the input file is _not_ a valid LZW-compressed file */
|
||||
if (!mightBeValidCompressedFile(in))
|
||||
return (-1);
|
||||
|
||||
/* load compressed file into compressed_mem[] */
|
||||
compressed_mem = (byte *) malloc(compressed_filesize);
|
||||
in->read(compressed_mem, compressed_filesize);
|
||||
|
||||
/*
|
||||
* determine decompressed file size
|
||||
* if lzw_get_decompressed_size() can't determine the decompressed size (i.e. the compressed
|
||||
* data is corrupt), it returns -1
|
||||
*/
|
||||
decompressed_filesize = lzwGetDecompressedSize(compressed_mem, compressed_filesize);
|
||||
|
||||
if (decompressed_filesize <= 0) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* decompress file from compressed_mem[] into decompressed_mem[] */
|
||||
decompressed_mem = (byte *) malloc(decompressed_filesize);
|
||||
|
||||
/* testing: clear destination mem */
|
||||
memset(decompressed_mem, 0, decompressed_filesize);
|
||||
|
||||
errorCode = lzwDecompress(compressed_mem, decompressed_mem, compressed_filesize);
|
||||
|
||||
free(compressed_mem);
|
||||
|
||||
*out = decompressed_mem;
|
||||
|
||||
return (errorCode);
|
||||
}
|
||||
|
||||
long decompress_u4_memory(void *in, long inlen, void **out) {
|
||||
byte *compressed_mem, *decompressed_mem;
|
||||
long compressed_filesize, decompressed_filesize;
|
||||
long errorCode;
|
||||
|
||||
/* size of the compressed input */
|
||||
compressed_filesize = inlen;
|
||||
|
||||
/* input file should be longer than 0 bytes */
|
||||
if (compressed_filesize == 0)
|
||||
return (-1);
|
||||
|
||||
compressed_mem = (byte *) in;
|
||||
|
||||
/*
|
||||
* determine decompressed data size
|
||||
* if lzw_get_decompressed_size() can't determine the decompressed size (i.e. the compressed
|
||||
* data is corrupt), it returns -1
|
||||
*/
|
||||
decompressed_filesize = lzwGetDecompressedSize(compressed_mem, compressed_filesize);
|
||||
|
||||
if (decompressed_filesize <= 0) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* decompress file from compressed_mem[] into decompressed_mem[] */
|
||||
decompressed_mem = (byte *) malloc(decompressed_filesize);
|
||||
|
||||
/* testing: clear destination mem */
|
||||
memset(decompressed_mem, 0, decompressed_filesize);
|
||||
|
||||
errorCode = lzwDecompress(compressed_mem, decompressed_mem, compressed_filesize);
|
||||
|
||||
*out = decompressed_mem;
|
||||
|
||||
return (errorCode);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the size of a file, and moves the file pointer to the beginning.
|
||||
* The file must already be open when this function is called.
|
||||
*/
|
||||
long getFilesize(Common::SeekableReadStream *input_file) {
|
||||
return input_file->size();
|
||||
}
|
||||
|
||||
/*
|
||||
* If the input file is a valid LZW-compressed file, the upper 4 bits of
|
||||
* the first byte must be 0, because the first codeword is always a root.
|
||||
*/
|
||||
byte mightBeValidCompressedFile(Common::SeekableReadStream *input_file) {
|
||||
byte firstByte;
|
||||
byte c1, c2, c3; /* booleans */
|
||||
long input_filesize;
|
||||
|
||||
/* check if the input file has a valid size */
|
||||
/* the compressed file is made up of 12-bit codewords, */
|
||||
/* so there are either 0 or 4 bits of wasted space */
|
||||
input_filesize = getFilesize(input_file);
|
||||
c1 = (input_filesize * 8) % 12 == 0;
|
||||
c2 = (input_filesize * 8 - 4) % 12 == 0;
|
||||
|
||||
// read first byte, and then reset back file pointer
|
||||
input_file->seek(0);
|
||||
firstByte = input_file->readByte();
|
||||
input_file->seek(0);
|
||||
c3 = (firstByte >> 4) == 0;
|
||||
|
||||
// check if upper 4 bits are 0
|
||||
return ((c1 || c2) && c3);
|
||||
}
|
||||
|
||||
} // End of namespace LZW
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
40
engines/ultima/ultima4/core/lzw/u4decode.h
Normal file
40
engines/ultima/ultima4/core/lzw/u4decode.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ULTIMA4_CORE_LZW_U4DECODE_H
|
||||
#define ULTIMA4_CORE_LZW_U4DECODE_H
|
||||
|
||||
#include "common/stream.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
namespace LZW {
|
||||
|
||||
long decompress_u4_file(Common::SeekableReadStream *in, long filesize, void **out);
|
||||
long getFilesize(Common::SeekableReadStream *input_file);
|
||||
byte mightBeValidCompressedFile(Common::SeekableReadStream *compressed_file);
|
||||
long decompress_u4_memory(void *in, long inlen, void **out);
|
||||
|
||||
} // End of namespace LZW
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user