From 8022122bb921c5b04dbbb5a2675aa5a5538a4592 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 1 Mar 2009 21:48:39 +0000 Subject: SCI: Added kDebugLevelDclInflate; changed decompressors to *not* use fprintf or printf svn-id: r39055 --- engines/sci/sci.cpp | 1 + engines/sci/sci.h | 3 +- engines/sci/scicore/decompress0.cpp | 15 +++++---- engines/sci/scicore/decompress01.cpp | 13 ++++---- engines/sci/scicore/decompress1.cpp | 61 +++++++++++++++--------------------- engines/sci/scicore/decompress11.cpp | 17 +++------- 6 files changed, 46 insertions(+), 64 deletions(-) diff --git a/engines/sci/sci.cpp b/engines/sci/sci.cpp index 487e3378de..29bdca5e91 100644 --- a/engines/sci/sci.cpp +++ b/engines/sci/sci.cpp @@ -156,6 +156,7 @@ SciEngine::SciEngine(OSystem *syst, const SciGameDescription *desc) Common::addDebugChannel(kDebugLevelTime, "Time", "Time debugging"); Common::addDebugChannel(kDebugLevelRoom, "Room", "Room number debugging"); Common::addDebugChannel(kDebugLevelAvoidPath, "Pathfinding", "Pathfinding debugging"); + Common::addDebugChannel(kDebugLevelDclInflate, "DCL", "DCL inflate debugging"); printf("SciEngine::SciEngine\n"); } diff --git a/engines/sci/sci.h b/engines/sci/sci.h index 480ec66ee8..7a8d328112 100644 --- a/engines/sci/sci.h +++ b/engines/sci/sci.h @@ -52,7 +52,8 @@ enum kDebugLevels { kDebugLevelFile = 1 << 13, kDebugLevelTime = 1 << 14, kDebugLevelRoom = 1 << 15, - kDebugLevelAvoidPath = 1 << 16 + kDebugLevelAvoidPath = 1 << 16, + kDebugLevelDclInflate = 1 << 17 }; struct GameFlags { diff --git a/engines/sci/scicore/decompress0.cpp b/engines/sci/scicore/decompress0.cpp index 1e9c263845..997f30ee87 100644 --- a/engines/sci/scicore/decompress0.cpp +++ b/engines/sci/scicore/decompress0.cpp @@ -102,7 +102,7 @@ int decrypt1(uint8 *dest, uint8 *src, int length, int complength) { if (token > 0xff) { if (token >= tokenctr) { #ifdef _SCI_DECOMPRESS_DEBUG - fprintf(stderr, "decrypt1: Bad token %x!\n", token); + warning("decrypt1: Bad token %x", token); #endif // Well this is really bad // May be it should throw something like SCI_ERROR_DECOMPRESSION_INSANE @@ -111,7 +111,7 @@ int decrypt1(uint8 *dest, uint8 *src, int length, int complength) { if (destctr + tokenlastlength > length) { #ifdef _SCI_DECOMPRESS_DEBUG // For me this seems a normal situation, It's necessary to handle it - printf("decrypt1: Trying to write beyond the end of array(len=%d, destctr=%d, tok_len=%d)!\n", + warning("decrypt1: Trying to write beyond the end of array(len=%d, destctr=%d, tok_len=%d)", length, destctr, tokenlastlength); #endif i = 0; @@ -128,7 +128,7 @@ int decrypt1(uint8 *dest, uint8 *src, int length, int complength) { tokenlastlength = 1; if (destctr >= length) { #ifdef _SCI_DECOMPRESS_DEBUG - printf("decrypt1: Try to write single byte beyond end of array!\n"); + warning("decrypt1: Try to write single byte beyond end of array"); #endif } else dest[destctr++] = (byte)token; @@ -280,12 +280,11 @@ int decompress0(Resource *result, Common::ReadStream &stream, int sci_version) { #ifdef _SCI_DECOMPRESS_DEBUG - fprintf(stderr, "Resource %s.%03hi encrypted with method %hi at %.2f%%" - " ratio\n", + debug("Resource %s.%03hi encrypted with method %hi at %.2f%% ratio", getResourceTypeName(result->type), result->number, compressionMethod, (result->size == 0) ? -1.0 : (100.0 * compressedLength / result->size)); - fprintf(stderr, " compressedLength = 0x%hx, actualLength=0x%hx\n", + debug(" compressedLength = 0x%hx, actualLength=0x%hx", compressedLength, result->size); #endif @@ -325,8 +324,8 @@ int decompress0(Resource *result, Common::ReadStream &stream, int sci_version) { break; default: - fprintf(stderr, "Resource %s.%03hi: Compression method %hi not " - "supported!\n", getResourceTypeName(result->type), result->number, + warning("Resource %s.%03hi: Compression method %hi not supported", + getResourceTypeName(result->type), result->number, compressionMethod); free(result->data); result->data = 0; // So that we know that it didn't work diff --git a/engines/sci/scicore/decompress01.cpp b/engines/sci/scicore/decompress01.cpp index e87d61cf28..a78bf30686 100644 --- a/engines/sci/scicore/decompress01.cpp +++ b/engines/sci/scicore/decompress01.cpp @@ -427,7 +427,7 @@ byte *view_reorder(byte *inbuffer, int dsize) { for (l = 0;l < loopheaders;l++) { if (lh_mask & lb) { // The loop is _not_ present if (lh_last == -1) { - fprintf(stderr, "Error: While reordering view: Loop not present, but can't re-use last loop!\n"); + warning("While reordering view: Loop not present, but can't re-use last loop"); lh_last = 0; } WRITE_LE_UINT16(lh_ptr, lh_last); @@ -461,7 +461,7 @@ byte *view_reorder(byte *inbuffer, int dsize) { } if (celindex < cel_total) { - fprintf(stderr, "View decompression generated too few (%d / %d) headers!\n", celindex, cel_total); + warning("View decompression generated too few (%d / %d) headers", celindex, cel_total); return NULL; } @@ -539,12 +539,11 @@ int decompress01(Resource *result, Common::ReadStream &stream, int sci_version) #ifdef _SCI_DECOMPRESS_DEBUG - fprintf(stderr, "Resource %s.%03hi encrypted with method SCI01/%hi at %.2f%%" - " ratio\n", + debug("Resource %s.%03hi encrypted with method SCI01/%hi at %.2f%% ratio", sci_resource_types[result->type], result->number, compressionMethod, (result->size == 0) ? -1.0 : (100.0 * compressedLength / result->size)); - fprintf(stderr, " compressedLength = 0x%hx, actualLength=0x%hx\n", + debug(" compressedLength = 0x%hx, actualLength=0x%hx", compressedLength, result->size); #endif @@ -612,8 +611,8 @@ int decompress01(Resource *result, Common::ReadStream &stream, int sci_version) break; default: - fprintf(stderr, "Resource %s.%03hi: Compression method SCI1/%hi not " - "supported!\n", getResourceTypeName(result->type), result->number, + warning("Resource %s.%03hi: Compression method SCI1/%hi not supported", + getResourceTypeName(result->type), result->number, compressionMethod); free(result->data); result->data = 0; // So that we know that it didn't work diff --git a/engines/sci/scicore/decompress1.cpp b/engines/sci/scicore/decompress1.cpp index c42f436bba..cadd390aeb 100644 --- a/engines/sci/scicore/decompress1.cpp +++ b/engines/sci/scicore/decompress1.cpp @@ -25,9 +25,11 @@ // Reads data from a resource file and stores the result in memory +#include "common/debug.h" #include "common/stream.h" #include "common/util.h" +#include "sci/sci.h" #include "sci/sci_memory.h" #include "sci/scicore/resource.h" @@ -73,7 +75,7 @@ static inline int getbits_msb_first(struct bit_read_struct *inp, int bits) { int i; if (inp->bytepos + morebytes >= inp->length) { - fprintf(stderr, "read out-of-bounds with bytepos %d + morebytes %d >= length %d\n", + warning("read out-of-bounds with bytepos %d + morebytes %d >= length %d", inp->bytepos, morebytes, inp->length); return -SCI_ERROR_DECOMPRESSION_OVERFLOW; } @@ -90,15 +92,13 @@ static inline int getbits_msb_first(struct bit_read_struct *inp, int bits) { return result; } -static int DEBUG_DCL_INFLATE = 0; // FIXME: Make this a define eventually - static inline int getbits(struct bit_read_struct *inp, int bits) { int morebytes = (bits + inp->bitpos - 1) >> 3; int result = 0; int i; if (inp->bytepos + morebytes >= inp->length) { - fprintf(stderr, "read out-of-bounds with bytepos %d + morebytes %d >= length %d\n", + warning("read out-of-bounds with bytepos %d + morebytes %d >= length %d", inp->bytepos, morebytes, inp->length); return -SCI_ERROR_DECOMPRESSION_OVERFLOW; } @@ -112,8 +112,7 @@ static inline int getbits(struct bit_read_struct *inp, int bits) { inp->bitpos += bits - (morebytes << 3); inp->bytepos += morebytes; - if (DEBUG_DCL_INFLATE) - fprintf(stderr, "(%d:%04x)", bits, result); + debugC(kDebugLevelDclInflate, "(%d:%04x)", bits, result); return result; } @@ -124,15 +123,13 @@ static int huffman_lookup(struct bit_read_struct *inp, int *tree) { while (!(tree[pos] & HUFFMAN_LEAF)) { CALLC(bit = getbits(inp, 1)); - if (DEBUG_DCL_INFLATE) - fprintf(stderr, "[%d]:%d->", pos, bit); + debugC(kDebugLevelDclInflate, "[%d]:%d->", pos, bit); if (bit) pos = tree[pos] & ~(~0 << BRANCH_SHIFT); else pos = tree[pos] >> BRANCH_SHIFT; } - if (DEBUG_DCL_INFLATE) - fprintf(stderr, "=%02x\n", tree[pos] & 0xffff); + debugC(kDebugLevelDclInflate, "=%02x\n", tree[pos] & 0xffff); return tree[pos] & 0xffff; } @@ -149,23 +146,20 @@ static int decrypt4_hdyn(byte *dest, int length, struct bit_read_struct *reader) if (mode == DCL_ASCII_MODE) { warning("DCL-INFLATE: Decompressing ASCII mode (untested)"); - //DEBUG_DCL_INFLATE = 1; } else if (mode) { warning("DCL-INFLATE: Error: Encountered mode %02x, expected 00 or 01\n", mode); return 1; } - if (DEBUG_DCL_INFLATE) { - int i; - - for (i = 0; i < reader->length; i++) { - fprintf(stderr, "%02x ", reader->data[i]); + if (Common::isDebugChannelEnabled(kDebugLevelDclInflate)) { + for (int i = 0; i < reader->length; i++) { + debugC(kDebugLevelDclInflate, "%02x ", reader->data[i]); if (!((i + 1) & 0x1f)) - fprintf(stderr, "\n"); + debugC(kDebugLevelDclInflate, "\n"); } - fprintf(stderr, "\n---\n"); + debugC(kDebugLevelDclInflate, "\n---\n"); } @@ -188,8 +182,7 @@ static int decrypt4_hdyn(byte *dest, int length, struct bit_read_struct *reader) val_length += length_bonus; } - if (DEBUG_DCL_INFLATE) - fprintf(stderr, " | "); + debugC(kDebugLevelDclInflate, " | "); CALLC(value = huffman_lookup(reader, distance_tree)); @@ -206,16 +199,15 @@ static int decrypt4_hdyn(byte *dest, int length, struct bit_read_struct *reader) } ++val_distance; - if (DEBUG_DCL_INFLATE) - fprintf(stderr, "\nCOPY(%d from %d)\n", val_length, val_distance); + debugC(kDebugLevelDclInflate, "\nCOPY(%d from %d)\n", val_length, val_distance); if (val_length + write_pos > length) { - fprintf(stderr, "DCL-INFLATE Error: Write out of bounds while copying %d bytes\n", val_length); + warning("DCL-INFLATE Error: Write out of bounds while copying %d bytes", val_length); return -SCI_ERROR_DECOMPRESSION_OVERFLOW; } if (write_pos < val_distance) { - fprintf(stderr, "DCL-INFLATE Error: Attempt to copy from before beginning of input stream\n"); + warning("DCL-INFLATE Error: Attempt to copy from before beginning of input stream"); return -SCI_ERROR_DECOMPRESSION_INSANE; } @@ -224,11 +216,10 @@ static int decrypt4_hdyn(byte *dest, int length, struct bit_read_struct *reader) memcpy(dest + write_pos, dest + write_pos - val_distance, copy_length); - if (DEBUG_DCL_INFLATE) { - int i; - for (i = 0; i < copy_length; i++) - fprintf(stderr, "\33[32;31m%02x\33[37;37m ", dest[write_pos + i]); - fprintf(stderr, "\n"); + if (Common::isDebugChannelEnabled(kDebugLevelDclInflate)) { + for (int i = 0; i < copy_length; i++) + debugC(kDebugLevelDclInflate, "\33[32;31m%02x\33[37;37m ", dest[write_pos + i]); + debugC(kDebugLevelDclInflate, "\n"); } val_length -= copy_length; @@ -245,8 +236,7 @@ static int decrypt4_hdyn(byte *dest, int length, struct bit_read_struct *reader) dest[write_pos++] = value; - if (DEBUG_DCL_INFLATE) - fprintf(stderr, "\33[32;31m%02x \33[37;37m", value); + debugC(kDebugLevelDclInflate, "\33[32;31m%02x \33[37;37m", value); } } @@ -328,14 +318,13 @@ int decompress1(Resource *result, Common::ReadStream &stream, int sci_version) { #ifdef _SCI_DECOMPRESS_DEBUG - fprintf(stderr, "Resource %i.%s encrypted with method SCI1%c/%hi at %.2f%%" - " ratio\n", + debug("Resource %i.%s encrypted with method SCI1%c/%hi at %.2f%% ratio", result->number, sci_resource_type_suffixes[result->type], early ? 'e' : 'l', compressionMethod, (result->size == 0) ? -1.0 : (100.0 * compressedLength / result->size)); - fprintf(stderr, " compressedLength = 0x%hx, actualLength=0x%hx\n", + debug(" compressedLength = 0x%hx, actualLength=0x%hx", compressedLength, result->size); #endif @@ -402,8 +391,8 @@ int decompress1(Resource *result, Common::ReadStream &stream, int sci_version) { break; default: - fprintf(stderr, "Resource %s.%03hi: Compression method SCI1/%hi not " - "supported!\n", getResourceTypeName(result->type), result->number, + warning("Resource %s.%03hi: Compression method SCI1/%hi not supported", + getResourceTypeName(result->type), result->number, compressionMethod); free(result->data); result->data = 0; // So that we know that it didn't work diff --git a/engines/sci/scicore/decompress11.cpp b/engines/sci/scicore/decompress11.cpp index aff87c9fdf..cabbb5a50d 100644 --- a/engines/sci/scicore/decompress11.cpp +++ b/engines/sci/scicore/decompress11.cpp @@ -32,8 +32,6 @@ namespace Sci { -#define DDEBUG if (0) printf - void decryptinit3(); int decrypt3(uint8* dest, uint8* src, int length, int complength); int decrypt4(uint8* dest, uint8* src, int length, int complength); @@ -43,8 +41,6 @@ int decompress11(Resource *result, Common::ReadStream &stream, int sci_version) uint16 compressionMethod; uint8 *buffer; - DDEBUG("d1"); - result->id = stream.readByte(); if (stream.err()) return SCI_ERROR_IO_ERROR; @@ -90,18 +86,15 @@ int decompress11(Resource *result, Common::ReadStream &stream, int sci_version) } #ifdef _SCI_DECOMPRESS_DEBUG - fprintf(stderr, "Resource %i.%s encrypted with method SCI1.1/%hi at %.2f%%" - " ratio\n", + debug("Resource %i.%s encrypted with method SCI1.1/%hi at %.2f%% ratio", result->number, getResourceTypeSuffix(result->type), compressionMethod, (result->size == 0) ? -1.0 : (100.0 * compressedLength / result->size)); - fprintf(stderr, " compressedLength = 0x%hx, actualLength=0x%hx\n", + debug(" compressedLength = 0x%hx, actualLength=0x%hx", compressedLength, result->size); #endif - DDEBUG("/%d[%d]", compressionMethod, result->size); - switch (compressionMethod) { case 0: // no compression if (result->size != compressedLength) { @@ -130,7 +123,7 @@ int decompress11(Resource *result, Common::ReadStream &stream, int sci_version) case 3: case 4: // NYI - fprintf(stderr, "Resource %d.%s: Warning: compression type #%d not yet implemented\n", + warning("Resource %d.%s: Warning: compression type #%d not yet implemented", result->number, getResourceTypeSuffix(result->type), compressionMethod); free(result->data); result->data = NULL; @@ -138,8 +131,8 @@ int decompress11(Resource *result, Common::ReadStream &stream, int sci_version) break; default: - fprintf(stderr, "Resource %d.%s: Compression method SCI1/%hi not " - "supported!\n", result->number, getResourceTypeSuffix(result->type), + warning("Resource %d.%s: Compression method SCI1/%hi not supported", + result->number, getResourceTypeSuffix(result->type), compressionMethod); free(result->data); result->data = NULL; // So that we know that it didn't work -- cgit v1.2.3