-
Notifications
You must be signed in to change notification settings - Fork 2
/
Common.cpp
468 lines (439 loc) · 16.7 KB
/
Common.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/************************************************************************
* common.cpp *
* This includes the lptr class, and any other common functions that *
* are needed. *
* The lptr class is a class of 'long pointers' which contain a segment *
* and an offset value. *
* I have defined some arithmetic on these which make coding in Borg *
* easier. *
* Within Borg the seg/offset values should be well defined, ie unique. *
* This means we do not have to test for seg1:offs1==seg2:offs2 where *
* seg1!=seg2. Borg addresses should all be individual. [Of course this *
* may make some coding of DOS exe's harder later on, but I dont believe *
* it will be a great burden. *
************************************************************************/
#include <windows.h>
#include "common.h"
#include "dasm.h"
#include "resource.h"
/************************************************************************
* nlptr *
* - this is a predefined null pointer *
************************************************************************/
const lptr nlptr(0,0);
/************************************************************************
* lptr constructor with values *
* - the standard constructor and destructor are defined in the header *
* as null functions. This constructor allows for declarations like *
* the nlptr declaration above *
************************************************************************/
lptr::lptr(word seg,dword off)
{ segm=seg;
offs=off;
}
/************************************************************************
* assign *
* - this is an assign function for the lptr, to give it a specific *
* segment and offset combination. *
************************************************************************/
void lptr::assign(word seg,dword off)
{ segm=seg;
offs=off;
}
/************************************************************************
* between *
* - returns true if loc >= lwb and loc <= upb *
************************************************************************/
bool lptr::between(const lptr& lwb,const lptr& upb)
{ return ((*this>=lwb) && (*this<=upb));
}
/************************************************************************
* lptr == operator *
************************************************************************/
bool lptr::operator==(const lptr& loc2)
{ return ((segm==loc2.segm)&&(offs==loc2.offs));
}
/************************************************************************
* lptr <= operator *
* - this overload of <= is well defined given the constraints we have *
* placed on the lptr class. It allows simplification of code in many *
* of the lists and databases. *
************************************************************************/
bool lptr::operator<=(const lptr& loc2)
{ if(segm!=loc2.segm)
return (segm<=loc2.segm);
return (offs<=loc2.offs);
}
/************************************************************************
* lptr >= operator *
* - this overload of >= is well defined given the constraints we have *
* placed on the lptr class. It allows simplification of code in many *
* of the lists and databases. *
************************************************************************/
bool lptr::operator>=(const lptr& loc2)
{ if(segm!=loc2.segm)
return (segm>=loc2.segm);
return (offs>=loc2.offs);
}
/************************************************************************
* lptr < operator *
* - this overload of < is well defined given the constraints we have *
* placed on the lptr class. It allows simplification of code in many *
* of the lists and databases. *
************************************************************************/
bool lptr::operator<(const lptr& loc2)
{ if(segm!=loc2.segm)
return (segm<loc2.segm);
return (offs<loc2.offs);
}
/************************************************************************
* lptr > operator *
* - this overload of > is well defined given the constraints we have *
* placed on the lptr class. It allows simplification of code in many *
* of the lists and databases. *
************************************************************************/
bool lptr::operator>(const lptr& loc2)
{ if(segm!=loc2.segm)
return (segm>loc2.segm);
return (offs>loc2.offs);
}
/************************************************************************
* lptr != operator *
* - this overload of != is well defined given the constraints we have *
* placed on the lptr class. It allows simplification of code in many *
* of the lists and databases. *
************************************************************************/
bool lptr::operator!=(const lptr& loc2)
{ return ((segm!=loc2.segm)||(offs!=loc2.offs));
}
/************************************************************************
* lptr + dword operator *
* - this has been defined to allowed an expression like lptr r+1 to be *
* used. It simply adds the dword to the offset of the lptr, and does *
* not change the segment at all *
************************************************************************/
lptr lptr::operator+(dword offs2)
{ lptr tmp;
tmp.segm=segm;
tmp.offs=offs+offs2;
return tmp;
}
/************************************************************************
* lptr ++ operator *
* - as for + dword we can define ++ similarly *
* - note that in these functions offsets can wrap around but generally *
* this should not be a problem for us *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
// increment lptr.
lptr& lptr::operator++(int x)
{ offs++;
return *this;
}
#ifdef __BORLANDC__
#pragma warn +par
#endif
/************************************************************************
* lptr += operator *
* - An overload of +=, as for + and ++ *
************************************************************************/
lptr& lptr::operator+=(dword offs2)
{ offs+=offs2;
return *this;
}
/************************************************************************
* lptr - (dword) operator *
* - If we can add a dword then we take one away also.... *
************************************************************************/
lptr lptr::operator-(dword offs2)
{ lptr tmp;
tmp.segm=segm;
tmp.offs=offs-offs2;
return tmp;
}
/************************************************************************
* lptr1 - lptr2 operator *
* - I have overloaded - in two ways. The first, above, takes a dword *
* from an operator, by taking it from the lptrs offset. This overload *
* allows the difference in two operators to be taken, and returns a *
* dword which is the difference in offsets. Now you should only *
* really be interested in this if the segments are the same. I found *
* it useful to make this definition because it greatly simplifies *
* code elsewhere. Note that with some complex expressions you need to *
* force operator precedence, for example dword+(loc1-loc2)+dword2 *
* type expressions need the brackets for force the - to be performed *
* first. *
************************************************************************/
dword lptr::operator-(lptr& loc2)
{ return offs-loc2.offs;
}
/************************************************************************
* Basic Support Functions *
************************************************************************/
/************************************************************************
* cleanstring *
* - moved out of the disasm class build 211. *
* - simply changes any strange characters in a string into an *
* underscore, used for generating automatic names from disassembled *
* strings. *
************************************************************************/
void cleanstring(char *str)
{ unsigned int i;
for(i=0;i<strlen(str);i++)
{ if(!isalnum(str[i]))
str[i]='_';
}
}
/************************************************************************
* CenterWindow *
* - centers a window within its client area, used by Dialog functions *
************************************************************************/
void CenterWindow(HWND hdwnd)
{ RECT drect,prect;
HWND parent;
parent=GetParent(hdwnd);
GetWindowRect(parent,&prect);
GetWindowRect(hdwnd,&drect);
MoveWindow(hdwnd,((prect.right+prect.left)-(drect.right-drect.left))/2,
((prect.bottom+prect.top)-(drect.bottom-drect.top))/2,drect.right-drect.left,
drect.bottom-drect.top,true);
}
/************************************************************************
* demangle *
* - this is a general string function. Name damangling is currently not *
* very good, and I have some old Borland source code which could *
* improve this greatly......... just need to dig it out, rework it *
* for Borg, and put it in here now...... *
************************************************************************/
void demangle(char **nme)
{ unsigned char buff[256],buff2[256],*name;
unsigned int namelen,i,j,k,atcount,bpoint;
bool brac,pointer,rpointer;
if(!options.demangle)
return;
atcount=0;
i=0;
j=0;
bpoint=0;
brac=false;
name=(unsigned char *)(*nme);
while(name[i])
{ if(name[i]=='@')
{ atcount++;
if(atcount>1)
{ buff[j++]=':';
buff[j++]=':';
}
}
else if((name[i]=='$')&&(name[i+1]=='q')&&(!brac))
{ brac=true;
bpoint=j;
buff[j++]='(';
i+=1;
}
else if((name[i]=='$')&&(name[i+1]=='x')&&(name[i+2]=='q')&&(!brac))
{ brac=true;
bpoint=j;
buff[j++]='(';
i+=2;
}
else if(!strnicmp((char *)&name[i],"$bctr",5))
{ k=0;
while((buff[k]!=':')&&(k<20))
{ buff[j++]=buff[k++];
}
i+=4;
}
else if(!strnicmp((char *)&name[i],"$bdtr",5))
{ k=0;
buff[j++]='~';
while((buff[k]!=':')&&(k<20))
{ buff[j++]=buff[k++];
}
i+=4;
}
else if(!strnicmp((char *)&name[i],"$bdla",5))
{ strcpy((char *)&buff[j],"delete");
j+=6;
i+=4;
}
else if(!strnicmp((char *)&name[i],"$bnwa",5))
{ strcpy((char *)&buff[j],"new");
j+=3;
i+=4;
}
else if(!strnicmp((char *)&name[i],"$bdele",6))
{ strcpy((char *)&buff[j],"delete");
j+=6;
i+=5;
}
else if(!strnicmp((char *)&name[i],"$bnew",5))
{ strcpy((char *)&buff[j],"new");
j+=5;
i+=4;
}
else
buff[j++]=name[i];
i++;
}
if(brac)
buff[j++]=')';
buff[j]=0;
strcpy((char *)buff2,(char *)buff);
if(brac)
{ j=bpoint;
k=bpoint;
i=0;
pointer=false;
rpointer=false;
while(buff[j])
{ if((buff[j]=='p')&&(!i))
{ pointer=true;
j++;
}
if((buff[j]=='r')&&(!i))
{ rpointer=true;
j++;
}
else if((buff[j]=='i')&&(!i))
{ i=1;
strcpy((char *)&buff2[k],"int");
k+=3;
j+=1;
}
else if((buff[j]=='c')&&(!i))
{ i=1;
strcpy((char *)&buff2[k],"char");
k+=4;
j+=1;
}
else if((buff[j]=='v')&&(!i))
{ i=1;
strcpy((char *)&buff2[k],"void");
k+=4;
j+=1;
}
else if((buff[j]=='l')&&(!i))
{ i=1;
strcpy((char *)&buff2[k],"long");
k+=4;
j+=1;
}
else if((buff[j]=='u')&&(buff[j+1]=='i')&&(!i))
{ i=1;
strcpy((char *)&buff2[k],"uint");
k+=4;
j+=2;
}
else if((buff[j]==':')&&(buff[j+1]==':')&&(i))
{ strcpy((char *)&buff2[k],"::");
k+=2;
j+=2;
}
else if((buff[j]=='t')&&((buff[j+1]>='0')&&(buff[j+1]<='9'))&&(!i))
{ buff2[k++]=buff[j++];
buff2[k++]=buff[j++];
i=1;
}
else if(((buff[j]>='0')&&(buff[j]<='9'))&&(!i))
{ i=buff[j]-'0';
j++;
if((buff[j]>='0')&&(buff[j]<='9'))
{ i=i*10+buff[j]-'0';
j++;
}
i++;
}
else
buff2[k++]=buff[j++];
if(i)
{ i--;
if(!i)
{ if((pointer)&&(buff2[k-1]!=')'))
buff2[k++]='*';
if((rpointer)&&(buff2[k-1]!=')'))
buff2[k++]='&';
if((buff[j]!=')')&&(buff2[k-1]!=')'))
buff2[k++]=',';
pointer=false;
rpointer=false;
}
}
}
buff2[k]=0;
}
namelen=strlen((char *)buff2);
//BugFix Build 15 nme-> *nme.
delete *nme;
name=new unsigned char[namelen+1];
strcpy((char *)name,(char *)buff2);
*nme=(char *)name;
}
/************************************************************************
* init_ofn *
* - initialises the OPENFILENAME struct used in calls to common dialogs *
* callers can then change options further as needed *
************************************************************************/
void init_ofn(OPENFILENAME *ofn)
{ int cbString,i;
char chReplace;
static char szDirName[MAX_PATH*2];
static char szFilesave[MAX_PATH*2];
static char szFilter[MAX_PATH];
static char szFileTitle[MAX_PATH*2];
GetCurrentDirectory(MAX_PATH,szDirName);
szFilesave[0]=0;
cbString=LoadString(hInst,IDS_FILTERSTRING,szFilter,sizeof(szFilter));
chReplace=szFilter[cbString-1];
for(i=0;szFilter[i]!=0;i++)
{ if(szFilter[i]==chReplace)
szFilter[i]=0;
}
ofn->lStructSize=sizeof(OPENFILENAME);
ofn->hwndOwner=mainwindow;
ofn->hInstance=NULL;
ofn->lpstrFilter=szFilter;
ofn->lpstrCustomFilter=NULL;
ofn->nMaxCustFilter=0;
ofn->nFilterIndex=1;
ofn->lpstrFile=szFilesave;
ofn->nMaxFile=sizeof(szFilesave)/2;
ofn->lpstrFileTitle=szFileTitle;
ofn->nMaxFileTitle=sizeof(szFileTitle)/2;
ofn->lpstrInitialDir=szDirName;
ofn->lpstrTitle="Borg Disassembler - Select File";
ofn->Flags=OFN_PATHMUSTEXIST|OFN_HIDEREADONLY|OFN_LONGNAMES
|OFN_EXPLORER;
ofn->nFileOffset=0;
ofn->nFileExtension=NULL;
ofn->lpstrDefExt=NULL;
ofn->lCustData=0;
ofn->lpfnHook=NULL;
ofn->lpTemplateName=NULL;
}
/************************************************************************
* getfiletoload *
* - obtains a filename for the file to be loaded *
* used for new files to load, and load database file *
* sets fname to the filename *
************************************************************************/
void getfiletoload(char *fname)
{ OPENFILENAME ofn;
init_ofn(&ofn);
ofn.Flags|=OFN_FILEMUSTEXIST;
GetOpenFileName(&ofn);
strcpy(fname,ofn.lpstrFile);
}
/************************************************************************
* getfiletosave *
* - obtains a filename for the file to be saved *
* sets fname to the filename *
************************************************************************/
void getfiletosave(char *fname)
{ OPENFILENAME ofn;
init_ofn(&ofn);
GetSaveFileName(&ofn);
strcpy(fname,ofn.lpstrFile);
}