File: mz_sys.c

package info (click to toggle)
biew 5.2.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,808 kB
  • ctags: 5,528
  • sloc: ansic: 38,763; makefile: 389; pascal: 245
file content (225 lines) | stat: -rw-r--r-- 7,931 bytes parent folder | download
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
/** 
 * @namespace   biew
 * @file        mz_sys.c
 * @brief       This file contains implementation of MZ and DOS-SYS format file 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       1995
 * @note        Development, fixes and improvements
**/
#include <string.h>
#include <stdio.h>

#include "colorset.h"
#include "bmfile.h"
#include "biewutil.h"
#include "bconsole.h"
#include "biewlib/pmalloc.h"
#include "biewlib/kbd_code.h"

static const char * __NEAR__ __FASTCALL__ __QueryAddInfo( unsigned char *memmap )
{
  static char rbuff[41];
  unsigned long idl;
  unsigned short idw,idw0;
  idl = ((unsigned long *)memmap)[0];
  idw0 = ((unsigned short *)memmap)[0];
  idw = ((unsigned short *)memmap)[2];
  if(memcmp(memmap,"RJSX",4) == 0) { ArjARC: return "ARJ self-extracting archive"; }
  else
    if(memcmp(memmap,"LZ09",4) == 0) return "LZEXE 0.90 compressed executable";
    else
      if(memcmp(memmap,"LZ91",4) == 0) return "LZEXE 0.91 compressed executable";
      else
        if(memmap[2] == 0xFB)
        {
           char hi,low;
           hi = (memmap[3] >> 4) & 0x0F;
           low = memmap[3] & 0x0F;
           sprintf(rbuff,"Borland TLINK version: %u.%u",(unsigned)hi,(unsigned)low);
           return rbuff;
        }
        else
          if(memcmp(&memmap[2],"PKLITE",6) == 0)
          {
             char hi, low;
             low = memmap[0];
             hi =  memmap[1] & 0x0F;
             sprintf(rbuff,"PKLITE v%u.%u compressed executable",(unsigned)hi,(unsigned)low);
             return rbuff;
          }
          else
            if(memcmp(&memmap[9],"LHarc's SFX",11) == 0) return "LHarc 1.x self-extracting archive";
            else
              if(memcmp(&memmap[8],"LHa's SFX",9) == 0) return "LHa 2.x self-extracting archive";
              else
                if(idl == 0x018A0001L && idw == 0x1565) return "TopSpeed 3.0 CRUNCH compressed file";
                else
                  if(idl == 0x00020001L && idw == 0x0700) return "PKARCK 3.5 self-extracting-archive";
                  else
                    if(idw0 == 0x000F && memmap[2] == 0xA7) return "BSA (Soviet archiver) selft-extarcting";
                    else
                      if(memcmp(&memmap[4],"SFX by LARC",11) == 0) return "LARC self-extracting archive";
                      else
                        if(memcmp(&memmap[8],"LH's SFX",8) == 0) return "LH self-extracting archive";
                        else
                        {
                          unsigned i;
                          for(i = 0;i < 1000-6;i++)
                          {
                            if(memmap[i] == 'a' && memcmp(&memmap[i+1],"RJsfX",5) == 0)
                            {
                               goto ArjARC;
                            }
                          }
                        }
 return 0;
}

static const char * __NEAR__ __FASTCALL__ QueryAddInfo( void )
{
   unsigned char *memmap;
   memmap = PMalloc(1000);
   if(memmap)
   {
     const char *ret;
     unsigned long fpos;
     fpos = bmGetCurrFilePos();
     bmReadBufferEx(memmap,1000,0x1C,BM_SEEK_SET);
     bmSeek(fpos,BM_SEEK_SET);
     ret = __QueryAddInfo(memmap);
     PFREE(memmap);
     return ret;
   }
   return NULL;
}

unsigned long ShowMZHeader( void )
{
 unsigned keycode;
 TWindow * hwnd;
 unsigned long HeadSize;
 unsigned long newcpos,fpos;
 unsigned long FPageCnt;
 const char * addinfo;
 MZHEADER *mz = &oldh.mz;
 fpos = BMGetCurrFilePos();
 keycode = 16;
 if(FType > OLD_EXE) keycode++;
 addinfo = QueryAddInfo();
 if(addinfo) keycode++;
 hwnd = CrtDlgWndnls(" Old Exe Header ",43,keycode-1);
 HeadSize = ((unsigned long)mz->mzHeaderSize) << 4;
 FPageCnt =  ((long)mz->mzPageCount - 1)*512;
 twUseWin(hwnd);
 twGotoXY(1,1);
 twPrintF("Signature            = 'MZ'\n"
          "Part Last Page       = %hu [ bytes ]\n"
          "Page count           = %hu [ pages ]\n"
          "Relocations count    = %hu\n"
          "Header size          = %hu [ paragraphs ]\n"
          "Minimum memory       = %04hXH [ paragraphs ]\n"
          "Maximum memory       = %04hXH [ paragraphs ]\n"
          "SS : SP              = %04hX:%04hXH\n"
          "Check summ           = %hu\n"
          "CS : IP              = %04hX:%04hXH\n"
          "Table offset         = %04hXH [ bytes ]\n"
          "Overlay Number       = %hu\n"
          ,mz->mzPartLastPage
          ,mz->mzPageCount
          ,mz->mzRelocationCount
          ,mz->mzHeaderSize
          ,mz->mzMinMem
          ,mz->mzMaxMem
          ,mz->mzRelocationSS,mz->mzExeSP
          ,mz->mzCheckSumm
          ,mz->mzRelocationCS,mz->mzExeIP
          ,mz->mzTableOffset
          ,mz->mzOverlayNumber);
 newcpos = HeadSize;
 newcpos += (((unsigned long)mz->mzRelocationCS) << 4) + (unsigned long)mz->mzExeIP;
 twSetColorAttr(dialog_cset.entry);
 twPrintF(">Entry Point         = %08lXH",newcpos); twClrEOL();
 twSetColorAttr(dialog_cset.addinfo);
 twPrintF("\nModule Length        = %lu [ bytes ]",(FPageCnt - HeadSize) + mz->mzPartLastPage);
 twClrEOL();
 twSetColorAttr(dialog_cset.main);
 twPrintF("\nImage offset         = %08lXH",(long)HeadSize);
 if(headshift)
 {
   twSetColorAttr(dialog_cset.altinfo);
   twPrintF("\nNew EXE header shift = %08lXH",(long)headshift);
   twClrEOL();
 }
 if(addinfo)
 {
   twSetColorAttr(dialog_cset.extrainfo);
   twPrintF("\n%s",addinfo);
   twClrEOL();
 }
 while(1)
 {
   keycode = GetEvent(drawEmptyPrompt,hwnd);
   if(keycode == KE_F(5) || keycode == KE_ENTER) { fpos = newcpos; break; }
   else
     if(keycode == KE_ESCAPE || keycode == KE_F(10)) break;
 }
 CloseWnd(hwnd);
 return fpos;
}

unsigned long ShowSysHeader( void )
{
 int keycode;
 TWindow *hwnd;
 tBool charun;
 long fpos;
 fpos = BMGetCurrFilePos();
 hwnd = CrtDlgWndnls(" DOS Device Driver Header ",57,13);
 charun = (oldh.drv.ddAttribute & 0x8000) == 0x8000;
 if(charun) twPrintF("Device Name               = %8s\n",oldh.drv.ddName);
 else       twPrintF("Device Supports             %u Block Units\n",(unsigned)oldh.drv.ddName[0]);
 twPrintF("Attributes :\n"
          "  [%c]  STDIN\n"
          "  [%c]  STDOUT\n"
          "  [%c]  STDNUL\n"
          "  [%c]  CLOCK\n"
          "  [%c]  Support Logical Device\n"
          "  [%c]  Support OPEN/CLOSE/RM\n"
          "  [%c]  Non IBM Block-Device\n"
          "  [%c]  Support IOCTL\n"
          "  Device is                %s\n"
         ,GetBool(oldh.drv.ddAttribute & 0x0001)
         ,GetBool(oldh.drv.ddAttribute & 0x0002)
         ,GetBool(oldh.drv.ddAttribute & 0x0004)
         ,GetBool(oldh.drv.ddAttribute & 0x0008)
         ,GetBool(oldh.drv.ddAttribute & 0x0040)
         ,GetBool(oldh.drv.ddAttribute & 0x0800)
         ,GetBool(oldh.drv.ddAttribute & 0x2000)
         ,GetBool(oldh.drv.ddAttribute & 0x4000)
         ,(oldh.drv.ddAttribute & 0x8000 ? "CHARACTERS" : "BLOCKS"));
 twSetColorAttr(dialog_cset.entry);
 twPrintF(">STRATEGY routine offset  = %04hXH bytes [Enter]",oldh.drv.ddStrategyOff);
 twClrEOL(); twPrintF("\n");
 twSetColorAttr(dialog_cset.altentry);
 twPrintF(">INTERRUPT routine offset = %04hXH bytes [Ctrl+Enter | F5]",oldh.drv.ddInterruptOff);
 twClrEOL();
 while(1)
 {
   keycode = GetEvent(drawEmptyPrompt,hwnd);
   if(keycode == KE_ENTER) { fpos = oldh.drv.ddStrategyOff; break; }
   else
     if(keycode == KE_CTL_ENTER || keycode == KE_F(5)) { fpos = oldh.drv.ddInterruptOff; break; }
     else
       if(keycode == KE_ESCAPE || keycode == KE_F(10)) break;
 }
 CloseWnd(hwnd);
 return fpos;
}