2002-11-19 01:47:12 +01:00
|
|
|
/*
|
|
|
|
* File Compression Interface
|
|
|
|
*
|
|
|
|
* Copyright 2002 Patrik Stridvall
|
2005-05-14 13:06:10 +02:00
|
|
|
* Copyright 2005 Gerold Jens Wucherpfennig
|
2011-02-14 20:32:19 +01:00
|
|
|
* Copyright 2011 Alexandre Julliard
|
2002-11-19 01:47:12 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-11-19 01:47:12 +01:00
|
|
|
*/
|
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
There is still some work to be done:
|
|
|
|
|
2006-02-24 14:28:20 +01:00
|
|
|
- unknown behaviour if files>=2GB or cabinet >=4GB
|
2005-06-24 14:16:37 +02:00
|
|
|
- check if the maximum size for a cabinet is too small to store any data
|
|
|
|
- call pfnfcignc on exactly the same position as MS FCIAddFile in every case
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-11-19 01:47:12 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-02-15 20:08:44 +01:00
|
|
|
#include <assert.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2005-06-24 14:16:37 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2011-02-18 00:15:44 +01:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
# include <zlib.h>
|
|
|
|
#endif
|
2003-09-06 01:08:26 +02:00
|
|
|
|
2002-11-19 01:47:12 +01:00
|
|
|
#include "windef.h"
|
2002-12-02 20:00:59 +01:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
2005-06-29 13:19:47 +02:00
|
|
|
#include "winternl.h"
|
2002-11-19 01:47:12 +01:00
|
|
|
#include "fci.h"
|
2005-05-14 13:06:10 +02:00
|
|
|
#include "cabinet.h"
|
2011-02-14 20:05:46 +01:00
|
|
|
#include "wine/list.h"
|
2002-11-19 01:47:12 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(cabinet);
|
2005-07-10 19:46:34 +02:00
|
|
|
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
#define fci_endian_ulong(x) RtlUlongByteSwap(x)
|
|
|
|
#define fci_endian_uword(x) RtlUshortByteSwap(x)
|
|
|
|
#else
|
|
|
|
#define fci_endian_ulong(x) (x)
|
|
|
|
#define fci_endian_uword(x) (x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
typedef struct {
|
|
|
|
cab_UBYTE signature[4]; /* !CAB for unfinished cabinets else MSCF */
|
|
|
|
cab_ULONG reserved1;
|
|
|
|
cab_ULONG cbCabinet; /* size of the cabinet file in bytes*/
|
|
|
|
cab_ULONG reserved2;
|
|
|
|
cab_ULONG coffFiles; /* offset to first CFFILE section */
|
|
|
|
cab_ULONG reserved3;
|
|
|
|
cab_UBYTE versionMinor; /* 3 */
|
|
|
|
cab_UBYTE versionMajor; /* 1 */
|
|
|
|
cab_UWORD cFolders; /* number of CFFOLDER entries in the cabinet*/
|
|
|
|
cab_UWORD cFiles; /* number of CFFILE entries in the cabinet*/
|
2008-01-17 00:04:38 +01:00
|
|
|
cab_UWORD flags; /* 1=prev cab, 2=next cabinet, 4=reserved sections*/
|
2005-06-24 14:16:37 +02:00
|
|
|
cab_UWORD setID; /* identification number of all cabinets in a set*/
|
|
|
|
cab_UWORD iCabinet; /* number of the cabinet in a set */
|
|
|
|
/* additional area if "flags" were set*/
|
|
|
|
} CFHEADER; /* minimum 36 bytes */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
cab_ULONG coffCabStart; /* offset to the folder's first CFDATA section */
|
|
|
|
cab_UWORD cCFData; /* number of this folder's CFDATA sections */
|
|
|
|
cab_UWORD typeCompress; /* compression type of data in CFDATA section*/
|
|
|
|
/* additional area if reserve flag was set */
|
2008-01-16 12:20:50 +01:00
|
|
|
} CFFOLDER; /* minimum 8 bytes */
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
cab_ULONG cbFile; /* size of the uncompressed file in bytes */
|
|
|
|
cab_ULONG uoffFolderStart; /* offset of the uncompressed file in the folder */
|
|
|
|
cab_UWORD iFolder; /* number of folder in the cabinet 0=first */
|
|
|
|
/* for special values see below this structure*/
|
|
|
|
cab_UWORD date; /* last modification date*/
|
|
|
|
cab_UWORD time; /* last modification time*/
|
|
|
|
cab_UWORD attribs; /* DOS fat attributes and UTF indicator */
|
|
|
|
/* ... and a C string with the name of the file */
|
|
|
|
} CFFILE; /* 16 bytes + name of file */
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
cab_ULONG csum; /* checksum of this entry*/
|
|
|
|
cab_UWORD cbData; /* number of compressed bytes */
|
|
|
|
cab_UWORD cbUncomp; /* number of bytes when data is uncompressed */
|
|
|
|
/* optional reserved area */
|
|
|
|
/* compressed data */
|
|
|
|
} CFDATA;
|
|
|
|
|
2011-02-15 12:58:07 +01:00
|
|
|
struct temp_file
|
|
|
|
{
|
|
|
|
INT_PTR handle;
|
|
|
|
char name[CB_MAX_FILENAME];
|
|
|
|
};
|
|
|
|
|
2011-02-14 20:05:46 +01:00
|
|
|
struct folder
|
|
|
|
{
|
2011-02-17 16:48:19 +01:00
|
|
|
struct list entry;
|
|
|
|
struct list files_list;
|
|
|
|
struct list blocks_list;
|
|
|
|
struct temp_file data;
|
|
|
|
cab_ULONG data_start;
|
|
|
|
cab_UWORD data_count;
|
2011-02-17 21:08:28 +01:00
|
|
|
TCOMP compression;
|
2011-02-14 20:05:46 +01:00
|
|
|
};
|
2005-06-24 14:16:37 +02:00
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
struct file
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
cab_ULONG size; /* uncompressed size */
|
|
|
|
cab_ULONG offset; /* offset in folder */
|
|
|
|
cab_UWORD folder; /* index of folder */
|
|
|
|
cab_UWORD date;
|
|
|
|
cab_UWORD time;
|
|
|
|
cab_UWORD attribs;
|
|
|
|
char name[1];
|
|
|
|
};
|
|
|
|
|
2011-02-16 00:20:49 +01:00
|
|
|
struct data_block
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
cab_UWORD compressed;
|
|
|
|
cab_UWORD uncompressed;
|
|
|
|
};
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
typedef struct FCI_Int
|
2011-02-04 11:35:18 +01:00
|
|
|
{
|
|
|
|
unsigned int magic;
|
2011-02-15 14:11:22 +01:00
|
|
|
PERF perf;
|
2011-02-04 11:35:18 +01:00
|
|
|
PFNFCIFILEPLACED fileplaced;
|
|
|
|
PFNFCIALLOC alloc;
|
|
|
|
PFNFCIFREE free;
|
|
|
|
PFNFCIOPEN open;
|
|
|
|
PFNFCIREAD read;
|
|
|
|
PFNFCIWRITE write;
|
|
|
|
PFNFCICLOSE close;
|
|
|
|
PFNFCISEEK seek;
|
|
|
|
PFNFCIDELETE delete;
|
|
|
|
PFNFCIGETTEMPFILE gettemp;
|
2011-02-15 14:11:22 +01:00
|
|
|
CCAB ccab;
|
2011-02-04 11:35:18 +01:00
|
|
|
PCCAB pccab;
|
|
|
|
BOOL fPrevCab;
|
|
|
|
BOOL fNextCab;
|
|
|
|
BOOL fSplitFolder;
|
|
|
|
cab_ULONG statusFolderCopied;
|
|
|
|
cab_ULONG statusFolderTotal;
|
|
|
|
BOOL fGetNextCabInVain;
|
|
|
|
void *pv;
|
2011-02-18 11:32:35 +01:00
|
|
|
char szPrevCab[CB_MAX_CABINET_NAME]; /* previous cabinet name */
|
|
|
|
char szPrevDisk[CB_MAX_DISK_NAME]; /* disk name of previous cabinet */
|
|
|
|
unsigned char data_in[CAB_BLOCKMAX]; /* uncompressed data blocks */
|
|
|
|
unsigned char data_out[2 * CAB_BLOCKMAX]; /* compressed data blocks */
|
2011-02-04 11:35:18 +01:00
|
|
|
cab_UWORD cdata_in;
|
|
|
|
ULONG cCompressedBytesInFolder;
|
|
|
|
cab_UWORD cFolders;
|
|
|
|
cab_UWORD cFiles;
|
|
|
|
cab_ULONG cDataBlocks;
|
|
|
|
cab_ULONG cbFileRemainer; /* uncompressed, yet to be written data */
|
|
|
|
/* of spanned file of a spanning folder of a spanning cabinet */
|
2011-02-17 16:48:19 +01:00
|
|
|
struct temp_file data;
|
2011-02-04 11:35:18 +01:00
|
|
|
BOOL fNewPrevious;
|
|
|
|
cab_ULONG estimatedCabinetSize;
|
2011-02-14 20:05:46 +01:00
|
|
|
struct list folders_list;
|
2011-02-14 20:32:19 +01:00
|
|
|
struct list files_list;
|
2011-02-16 00:20:49 +01:00
|
|
|
struct list blocks_list;
|
2011-02-14 20:05:46 +01:00
|
|
|
cab_ULONG folders_size;
|
2011-02-17 21:23:03 +01:00
|
|
|
cab_ULONG files_size; /* size of files not yet assigned to a folder */
|
|
|
|
cab_ULONG placed_files_size; /* size of files already placed into a folder */
|
|
|
|
cab_ULONG pending_data_size; /* size of data not yet assigned to a folder */
|
|
|
|
cab_ULONG folders_data_size; /* total size of data contained in the current folders */
|
2011-02-17 21:08:28 +01:00
|
|
|
TCOMP compression;
|
|
|
|
cab_UWORD (*compress)(struct FCI_Int *);
|
2011-02-04 11:35:18 +01:00
|
|
|
} FCI_Int;
|
|
|
|
|
|
|
|
#define FCI_INT_MAGIC 0xfcfcfc05
|
|
|
|
|
2011-02-04 11:20:12 +01:00
|
|
|
static void set_error( FCI_Int *fci, int oper, int err )
|
|
|
|
{
|
|
|
|
fci->perf->erfOper = oper;
|
|
|
|
fci->perf->erfType = err;
|
|
|
|
fci->perf->fError = TRUE;
|
|
|
|
if (err) SetLastError( err );
|
|
|
|
}
|
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
static FCI_Int *get_fci_ptr( HFCI hfci )
|
|
|
|
{
|
|
|
|
FCI_Int *fci= (FCI_Int *)hfci;
|
|
|
|
|
2011-02-04 11:35:18 +01:00
|
|
|
if (!fci || !fci->magic == FCI_INT_MAGIC)
|
2011-02-04 11:32:46 +01:00
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_HANDLE );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return fci;
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:11:22 +01:00
|
|
|
/* compute the cabinet header size */
|
|
|
|
static cab_ULONG get_header_size( FCI_Int *fci )
|
|
|
|
{
|
|
|
|
cab_ULONG ret = sizeof(CFHEADER) + fci->ccab.cbReserveCFHeader;
|
|
|
|
|
|
|
|
if (fci->ccab.cbReserveCFHeader || fci->ccab.cbReserveCFFolder || fci->ccab.cbReserveCFData)
|
|
|
|
ret += 4;
|
|
|
|
|
|
|
|
if (fci->fPrevCab)
|
|
|
|
ret += strlen( fci->szPrevCab ) + 1 + strlen( fci->szPrevDisk ) + 1;
|
|
|
|
|
|
|
|
if (fci->fNextCab)
|
|
|
|
ret += strlen( fci->pccab->szCab ) + 1 + strlen( fci->pccab->szDisk ) + 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-15 12:58:07 +01:00
|
|
|
static BOOL create_temp_file( FCI_Int *fci, struct temp_file *file )
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!fci->gettemp( file->name, CB_MAX_FILENAME, fci->pv ))
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, ERROR_FUNCTION_FAILED );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((file->handle = fci->open( file->name, _O_RDWR | _O_CREAT | _O_EXCL | _O_BINARY,
|
|
|
|
_S_IREAD | _S_IWRITE, &err, fci->pv )) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL close_temp_file( FCI_Int *fci, struct temp_file *file )
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (file->handle == -1) return TRUE;
|
|
|
|
if (fci->close( file->handle, &err, fci->pv ) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
file->handle = -1;
|
|
|
|
if (fci->delete( file->name, &err, fci->pv ) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
static struct file *add_file( FCI_Int *fci, const char *filename )
|
|
|
|
{
|
|
|
|
unsigned int size = FIELD_OFFSET( struct file, name[strlen(filename) + 1] );
|
|
|
|
struct file *file = fci->alloc( size );
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_ALLOC_FAIL, ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
file->size = 0;
|
|
|
|
file->offset = fci->cDataBlocks * CAB_BLOCKMAX + fci->cdata_in;
|
|
|
|
file->folder = fci->cFolders;
|
|
|
|
file->date = 0;
|
|
|
|
file->time = 0;
|
|
|
|
file->attribs = 0;
|
|
|
|
strcpy( file->name, filename );
|
|
|
|
list_add_tail( &fci->files_list, &file->entry );
|
2011-02-17 21:08:28 +01:00
|
|
|
fci->files_size += sizeof(CFFILE) + strlen(filename) + 1;
|
2011-02-14 20:32:19 +01:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file *copy_file( FCI_Int *fci, const struct file *orig )
|
|
|
|
{
|
|
|
|
unsigned int size = FIELD_OFFSET( struct file, name[strlen(orig->name) + 1] );
|
|
|
|
struct file *file = fci->alloc( size );
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_ALLOC_FAIL, ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy( file, orig, size );
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_file( FCI_Int *fci, struct file *file )
|
|
|
|
{
|
|
|
|
list_remove( &file->entry );
|
|
|
|
fci->free( file );
|
|
|
|
}
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
/* create a new data block for the data in fci->data_in */
|
|
|
|
static BOOL add_data_block( FCI_Int *fci, PFNFCISTATUS status_callback )
|
2011-02-17 16:48:19 +01:00
|
|
|
{
|
2011-02-17 21:08:28 +01:00
|
|
|
int err;
|
|
|
|
struct data_block *block;
|
2011-02-17 16:48:19 +01:00
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
if (!fci->cdata_in) return TRUE;
|
|
|
|
|
2011-02-17 21:23:03 +01:00
|
|
|
if (fci->data.handle == -1 && !create_temp_file( fci, &fci->data )) return FALSE;
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
if (!(block = fci->alloc( sizeof(*block) )))
|
2011-02-17 16:48:19 +01:00
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_ALLOC_FAIL, ERROR_NOT_ENOUGH_MEMORY );
|
2011-02-17 21:08:28 +01:00
|
|
|
return FALSE;
|
2011-02-17 16:48:19 +01:00
|
|
|
}
|
2011-02-17 21:08:28 +01:00
|
|
|
block->uncompressed = fci->cdata_in;
|
|
|
|
block->compressed = fci->compress( fci );
|
|
|
|
|
|
|
|
if (fci->write( fci->data.handle, fci->data_out,
|
|
|
|
block->compressed, &err, fci->pv ) != block->compressed)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, err );
|
|
|
|
fci->free( block );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fci->cdata_in = 0;
|
2011-02-17 21:23:03 +01:00
|
|
|
fci->pending_data_size += sizeof(CFDATA) + fci->ccab.cbReserveCFData + block->compressed;
|
2011-02-17 21:08:28 +01:00
|
|
|
fci->cCompressedBytesInFolder += block->compressed;
|
2011-02-17 16:48:19 +01:00
|
|
|
fci->cDataBlocks++;
|
2011-02-17 21:08:28 +01:00
|
|
|
list_add_tail( &fci->blocks_list, &block->entry );
|
|
|
|
|
|
|
|
if (status_callback( statusFile, block->compressed, block->uncompressed, fci->pv ) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_USER_ABORT, 0 );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add compressed blocks for all the data that can be read from the file */
|
|
|
|
static BOOL add_file_data( FCI_Int *fci, char *sourcefile, char *filename, BOOL execute,
|
|
|
|
PFNFCIGETOPENINFO get_open_info, PFNFCISTATUS status_callback )
|
|
|
|
{
|
|
|
|
int err, len;
|
|
|
|
INT_PTR handle;
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
if (!(file = add_file( fci, filename ))) return FALSE;
|
|
|
|
|
|
|
|
handle = get_open_info( sourcefile, &file->date, &file->time, &file->attribs, &err, fci->pv );
|
|
|
|
if (handle == -1)
|
|
|
|
{
|
|
|
|
free_file( fci, file );
|
|
|
|
set_error( fci, FCIERR_OPEN_SRC, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (execute) file->attribs |= _A_EXEC;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
len = fci->read( handle, fci->data_in + fci->cdata_in,
|
|
|
|
CAB_BLOCKMAX - fci->cdata_in, &err, fci->pv );
|
|
|
|
if (!len) break;
|
|
|
|
|
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_READ_SRC, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
file->size += len;
|
|
|
|
fci->cdata_in += len;
|
|
|
|
if (fci->cdata_in == CAB_BLOCKMAX && !add_data_block( fci, status_callback )) return FALSE;
|
|
|
|
}
|
|
|
|
fci->close( handle, &err, fci->pv );
|
|
|
|
return TRUE;
|
2011-02-17 16:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void free_data_block( FCI_Int *fci, struct data_block *block )
|
|
|
|
{
|
|
|
|
list_remove( &block->entry );
|
|
|
|
fci->free( block );
|
|
|
|
}
|
|
|
|
|
2011-02-14 20:05:46 +01:00
|
|
|
static struct folder *add_folder( FCI_Int *fci )
|
|
|
|
{
|
|
|
|
struct folder *folder = fci->alloc( sizeof(*folder) );
|
|
|
|
|
|
|
|
if (!folder)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_ALLOC_FAIL, ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-17 16:48:19 +01:00
|
|
|
folder->data.handle = -1;
|
|
|
|
folder->data_start = fci->folders_data_size;
|
|
|
|
folder->data_count = 0;
|
2011-02-17 21:08:28 +01:00
|
|
|
folder->compression = fci->compression;
|
2011-02-14 20:32:19 +01:00
|
|
|
list_init( &folder->files_list );
|
2011-02-17 16:48:19 +01:00
|
|
|
list_init( &folder->blocks_list );
|
2011-02-14 20:05:46 +01:00
|
|
|
list_add_tail( &fci->folders_list, &folder->entry );
|
2011-02-15 14:11:22 +01:00
|
|
|
fci->folders_size += sizeof(CFFOLDER) + fci->ccab.cbReserveCFFolder;
|
2011-02-14 20:05:46 +01:00
|
|
|
fci->cFolders++;
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
static void free_folder( FCI_Int *fci, struct folder *folder )
|
|
|
|
{
|
2011-02-17 16:48:19 +01:00
|
|
|
struct file *file, *file_next;
|
|
|
|
struct data_block *block, *block_next;
|
2011-02-14 20:32:19 +01:00
|
|
|
|
2011-02-17 16:48:19 +01:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( file, file_next, &folder->files_list, struct file, entry )
|
|
|
|
free_file( fci, file );
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( block, block_next, &folder->blocks_list, struct data_block, entry )
|
|
|
|
free_data_block( fci, block );
|
|
|
|
close_temp_file( fci, &folder->data );
|
2011-02-14 20:32:19 +01:00
|
|
|
list_remove( &folder->entry );
|
|
|
|
fci->free( folder );
|
|
|
|
}
|
|
|
|
|
2011-02-16 00:20:49 +01:00
|
|
|
/* reset state for the next cabinet file once the current one has been flushed */
|
|
|
|
static void reset_cabinet( FCI_Int *fci )
|
|
|
|
{
|
|
|
|
struct folder *folder, *folder_next;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( folder, folder_next, &fci->folders_list, struct folder, entry )
|
|
|
|
free_folder( fci, folder );
|
|
|
|
|
|
|
|
fci->cFolders = 0;
|
|
|
|
fci->cFiles = 0;
|
|
|
|
fci->folders_size = 0;
|
|
|
|
fci->placed_files_size = 0;
|
2011-02-17 16:48:19 +01:00
|
|
|
fci->folders_data_size = 0;
|
2011-02-16 00:20:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cab_ULONG fci_get_checksum( const void *pv, UINT cb, cab_ULONG seed )
|
|
|
|
{
|
|
|
|
cab_ULONG csum;
|
|
|
|
cab_ULONG ul;
|
|
|
|
int cUlong;
|
|
|
|
const BYTE *pb;
|
|
|
|
|
|
|
|
csum = seed;
|
|
|
|
cUlong = cb / 4;
|
|
|
|
pb = pv;
|
|
|
|
|
|
|
|
while (cUlong-- > 0) {
|
|
|
|
ul = *pb++;
|
|
|
|
ul |= (((cab_ULONG)(*pb++)) << 8);
|
|
|
|
ul |= (((cab_ULONG)(*pb++)) << 16);
|
|
|
|
ul |= (((cab_ULONG)(*pb++)) << 24);
|
|
|
|
csum ^= ul;
|
|
|
|
}
|
|
|
|
|
|
|
|
ul = 0;
|
|
|
|
switch (cb % 4) {
|
|
|
|
case 3:
|
|
|
|
ul |= (((ULONG)(*pb++)) << 16);
|
|
|
|
case 2:
|
|
|
|
ul |= (((ULONG)(*pb++)) << 8);
|
|
|
|
case 1:
|
|
|
|
ul |= *pb;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
csum ^= ul;
|
|
|
|
|
|
|
|
return csum;
|
|
|
|
}
|
|
|
|
|
2011-02-17 16:48:19 +01:00
|
|
|
/* copy all remaining data block to a new temp file */
|
|
|
|
static BOOL copy_data_blocks( FCI_Int *fci, INT_PTR handle, cab_ULONG start_pos,
|
|
|
|
struct temp_file *temp, PFNFCISTATUS status_callback )
|
|
|
|
{
|
|
|
|
struct data_block *block;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (fci->seek( handle, start_pos, SEEK_SET, &err, fci->pv ) != start_pos)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!create_temp_file( fci, temp )) return FALSE;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( block, &fci->blocks_list, struct data_block, entry )
|
|
|
|
{
|
|
|
|
if (fci->read( handle, fci->data_out, block->compressed,
|
|
|
|
&err, fci->pv ) != block->compressed)
|
|
|
|
{
|
|
|
|
close_temp_file( fci, temp );
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (fci->write( temp->handle, fci->data_out, block->compressed,
|
|
|
|
&err, fci->pv ) != block->compressed)
|
|
|
|
{
|
|
|
|
close_temp_file( fci, temp );
|
|
|
|
set_error( fci, FCIERR_TEMP_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-02-17 21:23:03 +01:00
|
|
|
fci->pending_data_size += sizeof(CFDATA) + fci->ccab.cbReserveCFData + block->compressed;
|
2011-02-17 16:48:19 +01:00
|
|
|
fci->statusFolderCopied += block->compressed;
|
|
|
|
|
|
|
|
if (status_callback( statusFolder, fci->statusFolderCopied,
|
|
|
|
fci->statusFolderTotal, fci->pv) == -1)
|
|
|
|
{
|
|
|
|
close_temp_file( fci, temp );
|
|
|
|
set_error( fci, FCIERR_USER_ABORT, 0 );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-14 20:05:46 +01:00
|
|
|
/* write all folders to disk and remove them from the list */
|
2011-02-14 20:32:19 +01:00
|
|
|
static BOOL write_folders( FCI_Int *fci, INT_PTR handle, cab_ULONG header_size, PFNFCISTATUS status_callback )
|
2011-02-14 20:05:46 +01:00
|
|
|
{
|
2011-02-16 00:20:49 +01:00
|
|
|
struct folder *folder;
|
2011-02-14 20:05:46 +01:00
|
|
|
int err;
|
2011-02-18 11:32:35 +01:00
|
|
|
CFFOLDER *cffolder = (CFFOLDER *)fci->data_out;
|
2011-02-16 00:20:49 +01:00
|
|
|
cab_ULONG folder_size = sizeof(CFFOLDER) + fci->ccab.cbReserveCFFolder;
|
2011-02-14 20:05:46 +01:00
|
|
|
|
|
|
|
memset( cffolder, 0, folder_size );
|
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
/* write the folders */
|
|
|
|
LIST_FOR_EACH_ENTRY( folder, &fci->folders_list, struct folder, entry )
|
2011-02-14 20:05:46 +01:00
|
|
|
{
|
|
|
|
cffolder->coffCabStart = fci_endian_ulong( folder->data_start + header_size );
|
|
|
|
cffolder->cCFData = fci_endian_uword( folder->data_count );
|
|
|
|
cffolder->typeCompress = fci_endian_uword( folder->compression );
|
|
|
|
if (fci->write( handle, cffolder, folder_size, &err, fci->pv ) != folder_size)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
2011-02-18 11:32:35 +01:00
|
|
|
return FALSE;
|
2011-02-14 20:05:46 +01:00
|
|
|
}
|
2011-02-14 20:32:19 +01:00
|
|
|
}
|
2011-02-18 11:32:35 +01:00
|
|
|
return TRUE;
|
2011-02-16 00:20:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write all the files to the cabinet file */
|
|
|
|
static BOOL write_files( FCI_Int *fci, INT_PTR handle, PFNFCISTATUS status_callback )
|
|
|
|
{
|
|
|
|
cab_ULONG file_size;
|
|
|
|
struct folder *folder;
|
|
|
|
struct file *file;
|
|
|
|
int err;
|
2011-02-18 11:32:35 +01:00
|
|
|
CFFILE *cffile = (CFFILE *)fci->data_out;
|
2011-02-16 00:20:49 +01:00
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( folder, &fci->folders_list, struct folder, entry )
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH_ENTRY( file, &folder->files_list, struct file, entry )
|
|
|
|
{
|
|
|
|
cffile->cbFile = fci_endian_ulong( file->size );
|
|
|
|
cffile->uoffFolderStart = fci_endian_ulong( file->offset );
|
|
|
|
cffile->iFolder = fci_endian_uword( file->folder );
|
|
|
|
cffile->date = fci_endian_uword( file->date );
|
|
|
|
cffile->time = fci_endian_uword( file->time );
|
|
|
|
cffile->attribs = fci_endian_uword( file->attribs );
|
|
|
|
lstrcpynA( (char *)(cffile + 1), file->name, CB_MAX_FILENAME );
|
|
|
|
file_size = sizeof(CFFILE) + strlen( (char *)(cffile + 1) ) + 1;
|
|
|
|
if (fci->write( handle, cffile, file_size, &err, fci->pv ) != file_size)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
2011-02-18 11:32:35 +01:00
|
|
|
return FALSE;
|
2011-02-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
if (!fci->fSplitFolder)
|
|
|
|
{
|
|
|
|
fci->statusFolderCopied = 0;
|
|
|
|
/* TODO TEST THIS further */
|
2011-02-17 16:48:19 +01:00
|
|
|
fci->statusFolderTotal = fci->folders_data_size + fci->placed_files_size;
|
2011-02-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
fci->statusFolderCopied += file_size;
|
|
|
|
/* report status about copied size of folder */
|
|
|
|
if (status_callback( statusFolder, fci->statusFolderCopied,
|
|
|
|
fci->statusFolderTotal, fci->pv ) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_USER_ABORT, 0 );
|
2011-02-18 11:32:35 +01:00
|
|
|
return FALSE;
|
2011-02-14 20:32:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-18 11:32:35 +01:00
|
|
|
return TRUE;
|
2011-02-14 20:05:46 +01:00
|
|
|
}
|
|
|
|
|
2011-02-16 00:20:49 +01:00
|
|
|
/* write all data blocks to the cabinet file */
|
|
|
|
static BOOL write_data_blocks( FCI_Int *fci, INT_PTR handle, PFNFCISTATUS status_callback )
|
|
|
|
{
|
2011-02-17 16:48:19 +01:00
|
|
|
struct folder *folder;
|
2011-02-16 00:20:49 +01:00
|
|
|
struct data_block *block;
|
|
|
|
int err, len;
|
|
|
|
CFDATA *cfdata;
|
|
|
|
void *data;
|
|
|
|
cab_UWORD header_size;
|
|
|
|
|
|
|
|
header_size = sizeof(CFDATA) + fci->ccab.cbReserveCFData;
|
|
|
|
cfdata = (CFDATA *)fci->data_out;
|
|
|
|
memset( cfdata, 0, header_size );
|
|
|
|
data = (char *)cfdata + header_size;
|
|
|
|
|
2011-02-17 16:48:19 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( folder, &fci->folders_list, struct folder, entry )
|
2011-02-16 00:20:49 +01:00
|
|
|
{
|
2011-02-17 16:48:19 +01:00
|
|
|
if (fci->seek( folder->data.handle, 0, SEEK_SET, &err, fci->pv ) != 0)
|
2011-02-16 00:20:49 +01:00
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-02-17 16:48:19 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( block, &folder->blocks_list, struct data_block, entry )
|
2011-02-16 00:20:49 +01:00
|
|
|
{
|
2011-02-17 16:48:19 +01:00
|
|
|
len = fci->read( folder->data.handle, data, block->compressed, &err, fci->pv );
|
|
|
|
if (len != block->compressed) return FALSE;
|
|
|
|
|
|
|
|
cfdata->cbData = fci_endian_uword( block->compressed );
|
|
|
|
cfdata->cbUncomp = fci_endian_uword( block->uncompressed );
|
|
|
|
cfdata->csum = fci_endian_ulong( fci_get_checksum( &cfdata->cbData,
|
|
|
|
header_size - FIELD_OFFSET(CFDATA, cbData),
|
|
|
|
fci_get_checksum( data, len, 0 )));
|
|
|
|
|
|
|
|
fci->statusFolderCopied += len;
|
|
|
|
len += header_size;
|
|
|
|
if (fci->write( handle, fci->data_out, len, &err, fci->pv ) != len)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (status_callback( statusFolder, fci->statusFolderCopied, fci->statusFolderTotal, fci->pv) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_USER_ABORT, 0 );
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-02-16 00:20:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-15 20:08:44 +01:00
|
|
|
/* write the cabinet file to disk */
|
2011-02-16 00:20:49 +01:00
|
|
|
static BOOL write_cabinet( FCI_Int *fci, PFNFCISTATUS status_callback )
|
2011-02-15 20:08:44 +01:00
|
|
|
{
|
|
|
|
char filename[CB_MAX_CAB_PATH + CB_MAX_CABINET_NAME];
|
|
|
|
int err;
|
2011-02-16 00:20:49 +01:00
|
|
|
char *ptr;
|
2011-02-15 20:08:44 +01:00
|
|
|
INT_PTR handle;
|
2011-02-18 11:32:35 +01:00
|
|
|
CFHEADER *cfheader = (CFHEADER *)fci->data_out;
|
2011-02-15 20:08:44 +01:00
|
|
|
cab_UWORD flags = 0;
|
|
|
|
cab_ULONG header_size = get_header_size( fci );
|
2011-02-18 11:32:35 +01:00
|
|
|
cab_ULONG total_size = header_size + fci->folders_size +
|
|
|
|
fci->placed_files_size + fci->folders_data_size;
|
2011-02-15 20:08:44 +01:00
|
|
|
|
2011-02-18 11:32:35 +01:00
|
|
|
assert( header_size <= sizeof(fci->data_out) );
|
2011-02-15 20:08:44 +01:00
|
|
|
memset( cfheader, 0, header_size );
|
|
|
|
|
|
|
|
if (fci->fPrevCab) flags |= cfheadPREV_CABINET;
|
|
|
|
if (fci->fNextCab) flags |= cfheadNEXT_CABINET;
|
|
|
|
if (fci->ccab.cbReserveCFHeader || fci->ccab.cbReserveCFFolder || fci->ccab.cbReserveCFData)
|
|
|
|
flags |= cfheadRESERVE_PRESENT;
|
|
|
|
|
|
|
|
memcpy( cfheader->signature, "!CAB", 4 );
|
2011-02-16 00:20:49 +01:00
|
|
|
cfheader->cbCabinet = fci_endian_ulong( total_size );
|
2011-02-15 20:08:44 +01:00
|
|
|
cfheader->coffFiles = fci_endian_ulong( header_size + fci->folders_size );
|
|
|
|
cfheader->versionMinor = 3;
|
|
|
|
cfheader->versionMajor = 1;
|
|
|
|
cfheader->cFolders = fci_endian_uword( fci->cFolders );
|
|
|
|
cfheader->cFiles = fci_endian_uword( fci->cFiles );
|
|
|
|
cfheader->flags = fci_endian_uword( flags );
|
|
|
|
cfheader->setID = fci_endian_uword( fci->ccab.setID );
|
2011-02-19 12:50:09 +01:00
|
|
|
cfheader->iCabinet = fci_endian_uword( fci->ccab.iCab );
|
2011-02-15 20:08:44 +01:00
|
|
|
ptr = (char *)(cfheader + 1);
|
|
|
|
|
|
|
|
if (flags & cfheadRESERVE_PRESENT)
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
cab_UWORD cbCFHeader;
|
|
|
|
cab_UBYTE cbCFFolder;
|
|
|
|
cab_UBYTE cbCFData;
|
|
|
|
} *reserve = (void *)ptr;
|
|
|
|
|
|
|
|
reserve->cbCFHeader = fci_endian_uword( fci->ccab.cbReserveCFHeader );
|
|
|
|
reserve->cbCFFolder = fci->ccab.cbReserveCFFolder;
|
|
|
|
reserve->cbCFData = fci->ccab.cbReserveCFData;
|
|
|
|
ptr = (char *)(reserve + 1);
|
|
|
|
}
|
|
|
|
ptr += fci->ccab.cbReserveCFHeader;
|
|
|
|
|
|
|
|
if (flags & cfheadPREV_CABINET)
|
|
|
|
{
|
|
|
|
strcpy( ptr, fci->szPrevCab );
|
|
|
|
ptr += strlen( ptr ) + 1;
|
|
|
|
strcpy( ptr, fci->szPrevDisk );
|
|
|
|
ptr += strlen( ptr ) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & cfheadNEXT_CABINET)
|
|
|
|
{
|
|
|
|
strcpy( ptr, fci->pccab->szCab );
|
|
|
|
ptr += strlen( ptr ) + 1;
|
|
|
|
strcpy( ptr, fci->pccab->szDisk );
|
|
|
|
ptr += strlen( ptr ) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( ptr - (char *)cfheader == header_size );
|
|
|
|
|
|
|
|
strcpy( filename, fci->ccab.szCabPath );
|
|
|
|
strcat( filename, fci->ccab.szCab );
|
|
|
|
|
|
|
|
if ((handle = fci->open( filename, _O_RDWR | _O_CREAT | _O_TRUNC | _O_BINARY,
|
|
|
|
_S_IREAD | _S_IWRITE, &err, fci->pv )) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
2011-02-16 00:20:49 +01:00
|
|
|
return FALSE;
|
2011-02-15 20:08:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fci->write( handle, cfheader, header_size, &err, fci->pv ) != header_size)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add size of header size of all CFFOLDERs and size of all CFFILEs */
|
|
|
|
header_size += fci->placed_files_size + fci->folders_size;
|
|
|
|
if (!write_folders( fci, handle, header_size, status_callback )) goto failed;
|
2011-02-16 00:20:49 +01:00
|
|
|
if (!write_files( fci, handle, status_callback )) goto failed;
|
|
|
|
if (!write_data_blocks( fci, handle, status_callback )) goto failed;
|
2011-02-15 20:08:44 +01:00
|
|
|
|
2011-02-16 00:20:49 +01:00
|
|
|
/* update the signature */
|
|
|
|
if (fci->seek( handle, 0, SEEK_SET, &err, fci->pv ) != 0 )
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
memcpy( cfheader->signature, "MSCF", 4 );
|
|
|
|
if (fci->write( handle, cfheader->signature, 4, &err, fci->pv ) != 4)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_CAB_FILE, err );
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
fci->close( handle, &err, fci->pv );
|
|
|
|
|
|
|
|
reset_cabinet( fci );
|
|
|
|
status_callback( statusCabinet, fci->estimatedCabinetSize, total_size, fci->pv );
|
|
|
|
return TRUE;
|
2011-02-15 20:08:44 +01:00
|
|
|
|
|
|
|
failed:
|
|
|
|
fci->close( handle, &err, fci->pv );
|
|
|
|
fci->delete( filename, &err, fci->pv );
|
2011-02-16 00:20:49 +01:00
|
|
|
return FALSE;
|
2011-02-15 20:08:44 +01:00
|
|
|
}
|
|
|
|
|
2011-02-17 16:48:19 +01:00
|
|
|
/* add all pending data blocks folder */
|
|
|
|
static BOOL add_data_to_folder( FCI_Int *fci, struct folder *folder, cab_ULONG *payload,
|
|
|
|
PFNFCISTATUS status_callback )
|
|
|
|
{
|
|
|
|
struct data_block *block, *new, *next;
|
|
|
|
BOOL split_block = FALSE;
|
|
|
|
cab_ULONG current_size, start_pos = 0;
|
|
|
|
|
|
|
|
*payload = 0;
|
|
|
|
current_size = get_header_size( fci ) + fci->folders_size +
|
|
|
|
fci->files_size + fci->placed_files_size + fci->folders_data_size;
|
|
|
|
|
|
|
|
/* move the temp file into the folder structure */
|
|
|
|
folder->data = fci->data;
|
|
|
|
fci->data.handle = -1;
|
2011-02-17 21:23:03 +01:00
|
|
|
fci->pending_data_size = 0;
|
2011-02-17 16:48:19 +01:00
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( block, next, &fci->blocks_list, struct data_block, entry )
|
|
|
|
{
|
|
|
|
/* No more CFDATA fits into the cabinet under construction */
|
|
|
|
/* So don't try to store more data into it */
|
|
|
|
if (fci->fNextCab && (fci->ccab.cb <= sizeof(CFDATA) + fci->ccab.cbReserveCFData +
|
|
|
|
current_size + sizeof(CFFOLDER) + fci->ccab.cbReserveCFFolder))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!(new = fci->alloc( sizeof(*new) )))
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_ALLOC_FAIL, ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Is cabinet with new CFDATA too large? Then data block has to be split */
|
|
|
|
if( fci->fNextCab &&
|
|
|
|
(fci->ccab.cb < sizeof(CFDATA) + fci->ccab.cbReserveCFData +
|
|
|
|
block->compressed + current_size + sizeof(CFFOLDER) + fci->ccab.cbReserveCFFolder))
|
|
|
|
{
|
|
|
|
/* Modify the size of the compressed data to store only a part of the */
|
|
|
|
/* data block into the current cabinet. This is done to prevent */
|
|
|
|
/* that the maximum cabinet size will be exceeded. The remainder */
|
|
|
|
/* will be stored into the next following cabinet. */
|
|
|
|
|
|
|
|
new->compressed = fci->ccab.cb - (sizeof(CFDATA) + fci->ccab.cbReserveCFData + current_size +
|
|
|
|
sizeof(CFFOLDER) + fci->ccab.cbReserveCFFolder );
|
|
|
|
new->uncompressed = 0; /* on split blocks of data this is zero */
|
|
|
|
block->compressed -= new->compressed;
|
|
|
|
split_block = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new->compressed = block->compressed;
|
|
|
|
new->uncompressed = block->uncompressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_pos += new->compressed;
|
|
|
|
current_size += sizeof(CFDATA) + fci->ccab.cbReserveCFData + new->compressed;
|
|
|
|
fci->folders_data_size += sizeof(CFDATA) + fci->ccab.cbReserveCFData + new->compressed;
|
|
|
|
fci->statusFolderCopied += new->compressed;
|
|
|
|
(*payload) += new->uncompressed;
|
|
|
|
|
|
|
|
list_add_tail( &folder->blocks_list, &new->entry );
|
|
|
|
folder->data_count++;
|
|
|
|
|
|
|
|
/* report status with pfnfcis about copied size of folder */
|
|
|
|
if (status_callback( statusFolder, fci->statusFolderCopied,
|
|
|
|
fci->statusFolderTotal, fci->pv ) == -1)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_USER_ABORT, 0 );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (split_block) break;
|
|
|
|
free_data_block( fci, block );
|
|
|
|
fci->cDataBlocks--;
|
|
|
|
}
|
|
|
|
|
2011-02-17 21:23:03 +01:00
|
|
|
if (list_empty( &fci->blocks_list )) return TRUE;
|
2011-02-17 16:48:19 +01:00
|
|
|
return copy_data_blocks( fci, folder->data.handle, start_pos, &fci->data, status_callback );
|
|
|
|
}
|
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
/* add all pending files to folder */
|
|
|
|
static BOOL add_files_to_folder( FCI_Int *fci, struct folder *folder, cab_ULONG payload )
|
|
|
|
{
|
|
|
|
cab_ULONG sizeOfFiles = 0, sizeOfFilesPrev;
|
|
|
|
cab_ULONG cbFileRemainer = 0;
|
|
|
|
struct file *file, *next;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( file, next, &fci->files_list, struct file, entry )
|
|
|
|
{
|
|
|
|
cab_ULONG size = sizeof(CFFILE) + strlen(file->name) + 1;
|
|
|
|
|
|
|
|
/* fnfilfnfildest: placed file on cabinet */
|
2011-02-15 14:11:22 +01:00
|
|
|
fci->fileplaced( &fci->ccab, file->name, file->size,
|
|
|
|
(file->folder == cffileCONTINUED_FROM_PREV), fci->pv );
|
2011-02-14 20:32:19 +01:00
|
|
|
|
|
|
|
sizeOfFilesPrev = sizeOfFiles;
|
|
|
|
/* set complete size of all processed files */
|
|
|
|
if (file->folder == cffileCONTINUED_FROM_PREV && fci->cbFileRemainer != 0)
|
|
|
|
{
|
|
|
|
sizeOfFiles += fci->cbFileRemainer;
|
|
|
|
fci->cbFileRemainer = 0;
|
|
|
|
}
|
|
|
|
else sizeOfFiles += file->size;
|
|
|
|
|
|
|
|
/* check if spanned file fits into this cabinet folder */
|
|
|
|
if (sizeOfFiles > payload)
|
|
|
|
{
|
|
|
|
if (file->folder == cffileCONTINUED_FROM_PREV)
|
|
|
|
file->folder = cffileCONTINUED_PREV_AND_NEXT;
|
|
|
|
else
|
|
|
|
file->folder = cffileCONTINUED_TO_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_remove( &file->entry );
|
|
|
|
list_add_tail( &folder->files_list, &file->entry );
|
|
|
|
fci->placed_files_size += size;
|
|
|
|
fci->cFiles++;
|
|
|
|
|
|
|
|
/* This is only true for files which will be written into the */
|
|
|
|
/* next cabinet of the spanning folder */
|
|
|
|
if (sizeOfFiles > payload)
|
|
|
|
{
|
|
|
|
/* add a copy back onto the list */
|
|
|
|
if (!(file = copy_file( fci, file ))) return FALSE;
|
|
|
|
list_add_before( &next->entry, &file->entry );
|
|
|
|
|
|
|
|
/* Files which data will be partially written into the current cabinet */
|
|
|
|
if (file->folder == cffileCONTINUED_PREV_AND_NEXT || file->folder == cffileCONTINUED_TO_NEXT)
|
|
|
|
{
|
|
|
|
if (sizeOfFilesPrev <= payload)
|
|
|
|
{
|
|
|
|
/* The size of the uncompressed, data of a spanning file in a */
|
|
|
|
/* spanning data */
|
|
|
|
cbFileRemainer = sizeOfFiles - payload;
|
|
|
|
}
|
|
|
|
file->folder = cffileCONTINUED_FROM_PREV;
|
|
|
|
}
|
|
|
|
else file->folder = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fci->files_size -= size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fci->cbFileRemainer = cbFileRemainer;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
static cab_UWORD compress_NONE( FCI_Int *fci )
|
|
|
|
{
|
|
|
|
memcpy( fci->data_out, fci->data_in, fci->cdata_in );
|
|
|
|
return fci->cdata_in;
|
|
|
|
}
|
|
|
|
|
2011-02-18 00:15:44 +01:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
|
|
|
|
static void *zalloc( void *opaque, unsigned int items, unsigned int size )
|
|
|
|
{
|
|
|
|
FCI_Int *fci = opaque;
|
|
|
|
return fci->alloc( items * size );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zfree( void *opaque, void *ptr )
|
|
|
|
{
|
|
|
|
FCI_Int *fci = opaque;
|
|
|
|
return fci->free( ptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
static cab_UWORD compress_MSZIP( FCI_Int *fci )
|
|
|
|
{
|
|
|
|
z_stream stream;
|
|
|
|
|
|
|
|
stream.zalloc = zalloc;
|
|
|
|
stream.zfree = zfree;
|
|
|
|
stream.opaque = fci;
|
|
|
|
if (deflateInit2( &stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY ) != Z_OK)
|
|
|
|
{
|
|
|
|
set_error( fci, FCIERR_ALLOC_FAIL, ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
stream.next_in = fci->data_in;
|
|
|
|
stream.avail_in = fci->cdata_in;
|
|
|
|
stream.next_out = fci->data_out + 2;
|
2011-02-18 11:32:35 +01:00
|
|
|
stream.avail_out = sizeof(fci->data_out) - 2;
|
2011-02-18 00:15:44 +01:00
|
|
|
/* insert the signature */
|
|
|
|
fci->data_out[0] = 'C';
|
|
|
|
fci->data_out[1] = 'K';
|
|
|
|
deflate( &stream, Z_FINISH );
|
|
|
|
deflateEnd( &stream );
|
|
|
|
return stream.total_out + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_ZLIB */
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
|
2002-11-19 01:47:12 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* FCICreate (CABINET.10)
|
2005-05-14 13:06:10 +02:00
|
|
|
*
|
2005-06-24 14:16:37 +02:00
|
|
|
* FCICreate is provided with several callbacks and
|
|
|
|
* returns a handle which can be used to create cabinet files.
|
2005-05-14 13:06:10 +02:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* perf [IO] A pointer to an ERF structure. When FCICreate
|
|
|
|
* returns an error condition, error information may
|
|
|
|
* be found here as well as from GetLastError.
|
|
|
|
* pfnfiledest [I] A pointer to a function which is called when a file
|
|
|
|
* is placed. Only useful for subsequent cabinet files.
|
|
|
|
* pfnalloc [I] A pointer to a function which allocates ram. Uses
|
|
|
|
* the same interface as malloc.
|
|
|
|
* pfnfree [I] A pointer to a function which frees ram. Uses the
|
|
|
|
* same interface as free.
|
|
|
|
* pfnopen [I] A pointer to a function which opens a file. Uses
|
|
|
|
* the same interface as _open.
|
|
|
|
* pfnread [I] A pointer to a function which reads from a file into
|
|
|
|
* a caller-provided buffer. Uses the same interface
|
2005-06-24 14:16:37 +02:00
|
|
|
* as _read.
|
2005-05-14 13:06:10 +02:00
|
|
|
* pfnwrite [I] A pointer to a function which writes to a file from
|
|
|
|
* a caller-provided buffer. Uses the same interface
|
|
|
|
* as _write.
|
|
|
|
* pfnclose [I] A pointer to a function which closes a file handle.
|
|
|
|
* Uses the same interface as _close.
|
|
|
|
* pfnseek [I] A pointer to a function which seeks in a file.
|
|
|
|
* Uses the same interface as _lseek.
|
|
|
|
* pfndelete [I] A pointer to a function which deletes a file.
|
|
|
|
* pfnfcigtf [I] A pointer to a function which gets the name of a
|
2005-06-24 14:16:37 +02:00
|
|
|
* temporary file.
|
|
|
|
* pccab [I] A pointer to an initialized CCAB structure.
|
2005-05-14 13:06:10 +02:00
|
|
|
* pv [I] A pointer to an application-defined notification
|
|
|
|
* function which will be passed to other FCI functions
|
|
|
|
* as a parameter.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* On success, returns an FCI handle of type HFCI.
|
|
|
|
* On failure, the NULL file handle is returned. Error
|
|
|
|
* info can be retrieved from perf.
|
|
|
|
*
|
|
|
|
* INCLUDES
|
|
|
|
* fci.h
|
|
|
|
*
|
2002-11-19 01:47:12 +01:00
|
|
|
*/
|
|
|
|
HFCI __cdecl FCICreate(
|
|
|
|
PERF perf,
|
2005-05-14 13:06:10 +02:00
|
|
|
PFNFCIFILEPLACED pfnfiledest,
|
|
|
|
PFNFCIALLOC pfnalloc,
|
|
|
|
PFNFCIFREE pfnfree,
|
2002-11-19 01:47:12 +01:00
|
|
|
PFNFCIOPEN pfnopen,
|
|
|
|
PFNFCIREAD pfnread,
|
|
|
|
PFNFCIWRITE pfnwrite,
|
|
|
|
PFNFCICLOSE pfnclose,
|
|
|
|
PFNFCISEEK pfnseek,
|
|
|
|
PFNFCIDELETE pfndelete,
|
|
|
|
PFNFCIGETTEMPFILE pfnfcigtf,
|
|
|
|
PCCAB pccab,
|
|
|
|
void *pv)
|
|
|
|
{
|
2011-02-04 11:32:46 +01:00
|
|
|
FCI_Int *p_fci_internal;
|
2002-12-02 20:00:59 +01:00
|
|
|
|
2007-06-27 14:10:38 +02:00
|
|
|
if (!perf) {
|
|
|
|
SetLastError(ERROR_BAD_ARGUMENTS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((!pfnalloc) || (!pfnfree) || (!pfnopen) || (!pfnread) ||
|
2005-06-24 14:16:37 +02:00
|
|
|
(!pfnwrite) || (!pfnclose) || (!pfnseek) || (!pfndelete) ||
|
|
|
|
(!pfnfcigtf) || (!pccab)) {
|
2002-12-02 20:00:59 +01:00
|
|
|
perf->erfOper = FCIERR_NONE;
|
2005-05-14 13:06:10 +02:00
|
|
|
perf->erfType = ERROR_BAD_ARGUMENTS;
|
2002-12-02 20:00:59 +01:00
|
|
|
perf->fError = TRUE;
|
|
|
|
|
2005-05-14 13:06:10 +02:00
|
|
|
SetLastError(ERROR_BAD_ARGUMENTS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
if (!((p_fci_internal = pfnalloc(sizeof(FCI_Int))))) {
|
2005-05-14 13:06:10 +02:00
|
|
|
perf->erfOper = FCIERR_ALLOC_FAIL;
|
|
|
|
perf->erfType = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
perf->fError = TRUE;
|
2002-12-02 20:00:59 +01:00
|
|
|
|
2005-05-14 13:06:10 +02:00
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2002-11-19 01:47:12 +01:00
|
|
|
return NULL;
|
2005-05-14 13:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-02-04 11:35:18 +01:00
|
|
|
p_fci_internal->magic = FCI_INT_MAGIC;
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->perf = perf;
|
2011-02-04 11:27:31 +01:00
|
|
|
p_fci_internal->fileplaced = pfnfiledest;
|
|
|
|
p_fci_internal->alloc = pfnalloc;
|
|
|
|
p_fci_internal->free = pfnfree;
|
|
|
|
p_fci_internal->open = pfnopen;
|
|
|
|
p_fci_internal->read = pfnread;
|
|
|
|
p_fci_internal->write = pfnwrite;
|
|
|
|
p_fci_internal->close = pfnclose;
|
|
|
|
p_fci_internal->seek = pfnseek;
|
|
|
|
p_fci_internal->delete = pfndelete;
|
|
|
|
p_fci_internal->gettemp = pfnfcigtf;
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab = *pccab;
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->pccab = pccab;
|
|
|
|
p_fci_internal->fPrevCab = FALSE;
|
|
|
|
p_fci_internal->fNextCab = FALSE;
|
|
|
|
p_fci_internal->fSplitFolder = FALSE;
|
|
|
|
p_fci_internal->fGetNextCabInVain = FALSE;
|
|
|
|
p_fci_internal->pv = pv;
|
|
|
|
p_fci_internal->cdata_in = 0;
|
|
|
|
p_fci_internal->cCompressedBytesInFolder = 0;
|
|
|
|
p_fci_internal->cFolders = 0;
|
|
|
|
p_fci_internal->cFiles = 0;
|
|
|
|
p_fci_internal->cDataBlocks = 0;
|
2011-02-17 16:48:19 +01:00
|
|
|
p_fci_internal->data.handle = -1;
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->fNewPrevious = FALSE;
|
2006-02-24 14:28:20 +01:00
|
|
|
p_fci_internal->estimatedCabinetSize = 0;
|
|
|
|
p_fci_internal->statusFolderTotal = 0;
|
2011-02-14 20:05:46 +01:00
|
|
|
p_fci_internal->folders_size = 0;
|
2011-02-14 20:32:19 +01:00
|
|
|
p_fci_internal->files_size = 0;
|
|
|
|
p_fci_internal->placed_files_size = 0;
|
2011-02-17 21:23:03 +01:00
|
|
|
p_fci_internal->pending_data_size = 0;
|
2011-02-17 16:48:19 +01:00
|
|
|
p_fci_internal->folders_data_size = 0;
|
2011-02-17 21:08:28 +01:00
|
|
|
p_fci_internal->compression = tcompTYPE_NONE;
|
|
|
|
p_fci_internal->compress = compress_NONE;
|
2011-02-14 20:05:46 +01:00
|
|
|
|
|
|
|
list_init( &p_fci_internal->folders_list );
|
2011-02-14 20:32:19 +01:00
|
|
|
list_init( &p_fci_internal->files_list );
|
2011-02-16 00:20:49 +01:00
|
|
|
list_init( &p_fci_internal->blocks_list );
|
2005-05-14 13:06:10 +02:00
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
memcpy(p_fci_internal->szPrevCab, pccab->szCab, CB_MAX_CABINET_NAME);
|
|
|
|
memcpy(p_fci_internal->szPrevDisk, pccab->szDisk, CB_MAX_DISK_NAME);
|
2005-05-14 13:06:10 +02:00
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
return (HFCI)p_fci_internal;
|
|
|
|
}
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
static BOOL fci_flush_folder( FCI_Int *p_fci_internal,
|
2002-11-19 01:47:12 +01:00
|
|
|
BOOL fGetNextCab,
|
|
|
|
PFNFCIGETNEXTCABINET pfnfcignc,
|
|
|
|
PFNFCISTATUS pfnfcis)
|
|
|
|
{
|
2005-06-24 14:16:37 +02:00
|
|
|
cab_ULONG payload;
|
|
|
|
cab_ULONG read_result;
|
2011-02-14 20:05:46 +01:00
|
|
|
struct folder *folder;
|
2002-12-02 20:00:59 +01:00
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
if ((!pfnfcignc) || (!pfnfcis)) {
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_BAD_ARGUMENTS );
|
2002-12-02 20:00:59 +01:00
|
|
|
return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
2002-11-19 01:47:12 +01:00
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
if( p_fci_internal->fGetNextCabInVain &&
|
|
|
|
p_fci_internal->fNextCab ){
|
2006-02-24 14:28:20 +01:00
|
|
|
/* internal error */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there was no FCIAddFile or FCIFlushFolder has already been called */
|
|
|
|
/* this function will return TRUE */
|
2011-02-14 20:32:19 +01:00
|
|
|
if( p_fci_internal->files_size == 0 ) {
|
2011-02-17 21:23:03 +01:00
|
|
|
if ( p_fci_internal->pending_data_size != 0 ) {
|
2006-02-24 14:28:20 +01:00
|
|
|
/* error handling */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-12-02 20:00:59 +01:00
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
/* FCIFlushFolder has already been called... */
|
2011-02-14 20:32:19 +01:00
|
|
|
if (p_fci_internal->fSplitFolder && p_fci_internal->placed_files_size!=0) {
|
2005-06-24 14:16:37 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-02-24 14:28:20 +01:00
|
|
|
/* This can be set already, because it makes only a difference */
|
|
|
|
/* when the current function exits with return FALSE */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->fSplitFolder=FALSE;
|
|
|
|
|
|
|
|
/* START of COPY */
|
2011-02-17 21:08:28 +01:00
|
|
|
if (!add_data_block( p_fci_internal, pfnfcis )) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
/* reset to get the number of data blocks of this folder which are */
|
|
|
|
/* actually in this cabinet ( at least partially ) */
|
|
|
|
p_fci_internal->cDataBlocks=0;
|
|
|
|
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->statusFolderTotal = get_header_size( p_fci_internal ) +
|
|
|
|
sizeof(CFFOLDER) + p_fci_internal->ccab.cbReserveCFFolder +
|
|
|
|
p_fci_internal->placed_files_size+
|
2011-02-17 16:48:19 +01:00
|
|
|
p_fci_internal->folders_data_size + p_fci_internal->files_size+
|
2011-02-17 21:23:03 +01:00
|
|
|
p_fci_internal->pending_data_size + p_fci_internal->folders_size;
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->statusFolderCopied = 0;
|
|
|
|
|
|
|
|
/* report status with pfnfcis about copied size of folder */
|
|
|
|
if( (*pfnfcis)(statusFolder, p_fci_internal->statusFolderCopied,
|
|
|
|
p_fci_internal->statusFolderTotal, /* TODO total folder size */
|
|
|
|
p_fci_internal->pv) == -1) {
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_USER_ABORT, 0 );
|
2002-12-02 20:00:59 +01:00
|
|
|
return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
2002-11-19 01:47:12 +01:00
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
/* USE the variable read_result */
|
2011-02-17 16:48:19 +01:00
|
|
|
read_result = get_header_size( p_fci_internal ) + p_fci_internal->folders_data_size +
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->placed_files_size + p_fci_internal->folders_size;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
if(p_fci_internal->files_size!=0) {
|
2011-02-15 14:11:22 +01:00
|
|
|
read_result+= sizeof(CFFOLDER)+p_fci_internal->ccab.cbReserveCFFolder;
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if multiple cabinets have to be created. */
|
|
|
|
|
|
|
|
/* Might be too much data for the maximum allowed cabinet size.*/
|
|
|
|
/* When any further data will be added later, it might not */
|
|
|
|
/* be possible to flush the cabinet, because there might */
|
|
|
|
/* not be enough space to store the name of the following */
|
|
|
|
/* cabinet and name of the corresponding disk. */
|
|
|
|
/* So take care of this and get the name of the next cabinet */
|
|
|
|
if( p_fci_internal->fGetNextCabInVain==FALSE &&
|
|
|
|
p_fci_internal->fNextCab==FALSE &&
|
|
|
|
(
|
|
|
|
(
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result +
|
2011-02-17 21:23:03 +01:00
|
|
|
p_fci_internal->pending_data_size +
|
2011-02-14 20:32:19 +01:00
|
|
|
p_fci_internal->files_size +
|
2005-06-24 14:16:37 +02:00
|
|
|
CB_MAX_CABINET_NAME + /* next cabinet name */
|
|
|
|
CB_MAX_DISK_NAME /* next disk name */
|
|
|
|
) || fGetNextCab
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
/* increment cabinet index */
|
|
|
|
++(p_fci_internal->pccab->iCab);
|
|
|
|
/* get name of next cabinet */
|
2006-02-24 14:28:20 +01:00
|
|
|
p_fci_internal->estimatedCabinetSize=p_fci_internal->statusFolderTotal;
|
|
|
|
if (!(*pfnfcignc)(p_fci_internal->pccab,
|
|
|
|
p_fci_internal->estimatedCabinetSize, /* estimated size of cab */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->pv)) {
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_FUNCTION_FAILED );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-12-14 17:59:46 +01:00
|
|
|
/* Skip a few lines of code. This is caught by the next if. */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->fGetNextCabInVain=TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* too much data for cabinet */
|
|
|
|
if( (p_fci_internal->fGetNextCabInVain ||
|
|
|
|
p_fci_internal->fNextCab ) &&
|
|
|
|
(
|
|
|
|
(
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result +
|
2011-02-17 21:23:03 +01:00
|
|
|
p_fci_internal->pending_data_size +
|
2011-02-14 20:32:19 +01:00
|
|
|
p_fci_internal->files_size +
|
2005-06-24 14:16:37 +02:00
|
|
|
strlen(p_fci_internal->pccab->szCab)+1 + /* next cabinet name */
|
|
|
|
strlen(p_fci_internal->pccab->szDisk)+1 /* next disk name */
|
|
|
|
) || fGetNextCab
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
p_fci_internal->fGetNextCabInVain=FALSE;
|
|
|
|
p_fci_internal->fNextCab=TRUE;
|
|
|
|
|
|
|
|
/* return FALSE if there is not enough space left*/
|
|
|
|
/* this should never happen */
|
2011-02-15 14:11:22 +01:00
|
|
|
if (p_fci_internal->ccab.cb <=
|
2011-02-14 20:32:19 +01:00
|
|
|
p_fci_internal->files_size +
|
2005-06-24 14:16:37 +02:00
|
|
|
read_result +
|
|
|
|
strlen(p_fci_internal->pccab->szCab)+1 + /* next cabinet name */
|
|
|
|
strlen(p_fci_internal->pccab->szDisk)+1 /* next disk name */
|
|
|
|
) {
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the folder will be split across cabinets */
|
|
|
|
p_fci_internal->fSplitFolder=TRUE;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* this should never happen */
|
|
|
|
if (p_fci_internal->fNextCab) {
|
2006-02-24 14:28:20 +01:00
|
|
|
/* internal error */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 20:05:46 +01:00
|
|
|
if (!(folder = add_folder( p_fci_internal ))) return FALSE;
|
2011-02-17 16:48:19 +01:00
|
|
|
if (!add_data_to_folder( p_fci_internal, folder, &payload, pfnfcis )) return FALSE;
|
|
|
|
if (!add_files_to_folder( p_fci_internal, folder, payload )) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
/* reset CFFolder specific information */
|
|
|
|
p_fci_internal->cDataBlocks=0;
|
|
|
|
p_fci_internal->cCompressedBytesInFolder=0;
|
|
|
|
|
|
|
|
return TRUE;
|
2011-02-15 12:58:07 +01:00
|
|
|
}
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
static BOOL fci_flush_cabinet( FCI_Int *p_fci_internal,
|
2005-06-24 14:16:37 +02:00
|
|
|
BOOL fGetNextCab,
|
|
|
|
PFNFCIGETNEXTCABINET pfnfcignc,
|
|
|
|
PFNFCISTATUS pfnfcis)
|
|
|
|
{
|
2011-02-16 00:20:49 +01:00
|
|
|
cab_ULONG read_result=0;
|
2005-06-24 14:16:37 +02:00
|
|
|
BOOL returntrue=FALSE;
|
|
|
|
|
|
|
|
/* TODO test if fci_flush_cabinet really aborts if there was no FCIAddFile */
|
|
|
|
|
2006-02-24 14:28:20 +01:00
|
|
|
/* when FCIFlushCabinet was or FCIAddFile hasn't been called */
|
2011-02-14 20:32:19 +01:00
|
|
|
if( p_fci_internal->files_size==0 && fGetNextCab ) {
|
2005-06-24 14:16:37 +02:00
|
|
|
returntrue=TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
if (!fci_flush_folder(p_fci_internal,fGetNextCab,pfnfcignc,pfnfcis)){
|
2005-06-24 14:16:37 +02:00
|
|
|
/* TODO set error */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(returntrue) return TRUE;
|
|
|
|
|
2006-02-24 14:28:20 +01:00
|
|
|
if ( (p_fci_internal->fSplitFolder && p_fci_internal->fNextCab==FALSE)||
|
2011-02-14 20:05:46 +01:00
|
|
|
(p_fci_internal->folders_size==0 &&
|
2011-02-14 20:32:19 +01:00
|
|
|
(p_fci_internal->files_size!=0 ||
|
|
|
|
p_fci_internal->placed_files_size!=0 )
|
2006-02-24 14:28:20 +01:00
|
|
|
) )
|
|
|
|
{
|
|
|
|
/* error */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-02-24 14:28:20 +01:00
|
|
|
/* create the cabinet */
|
2011-02-16 00:20:49 +01:00
|
|
|
if (!write_cabinet( p_fci_internal, pfnfcis )) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
p_fci_internal->fPrevCab=TRUE;
|
|
|
|
/* The sections szPrevCab and szPrevDisk are not being updated, because */
|
|
|
|
/* MS CABINET.DLL always puts the first cabinet name and disk into them */
|
|
|
|
|
|
|
|
if (p_fci_internal->fNextCab) {
|
|
|
|
p_fci_internal->fNextCab=FALSE;
|
|
|
|
|
2011-02-17 21:23:03 +01:00
|
|
|
if (p_fci_internal->files_size==0 && p_fci_internal->pending_data_size!=0) {
|
2005-06-24 14:16:37 +02:00
|
|
|
/* THIS CAN NEVER HAPPEN */
|
2006-02-24 14:28:20 +01:00
|
|
|
/* set error code */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:11:22 +01:00
|
|
|
if( p_fci_internal->fNewPrevious ) {
|
|
|
|
memcpy(p_fci_internal->szPrevCab, p_fci_internal->ccab.szCab,
|
|
|
|
CB_MAX_CABINET_NAME);
|
|
|
|
memcpy(p_fci_internal->szPrevDisk, p_fci_internal->ccab.szDisk,
|
|
|
|
CB_MAX_DISK_NAME);
|
|
|
|
p_fci_internal->fNewPrevious=FALSE;
|
|
|
|
}
|
|
|
|
p_fci_internal->ccab = *p_fci_internal->pccab;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
/* REUSE the variable read_result */
|
2011-02-15 14:11:22 +01:00
|
|
|
read_result=get_header_size( p_fci_internal );
|
|
|
|
if(p_fci_internal->files_size!=0) {
|
|
|
|
read_result+=p_fci_internal->ccab.cbReserveCFFolder;
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
2011-02-17 21:23:03 +01:00
|
|
|
read_result+= p_fci_internal->pending_data_size +
|
2011-02-17 16:48:19 +01:00
|
|
|
p_fci_internal->files_size + p_fci_internal->folders_data_size +
|
2011-02-14 20:32:19 +01:00
|
|
|
p_fci_internal->placed_files_size + p_fci_internal->folders_size +
|
2005-06-24 14:16:37 +02:00
|
|
|
sizeof(CFFOLDER); /* set size of new CFFolder entry */
|
|
|
|
|
|
|
|
/* too much data for the maximum size of a cabinet */
|
|
|
|
if( p_fci_internal->fGetNextCabInVain==FALSE &&
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result ) {
|
2011-02-04 11:32:46 +01:00
|
|
|
return fci_flush_cabinet( p_fci_internal, FALSE, pfnfcignc, pfnfcis);
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Might be too much data for the maximum size of a cabinet.*/
|
|
|
|
/* When any further data will be added later, it might not */
|
|
|
|
/* be possible to flush the cabinet, because there might */
|
|
|
|
/* not be enough space to store the name of the following */
|
|
|
|
/* cabinet and name of the corresponding disk. */
|
|
|
|
/* So take care of this and get the name of the next cabinet */
|
|
|
|
if (p_fci_internal->fGetNextCabInVain==FALSE && (
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result +
|
2005-06-24 14:16:37 +02:00
|
|
|
CB_MAX_CABINET_NAME + CB_MAX_DISK_NAME
|
|
|
|
)) {
|
|
|
|
/* increment cabinet index */
|
|
|
|
++(p_fci_internal->pccab->iCab);
|
|
|
|
/* get name of next cabinet */
|
2006-02-24 14:28:20 +01:00
|
|
|
p_fci_internal->estimatedCabinetSize=p_fci_internal->statusFolderTotal;
|
|
|
|
if (!(*pfnfcignc)(p_fci_internal->pccab,
|
|
|
|
p_fci_internal->estimatedCabinetSize, /* estimated size of cab */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->pv)) {
|
2006-02-24 14:28:20 +01:00
|
|
|
/* error handling */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_FUNCTION_FAILED );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2006-12-14 17:59:46 +01:00
|
|
|
/* Skip a few lines of code. This is caught by the next if. */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->fGetNextCabInVain=TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* too much data for cabinet */
|
|
|
|
if (p_fci_internal->fGetNextCabInVain && (
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result +
|
|
|
|
strlen(p_fci_internal->ccab.szCab)+1+
|
|
|
|
strlen(p_fci_internal->ccab.szDisk)+1
|
2005-06-24 14:16:37 +02:00
|
|
|
)) {
|
|
|
|
p_fci_internal->fGetNextCabInVain=FALSE;
|
|
|
|
p_fci_internal->fNextCab=TRUE;
|
2011-02-04 11:32:46 +01:00
|
|
|
return fci_flush_cabinet( p_fci_internal, FALSE, pfnfcignc, pfnfcis);
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the FolderThreshold has been reached flush the folder automatically */
|
2011-02-15 14:11:22 +01:00
|
|
|
if (p_fci_internal->cCompressedBytesInFolder >= p_fci_internal->ccab.cbFolderThresh)
|
2011-02-04 11:32:46 +01:00
|
|
|
return fci_flush_folder(p_fci_internal, FALSE, pfnfcignc, pfnfcis);
|
2005-06-24 14:16:37 +02:00
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
if( p_fci_internal->files_size>0 ) {
|
2011-02-04 11:32:46 +01:00
|
|
|
if( !fci_flush_folder(p_fci_internal, FALSE, pfnfcignc, pfnfcis) ) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->fNewPrevious=TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p_fci_internal->fNewPrevious=FALSE;
|
2011-02-17 21:23:03 +01:00
|
|
|
if( p_fci_internal->files_size>0 || p_fci_internal->pending_data_size) {
|
2005-06-24 14:16:37 +02:00
|
|
|
/* THIS MAY NEVER HAPPEN */
|
2006-02-24 14:28:20 +01:00
|
|
|
/* set error structures */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
} /* end of fci_flush_cabinet */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FCIAddFile (CABINET.11)
|
|
|
|
*
|
|
|
|
* FCIAddFile adds a file to the to be created cabinet file
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* hfci [I] An HFCI from FCICreate
|
|
|
|
* pszSourceFile [I] A pointer to a C string which contains the name and
|
|
|
|
* location of the file which will be added to the cabinet
|
|
|
|
* pszFileName [I] A pointer to a C string which contains the name under
|
|
|
|
* which the file will be stored in the cabinet
|
|
|
|
* fExecute [I] A boolean value which indicates if the file should be
|
|
|
|
* executed after extraction of self extracting
|
|
|
|
* executables
|
|
|
|
* pfnfcignc [I] A pointer to a function which gets information about
|
|
|
|
* the next cabinet
|
|
|
|
* pfnfcis [IO] A pointer to a function which will report status
|
|
|
|
* information about the compression process
|
|
|
|
* pfnfcioi [I] A pointer to a function which reports file attributes
|
|
|
|
* and time and date information
|
|
|
|
* typeCompress [I] Compression type
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* On success, returns TRUE
|
|
|
|
* On failure, returns FALSE
|
|
|
|
*
|
|
|
|
* INCLUDES
|
|
|
|
* fci.h
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL __cdecl FCIAddFile(
|
|
|
|
HFCI hfci,
|
|
|
|
char *pszSourceFile,
|
|
|
|
char *pszFileName,
|
|
|
|
BOOL fExecute,
|
|
|
|
PFNFCIGETNEXTCABINET pfnfcignc,
|
|
|
|
PFNFCISTATUS pfnfcis,
|
|
|
|
PFNFCIGETOPENINFO pfnfcigoi,
|
|
|
|
TCOMP typeCompress)
|
|
|
|
{
|
|
|
|
cab_ULONG read_result;
|
2011-02-04 11:32:46 +01:00
|
|
|
FCI_Int *p_fci_internal = get_fci_ptr( hfci );
|
2005-06-24 14:16:37 +02:00
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
if (!p_fci_internal) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
if ((!pszSourceFile) || (!pszFileName) || (!pfnfcignc) || (!pfnfcis) ||
|
|
|
|
(!pfnfcigoi) || strlen(pszFileName)>=CB_MAX_FILENAME) {
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_BAD_ARGUMENTS );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
if (typeCompress != p_fci_internal->compression)
|
|
|
|
{
|
|
|
|
if (!FCIFlushFolder( hfci, pfnfcignc, pfnfcis )) return FALSE;
|
|
|
|
switch (typeCompress)
|
|
|
|
{
|
2011-02-18 00:15:44 +01:00
|
|
|
case tcompTYPE_MSZIP:
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
p_fci_internal->compression = tcompTYPE_MSZIP;
|
|
|
|
p_fci_internal->compress = compress_MSZIP;
|
|
|
|
break;
|
|
|
|
#endif
|
2011-02-17 21:08:28 +01:00
|
|
|
default:
|
|
|
|
FIXME( "compression %x not supported, defaulting to none\n", typeCompress );
|
|
|
|
/* fall through */
|
|
|
|
case tcompTYPE_NONE:
|
|
|
|
p_fci_internal->compression = tcompTYPE_NONE;
|
|
|
|
p_fci_internal->compress = compress_NONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 14:16:37 +02:00
|
|
|
/* TODO check if pszSourceFile??? */
|
|
|
|
|
|
|
|
if(p_fci_internal->fGetNextCabInVain && p_fci_internal->fNextCab) {
|
2006-02-24 14:28:20 +01:00
|
|
|
/* internal error */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p_fci_internal->fNextCab) {
|
2006-02-24 14:28:20 +01:00
|
|
|
/* internal error */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* REUSE the variable read_result */
|
2011-02-15 14:11:22 +01:00
|
|
|
read_result=get_header_size( p_fci_internal ) + p_fci_internal->ccab.cbReserveCFFolder;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
read_result+= sizeof(CFFILE) + strlen(pszFileName)+1 +
|
2011-02-17 16:48:19 +01:00
|
|
|
p_fci_internal->files_size + p_fci_internal->folders_data_size +
|
2011-02-14 20:32:19 +01:00
|
|
|
p_fci_internal->placed_files_size + p_fci_internal->folders_size +
|
2005-06-24 14:16:37 +02:00
|
|
|
sizeof(CFFOLDER); /* size of new CFFolder entry */
|
|
|
|
|
|
|
|
/* Might be too much data for the maximum size of a cabinet.*/
|
|
|
|
/* When any further data will be added later, it might not */
|
|
|
|
/* be possible to flush the cabinet, because there might */
|
|
|
|
/* not be enough space to store the name of the following */
|
|
|
|
/* cabinet and name of the corresponding disk. */
|
|
|
|
/* So take care of this and get the name of the next cabinet */
|
|
|
|
if( p_fci_internal->fGetNextCabInVain==FALSE &&
|
|
|
|
p_fci_internal->fNextCab==FALSE &&
|
2011-02-15 14:11:22 +01:00
|
|
|
( p_fci_internal->ccab.cb < read_result +
|
2005-06-24 14:16:37 +02:00
|
|
|
CB_MAX_CABINET_NAME + CB_MAX_DISK_NAME
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
/* increment cabinet index */
|
|
|
|
++(p_fci_internal->pccab->iCab);
|
|
|
|
/* get name of next cabinet */
|
2006-02-24 14:28:20 +01:00
|
|
|
p_fci_internal->estimatedCabinetSize=p_fci_internal->statusFolderTotal;
|
|
|
|
if (!(*pfnfcignc)(p_fci_internal->pccab,
|
|
|
|
p_fci_internal->estimatedCabinetSize, /* estimated size of cab */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->pv)) {
|
2006-02-24 14:28:20 +01:00
|
|
|
/* error handling */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_FUNCTION_FAILED );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2006-12-14 17:59:46 +01:00
|
|
|
/* Skip a few lines of code. This is caught by the next if. */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->fGetNextCabInVain=TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p_fci_internal->fGetNextCabInVain &&
|
|
|
|
p_fci_internal->fNextCab
|
|
|
|
) {
|
|
|
|
/* THIS CAN NEVER HAPPEN */
|
2006-02-24 14:28:20 +01:00
|
|
|
/* set error code */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* too much data for cabinet */
|
|
|
|
if( p_fci_internal->fGetNextCabInVain &&
|
|
|
|
(
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result +
|
2005-06-24 14:16:37 +02:00
|
|
|
strlen(p_fci_internal->pccab->szCab)+1+
|
|
|
|
strlen(p_fci_internal->pccab->szDisk)+1
|
|
|
|
)) {
|
|
|
|
p_fci_internal->fGetNextCabInVain=FALSE;
|
|
|
|
p_fci_internal->fNextCab=TRUE;
|
2011-02-04 11:32:46 +01:00
|
|
|
if(!fci_flush_cabinet( p_fci_internal, FALSE, pfnfcignc, pfnfcis)) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( p_fci_internal->fNextCab ) {
|
|
|
|
/* THIS MAY NEVER HAPPEN */
|
2006-02-24 14:28:20 +01:00
|
|
|
/* set error code */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-17 21:08:28 +01:00
|
|
|
if (!add_file_data( p_fci_internal, pszSourceFile, pszFileName, fExecute, pfnfcigoi, pfnfcis ))
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* REUSE the variable read_result */
|
2011-02-15 14:11:22 +01:00
|
|
|
read_result = get_header_size( p_fci_internal ) + p_fci_internal->ccab.cbReserveCFFolder;
|
2011-02-17 21:23:03 +01:00
|
|
|
read_result+= p_fci_internal->pending_data_size +
|
2011-02-17 16:48:19 +01:00
|
|
|
p_fci_internal->files_size + p_fci_internal->folders_data_size +
|
2011-02-14 20:32:19 +01:00
|
|
|
p_fci_internal->placed_files_size + p_fci_internal->folders_size +
|
2005-06-24 14:16:37 +02:00
|
|
|
sizeof(CFFOLDER); /* set size of new CFFolder entry */
|
|
|
|
|
|
|
|
/* too much data for the maximum size of a cabinet */
|
|
|
|
/* (ignoring the unflushed data block) */
|
|
|
|
if( p_fci_internal->fGetNextCabInVain==FALSE &&
|
|
|
|
p_fci_internal->fNextCab==FALSE && /* this is always the case */
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result ) {
|
2011-02-04 11:32:46 +01:00
|
|
|
return fci_flush_cabinet( p_fci_internal, FALSE, pfnfcignc, pfnfcis);
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Might be too much data for the maximum size of a cabinet.*/
|
|
|
|
/* When any further data will be added later, it might not */
|
|
|
|
/* be possible to flush the cabinet, because there might */
|
|
|
|
/* not be enough space to store the name of the following */
|
|
|
|
/* cabinet and name of the corresponding disk. */
|
|
|
|
/* So take care of this and get the name of the next cabinet */
|
|
|
|
/* (ignoring the unflushed data block) */
|
|
|
|
if( p_fci_internal->fGetNextCabInVain==FALSE &&
|
|
|
|
p_fci_internal->fNextCab==FALSE &&
|
2011-02-15 14:11:22 +01:00
|
|
|
( p_fci_internal->ccab.cb < read_result +
|
2005-06-24 14:16:37 +02:00
|
|
|
CB_MAX_CABINET_NAME + CB_MAX_DISK_NAME
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
/* increment cabinet index */
|
|
|
|
++(p_fci_internal->pccab->iCab);
|
|
|
|
/* get name of next cabinet */
|
2006-02-24 14:28:20 +01:00
|
|
|
p_fci_internal->estimatedCabinetSize=p_fci_internal->statusFolderTotal;
|
|
|
|
if (!(*pfnfcignc)(p_fci_internal->pccab,
|
|
|
|
p_fci_internal->estimatedCabinetSize,/* estimated size of cab */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->pv)) {
|
2006-02-24 14:28:20 +01:00
|
|
|
/* error handling */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_FUNCTION_FAILED );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2006-12-14 17:59:46 +01:00
|
|
|
/* Skip a few lines of code. This is caught by the next if. */
|
2005-06-24 14:16:37 +02:00
|
|
|
p_fci_internal->fGetNextCabInVain=TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p_fci_internal->fGetNextCabInVain &&
|
|
|
|
p_fci_internal->fNextCab
|
|
|
|
) {
|
|
|
|
/* THIS CAN NEVER HAPPEN */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* too much data for cabinet */
|
|
|
|
if( (p_fci_internal->fGetNextCabInVain ||
|
|
|
|
p_fci_internal->fNextCab) && (
|
2011-02-15 14:11:22 +01:00
|
|
|
p_fci_internal->ccab.cb < read_result +
|
2005-06-24 14:16:37 +02:00
|
|
|
strlen(p_fci_internal->pccab->szCab)+1+
|
|
|
|
strlen(p_fci_internal->pccab->szDisk)+1
|
|
|
|
)) {
|
|
|
|
|
|
|
|
p_fci_internal->fGetNextCabInVain=FALSE;
|
|
|
|
p_fci_internal->fNextCab=TRUE;
|
2011-02-04 11:32:46 +01:00
|
|
|
return fci_flush_cabinet( p_fci_internal, FALSE, pfnfcignc, pfnfcis);
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( p_fci_internal->fNextCab ) {
|
|
|
|
/* THIS MAY NEVER HAPPEN */
|
2006-02-24 14:28:20 +01:00
|
|
|
/* set error code */
|
2011-02-04 11:20:12 +01:00
|
|
|
set_error( p_fci_internal, FCIERR_NONE, ERROR_GEN_FAILURE );
|
2005-06-24 14:16:37 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the FolderThreshold has been reached flush the folder automatically */
|
2011-02-15 14:11:22 +01:00
|
|
|
if (p_fci_internal->cCompressedBytesInFolder >= p_fci_internal->ccab.cbFolderThresh)
|
2005-06-24 14:16:37 +02:00
|
|
|
return FCIFlushFolder(hfci, pfnfcignc, pfnfcis);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
} /* end of FCIAddFile */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FCIFlushFolder (CABINET.12)
|
|
|
|
*
|
|
|
|
* FCIFlushFolder completes the CFFolder structure under construction.
|
|
|
|
*
|
2008-01-17 00:04:38 +01:00
|
|
|
* All further data which is added by FCIAddFile will be associated to
|
2005-06-24 14:16:37 +02:00
|
|
|
* the next CFFolder structure.
|
|
|
|
*
|
|
|
|
* FCIFlushFolder will be called by FCIAddFile automatically if the
|
|
|
|
* threshold (stored in the member cbFolderThresh of the CCAB structure
|
|
|
|
* pccab passed to FCICreate) is exceeded.
|
|
|
|
*
|
|
|
|
* FCIFlushFolder will be called by FCIFlushFolder automatically before
|
|
|
|
* any data will be written into the cabinet file.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* hfci [I] An HFCI from FCICreate
|
|
|
|
* pfnfcignc [I] A pointer to a function which gets information about
|
|
|
|
* the next cabinet
|
|
|
|
* pfnfcis [IO] A pointer to a function which will report status
|
|
|
|
* information about the compression process
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* On success, returns TRUE
|
|
|
|
* On failure, returns FALSE
|
|
|
|
*
|
|
|
|
* INCLUDES
|
|
|
|
* fci.h
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL __cdecl FCIFlushFolder(
|
|
|
|
HFCI hfci,
|
|
|
|
PFNFCIGETNEXTCABINET pfnfcignc,
|
|
|
|
PFNFCISTATUS pfnfcis)
|
|
|
|
{
|
2011-02-04 11:32:46 +01:00
|
|
|
FCI_Int *p_fci_internal = get_fci_ptr( hfci );
|
|
|
|
|
|
|
|
if (!p_fci_internal) return FALSE;
|
|
|
|
return fci_flush_folder(p_fci_internal,FALSE,pfnfcignc,pfnfcis);
|
|
|
|
}
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FCIFlushCabinet (CABINET.13)
|
|
|
|
*
|
|
|
|
* FCIFlushCabinet stores the data which has been added by FCIAddFile
|
|
|
|
* into the cabinet file. If the maximum cabinet size (stored in the
|
|
|
|
* member cb of the CCAB structure pccab passed to FCICreate) has been
|
|
|
|
* exceeded FCIFlushCabinet will be called automatic by FCIAddFile.
|
|
|
|
* The remaining data still has to be flushed manually by calling
|
|
|
|
* FCIFlushCabinet.
|
|
|
|
*
|
|
|
|
* After FCIFlushCabinet has been called (manually) FCIAddFile must
|
|
|
|
* NOT be called again. Then hfci has to be released by FCIDestroy.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* hfci [I] An HFCI from FCICreate
|
|
|
|
* fGetNextCab [I] Whether you want to add additional files to a
|
|
|
|
* cabinet set (TRUE) or whether you want to
|
|
|
|
* finalize it (FALSE)
|
|
|
|
* pfnfcignc [I] A pointer to a function which gets information about
|
|
|
|
* the next cabinet
|
|
|
|
* pfnfcis [IO] A pointer to a function which will report status
|
|
|
|
* information about the compression process
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* On success, returns TRUE
|
|
|
|
* On failure, returns FALSE
|
|
|
|
*
|
|
|
|
* INCLUDES
|
|
|
|
* fci.h
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL __cdecl FCIFlushCabinet(
|
|
|
|
HFCI hfci,
|
|
|
|
BOOL fGetNextCab,
|
|
|
|
PFNFCIGETNEXTCABINET pfnfcignc,
|
|
|
|
PFNFCISTATUS pfnfcis)
|
|
|
|
{
|
2011-02-04 11:32:46 +01:00
|
|
|
FCI_Int *p_fci_internal = get_fci_ptr( hfci );
|
2005-06-24 14:16:37 +02:00
|
|
|
|
2011-02-04 11:32:46 +01:00
|
|
|
if (!p_fci_internal) return FALSE;
|
|
|
|
|
|
|
|
if(!fci_flush_cabinet(p_fci_internal,fGetNextCab,pfnfcignc,pfnfcis)) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
2011-02-14 20:32:19 +01:00
|
|
|
while( p_fci_internal->files_size>0 ||
|
|
|
|
p_fci_internal->placed_files_size>0 ) {
|
2011-02-04 11:32:46 +01:00
|
|
|
if(!fci_flush_cabinet(p_fci_internal,fGetNextCab,pfnfcignc,pfnfcis)) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2011-02-04 11:32:46 +01:00
|
|
|
}
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FCIDestroy (CABINET.14)
|
|
|
|
*
|
|
|
|
* Frees a handle created by FCICreate.
|
|
|
|
* Only reason for failure would be an invalid handle.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* hfci [I] The HFCI to free
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE for success
|
|
|
|
* FALSE for failure
|
|
|
|
*/
|
|
|
|
BOOL __cdecl FCIDestroy(HFCI hfci)
|
|
|
|
{
|
2011-02-14 20:05:46 +01:00
|
|
|
struct folder *folder, *folder_next;
|
2011-02-14 20:32:19 +01:00
|
|
|
struct file *file, *file_next;
|
2011-02-16 00:20:49 +01:00
|
|
|
struct data_block *block, *block_next;
|
2011-02-04 11:32:46 +01:00
|
|
|
FCI_Int *p_fci_internal = get_fci_ptr( hfci );
|
|
|
|
|
|
|
|
if (!p_fci_internal) return FALSE;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
/* before hfci can be removed all temporary files must be closed */
|
|
|
|
/* and deleted */
|
2011-02-04 11:35:18 +01:00
|
|
|
p_fci_internal->magic = 0;
|
2005-06-24 14:16:37 +02:00
|
|
|
|
2011-02-14 20:05:46 +01:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( folder, folder_next, &p_fci_internal->folders_list, struct folder, entry )
|
|
|
|
{
|
2011-02-14 20:32:19 +01:00
|
|
|
free_folder( p_fci_internal, folder );
|
|
|
|
}
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( file, file_next, &p_fci_internal->files_list, struct file, entry )
|
|
|
|
{
|
|
|
|
free_file( p_fci_internal, file );
|
2011-02-14 20:05:46 +01:00
|
|
|
}
|
2011-02-16 00:20:49 +01:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( block, block_next, &p_fci_internal->blocks_list, struct data_block, entry )
|
|
|
|
{
|
|
|
|
free_data_block( p_fci_internal, block );
|
|
|
|
}
|
2011-02-14 20:05:46 +01:00
|
|
|
|
2011-02-17 16:48:19 +01:00
|
|
|
close_temp_file( p_fci_internal, &p_fci_internal->data );
|
2005-06-24 14:16:37 +02:00
|
|
|
|
|
|
|
/* hfci can now be removed */
|
2011-02-04 11:27:31 +01:00
|
|
|
p_fci_internal->free(hfci);
|
2005-06-24 14:16:37 +02:00
|
|
|
return TRUE;
|
2011-02-04 11:32:46 +01:00
|
|
|
}
|