[Scummvm-cvs-logs] CVS: tools descumm-common.cpp,NONE,1.1 descumm.h,1.1,1.2 descumm.cpp,1.33,1.34 descumm6.cpp,1.95,1.96

Max Horn fingolfin at users.sourceforge.net
Sat May 10 11:30:02 CEST 2003


Update of /cvsroot/scummvm/tools
In directory sc8-pr-cvs1:/tmp/cvs-serv25534

Modified Files:
	descumm.h descumm.cpp descumm6.cpp 
Added Files:
	descumm-common.cpp 
Log Message:
Moved some code shared by descumm/descumm6 into descumm-common.cpp

--- NEW FILE: descumm-common.cpp ---
/* DeScumm - Scumm Script Disassembler (common code)
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2002-2003  The ScummVM Team
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/tools/descumm-common.cpp,v 1.1 2003/05/10 18:29:08 fingolfin Exp $
 *
 */

#include "descumm.h"


BlockStack *block_stack;
int num_block_stack;

bool pendingElse, haveElse;
int pendingElseTo;
int pendingElseOffs;
int pendingElseOpcode;
int pendingElseIndent;

int g_jump_opcode;

bool alwaysShowOffs = false;
bool dontOutputIfs = false;
bool dontOutputElse = false;
bool dontOutputElseif = false;
bool dontOutputWhile = false;
bool dontShowOpcode = false;
bool dontShowOffsets = false;
bool haltOnError;

byte scriptVersion;

byte *cur_pos, *org_pos;
int offs_of_line;

int size_of_code;


///////////////////////////////////////////////////////////////////////////

char *strecpy(char *buf, const char *src)
{
	strcpy(buf, src);
	return strchr(buf, 0);
}

int get_curoffs()
{
	return cur_pos - org_pos;
}

int get_byte()
{
	return (byte)(*cur_pos++);
}

uint get_word()
{
	int i;

	if (scriptVersion == 8) {
		i = TO_LE_32(*((int32 *)cur_pos));
		cur_pos += 4;
	} else {
		i = TO_LE_16(*((int16 *)cur_pos));
		cur_pos += 2;
	}
	return i;
}

int get_signed_word()
{
	uint i = get_word();

	if (scriptVersion == 8) {
		return (int32)i;
	} else {
		return (int16)i;
	}
}


///////////////////////////////////////////////////////////////////////////

#define INDENT_SIZE 2

static char *indentbuf;

char *getIndentString(int i)
{
	char *c = indentbuf;
	i += i;
	if (!c)
		indentbuf = c = (char *)malloc(127 * INDENT_SIZE + 1);
	if (i >= 127 * INDENT_SIZE)
		i = 127 * INDENT_SIZE;
	if (i < 0)
		i = 0;
	memset(c, 32, i);
	c[i] = 0;
	return c;
}

void outputLine(char *buf, int curoffs, int opcode, int indent)
{
	char *s;

	if (buf[0]) {
		if (indent == -1)
			indent = num_block_stack;
		if (curoffs == -1)
			curoffs = get_curoffs();

		s = getIndentString(indent);

		if (dontShowOpcode) {
			if (dontShowOffsets)
				printf("%s%s\n", s, buf);
			else
				printf("[%.4X] %s%s\n", curoffs, s, buf);
		} else {
			char buf2[4];
			if (opcode != -1)
				sprintf(buf2, "%.2X", opcode);
			else
				strcpy(buf2, "**");
			if (dontShowOffsets)
				printf("(%s) %s%s\n", buf2, s, buf);
			else
				printf("[%.4X] (%s) %s%s\n", curoffs, buf2, s, buf);
		}
	}
}

///////////////////////////////////////////////////////////////////////////

bool indentBlock(unsigned int cur)
{
	BlockStack *p;

	if (!num_block_stack)
		return false;

	p = &block_stack[num_block_stack - 1];
	if (cur < p->to)
		return false;

	num_block_stack--;
	return true;
}


BlockStack *pushBlockStackItem()
{
	if (!block_stack)
		block_stack = (BlockStack *) malloc(256 * sizeof(BlockStack));

	if (num_block_stack >= 256) {
		printf("block_stack full!\n");
		exit(0);
	}
	return &block_stack[num_block_stack++];
}

// Returns 0 or 1 depending if it's ok to add a block
bool maybeAddIf(unsigned int cur, unsigned int to)
{
	int i;
	BlockStack *p;
	
	if (((to | cur) >> 16) || (to <= cur))
		return false; // Invalid jump
	
	for (i = 0, p = block_stack; i < num_block_stack; i++, p++) {
		if (to > p->to)
			return false;
	}
	
	p = pushBlockStackItem();

	// Try to determine if this is a while loop. For this, first check if we 
	// jump right behind a regular jump, then whether that jump is targeting us.
	if (scriptVersion == 8) {
		p->isWhile = (*(byte*)(org_pos+to-5) == g_jump_opcode);
		i = TO_LE_32(*(int32*)(org_pos+to-4));
	} else {
		p->isWhile = (*(byte*)(org_pos+to-3) == g_jump_opcode);
		i = TO_LE_16(*(int16*)(org_pos+to-2));
	}
	
	p->isWhile = p->isWhile && (offs_of_line == (int)to + i);
	p->from = cur;
	p->to = to;
	return true;
}

void writePendingElse()
{
	if (pendingElse) {
		char buf[32];
		sprintf(buf, alwaysShowOffs ? "} else /*%.4X*/ {" : "} else {", pendingElseTo);
		outputLine(buf, pendingElseOffs, pendingElseOpcode, pendingElseIndent - 1);
		pendingElse = false;
	}
}

Index: descumm.h
===================================================================
RCS file: /cvsroot/scummvm/tools/descumm.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- descumm.h	10 May 2003 17:38:44 -0000	1.1
+++ descumm.h	10 May 2003 18:29:08 -0000	1.2
@@ -81,55 +81,67 @@
 	bool isWhile;			// Set to true if we think this jump is part of a while loop
 };
 
-BlockStack *block_stack;
-int num_block_stack;
+extern BlockStack *block_stack;
+extern int num_block_stack;
 
 //
 // Jump decoding auxillaries (used by the code which tries to translate jumps
 // back into if / else / while / etc. constructs).
 //
-bool pendingElse, haveElse;
-int pendingElseTo;
-int pendingElseOffs;
-int pendingElseOpcode;
-int pendingElseIndent;
+extern bool pendingElse, haveElse;
+extern int pendingElseTo;
+extern int pendingElseOffs;
+extern int pendingElseOpcode;
+extern int pendingElseIndent;
 
 //
 // The opcode of an unconditional jump instruction.
 //
-int g_jump_opcode;
-
+extern int g_jump_opcode;
 
 //
 // Command line options
 //
-bool alwaysShowOffs = false;
-bool dontOutputIfs = false;
-bool dontOutputElse = false;
-bool dontOutputElseif = false;
-bool dontOutputWhile = false;
-bool dontShowOpcode = false;
-bool dontShowOffsets = false;
-bool haltOnError;
-
+extern bool alwaysShowOffs;
+extern bool dontOutputIfs;
+extern bool dontOutputElse;
+extern bool dontOutputElseif;
+extern bool dontOutputWhile;
+extern bool dontShowOpcode;
+extern bool dontShowOffsets;
+extern bool haltOnError;
 
 //
 // The SCUMM version used for the script we are descumming.
 //
-byte scriptVersion;
-
+extern byte scriptVersion;
 
 //
 // Various positions / offsets
 //
-byte *cur_pos, *org_pos;
-int offs_of_line;
+extern byte *cur_pos, *org_pos;
+extern int offs_of_line;
 
 //
 // Total size of the currently loaded script
 //
-int size_of_code;
+extern int size_of_code;
 
+//
+// Common
+//
+
+extern char *getIndentString(int i);
+extern void outputLine(char *buf, int curoffs, int opcode, int indent);
+extern bool indentBlock(unsigned int cur);
+
+extern char *strecpy(char *buf, const char *src);
+extern int get_curoffs();
+extern int get_byte();
+extern uint get_word();
+extern int get_signed_word();
 
+extern bool maybeAddIf(unsigned int cur, unsigned int to);
+extern void writePendingElse();
 
 #endif

Index: descumm.cpp
===================================================================
RCS file: /cvsroot/scummvm/tools/descumm.cpp,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- descumm.cpp	10 May 2003 17:38:43 -0000	1.33
+++ descumm.cpp	10 May 2003 18:29:08 -0000	1.34
@@ -92,9 +92,7 @@
 
 
 void get_tok_V2(char *buf);	// For V2 (and V1?)
-void get_tok(char *buf);	// For V3, V4, V5
-
-char *indentbuf;
+void get_tok_V345(char *buf);	// For V3, V4, V5
 
 int get_curoffs();
 
@@ -104,33 +102,6 @@
 
 void emit_if(char *buf, char *condition);
 
-char *strecpy(char *buf, const char *src)
-{
-	strcpy(buf, src);
-	return strchr(buf, 0);
-}
-
-int get_curoffs()
-{
-	return cur_pos - org_pos;
-}
-
-int get_byte()
-{
-	return (byte)(*cur_pos++);
-}
-
-int get_word()
-{
-	int i = TO_LE_16(*((short *)cur_pos));
-	cur_pos += 2;
-	return i;
-}
-
-int get_signed_word()
-{
-	return (short)get_word();
-}
 
 
 const char *get_num_string(int i)
@@ -345,81 +316,6 @@
 }
 
 
-#define INDENT_SIZE 2
-
-
-
-char *GetIndentString(int i)
-{
-	char *c = indentbuf;
-	i += i;
-	if (!c)
-		indentbuf = c = (char *)malloc(127 * INDENT_SIZE + 1);
-	if (i >= 127 * INDENT_SIZE)
-		i = 127 * INDENT_SIZE;
-	if (i < 0)
-		i = 0;
-	memset(c, 32, i);
-	c[i] = 0;
-	return c;
-}
-
-
-
-BlockStack *pushBlockStackItem()
-{
-	if (!block_stack)
-		block_stack = (BlockStack *) malloc(256 * sizeof(BlockStack));
-
-	if (num_block_stack >= 256) {
-		printf("block_stack full!\n");
-		exit(0);
-	}
-	return &block_stack[num_block_stack++];
-}
-
-/* Returns 0 or 1 depending if it's ok to add a block */
-bool maybeAddIf(unsigned int cur, unsigned int to)
-{
-	int i;
-	BlockStack *p;
-	
-	if (((to | cur) >> 16) || (to <= cur))
-		return false; // Invalid jump
-	
-	for (i = 0, p = block_stack; i < num_block_stack; i++, p++) {
-		if (to > p->to)
-			return false;
-	}
-	
-	p = pushBlockStackItem();
-
-	// Try to determine if this is a while loop. For this, first check if we 
-	// jump right behind a regular jump, then whether that jump is targeting us.
-	p->isWhile = (*(byte*)(org_pos+to-3) == g_jump_opcode);
-	i = TO_LE_16(*(int16*)(org_pos+to-2));
-	
-	p->isWhile = p->isWhile && (offs_of_line == (int)to + i);
-	p->from = cur;
-	p->to = to;
-	return 1;
-}
-
-int indentBlock(unsigned int cur)
-{
-	BlockStack *p;
-
-	if (!num_block_stack)
-		return 0;
-
-	p = &block_stack[num_block_stack - 1];
-	if (cur < p->to)
-		return 0;
-
-	num_block_stack--;
-	return 1;
-}
-
 /* Returns 0 or 1 depending if it's ok to add an else */
 int maybeAddElse(int cur, int to)
 {
@@ -481,48 +377,6 @@
 }
 
 
-void outputLine(char *buf, int curoffs, int opcode, int indent)
-{
-	char *s;
-
-	if (buf[0]) {
-		if (indent == -1)
-			indent = num_block_stack;
-		if (curoffs == -1)
-			curoffs = get_curoffs();
-
-		s = GetIndentString(indent);
-
-		if (dontShowOpcode) {
-			if (dontShowOffsets)
-				printf("%s%s\n", s, buf);
-			else
-				printf("[%.4X] %s%s\n", curoffs, s, buf);
-		} else {
-			char buf2[4];
-			if (opcode != -1)
-				sprintf(buf2, "%.2X", opcode);
-			else
-				strcpy(buf2, "**");
-			if (dontShowOffsets)
-				printf("(%s) %s%s\n", buf2, s, buf);
-			else
-				printf("[%.4X] (%s) %s%s\n", curoffs, buf2, s, buf);
-		}
-	}
-}
-
-
-void writePendingElse()
-{
-	if (pendingElse) {
-		char buf[32];
-		sprintf(buf, alwaysShowOffs ? "} else /*%.4X*/ {" : "} else {", pendingElseTo);
-		outputLine(buf, pendingElseOffs, pendingElseOpcode, pendingElseIndent - 1);
-		pendingElse = 0;
-	}
-}
-
 void do_decodeparsestring_v2(char *buf, byte opcode)
 {
 	byte buffer[256];
@@ -789,7 +643,7 @@
 			if (scriptVersion == 2)
 				get_tok_V2(buf2);
 			else
-				get_tok(buf2);
+				get_tok_V345(buf2);
 			strecpy(strchr(buf2, 0), ">");
 			AddToExprStack(buf);
 			break;
@@ -837,7 +691,7 @@
 			if (scriptVersion == 2)
 				get_tok_V2(strchr(buf, 0));
 			else
-				get_tok(strchr(buf, 0));
+				get_tok_V345(strchr(buf, 0));
 			break;
 		default:
 			sprintf(buf, "UNKNOWN %d", i);
@@ -2354,7 +2208,7 @@
 	}
 }
 
-void get_tok(char *buf)
+void get_tok_V345(char *buf)
 {
 	byte opcode = get_byte();
 
@@ -3091,7 +2945,7 @@
 	exit(0);
 }
 
-int skipVerbHeader(byte *p)
+int skipVerbHeader_V23(byte *p)
 {
 	byte code;
 	int offset = 19;
@@ -3260,7 +3114,7 @@
 				mem += 6;
 				break;			/* Exit code */
 			case MKID('OC'):
-				mem += skipVerbHeader(mem + 19);
+				mem += skipVerbHeader_V23(mem + 19);
 				break;			/* Verb */
 			default:
 				printf("Unknown script type!\n");
@@ -3270,7 +3124,6 @@
 
 	cur_pos = mem;
 	org_pos = mem;
-
 	len -= mem - memorg;
 
 	offs_of_line = 0;
@@ -3278,10 +3131,11 @@
 	do {
 		byte opcode = *cur_pos;
 		int j = num_block_stack;
+		buf[0] = 0;
 		if (scriptVersion == 2)
 			get_tok_V2(buf);
 		else
-			get_tok(buf);
+			get_tok_V345(buf);
 		if (buf[0]) {
 			writePendingElse();
 			if (haveElse) {
@@ -3296,7 +3150,6 @@
 		}
 		fflush(stdout);
 	} while (cur_pos < mem + len);
-
 
 	printf("END\n");
 

Index: descumm6.cpp
===================================================================
RCS file: /cvsroot/scummvm/tools/descumm6.cpp,v
retrieving revision 1.95
retrieving revision 1.96
diff -u -d -r1.95 -r1.96
--- descumm6.cpp	10 May 2003 17:38:43 -0000	1.95
+++ descumm6.cpp	10 May 2003 18:29:08 -0000	1.96
@@ -594,85 +594,44 @@
 	exit(1);
 }
 
-char *strecpy(char *buf, const char *src)
-{
-	strcpy(buf, src);
-	return strchr(buf, 0);
-}
-
-int get_curoffs()
+byte *skipVerbHeader_V8(byte *p)
 {
-	return cur_pos - org_pos;
-}
+	uint32 *ptr;
+	uint32 code;
+	int hdrlen;
+	
+	ptr = (uint32 *)p;
+	while (TO_LE_32(*ptr++) != 0) {
+		ptr++;
+	}
+	hdrlen = (byte *)ptr - p;
+	
+	ptr = (uint32 *)p;
+	while ((code = TO_LE_32(*ptr++)) != 0) {
+		printf("  %2d - %.4X\n", code, TO_LE_32(*ptr++) - hdrlen);
+	}
 
-int get_byte()
-{
-	return (byte)(*cur_pos++);
+	return (byte *)ptr;
 }
-
-uint get_word()
+byte *skipVerbHeader_V67(byte *p)
 {
-	int i;
+	byte code;
+	byte *p2 = p;
+	int hdrlen;
 
-	if (scriptVersion == 8) {
-		i = TO_LE_32(*((int32 *)cur_pos));
-		cur_pos += 4;
-	} else {
-		i = TO_LE_16(*((int16 *)cur_pos));
-		cur_pos += 2;
+	while (*p2++ != 0) {
+		p2 += 2;
 	}
-	return i;
-}
 
-int get_signed_word()
-{
-	uint i = get_word();
-
-	if (scriptVersion == 8) {
-		return (int32)i;
-	} else {
-		return (int16)i;
-	}
-}
+	printf("Events:\n");
 
-byte *skipVerbHeader(byte *p)
-{
-	if (scriptVersion == 8) {
-		uint32 *ptr;
-		uint32 code;
-		int hdrlen;
-		
-		ptr = (uint32 *)p;
-		while (TO_LE_32(*ptr++) != 0) {
-			ptr++;
-		}
-		hdrlen = (byte *)ptr - p;
-		
-		ptr = (uint32 *)p;
-		while ((code = TO_LE_32(*ptr++)) != 0) {
-			printf("  %2d - %.4X\n", code, TO_LE_32(*ptr++) - hdrlen);
-		}
+	hdrlen = p2 - p + 8;
 
-		return (byte *)ptr;
-	} else {
-		byte code;
-		byte *p2 = p;
-		int hdrlen;
-	
-		while (*p2++ != 0) {
-			p2 += 2;
-		}
-	
-		printf("Events:\n");
-	
-		hdrlen = p2 - p + 8;
-	
-		while ((code = *p++) != 0) {
-			printf("  %2X - %.4X\n", code, *(uint16 *)p - hdrlen);
-			p += 2;
-		}
-		return p;
+	while ((code = *p++) != 0) {
+		printf("  %2X - %.4X\n", code, *(uint16 *)p - hdrlen);
+		p += 2;
 	}
+	return p;
 }
 
 StackEnt *se_new(int type)
@@ -1118,50 +1077,6 @@
 	}
 }
 
-BlockStack *pushBlockStackItem()
-{
-	if (!block_stack)
-		block_stack = (BlockStack *) malloc(256 * sizeof(BlockStack));
-
-	if (num_block_stack >= 256) {
-		printf("block_stack full!\n");
-		exit(0);
-	}
-	return &block_stack[num_block_stack++];
-}
-
-
-bool maybeAddIf(unsigned int cur, unsigned int to)
-{
-	int i;
-	BlockStack *p;
-	
-	if (((to | cur) >> 16) || (to <= cur))
-		return false; // Invalid jump
-	
-	for (i = 0, p = block_stack; i < num_block_stack; i++, p++) {
-		if (to > p->to)
-			return false;
-	}
-	
-	p = pushBlockStackItem();
-	
-	// Try to determine if this is a while loop. For this, first check if we 
-	// jump right behind a regular jump, then whether that jump is targeting us.
-	if (scriptVersion == 8) {
-		p->isWhile = (*(byte*)(org_pos+to-5) == g_jump_opcode);
-		i = TO_LE_32(*(int32*)(org_pos+to-4));
-	} else {
-		p->isWhile = (*(byte*)(org_pos+to-3) == g_jump_opcode);
-		i = TO_LE_16(*(int16*)(org_pos+to-2));
-	}
-	
-	p->isWhile = p->isWhile && (offs_of_line == (int)to + i);
-	p->from = cur;
-	p->to = to;
-	return true;
-}
-
 /* Returns 0 or 1 depending if it's ok to add an else */
 bool maybeAddElse(unsigned int cur, unsigned int to)
 {
@@ -1970,7 +1885,7 @@
 	}
 }
 
-void next_line()
+void next_line_V67()
 {
 	byte code = get_byte();
 	StackEnt *se_a, *se_b;
@@ -2629,84 +2544,6 @@
 }
 
 
-
-char *indentbuf;
-
-#define INDENT_SIZE 2
-char *getIndentString(int i)
-{
-	char *c = indentbuf;
-	i += i;
-	if (!c)
-		indentbuf = c = (char *)malloc(127 * INDENT_SIZE + 1);
-	if (i >= 127 * INDENT_SIZE)
-		i = 127 * INDENT_SIZE;
-	if (i < 0)
-		i = 0;
-	memset(c, 32, i);
-	c[i] = 0;
-	return c;
-}
-
-void outputLine(char *buf, int curoffs, int opcode, int indent)
-{
-	char *s;
-
-	if (buf[0]) {
-		if (indent == -1)
-			indent = num_block_stack;
-		if (curoffs == -1)
-			curoffs = get_curoffs();
-
-		s = getIndentString(indent);
-
-		if (dontShowOpcode) {
-			if (dontShowOffsets)
-				printf("%s%s\n", s, buf);
-			else
-				printf("[%.4X] %s%s\n", curoffs, s, buf);
-		} else {
-			char buf2[4];
-			if (opcode != -1)
-				sprintf(buf2, "%.2X", opcode);
-			else
-				strcpy(buf2, "**");
-			if (dontShowOffsets)
-				printf("(%s) %s%s\n", buf2, s, buf);
-			else
-				printf("[%.4X] (%s) %s%s\n", curoffs, buf2, s, buf);
-		}
-	}
-}
-
-void writePendingElse()
-{
-	if (pendingElse) {
-		char buf[32];
-		sprintf(buf, alwaysShowOffs ? "} else /*%.4X*/ {" : "} else {", pendingElseTo);
-		outputLine(buf, pendingElseOffs, pendingElseOpcode, pendingElseIndent - 1);
-		pendingElse = false;
-	}
-}
-
-bool indentBlock(unsigned int cur)
-{
-	BlockStack *p;
-
-	if (!num_block_stack)
-		return false;
-
-	p = &block_stack[num_block_stack - 1];
-	if (cur < p->to)
-		return false;
-
-	num_block_stack--;
-	return true;
-}
-
-
-
-
 void ShowHelpAndExit()
 {
 	printf("SCUMM Script decompiler\n"
@@ -2836,7 +2673,10 @@
 		mem += 8;
 		break;											/* Exit code */
 	case 'VERB':
-		mem = skipVerbHeader(mem + 8);
+		if (scriptVersion == 8)
+			mem = skipVerbHeader_V8(mem + 8);
+		else
+			mem = skipVerbHeader_V67(mem + 8);
 		break;											/* Verb */
 	default:
 		printf("Unknown script type!\n");
@@ -2856,7 +2696,7 @@
 		if (scriptVersion == 8)
 			next_line_V8();
 		else
-			next_line();
+			next_line_V67();
 		if (buf[0]) {
 			writePendingElse();
 			if (haveElse) {
@@ -2873,13 +2713,16 @@
 	} while (cur_pos < mem + len);
 
 	printf("END\n");
-	printf("Stack count: %d\n", num_stack);
-	if (num_stack > 0) {
-		printf("Stack contents:\n");
-		while (num_stack) {
-			buf[0] = 0;
-			se_astext(pop(), buf);
-			printf("%s\n", buf);
+	
+	if (scriptVersion >= 6 && num_stack != 0) {
+		printf("Stack count: %d\n", num_stack);
+		if (num_stack > 0) {
+			printf("Stack contents:\n");
+			while (num_stack) {
+				buf[0] = 0;
+				se_astext(pop(), buf);
+				printf("%s\n", buf);
+			}
 		}
 	}
 





More information about the Scummvm-git-logs mailing list