zip.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* $Id$ */
/** @file
* InnoTek Portable Runtime - Compression.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define RTZIP_USE_STORE 1
//#define RTZIP_USE_ZLIB 1
//#define RTZIP_USE_BZLIB 1
#define RTZIP_USE_LZF 1
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef RTZIP_USE_BZLIB
# include <bzlib.h>
#endif
#ifdef RTZIP_USE_ZLIB
# include <zlib.h>
#endif
#ifdef RTZIP_USE_LZF
# include <lzf.h>
#endif
#include <errno.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
#ifdef RTZIP_USE_LZF
/**
* LZF block header.
*/
typedef struct RTZIPLZFHDR
{
/** Magic word (RTZIPLZFHDR_MAGIC). */
/** The number of bytes of data following this header. */
/** The CRC32 of the block. */
/** The size of the uncompressed data in bytes. */
} RTZIPLZFHDR;
#pragma pack()
/** Pointer to a LZF block header. */
typedef RTZIPLZFHDR *PRTZIPLZFHDR;
/** Pointer to a const LZF block header. */
typedef const RTZIPLZFHDR *PCRTZIPLZFHDR;
/** The magic of a LZF block header. */
/** The max compressed data size.
* The maximum size of a block is currently 16KB.
* This is very important so we don't have to move input buffers around. */
/** The max uncompressed data size.
* This is important so we don't overflow the spill buffer in the decompressor. */
#endif /* RTZIP_USE_LZF */
/**
* Compressor/Decompressor instance data.
*/
typedef struct RTZIPCOMP
{
/** Output buffer. */
#ifdef RTZIP_USE_LZF
#else
#endif
/** Compression output consumer. */
/** User argument for the callback. */
void *pvUser;
/**
* @copydoc RTZipCompress
*/
/**
* @copydoc RTZipCompFinish
*/
/**
* @copydoc RTZipCompDestroy
*/
/** Compression type. */
/** Type specific data. */
union
{
#ifdef RTZIP_USE_STORE
/** Simple storing. */
struct
{
/** Current buffer postition. (where to start write) */
} Store;
#endif
#ifdef RTZIP_USE_ZLIB
/** Zlib stream. */
#endif
#ifdef RTZIP_USE_BZLIB
/** BZlib stream. */
#endif
#ifdef RTZIP_USE_LZF
/** LZF stream. */
struct
{
/** Current output buffer postition. */
/** The input buffer position. */
/** The number of free bytes in the input buffer. */
/** The input buffer. */
} LZF;
#endif
} u;
} RTZIPCOMP;
/**
* Decompressor instance data.
*/
typedef struct RTZIPDECOMP
{
/** Input buffer. */
/** Decompression input producer. */
/** User argument for the callback. */
void *pvUser;
/**
* @copydoc RTZipDecompress
*/
DECLCALLBACKMEMBER(int, pfnDecompress)(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten);
/**
* @copydoc RTZipDecompDestroy
*/
/** Compression type. */
/** Type specific data. */
union
{
#ifdef RTZIP_USE_STORE
/** Simple storing. */
struct
{
/** Current buffer postition. (where to start read) */
/** Number of bytes left in the buffer. */
} Store;
#endif
#ifdef RTZIP_USE_ZLIB
/** Zlib stream. */
#endif
#ifdef RTZIP_USE_BZLIB
/** BZlib stream. */
#endif
#ifdef RTZIP_USE_LZF
/** LZF 'stream'. */
struct
{
/** Current input buffer postition. */
/** The number of bytes left in the input buffer. */
/** The spill buffer.
* LZF is a block based compressor and not a stream compressor. So,
* we have to decompress full blocks if we want to get any of the data.
* This buffer is to store the spill after decompressing a block. */
/** The number of bytes left spill buffer. */
unsigned cbSpill;
/** The current spill buffer position. */
} LZF;
#endif
} u;
} RTZIPDECOM;
#ifdef RTZIP_USE_STORE
/**
* @copydoc RTZipCompress
*/
{
while (cbBuf)
{
/*
* Flush.
*/
if (cb <= 0)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Add to the buffer and advance.
*/
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompFinish
*/
{
if (cb > 0)
{
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompDestroy
*/
{
return VINF_SUCCESS;
}
/**
* Initializes the compressor instance.
* @returns iprt status code.
* @param pZip The compressor instance.
* @param enmLevel The desired compression level.
*/
{
return VINF_SUCCESS;
}
/**
* @copydoc RTZipDecompress
*/
static DECLCALLBACK(int) rtZipStoreDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
while (cbBuf)
{
/*
* Fill buffer.
*/
if (cb <= 0)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* No more data?
*/
if (cb == 0)
{
if (pcbWritten)
{
*pcbWritten = cbWritten;
return VINF_SUCCESS;
}
return VERR_NO_DATA;
}
/*
* Add to the buffer and advance.
*/
}
if (pcbWritten)
*pcbWritten = cbWritten;
return VINF_SUCCESS;
}
/**
* @copydoc RTZipDecompDestroy
*/
{
return VINF_SUCCESS;
}
/**
* Initialize the decompressor instance.
* @returns iprt status code.
* @param pZip The decompressor instance.
*/
{
return VINF_SUCCESS;
}
#endif
#ifdef RTZIP_USE_ZLIB
/**
* Convert from zlib errno to iprt status code.
* @returns iprt status code.
* @param rc Zlib error code.
*/
static int zipErrConvertFromZlib(int rc)
{
/** @todo proper zlib error convertion. */
switch (rc)
{
case Z_ERRNO:
return RTErrConvertFromErrno(errno);
case Z_STREAM_ERROR:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
case Z_BUF_ERROR:
case Z_VERSION_ERROR:
return VERR_GENERAL_FAILURE;
default:
if (rc >= 0)
return VINF_SUCCESS;
return VERR_GENERAL_FAILURE;
}
}
/**
* @copydoc RTZipCompress
*/
{
{
/*
* Flush output buffer?
*/
{
int rc = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.Zlib.avail_out);
if (RT_FAILURE(rc))
return rc;
}
/*
* Pass it on to zlib.
*/
return zipErrConvertFromZlib(rc);
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompFinish
*/
{
for (;;)
{
/*
* Flush outstanding stuff. writes.
*/
{
int rc2 = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.Zlib.avail_out);
if (RT_FAILURE(rc2))
return rc2;
if (rc == Z_STREAM_END)
return VINF_SUCCESS;
}
/*
* Tell zlib to flush.
*/
return zipErrConvertFromZlib(rc);
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompDestroy
*/
{
/*
* Terminate the deflate instance.
*/
return rc;
}
/**
* Initializes the compressor instance.
* @returns iprt status code.
* @param pZip The compressor instance.
* @param enmLevel The desired compression level.
*/
{
int iLevel = Z_DEFAULT_COMPRESSION;
switch (enmLevel)
{
case RTZIPLEVEL_STORE: iLevel = 0; break;
}
}
/**
* @copydoc RTZipDecompress
*/
static DECLCALLBACK(int) rtZipZlibDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
{
/*
* Read more input?
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Pass it on to zlib.
*/
if (rc == Z_STREAM_END)
{
if (pcbWritten)
return VERR_NO_DATA;
break;
}
return zipErrConvertFromZlib(rc);
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipDecompDestroy
*/
{
/*
* Terminate the deflate instance.
*/
return rc;
}
/**
* Initialize the decompressor instance.
* @returns iprt status code.
* @param pZip The decompressor instance.
*/
{
}
#endif
#ifdef RTZIP_USE_BZLIB
/**
* Convert from BZlib errno to iprt status code.
* @returns iprt status code.
* @param rc BZlib error code.
*/
static int zipErrConvertFromBZlib(int rc)
{
/** @todo proper bzlib error convertion. */
switch (rc)
{
case BZ_SEQUENCE_ERROR:
AssertMsgFailed(("BZ_SEQUENCE_ERROR shall not happen!\n"));
return VERR_GENERAL_FAILURE;
case BZ_PARAM_ERROR:
return VERR_INVALID_PARAMETER;
case BZ_MEM_ERROR:
return VERR_NO_MEMORY;
case BZ_DATA_ERROR:
case BZ_DATA_ERROR_MAGIC:
case BZ_IO_ERROR:
case BZ_UNEXPECTED_EOF:
case BZ_CONFIG_ERROR:
return VERR_GENERAL_FAILURE;
case BZ_OUTBUFF_FULL:
AssertMsgFailed(("BZ_OUTBUFF_FULL shall not happen!\n"));
return VERR_GENERAL_FAILURE;
default:
if (rc >= 0)
return VINF_SUCCESS;
return VERR_GENERAL_FAILURE;
}
}
/**
* @copydoc RTZipCompress
*/
{
{
/*
* Flush output buffer?
*/
{
int rc = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.BZlib.avail_out);
if (RT_FAILURE(rc))
return rc;
}
/*
* Pass it on to zlib.
*/
return zipErrConvertFromBZlib(rc);
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompFinish
*/
{
int rc = BZ_FINISH_OK;
for (;;)
{
/*
* Flush output buffer?
*/
{
int rc2 = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.BZlib.avail_out);
if (RT_FAILURE(rc2))
return rc2;
if (rc == BZ_STREAM_END)
return VINF_SUCCESS;
}
/*
* Tell BZlib to finish it.
*/
return zipErrConvertFromBZlib(rc);
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompDestroy
*/
{
/*
* Terminate the deflate instance.
*/
return rc;
}
/**
* Initializes the compressor instance.
* @returns iprt status code.
* @param pZip The compressor instance.
* @param enmLevel The desired compression level.
*/
{
int iSize = 6;
int iWork = 0;
switch (enmLevel)
{
}
}
/**
* @copydoc RTZipDecompress
*/
static DECLCALLBACK(int) rtZipBZlibDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
{
/*
* Read more output buffer?
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Pass it on to zlib.
*/
{
if (pcbWritten)
return VERR_NO_DATA;
break;
}
if (rc < 0)
return zipErrConvertFromBZlib(rc);
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipDecompDestroy
*/
{
/*
* Terminate the deflate instance.
*/
return rc;
}
/**
* Initialize the decompressor instance.
* @returns iprt status code.
* @param pZip The decompressor instance.
*/
{
}
#endif
#ifdef RTZIP_USE_LZF
/**
* Flushes the output buffer.
* @returns iprt status code.
* @param pZip The compressor instance.
*/
{
}
/**
* Compresses a buffer using LZF.
*
* @returns VBox status code.
* @param pZip The compressor instance.
* @param pbBuf What to compress.
* @param cbBuf How much to compress.
*/
{
bool fForceFlush = false;
while (cbBuf > 0)
{
/*
* Flush output buffer?
*/
if ( fForceFlush
{
if (RT_FAILURE(rc))
return rc;
fForceFlush = false;
}
/*
* Setup the block header.
*/
pHdr->cbUncompressed = 0;
/*
* Compress data for the block.
*
* We try compress as much as we have freespace for at first,
* but if it turns out the compression is inefficient, we'll
* reduce the size of data we try compress till it fits the
* output space.
*/
if (!cbOutput)
{
/** @todo add an alternative method which stores the raw data if bad compression. */
do
{
cbInput /= 2;
if (!cbInput)
{
return VERR_INTERNAL_ERROR;
}
} while (!cbOutput);
fForceFlush = true;
}
/*
* Upate the header and advance the input buffer.
*/
//pHdr->u32CRC = RTCrc32(pbBuf, cbInput); - too slow
}
return VINF_SUCCESS;
}
/**
* Flushes the input buffer.
* @returns iprt status code.
* @param pZip The compressor instance.
*/
{
if (cb)
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompress
*/
{
#define RTZIPLZF_SMALL_CHUNK (128)
/*
* Flush the input buffer if necessary.
*/
if ( ( cbBuf <= RTZIPLZF_SMALL_CHUNK
|| ( cbBuf > RTZIPLZF_SMALL_CHUNK
)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* If it's a relativly small block put it in the input buffer, elsewise
* compress directly it.
*/
if (cbBuf <= RTZIPLZF_SMALL_CHUNK)
{
}
else
{
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipCompFinish
*/
{
if (RT_SUCCESS(rc))
return rc;
}
/**
* @copydoc RTZipCompDestroy
*/
{
return VINF_SUCCESS;
}
/**
* Initializes the compressor instance.
* @returns iprt status code.
* @param pZip The compressor instance.
* @param enmLevel The desired compression level.
*/
{
return VINF_SUCCESS;
}
/**
* This will validate a header and to all the necessary bitching if it's invalid.
* @returns true if valid.
* @returns false if invalid.
* @param pHdr Pointer to the header.\
*/
{
|| !pHdr->cbUncompressed
)
{
return false;
}
return true;
}
/**
* @copydoc RTZipDecompress
*/
static DECLCALLBACK(int) rtZipLZFDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
/*
* Decompression loop.
*
* This a bit ugly because we have to deal with reading block...
* To simplify matters we've put a max block size and will never
* fill the input buffer with more than allows us to complete
* any partially read blocks.
*
* When possible we decompress directly to the user buffer, when
* not possible we'll use the spill buffer.
*/
while (cbBuf > 0)
{
/*
* Anything in the spill buffer?
*/
{
if (pcbWritten)
*pcbWritten = cb;
if (!cbBuf)
break;
}
/*
* Incomplete header or nothing at all.
*/
{
{
/* empty, fill the buffer. */
if (RT_FAILURE(rc))
return rc;
}
else
{
/* move the header up and fill the buffer. */
if (RT_FAILURE(rc))
return rc;
}
/*
* Validate the header.
*/
if (!rtZipLZFValidHeader(pHdr))
return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
}
else
{
/*
* Validate the header and check if it's an incomplete block.
*/
if (!rtZipLZFValidHeader(pHdr))
return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
{
/* read the remainder of the block. */
Assert(&pZip->u.LZF.pbInput[pZip->u.LZF.cbInput + cbToRead] <= &pZip->u.LZF.pbInput[sizeof(pZip->abBuffer)]);
if (RT_FAILURE(rc))
return rc;
}
}
VERR_GENERAL_FAILURE); /** @todo Get better error codes for RTZip! */
/*
* Does the uncompressed data fit into the supplied buffer?
* If so we uncompress it directly into the user buffer, else we'll have to use the spill buffer.
*/
if (cbUncompressed <= cbBuf)
{
if (cbOutput != cbUncompressed)
{
AssertMsgFailed(("Decompression error, errno=%d. cbOutput=%#x cbUncompressed=%#x\n",
return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
}
cbBuf -= cbUncompressed;
}
else
{
if (cbOutput != cbUncompressed)
{
AssertMsgFailed(("Decompression error, errno=%d. cbOutput=%#x cbUncompressed=%#x\n",
return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
}
}
/* advance the input buffer */
if (pcbWritten)
*pcbWritten += cbUncompressed;
}
return VINF_SUCCESS;
}
/**
* @copydoc RTZipDecompDestroy
*/
{
return VINF_SUCCESS;
}
/**
* Initalize the decompressor instance.
* @returns iprt status code.
* @param pZip The decompressor instance.
*/
{
return VINF_SUCCESS;
}
#endif /* RTZIP_USE_LZF */
/**
* Create a compressor instance.
*
* @returns iprt status code.
* @param ppZip Where to store the instance handle.
* @param pvUser User argument which will be passed on to pfnOut and pfnIn.
* @param pfnOut Callback for consuming output of compression.
* @param enmType Type of compressor to create.
* @param enmLevel Compression level.
*/
RTDECL(int) RTZipCompCreate(PRTZIPCOMP *ppZip, void *pvUser, PFNRTZIPOUT pfnOut, RTZIPTYPE enmType, RTZIPLEVEL enmLevel)
{
/*
* Validate input.
*/
if ( enmType < RTZIPTYPE_AUTO
|| enmType > RTZIPTYPE_LZF)
{
return VERR_INVALID_PARAMETER;
}
if ( enmLevel < RTZIPLEVEL_STORE
|| enmLevel > RTZIPLEVEL_MAX)
{
return VERR_INVALID_PARAMETER;
}
{
AssertMsgFailed(("Must supply pfnOut and ppZip!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Allocate memory for the instance data.
*/
if (!pZip)
return VERR_NO_MEMORY;
/*
* Determin auto type.
*/
if (enmType == RTZIPTYPE_AUTO)
{
if (enmLevel == RTZIPLEVEL_STORE)
else
{
#if defined(RTZIP_USE_ZLIB) && defined(RTZIP_USE_BZLIB)
if (enmLevel == RTZIPLEVEL_MAX)
else
#elif defined(RTZIP_USE_ZLIB)
#elif defined(RTZIP_USE_BZLIB)
#else
#endif
}
}
/*
* Init instance.
*/
int rc = VINF_SUCCESS;
switch (enmType)
{
#ifdef RTZIP_USE_STORE
case RTZIPTYPE_STORE:
break;
#endif
#ifdef RTZIP_USE_ZLIB
case RTZIPTYPE_ZLIB:
break;
#endif
#ifdef RTZIP_USE_BZLIB
case RTZIPTYPE_BZLIB:
break;
#endif
#ifdef RTZIP_USE_LZF
case RTZIPTYPE_LZF:
break;
#endif
default:
AssertMsgFailed(("Not implemented!\n"));
break;
}
if (RT_SUCCESS(rc))
else
return rc;
}
/**
* Compresses a chunk of memory.
*
* @returns iprt status code.
* @param pZip The compressor instance.
* @param pvBuf Pointer to buffer containing the bits to compress.
* @param cbBuf Number of bytes to compress.
*/
{
if (!cbBuf)
return VINF_SUCCESS;
}
/**
* Finishes the compression.
* This will flush all data and terminate the compression data stream.
*
* @returns iprt status code.
* @param pZip The compressor instance.
*/
{
}
/**
* Destroys the compressor instance.
*
* @returns iprt status code.
* @param pZip The compressor instance.
*/
{
/*
* Compressor specific destruction attempt first.
*/
/*
* Free the instance memory.
*/
return VINF_SUCCESS;
}
/**
* @copydoc RTZipDecompress
*/
static DECLCALLBACK(int) rtZipStubDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
return VERR_NOT_SUPPORTED;
}
/**
* @copydoc RTZipDecompDestroy
*/
{
return VINF_SUCCESS;
}
/**
* Create a decompressor instance.
*
* @returns iprt status code.
* @param ppZip Where to store the instance handle.
* @param pvUser User argument which will be passed on to pfnOut and pfnIn.
* @param pfnIn Callback for producing input for decompression.
*/
{
/*
* Validate input.
*/
{
AssertMsgFailed(("Must supply pfnIn and ppZip!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Allocate memory for the instance data.
*/
if (!pZip)
return VERR_NO_MEMORY;
/*
* Init instance.
*/
return VINF_SUCCESS;
}
/**
* Lazy init of the decompressor.
* @returns iprt status code.
* @param pZip The decompressor instance.
*/
{
/*
* Read the first byte from the stream so we can determin the type.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Determin type and do type specific init.
*/
{
#ifdef RTZIP_USE_STORE
case RTZIPTYPE_STORE:
break;
#endif
case RTZIPTYPE_ZLIB:
#ifdef RTZIP_USE_ZLIB
#else
#endif
break;
case RTZIPTYPE_BZLIB:
#ifdef RTZIP_USE_BZLIB
#else
#endif
break;
case RTZIPTYPE_LZF:
#ifdef RTZIP_USE_LZF
#else
#endif
break;
case RTZIPTYPE_INVALID:
AssertMsgFailed(("Invalid compression type RTZIPTYPE_INVALID!\n"));
break;
case RTZIPTYPE_AUTO:
AssertMsgFailed(("Invalid compression type RTZIPTYPE_AUTO!\n"));
break;
default:
break;
}
if (RT_FAILURE(rc))
{
}
return rc;
}
/**
* Decompresses a chunk of memory.
*
* @returns iprt status code.
* @param pZip The decompressor instance.
* @param pvBuf Where to store the decompressed data.
* @param cbBuf Number of bytes to produce. If pcbWritten is set
* any number of bytes up to cbBuf might be returned.
* @param pcbWritten Number of bytes actually written to the buffer. If NULL
* cbBuf number of bytes must be written.
*/
{
/*
* Skip empty requests.
*/
if (!cbBuf)
return VINF_SUCCESS;
/*
* Lazy init.
*/
if (!pZip->pfnDecompress)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* 'Read' the decompressed stream.
*/
}
/**
* Destroys the decompressor instance.
*
* @returns iprt status code.
* @param pZip The decompressor instance.
*/
{
/*
* Destroy compressor instance and flush the output buffer.
*/
/*
* Free the instance memory.
*/
return rc;
}