zip.cpp revision 4d98012b3229f47c9fb4eb767563359484284077
/* $Id$ */
/** @file
* IPRT - Compression.
*/
/*
* Copyright (C) 2006-2009 Oracle Corporation
*
* 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 (GPL) 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define RTZIP_USE_STORE 1
#define RTZIP_USE_ZLIB 1
//#define RTZIP_USE_BZLIB 1
#define RTZIP_USE_LZF 1
#define RTZIP_LZF_BLOCK_BY_BLOCK
//#define RTZIP_USE_LZJB 1
//#define RTZIP_USE_LZO 1
/** @todo FastLZ? QuickLZ? Others? */
/*******************************************************************************
* 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
#ifdef RTZIP_USE_LZJB
# include "lzjb.h"
#endif
#ifdef RTZIP_USE_LZO
#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. */
/** 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
{
# ifndef RTZIP_LZF_BLOCK_BY_BLOCK
/** Current input buffer postition. */
/** The number of bytes left in the input buffer. */
# endif
/** 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.
*/
size_t cb = sizeof(pZip->abBuffer) - (size_t)(pbDst - &pZip->abBuffer[0]); /* careful here, g++ 4.1.2 screws up easily */
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 /* RTZIP_USE_STORE */
#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)
{
/*
* Be greedy reading input, even if no output buffer is left. It's possible
* that it's just the end of stream marker which needs to be read. Happens
* for incompressible blocks just larger than the input buffer size.
*/
{
/*
* 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 /* RTZIP_USE_ZLIB */
#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 /* RTZIP_USE_BZLIB */
#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.
*/
# ifdef RTZIP_LZF_BLOCK_BY_BLOCK
while (cbBuf > 0)
{
/*
* Anything in the spill buffer?
*/
{
if (!cbBuf)
break;
}
/*
* We always read and work one block at a time.
*/
if (RT_FAILURE(rc))
return rc;
if (!rtZipLZFValidHeader(&Hdr))
return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
{
if (RT_FAILURE(rc))
return rc;
}
/*
* 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
{
unsigned cbOutput = lzf_decompress(&pZip->abBuffer[0], Hdr.cbData, pZip->u.LZF.abSpill, cbUncompressed);
if (cbOutput != cbUncompressed)
{
AssertMsgFailed(("Decompression error, errno=%d. cbOutput=%#x cbUncompressed=%#x\n",
return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
}
}
}
if (pcbWritten)
*pcbWritten = cbWritten;
# else /* !RTZIP_LZF_BLOCK_BY_BLOCK */
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;
}
# endif /* !RTZIP_LZF_BLOCK_BY_BLOCK */
return VINF_SUCCESS;
}
/**
* @copydoc RTZipDecompDestroy
*/
{
return VINF_SUCCESS;
}
/**
* Initalize the decompressor instance.
* @returns iprt status code.
* @param pZip The decompressor instance.
*/
{
# ifndef RTZIP_LZF_BLOCK_BY_BLOCK
# endif
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.
*/
/*
* 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 = VERR_NOT_IMPLEMENTED;
switch (enmType)
{
case RTZIPTYPE_STORE:
#ifdef RTZIP_USE_STORE
#endif
break;
case RTZIPTYPE_ZLIB:
#ifdef RTZIP_USE_ZLIB
#endif
break;
case RTZIPTYPE_BZLIB:
#ifdef RTZIP_USE_BZLIB
#endif
break;
case RTZIPTYPE_LZF:
#ifdef RTZIP_USE_LZF
#endif
break;
case RTZIPTYPE_LZJB:
case RTZIPTYPE_LZO:
break;
default:
}
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.
*/
/*
* 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.
*/
{
case RTZIPTYPE_STORE:
#ifdef RTZIP_USE_STORE
#else
AssertMsgFailed(("Store is not include in this build!\n"));
#endif
break;
case RTZIPTYPE_ZLIB:
#ifdef RTZIP_USE_ZLIB
#else
AssertMsgFailed(("Zlib is not include in this build!\n"));
#endif
break;
case RTZIPTYPE_BZLIB:
#ifdef RTZIP_USE_BZLIB
#else
AssertMsgFailed(("BZlib is not include in this build!\n"));
#endif
break;
case RTZIPTYPE_LZF:
#ifdef RTZIP_USE_LZF
#else
AssertMsgFailed(("LZF is not include in this build!\n"));
#endif
break;
case RTZIPTYPE_LZJB:
#ifdef RTZIP_USE_LZJB
AssertMsgFailed(("LZJB streaming support is not implemented yet!\n"));
#else
AssertMsgFailed(("LZJB is not include in this build!\n"));
#endif
break;
case RTZIPTYPE_LZO:
#ifdef RTZIP_USE_LZJB
AssertMsgFailed(("LZO streaming support is not implemented yet!\n"));
#else
AssertMsgFailed(("LZO is not include in this build!\n"));
#endif
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;
}
{
/* input validation - the crash and burn approach as speed is essential here. */
/*
* Deal with flags involving prefixes.
*/
/*
* The type specific part.
*/
switch (enmType)
{
case RTZIPTYPE_LZF:
{
#ifdef RTZIP_USE_LZF
# if 0
static const uint8_t s_abZero4K[] =
{
0x01, 0x00, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff,
0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0,
0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00,
0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff,
0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0,
0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00,
0xe0, 0x7d, 0x00
};
&& ASMMemIsZeroPage(pvSrc))
{
return VERR_BUFFER_OVERFLOW;
*pcbDstActual = sizeof(s_abZero4K);
break;
}
# endif
unsigned cbDstActual = lzf_compress(pvSrc, (unsigned)cbSrc, pvDst, (unsigned)cbDst); /** @todo deal with size type overflows */
return VERR_BUFFER_OVERFLOW;
break;
#else
return VERR_NOT_SUPPORTED;
#endif
}
case RTZIPTYPE_STORE:
{
return VERR_BUFFER_OVERFLOW;
*pcbDstActual = cbSrc;
break;
}
case RTZIPTYPE_LZJB:
{
#ifdef RTZIP_USE_LZJB
if (cbDstActual == cbSrc)
else
break;
#else
return VERR_NOT_SUPPORTED;
#endif
}
case RTZIPTYPE_LZO:
{
#ifdef RTZIP_USE_LZO
return VERR_INTERNAL_ERROR;
switch (rc)
{
case LZO_E_OUTPUT_OVERRUN: return VERR_BUFFER_OVERFLOW;
default: return VERR_GENERAL_FAILURE;
}
break;
#else
return VERR_NOT_SUPPORTED;
#endif
}
case RTZIPTYPE_ZLIB:
case RTZIPTYPE_BZLIB:
return VERR_NOT_SUPPORTED;
default:
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
{
/* input validation - the crash and burn approach as speed is essential here. */
/*
* Deal with flags involving prefixes.
*/
/*
* The type specific part.
*/
switch (enmType)
{
case RTZIPTYPE_LZF:
{
#ifdef RTZIP_USE_LZF
unsigned cbDstActual = lzf_decompress(pvSrc, (unsigned)cbSrc, pvDst, (unsigned)cbDst); /** @todo deal with size type overflows */
{
return VERR_BUFFER_OVERFLOW;
return VERR_GENERAL_FAILURE;
}
if (pcbDstActual)
if (pcbSrcActual)
*pcbSrcActual = cbSrc;
break;
#else
return VERR_NOT_SUPPORTED;
#endif
}
case RTZIPTYPE_STORE:
{
return VERR_BUFFER_OVERFLOW;
if (pcbDstActual)
*pcbDstActual = cbSrc;
if (pcbSrcActual)
*pcbSrcActual = cbSrc;
break;
}
case RTZIPTYPE_LZJB:
{
#ifdef RTZIP_USE_LZJB
{
if (RT_UNLIKELY(rc != 0))
return VERR_GENERAL_FAILURE;
if (pcbDstActual)
*pcbDstActual = cbDst;
}
else
{
if (pcbDstActual)
}
if (pcbSrcActual)
*pcbSrcActual = cbSrc;
break;
#else
return VERR_NOT_SUPPORTED;
#endif
}
case RTZIPTYPE_LZO:
{
#ifdef RTZIP_USE_LZO
return VERR_INTERNAL_ERROR;
switch (rc)
{
case LZO_E_OUTPUT_OVERRUN: return VERR_BUFFER_OVERFLOW;
default:
case LZO_E_INPUT_OVERRUN: return VERR_GENERAL_FAILURE;
}
if (pcbSrcActual)
*pcbSrcActual = cbSrc;
if (pcbDstActual)
break;
#else
return VERR_NOT_SUPPORTED;
#endif
}
case RTZIPTYPE_ZLIB:
case RTZIPTYPE_BZLIB:
return VERR_NOT_SUPPORTED;
default:
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}