File: pharlap.c

package info (click to toggle)
biew 5.6.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,004 kB
  • ctags: 7,157
  • sloc: ansic: 50,860; asm: 809; makefile: 396; pascal: 371
file content (310 lines) | stat: -rw-r--r-- 10,115 bytes parent folder | download | duplicates (2)
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
/**
 * @namespace   biew_plugins_auto
 * @file        plugins/bin/pharlap.c
 * @brief       This file contains implementation of PharLap file format decoder.
 * @version     -
 * @remark      this source file is part of Binary vIEW project (BIEW).
 *              The Binary vIEW (BIEW) is copyright (C) 1995 Nick Kurshev.
 *              All rights reserved. This software is redistributable under the
 *              licence given in the file "Licence.en" ("Licence.ru" in russian
 *              translation) distributed in the BIEW archive.
 * @note        Requires POSIX compatible development system
 *
 * @author      Nick Kurshev
 * @since       1999
 * @note        Development, fixes and improvements
**/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "colorset.h"
#include "bconsole.h"
#include "bin_util.h"
#include "biewutil.h"
#include "biewhelp.h"
#include "tstrings.h"
#include "bmfile.h"
#include "reg_form.h"
#include "plugins/bin/pharlap.h"
#include "plugins/disasm.h"
#include "biewlib/biewlib.h"
#include "biewlib/kbd_code.h"

static newPharLap nph;

static BGLOBAL pl_cache = &bNull;

static unsigned long __FASTCALL__ ShowPharLapHeader( void )
{
  unsigned long fpos;
  TWindow *w;
  unsigned keycode;
  char sign[3];
  fpos = BMGetCurrFilePos();
  strncpy(sign,(char *)nph.plSignature,2);
  sign[2] = 0;
  w = CrtDlgWndnls(" New PharLap executable ",59,23);
  twGotoXY(1,1);
  twPrintF("Signature                        = %s\n"
           "Level                            = %s\n"
           "Header size                      = %04XH\n"
           "File size                        = %08lXH\n"
           "Check sum (32-bit checksum)      = %04XH (%08lXH)\n"
           "Run-time parameters (offset/size)= %08lXH/%08lXH\n"
           "Relocations (offset/size)        = %08lXH/%08lXH\n"
           "Segment info table (offset/size) = %08lXH/%08lXH\n"
           "Image (offset/size)              = %08lXH/%08lXH\n"
           "Symbol table (offset/size)       = %08lXH/%08lXH\n"
           "GDT (offset/size)                = %08lXH/%08lXH\n"
           "LDT (offset/size)                = %08lXH/%08lXH\n"
           "IDT (offset/size)                = %08lXH/%08lXH\n"
           "TSS (offset/size)                = %08lXH/%08lXH\n"
           "Min. number of extra 4K pages    = %08lXH\n"
           "Max. number of extra 4K pages    = %08lXH\n"
           "Image base (flat level only)     = %08lXH\n"
           "Initial stack (SS:ESP)           = %04XH:%08lXH\n"
           "Initail code  (CS:EIP)           = %04XH:%08lXH\n"
           "Initial LDT/TSS                  = %04XH/%04XH\n"
           "Flags                            = %04XH\n"
           "Memory requirement for image     = %08lXH\n"
           "Stack size                       = %08lXH"
           ,sign
           ,nph.plLevel == 0x01 ? "Flat" : nph.plLevel == 0x02 ? "Multisegmented" : "Unknown"
           ,nph.plHeaderSize
           ,nph.plFileSize
           ,nph.plCheckSum,nph.plChecksum32
           ,nph.plRunTimeParms,nph.plRunTimeSize
           ,nph.plRelocOffset,nph.plRelocSize
           ,nph.plSegInfoOffset,nph.plSegInfoSize
           ,nph.plImageOffset,nph.plImageSize
           ,nph.plSymTabOffset,nph.plSymTabSize
           ,nph.plGDTOffset,nph.plGDTSize
           ,nph.plLDTOffset,nph.plLDTSize
           ,nph.plIDTOffset,nph.plIDTSize
           ,nph.plTSSOffset,nph.plTSSSize
           ,nph.plMinExtraPages
           ,nph.plMaxExtraPages
           ,nph.plBase
           ,nph.plSS,nph.plESP
           ,nph.plCS,nph.plEIP
           ,nph.plLDT,nph.plTSS
           ,nph.plFlags
           ,nph.plMemReq
           ,nph.plStackSize);
  while(1)
  {
    keycode = GetEvent(drawEmptyPrompt,NULL,w);
    if(keycode == KE_ENTER) { /* fpos = entrypoint*/; break; }
    else
      if(keycode == KE_ESCAPE || keycode == KE_F(10)) break;
  }
  CloseWnd(w);
  return fpos;
}

static void __FASTCALL__ PLSegPaint(TWindow * win,const void ** names,unsigned start,unsigned nlist)
{
 char buffer[81];
 const PLSegInfo ** nam = (const PLSegInfo **)names;
 twUseWin(win);
 twFreezeWin(win);
 twClearWin();
 sprintf(buffer," Segment Table [ %u / %u ] ",start + 1,nlist);
 twSetTitleAttr(win,buffer,TW_TMODE_CENTER,dialog_cset.title);
 twSetFooterAttr(win,PAGEBOX_SUB,TW_TMODE_RIGHT,dialog_cset.selfooter);
 twGotoXY(1,1);
 twPrintF("Selector number            = %04hXH\n"
          "Flags                      = %04hXH\n"
          "Base offset of selector    = %08lXH\n"
          "Min extra memory alloc     = %08lXH"
          ,nam[start]->siSelector
          ,nam[start]->siFlags
          ,nam[start]->siBaseOff
          ,nam[start]->siMinAlloc);
 twRefreshFullWin(win);
}

static tBool __FASTCALL__ __PLReadSegInfo(BGLOBAL handle,memArray * obj,unsigned nnames)
{
 unsigned i;
 for(i = 0;i < nnames;i++)
 {
   PLSegInfo plsi;
   if(IsKbdTerminate() || bioEOF(handle)) break;
   bioReadBuffer(handle,&plsi,sizeof(PLSegInfo));
   if(!ma_AddData(obj,&plsi,sizeof(PLSegInfo),True)) break;
 }
 return True;
}

static unsigned long __FASTCALL__ PharLapSegInfo( void )
{
 BGLOBAL handle;
 unsigned nnames;
 unsigned long fpos;
 memArray * obj;
 if(nph.plSegInfoOffset && nph.plSegInfoSize) nnames = (unsigned)(nph.plSegInfoSize / sizeof(PLSegInfo));
 else                                           nnames = 0;
 fpos = BMGetCurrFilePos();
 if(!nnames) { NotifyBox(NOT_ENTRY," Segment Info table "); return fpos; }
 if(!(obj = ma_Build(nnames,True))) return fpos;
 handle = pl_cache;
 bioSeek(handle,nph.plSegInfoOffset,SEEK_SET);
 if(__PLReadSegInfo(handle,obj,nnames))
 {
    int i;
    i = PageBox(50,4,(const void **)obj->data,obj->nItems,PLSegPaint) + 1;
    if(i > 0)
    {
      fpos = ((unsigned long)((PLSegInfo *)obj->data[i - 1])->siBaseOff)+nph.plImageOffset;
    }
 }
 ma_Destroy(obj);
 return fpos;
}

static void __FASTCALL__ PLRunTimePaint(TWindow * win,const void ** names,unsigned start,unsigned nlist)
{
 char buffer[81];
 char sign[3];
 const PLRunTimeParms ** nam = (const PLRunTimeParms **)names;
 twUseWin(win);
 twFreezeWin(win);
 twClearWin();
 sprintf(buffer," Run-time Parameters Table [ %u / %u ] ",start + 1,nlist);
 twSetTitleAttr(win,buffer,TW_TMODE_CENTER,dialog_cset.title);
 twSetFooterAttr(win,PAGEBOX_SUB,TW_TMODE_RIGHT,dialog_cset.selfooter);
 strncpy(sign,(const char *)nam[start]->rtSignature,2);
 sign[2] = 0;
 twGotoXY(1,1);
 twPrintF("Signature                      = %s\n"
          "Min. number of real-mode parms = %04hXH\n"
          "Max. number of real-mode parms = %04hXH\n"
          "Min. interrupt buffer size     = %04hXH\n"
          "Max. interrupt buffer size     = %04hXH\n"
          "Number of interrupt stacks     = %04hXH\n"
          "Size of each interrupt stack   = %04hXH\n"
          "Offset of end of real-mode data= %08lXH\n"
          "Call buffer size               = %04hXH\n"
          "Flags                          = %04hXH\n"
          "Unpriviledge flags             = %04hXH"
          ,sign
          ,nam[start]->rtMinRModeParms
          ,nam[start]->rtMaxRModeParms
          ,nam[start]->rtMinIBuffSize
          ,nam[start]->rtMaxIBuffSize
          ,nam[start]->rtNIStacks
          ,nam[start]->rtIStackSize
          ,nam[start]->rtEndRModeOffset
          ,nam[start]->rtCallBuffSize
          ,nam[start]->rtFlags
          ,nam[start]->rtUnprivFlags);
 twRefreshFullWin(win);
}

static tBool __FASTCALL__ __PLReadRunTime(BGLOBAL handle,memArray * obj,unsigned nnames)
{
 unsigned i;
 for(i = 0;i < nnames;i++)
 {
   PLRunTimeParms plrtp;
   if(IsKbdTerminate() || bioEOF(handle)) break;
   bioReadBuffer(handle,&plrtp,sizeof(PLRunTimeParms));
   if(!ma_AddData(obj,&plrtp,sizeof(PLRunTimeParms),True)) break;
 }
 return True;
}

static unsigned long __FASTCALL__ PharLapRunTimeParms( void )
{
 BGLOBAL handle;
 unsigned nnames;
 unsigned long fpos;
 memArray * obj;
 if(nph.plRunTimeParms && nph.plRunTimeSize) nnames = (unsigned)(nph.plRunTimeSize / sizeof(PLRunTimeParms));
 else                                          nnames = 0;
 fpos = BMGetCurrFilePos();
 if(!nnames) { NotifyBox(NOT_ENTRY," Run-time parameters "); return fpos; }
 if(!(obj = ma_Build(nnames,True))) return fpos;
 handle = pl_cache;
 bioSeek(handle,nph.plRunTimeParms,SEEK_SET);
 if(__PLReadRunTime(handle,obj,nnames))
 {
    int i;
    i = PageBox(50,11,(const void **)obj->data,obj->nItems,PLRunTimePaint) + 1;
    if(i > 0)
    {
      fpos = nph.plRunTimeParms+i*sizeof(PLRunTimeParms);
    }
 }
 ma_Destroy(obj);
 return fpos;
}

static tBool __FASTCALL__ IsPharLap( void )
{
   char sign[2];
   bmReadBufferEx(sign,2,0,BM_SEEK_SET);
   if(sign[0] == 'P' && (sign[1] == '2' || sign[1] == '3')) return True;
   return False;
}

static void __FASTCALL__ PharLapInit( void )
{
  BGLOBAL main_handle;
  bmReadBufferEx(&nph,sizeof(nph),0,BM_SEEK_SET);
  main_handle = bmbioHandle();
  if((pl_cache = bioDupEx(main_handle,BBIO_SMALL_CACHE_SIZE)) == &bNull) pl_cache = main_handle;
}

static void __FASTCALL__ PharLapDestroy( void )
{
  BGLOBAL main_handle;
  main_handle = bmbioHandle();
  if(pl_cache != &bNull && pl_cache != main_handle) bioClose(pl_cache);
}

static tBool __FASTCALL__ PharLapAddrResolv(char *addr,unsigned long cfpos)
{
 /* Since this function is used in references resolving of disassembler
    it must be seriously optimized for speed. */
  tBool bret = True;
  if(cfpos < sizeof(newPharLap))
  {
    strcpy(addr,"nplhdr:");
    strcpy(&addr[7],Get2Digit(cfpos));
  }
  else bret = False;
  return bret;
}

static unsigned long __FASTCALL__ PharLapHelp( void )
{
  hlpDisplay(10010);
  return BMGetCurrFilePos();
}

static int __FASTCALL__ PharLapPlatform( void ) { return DISASM_CPU_IX86; }

REGISTRY_BIN PharLapTable =
{
  "PharLap",
  { "PLHelp", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "RunTim", "SegInf" },
  { PharLapHelp, NULL, NULL, NULL, NULL, NULL, NULL, NULL, PharLapRunTimeParms, PharLapSegInfo },
  IsPharLap,
  PharLapInit,
  PharLapDestroy,
  ShowPharLapHeader,
  NULL,
  NULL,
  PharLapPlatform,
  NULL,
  PharLapAddrResolv,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL
};