File: arch.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 (203 lines) | stat: -rw-r--r-- 5,543 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
/**
 * @namespace   biew_plugins_auto
 * @file        plugins/bin/arch.c
 * @brief       This file contains implementation of Archive 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 <time.h>

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

ar_hdr arch;

static unsigned long __FASTCALL__ ShowARCHHeader( void )
{
  unsigned long fpos,ldat;
  unsigned evt;
  TWindow * w;
  struct tm * tm;
  char sout[50];
  fpos = BMGetCurrFilePos();
  w = CrtDlgWndnls(" This is COFF or a.out archive ",54,6);
  twGotoXY(1,1);
  strncpy(sout,(char *)arch.ar_name,16);
  sout[16] = 0;
  twPrintF("Name           = %s\n",sout);
  strncpy(sout,(char *)arch.ar_date,12);
  sout[12] = 0;
  ldat = atol(sout);
  tm = localtime((time_t *)&ldat);
  strftime(sout,sizeof(sout),"%X %x",tm);
  twPrintF("Date           = %s\n",sout);
  strncpy(sout,(char *)arch.ar_uid,6);
  sout[6] = 0;
  twPrintF("Owner UID      = %s\n",sout);
  strncpy(sout,(char *)arch.ar_gid,6);
  sout[6] = 0;
  twPrintF("Owner GID      = %s\n",sout);
  strncpy(sout,(char *)arch.ar_mode,8);
  sout[8] = 0;
  twPrintF("File mode      = %s\n",sout);
  strncpy(sout,(char *)arch.ar_size,10);
  sout[10] = 0;
  twPrintF("File size      = %s bytes",sout);
  do
  {
    evt = GetEvent(drawEmptyPrompt,NULL,w);
  }
  while(!(evt == KE_ESCAPE || evt == KE_F(10)));
  CloseWnd(w);
  return fpos;
}

static tBool __NEAR__ __FASTCALL__ archReadModList(memArray *obj,unsigned nnames,unsigned long *addr)
{
  unsigned long foff,flen;
  unsigned i;
  char stmp[80];
  flen = bmGetFLength();
  for(i = 0;i < nnames;i++)
  {
    tBool is_eof;
    /**
       Some archives sometimes have big and sometimes little endian.
       Here is a horrible attempt to determine it.
    */
    foff = addr[i];
    if(foff > flen)  foff = ByteSwapL(foff);
    if(IsKbdTerminate()) break;
    bmReadBufferEx(stmp,sizeof(ar_sub_hdr),foff,BM_SEEK_SET);
    is_eof = bmEOF();
    stmp[sizeof(ar_sub_hdr)-2] = 0;
    if(!ma_AddString(obj,is_eof ? CORRUPT_BIN_MSG : stmp,True)) break;
    if(is_eof) break;
  }
  return True;
}

static unsigned long __FASTCALL__ archModLst( void )
{
   memArray *obj;
   unsigned long *addr;
   unsigned nnames;
   unsigned long rnames,bnames;
   unsigned long fpos,flen;
   fpos = BMGetCurrFilePos();
   flen = bmGetFLength();
   rnames = bmReadDWordEx(sizeof(ar_hdr),BM_SEEK_SET);
   bnames = ByteSwapL(rnames);
   /**
      Some archives sometimes have big and sometimes little endian.
      Here is a horrible attempt to determine it.
   */
   if(!(nnames = (unsigned)min(rnames,bnames))) { NotifyBox(NOT_ENTRY,"Archive modules list"); return fpos; }
   /**
      Some archives sometimes have length and sometimes number of entries
      Here is a horrible attempt to determine it.
   */
   if(!(nnames%4)) nnames/=sizeof(unsigned long);
   if(!(obj = ma_Build(nnames,True))) return fpos;
   if(!(addr = PMalloc(sizeof(unsigned long)*nnames))) goto exit;
   bmReadBufferEx(addr,sizeof(unsigned long)*nnames,sizeof(ar_hdr)+sizeof(unsigned long),BM_SEEK_SET);
   if(archReadModList(obj,nnames,addr))
   {
     int ret;
     ret = ma_Display(obj," Archive modules list ",LB_SELECTIVE,-1);
     if(ret != -1)
     {
       /**
          Some archives sometimes have big and sometimes little endian.
          Here is a horrible attempt to determine it.
       */
       fpos = addr[ret];
       if(fpos > flen) fpos = ByteSwapL(fpos);
       fpos += sizeof(ar_sub_hdr);
     }
   }
   free(addr);
   exit:
   ma_Destroy(obj);
   return fpos;
}

static tBool __FASTCALL__ IsArch( void )
{
  char str[16];
  bmReadBufferEx(str,sizeof(str),0,BM_SEEK_SET);
  return strncmp(str,"!<arch>\012",8) == 0;
}

static void __FASTCALL__ ArchInit( void )
{
  bmReadBufferEx(&arch,sizeof(arch),0,BM_SEEK_SET);
}

static void __FASTCALL__ ArchDestroy( void )
{
}

static tBool __FASTCALL__ archAddrResolv(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(ar_hdr))
  {
    strcpy(addr,"arch.h:");
    strcpy(&addr[7],Get2Digit(cfpos));
  }
  else bret = False;
  return bret;
}

static unsigned long __FASTCALL__ archHelp( void )
{
  hlpDisplay(10001);
  return BMGetCurrFilePos();
}

static int __FASTCALL__ arch_platform( void ) { return DISASM_DEFAULT; }

REGISTRY_BIN archTable =
{
  "arch (Archive)",
  { "ArcHlp", NULL, "ModLst", NULL, NULL, NULL, NULL, NULL, NULL, NULL },
  { archHelp, NULL, archModLst, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
  IsArch, ArchInit, ArchDestroy,
  ShowARCHHeader,
  NULL,
  NULL,
  arch_platform,
  NULL,
  archAddrResolv,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL
};