summaryrefslogtreecommitdiff
path: root/tools/dbtool/modulechain.cpp
blob: 515ec9bfbd212768baf2c7ea9abea33699cebfe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Miranda Database Tool
Copyright (C) 2001-2005  Richard Hughes

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.
*/
#include "dbtool.h"

struct ModChainEntry {
	DWORD ofsOld,ofsNew;
	int size;
	char name[257];
} static *modChain=NULL;
static int modChainCount;
static DWORD ofsCurrent;
static int phase,iCurrentModName;
static DWORD ofsLast;
static int last_mod = 0;

int WorkModuleChain(int firstTime)
{
	DBModuleName moduleName,*newModName;

	if(firstTime) {
		AddToStatus(STATUS_MESSAGE,TranslateT("Processing module name chain"));
		modChainCount=0;
		last_mod = 0;
		if(modChain!=NULL) free(modChain);
		modChain = (ModChainEntry*)malloc(sizeof(ModChainEntry));
		phase=0;
		ofsCurrent=dbhdr.ofsFirstModuleName;
	}
	switch(phase) {
		case 0:
			if(ofsCurrent==0) {
				phase++;
				return ERROR_SUCCESS;
			}
			if(!SignatureValid(ofsCurrent,DBMODULENAME_SIGNATURE)) {
				AddToStatus(STATUS_ERROR,TranslateT("Module chain corrupted, further entries ignored"));
				phase++;
				return ERROR_SUCCESS;
			}
			if(PeekSegment(ofsCurrent,&moduleName,offsetof(DBModuleName,name))!=ERROR_SUCCESS) {
				phase++;
				return ERROR_SUCCESS;
			}
			if(moduleName.cbName>256)
				AddToStatus(STATUS_WARNING,TranslateT("Unreasonably long module name, skipping"));
			else {
				modChain=(ModChainEntry*)realloc(modChain,sizeof(ModChainEntry)*++modChainCount);

				modChain[modChainCount-1].ofsOld=ofsCurrent;
				modChain[modChainCount-1].size=offsetof(DBModuleName,name)+moduleName.cbName;
				modChain[modChainCount-1].ofsNew=0;

				if (moduleName.cbName)
					PeekSegment(ofsCurrent+offsetof(DBModuleName,name),&modChain[modChainCount-1].name,moduleName.cbName);
				modChain[modChainCount-1].name[moduleName.cbName]=0;
			}
			ofsCurrent=moduleName.ofsNext;
			break;
		case 1:
			ofsLast = 0;
			iCurrentModName=0;
			dbhdr.ofsFirstModuleName=0;
			phase++;
		case 2:
			if(iCurrentModName>=modChainCount) {
				DWORD dw = 0;
				if(ofsLast)	WriteSegment(ofsLast+offsetof(DBModuleName,ofsNext),&dw,sizeof(DWORD));
				return ERROR_NO_MORE_ITEMS;
			}
			if(modChain[iCurrentModName].ofsNew==0) {
				newModName=(DBModuleName*)_alloca(modChain[iCurrentModName].size);
				if(ReadSegment(modChain[iCurrentModName].ofsOld,newModName,modChain[iCurrentModName].size)!=ERROR_SUCCESS)
					return ERROR_NO_MORE_ITEMS;
				if((modChain[iCurrentModName].ofsNew=WriteSegment(WSOFS_END,newModName,modChain[iCurrentModName].size))==WS_ERROR)
					return ERROR_HANDLE_DISK_FULL;
				{ // check duplicated modulenames
					int i, n=0;
					for(i=iCurrentModName+1;i<modChainCount;i++)
						if(!strcmp(modChain[i].name, modChain[iCurrentModName].name)) {
							modChain[i].ofsNew = modChain[iCurrentModName].ofsNew;
							n++;
						}
					if (n) {
						TCHAR *pszModuleName;
#ifdef UNICODE
						TCHAR szModuleName[257];
						MultiByteToWideChar(CP_ACP, 0, modChain[iCurrentModName].name, -1, szModuleName, sizeof(szModuleName) / sizeof(TCHAR));
						pszModuleName = szModuleName;
#else
						pszModuleName = modChain[iCurrentModName].name;
#endif
						AddToStatus(STATUS_WARNING,TranslateT("Module name '%s' is not unique: %d duplicates found)"), pszModuleName, n);
					}
				}
				if(iCurrentModName==0)
					dbhdr.ofsFirstModuleName=modChain[iCurrentModName].ofsNew;
				else
					if(WriteSegment(ofsLast+offsetof(DBModuleName,ofsNext),&modChain[iCurrentModName].ofsNew,sizeof(DWORD))==WS_ERROR)
						return ERROR_HANDLE_DISK_FULL;
				ofsLast = modChain[iCurrentModName].ofsNew;
			}
			iCurrentModName++;
			break;
	}
	return ERROR_SUCCESS;
}

DWORD ConvertModuleNameOfs(DWORD ofsOld)
{
	int i;

	if ( modChain[last_mod].ofsOld==ofsOld )
		return modChain[last_mod].ofsNew;

	for(i=0;i<modChainCount;i++)
		if(modChain[i].ofsOld==ofsOld) {
			last_mod = i;
			return modChain[last_mod].ofsNew;
		}

	AddToStatus(STATUS_ERROR,TranslateT("Invalid module name offset, skipping data"));
	return 0;
}

void FreeModuleChain()
{
	if(modChain!=NULL) {
		free(modChain);
		modChain = NULL;
		last_mod = 0;
	}
}