Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cab.cpp « cab « formats « src « multiarc - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4528cf33eacc534284283f56225f33077b025a9d (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
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
/*
  CAB.CPP

  Second-level plugin module for FAR Manager and MultiArc plugin

  Copyright (c) 1996 Eugene Roshal
  Copyrigth (c) 2000 FAR group
*/

#include <windows.h>
#include <sudo.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <farplug-mb.h>
using namespace oldfar;
#include "fmt.hpp"

#if defined(__BORLANDC__)
  #pragma option -a1
#elif defined(__GNUC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1100)) || defined(__LCC__)
  #pragma pack(1)
#else
  #pragma pack(push,1)
  #if _MSC_VER
    #define _export
  #endif
#endif



typedef BYTE u1;
typedef WORD u2;
typedef DWORD u4;

struct CFHEADER
{
  u4 signature;
  u4 reserved1;
  u4 cbCabinet;
  u4 reserved2;
  u4 coffFiles;
  u4 nFiles;
  u1 versionMinor;
  u1 versionMajor;
  u2 cFolders;
  u2 cFiles;
  u2 flags;
  u2 setID;
  u2 iCabinet;
};

struct CFFILE
{
  u4 cbFile;
  u4 uoffFolderStart;
  u2 iFolder;
  u2 date;
  u2 time;
  u2 attribs;
  u1 szName[256];
};

static int ArcHandle = -1;
static DWORD SFXSize,FilesNumber;
static DWORD UnpVer;

BOOL WINAPI _export CAB_IsArchive(const char *Name,const unsigned char *Data,int DataSize)
{
  int I;
  if (!CanBeExecutableFileHeader(Data, DataSize) && DataSize > 0x1000)
    DataSize = 0x1000;

  for( I=0; I <= (int)(DataSize-sizeof(struct CFHEADER)); I++ )
  {
    const unsigned char *D=Data+I;
    if (D[0]=='M' && D[1]=='S' && D[2]=='C' && D[3]=='F')
    {
      struct CFHEADER *Header=(struct CFHEADER *)(Data+I);
      if (Header->cbCabinet>sizeof(Header) && Header->coffFiles>sizeof(Header) &&
          Header->coffFiles<0xffff && Header->versionMajor>0 &&
          Header->versionMajor<0x10 && Header->cFolders>0)
      {
        SFXSize=I;
        return(TRUE);
      }
    }
  }
  return(FALSE);
}

static void CloseArcHandle()
{
	sdc_close(ArcHandle);
	ArcHandle = -1;	
}

BOOL WINAPI _export CAB_OpenArchive(const char *Name,int *Type,bool Silent)
{
  struct CFHEADER MainHeader;
  int ReadSize;
  int I;

  ArcHandle = sdc_open(Name, O_RDONLY);
  if (ArcHandle == -1)
    return FALSE;

  *Type=0;

  lseek(ArcHandle, SFXSize, SEEK_SET);
  I = SFXSize;
  ReadSize = sdc_read(ArcHandle, &MainHeader, sizeof(MainHeader));
  if ( ReadSize != sizeof(MainHeader))
    return CloseArcHandle(), FALSE;
	
  if ( !CAB_IsArchive( NULL, (u1*)&MainHeader, sizeof(MainHeader) ))
  {
	struct stat s = {0};
	if (sdc_fstat(ArcHandle, &s)==-1 || (s.st_mode & S_IFMT)!=S_IFREG) {
		return CloseArcHandle(), FALSE;
	}

	ReadSize = (s.st_size > 0x100000) ? 0x100000 : s.st_size ;

//todo: replace with sdc_read cuz mmap is not sudo-able
    LPBYTE Data = (LPBYTE)mmap( NULL, ReadSize, PROT_READ, MAP_PRIVATE, ArcHandle, 0);
    if (Data == (LPBYTE)MAP_FAILED)
		return CloseArcHandle(), FALSE;
		
    I = CAB_IsArchive( NULL, Data, ReadSize );
    if (I)
      memcpy( &MainHeader, Data + SFXSize, sizeof(MainHeader) );
	  
    munmap( Data, ReadSize );
    if (I == 0)
      return CloseArcHandle(), FALSE;
  }
  else
    SFXSize = I;

  lseek(ArcHandle, SFXSize+MainHeader.coffFiles, SEEK_SET);
  FilesNumber = MainHeader.cFiles;
  if (FilesNumber == 65535 && (MainHeader.flags & 8))
    FilesNumber = MainHeader.nFiles;
  UnpVer=MainHeader.versionMajor*256+MainHeader.versionMinor;

  while (FilesNumber && (MainHeader.flags & 1))
  {
    char *EndPos;
    struct CFFILE FileHeader;
	ReadSize = sdc_read(ArcHandle, &FileHeader, sizeof(FileHeader));
    if (ReadSize < 18)
      return CloseArcHandle(), FALSE;
	  
    if (FileHeader.iFolder == 0xFFFD || FileHeader.iFolder == 0xFFFF)
    {
      EndPos = (char*)FileHeader.szName;
      while (EndPos - (char*)&FileHeader < (int)sizeof(FileHeader) && *EndPos)
        EndPos++;
      if (EndPos - (char*)&FileHeader >= (int)sizeof(FileHeader))
        return CloseArcHandle(), FALSE;


      lseek(ArcHandle, (LONG)((EndPos-(char*)&FileHeader+1) - ReadSize), SEEK_CUR);
      FilesNumber--;
    }
    else
    {
      lseek(ArcHandle, 0 - ReadSize, SEEK_CUR);
      break;
    }
  }
///
  return(TRUE);
}


int WINAPI _export CAB_GetArcItem(struct ArcItemInfo *Info)
{
  struct CFFILE FileHeader;

  int ReadSize;
  char *EndPos;
  FILETIME lft;

  if (FilesNumber-- == 0)
    return GETARC_EOF;
  ReadSize = sdc_read(ArcHandle,&FileHeader,sizeof(FileHeader));
  if (ReadSize < 18)
    return GETARC_READERROR;

  EndPos = (char *)FileHeader.szName;
  while (EndPos - (char*)&FileHeader < (int)sizeof(FileHeader) && *EndPos)
    EndPos++;
  if (EndPos - (char*)&FileHeader >= (int)sizeof(FileHeader))
    return GETARC_BROKEN;

  lseek( ArcHandle, (LONG)((EndPos-(char*)&FileHeader+1) - ReadSize), SEEK_CUR);

  EndPos = (char *)FileHeader.szName;
  if (EndPos[ 0 ] == '\\' && EndPos[ 1 ] != '\\')
    EndPos++;

  Info->PathName.assign(EndPos, strnlen(EndPos, &FileHeader.szName[ARRAYSIZE(FileHeader.szName)] - EndPos));

  for (auto &c : Info->PathName) {
    if (c == '\\') c = '/';
  }

  #define _A_ENCRYPTED 8
  Info->dwFileAttributes = FileHeader.attribs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_DIRECTORY);
  Info->Encrypted = FileHeader.attribs & _A_ENCRYPTED;
  Info->nPhysicalSize=0;
  Info->nFileSize=FileHeader.cbFile;
  WINPORT(DosDateTimeToFileTime)(FileHeader.date,FileHeader.time,&lft);
  WINPORT(LocalFileTimeToFileTime)(&lft,&Info->ftLastWriteTime);
  Info->UnpVer=UnpVer;
  return(GETARC_SUCCESS);
}


BOOL WINAPI _export CAB_CloseArchive(struct ArcInfo *Info)
{
  Info->SFXSize=SFXSize;
  return(sdc_close(ArcHandle));
}

DWORD WINAPI _export CAB_GetSFXPos(void)
{
  return SFXSize;
}


BOOL WINAPI _export CAB_GetFormatName(int Type,char *FormatName,char *DefaultExt)
{
  if (Type==0)
  {
    strcpy(FormatName,"CAB");
    strcpy(DefaultExt,"cab");
    return(TRUE);
  }
  return(FALSE);
}


BOOL WINAPI _export CAB_GetDefaultCommands(int Type,int Command,char *Dest)
{
  if (Type==0)
  {
    static const char *Commands[]={
    /*Extract               */"MsCab -i0 -FAR {-ap%%R} {-p%%P} {%%S} x %%A @%%LMA",
    /*Extract without paths */"MsCab -i0 -FAR {-p%%P} {%%S} e %%A @%%LMA",
    /*Test                  */"MsCab -i0 {-p%%P} {%%S} t %%A",
    /*Delete                */"MsCab -i0 -FAR {-p%%P} {%%S} d %%A @%%LMA",
    /*Comment archive       */"",
    /*Comment files         */"",
    /*Convert to SFX        */"MsCab {%%S} s %%A",
    /*Lock archive          */"",
    /*Protect archive       */"",
    /*Recover archive       */"",
    /*Add files             */"MsCab -i0 -dirs {-ap%%R} {-p%%P} {%%S} a %%A @%%LNMA",
    /*Move files            */"MsCab -i0 -dirs {-ap%%R} {-p%%P} {%%S} m %%A @%%LNMA",
    /*Add files and folders */"MsCab -r0 -i0 -dirs {-ap%%R} {-p%%P} {%%S} a %%A @%%LNMA",
    /*Move files and folders*/"MsCab -r0 -i0 -dirs {-ap%%R} {-p%%P} {%%S} m %%A @%%LNMA",
    /*"All files" mask      */"*"
    };
    if (Command < (int)(ARRAYSIZE(Commands)))
    {
      strcpy(Dest,Commands[Command]);
      return(TRUE);
    }
  }
  return(FALSE);
}