2 * File Compression Interface
4 * Copyright 2002 Patrik Stridvall
5 * Copyright 2005 Gerold Jens Wucherpfennig
6 * Copyright 2011 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 There is still some work to be done:
27 - unknown behaviour if files>=2GB or cabinet >=4GB
28 - check if the maximum size for a cabinet is too small to store any data
29 - call pfnfcignc on exactly the same position as MS FCIAddFile in every case
42 #include <wine/list.h>
44 #ifdef WORDS_BIGENDIAN
45 #define fci_endian_ulong(x) RtlUlongByteSwap(x)
46 #define fci_endian_uword(x) RtlUshortByteSwap(x)
48 #define fci_endian_ulong(x) (x)
49 #define fci_endian_uword(x) (x)
54 cab_UBYTE signature
[4]; /* !CAB for unfinished cabinets else MSCF */
56 cab_ULONG cbCabinet
; /* size of the cabinet file in bytes*/
58 cab_ULONG coffFiles
; /* offset to first CFFILE section */
60 cab_UBYTE versionMinor
; /* 3 */
61 cab_UBYTE versionMajor
; /* 1 */
62 cab_UWORD cFolders
; /* number of CFFOLDER entries in the cabinet*/
63 cab_UWORD cFiles
; /* number of CFFILE entries in the cabinet*/
64 cab_UWORD flags
; /* 1=prev cab, 2=next cabinet, 4=reserved sections*/
65 cab_UWORD setID
; /* identification number of all cabinets in a set*/
66 cab_UWORD iCabinet
; /* number of the cabinet in a set */
67 /* additional area if "flags" were set*/
68 } CFHEADER
; /* minimum 36 bytes */
71 cab_ULONG coffCabStart
; /* offset to the folder's first CFDATA section */
72 cab_UWORD cCFData
; /* number of this folder's CFDATA sections */
73 cab_UWORD typeCompress
; /* compression type of data in CFDATA section*/
74 /* additional area if reserve flag was set */
75 } CFFOLDER
; /* minimum 8 bytes */
78 cab_ULONG cbFile
; /* size of the uncompressed file in bytes */
79 cab_ULONG uoffFolderStart
; /* offset of the uncompressed file in the folder */
80 cab_UWORD iFolder
; /* number of folder in the cabinet 0=first */
81 /* for special values see below this structure*/
82 cab_UWORD date
; /* last modification date*/
83 cab_UWORD time
; /* last modification time*/
84 cab_UWORD attribs
; /* DOS fat attributes and UTF indicator */
85 /* ... and a C string with the name of the file */
86 } CFFILE
; /* 16 bytes + name of file */
90 cab_ULONG csum
; /* checksum of this entry*/
91 cab_UWORD cbData
; /* number of compressed bytes */
92 cab_UWORD cbUncomp
; /* number of bytes when data is uncompressed */
93 /* optional reserved area */
100 char name
[CB_MAX_FILENAME
];
106 struct list files_list
;
107 struct list blocks_list
;
108 struct temp_file data
;
109 cab_ULONG data_start
;
110 cab_UWORD data_count
;
117 cab_ULONG size
; /* uncompressed size */
118 cab_ULONG offset
; /* offset in folder */
119 cab_UWORD folder
; /* index of folder */
129 cab_UWORD compressed
;
130 cab_UWORD uncompressed
;
133 typedef struct FCI_Int
137 PFNFCIFILEPLACED fileplaced
;
146 PFNFCIGETTEMPFILE gettemp
;
152 cab_ULONG statusFolderCopied
;
153 cab_ULONG statusFolderTotal
;
154 BOOL fGetNextCabInVain
;
156 char szPrevCab
[CB_MAX_CABINET_NAME
]; /* previous cabinet name */
157 char szPrevDisk
[CB_MAX_DISK_NAME
]; /* disk name of previous cabinet */
158 unsigned char data_in
[CAB_BLOCKMAX
]; /* uncompressed data blocks */
159 unsigned char data_out
[2 * CAB_BLOCKMAX
]; /* compressed data blocks */
161 ULONG cCompressedBytesInFolder
;
164 cab_ULONG cDataBlocks
;
165 cab_ULONG cbFileRemainer
; /* uncompressed, yet to be written data */
166 /* of spanned file of a spanning folder of a spanning cabinet */
167 struct temp_file data
;
169 cab_ULONG estimatedCabinetSize
;
170 struct list folders_list
;
171 struct list files_list
;
172 struct list blocks_list
;
173 cab_ULONG folders_size
;
174 cab_ULONG files_size
; /* size of files not yet assigned to a folder */
175 cab_ULONG placed_files_size
; /* size of files already placed into a folder */
176 cab_ULONG pending_data_size
; /* size of data not yet assigned to a folder */
177 cab_ULONG folders_data_size
; /* total size of data contained in the current folders */
179 cab_UWORD (*compress
)(struct FCI_Int
*);
182 #define FCI_INT_MAGIC 0xfcfcfc05
184 static void set_error( FCI_Int
*fci
, int oper
, int err
)
186 fci
->perf
->erfOper
= oper
;
187 fci
->perf
->erfType
= err
;
188 fci
->perf
->fError
= TRUE
;
189 if (err
) SetLastError( err
);
192 static FCI_Int
*get_fci_ptr( HFCI hfci
)
194 FCI_Int
*fci
= (FCI_Int
*)hfci
;
196 if (!fci
|| fci
->magic
!= FCI_INT_MAGIC
)
198 SetLastError( ERROR_INVALID_HANDLE
);
204 /* compute the cabinet header size */
205 static cab_ULONG
get_header_size( FCI_Int
*fci
)
207 cab_ULONG ret
= sizeof(CFHEADER
) + fci
->ccab
.cbReserveCFHeader
;
209 if (fci
->ccab
.cbReserveCFHeader
|| fci
->ccab
.cbReserveCFFolder
|| fci
->ccab
.cbReserveCFData
)
213 ret
+= strlen( fci
->szPrevCab
) + 1 + strlen( fci
->szPrevDisk
) + 1;
216 ret
+= strlen( fci
->pccab
->szCab
) + 1 + strlen( fci
->pccab
->szDisk
) + 1;
221 static BOOL
create_temp_file( FCI_Int
*fci
, struct temp_file
*file
)
225 if (!fci
->gettemp( file
->name
, CB_MAX_FILENAME
, fci
->pv
))
227 set_error( fci
, FCIERR_TEMP_FILE
, ERROR_FUNCTION_FAILED
);
230 if ((file
->handle
= fci
->open( file
->name
, _O_RDWR
| _O_CREAT
| _O_EXCL
| _O_BINARY
,
231 _S_IREAD
| _S_IWRITE
, &err
, fci
->pv
)) == -1)
233 set_error( fci
, FCIERR_TEMP_FILE
, err
);
239 static BOOL
close_temp_file( FCI_Int
*fci
, struct temp_file
*file
)
243 if (file
->handle
== -1) return TRUE
;
244 if (fci
->close( file
->handle
, &err
, fci
->pv
) == -1)
246 set_error( fci
, FCIERR_TEMP_FILE
, err
);
250 if (fci
->delete( file
->name
, &err
, fci
->pv
) == -1)
252 set_error( fci
, FCIERR_TEMP_FILE
, err
);
258 static struct file
*add_file( FCI_Int
*fci
, const char *filename
)
260 unsigned int size
= FIELD_OFFSET( struct file
, name
[strlen(filename
) + 1] );
261 struct file
*file
= fci
->alloc( size
);
265 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
269 file
->offset
= fci
->cDataBlocks
* CAB_BLOCKMAX
+ fci
->cdata_in
;
270 file
->folder
= fci
->cFolders
;
274 strcpy( file
->name
, filename
);
275 list_add_tail( &fci
->files_list
, &file
->entry
);
276 fci
->files_size
+= sizeof(CFFILE
) + strlen(filename
) + 1;
280 static struct file
*copy_file( FCI_Int
*fci
, const struct file
*orig
)
282 unsigned int size
= FIELD_OFFSET( struct file
, name
[strlen(orig
->name
) + 1] );
283 struct file
*file
= fci
->alloc( size
);
287 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
290 memcpy( file
, orig
, size
);
294 static void free_file( FCI_Int
*fci
, struct file
*file
)
296 list_remove( &file
->entry
);
300 /* create a new data block for the data in fci->data_in */
301 static BOOL
add_data_block( FCI_Int
*fci
, PFNFCISTATUS status_callback
)
304 struct data_block
*block
;
306 if (!fci
->cdata_in
) return TRUE
;
308 if (fci
->data
.handle
== -1 && !create_temp_file( fci
, &fci
->data
)) return FALSE
;
310 if (!(block
= fci
->alloc( sizeof(*block
) )))
312 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
315 block
->uncompressed
= fci
->cdata_in
;
316 block
->compressed
= fci
->compress( fci
);
318 if (fci
->write( fci
->data
.handle
, fci
->data_out
,
319 block
->compressed
, &err
, fci
->pv
) != block
->compressed
)
321 set_error( fci
, FCIERR_TEMP_FILE
, err
);
327 fci
->pending_data_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ block
->compressed
;
328 fci
->cCompressedBytesInFolder
+= block
->compressed
;
330 list_add_tail( &fci
->blocks_list
, &block
->entry
);
332 if (status_callback( statusFile
, block
->compressed
, block
->uncompressed
, fci
->pv
) == -1)
334 set_error( fci
, FCIERR_USER_ABORT
, 0 );
340 /* add compressed blocks for all the data that can be read from the file */
341 static BOOL
add_file_data( FCI_Int
*fci
, char *sourcefile
, char *filename
, BOOL execute
,
342 PFNFCIGETOPENINFO get_open_info
, PFNFCISTATUS status_callback
)
348 if (!(file
= add_file( fci
, filename
))) return FALSE
;
350 handle
= get_open_info( sourcefile
, &file
->date
, &file
->time
, &file
->attribs
, &err
, fci
->pv
);
353 free_file( fci
, file
);
354 set_error( fci
, FCIERR_OPEN_SRC
, err
);
357 if (execute
) file
->attribs
|= _A_EXEC
;
361 len
= fci
->read( handle
, fci
->data_in
+ fci
->cdata_in
,
362 CAB_BLOCKMAX
- fci
->cdata_in
, &err
, fci
->pv
);
367 set_error( fci
, FCIERR_READ_SRC
, err
);
371 fci
->cdata_in
+= len
;
372 if (fci
->cdata_in
== CAB_BLOCKMAX
&& !add_data_block( fci
, status_callback
)) return FALSE
;
374 fci
->close( handle
, &err
, fci
->pv
);
378 static void free_data_block( FCI_Int
*fci
, struct data_block
*block
)
380 list_remove( &block
->entry
);
384 static struct folder
*add_folder( FCI_Int
*fci
)
386 struct folder
*folder
= fci
->alloc( sizeof(*folder
) );
390 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
393 folder
->data
.handle
= -1;
394 folder
->data_start
= fci
->folders_data_size
;
395 folder
->data_count
= 0;
396 folder
->compression
= fci
->compression
;
397 list_init( &folder
->files_list
);
398 list_init( &folder
->blocks_list
);
399 list_add_tail( &fci
->folders_list
, &folder
->entry
);
400 fci
->folders_size
+= sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
;
405 static void free_folder( FCI_Int
*fci
, struct folder
*folder
)
407 struct file
*file
, *file_next
;
408 struct data_block
*block
, *block_next
;
410 LIST_FOR_EACH_ENTRY_SAFE( file
, file_next
, &folder
->files_list
, struct file
, entry
)
411 free_file( fci
, file
);
412 LIST_FOR_EACH_ENTRY_SAFE( block
, block_next
, &folder
->blocks_list
, struct data_block
, entry
)
413 free_data_block( fci
, block
);
414 close_temp_file( fci
, &folder
->data
);
415 list_remove( &folder
->entry
);
419 /* reset state for the next cabinet file once the current one has been flushed */
420 static void reset_cabinet( FCI_Int
*fci
)
422 struct folder
*folder
, *folder_next
;
424 LIST_FOR_EACH_ENTRY_SAFE( folder
, folder_next
, &fci
->folders_list
, struct folder
, entry
)
425 free_folder( fci
, folder
);
429 fci
->folders_size
= 0;
430 fci
->placed_files_size
= 0;
431 fci
->folders_data_size
= 0;
434 static cab_ULONG
fci_get_checksum( const void *pv
, UINT cb
, cab_ULONG seed
)
445 while (cUlong
-- > 0) {
447 ul
|= (((cab_ULONG
)(*pb
++)) << 8);
448 ul
|= (((cab_ULONG
)(*pb
++)) << 16);
449 ul
|= (((cab_ULONG
)(*pb
++)) << 24);
456 ul
|= (((ULONG
)(*pb
++)) << 16);
459 ul
|= (((ULONG
)(*pb
++)) << 8);
472 /* copy all remaining data block to a new temp file */
473 static BOOL
copy_data_blocks( FCI_Int
*fci
, INT_PTR handle
, cab_ULONG start_pos
,
474 struct temp_file
*temp
, PFNFCISTATUS status_callback
)
476 struct data_block
*block
;
479 if (fci
->seek( handle
, start_pos
, SEEK_SET
, &err
, fci
->pv
) != start_pos
)
481 set_error( fci
, FCIERR_TEMP_FILE
, err
);
484 if (!create_temp_file( fci
, temp
)) return FALSE
;
486 LIST_FOR_EACH_ENTRY( block
, &fci
->blocks_list
, struct data_block
, entry
)
488 if (fci
->read( handle
, fci
->data_out
, block
->compressed
,
489 &err
, fci
->pv
) != block
->compressed
)
491 close_temp_file( fci
, temp
);
492 set_error( fci
, FCIERR_TEMP_FILE
, err
);
495 if (fci
->write( temp
->handle
, fci
->data_out
, block
->compressed
,
496 &err
, fci
->pv
) != block
->compressed
)
498 close_temp_file( fci
, temp
);
499 set_error( fci
, FCIERR_TEMP_FILE
, err
);
502 fci
->pending_data_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ block
->compressed
;
503 fci
->statusFolderCopied
+= block
->compressed
;
505 if (status_callback( statusFolder
, fci
->statusFolderCopied
,
506 fci
->statusFolderTotal
, fci
->pv
) == -1)
508 close_temp_file( fci
, temp
);
509 set_error( fci
, FCIERR_USER_ABORT
, 0 );
516 /* write all folders to disk and remove them from the list */
517 static BOOL
write_folders( FCI_Int
*fci
, INT_PTR handle
, cab_ULONG header_size
, PFNFCISTATUS status_callback
)
519 struct folder
*folder
;
521 CFFOLDER
*cffolder
= (CFFOLDER
*)fci
->data_out
;
522 cab_ULONG folder_size
= sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
;
524 memset( cffolder
, 0, folder_size
);
526 /* write the folders */
527 LIST_FOR_EACH_ENTRY( folder
, &fci
->folders_list
, struct folder
, entry
)
529 cffolder
->coffCabStart
= fci_endian_ulong( folder
->data_start
+ header_size
);
530 cffolder
->cCFData
= fci_endian_uword( folder
->data_count
);
531 cffolder
->typeCompress
= fci_endian_uword( folder
->compression
);
532 if (fci
->write( handle
, cffolder
, folder_size
, &err
, fci
->pv
) != folder_size
)
534 set_error( fci
, FCIERR_CAB_FILE
, err
);
541 /* write all the files to the cabinet file */
542 static BOOL
write_files( FCI_Int
*fci
, INT_PTR handle
, PFNFCISTATUS status_callback
)
545 struct folder
*folder
;
548 CFFILE
*cffile
= (CFFILE
*)fci
->data_out
;
550 LIST_FOR_EACH_ENTRY( folder
, &fci
->folders_list
, struct folder
, entry
)
552 LIST_FOR_EACH_ENTRY( file
, &folder
->files_list
, struct file
, entry
)
554 cffile
->cbFile
= fci_endian_ulong( file
->size
);
555 cffile
->uoffFolderStart
= fci_endian_ulong( file
->offset
);
556 cffile
->iFolder
= fci_endian_uword( file
->folder
);
557 cffile
->date
= fci_endian_uword( file
->date
);
558 cffile
->time
= fci_endian_uword( file
->time
);
559 cffile
->attribs
= fci_endian_uword( file
->attribs
);
560 lstrcpynA( (char *)(cffile
+ 1), file
->name
, CB_MAX_FILENAME
);
561 file_size
= sizeof(CFFILE
) + strlen( (char *)(cffile
+ 1) ) + 1;
562 if (fci
->write( handle
, cffile
, file_size
, &err
, fci
->pv
) != file_size
)
564 set_error( fci
, FCIERR_CAB_FILE
, err
);
567 if (!fci
->fSplitFolder
)
569 fci
->statusFolderCopied
= 0;
570 /* TODO TEST THIS further */
571 fci
->statusFolderTotal
= fci
->folders_data_size
+ fci
->placed_files_size
;
573 fci
->statusFolderCopied
+= file_size
;
574 /* report status about copied size of folder */
575 if (status_callback( statusFolder
, fci
->statusFolderCopied
,
576 fci
->statusFolderTotal
, fci
->pv
) == -1)
578 set_error( fci
, FCIERR_USER_ABORT
, 0 );
586 /* write all data blocks to the cabinet file */
587 static BOOL
write_data_blocks( FCI_Int
*fci
, INT_PTR handle
, PFNFCISTATUS status_callback
)
589 struct folder
*folder
;
590 struct data_block
*block
;
594 cab_UWORD header_size
;
596 header_size
= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
;
597 cfdata
= (CFDATA
*)fci
->data_out
;
598 memset( cfdata
, 0, header_size
);
599 data
= (char *)cfdata
+ header_size
;
601 LIST_FOR_EACH_ENTRY( folder
, &fci
->folders_list
, struct folder
, entry
)
603 if (fci
->seek( folder
->data
.handle
, 0, SEEK_SET
, &err
, fci
->pv
) != 0)
605 set_error( fci
, FCIERR_CAB_FILE
, err
);
608 LIST_FOR_EACH_ENTRY( block
, &folder
->blocks_list
, struct data_block
, entry
)
610 len
= fci
->read( folder
->data
.handle
, data
, block
->compressed
, &err
, fci
->pv
);
611 if (len
!= block
->compressed
) return FALSE
;
613 cfdata
->cbData
= fci_endian_uword( block
->compressed
);
614 cfdata
->cbUncomp
= fci_endian_uword( block
->uncompressed
);
615 cfdata
->csum
= fci_endian_ulong( fci_get_checksum( &cfdata
->cbData
,
616 header_size
- FIELD_OFFSET(CFDATA
, cbData
),
617 fci_get_checksum( data
, len
, 0 )));
619 fci
->statusFolderCopied
+= len
;
621 if (fci
->write( handle
, fci
->data_out
, len
, &err
, fci
->pv
) != len
)
623 set_error( fci
, FCIERR_CAB_FILE
, err
);
626 if (status_callback( statusFolder
, fci
->statusFolderCopied
, fci
->statusFolderTotal
, fci
->pv
) == -1)
628 set_error( fci
, FCIERR_USER_ABORT
, 0 );
636 /* write the cabinet file to disk */
637 static BOOL
write_cabinet( FCI_Int
*fci
, PFNFCISTATUS status_callback
)
639 char filename
[CB_MAX_CAB_PATH
+ CB_MAX_CABINET_NAME
];
643 CFHEADER
*cfheader
= (CFHEADER
*)fci
->data_out
;
645 cab_ULONG header_size
= get_header_size( fci
);
646 cab_ULONG total_size
= header_size
+ fci
->folders_size
+
647 fci
->placed_files_size
+ fci
->folders_data_size
;
649 assert( header_size
<= sizeof(fci
->data_out
) );
650 memset( cfheader
, 0, header_size
);
652 if (fci
->fPrevCab
) flags
|= cfheadPREV_CABINET
;
653 if (fci
->fNextCab
) flags
|= cfheadNEXT_CABINET
;
654 if (fci
->ccab
.cbReserveCFHeader
|| fci
->ccab
.cbReserveCFFolder
|| fci
->ccab
.cbReserveCFData
)
655 flags
|= cfheadRESERVE_PRESENT
;
657 memcpy( cfheader
->signature
, "!CAB", 4 );
658 cfheader
->cbCabinet
= fci_endian_ulong( total_size
);
659 cfheader
->coffFiles
= fci_endian_ulong( header_size
+ fci
->folders_size
);
660 cfheader
->versionMinor
= 3;
661 cfheader
->versionMajor
= 1;
662 cfheader
->cFolders
= fci_endian_uword( fci
->cFolders
);
663 cfheader
->cFiles
= fci_endian_uword( fci
->cFiles
);
664 cfheader
->flags
= fci_endian_uword( flags
);
665 cfheader
->setID
= fci_endian_uword( fci
->ccab
.setID
);
666 cfheader
->iCabinet
= fci_endian_uword( fci
->ccab
.iCab
);
667 ptr
= (char *)(cfheader
+ 1);
669 if (flags
& cfheadRESERVE_PRESENT
)
673 cab_UWORD cbCFHeader
;
674 cab_UBYTE cbCFFolder
;
676 } *reserve
= (void *)ptr
;
678 reserve
->cbCFHeader
= fci_endian_uword( fci
->ccab
.cbReserveCFHeader
);
679 reserve
->cbCFFolder
= fci
->ccab
.cbReserveCFFolder
;
680 reserve
->cbCFData
= fci
->ccab
.cbReserveCFData
;
681 ptr
= (char *)(reserve
+ 1);
683 ptr
+= fci
->ccab
.cbReserveCFHeader
;
685 if (flags
& cfheadPREV_CABINET
)
687 strcpy( ptr
, fci
->szPrevCab
);
688 ptr
+= strlen( ptr
) + 1;
689 strcpy( ptr
, fci
->szPrevDisk
);
690 ptr
+= strlen( ptr
) + 1;
693 if (flags
& cfheadNEXT_CABINET
)
695 strcpy( ptr
, fci
->pccab
->szCab
);
696 ptr
+= strlen( ptr
) + 1;
697 strcpy( ptr
, fci
->pccab
->szDisk
);
698 ptr
+= strlen( ptr
) + 1;
701 assert( ptr
- (char *)cfheader
== header_size
);
703 strcpy( filename
, fci
->ccab
.szCabPath
);
704 strcat( filename
, fci
->ccab
.szCab
);
706 if ((handle
= fci
->open( filename
, _O_RDWR
| _O_CREAT
| _O_TRUNC
| _O_BINARY
,
707 _S_IREAD
| _S_IWRITE
, &err
, fci
->pv
)) == -1)
709 set_error( fci
, FCIERR_CAB_FILE
, err
);
713 if (fci
->write( handle
, cfheader
, header_size
, &err
, fci
->pv
) != header_size
)
715 set_error( fci
, FCIERR_CAB_FILE
, err
);
719 /* add size of header size of all CFFOLDERs and size of all CFFILEs */
720 header_size
+= fci
->placed_files_size
+ fci
->folders_size
;
721 if (!write_folders( fci
, handle
, header_size
, status_callback
)) goto failed
;
722 if (!write_files( fci
, handle
, status_callback
)) goto failed
;
723 if (!write_data_blocks( fci
, handle
, status_callback
)) goto failed
;
725 /* update the signature */
726 if (fci
->seek( handle
, 0, SEEK_SET
, &err
, fci
->pv
) != 0 )
728 set_error( fci
, FCIERR_CAB_FILE
, err
);
731 memcpy( cfheader
->signature
, "MSCF", 4 );
732 if (fci
->write( handle
, cfheader
->signature
, 4, &err
, fci
->pv
) != 4)
734 set_error( fci
, FCIERR_CAB_FILE
, err
);
737 fci
->close( handle
, &err
, fci
->pv
);
739 reset_cabinet( fci
);
740 status_callback( statusCabinet
, fci
->estimatedCabinetSize
, total_size
, fci
->pv
);
744 fci
->close( handle
, &err
, fci
->pv
);
745 fci
->delete( filename
, &err
, fci
->pv
);
749 /* add all pending data blocks folder */
750 static BOOL
add_data_to_folder( FCI_Int
*fci
, struct folder
*folder
, cab_ULONG
*payload
,
751 PFNFCISTATUS status_callback
)
753 struct data_block
*block
, *new, *next
;
754 BOOL split_block
= FALSE
;
755 cab_ULONG current_size
, start_pos
= 0;
758 current_size
= get_header_size( fci
) + fci
->folders_size
+
759 fci
->files_size
+ fci
->placed_files_size
+ fci
->folders_data_size
;
761 /* move the temp file into the folder structure */
762 folder
->data
= fci
->data
;
763 fci
->data
.handle
= -1;
764 fci
->pending_data_size
= 0;
766 LIST_FOR_EACH_ENTRY_SAFE( block
, next
, &fci
->blocks_list
, struct data_block
, entry
)
768 /* No more CFDATA fits into the cabinet under construction */
769 /* So don't try to store more data into it */
770 if (fci
->fNextCab
&& (fci
->ccab
.cb
<= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+
771 current_size
+ sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
))
774 if (!(new = fci
->alloc( sizeof(*new) )))
776 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
779 /* Is cabinet with new CFDATA too large? Then data block has to be split */
781 (fci
->ccab
.cb
< sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+
782 block
->compressed
+ current_size
+ sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
))
784 /* Modify the size of the compressed data to store only a part of the */
785 /* data block into the current cabinet. This is done to prevent */
786 /* that the maximum cabinet size will be exceeded. The remainder */
787 /* will be stored into the next following cabinet. */
789 new->compressed
= fci
->ccab
.cb
- (sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ current_size
+
790 sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
);
791 new->uncompressed
= 0; /* on split blocks of data this is zero */
792 block
->compressed
-= new->compressed
;
797 new->compressed
= block
->compressed
;
798 new->uncompressed
= block
->uncompressed
;
801 start_pos
+= new->compressed
;
802 current_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ new->compressed
;
803 fci
->folders_data_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ new->compressed
;
804 fci
->statusFolderCopied
+= new->compressed
;
805 (*payload
) += new->uncompressed
;
807 list_add_tail( &folder
->blocks_list
, &new->entry
);
808 folder
->data_count
++;
810 /* report status with pfnfcis about copied size of folder */
811 if (status_callback( statusFolder
, fci
->statusFolderCopied
,
812 fci
->statusFolderTotal
, fci
->pv
) == -1)
814 set_error( fci
, FCIERR_USER_ABORT
, 0 );
817 if (split_block
) break;
818 free_data_block( fci
, block
);
822 if (list_empty( &fci
->blocks_list
)) return TRUE
;
823 return copy_data_blocks( fci
, folder
->data
.handle
, start_pos
, &fci
->data
, status_callback
);
826 /* add all pending files to folder */
827 static BOOL
add_files_to_folder( FCI_Int
*fci
, struct folder
*folder
, cab_ULONG payload
)
829 cab_ULONG sizeOfFiles
= 0, sizeOfFilesPrev
;
830 cab_ULONG cbFileRemainer
= 0;
831 struct file
*file
, *next
;
833 LIST_FOR_EACH_ENTRY_SAFE( file
, next
, &fci
->files_list
, struct file
, entry
)
835 cab_ULONG size
= sizeof(CFFILE
) + strlen(file
->name
) + 1;
837 /* fnfilfnfildest: placed file on cabinet */
838 fci
->fileplaced( &fci
->ccab
, file
->name
, file
->size
,
839 (file
->folder
== cffileCONTINUED_FROM_PREV
), fci
->pv
);
841 sizeOfFilesPrev
= sizeOfFiles
;
842 /* set complete size of all processed files */
843 if (file
->folder
== cffileCONTINUED_FROM_PREV
&& fci
->cbFileRemainer
!= 0)
845 sizeOfFiles
+= fci
->cbFileRemainer
;
846 fci
->cbFileRemainer
= 0;
848 else sizeOfFiles
+= file
->size
;
850 /* check if spanned file fits into this cabinet folder */
851 if (sizeOfFiles
> payload
)
853 if (file
->folder
== cffileCONTINUED_FROM_PREV
)
854 file
->folder
= cffileCONTINUED_PREV_AND_NEXT
;
856 file
->folder
= cffileCONTINUED_TO_NEXT
;
859 list_remove( &file
->entry
);
860 list_add_tail( &folder
->files_list
, &file
->entry
);
861 fci
->placed_files_size
+= size
;
864 /* This is only true for files which will be written into the */
865 /* next cabinet of the spanning folder */
866 if (sizeOfFiles
> payload
)
868 /* add a copy back onto the list */
869 if (!(file
= copy_file( fci
, file
))) return FALSE
;
870 list_add_before( &next
->entry
, &file
->entry
);
872 /* Files which data will be partially written into the current cabinet */
873 if (file
->folder
== cffileCONTINUED_PREV_AND_NEXT
|| file
->folder
== cffileCONTINUED_TO_NEXT
)
875 if (sizeOfFilesPrev
<= payload
)
877 /* The size of the uncompressed, data of a spanning file in a */
879 cbFileRemainer
= sizeOfFiles
- payload
;
881 file
->folder
= cffileCONTINUED_FROM_PREV
;
883 else file
->folder
= 0;
887 fci
->files_size
-= size
;
890 fci
->cbFileRemainer
= cbFileRemainer
;
894 static cab_UWORD
compress_NONE( FCI_Int
*fci
)
896 memcpy( fci
->data_out
, fci
->data_in
, fci
->cdata_in
);
897 return fci
->cdata_in
;
902 static void *zalloc( void *opaque
, unsigned int items
, unsigned int size
)
904 FCI_Int
*fci
= opaque
;
905 return fci
->alloc( items
* size
);
908 static void zfree( void *opaque
, void *ptr
)
910 FCI_Int
*fci
= opaque
;
914 static cab_UWORD
compress_MSZIP( FCI_Int
*fci
)
918 stream
.zalloc
= zalloc
;
919 stream
.zfree
= zfree
;
921 if (deflateInit2( &stream
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, -15, 8, Z_DEFAULT_STRATEGY
) != Z_OK
)
923 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
926 stream
.next_in
= fci
->data_in
;
927 stream
.avail_in
= fci
->cdata_in
;
928 stream
.next_out
= fci
->data_out
+ 2;
929 stream
.avail_out
= sizeof(fci
->data_out
) - 2;
930 /* insert the signature */
931 fci
->data_out
[0] = 'C';
932 fci
->data_out
[1] = 'K';
933 deflate( &stream
, Z_FINISH
);
934 deflateEnd( &stream
);
935 return stream
.total_out
+ 2;
938 #endif /* HAVE_ZLIB */
941 /***********************************************************************
942 * FCICreate (CABINET.10)
944 * FCICreate is provided with several callbacks and
945 * returns a handle which can be used to create cabinet files.
948 * perf [IO] A pointer to an ERF structure. When FCICreate
949 * returns an error condition, error information may
950 * be found here as well as from GetLastError.
951 * pfnfiledest [I] A pointer to a function which is called when a file
952 * is placed. Only useful for subsequent cabinet files.
953 * pfnalloc [I] A pointer to a function which allocates ram. Uses
954 * the same interface as malloc.
955 * pfnfree [I] A pointer to a function which frees ram. Uses the
956 * same interface as free.
957 * pfnopen [I] A pointer to a function which opens a file. Uses
958 * the same interface as _open.
959 * pfnread [I] A pointer to a function which reads from a file into
960 * a caller-provided buffer. Uses the same interface
962 * pfnwrite [I] A pointer to a function which writes to a file from
963 * a caller-provided buffer. Uses the same interface
965 * pfnclose [I] A pointer to a function which closes a file handle.
966 * Uses the same interface as _close.
967 * pfnseek [I] A pointer to a function which seeks in a file.
968 * Uses the same interface as _lseek.
969 * pfndelete [I] A pointer to a function which deletes a file.
970 * pfnfcigtf [I] A pointer to a function which gets the name of a
972 * pccab [I] A pointer to an initialized CCAB structure.
973 * pv [I] A pointer to an application-defined notification
974 * function which will be passed to other FCI functions
978 * On success, returns an FCI handle of type HFCI.
979 * On failure, the NULL file handle is returned. Error
980 * info can be retrieved from perf.
986 HFCI __cdecl
FCICreate(
988 PFNFCIFILEPLACED pfnfiledest
,
989 PFNFCIALLOC pfnalloc
,
993 PFNFCIWRITE pfnwrite
,
994 PFNFCICLOSE pfnclose
,
996 PFNFCIDELETE pfndelete
,
997 PFNFCIGETTEMPFILE pfnfcigtf
,
1001 FCI_Int
*p_fci_internal
;
1004 SetLastError(ERROR_BAD_ARGUMENTS
);
1007 if ((!pfnalloc
) || (!pfnfree
) || (!pfnopen
) || (!pfnread
) ||
1008 (!pfnwrite
) || (!pfnclose
) || (!pfnseek
) || (!pfndelete
) ||
1009 (!pfnfcigtf
) || (!pccab
)) {
1010 perf
->erfOper
= FCIERR_NONE
;
1011 perf
->erfType
= ERROR_BAD_ARGUMENTS
;
1012 perf
->fError
= TRUE
;
1014 SetLastError(ERROR_BAD_ARGUMENTS
);
1018 if (!((p_fci_internal
= pfnalloc(sizeof(FCI_Int
))))) {
1019 perf
->erfOper
= FCIERR_ALLOC_FAIL
;
1020 perf
->erfType
= ERROR_NOT_ENOUGH_MEMORY
;
1021 perf
->fError
= TRUE
;
1023 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1027 p_fci_internal
->magic
= FCI_INT_MAGIC
;
1028 p_fci_internal
->perf
= perf
;
1029 p_fci_internal
->fileplaced
= pfnfiledest
;
1030 p_fci_internal
->alloc
= pfnalloc
;
1031 p_fci_internal
->free
= pfnfree
;
1032 p_fci_internal
->open
= pfnopen
;
1033 p_fci_internal
->read
= pfnread
;
1034 p_fci_internal
->write
= pfnwrite
;
1035 p_fci_internal
->close
= pfnclose
;
1036 p_fci_internal
->seek
= pfnseek
;
1037 p_fci_internal
->delete = pfndelete
;
1038 p_fci_internal
->gettemp
= pfnfcigtf
;
1039 p_fci_internal
->ccab
= *pccab
;
1040 p_fci_internal
->pccab
= pccab
;
1041 p_fci_internal
->fPrevCab
= FALSE
;
1042 p_fci_internal
->fNextCab
= FALSE
;
1043 p_fci_internal
->fSplitFolder
= FALSE
;
1044 p_fci_internal
->fGetNextCabInVain
= FALSE
;
1045 p_fci_internal
->pv
= pv
;
1046 p_fci_internal
->cdata_in
= 0;
1047 p_fci_internal
->cCompressedBytesInFolder
= 0;
1048 p_fci_internal
->cFolders
= 0;
1049 p_fci_internal
->cFiles
= 0;
1050 p_fci_internal
->cDataBlocks
= 0;
1051 p_fci_internal
->data
.handle
= -1;
1052 p_fci_internal
->fNewPrevious
= FALSE
;
1053 p_fci_internal
->estimatedCabinetSize
= 0;
1054 p_fci_internal
->statusFolderTotal
= 0;
1055 p_fci_internal
->folders_size
= 0;
1056 p_fci_internal
->files_size
= 0;
1057 p_fci_internal
->placed_files_size
= 0;
1058 p_fci_internal
->pending_data_size
= 0;
1059 p_fci_internal
->folders_data_size
= 0;
1060 p_fci_internal
->compression
= tcompTYPE_NONE
;
1061 p_fci_internal
->compress
= compress_NONE
;
1063 list_init( &p_fci_internal
->folders_list
);
1064 list_init( &p_fci_internal
->files_list
);
1065 list_init( &p_fci_internal
->blocks_list
);
1067 memcpy(p_fci_internal
->szPrevCab
, pccab
->szCab
, CB_MAX_CABINET_NAME
);
1068 memcpy(p_fci_internal
->szPrevDisk
, pccab
->szDisk
, CB_MAX_DISK_NAME
);
1070 return (HFCI
)p_fci_internal
;
1076 static BOOL
fci_flush_folder( FCI_Int
*p_fci_internal
,
1078 PFNFCIGETNEXTCABINET pfnfcignc
,
1079 PFNFCISTATUS pfnfcis
)
1082 cab_ULONG read_result
;
1083 struct folder
*folder
;
1085 if ((!pfnfcignc
) || (!pfnfcis
)) {
1086 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_BAD_ARGUMENTS
);
1090 if( p_fci_internal
->fGetNextCabInVain
&&
1091 p_fci_internal
->fNextCab
){
1092 /* internal error */
1093 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1097 /* If there was no FCIAddFile or FCIFlushFolder has already been called */
1098 /* this function will return TRUE */
1099 if( p_fci_internal
->files_size
== 0 ) {
1100 if ( p_fci_internal
->pending_data_size
!= 0 ) {
1101 /* error handling */
1102 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1108 /* FCIFlushFolder has already been called... */
1109 if (p_fci_internal
->fSplitFolder
&& p_fci_internal
->placed_files_size
!=0) {
1113 /* This can be set already, because it makes only a difference */
1114 /* when the current function exits with return FALSE */
1115 p_fci_internal
->fSplitFolder
=FALSE
;
1118 if (!add_data_block( p_fci_internal
, pfnfcis
)) return FALSE
;
1120 /* reset to get the number of data blocks of this folder which are */
1121 /* actually in this cabinet ( at least partially ) */
1122 p_fci_internal
->cDataBlocks
=0;
1124 p_fci_internal
->statusFolderTotal
= get_header_size( p_fci_internal
) +
1125 sizeof(CFFOLDER
) + p_fci_internal
->ccab
.cbReserveCFFolder
+
1126 p_fci_internal
->placed_files_size
+
1127 p_fci_internal
->folders_data_size
+ p_fci_internal
->files_size
+
1128 p_fci_internal
->pending_data_size
+ p_fci_internal
->folders_size
;
1129 p_fci_internal
->statusFolderCopied
= 0;
1131 /* report status with pfnfcis about copied size of folder */
1132 if( (*pfnfcis
)(statusFolder
, p_fci_internal
->statusFolderCopied
,
1133 p_fci_internal
->statusFolderTotal
, /* TODO total folder size */
1134 p_fci_internal
->pv
) == -1) {
1135 set_error( p_fci_internal
, FCIERR_USER_ABORT
, 0 );
1139 /* USE the variable read_result */
1140 read_result
= get_header_size( p_fci_internal
) + p_fci_internal
->folders_data_size
+
1141 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
;
1143 if(p_fci_internal
->files_size
!=0) {
1144 read_result
+= sizeof(CFFOLDER
)+p_fci_internal
->ccab
.cbReserveCFFolder
;
1147 /* Check if multiple cabinets have to be created. */
1149 /* Might be too much data for the maximum allowed cabinet size.*/
1150 /* When any further data will be added later, it might not */
1151 /* be possible to flush the cabinet, because there might */
1152 /* not be enough space to store the name of the following */
1153 /* cabinet and name of the corresponding disk. */
1154 /* So take care of this and get the name of the next cabinet */
1155 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1156 p_fci_internal
->fNextCab
==FALSE
&&
1159 p_fci_internal
->ccab
.cb
< read_result
+
1160 p_fci_internal
->pending_data_size
+
1161 p_fci_internal
->files_size
+
1162 CB_MAX_CABINET_NAME
+ /* next cabinet name */
1163 CB_MAX_DISK_NAME
/* next disk name */
1167 /* increment cabinet index */
1168 ++(p_fci_internal
->pccab
->iCab
);
1169 /* get name of next cabinet */
1170 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1171 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1172 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1173 p_fci_internal
->pv
)) {
1174 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1178 /* Skip a few lines of code. This is caught by the next if. */
1179 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1182 /* too much data for cabinet */
1183 if( (p_fci_internal
->fGetNextCabInVain
||
1184 p_fci_internal
->fNextCab
) &&
1187 p_fci_internal
->ccab
.cb
< read_result
+
1188 p_fci_internal
->pending_data_size
+
1189 p_fci_internal
->files_size
+
1190 strlen(p_fci_internal
->pccab
->szCab
)+1 + /* next cabinet name */
1191 strlen(p_fci_internal
->pccab
->szDisk
)+1 /* next disk name */
1195 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1196 p_fci_internal
->fNextCab
=TRUE
;
1198 /* return FALSE if there is not enough space left*/
1199 /* this should never happen */
1200 if (p_fci_internal
->ccab
.cb
<=
1201 p_fci_internal
->files_size
+
1203 strlen(p_fci_internal
->pccab
->szCab
)+1 + /* next cabinet name */
1204 strlen(p_fci_internal
->pccab
->szDisk
)+1 /* next disk name */
1210 /* the folder will be split across cabinets */
1211 p_fci_internal
->fSplitFolder
=TRUE
;
1214 /* this should never happen */
1215 if (p_fci_internal
->fNextCab
) {
1216 /* internal error */
1217 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1222 if (!(folder
= add_folder( p_fci_internal
))) return FALSE
;
1223 if (!add_data_to_folder( p_fci_internal
, folder
, &payload
, pfnfcis
)) return FALSE
;
1224 if (!add_files_to_folder( p_fci_internal
, folder
, payload
)) return FALSE
;
1226 /* reset CFFolder specific information */
1227 p_fci_internal
->cDataBlocks
=0;
1228 p_fci_internal
->cCompressedBytesInFolder
=0;
1236 static BOOL
fci_flush_cabinet( FCI_Int
*p_fci_internal
,
1238 PFNFCIGETNEXTCABINET pfnfcignc
,
1239 PFNFCISTATUS pfnfcis
)
1241 cab_ULONG read_result
=0;
1242 BOOL returntrue
=FALSE
;
1244 /* TODO test if fci_flush_cabinet really aborts if there was no FCIAddFile */
1246 /* when FCIFlushCabinet was or FCIAddFile hasn't been called */
1247 if( p_fci_internal
->files_size
==0 && fGetNextCab
) {
1251 if (!fci_flush_folder(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)){
1252 /* TODO set error */
1256 if(returntrue
) return TRUE
;
1258 if ( (p_fci_internal
->fSplitFolder
&& p_fci_internal
->fNextCab
==FALSE
)||
1259 (p_fci_internal
->folders_size
==0 &&
1260 (p_fci_internal
->files_size
!=0 ||
1261 p_fci_internal
->placed_files_size
!=0 )
1265 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1269 /* create the cabinet */
1270 if (!write_cabinet( p_fci_internal
, pfnfcis
)) return FALSE
;
1272 p_fci_internal
->fPrevCab
=TRUE
;
1273 /* The sections szPrevCab and szPrevDisk are not being updated, because */
1274 /* MS CABINET.DLL always puts the first cabinet name and disk into them */
1276 if (p_fci_internal
->fNextCab
) {
1277 p_fci_internal
->fNextCab
=FALSE
;
1279 if (p_fci_internal
->files_size
==0 && p_fci_internal
->pending_data_size
!=0) {
1280 /* THIS CAN NEVER HAPPEN */
1281 /* set error code */
1282 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1286 if( p_fci_internal
->fNewPrevious
) {
1287 memcpy(p_fci_internal
->szPrevCab
, p_fci_internal
->ccab
.szCab
,
1288 CB_MAX_CABINET_NAME
);
1289 memcpy(p_fci_internal
->szPrevDisk
, p_fci_internal
->ccab
.szDisk
,
1291 p_fci_internal
->fNewPrevious
=FALSE
;
1293 p_fci_internal
->ccab
= *p_fci_internal
->pccab
;
1295 /* REUSE the variable read_result */
1296 read_result
=get_header_size( p_fci_internal
);
1297 if(p_fci_internal
->files_size
!=0) {
1298 read_result
+=p_fci_internal
->ccab
.cbReserveCFFolder
;
1300 read_result
+= p_fci_internal
->pending_data_size
+
1301 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1302 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1303 sizeof(CFFOLDER
); /* set size of new CFFolder entry */
1305 /* too much data for the maximum size of a cabinet */
1306 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1307 p_fci_internal
->ccab
.cb
< read_result
) {
1308 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1311 /* Might be too much data for the maximum size of a cabinet.*/
1312 /* When any further data will be added later, it might not */
1313 /* be possible to flush the cabinet, because there might */
1314 /* not be enough space to store the name of the following */
1315 /* cabinet and name of the corresponding disk. */
1316 /* So take care of this and get the name of the next cabinet */
1317 if (p_fci_internal
->fGetNextCabInVain
==FALSE
&& (
1318 p_fci_internal
->ccab
.cb
< read_result
+
1319 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1321 /* increment cabinet index */
1322 ++(p_fci_internal
->pccab
->iCab
);
1323 /* get name of next cabinet */
1324 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1325 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1326 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1327 p_fci_internal
->pv
)) {
1328 /* error handling */
1329 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1332 /* Skip a few lines of code. This is caught by the next if. */
1333 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1336 /* too much data for cabinet */
1337 if (p_fci_internal
->fGetNextCabInVain
&& (
1338 p_fci_internal
->ccab
.cb
< read_result
+
1339 strlen(p_fci_internal
->ccab
.szCab
)+1+
1340 strlen(p_fci_internal
->ccab
.szDisk
)+1
1342 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1343 p_fci_internal
->fNextCab
=TRUE
;
1344 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1347 /* if the FolderThreshold has been reached flush the folder automatically */
1348 if (p_fci_internal
->cCompressedBytesInFolder
>= p_fci_internal
->ccab
.cbFolderThresh
)
1349 return fci_flush_folder(p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1351 if( p_fci_internal
->files_size
>0 ) {
1352 if( !fci_flush_folder(p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
) ) return FALSE
;
1353 p_fci_internal
->fNewPrevious
=TRUE
;
1356 p_fci_internal
->fNewPrevious
=FALSE
;
1357 if( p_fci_internal
->files_size
>0 || p_fci_internal
->pending_data_size
) {
1358 /* THIS MAY NEVER HAPPEN */
1359 /* set error structures */
1360 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1366 } /* end of fci_flush_cabinet */
1372 /***********************************************************************
1373 * FCIAddFile (CABINET.11)
1375 * FCIAddFile adds a file to the to be created cabinet file
1378 * hfci [I] An HFCI from FCICreate
1379 * pszSourceFile [I] A pointer to a C string which contains the name and
1380 * location of the file which will be added to the cabinet
1381 * pszFileName [I] A pointer to a C string which contains the name under
1382 * which the file will be stored in the cabinet
1383 * fExecute [I] A boolean value which indicates if the file should be
1384 * executed after extraction of self extracting
1386 * pfnfcignc [I] A pointer to a function which gets information about
1388 * pfnfcis [IO] A pointer to a function which will report status
1389 * information about the compression process
1390 * pfnfcioi [I] A pointer to a function which reports file attributes
1391 * and time and date information
1392 * typeCompress [I] Compression type
1395 * On success, returns TRUE
1396 * On failure, returns FALSE
1402 BOOL __cdecl
FCIAddFile(
1404 char *pszSourceFile
,
1407 PFNFCIGETNEXTCABINET pfnfcignc
,
1408 PFNFCISTATUS pfnfcis
,
1409 PFNFCIGETOPENINFO pfnfcigoi
,
1412 cab_ULONG read_result
;
1413 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1415 if (!p_fci_internal
) return FALSE
;
1417 if ((!pszSourceFile
) || (!pszFileName
) || (!pfnfcignc
) || (!pfnfcis
) ||
1418 (!pfnfcigoi
) || strlen(pszFileName
)>=CB_MAX_FILENAME
) {
1419 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_BAD_ARGUMENTS
);
1423 if (typeCompress
!= p_fci_internal
->compression
)
1425 if (!FCIFlushFolder( hfci
, pfnfcignc
, pfnfcis
)) return FALSE
;
1426 switch (typeCompress
)
1428 case tcompTYPE_MSZIP
:
1430 p_fci_internal
->compression
= tcompTYPE_MSZIP
;
1431 p_fci_internal
->compress
= compress_MSZIP
;
1435 FIXME( "compression %x not supported, defaulting to none\n", typeCompress
);
1437 case tcompTYPE_NONE
:
1438 p_fci_internal
->compression
= tcompTYPE_NONE
;
1439 p_fci_internal
->compress
= compress_NONE
;
1444 /* TODO check if pszSourceFile??? */
1446 if(p_fci_internal
->fGetNextCabInVain
&& p_fci_internal
->fNextCab
) {
1447 /* internal error */
1448 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1452 if(p_fci_internal
->fNextCab
) {
1453 /* internal error */
1454 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1458 /* REUSE the variable read_result */
1459 read_result
=get_header_size( p_fci_internal
) + p_fci_internal
->ccab
.cbReserveCFFolder
;
1461 read_result
+= sizeof(CFFILE
) + strlen(pszFileName
)+1 +
1462 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1463 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1464 sizeof(CFFOLDER
); /* size of new CFFolder entry */
1466 /* Might be too much data for the maximum size of a cabinet.*/
1467 /* When any further data will be added later, it might not */
1468 /* be possible to flush the cabinet, because there might */
1469 /* not be enough space to store the name of the following */
1470 /* cabinet and name of the corresponding disk. */
1471 /* So take care of this and get the name of the next cabinet */
1472 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1473 p_fci_internal
->fNextCab
==FALSE
&&
1474 ( p_fci_internal
->ccab
.cb
< read_result
+
1475 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1478 /* increment cabinet index */
1479 ++(p_fci_internal
->pccab
->iCab
);
1480 /* get name of next cabinet */
1481 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1482 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1483 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1484 p_fci_internal
->pv
)) {
1485 /* error handling */
1486 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1489 /* Skip a few lines of code. This is caught by the next if. */
1490 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1493 if( p_fci_internal
->fGetNextCabInVain
&&
1494 p_fci_internal
->fNextCab
1496 /* THIS CAN NEVER HAPPEN */
1497 /* set error code */
1498 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1502 /* too much data for cabinet */
1503 if( p_fci_internal
->fGetNextCabInVain
&&
1505 p_fci_internal
->ccab
.cb
< read_result
+
1506 strlen(p_fci_internal
->pccab
->szCab
)+1+
1507 strlen(p_fci_internal
->pccab
->szDisk
)+1
1509 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1510 p_fci_internal
->fNextCab
=TRUE
;
1511 if(!fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
)) return FALSE
;
1514 if( p_fci_internal
->fNextCab
) {
1515 /* THIS MAY NEVER HAPPEN */
1516 /* set error code */
1517 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1521 if (!add_file_data( p_fci_internal
, pszSourceFile
, pszFileName
, fExecute
, pfnfcigoi
, pfnfcis
))
1524 /* REUSE the variable read_result */
1525 read_result
= get_header_size( p_fci_internal
) + p_fci_internal
->ccab
.cbReserveCFFolder
;
1526 read_result
+= p_fci_internal
->pending_data_size
+
1527 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1528 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1529 sizeof(CFFOLDER
); /* set size of new CFFolder entry */
1531 /* too much data for the maximum size of a cabinet */
1532 /* (ignoring the unflushed data block) */
1533 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1534 p_fci_internal
->fNextCab
==FALSE
&& /* this is always the case */
1535 p_fci_internal
->ccab
.cb
< read_result
) {
1536 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1539 /* Might be too much data for the maximum size of a cabinet.*/
1540 /* When any further data will be added later, it might not */
1541 /* be possible to flush the cabinet, because there might */
1542 /* not be enough space to store the name of the following */
1543 /* cabinet and name of the corresponding disk. */
1544 /* So take care of this and get the name of the next cabinet */
1545 /* (ignoring the unflushed data block) */
1546 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1547 p_fci_internal
->fNextCab
==FALSE
&&
1548 ( p_fci_internal
->ccab
.cb
< read_result
+
1549 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1552 /* increment cabinet index */
1553 ++(p_fci_internal
->pccab
->iCab
);
1554 /* get name of next cabinet */
1555 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1556 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1557 p_fci_internal
->estimatedCabinetSize
,/* estimated size of cab */
1558 p_fci_internal
->pv
)) {
1559 /* error handling */
1560 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1563 /* Skip a few lines of code. This is caught by the next if. */
1564 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1567 if( p_fci_internal
->fGetNextCabInVain
&&
1568 p_fci_internal
->fNextCab
1570 /* THIS CAN NEVER HAPPEN */
1571 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1575 /* too much data for cabinet */
1576 if( (p_fci_internal
->fGetNextCabInVain
||
1577 p_fci_internal
->fNextCab
) && (
1578 p_fci_internal
->ccab
.cb
< read_result
+
1579 strlen(p_fci_internal
->pccab
->szCab
)+1+
1580 strlen(p_fci_internal
->pccab
->szDisk
)+1
1583 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1584 p_fci_internal
->fNextCab
=TRUE
;
1585 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1588 if( p_fci_internal
->fNextCab
) {
1589 /* THIS MAY NEVER HAPPEN */
1590 /* set error code */
1591 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1595 /* if the FolderThreshold has been reached flush the folder automatically */
1596 if (p_fci_internal
->cCompressedBytesInFolder
>= p_fci_internal
->ccab
.cbFolderThresh
)
1597 return FCIFlushFolder(hfci
, pfnfcignc
, pfnfcis
);
1600 } /* end of FCIAddFile */
1606 /***********************************************************************
1607 * FCIFlushFolder (CABINET.12)
1609 * FCIFlushFolder completes the CFFolder structure under construction.
1611 * All further data which is added by FCIAddFile will be associated to
1612 * the next CFFolder structure.
1614 * FCIFlushFolder will be called by FCIAddFile automatically if the
1615 * threshold (stored in the member cbFolderThresh of the CCAB structure
1616 * pccab passed to FCICreate) is exceeded.
1618 * FCIFlushFolder will be called by FCIFlushFolder automatically before
1619 * any data will be written into the cabinet file.
1622 * hfci [I] An HFCI from FCICreate
1623 * pfnfcignc [I] A pointer to a function which gets information about
1625 * pfnfcis [IO] A pointer to a function which will report status
1626 * information about the compression process
1629 * On success, returns TRUE
1630 * On failure, returns FALSE
1636 BOOL __cdecl
FCIFlushFolder(
1638 PFNFCIGETNEXTCABINET pfnfcignc
,
1639 PFNFCISTATUS pfnfcis
)
1641 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1643 if (!p_fci_internal
) return FALSE
;
1644 return fci_flush_folder(p_fci_internal
,FALSE
,pfnfcignc
,pfnfcis
);
1649 /***********************************************************************
1650 * FCIFlushCabinet (CABINET.13)
1652 * FCIFlushCabinet stores the data which has been added by FCIAddFile
1653 * into the cabinet file. If the maximum cabinet size (stored in the
1654 * member cb of the CCAB structure pccab passed to FCICreate) has been
1655 * exceeded FCIFlushCabinet will be called automatic by FCIAddFile.
1656 * The remaining data still has to be flushed manually by calling
1659 * After FCIFlushCabinet has been called (manually) FCIAddFile must
1660 * NOT be called again. Then hfci has to be released by FCIDestroy.
1663 * hfci [I] An HFCI from FCICreate
1664 * fGetNextCab [I] Whether you want to add additional files to a
1665 * cabinet set (TRUE) or whether you want to
1666 * finalize it (FALSE)
1667 * pfnfcignc [I] A pointer to a function which gets information about
1669 * pfnfcis [IO] A pointer to a function which will report status
1670 * information about the compression process
1673 * On success, returns TRUE
1674 * On failure, returns FALSE
1680 BOOL __cdecl
FCIFlushCabinet(
1683 PFNFCIGETNEXTCABINET pfnfcignc
,
1684 PFNFCISTATUS pfnfcis
)
1686 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1688 if (!p_fci_internal
) return FALSE
;
1690 if(!fci_flush_cabinet(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)) return FALSE
;
1692 while( p_fci_internal
->files_size
>0 ||
1693 p_fci_internal
->placed_files_size
>0 ) {
1694 if(!fci_flush_cabinet(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)) return FALSE
;
1701 /***********************************************************************
1702 * FCIDestroy (CABINET.14)
1704 * Frees a handle created by FCICreate.
1705 * Only reason for failure would be an invalid handle.
1708 * hfci [I] The HFCI to free
1714 BOOL __cdecl
FCIDestroy(HFCI hfci
)
1716 struct folder
*folder
, *folder_next
;
1717 struct file
*file
, *file_next
;
1718 struct data_block
*block
, *block_next
;
1719 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1721 if (!p_fci_internal
) return FALSE
;
1723 /* before hfci can be removed all temporary files must be closed */
1725 p_fci_internal
->magic
= 0;
1727 LIST_FOR_EACH_ENTRY_SAFE( folder
, folder_next
, &p_fci_internal
->folders_list
, struct folder
, entry
)
1729 free_folder( p_fci_internal
, folder
);
1731 LIST_FOR_EACH_ENTRY_SAFE( file
, file_next
, &p_fci_internal
->files_list
, struct file
, entry
)
1733 free_file( p_fci_internal
, file
);
1735 LIST_FOR_EACH_ENTRY_SAFE( block
, block_next
, &p_fci_internal
->blocks_list
, struct data_block
, entry
)
1737 free_data_block( p_fci_internal
, block
);
1740 close_temp_file( p_fci_internal
, &p_fci_internal
->data
);
1742 /* hfci can now be removed */
1743 p_fci_internal
->free(hfci
);