~ximion/listaller/master

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
(* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is TurboPower Abbrevia
 *
 * The Initial Developer of the Original Code is
 * TurboPower Software
 *
 * Portions created by the Initial Developer are Copyright (C) 1997-2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** *)

{*********************************************************}
{* ABBREVIA: AbFciFdi.pas 3.05                           *}
{*********************************************************}
{* ABBREVIA: Cabinet DLL wrapper                         *}
{* Based on info from the FCI/FDI Library Description,   *}
{* included in the Microsoft Cabinet SDK                 *}
{*********************************************************}

unit AbFciFdi;

{$mode delphi}

interface

uses
  typesarchive, //!! MVC for BOOL
  dynlibs, // loadlibrary
{$ifdef mswindows}
  Windows,
{$endif}  
  SysUtils, Classes;

const
  CabinetDLL  = 'cabinet.dll';
  cpuUnknown  = -1;
  cpu80286    = 0;
  cpu80386    = 1;
  cpuDefault  = cpuUnknown;


type
  {FDI errors}
  FDIError =
    (FDIError_None,            FDIError_Cabinet_Not_Found,
     FDIError_Not_A_Cabinet,   FDIError_Unknown_Cabinet_Version,
     FDIError_Corrupt_Cabinet, FDIError_Alloc_Fail,
     FDIError_Bad_Compr_Type,  FDIError_MDI_Fail,
     FDIError_Target_File,     FDIError_Reserve_Mismatch,
     FDIError_Wrong_Cabinet,   FDIError_User_Abort);

  {FCI errors}
  FCIError =
    (FCIError_NONE,      FCIError_Open_SRC,
     FCIError_Read_SRC,  FCIError_Alloc_Fail,
     FCIError_Temp_File, FCIError_Bad_Compr_Type,
     FCIError_Cab_File,  FCIError_User_Abort,
     FCIERRor_MCI_Fail);

  {FDI notifications}
  FDINotificationType =
    (FDINT_Cabinet_Info, FDINT_Partial_File,
     FDINT_Copy_File,    FDINT_Close_File_Info,
     FDINT_Next_Cabinet, FDINT_Enumerate);

  {FDI/FCI error structure}
  PCabErrorRecord = ^CabErrorRecord;
  CabErrorRecord  = record
    ErrorCode     : Integer;
    ErrorType     : Integer;
    ErrorPresent  : Boolean;
  end;

  {FDI cabinet information structure}
  PFDICabInfo = ^FDICabInfo;
  FDICabInfo  = record
    cbCabinet : Longint;
    cFolders  : Word;
    cFiles    : Word;
    setID     : Word;
    iCabinet  : Word;
    fReserve  : BOOL;
    hasprev   : BOOL;
    hasnext   : BOOL;
  end;

  {FCI cabinet information structure}
  PFCICabInfo = ^FCICabInfo;
  FCICabInfo  = record
    cb                    : Longint;
    cbFolderThresh        : Longint;
    cbReserveCFHeader     : Integer;
    cbReserveCFFolder     : Integer;
    cbReserveCFData       : Integer;
    iCab                  : Integer;
    iDisk                 : Integer;
    fFailOnIncompressible : Integer;
    setID                 : Word;
    szDisk                : array[0..255] of Char;
    szCab                 : array[0..255] of Char;
    szCabPath             : array[0..255] of Char;
  end;

  {FDI notification structure}
  PFDINotification = ^FDINotification;
  FDINotification  = record
    cb       : Longint;
    psz1     : PChar;
    psz2     : PChar;
    psz3     : PChar;
    pv       : Pointer;
    hf       : Integer;
    date     : Word;
    time     : Word;
    attribs  : Word;
    setID    : Word;
    iCabinet : Word;
    iFolder  : Word;
    fdie     : FDIERROR;
  end;

  {misc defines}
  HFDI    = Pointer;
  HFCI    = Pointer;
  FARPROC = Pointer;


{== Cabinet DLL routine prototypes ==========================================}
type
  TFDICreate =
    function (pfnalloc, pfnfree, pfnopen, pfnread, pfnwrite, pfnclose,
      pfnseek : FARPROC; cpuType  : Integer; pError : PCabErrorRecord) : HFDI;
      cdecl;
{----------------------------------------------------------------------------}
  TFDIIsCabinet =
    function(hfdi : HFDI; hf : Integer; pfdici : PFDICabInfo) : Boolean;
      cdecl;
{----------------------------------------------------------------------------}
  TFDICopy =
    function(hfdi : HFDI; pszCabinet, pszCabPath : PChar;
      flags : Integer; pfnfdin, pfnfdid : FARPROC; Archive : Pointer) : Boolean;
      cdecl;
{----------------------------------------------------------------------------}
  TFDIDestroy =
    function(hfdi : HFDI) : Boolean;
      cdecl;
{----------------------------------------------------------------------------}
  TFCICreate =
    function(pError : PCabErrorRecord; pfnfcifp, pfnalloc, pfnfree,
      pfnopen, pfnread, pfnwrite, pfnclose, pfnseek, pfndelete,
      pfnfcigtf : FARPROC; pccab : PFCICabInfo; Archive : Pointer) : HFCI;
      cdecl;
{----------------------------------------------------------------------------}
  TFCIAddFile =
    function(hfci : HFCI; pszFilePath, pszFileName : PChar;
      fExecute : Boolean; pfnfcignc, pfnfcis, pfnfcigoi : FARPROC;
      typeCompress : Word) : Boolean;
      cdecl;
{----------------------------------------------------------------------------}
  TFCIFlushCabinet =
    function(hfci : HFCI; fGetNextCab : Boolean;
      pfnfcignc, pfnfcis : FARPROC) : Boolean;
      cdecl;
{----------------------------------------------------------------------------}
  TFCIFlushFolder =
    function(hfci : HFCI; pfnfcignc, pfnfcis : FARPROC) : Boolean;
      cdecl;
{----------------------------------------------------------------------------}
  TFCIDestroy =
    function(hfci : HFCI) : Boolean;
      cdecl;


{== DLL routine wrappers ====================================================}
function FDICreate(pfnalloc, pfnfree, pfnopen, pfnread,
  pfnwrite, pfnclose, pfnseek : FARPROC;
  cpuType  : Integer; pError : PCabErrorRecord) : HFDI;
  {returns an FDI context for opening an existing cabinet}
  {  pfnalloc - heap allocation callback function }
  {  pfnfree  -  heap deallocation callback function }
  {  pfnopen  -  open file callback function }
  {  pfnwrite - write file callback function }
  {  pfnclose - close file callback function }
  {  pfnseek  - reposition file pointer callback function }
  {  cpuType  -  -1: unknown, 0: 80286, 1: 80386 }
  {  pError   -  pointer to error record }
{----------------------------------------------------------------------------}
function FDIIsCabinet(hfdi : HFDI; hf : Integer;
  pfdici : PFDICabInfo) : Boolean;
  {checks cabinet file for validity}
  {  hfdi   - FDI context }
  {  hf     - cabinet file handle }
  {  pfdici - pointer to FDI cabinet info structure }
{----------------------------------------------------------------------------}
function FDICopy(hfdi : HFDI; pszCabinet, pszCabPath : PChar;
  flags : Integer; pfnfdin, pfnfdid : FARPROC;
  Archive : Pointer) : Boolean;
  {enumerates every file in the cabinet.  The callback function}
  {should indicate whether or not to extract a given file}
  {  hfdi       - FDI context }
  {  pszCabinet - cabinet file name }
  {  pszCabPath - cabinet file path }
  {  flags      - currently not used }
  {  pfnfdin    - FDI notifaction callback function }
  {  pfnfdid    - decryption callback (currently not used)}
  {  Archive    - the calling TAbCabArchive instance }
{----------------------------------------------------------------------------}
function FDIDestroy(hfdi : HFDI) : Boolean;
  {releases FDI context and frees resources}
  {  hfdi - FDI context }
{----------------------------------------------------------------------------}
function FCICreate(pError : PCabErrorRecord;
  pfnfcifp, pfnalloc, pfnfree, pfnopen, pfnread, pfnwrite, pfnclose,
  pfnseek, pfndelete, pfnfcigtf : FARPROC;
  pccab : PFCICabInfo; Archive : Pointer) : HFCI;
  {creates a new cabinet file and returns the FCI context}
  {  pError    - pointer to error record }
  {  pfnfcifp  - callback notification when file has been placed in cabinet }
  {  pfnalloc  - callback function to allocate memory }
  {  pfnfree   - callback function to free memory }
  {  pfnopen   - callback function to open a file }
  {  pfnwrite  - callback function to write to a file }
  {  pfnclose  - callback function to close a file }
  {  pfnseek   - callback function to reposition file pointer }
  {  pfndelete - callback function to delete a file }
  {  pfnfcigtf - callback function to obtain temp filename }
  {  pccab     - pointer to FCI cabinet infor structure }
  {  Archive   - the calling TAbCabArchive instance }
{----------------------------------------------------------------------------}
function FCIAddFile(hfci : HFCI; pszFilePath, pszFileName : PChar;
  fExecute : Boolean; pfnfcignc, pfnfcis, pfnfcigoi : FARPROC;
  typeCompress : Word) : Boolean;
  {adds a file to the cabinet}
  {  hfci         - FCI context }
  {  pszFilePath  - full pathname of file being added }
  {  pszFileName  - just the file name }
  {  fExecute     - flag to indicate if file is executable }
  {  pfnfcignc    - callback function to obtain next cabinet info }
  {  pfnfcis      - callback function to relay progress }
  {  pfnfcigoi    - callback function to open file and get attributes }
  {  typeCompress - compression type to use }
{----------------------------------------------------------------------------}
function FCIFlushCabinet(hfci : HFCI; fGetNextCab : Boolean;
  pfnfcignc, pfnfcis : FARPROC) : Boolean;
  {writes current cabinet file out to disk and optionally starts a new one}
  {  hfci        - FCI context }
  {  fGetNextCab - flag indicating whether to start a new cabinet }
  {  pfnfcignc   - callback function to obtain next cabinet info }
  {  pfnfcis     - callback function to relay progress }
{----------------------------------------------------------------------------}
function FCIFlushFolder(hfci : HFCI;
  pfnfcignc, pfnfcis : FARPROC) : Boolean;
  {close current compression block and start a new one}
  {  hfci      - FCI context }
  {  pfnfcignc - callback function to obtain next cabinet info }
  {  pfnfcis   - callback function to relay progress }
{----------------------------------------------------------------------------}
function FCIDestroy(hfci : HFCI) : Boolean;
  {releases FCI context and frees resources}
  {  hfdi - FDI context }
{----------------------------------------------------------------------------}


implementation

uses
  AbExcept,
  AbConst;


var
  CabDLLLoaded        : Boolean;
  CabDLLHandle        : THandle;
  FDICreateProc       : TFDICreate;
  FDIIsCabinetProc    : TFDIIsCabinet;
  FDICopyProc         : TFDICopy;
  FDIDestroyProc      : TFDIDestroy;
  FCICreateProc       : TFCICreate;
  FCIAddFileProc      : TFCIAddFile;
  FCIFlushCabinetProc : TFCIFlushCabinet;
  FCIFlushFolderProc  : TFCIFlushFolder;
  FCIDestroyProc      : TFCIDestroy;


{============================================================================}
procedure LoadCabinetDLL;
begin
  if CabDllLoaded then
    Exit;
//  CabDllHandle := LoadLibrary(CabinetDLL);
  if (CabDllHandle = 0) then
    raise EAbNoCabinetDLL.Create;
{$ifdef MSWINDOWS}
  @FDICreateProc := GetProcAddress(CabDllHandle, 'FDICreate');
  @FDIIsCabinetProc := GetProcAddress(CabDllHandle, 'FDIIsCabinet');
  @FDICopyProc := GetProcAddress(CabDllHandle, 'FDICopy');
  @FDIDestroyProc := GetProcAddress(CabDllHandle, 'FDIDestroy');
  @FCICreateProc := GetProcAddress(CabDllHandle, 'FCICreate');
  @FCIAddFileProc := GetProcAddress(CabDllHandle, 'FCIAddFile');
  @FCIFlushCabinetProc := GetProcAddress(CabDllHandle, 'FCIFlushCabinet');
  @FCIFlushFolderProc := GetProcAddress(CabDllHandle, 'FCIFlushFolder');
  @FCIDestroyProc := GetProcAddress(CabDllHandle, 'FCIDestroy');
  CabDllLoaded := True;
{$endif}
end;
{----------------------------------------------------------------------------}
function FDICreate(pfnalloc, pfnfree, pfnopen, pfnread,
  pfnwrite, pfnclose, pfnseek : FARPROC;
  cpuType  : Integer; pError : PCabErrorRecord) : HFDI;
begin
  LoadCabinetDLL;
  if Assigned(FDICreateProc) then
    Result := FDICreateProc(pfnalloc, pfnfree, pfnopen, pfnread,
      pfnwrite, pfnclose, pfnseek, cpuType, pError)
  else
    Result := nil;
end;
{----------------------------------------------------------------------------}
function FDIIsCabinet(hfdi : HFDI; hf : Integer;
  pfdici : PFDICabInfo) : Boolean;
begin
  LoadCabinetDLL;
  if Assigned(FDIIsCabinetProc) then
    Result := FDIIsCabinetProc(hfdi, hf, pfdici)
  else
    Result := False;
end;
{----------------------------------------------------------------------------}
function FDICopy(hfdi : HFDI; pszCabinet, pszCabPath : PChar;
  flags : Integer; pfnfdin, pfnfdid : FARPROC;
  Archive : Pointer) : Boolean;
begin
  LoadCabinetDLL;
  if Assigned(FDICopyProc) then
    Result := FDICopyProc(hfdi, pszCabinet, pszCabPath, flags,
      pfnfdin, pfnfdid, Archive)
  else
    Result := False;
end;
{----------------------------------------------------------------------------}
function FDIDestroy(hfdi : HFDI) : Boolean;
begin
  LoadCabinetDLL;
  if Assigned(FDIDestroyProc) then
    Result := FDIDestroyProc(hfdi)
  else
    Result := False;
end;
{----------------------------------------------------------------------------}
function FCICreate(pError : PCabErrorRecord;
  pfnfcifp, pfnalloc, pfnfree, pfnopen, pfnread, pfnwrite, pfnclose,
  pfnseek, pfndelete, pfnfcigtf : FARPROC;
  pccab : PFCICabInfo; Archive : Pointer) : HFCI;
begin
  LoadCabinetDLL;
  if Assigned(FCICreateProc) then
    Result := FCICreateProc(pError, pfnfcifp, pfnalloc, pfnfree, pfnopen,
      pfnread, pfnwrite, pfnclose, pfnseek, pfndelete, pfnfcigtf,
      pccab, Archive)
  else
    Result := nil;
end;
{----------------------------------------------------------------------------}
function FCIAddFile(hfci : HFCI; pszFilePath, pszFileName : PChar;
  fExecute : Boolean; pfnfcignc, pfnfcis, pfnfcigoi : FARPROC;
  typeCompress : Word) : Boolean;
begin
  LoadCabinetDLL;
  if Assigned(FCIAddFileProc) then
    Result := FCIAddFileProc(hfci, pszFilePath, pszFileName,
      fExecute, pfnfcignc, pfnfcis, pfnfcigoi, typeCompress)
  else
    Result := False;
end;
{----------------------------------------------------------------------------}
function FCIFlushCabinet(hfci : HFCI; fGetNextCab : Boolean;
  pfnfcignc, pfnfcis : FARPROC) : Boolean;
begin
  LoadCabinetDLL;
  if Assigned(FCIFlushCabinetProc) then
    Result := FCIFlushCabinetProc(hfci, fGetNextCab, pfnfcignc, pfnfcis)
  else
    Result := False;
end;
{----------------------------------------------------------------------------}
function FCIFlushFolder(hfci : HFCI;
  pfnfcignc, pfnfcis : FARPROC) : Boolean;
begin
  LoadCabinetDLL;
  if Assigned(FCIFlushFolderProc) then
    Result := FCIFlushFolderProc(hfci, pfnfcignc, pfnfcis)
  else
    Result := False;
end;
{----------------------------------------------------------------------------}
function FCIDestroy(hfci : HFCI) : Boolean;
begin
  LoadCabinetDLL;
  if Assigned(FCIDestroyProc) then
    Result := FCIDestroyProc(hfci)
  else
    Result := False;
end;
{----------------------------------------------------------------------------}
initialization
  CabDllLoaded := False;

end.