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
47 #include "wine/list.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(cabinet
);
52 #ifdef WORDS_BIGENDIAN
53 #define fci_endian_ulong(x) RtlUlongByteSwap(x)
54 #define fci_endian_uword(x) RtlUshortByteSwap(x)
56 #define fci_endian_ulong(x) (x)
57 #define fci_endian_uword(x) (x)
62 cab_UBYTE signature
[4]; /* !CAB for unfinished cabinets else MSCF */
64 cab_ULONG cbCabinet
; /* size of the cabinet file in bytes*/
66 cab_ULONG coffFiles
; /* offset to first CFFILE section */
68 cab_UBYTE versionMinor
; /* 3 */
69 cab_UBYTE versionMajor
; /* 1 */
70 cab_UWORD cFolders
; /* number of CFFOLDER entries in the cabinet*/
71 cab_UWORD cFiles
; /* number of CFFILE entries in the cabinet*/
72 cab_UWORD flags
; /* 1=prev cab, 2=next cabinet, 4=reserved sections*/
73 cab_UWORD setID
; /* identification number of all cabinets in a set*/
74 cab_UWORD iCabinet
; /* number of the cabinet in a set */
75 /* additional area if "flags" were set*/
76 } CFHEADER
; /* minimum 36 bytes */
79 cab_ULONG coffCabStart
; /* offset to the folder's first CFDATA section */
80 cab_UWORD cCFData
; /* number of this folder's CFDATA sections */
81 cab_UWORD typeCompress
; /* compression type of data in CFDATA section*/
82 /* additional area if reserve flag was set */
83 } CFFOLDER
; /* minimum 8 bytes */
86 cab_ULONG cbFile
; /* size of the uncompressed file in bytes */
87 cab_ULONG uoffFolderStart
; /* offset of the uncompressed file in the folder */
88 cab_UWORD iFolder
; /* number of folder in the cabinet 0=first */
89 /* for special values see below this structure*/
90 cab_UWORD date
; /* last modification date*/
91 cab_UWORD time
; /* last modification time*/
92 cab_UWORD attribs
; /* DOS fat attributes and UTF indicator */
93 /* ... and a C string with the name of the file */
94 } CFFILE
; /* 16 bytes + name of file */
98 cab_ULONG csum
; /* checksum of this entry*/
99 cab_UWORD cbData
; /* number of compressed bytes */
100 cab_UWORD cbUncomp
; /* number of bytes when data is uncompressed */
101 /* optional reserved area */
102 /* compressed data */
108 char name
[CB_MAX_FILENAME
];
114 struct list files_list
;
115 struct list blocks_list
;
116 struct temp_file data
;
117 cab_ULONG data_start
;
118 cab_UWORD data_count
;
125 cab_ULONG size
; /* uncompressed size */
126 cab_ULONG offset
; /* offset in folder */
127 cab_UWORD folder
; /* index of folder */
137 cab_UWORD compressed
;
138 cab_UWORD uncompressed
;
141 typedef struct FCI_Int
145 PFNFCIFILEPLACED fileplaced
;
154 PFNFCIGETTEMPFILE gettemp
;
160 cab_ULONG statusFolderCopied
;
161 cab_ULONG statusFolderTotal
;
162 BOOL fGetNextCabInVain
;
164 char szPrevCab
[CB_MAX_CABINET_NAME
]; /* previous cabinet name */
165 char szPrevDisk
[CB_MAX_DISK_NAME
]; /* disk name of previous cabinet */
166 unsigned char data_in
[CAB_BLOCKMAX
]; /* uncompressed data blocks */
167 unsigned char data_out
[2 * CAB_BLOCKMAX
]; /* compressed data blocks */
169 ULONG cCompressedBytesInFolder
;
172 cab_ULONG cDataBlocks
;
173 cab_ULONG cbFileRemainder
; /* uncompressed, yet to be written data */
174 /* of spanned file of a spanning folder of a spanning cabinet */
175 struct temp_file data
;
177 cab_ULONG estimatedCabinetSize
;
178 struct list folders_list
;
179 struct list files_list
;
180 struct list blocks_list
;
181 cab_ULONG folders_size
;
182 cab_ULONG files_size
; /* size of files not yet assigned to a folder */
183 cab_ULONG placed_files_size
; /* size of files already placed into a folder */
184 cab_ULONG pending_data_size
; /* size of data not yet assigned to a folder */
185 cab_ULONG folders_data_size
; /* total size of data contained in the current folders */
187 cab_UWORD (*compress
)(struct FCI_Int
*);
190 #define FCI_INT_MAGIC 0xfcfcfc05
192 static void set_error( FCI_Int
*fci
, int oper
, int err
)
194 fci
->perf
->erfOper
= oper
;
195 fci
->perf
->erfType
= err
;
196 fci
->perf
->fError
= TRUE
;
197 if (err
) SetLastError( err
);
200 static FCI_Int
*get_fci_ptr( HFCI hfci
)
202 FCI_Int
*fci
= (FCI_Int
*)hfci
;
204 if (!fci
|| fci
->magic
!= FCI_INT_MAGIC
)
206 SetLastError( ERROR_INVALID_HANDLE
);
212 /* compute the cabinet header size */
213 static cab_ULONG
get_header_size( FCI_Int
*fci
)
215 cab_ULONG ret
= sizeof(CFHEADER
) + fci
->ccab
.cbReserveCFHeader
;
217 if (fci
->ccab
.cbReserveCFHeader
|| fci
->ccab
.cbReserveCFFolder
|| fci
->ccab
.cbReserveCFData
)
221 ret
+= strlen( fci
->szPrevCab
) + 1 + strlen( fci
->szPrevDisk
) + 1;
224 ret
+= strlen( fci
->pccab
->szCab
) + 1 + strlen( fci
->pccab
->szDisk
) + 1;
229 static BOOL
create_temp_file( FCI_Int
*fci
, struct temp_file
*file
)
233 if (!fci
->gettemp( file
->name
, CB_MAX_FILENAME
, fci
->pv
))
235 set_error( fci
, FCIERR_TEMP_FILE
, ERROR_FUNCTION_FAILED
);
238 if ((file
->handle
= fci
->open( file
->name
, _O_RDWR
| _O_CREAT
| _O_EXCL
| _O_BINARY
,
239 _S_IREAD
| _S_IWRITE
, &err
, fci
->pv
)) == -1)
241 set_error( fci
, FCIERR_TEMP_FILE
, err
);
247 static BOOL
close_temp_file( FCI_Int
*fci
, struct temp_file
*file
)
251 if (file
->handle
== -1) return TRUE
;
252 if (fci
->close( file
->handle
, &err
, fci
->pv
) == -1)
254 set_error( fci
, FCIERR_TEMP_FILE
, err
);
258 if (fci
->delete( file
->name
, &err
, fci
->pv
) == -1)
260 set_error( fci
, FCIERR_TEMP_FILE
, err
);
266 static struct file
*add_file( FCI_Int
*fci
, const char *filename
)
268 unsigned int size
= FIELD_OFFSET( struct file
, name
[strlen(filename
) + 1] );
269 struct file
*file
= fci
->alloc( size
);
273 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
277 file
->offset
= fci
->cDataBlocks
* CAB_BLOCKMAX
+ fci
->cdata_in
;
278 file
->folder
= fci
->cFolders
;
282 strcpy( file
->name
, filename
);
283 list_add_tail( &fci
->files_list
, &file
->entry
);
284 fci
->files_size
+= sizeof(CFFILE
) + strlen(filename
) + 1;
288 static struct file
*copy_file( FCI_Int
*fci
, const struct file
*orig
)
290 unsigned int size
= FIELD_OFFSET( struct file
, name
[strlen(orig
->name
) + 1] );
291 struct file
*file
= fci
->alloc( size
);
295 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
298 memcpy( file
, orig
, size
);
302 static void free_file( FCI_Int
*fci
, struct file
*file
)
304 list_remove( &file
->entry
);
308 /* create a new data block for the data in fci->data_in */
309 static BOOL
add_data_block( FCI_Int
*fci
, PFNFCISTATUS status_callback
)
312 struct data_block
*block
;
314 if (!fci
->cdata_in
) return TRUE
;
316 if (fci
->data
.handle
== -1 && !create_temp_file( fci
, &fci
->data
)) return FALSE
;
318 if (!(block
= fci
->alloc( sizeof(*block
) )))
320 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
323 block
->uncompressed
= fci
->cdata_in
;
324 block
->compressed
= fci
->compress( fci
);
326 if (fci
->write( fci
->data
.handle
, fci
->data_out
,
327 block
->compressed
, &err
, fci
->pv
) != block
->compressed
)
329 set_error( fci
, FCIERR_TEMP_FILE
, err
);
335 fci
->pending_data_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ block
->compressed
;
336 fci
->cCompressedBytesInFolder
+= block
->compressed
;
338 list_add_tail( &fci
->blocks_list
, &block
->entry
);
340 if (status_callback( statusFile
, block
->compressed
, block
->uncompressed
, fci
->pv
) == -1)
342 set_error( fci
, FCIERR_USER_ABORT
, 0 );
348 /* add compressed blocks for all the data that can be read from the file */
349 static BOOL
add_file_data( FCI_Int
*fci
, char *sourcefile
, char *filename
, BOOL execute
,
350 PFNFCIGETOPENINFO get_open_info
, PFNFCISTATUS status_callback
)
356 if (!(file
= add_file( fci
, filename
))) return FALSE
;
358 handle
= get_open_info( sourcefile
, &file
->date
, &file
->time
, &file
->attribs
, &err
, fci
->pv
);
361 free_file( fci
, file
);
362 set_error( fci
, FCIERR_OPEN_SRC
, err
);
365 if (execute
) file
->attribs
|= _A_EXEC
;
369 len
= fci
->read( handle
, fci
->data_in
+ fci
->cdata_in
,
370 CAB_BLOCKMAX
- fci
->cdata_in
, &err
, fci
->pv
);
375 set_error( fci
, FCIERR_READ_SRC
, err
);
379 fci
->cdata_in
+= len
;
380 if (fci
->cdata_in
== CAB_BLOCKMAX
&& !add_data_block( fci
, status_callback
)) return FALSE
;
382 fci
->close( handle
, &err
, fci
->pv
);
386 static void free_data_block( FCI_Int
*fci
, struct data_block
*block
)
388 list_remove( &block
->entry
);
392 static struct folder
*add_folder( FCI_Int
*fci
)
394 struct folder
*folder
= fci
->alloc( sizeof(*folder
) );
398 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
401 folder
->data
.handle
= -1;
402 folder
->data_start
= fci
->folders_data_size
;
403 folder
->data_count
= 0;
404 folder
->compression
= fci
->compression
;
405 list_init( &folder
->files_list
);
406 list_init( &folder
->blocks_list
);
407 list_add_tail( &fci
->folders_list
, &folder
->entry
);
408 fci
->folders_size
+= sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
;
413 static void free_folder( FCI_Int
*fci
, struct folder
*folder
)
415 struct file
*file
, *file_next
;
416 struct data_block
*block
, *block_next
;
418 LIST_FOR_EACH_ENTRY_SAFE( file
, file_next
, &folder
->files_list
, struct file
, entry
)
419 free_file( fci
, file
);
420 LIST_FOR_EACH_ENTRY_SAFE( block
, block_next
, &folder
->blocks_list
, struct data_block
, entry
)
421 free_data_block( fci
, block
);
422 close_temp_file( fci
, &folder
->data
);
423 list_remove( &folder
->entry
);
427 /* reset state for the next cabinet file once the current one has been flushed */
428 static void reset_cabinet( FCI_Int
*fci
)
430 struct folder
*folder
, *folder_next
;
432 LIST_FOR_EACH_ENTRY_SAFE( folder
, folder_next
, &fci
->folders_list
, struct folder
, entry
)
433 free_folder( fci
, folder
);
437 fci
->folders_size
= 0;
438 fci
->placed_files_size
= 0;
439 fci
->folders_data_size
= 0;
442 static cab_ULONG
fci_get_checksum( const void *pv
, UINT cb
, cab_ULONG seed
)
453 while (cUlong
-- > 0) {
455 ul
|= (((cab_ULONG
)(*pb
++)) << 8);
456 ul
|= (((cab_ULONG
)(*pb
++)) << 16);
457 ul
|= (((cab_ULONG
)(*pb
++)) << 24);
464 ul
|= (((ULONG
)(*pb
++)) << 16);
467 ul
|= (((ULONG
)(*pb
++)) << 8);
480 /* copy all remaining data block to a new temp file */
481 static BOOL
copy_data_blocks( FCI_Int
*fci
, INT_PTR handle
, cab_ULONG start_pos
,
482 struct temp_file
*temp
, PFNFCISTATUS status_callback
)
484 struct data_block
*block
;
487 if (fci
->seek( handle
, start_pos
, SEEK_SET
, &err
, fci
->pv
) != start_pos
)
489 set_error( fci
, FCIERR_TEMP_FILE
, err
);
492 if (!create_temp_file( fci
, temp
)) return FALSE
;
494 LIST_FOR_EACH_ENTRY( block
, &fci
->blocks_list
, struct data_block
, entry
)
496 if (fci
->read( handle
, fci
->data_out
, block
->compressed
,
497 &err
, fci
->pv
) != block
->compressed
)
499 close_temp_file( fci
, temp
);
500 set_error( fci
, FCIERR_TEMP_FILE
, err
);
503 if (fci
->write( temp
->handle
, fci
->data_out
, block
->compressed
,
504 &err
, fci
->pv
) != block
->compressed
)
506 close_temp_file( fci
, temp
);
507 set_error( fci
, FCIERR_TEMP_FILE
, err
);
510 fci
->pending_data_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ block
->compressed
;
511 fci
->statusFolderCopied
+= block
->compressed
;
513 if (status_callback( statusFolder
, fci
->statusFolderCopied
,
514 fci
->statusFolderTotal
, fci
->pv
) == -1)
516 close_temp_file( fci
, temp
);
517 set_error( fci
, FCIERR_USER_ABORT
, 0 );
524 /* write all folders to disk and remove them from the list */
525 static BOOL
write_folders( FCI_Int
*fci
, INT_PTR handle
, cab_ULONG header_size
, PFNFCISTATUS status_callback
)
527 struct folder
*folder
;
529 CFFOLDER
*cffolder
= (CFFOLDER
*)fci
->data_out
;
530 cab_ULONG folder_size
= sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
;
532 memset( cffolder
, 0, folder_size
);
534 /* write the folders */
535 LIST_FOR_EACH_ENTRY( folder
, &fci
->folders_list
, struct folder
, entry
)
537 cffolder
->coffCabStart
= fci_endian_ulong( folder
->data_start
+ header_size
);
538 cffolder
->cCFData
= fci_endian_uword( folder
->data_count
);
539 cffolder
->typeCompress
= fci_endian_uword( folder
->compression
);
540 if (fci
->write( handle
, cffolder
, folder_size
, &err
, fci
->pv
) != folder_size
)
542 set_error( fci
, FCIERR_CAB_FILE
, err
);
549 /* write all the files to the cabinet file */
550 static BOOL
write_files( FCI_Int
*fci
, INT_PTR handle
, PFNFCISTATUS status_callback
)
553 struct folder
*folder
;
556 CFFILE
*cffile
= (CFFILE
*)fci
->data_out
;
558 LIST_FOR_EACH_ENTRY( folder
, &fci
->folders_list
, struct folder
, entry
)
560 LIST_FOR_EACH_ENTRY( file
, &folder
->files_list
, struct file
, entry
)
562 cffile
->cbFile
= fci_endian_ulong( file
->size
);
563 cffile
->uoffFolderStart
= fci_endian_ulong( file
->offset
);
564 cffile
->iFolder
= fci_endian_uword( file
->folder
);
565 cffile
->date
= fci_endian_uword( file
->date
);
566 cffile
->time
= fci_endian_uword( file
->time
);
567 cffile
->attribs
= fci_endian_uword( file
->attribs
);
568 lstrcpynA( (char *)(cffile
+ 1), file
->name
, CB_MAX_FILENAME
);
569 file_size
= sizeof(CFFILE
) + strlen( (char *)(cffile
+ 1) ) + 1;
570 if (fci
->write( handle
, cffile
, file_size
, &err
, fci
->pv
) != file_size
)
572 set_error( fci
, FCIERR_CAB_FILE
, err
);
575 if (!fci
->fSplitFolder
)
577 fci
->statusFolderCopied
= 0;
578 /* TODO TEST THIS further */
579 fci
->statusFolderTotal
= fci
->folders_data_size
+ fci
->placed_files_size
;
581 fci
->statusFolderCopied
+= file_size
;
582 /* report status about copied size of folder */
583 if (status_callback( statusFolder
, fci
->statusFolderCopied
,
584 fci
->statusFolderTotal
, fci
->pv
) == -1)
586 set_error( fci
, FCIERR_USER_ABORT
, 0 );
594 /* write all data blocks to the cabinet file */
595 static BOOL
write_data_blocks( FCI_Int
*fci
, INT_PTR handle
, PFNFCISTATUS status_callback
)
597 struct folder
*folder
;
598 struct data_block
*block
;
602 cab_UWORD header_size
;
604 header_size
= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
;
605 cfdata
= (CFDATA
*)fci
->data_out
;
606 memset( cfdata
, 0, header_size
);
607 data
= (char *)cfdata
+ header_size
;
609 LIST_FOR_EACH_ENTRY( folder
, &fci
->folders_list
, struct folder
, entry
)
611 if (fci
->seek( folder
->data
.handle
, 0, SEEK_SET
, &err
, fci
->pv
) != 0)
613 set_error( fci
, FCIERR_CAB_FILE
, err
);
616 LIST_FOR_EACH_ENTRY( block
, &folder
->blocks_list
, struct data_block
, entry
)
618 len
= fci
->read( folder
->data
.handle
, data
, block
->compressed
, &err
, fci
->pv
);
619 if (len
!= block
->compressed
) return FALSE
;
621 cfdata
->cbData
= fci_endian_uword( block
->compressed
);
622 cfdata
->cbUncomp
= fci_endian_uword( block
->uncompressed
);
623 cfdata
->csum
= fci_endian_ulong( fci_get_checksum( &cfdata
->cbData
,
624 header_size
- FIELD_OFFSET(CFDATA
, cbData
),
625 fci_get_checksum( data
, len
, 0 )));
627 fci
->statusFolderCopied
+= len
;
629 if (fci
->write( handle
, fci
->data_out
, len
, &err
, fci
->pv
) != len
)
631 set_error( fci
, FCIERR_CAB_FILE
, err
);
634 if (status_callback( statusFolder
, fci
->statusFolderCopied
, fci
->statusFolderTotal
, fci
->pv
) == -1)
636 set_error( fci
, FCIERR_USER_ABORT
, 0 );
644 /* write the cabinet file to disk */
645 static BOOL
write_cabinet( FCI_Int
*fci
, PFNFCISTATUS status_callback
)
647 char filename
[CB_MAX_CAB_PATH
+ CB_MAX_CABINET_NAME
];
651 CFHEADER
*cfheader
= (CFHEADER
*)fci
->data_out
;
653 cab_ULONG header_size
= get_header_size( fci
);
654 cab_ULONG total_size
= header_size
+ fci
->folders_size
+
655 fci
->placed_files_size
+ fci
->folders_data_size
;
657 assert( header_size
<= sizeof(fci
->data_out
) );
658 memset( cfheader
, 0, header_size
);
660 if (fci
->fPrevCab
) flags
|= cfheadPREV_CABINET
;
661 if (fci
->fNextCab
) flags
|= cfheadNEXT_CABINET
;
662 if (fci
->ccab
.cbReserveCFHeader
|| fci
->ccab
.cbReserveCFFolder
|| fci
->ccab
.cbReserveCFData
)
663 flags
|= cfheadRESERVE_PRESENT
;
665 memcpy( cfheader
->signature
, "!CAB", 4 );
666 cfheader
->cbCabinet
= fci_endian_ulong( total_size
);
667 cfheader
->coffFiles
= fci_endian_ulong( header_size
+ fci
->folders_size
);
668 cfheader
->versionMinor
= 3;
669 cfheader
->versionMajor
= 1;
670 cfheader
->cFolders
= fci_endian_uword( fci
->cFolders
);
671 cfheader
->cFiles
= fci_endian_uword( fci
->cFiles
);
672 cfheader
->flags
= fci_endian_uword( flags
);
673 cfheader
->setID
= fci_endian_uword( fci
->ccab
.setID
);
674 cfheader
->iCabinet
= fci_endian_uword( fci
->ccab
.iCab
);
675 ptr
= (char *)(cfheader
+ 1);
677 if (flags
& cfheadRESERVE_PRESENT
)
681 cab_UWORD cbCFHeader
;
682 cab_UBYTE cbCFFolder
;
684 } *reserve
= (void *)ptr
;
686 reserve
->cbCFHeader
= fci_endian_uword( fci
->ccab
.cbReserveCFHeader
);
687 reserve
->cbCFFolder
= fci
->ccab
.cbReserveCFFolder
;
688 reserve
->cbCFData
= fci
->ccab
.cbReserveCFData
;
689 ptr
= (char *)(reserve
+ 1);
691 ptr
+= fci
->ccab
.cbReserveCFHeader
;
693 if (flags
& cfheadPREV_CABINET
)
695 strcpy( ptr
, fci
->szPrevCab
);
696 ptr
+= strlen( ptr
) + 1;
697 strcpy( ptr
, fci
->szPrevDisk
);
698 ptr
+= strlen( ptr
) + 1;
701 if (flags
& cfheadNEXT_CABINET
)
703 strcpy( ptr
, fci
->pccab
->szCab
);
704 ptr
+= strlen( ptr
) + 1;
705 strcpy( ptr
, fci
->pccab
->szDisk
);
706 ptr
+= strlen( ptr
) + 1;
709 assert( ptr
- (char *)cfheader
== header_size
);
711 strcpy( filename
, fci
->ccab
.szCabPath
);
712 strcat( filename
, fci
->ccab
.szCab
);
714 if ((handle
= fci
->open( filename
, _O_RDWR
| _O_CREAT
| _O_TRUNC
| _O_BINARY
,
715 _S_IREAD
| _S_IWRITE
, &err
, fci
->pv
)) == -1)
717 set_error( fci
, FCIERR_CAB_FILE
, err
);
721 if (fci
->write( handle
, cfheader
, header_size
, &err
, fci
->pv
) != header_size
)
723 set_error( fci
, FCIERR_CAB_FILE
, err
);
727 /* add size of header size of all CFFOLDERs and size of all CFFILEs */
728 header_size
+= fci
->placed_files_size
+ fci
->folders_size
;
729 if (!write_folders( fci
, handle
, header_size
, status_callback
)) goto failed
;
730 if (!write_files( fci
, handle
, status_callback
)) goto failed
;
731 if (!write_data_blocks( fci
, handle
, status_callback
)) goto failed
;
733 /* update the signature */
734 if (fci
->seek( handle
, 0, SEEK_SET
, &err
, fci
->pv
) != 0 )
736 set_error( fci
, FCIERR_CAB_FILE
, err
);
739 memcpy( cfheader
->signature
, "MSCF", 4 );
740 if (fci
->write( handle
, cfheader
->signature
, 4, &err
, fci
->pv
) != 4)
742 set_error( fci
, FCIERR_CAB_FILE
, err
);
745 fci
->close( handle
, &err
, fci
->pv
);
747 reset_cabinet( fci
);
748 status_callback( statusCabinet
, fci
->estimatedCabinetSize
, total_size
, fci
->pv
);
752 fci
->close( handle
, &err
, fci
->pv
);
753 fci
->delete( filename
, &err
, fci
->pv
);
757 /* add all pending data blocks folder */
758 static BOOL
add_data_to_folder( FCI_Int
*fci
, struct folder
*folder
, cab_ULONG
*payload
,
759 PFNFCISTATUS status_callback
)
761 struct data_block
*block
, *new, *next
;
762 BOOL split_block
= FALSE
;
763 cab_ULONG current_size
, start_pos
= 0;
766 current_size
= get_header_size( fci
) + fci
->folders_size
+
767 fci
->files_size
+ fci
->placed_files_size
+ fci
->folders_data_size
;
769 /* move the temp file into the folder structure */
770 folder
->data
= fci
->data
;
771 fci
->data
.handle
= -1;
772 fci
->pending_data_size
= 0;
774 LIST_FOR_EACH_ENTRY_SAFE( block
, next
, &fci
->blocks_list
, struct data_block
, entry
)
776 /* No more CFDATA fits into the cabinet under construction */
777 /* So don't try to store more data into it */
778 if (fci
->fNextCab
&& (fci
->ccab
.cb
<= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+
779 current_size
+ sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
))
782 if (!(new = fci
->alloc( sizeof(*new) )))
784 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
787 /* Is cabinet with new CFDATA too large? Then data block has to be split */
789 (fci
->ccab
.cb
< sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+
790 block
->compressed
+ current_size
+ sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
))
792 /* Modify the size of the compressed data to store only a part of the */
793 /* data block into the current cabinet. This is done to prevent */
794 /* that the maximum cabinet size will be exceeded. The remainder */
795 /* will be stored into the next following cabinet. */
797 new->compressed
= fci
->ccab
.cb
- (sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ current_size
+
798 sizeof(CFFOLDER
) + fci
->ccab
.cbReserveCFFolder
);
799 new->uncompressed
= 0; /* on split blocks of data this is zero */
800 block
->compressed
-= new->compressed
;
805 new->compressed
= block
->compressed
;
806 new->uncompressed
= block
->uncompressed
;
809 start_pos
+= new->compressed
;
810 current_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ new->compressed
;
811 fci
->folders_data_size
+= sizeof(CFDATA
) + fci
->ccab
.cbReserveCFData
+ new->compressed
;
812 fci
->statusFolderCopied
+= new->compressed
;
813 (*payload
) += new->uncompressed
;
815 list_add_tail( &folder
->blocks_list
, &new->entry
);
816 folder
->data_count
++;
818 /* report status with pfnfcis about copied size of folder */
819 if (status_callback( statusFolder
, fci
->statusFolderCopied
,
820 fci
->statusFolderTotal
, fci
->pv
) == -1)
822 set_error( fci
, FCIERR_USER_ABORT
, 0 );
825 if (split_block
) break;
826 free_data_block( fci
, block
);
830 if (list_empty( &fci
->blocks_list
)) return TRUE
;
831 return copy_data_blocks( fci
, folder
->data
.handle
, start_pos
, &fci
->data
, status_callback
);
834 /* add all pending files to folder */
835 static BOOL
add_files_to_folder( FCI_Int
*fci
, struct folder
*folder
, cab_ULONG payload
)
837 cab_ULONG sizeOfFiles
= 0, sizeOfFilesPrev
;
838 cab_ULONG cbFileRemainder
= 0;
839 struct file
*file
, *next
;
841 LIST_FOR_EACH_ENTRY_SAFE( file
, next
, &fci
->files_list
, struct file
, entry
)
843 cab_ULONG size
= sizeof(CFFILE
) + strlen(file
->name
) + 1;
845 /* fnfilfnfildest: placed file on cabinet */
846 fci
->fileplaced( &fci
->ccab
, file
->name
, file
->size
,
847 (file
->folder
== cffileCONTINUED_FROM_PREV
), fci
->pv
);
849 sizeOfFilesPrev
= sizeOfFiles
;
850 /* set complete size of all processed files */
851 if (file
->folder
== cffileCONTINUED_FROM_PREV
&& fci
->cbFileRemainder
!= 0)
853 sizeOfFiles
+= fci
->cbFileRemainder
;
854 fci
->cbFileRemainder
= 0;
856 else sizeOfFiles
+= file
->size
;
858 /* check if spanned file fits into this cabinet folder */
859 if (sizeOfFiles
> payload
)
861 if (file
->folder
== cffileCONTINUED_FROM_PREV
)
862 file
->folder
= cffileCONTINUED_PREV_AND_NEXT
;
864 file
->folder
= cffileCONTINUED_TO_NEXT
;
867 list_remove( &file
->entry
);
868 list_add_tail( &folder
->files_list
, &file
->entry
);
869 fci
->placed_files_size
+= size
;
872 /* This is only true for files which will be written into the */
873 /* next cabinet of the spanning folder */
874 if (sizeOfFiles
> payload
)
876 /* add a copy back onto the list */
877 if (!(file
= copy_file( fci
, file
))) return FALSE
;
878 list_add_before( &next
->entry
, &file
->entry
);
880 /* Files which data will be partially written into the current cabinet */
881 if (file
->folder
== cffileCONTINUED_PREV_AND_NEXT
|| file
->folder
== cffileCONTINUED_TO_NEXT
)
883 if (sizeOfFilesPrev
<= payload
)
885 /* The size of the uncompressed, data of a spanning file in a */
887 cbFileRemainder
= sizeOfFiles
- payload
;
889 file
->folder
= cffileCONTINUED_FROM_PREV
;
891 else file
->folder
= 0;
895 fci
->files_size
-= size
;
898 fci
->cbFileRemainder
= cbFileRemainder
;
902 static cab_UWORD
compress_NONE( FCI_Int
*fci
)
904 memcpy( fci
->data_out
, fci
->data_in
, fci
->cdata_in
);
905 return fci
->cdata_in
;
908 static void *zalloc( void *opaque
, unsigned int items
, unsigned int size
)
910 FCI_Int
*fci
= opaque
;
911 return fci
->alloc( items
* size
);
914 static void zfree( void *opaque
, void *ptr
)
916 FCI_Int
*fci
= opaque
;
920 static cab_UWORD
compress_MSZIP( FCI_Int
*fci
)
924 stream
.zalloc
= zalloc
;
925 stream
.zfree
= zfree
;
927 if (deflateInit2( &stream
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, -15, 8, Z_DEFAULT_STRATEGY
) != Z_OK
)
929 set_error( fci
, FCIERR_ALLOC_FAIL
, ERROR_NOT_ENOUGH_MEMORY
);
932 stream
.next_in
= fci
->data_in
;
933 stream
.avail_in
= fci
->cdata_in
;
934 stream
.next_out
= fci
->data_out
+ 2;
935 stream
.avail_out
= sizeof(fci
->data_out
) - 2;
936 /* insert the signature */
937 fci
->data_out
[0] = 'C';
938 fci
->data_out
[1] = 'K';
939 deflate( &stream
, Z_FINISH
);
940 deflateEnd( &stream
);
941 return stream
.total_out
+ 2;
945 /***********************************************************************
946 * FCICreate (CABINET.10)
948 * FCICreate is provided with several callbacks and
949 * returns a handle which can be used to create cabinet files.
952 * perf [IO] A pointer to an ERF structure. When FCICreate
953 * returns an error condition, error information may
954 * be found here as well as from GetLastError.
955 * pfnfiledest [I] A pointer to a function which is called when a file
956 * is placed. Only useful for subsequent cabinet files.
957 * pfnalloc [I] A pointer to a function which allocates ram. Uses
958 * the same interface as malloc.
959 * pfnfree [I] A pointer to a function which frees ram. Uses the
960 * same interface as free.
961 * pfnopen [I] A pointer to a function which opens a file. Uses
962 * the same interface as _open.
963 * pfnread [I] A pointer to a function which reads from a file into
964 * a caller-provided buffer. Uses the same interface
966 * pfnwrite [I] A pointer to a function which writes to a file from
967 * a caller-provided buffer. Uses the same interface
969 * pfnclose [I] A pointer to a function which closes a file handle.
970 * Uses the same interface as _close.
971 * pfnseek [I] A pointer to a function which seeks in a file.
972 * Uses the same interface as _lseek.
973 * pfndelete [I] A pointer to a function which deletes a file.
974 * pfnfcigtf [I] A pointer to a function which gets the name of a
976 * pccab [I] A pointer to an initialized CCAB structure.
977 * pv [I] A pointer to an application-defined notification
978 * function which will be passed to other FCI functions
982 * On success, returns an FCI handle of type HFCI.
983 * On failure, the NULL file handle is returned. Error
984 * info can be retrieved from perf.
990 HFCI __cdecl
FCICreate(
992 PFNFCIFILEPLACED pfnfiledest
,
993 PFNFCIALLOC pfnalloc
,
997 PFNFCIWRITE pfnwrite
,
998 PFNFCICLOSE pfnclose
,
1000 PFNFCIDELETE pfndelete
,
1001 PFNFCIGETTEMPFILE pfnfcigtf
,
1005 FCI_Int
*p_fci_internal
;
1008 SetLastError(ERROR_BAD_ARGUMENTS
);
1011 if ((!pfnalloc
) || (!pfnfree
) || (!pfnopen
) || (!pfnread
) ||
1012 (!pfnwrite
) || (!pfnclose
) || (!pfnseek
) || (!pfndelete
) ||
1013 (!pfnfcigtf
) || (!pccab
)) {
1014 perf
->erfOper
= FCIERR_NONE
;
1015 perf
->erfType
= ERROR_BAD_ARGUMENTS
;
1016 perf
->fError
= TRUE
;
1018 SetLastError(ERROR_BAD_ARGUMENTS
);
1022 if (!((p_fci_internal
= pfnalloc(sizeof(FCI_Int
))))) {
1023 perf
->erfOper
= FCIERR_ALLOC_FAIL
;
1024 perf
->erfType
= ERROR_NOT_ENOUGH_MEMORY
;
1025 perf
->fError
= TRUE
;
1027 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1031 memset(p_fci_internal
, 0, sizeof(FCI_Int
));
1032 p_fci_internal
->magic
= FCI_INT_MAGIC
;
1033 p_fci_internal
->perf
= perf
;
1034 p_fci_internal
->fileplaced
= pfnfiledest
;
1035 p_fci_internal
->alloc
= pfnalloc
;
1036 p_fci_internal
->free
= pfnfree
;
1037 p_fci_internal
->open
= pfnopen
;
1038 p_fci_internal
->read
= pfnread
;
1039 p_fci_internal
->write
= pfnwrite
;
1040 p_fci_internal
->close
= pfnclose
;
1041 p_fci_internal
->seek
= pfnseek
;
1042 p_fci_internal
->delete = pfndelete
;
1043 p_fci_internal
->gettemp
= pfnfcigtf
;
1044 p_fci_internal
->ccab
= *pccab
;
1045 p_fci_internal
->pccab
= pccab
;
1046 p_fci_internal
->pv
= pv
;
1047 p_fci_internal
->data
.handle
= -1;
1048 p_fci_internal
->compress
= compress_NONE
;
1050 list_init( &p_fci_internal
->folders_list
);
1051 list_init( &p_fci_internal
->files_list
);
1052 list_init( &p_fci_internal
->blocks_list
);
1054 memcpy(p_fci_internal
->szPrevCab
, pccab
->szCab
, CB_MAX_CABINET_NAME
);
1055 memcpy(p_fci_internal
->szPrevDisk
, pccab
->szDisk
, CB_MAX_DISK_NAME
);
1057 return (HFCI
)p_fci_internal
;
1063 static BOOL
fci_flush_folder( FCI_Int
*p_fci_internal
,
1065 PFNFCIGETNEXTCABINET pfnfcignc
,
1066 PFNFCISTATUS pfnfcis
)
1069 cab_ULONG read_result
;
1070 struct folder
*folder
;
1072 if ((!pfnfcignc
) || (!pfnfcis
)) {
1073 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_BAD_ARGUMENTS
);
1077 if( p_fci_internal
->fGetNextCabInVain
&&
1078 p_fci_internal
->fNextCab
){
1079 /* internal error */
1080 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1084 /* If there was no FCIAddFile or FCIFlushFolder has already been called */
1085 /* this function will return TRUE */
1086 if( p_fci_internal
->files_size
== 0 ) {
1087 if ( p_fci_internal
->pending_data_size
!= 0 ) {
1088 /* error handling */
1089 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1095 /* FCIFlushFolder has already been called... */
1096 if (p_fci_internal
->fSplitFolder
&& p_fci_internal
->placed_files_size
!=0) {
1100 /* This can be set already, because it makes only a difference */
1101 /* when the current function exits with return FALSE */
1102 p_fci_internal
->fSplitFolder
=FALSE
;
1105 if (!add_data_block( p_fci_internal
, pfnfcis
)) return FALSE
;
1107 /* reset to get the number of data blocks of this folder which are */
1108 /* actually in this cabinet ( at least partially ) */
1109 p_fci_internal
->cDataBlocks
=0;
1111 p_fci_internal
->statusFolderTotal
= get_header_size( p_fci_internal
) +
1112 sizeof(CFFOLDER
) + p_fci_internal
->ccab
.cbReserveCFFolder
+
1113 p_fci_internal
->placed_files_size
+
1114 p_fci_internal
->folders_data_size
+ p_fci_internal
->files_size
+
1115 p_fci_internal
->pending_data_size
+ p_fci_internal
->folders_size
;
1116 p_fci_internal
->statusFolderCopied
= 0;
1118 /* report status with pfnfcis about copied size of folder */
1119 if( (*pfnfcis
)(statusFolder
, p_fci_internal
->statusFolderCopied
,
1120 p_fci_internal
->statusFolderTotal
, /* TODO total folder size */
1121 p_fci_internal
->pv
) == -1) {
1122 set_error( p_fci_internal
, FCIERR_USER_ABORT
, 0 );
1126 /* USE the variable read_result */
1127 read_result
= get_header_size( p_fci_internal
) + p_fci_internal
->folders_data_size
+
1128 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
;
1130 if(p_fci_internal
->files_size
!=0) {
1131 read_result
+= sizeof(CFFOLDER
)+p_fci_internal
->ccab
.cbReserveCFFolder
;
1134 /* Check if multiple cabinets have to be created. */
1136 /* Might be too much data for the maximum allowed cabinet size.*/
1137 /* When any further data will be added later, it might not */
1138 /* be possible to flush the cabinet, because there might */
1139 /* not be enough space to store the name of the following */
1140 /* cabinet and name of the corresponding disk. */
1141 /* So take care of this and get the name of the next cabinet */
1142 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1143 p_fci_internal
->fNextCab
==FALSE
&&
1146 p_fci_internal
->ccab
.cb
< read_result
+
1147 p_fci_internal
->pending_data_size
+
1148 p_fci_internal
->files_size
+
1149 CB_MAX_CABINET_NAME
+ /* next cabinet name */
1150 CB_MAX_DISK_NAME
/* next disk name */
1154 /* increment cabinet index */
1155 ++(p_fci_internal
->pccab
->iCab
);
1156 /* get name of next cabinet */
1157 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1158 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1159 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1160 p_fci_internal
->pv
)) {
1161 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1165 /* Skip a few lines of code. This is caught by the next if. */
1166 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1169 /* too much data for cabinet */
1170 if( (p_fci_internal
->fGetNextCabInVain
||
1171 p_fci_internal
->fNextCab
) &&
1174 p_fci_internal
->ccab
.cb
< read_result
+
1175 p_fci_internal
->pending_data_size
+
1176 p_fci_internal
->files_size
+
1177 strlen(p_fci_internal
->pccab
->szCab
)+1 + /* next cabinet name */
1178 strlen(p_fci_internal
->pccab
->szDisk
)+1 /* next disk name */
1182 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1183 p_fci_internal
->fNextCab
=TRUE
;
1185 /* return FALSE if there is not enough space left*/
1186 /* this should never happen */
1187 if (p_fci_internal
->ccab
.cb
<=
1188 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 */
1197 /* the folder will be split across cabinets */
1198 p_fci_internal
->fSplitFolder
=TRUE
;
1201 /* this should never happen */
1202 if (p_fci_internal
->fNextCab
) {
1203 /* internal error */
1204 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1209 if (!(folder
= add_folder( p_fci_internal
))) return FALSE
;
1210 if (!add_data_to_folder( p_fci_internal
, folder
, &payload
, pfnfcis
)) return FALSE
;
1211 if (!add_files_to_folder( p_fci_internal
, folder
, payload
)) return FALSE
;
1213 /* reset CFFolder specific information */
1214 p_fci_internal
->cDataBlocks
=0;
1215 p_fci_internal
->cCompressedBytesInFolder
=0;
1223 static BOOL
fci_flush_cabinet( FCI_Int
*p_fci_internal
,
1225 PFNFCIGETNEXTCABINET pfnfcignc
,
1226 PFNFCISTATUS pfnfcis
)
1228 cab_ULONG read_result
=0;
1229 BOOL returntrue
=FALSE
;
1231 /* TODO test if fci_flush_cabinet really aborts if there was no FCIAddFile */
1233 /* when FCIFlushCabinet was or FCIAddFile hasn't been called */
1234 if( p_fci_internal
->files_size
==0 && fGetNextCab
) {
1238 if (!fci_flush_folder(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)){
1239 /* TODO set error */
1243 if(returntrue
) return TRUE
;
1245 if ( (p_fci_internal
->fSplitFolder
&& p_fci_internal
->fNextCab
==FALSE
)||
1246 (p_fci_internal
->folders_size
==0 &&
1247 (p_fci_internal
->files_size
!=0 ||
1248 p_fci_internal
->placed_files_size
!=0 )
1252 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1256 /* create the cabinet */
1257 if (!write_cabinet( p_fci_internal
, pfnfcis
)) return FALSE
;
1259 p_fci_internal
->fPrevCab
=TRUE
;
1260 /* The sections szPrevCab and szPrevDisk are not being updated, because */
1261 /* MS CABINET.DLL always puts the first cabinet name and disk into them */
1263 if (p_fci_internal
->fNextCab
) {
1264 p_fci_internal
->fNextCab
=FALSE
;
1266 if (p_fci_internal
->files_size
==0 && p_fci_internal
->pending_data_size
!=0) {
1267 /* THIS CAN NEVER HAPPEN */
1268 /* set error code */
1269 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1273 if( p_fci_internal
->fNewPrevious
) {
1274 memcpy(p_fci_internal
->szPrevCab
, p_fci_internal
->ccab
.szCab
,
1275 CB_MAX_CABINET_NAME
);
1276 memcpy(p_fci_internal
->szPrevDisk
, p_fci_internal
->ccab
.szDisk
,
1278 p_fci_internal
->fNewPrevious
=FALSE
;
1280 p_fci_internal
->ccab
= *p_fci_internal
->pccab
;
1282 /* REUSE the variable read_result */
1283 read_result
=get_header_size( p_fci_internal
);
1284 if(p_fci_internal
->files_size
!=0) {
1285 read_result
+=p_fci_internal
->ccab
.cbReserveCFFolder
;
1287 read_result
+= p_fci_internal
->pending_data_size
+
1288 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1289 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1290 sizeof(CFFOLDER
); /* set size of new CFFolder entry */
1292 /* too much data for the maximum size of a cabinet */
1293 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1294 p_fci_internal
->ccab
.cb
< read_result
) {
1295 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1298 /* Might be too much data for the maximum size of a cabinet.*/
1299 /* When any further data will be added later, it might not */
1300 /* be possible to flush the cabinet, because there might */
1301 /* not be enough space to store the name of the following */
1302 /* cabinet and name of the corresponding disk. */
1303 /* So take care of this and get the name of the next cabinet */
1304 if (p_fci_internal
->fGetNextCabInVain
==FALSE
&& (
1305 p_fci_internal
->ccab
.cb
< read_result
+
1306 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1308 /* increment cabinet index */
1309 ++(p_fci_internal
->pccab
->iCab
);
1310 /* get name of next cabinet */
1311 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1312 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1313 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1314 p_fci_internal
->pv
)) {
1315 /* error handling */
1316 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1319 /* Skip a few lines of code. This is caught by the next if. */
1320 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1323 /* too much data for cabinet */
1324 if (p_fci_internal
->fGetNextCabInVain
&& (
1325 p_fci_internal
->ccab
.cb
< read_result
+
1326 strlen(p_fci_internal
->ccab
.szCab
)+1+
1327 strlen(p_fci_internal
->ccab
.szDisk
)+1
1329 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1330 p_fci_internal
->fNextCab
=TRUE
;
1331 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1334 /* if the FolderThreshold has been reached flush the folder automatically */
1335 if (p_fci_internal
->cCompressedBytesInFolder
>= p_fci_internal
->ccab
.cbFolderThresh
)
1336 return fci_flush_folder(p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1338 if( p_fci_internal
->files_size
>0 ) {
1339 if( !fci_flush_folder(p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
) ) return FALSE
;
1340 p_fci_internal
->fNewPrevious
=TRUE
;
1343 p_fci_internal
->fNewPrevious
=FALSE
;
1344 if( p_fci_internal
->files_size
>0 || p_fci_internal
->pending_data_size
) {
1345 /* THIS MAY NEVER HAPPEN */
1346 /* set error structures */
1347 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1353 } /* end of fci_flush_cabinet */
1359 /***********************************************************************
1360 * FCIAddFile (CABINET.11)
1362 * FCIAddFile adds a file to the to be created cabinet file
1365 * hfci [I] An HFCI from FCICreate
1366 * pszSourceFile [I] A pointer to a C string which contains the name and
1367 * location of the file which will be added to the cabinet
1368 * pszFileName [I] A pointer to a C string which contains the name under
1369 * which the file will be stored in the cabinet
1370 * fExecute [I] A boolean value which indicates if the file should be
1371 * executed after extraction of self extracting
1373 * pfnfcignc [I] A pointer to a function which gets information about
1375 * pfnfcis [IO] A pointer to a function which will report status
1376 * information about the compression process
1377 * pfnfcioi [I] A pointer to a function which reports file attributes
1378 * and time and date information
1379 * typeCompress [I] Compression type
1382 * On success, returns TRUE
1383 * On failure, returns FALSE
1389 BOOL __cdecl
FCIAddFile(
1391 char *pszSourceFile
,
1394 PFNFCIGETNEXTCABINET pfnfcignc
,
1395 PFNFCISTATUS pfnfcis
,
1396 PFNFCIGETOPENINFO pfnfcigoi
,
1399 cab_ULONG read_result
;
1400 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1402 if (!p_fci_internal
) return FALSE
;
1404 if ((!pszSourceFile
) || (!pszFileName
) || (!pfnfcignc
) || (!pfnfcis
) ||
1405 (!pfnfcigoi
) || strlen(pszFileName
)>=CB_MAX_FILENAME
) {
1406 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_BAD_ARGUMENTS
);
1410 if (typeCompress
!= p_fci_internal
->compression
)
1412 if (!FCIFlushFolder( hfci
, pfnfcignc
, pfnfcis
)) return FALSE
;
1413 switch (typeCompress
)
1415 case tcompTYPE_MSZIP
:
1416 p_fci_internal
->compression
= tcompTYPE_MSZIP
;
1417 p_fci_internal
->compress
= compress_MSZIP
;
1420 FIXME( "compression %x not supported, defaulting to none\n", typeCompress
);
1422 case tcompTYPE_NONE
:
1423 p_fci_internal
->compression
= tcompTYPE_NONE
;
1424 p_fci_internal
->compress
= compress_NONE
;
1429 /* TODO check if pszSourceFile??? */
1431 if(p_fci_internal
->fGetNextCabInVain
&& p_fci_internal
->fNextCab
) {
1432 /* internal error */
1433 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1437 if(p_fci_internal
->fNextCab
) {
1438 /* internal error */
1439 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1443 /* REUSE the variable read_result */
1444 read_result
=get_header_size( p_fci_internal
) + p_fci_internal
->ccab
.cbReserveCFFolder
;
1446 read_result
+= sizeof(CFFILE
) + strlen(pszFileName
)+1 +
1447 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1448 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1449 sizeof(CFFOLDER
); /* size of new CFFolder entry */
1451 /* Might be too much data for the maximum size of a cabinet.*/
1452 /* When any further data will be added later, it might not */
1453 /* be possible to flush the cabinet, because there might */
1454 /* not be enough space to store the name of the following */
1455 /* cabinet and name of the corresponding disk. */
1456 /* So take care of this and get the name of the next cabinet */
1457 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1458 p_fci_internal
->fNextCab
==FALSE
&&
1459 ( p_fci_internal
->ccab
.cb
< read_result
+
1460 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1463 /* increment cabinet index */
1464 ++(p_fci_internal
->pccab
->iCab
);
1465 /* get name of next cabinet */
1466 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1467 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1468 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1469 p_fci_internal
->pv
)) {
1470 /* error handling */
1471 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1474 /* Skip a few lines of code. This is caught by the next if. */
1475 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1478 if( p_fci_internal
->fGetNextCabInVain
&&
1479 p_fci_internal
->fNextCab
1481 /* THIS CAN NEVER HAPPEN */
1482 /* set error code */
1483 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1487 /* too much data for cabinet */
1488 if( p_fci_internal
->fGetNextCabInVain
&&
1490 p_fci_internal
->ccab
.cb
< read_result
+
1491 strlen(p_fci_internal
->pccab
->szCab
)+1+
1492 strlen(p_fci_internal
->pccab
->szDisk
)+1
1494 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1495 p_fci_internal
->fNextCab
=TRUE
;
1496 if(!fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
)) return FALSE
;
1499 if( p_fci_internal
->fNextCab
) {
1500 /* THIS MAY NEVER HAPPEN */
1501 /* set error code */
1502 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1506 if (!add_file_data( p_fci_internal
, pszSourceFile
, pszFileName
, fExecute
, pfnfcigoi
, pfnfcis
))
1509 /* REUSE the variable read_result */
1510 read_result
= get_header_size( p_fci_internal
) + p_fci_internal
->ccab
.cbReserveCFFolder
;
1511 read_result
+= p_fci_internal
->pending_data_size
+
1512 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1513 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1514 sizeof(CFFOLDER
); /* set size of new CFFolder entry */
1516 /* too much data for the maximum size of a cabinet */
1517 /* (ignoring the unflushed data block) */
1518 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1519 p_fci_internal
->fNextCab
==FALSE
&& /* this is always the case */
1520 p_fci_internal
->ccab
.cb
< read_result
) {
1521 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1524 /* Might be too much data for the maximum size of a cabinet.*/
1525 /* When any further data will be added later, it might not */
1526 /* be possible to flush the cabinet, because there might */
1527 /* not be enough space to store the name of the following */
1528 /* cabinet and name of the corresponding disk. */
1529 /* So take care of this and get the name of the next cabinet */
1530 /* (ignoring the unflushed data block) */
1531 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1532 p_fci_internal
->fNextCab
==FALSE
&&
1533 ( p_fci_internal
->ccab
.cb
< read_result
+
1534 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1537 /* increment cabinet index */
1538 ++(p_fci_internal
->pccab
->iCab
);
1539 /* get name of next cabinet */
1540 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1541 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1542 p_fci_internal
->estimatedCabinetSize
,/* estimated size of cab */
1543 p_fci_internal
->pv
)) {
1544 /* error handling */
1545 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1548 /* Skip a few lines of code. This is caught by the next if. */
1549 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1552 if( p_fci_internal
->fGetNextCabInVain
&&
1553 p_fci_internal
->fNextCab
1555 /* THIS CAN NEVER HAPPEN */
1556 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1560 /* too much data for cabinet */
1561 if( (p_fci_internal
->fGetNextCabInVain
||
1562 p_fci_internal
->fNextCab
) && (
1563 p_fci_internal
->ccab
.cb
< read_result
+
1564 strlen(p_fci_internal
->pccab
->szCab
)+1+
1565 strlen(p_fci_internal
->pccab
->szDisk
)+1
1568 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1569 p_fci_internal
->fNextCab
=TRUE
;
1570 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1573 if( p_fci_internal
->fNextCab
) {
1574 /* THIS MAY NEVER HAPPEN */
1575 /* set error code */
1576 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1580 /* if the FolderThreshold has been reached flush the folder automatically */
1581 if (p_fci_internal
->cCompressedBytesInFolder
>= p_fci_internal
->ccab
.cbFolderThresh
)
1582 return FCIFlushFolder(hfci
, pfnfcignc
, pfnfcis
);
1585 } /* end of FCIAddFile */
1591 /***********************************************************************
1592 * FCIFlushFolder (CABINET.12)
1594 * FCIFlushFolder completes the CFFolder structure under construction.
1596 * All further data which is added by FCIAddFile will be associated to
1597 * the next CFFolder structure.
1599 * FCIFlushFolder will be called by FCIAddFile automatically if the
1600 * threshold (stored in the member cbFolderThresh of the CCAB structure
1601 * pccab passed to FCICreate) is exceeded.
1603 * FCIFlushFolder will be called by FCIFlushFolder automatically before
1604 * any data will be written into the cabinet file.
1607 * hfci [I] An HFCI from FCICreate
1608 * pfnfcignc [I] A pointer to a function which gets information about
1610 * pfnfcis [IO] A pointer to a function which will report status
1611 * information about the compression process
1614 * On success, returns TRUE
1615 * On failure, returns FALSE
1621 BOOL __cdecl
FCIFlushFolder(
1623 PFNFCIGETNEXTCABINET pfnfcignc
,
1624 PFNFCISTATUS pfnfcis
)
1626 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1628 if (!p_fci_internal
) return FALSE
;
1629 return fci_flush_folder(p_fci_internal
,FALSE
,pfnfcignc
,pfnfcis
);
1634 /***********************************************************************
1635 * FCIFlushCabinet (CABINET.13)
1637 * FCIFlushCabinet stores the data which has been added by FCIAddFile
1638 * into the cabinet file. If the maximum cabinet size (stored in the
1639 * member cb of the CCAB structure pccab passed to FCICreate) has been
1640 * exceeded FCIFlushCabinet will be called automatic by FCIAddFile.
1641 * The remaining data still has to be flushed manually by calling
1644 * After FCIFlushCabinet has been called (manually) FCIAddFile must
1645 * NOT be called again. Then hfci has to be released by FCIDestroy.
1648 * hfci [I] An HFCI from FCICreate
1649 * fGetNextCab [I] Whether you want to add additional files to a
1650 * cabinet set (TRUE) or whether you want to
1651 * finalize it (FALSE)
1652 * pfnfcignc [I] A pointer to a function which gets information about
1654 * pfnfcis [IO] A pointer to a function which will report status
1655 * information about the compression process
1658 * On success, returns TRUE
1659 * On failure, returns FALSE
1665 BOOL __cdecl
FCIFlushCabinet(
1668 PFNFCIGETNEXTCABINET pfnfcignc
,
1669 PFNFCISTATUS pfnfcis
)
1671 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1673 if (!p_fci_internal
) return FALSE
;
1675 if(!fci_flush_cabinet(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)) return FALSE
;
1677 while( p_fci_internal
->files_size
>0 ||
1678 p_fci_internal
->placed_files_size
>0 ) {
1679 if(!fci_flush_cabinet(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)) return FALSE
;
1686 /***********************************************************************
1687 * FCIDestroy (CABINET.14)
1689 * Frees a handle created by FCICreate.
1690 * Only reason for failure would be an invalid handle.
1693 * hfci [I] The HFCI to free
1699 BOOL __cdecl
FCIDestroy(HFCI hfci
)
1701 struct folder
*folder
, *folder_next
;
1702 struct file
*file
, *file_next
;
1703 struct data_block
*block
, *block_next
;
1704 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1706 if (!p_fci_internal
) return FALSE
;
1708 /* before hfci can be removed all temporary files must be closed */
1710 p_fci_internal
->magic
= 0;
1712 LIST_FOR_EACH_ENTRY_SAFE( folder
, folder_next
, &p_fci_internal
->folders_list
, struct folder
, entry
)
1714 free_folder( p_fci_internal
, folder
);
1716 LIST_FOR_EACH_ENTRY_SAFE( file
, file_next
, &p_fci_internal
->files_list
, struct file
, entry
)
1718 free_file( p_fci_internal
, file
);
1720 LIST_FOR_EACH_ENTRY_SAFE( block
, block_next
, &p_fci_internal
->blocks_list
, struct data_block
, entry
)
1722 free_data_block( p_fci_internal
, block
);
1725 close_temp_file( p_fci_internal
, &p_fci_internal
->data
);
1727 /* hfci can now be removed */
1728 p_fci_internal
->free(hfci
);