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 memset(p_fci_internal
, 0, sizeof(FCI_Int
));
1028 p_fci_internal
->magic
= FCI_INT_MAGIC
;
1029 p_fci_internal
->perf
= perf
;
1030 p_fci_internal
->fileplaced
= pfnfiledest
;
1031 p_fci_internal
->alloc
= pfnalloc
;
1032 p_fci_internal
->free
= pfnfree
;
1033 p_fci_internal
->open
= pfnopen
;
1034 p_fci_internal
->read
= pfnread
;
1035 p_fci_internal
->write
= pfnwrite
;
1036 p_fci_internal
->close
= pfnclose
;
1037 p_fci_internal
->seek
= pfnseek
;
1038 p_fci_internal
->delete = pfndelete
;
1039 p_fci_internal
->gettemp
= pfnfcigtf
;
1040 p_fci_internal
->ccab
= *pccab
;
1041 p_fci_internal
->pccab
= pccab
;
1042 p_fci_internal
->pv
= pv
;
1043 p_fci_internal
->data
.handle
= -1;
1044 p_fci_internal
->compress
= compress_NONE
;
1046 list_init( &p_fci_internal
->folders_list
);
1047 list_init( &p_fci_internal
->files_list
);
1048 list_init( &p_fci_internal
->blocks_list
);
1050 memcpy(p_fci_internal
->szPrevCab
, pccab
->szCab
, CB_MAX_CABINET_NAME
);
1051 memcpy(p_fci_internal
->szPrevDisk
, pccab
->szDisk
, CB_MAX_DISK_NAME
);
1053 return (HFCI
)p_fci_internal
;
1059 static BOOL
fci_flush_folder( FCI_Int
*p_fci_internal
,
1061 PFNFCIGETNEXTCABINET pfnfcignc
,
1062 PFNFCISTATUS pfnfcis
)
1065 cab_ULONG read_result
;
1066 struct folder
*folder
;
1068 if ((!pfnfcignc
) || (!pfnfcis
)) {
1069 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_BAD_ARGUMENTS
);
1073 if( p_fci_internal
->fGetNextCabInVain
&&
1074 p_fci_internal
->fNextCab
){
1075 /* internal error */
1076 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1080 /* If there was no FCIAddFile or FCIFlushFolder has already been called */
1081 /* this function will return TRUE */
1082 if( p_fci_internal
->files_size
== 0 ) {
1083 if ( p_fci_internal
->pending_data_size
!= 0 ) {
1084 /* error handling */
1085 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1091 /* FCIFlushFolder has already been called... */
1092 if (p_fci_internal
->fSplitFolder
&& p_fci_internal
->placed_files_size
!=0) {
1096 /* This can be set already, because it makes only a difference */
1097 /* when the current function exits with return FALSE */
1098 p_fci_internal
->fSplitFolder
=FALSE
;
1101 if (!add_data_block( p_fci_internal
, pfnfcis
)) return FALSE
;
1103 /* reset to get the number of data blocks of this folder which are */
1104 /* actually in this cabinet ( at least partially ) */
1105 p_fci_internal
->cDataBlocks
=0;
1107 p_fci_internal
->statusFolderTotal
= get_header_size( p_fci_internal
) +
1108 sizeof(CFFOLDER
) + p_fci_internal
->ccab
.cbReserveCFFolder
+
1109 p_fci_internal
->placed_files_size
+
1110 p_fci_internal
->folders_data_size
+ p_fci_internal
->files_size
+
1111 p_fci_internal
->pending_data_size
+ p_fci_internal
->folders_size
;
1112 p_fci_internal
->statusFolderCopied
= 0;
1114 /* report status with pfnfcis about copied size of folder */
1115 if( (*pfnfcis
)(statusFolder
, p_fci_internal
->statusFolderCopied
,
1116 p_fci_internal
->statusFolderTotal
, /* TODO total folder size */
1117 p_fci_internal
->pv
) == -1) {
1118 set_error( p_fci_internal
, FCIERR_USER_ABORT
, 0 );
1122 /* USE the variable read_result */
1123 read_result
= get_header_size( p_fci_internal
) + p_fci_internal
->folders_data_size
+
1124 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
;
1126 if(p_fci_internal
->files_size
!=0) {
1127 read_result
+= sizeof(CFFOLDER
)+p_fci_internal
->ccab
.cbReserveCFFolder
;
1130 /* Check if multiple cabinets have to be created. */
1132 /* Might be too much data for the maximum allowed cabinet size.*/
1133 /* When any further data will be added later, it might not */
1134 /* be possible to flush the cabinet, because there might */
1135 /* not be enough space to store the name of the following */
1136 /* cabinet and name of the corresponding disk. */
1137 /* So take care of this and get the name of the next cabinet */
1138 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1139 p_fci_internal
->fNextCab
==FALSE
&&
1142 p_fci_internal
->ccab
.cb
< read_result
+
1143 p_fci_internal
->pending_data_size
+
1144 p_fci_internal
->files_size
+
1145 CB_MAX_CABINET_NAME
+ /* next cabinet name */
1146 CB_MAX_DISK_NAME
/* next disk name */
1150 /* increment cabinet index */
1151 ++(p_fci_internal
->pccab
->iCab
);
1152 /* get name of next cabinet */
1153 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1154 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1155 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1156 p_fci_internal
->pv
)) {
1157 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1161 /* Skip a few lines of code. This is caught by the next if. */
1162 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1165 /* too much data for cabinet */
1166 if( (p_fci_internal
->fGetNextCabInVain
||
1167 p_fci_internal
->fNextCab
) &&
1170 p_fci_internal
->ccab
.cb
< read_result
+
1171 p_fci_internal
->pending_data_size
+
1172 p_fci_internal
->files_size
+
1173 strlen(p_fci_internal
->pccab
->szCab
)+1 + /* next cabinet name */
1174 strlen(p_fci_internal
->pccab
->szDisk
)+1 /* next disk name */
1178 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1179 p_fci_internal
->fNextCab
=TRUE
;
1181 /* return FALSE if there is not enough space left*/
1182 /* this should never happen */
1183 if (p_fci_internal
->ccab
.cb
<=
1184 p_fci_internal
->files_size
+
1186 strlen(p_fci_internal
->pccab
->szCab
)+1 + /* next cabinet name */
1187 strlen(p_fci_internal
->pccab
->szDisk
)+1 /* next disk name */
1193 /* the folder will be split across cabinets */
1194 p_fci_internal
->fSplitFolder
=TRUE
;
1197 /* this should never happen */
1198 if (p_fci_internal
->fNextCab
) {
1199 /* internal error */
1200 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1205 if (!(folder
= add_folder( p_fci_internal
))) return FALSE
;
1206 if (!add_data_to_folder( p_fci_internal
, folder
, &payload
, pfnfcis
)) return FALSE
;
1207 if (!add_files_to_folder( p_fci_internal
, folder
, payload
)) return FALSE
;
1209 /* reset CFFolder specific information */
1210 p_fci_internal
->cDataBlocks
=0;
1211 p_fci_internal
->cCompressedBytesInFolder
=0;
1219 static BOOL
fci_flush_cabinet( FCI_Int
*p_fci_internal
,
1221 PFNFCIGETNEXTCABINET pfnfcignc
,
1222 PFNFCISTATUS pfnfcis
)
1224 cab_ULONG read_result
=0;
1225 BOOL returntrue
=FALSE
;
1227 /* TODO test if fci_flush_cabinet really aborts if there was no FCIAddFile */
1229 /* when FCIFlushCabinet was or FCIAddFile hasn't been called */
1230 if( p_fci_internal
->files_size
==0 && fGetNextCab
) {
1234 if (!fci_flush_folder(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)){
1235 /* TODO set error */
1239 if(returntrue
) return TRUE
;
1241 if ( (p_fci_internal
->fSplitFolder
&& p_fci_internal
->fNextCab
==FALSE
)||
1242 (p_fci_internal
->folders_size
==0 &&
1243 (p_fci_internal
->files_size
!=0 ||
1244 p_fci_internal
->placed_files_size
!=0 )
1248 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1252 /* create the cabinet */
1253 if (!write_cabinet( p_fci_internal
, pfnfcis
)) return FALSE
;
1255 p_fci_internal
->fPrevCab
=TRUE
;
1256 /* The sections szPrevCab and szPrevDisk are not being updated, because */
1257 /* MS CABINET.DLL always puts the first cabinet name and disk into them */
1259 if (p_fci_internal
->fNextCab
) {
1260 p_fci_internal
->fNextCab
=FALSE
;
1262 if (p_fci_internal
->files_size
==0 && p_fci_internal
->pending_data_size
!=0) {
1263 /* THIS CAN NEVER HAPPEN */
1264 /* set error code */
1265 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1269 if( p_fci_internal
->fNewPrevious
) {
1270 memcpy(p_fci_internal
->szPrevCab
, p_fci_internal
->ccab
.szCab
,
1271 CB_MAX_CABINET_NAME
);
1272 memcpy(p_fci_internal
->szPrevDisk
, p_fci_internal
->ccab
.szDisk
,
1274 p_fci_internal
->fNewPrevious
=FALSE
;
1276 p_fci_internal
->ccab
= *p_fci_internal
->pccab
;
1278 /* REUSE the variable read_result */
1279 read_result
=get_header_size( p_fci_internal
);
1280 if(p_fci_internal
->files_size
!=0) {
1281 read_result
+=p_fci_internal
->ccab
.cbReserveCFFolder
;
1283 read_result
+= p_fci_internal
->pending_data_size
+
1284 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1285 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1286 sizeof(CFFOLDER
); /* set size of new CFFolder entry */
1288 /* too much data for the maximum size of a cabinet */
1289 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1290 p_fci_internal
->ccab
.cb
< read_result
) {
1291 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1294 /* Might be too much data for the maximum size of a cabinet.*/
1295 /* When any further data will be added later, it might not */
1296 /* be possible to flush the cabinet, because there might */
1297 /* not be enough space to store the name of the following */
1298 /* cabinet and name of the corresponding disk. */
1299 /* So take care of this and get the name of the next cabinet */
1300 if (p_fci_internal
->fGetNextCabInVain
==FALSE
&& (
1301 p_fci_internal
->ccab
.cb
< read_result
+
1302 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1304 /* increment cabinet index */
1305 ++(p_fci_internal
->pccab
->iCab
);
1306 /* get name of next cabinet */
1307 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1308 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1309 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1310 p_fci_internal
->pv
)) {
1311 /* error handling */
1312 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1315 /* Skip a few lines of code. This is caught by the next if. */
1316 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1319 /* too much data for cabinet */
1320 if (p_fci_internal
->fGetNextCabInVain
&& (
1321 p_fci_internal
->ccab
.cb
< read_result
+
1322 strlen(p_fci_internal
->ccab
.szCab
)+1+
1323 strlen(p_fci_internal
->ccab
.szDisk
)+1
1325 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1326 p_fci_internal
->fNextCab
=TRUE
;
1327 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1330 /* if the FolderThreshold has been reached flush the folder automatically */
1331 if (p_fci_internal
->cCompressedBytesInFolder
>= p_fci_internal
->ccab
.cbFolderThresh
)
1332 return fci_flush_folder(p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1334 if( p_fci_internal
->files_size
>0 ) {
1335 if( !fci_flush_folder(p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
) ) return FALSE
;
1336 p_fci_internal
->fNewPrevious
=TRUE
;
1339 p_fci_internal
->fNewPrevious
=FALSE
;
1340 if( p_fci_internal
->files_size
>0 || p_fci_internal
->pending_data_size
) {
1341 /* THIS MAY NEVER HAPPEN */
1342 /* set error structures */
1343 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1349 } /* end of fci_flush_cabinet */
1355 /***********************************************************************
1356 * FCIAddFile (CABINET.11)
1358 * FCIAddFile adds a file to the to be created cabinet file
1361 * hfci [I] An HFCI from FCICreate
1362 * pszSourceFile [I] A pointer to a C string which contains the name and
1363 * location of the file which will be added to the cabinet
1364 * pszFileName [I] A pointer to a C string which contains the name under
1365 * which the file will be stored in the cabinet
1366 * fExecute [I] A boolean value which indicates if the file should be
1367 * executed after extraction of self extracting
1369 * pfnfcignc [I] A pointer to a function which gets information about
1371 * pfnfcis [IO] A pointer to a function which will report status
1372 * information about the compression process
1373 * pfnfcioi [I] A pointer to a function which reports file attributes
1374 * and time and date information
1375 * typeCompress [I] Compression type
1378 * On success, returns TRUE
1379 * On failure, returns FALSE
1385 BOOL __cdecl
FCIAddFile(
1387 char *pszSourceFile
,
1390 PFNFCIGETNEXTCABINET pfnfcignc
,
1391 PFNFCISTATUS pfnfcis
,
1392 PFNFCIGETOPENINFO pfnfcigoi
,
1395 cab_ULONG read_result
;
1396 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1398 if (!p_fci_internal
) return FALSE
;
1400 if ((!pszSourceFile
) || (!pszFileName
) || (!pfnfcignc
) || (!pfnfcis
) ||
1401 (!pfnfcigoi
) || strlen(pszFileName
)>=CB_MAX_FILENAME
) {
1402 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_BAD_ARGUMENTS
);
1406 if (typeCompress
!= p_fci_internal
->compression
)
1408 if (!FCIFlushFolder( hfci
, pfnfcignc
, pfnfcis
)) return FALSE
;
1409 switch (typeCompress
)
1411 case tcompTYPE_MSZIP
:
1413 p_fci_internal
->compression
= tcompTYPE_MSZIP
;
1414 p_fci_internal
->compress
= compress_MSZIP
;
1418 FIXME( "compression %x not supported, defaulting to none\n", typeCompress
);
1420 case tcompTYPE_NONE
:
1421 p_fci_internal
->compression
= tcompTYPE_NONE
;
1422 p_fci_internal
->compress
= compress_NONE
;
1427 /* TODO check if pszSourceFile??? */
1429 if(p_fci_internal
->fGetNextCabInVain
&& p_fci_internal
->fNextCab
) {
1430 /* internal error */
1431 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1435 if(p_fci_internal
->fNextCab
) {
1436 /* internal error */
1437 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1441 /* REUSE the variable read_result */
1442 read_result
=get_header_size( p_fci_internal
) + p_fci_internal
->ccab
.cbReserveCFFolder
;
1444 read_result
+= sizeof(CFFILE
) + strlen(pszFileName
)+1 +
1445 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1446 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1447 sizeof(CFFOLDER
); /* size of new CFFolder entry */
1449 /* Might be too much data for the maximum size of a cabinet.*/
1450 /* When any further data will be added later, it might not */
1451 /* be possible to flush the cabinet, because there might */
1452 /* not be enough space to store the name of the following */
1453 /* cabinet and name of the corresponding disk. */
1454 /* So take care of this and get the name of the next cabinet */
1455 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1456 p_fci_internal
->fNextCab
==FALSE
&&
1457 ( p_fci_internal
->ccab
.cb
< read_result
+
1458 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1461 /* increment cabinet index */
1462 ++(p_fci_internal
->pccab
->iCab
);
1463 /* get name of next cabinet */
1464 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1465 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1466 p_fci_internal
->estimatedCabinetSize
, /* estimated size of cab */
1467 p_fci_internal
->pv
)) {
1468 /* error handling */
1469 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1472 /* Skip a few lines of code. This is caught by the next if. */
1473 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1476 if( p_fci_internal
->fGetNextCabInVain
&&
1477 p_fci_internal
->fNextCab
1479 /* THIS CAN NEVER HAPPEN */
1480 /* set error code */
1481 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1485 /* too much data for cabinet */
1486 if( p_fci_internal
->fGetNextCabInVain
&&
1488 p_fci_internal
->ccab
.cb
< read_result
+
1489 strlen(p_fci_internal
->pccab
->szCab
)+1+
1490 strlen(p_fci_internal
->pccab
->szDisk
)+1
1492 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1493 p_fci_internal
->fNextCab
=TRUE
;
1494 if(!fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
)) return FALSE
;
1497 if( p_fci_internal
->fNextCab
) {
1498 /* THIS MAY NEVER HAPPEN */
1499 /* set error code */
1500 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1504 if (!add_file_data( p_fci_internal
, pszSourceFile
, pszFileName
, fExecute
, pfnfcigoi
, pfnfcis
))
1507 /* REUSE the variable read_result */
1508 read_result
= get_header_size( p_fci_internal
) + p_fci_internal
->ccab
.cbReserveCFFolder
;
1509 read_result
+= p_fci_internal
->pending_data_size
+
1510 p_fci_internal
->files_size
+ p_fci_internal
->folders_data_size
+
1511 p_fci_internal
->placed_files_size
+ p_fci_internal
->folders_size
+
1512 sizeof(CFFOLDER
); /* set size of new CFFolder entry */
1514 /* too much data for the maximum size of a cabinet */
1515 /* (ignoring the unflushed data block) */
1516 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1517 p_fci_internal
->fNextCab
==FALSE
&& /* this is always the case */
1518 p_fci_internal
->ccab
.cb
< read_result
) {
1519 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1522 /* Might be too much data for the maximum size of a cabinet.*/
1523 /* When any further data will be added later, it might not */
1524 /* be possible to flush the cabinet, because there might */
1525 /* not be enough space to store the name of the following */
1526 /* cabinet and name of the corresponding disk. */
1527 /* So take care of this and get the name of the next cabinet */
1528 /* (ignoring the unflushed data block) */
1529 if( p_fci_internal
->fGetNextCabInVain
==FALSE
&&
1530 p_fci_internal
->fNextCab
==FALSE
&&
1531 ( p_fci_internal
->ccab
.cb
< read_result
+
1532 CB_MAX_CABINET_NAME
+ CB_MAX_DISK_NAME
1535 /* increment cabinet index */
1536 ++(p_fci_internal
->pccab
->iCab
);
1537 /* get name of next cabinet */
1538 p_fci_internal
->estimatedCabinetSize
=p_fci_internal
->statusFolderTotal
;
1539 if (!(*pfnfcignc
)(p_fci_internal
->pccab
,
1540 p_fci_internal
->estimatedCabinetSize
,/* estimated size of cab */
1541 p_fci_internal
->pv
)) {
1542 /* error handling */
1543 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_FUNCTION_FAILED
);
1546 /* Skip a few lines of code. This is caught by the next if. */
1547 p_fci_internal
->fGetNextCabInVain
=TRUE
;
1550 if( p_fci_internal
->fGetNextCabInVain
&&
1551 p_fci_internal
->fNextCab
1553 /* THIS CAN NEVER HAPPEN */
1554 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1558 /* too much data for cabinet */
1559 if( (p_fci_internal
->fGetNextCabInVain
||
1560 p_fci_internal
->fNextCab
) && (
1561 p_fci_internal
->ccab
.cb
< read_result
+
1562 strlen(p_fci_internal
->pccab
->szCab
)+1+
1563 strlen(p_fci_internal
->pccab
->szDisk
)+1
1566 p_fci_internal
->fGetNextCabInVain
=FALSE
;
1567 p_fci_internal
->fNextCab
=TRUE
;
1568 return fci_flush_cabinet( p_fci_internal
, FALSE
, pfnfcignc
, pfnfcis
);
1571 if( p_fci_internal
->fNextCab
) {
1572 /* THIS MAY NEVER HAPPEN */
1573 /* set error code */
1574 set_error( p_fci_internal
, FCIERR_NONE
, ERROR_GEN_FAILURE
);
1578 /* if the FolderThreshold has been reached flush the folder automatically */
1579 if (p_fci_internal
->cCompressedBytesInFolder
>= p_fci_internal
->ccab
.cbFolderThresh
)
1580 return FCIFlushFolder(hfci
, pfnfcignc
, pfnfcis
);
1583 } /* end of FCIAddFile */
1589 /***********************************************************************
1590 * FCIFlushFolder (CABINET.12)
1592 * FCIFlushFolder completes the CFFolder structure under construction.
1594 * All further data which is added by FCIAddFile will be associated to
1595 * the next CFFolder structure.
1597 * FCIFlushFolder will be called by FCIAddFile automatically if the
1598 * threshold (stored in the member cbFolderThresh of the CCAB structure
1599 * pccab passed to FCICreate) is exceeded.
1601 * FCIFlushFolder will be called by FCIFlushFolder automatically before
1602 * any data will be written into the cabinet file.
1605 * hfci [I] An HFCI from FCICreate
1606 * pfnfcignc [I] A pointer to a function which gets information about
1608 * pfnfcis [IO] A pointer to a function which will report status
1609 * information about the compression process
1612 * On success, returns TRUE
1613 * On failure, returns FALSE
1619 BOOL __cdecl
FCIFlushFolder(
1621 PFNFCIGETNEXTCABINET pfnfcignc
,
1622 PFNFCISTATUS pfnfcis
)
1624 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1626 if (!p_fci_internal
) return FALSE
;
1627 return fci_flush_folder(p_fci_internal
,FALSE
,pfnfcignc
,pfnfcis
);
1632 /***********************************************************************
1633 * FCIFlushCabinet (CABINET.13)
1635 * FCIFlushCabinet stores the data which has been added by FCIAddFile
1636 * into the cabinet file. If the maximum cabinet size (stored in the
1637 * member cb of the CCAB structure pccab passed to FCICreate) has been
1638 * exceeded FCIFlushCabinet will be called automatic by FCIAddFile.
1639 * The remaining data still has to be flushed manually by calling
1642 * After FCIFlushCabinet has been called (manually) FCIAddFile must
1643 * NOT be called again. Then hfci has to be released by FCIDestroy.
1646 * hfci [I] An HFCI from FCICreate
1647 * fGetNextCab [I] Whether you want to add additional files to a
1648 * cabinet set (TRUE) or whether you want to
1649 * finalize it (FALSE)
1650 * pfnfcignc [I] A pointer to a function which gets information about
1652 * pfnfcis [IO] A pointer to a function which will report status
1653 * information about the compression process
1656 * On success, returns TRUE
1657 * On failure, returns FALSE
1663 BOOL __cdecl
FCIFlushCabinet(
1666 PFNFCIGETNEXTCABINET pfnfcignc
,
1667 PFNFCISTATUS pfnfcis
)
1669 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1671 if (!p_fci_internal
) return FALSE
;
1673 if(!fci_flush_cabinet(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)) return FALSE
;
1675 while( p_fci_internal
->files_size
>0 ||
1676 p_fci_internal
->placed_files_size
>0 ) {
1677 if(!fci_flush_cabinet(p_fci_internal
,fGetNextCab
,pfnfcignc
,pfnfcis
)) return FALSE
;
1684 /***********************************************************************
1685 * FCIDestroy (CABINET.14)
1687 * Frees a handle created by FCICreate.
1688 * Only reason for failure would be an invalid handle.
1691 * hfci [I] The HFCI to free
1697 BOOL __cdecl
FCIDestroy(HFCI hfci
)
1699 struct folder
*folder
, *folder_next
;
1700 struct file
*file
, *file_next
;
1701 struct data_block
*block
, *block_next
;
1702 FCI_Int
*p_fci_internal
= get_fci_ptr( hfci
);
1704 if (!p_fci_internal
) return FALSE
;
1706 /* before hfci can be removed all temporary files must be closed */
1708 p_fci_internal
->magic
= 0;
1710 LIST_FOR_EACH_ENTRY_SAFE( folder
, folder_next
, &p_fci_internal
->folders_list
, struct folder
, entry
)
1712 free_folder( p_fci_internal
, folder
);
1714 LIST_FOR_EACH_ENTRY_SAFE( file
, file_next
, &p_fci_internal
->files_list
, struct file
, entry
)
1716 free_file( p_fci_internal
, file
);
1718 LIST_FOR_EACH_ENTRY_SAFE( block
, block_next
, &p_fci_internal
->blocks_list
, struct data_block
, entry
)
1720 free_data_block( p_fci_internal
, block
);
1723 close_temp_file( p_fci_internal
, &p_fci_internal
->data
);
1725 /* hfci can now be removed */
1726 p_fci_internal
->free(hfci
);