diff options
Diffstat (limited to '3rdparty/lzma/C/XzEnc.c')
-rw-r--r-- | 3rdparty/lzma/C/XzEnc.c | 94 |
1 files changed, 56 insertions, 38 deletions
diff --git a/3rdparty/lzma/C/XzEnc.c b/3rdparty/lzma/C/XzEnc.c index 56cfd579c69..ccac7a8ed4b 100644 --- a/3rdparty/lzma/C/XzEnc.c +++ b/3rdparty/lzma/C/XzEnc.c @@ -1,5 +1,7 @@ /* XzEnc.c -- Xz Encode -2011-02-07 : Igor Pavlov : Public domain */ +2015-09-16 : Igor Pavlov : Public domain */ + +#include "Precomp.h" #include <stdlib.h> #include <string.h> @@ -8,6 +10,7 @@ #include "Alloc.h" #include "Bra.h" #include "CpuArch.h" + #ifdef USE_SUBBLOCK #include "Bcj3Enc.c" #include "SbFind.c" @@ -16,14 +19,6 @@ #include "XzEnc.h" -static void *SzBigAlloc(void *p, size_t size) { p = p; return BigAlloc(size); } -static void SzBigFree(void *p, void *address) { p = p; BigFree(address); } -static ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree }; - -static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } -static void SzFree(void *p, void *address) { p = p; MyFree(address); } -static ISzAlloc g_Alloc = { SzAlloc, SzFree }; - #define XzBlock_ClearFlags(p) (p)->flags = 0; #define XzBlock_SetNumFilters(p, n) (p)->flags |= ((n) - 1); #define XzBlock_SetHasPackSize(p) (p)->flags |= XZ_BF_PACK_SIZE; @@ -40,7 +35,7 @@ static SRes WriteBytesAndCrc(ISeqOutStream *s, const void *buf, UInt32 size, UIn return WriteBytes(s, buf, size); } -SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStream *s) +static SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStream *s) { UInt32 crc; Byte header[XZ_STREAM_HEADER_SIZE]; @@ -52,17 +47,19 @@ SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStream *s) return WriteBytes(s, header, XZ_STREAM_HEADER_SIZE); } -SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStream *s) + +static SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStream *s) { Byte header[XZ_BLOCK_HEADER_SIZE_MAX]; unsigned pos = 1; - int numFilters, i; + unsigned numFilters, i; header[pos++] = p->flags; if (XzBlock_HasPackSize(p)) pos += Xz_WriteVarInt(header + pos, p->packSize); if (XzBlock_HasUnpackSize(p)) pos += Xz_WriteVarInt(header + pos, p->unpackSize); numFilters = XzBlock_GetNumFilters(p); + for (i = 0; i < numFilters; i++) { const CXzFilter *f = &p->filters[i]; @@ -71,14 +68,17 @@ SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStream *s) memcpy(header + pos, f->props, f->propsSize); pos += f->propsSize; } - while((pos & 3) != 0) + + while ((pos & 3) != 0) header[pos++] = 0; + header[0] = (Byte)(pos >> 2); SetUi32(header + pos, CrcCalc(header, pos)); return WriteBytes(s, header, pos + 4); } -SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s) + +static SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s) { Byte buf[32]; UInt64 globalPos; @@ -90,6 +90,7 @@ SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s) globalPos = pos; buf[0] = 0; RINOK(WriteBytesAndCrc(s, buf, pos, &crc)); + for (i = 0; i < p->numBlocks; i++) { const CXzBlockSizes *block = &p->blocks[i]; @@ -98,7 +99,9 @@ SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s) globalPos += pos; RINOK(WriteBytesAndCrc(s, buf, pos, &crc)); } + pos = ((unsigned)globalPos & 3); + if (pos != 0) { buf[0] = buf[1] = buf[2] = 0; @@ -123,34 +126,36 @@ SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s) } } -SRes Xz_AddIndexRecord(CXzStream *p, UInt64 unpackSize, UInt64 totalSize, ISzAlloc *alloc) + +static SRes Xz_AddIndexRecord(CXzStream *p, UInt64 unpackSize, UInt64 totalSize, ISzAlloc *alloc) { - if (p->blocks == 0 || p->numBlocksAllocated == p->numBlocks) + if (!p->blocks || p->numBlocksAllocated == p->numBlocks) { - size_t num = (p->numBlocks + 1) * 2; + size_t num = p->numBlocks * 2 + 1; size_t newSize = sizeof(CXzBlockSizes) * num; CXzBlockSizes *blocks; if (newSize / sizeof(CXzBlockSizes) != num) return SZ_ERROR_MEM; - blocks = alloc->Alloc(alloc, newSize); - if (blocks == 0) + blocks = (CXzBlockSizes *)alloc->Alloc(alloc, newSize); + if (!blocks) return SZ_ERROR_MEM; if (p->numBlocks != 0) { memcpy(blocks, p->blocks, p->numBlocks * sizeof(CXzBlockSizes)); - Xz_Free(p, alloc); + alloc->Free(alloc, p->blocks); } p->blocks = blocks; p->numBlocksAllocated = num; } { CXzBlockSizes *block = &p->blocks[p->numBlocks++]; - block->totalSize = totalSize; block->unpackSize = unpackSize; + block->totalSize = totalSize; } return SZ_OK; } + /* ---------- CSeqCheckInStream ---------- */ typedef struct @@ -161,13 +166,13 @@ typedef struct CXzCheck check; } CSeqCheckInStream; -void SeqCheckInStream_Init(CSeqCheckInStream *p, int mode) +static void SeqCheckInStream_Init(CSeqCheckInStream *p, unsigned mode) { p->processed = 0; XzCheck_Init(&p->check, mode); } -void SeqCheckInStream_GetDigest(CSeqCheckInStream *p, Byte *digest) +static void SeqCheckInStream_GetDigest(CSeqCheckInStream *p, Byte *digest) { XzCheck_Final(&p->check, digest); } @@ -181,6 +186,7 @@ static SRes SeqCheckInStream_Read(void *pp, void *data, size_t *size) return res; } + /* ---------- CSeqSizeOutStream ---------- */ typedef struct @@ -198,6 +204,7 @@ static size_t MyWrite(void *pp, const void *data, size_t size) return size; } + /* ---------- CSeqInFilter ---------- */ #define FILTER_BUF_SIZE (1 << 20) @@ -218,8 +225,9 @@ static SRes SeqInFilter_Read(void *pp, void *data, size_t *size) CSeqInFilter *p = (CSeqInFilter *)pp; size_t sizeOriginal = *size; if (sizeOriginal == 0) - return S_OK; + return SZ_OK; *size = 0; + for (;;) { if (!p->srcWasFinished && p->curPos == p->endPos) @@ -274,9 +282,10 @@ static SRes SeqInFilter_Init(CSeqInFilter *p, const CXzFilter *props) RINOK(BraState_SetFromMethod(&p->StateCoder, props->id, 1, &g_Alloc)); RINOK(p->StateCoder.SetProps(p->StateCoder.p, props->props, props->propsSize, &g_Alloc)); p->StateCoder.Init(p->StateCoder.p); - return S_OK; + return SZ_OK; } + /* ---------- CSbEncInStream ---------- */ #ifdef USE_SUBBLOCK @@ -294,6 +303,7 @@ static SRes SbEncInStream_Read(void *pp, void *data, size_t *size) size_t sizeOriginal = *size; if (sizeOriginal == 0) return S_OK; + for (;;) { if (p->enc.needRead && !p->enc.readWasFinished) @@ -308,6 +318,7 @@ static SRes SbEncInStream_Read(void *pp, void *data, size_t *size) } p->enc.needRead = False; } + *size = sizeOriginal; RINOK(SbEnc_Read(&p->enc, data, size)); if (*size != 0 || !p->enc.needRead) @@ -360,7 +371,7 @@ static void Lzma2WithFilters_Construct(CLzma2WithFilters *p, ISzAlloc *alloc, IS static SRes Lzma2WithFilters_Create(CLzma2WithFilters *p) { p->lzma2 = Lzma2Enc_Create(p->alloc, p->bigAlloc); - if (p->lzma2 == 0) + if (!p->lzma2) return SZ_ERROR_MEM; return SZ_OK; } @@ -378,10 +389,11 @@ static void Lzma2WithFilters_Free(CLzma2WithFilters *p) } } + void XzProps_Init(CXzProps *p) { - p->lzma2Props = 0; - p->filterProps = 0; + p->lzma2Props = NULL; + p->filterProps = NULL; p->checkId = XZ_CHECK_CRC32; } @@ -389,10 +401,11 @@ void XzFilterProps_Init(CXzFilterProps *p) { p->id = 0; p->delta = 0; - p->ip= 0; + p->ip = 0; p->ipDefined = False; } + static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf, ISeqOutStream *outStream, ISeqInStream *inStream, const CXzProps *props, ICompressProgress *progress) @@ -406,7 +419,7 @@ static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf, CSeqCheckInStream checkInStream; CSeqSizeOutStream seqSizeOutStream; CXzBlock block; - int filterIndex = 0; + unsigned filterIndex = 0; CXzFilter *filter = NULL; const CXzFilterProps *fp = props->filterProps; @@ -418,6 +431,7 @@ static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf, filter = &block.filters[filterIndex++]; filter->id = fp->id; filter->propsSize = 0; + if (fp->id == XZ_ID_Delta) { filter->props[0] = (Byte)(fp->delta - 1); @@ -465,14 +479,16 @@ static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf, { UInt64 packPos = seqSizeOutStream.processed; + SRes res = Lzma2Enc_Encode(lzmaf->lzma2, &seqSizeOutStream.p, - fp ? - #ifdef USE_SUBBLOCK - (fp->id == XZ_ID_Subblock) ? &lzmaf->sb.p: - #endif - &lzmaf->filter.p: - &checkInStream.p, - progress); + fp ? + #ifdef USE_SUBBLOCK + (fp->id == XZ_ID_Subblock) ? &lzmaf->sb.p: + #endif + &lzmaf->filter.p: + &checkInStream.p, + progress); + RINOK(res); block.unpackSize = checkInStream.processed; block.packSize = seqSizeOutStream.processed - packPos; @@ -481,7 +497,7 @@ static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf, { unsigned padSize = 0; Byte buf[128]; - while((((unsigned)block.packSize + padSize) & 3) != 0) + while ((((unsigned)block.packSize + padSize) & 3) != 0) buf[padSize++] = 0; SeqCheckInStream_GetDigest(&checkInStream, buf + padSize); RINOK(WriteBytes(&seqSizeOutStream.p, buf, padSize + XzFlags_GetCheckSize(xz->flags))); @@ -491,6 +507,7 @@ static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf, return Xz_WriteFooter(xz, outStream); } + SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream, const CXzProps *props, ICompressProgress *progress) { @@ -507,6 +524,7 @@ SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream, return res; } + SRes Xz_EncodeEmpty(ISeqOutStream *outStream) { SRes res; |