Reformat SevenZip (Resharper)

This commit is contained in:
Vladimir Kononovich 2020-12-19 23:32:06 +03:00
parent 4925698573
commit 68c88a6b7c
35 changed files with 5095 additions and 5064 deletions

View file

@ -15,24 +15,24 @@ extern const Byte k7zSignature[k7zSignatureSize];
typedef struct typedef struct
{ {
const Byte *Data; const Byte* Data;
size_t Size; size_t Size;
} CSzData; } CSzData;
/* CSzCoderInfo & CSzFolder support only default methods */ /* CSzCoderInfo & CSzFolder support only default methods */
typedef struct typedef struct
{ {
size_t PropsOffset; size_t PropsOffset;
UInt32 MethodID; UInt32 MethodID;
Byte NumStreams; Byte NumStreams;
Byte PropsSize; Byte PropsSize;
} CSzCoderInfo; } CSzCoderInfo;
typedef struct typedef struct
{ {
UInt32 InIndex; UInt32 InIndex;
UInt32 OutIndex; UInt32 OutIndex;
} CSzBond; } CSzBond;
#define SZ_NUM_CODERS_IN_FOLDER_MAX 4 #define SZ_NUM_CODERS_IN_FOLDER_MAX 4
@ -41,35 +41,35 @@ typedef struct
typedef struct typedef struct
{ {
UInt32 NumCoders; UInt32 NumCoders;
UInt32 NumBonds; UInt32 NumBonds;
UInt32 NumPackStreams; UInt32 NumPackStreams;
UInt32 UnpackStream; UInt32 UnpackStream;
UInt32 PackStreams[SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX]; UInt32 PackStreams[SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX];
CSzBond Bonds[SZ_NUM_BONDS_IN_FOLDER_MAX]; CSzBond Bonds[SZ_NUM_BONDS_IN_FOLDER_MAX];
CSzCoderInfo Coders[SZ_NUM_CODERS_IN_FOLDER_MAX]; CSzCoderInfo Coders[SZ_NUM_CODERS_IN_FOLDER_MAX];
} CSzFolder; } CSzFolder;
SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd); SRes SzGetNextFolderItem(CSzFolder* f, CSzData* sd);
typedef struct typedef struct
{ {
UInt32 Low; UInt32 Low;
UInt32 High; UInt32 High;
} CNtfsFileTime; } CNtfsFileTime;
typedef struct typedef struct
{ {
Byte *Defs; /* MSB 0 bit numbering */ Byte* Defs; /* MSB 0 bit numbering */
UInt32 *Vals; UInt32* Vals;
} CSzBitUi32s; } CSzBitUi32s;
typedef struct typedef struct
{ {
Byte *Defs; /* MSB 0 bit numbering */ Byte* Defs; /* MSB 0 bit numbering */
// UInt64 *Vals; // UInt64 *Vals;
CNtfsFileTime *Vals; CNtfsFileTime* Vals;
} CSzBitUi64s; } CSzBitUi64s;
#define SzBitArray_Check(p, i) (((p)[(i) >> 3] & (0x80 >> ((i) & 7))) != 0) #define SzBitArray_Check(p, i) (((p)[(i) >> 3] & (0x80 >> ((i) & 7))) != 0)
@ -78,62 +78,62 @@ typedef struct
typedef struct typedef struct
{ {
UInt32 NumPackStreams; UInt32 NumPackStreams;
UInt32 NumFolders; UInt32 NumFolders;
UInt64 *PackPositions; // NumPackStreams + 1 UInt64* PackPositions; // NumPackStreams + 1
CSzBitUi32s FolderCRCs; // NumFolders CSzBitUi32s FolderCRCs; // NumFolders
size_t *FoCodersOffsets; // NumFolders + 1 size_t* FoCodersOffsets; // NumFolders + 1
UInt32 *FoStartPackStreamIndex; // NumFolders + 1 UInt32* FoStartPackStreamIndex; // NumFolders + 1
UInt32 *FoToCoderUnpackSizes; // NumFolders + 1 UInt32* FoToCoderUnpackSizes; // NumFolders + 1
Byte *FoToMainUnpackSizeIndex; // NumFolders Byte* FoToMainUnpackSizeIndex; // NumFolders
UInt64 *CoderUnpackSizes; // for all coders in all folders UInt64* CoderUnpackSizes; // for all coders in all folders
Byte *CodersData; Byte* CodersData;
} CSzAr; } CSzAr;
UInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex); UInt64 SzAr_GetFolderUnpackSize(const CSzAr* p, UInt32 folderIndex);
SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex, SRes SzAr_DecodeFolder(const CSzAr* p, UInt32 folderIndex,
ILookInStream *stream, UInt64 startPos, ILookInStream* stream, UInt64 startPos,
Byte *outBuffer, size_t outSize, Byte* outBuffer, size_t outSize,
ISzAlloc *allocMain); ISzAlloc* allocMain);
typedef struct typedef struct
{ {
CSzAr db; CSzAr db;
UInt64 startPosAfterHeader; UInt64 startPosAfterHeader;
UInt64 dataPos; UInt64 dataPos;
UInt32 NumFiles;
UInt64 *UnpackPositions; // NumFiles + 1 UInt32 NumFiles;
// Byte *IsEmptyFiles;
Byte *IsDirs;
CSzBitUi32s CRCs;
CSzBitUi32s Attribs; UInt64* UnpackPositions; // NumFiles + 1
// CSzBitUi32s Parents; // Byte *IsEmptyFiles;
CSzBitUi64s MTime; Byte* IsDirs;
CSzBitUi64s CTime; CSzBitUi32s CRCs;
UInt32 *FolderToFile; // NumFolders + 1 CSzBitUi32s Attribs;
UInt32 *FileToFolder; // NumFiles // CSzBitUi32s Parents;
CSzBitUi64s MTime;
CSzBitUi64s CTime;
size_t *FileNameOffsets; /* in 2-byte steps */ UInt32* FolderToFile; // NumFolders + 1
Byte *FileNames; /* UTF-16-LE */ UInt32* FileToFolder; // NumFiles
size_t* FileNameOffsets; /* in 2-byte steps */
Byte* FileNames; /* UTF-16-LE */
} CSzArEx; } CSzArEx;
#define SzArEx_IsDir(p, i) (SzBitArray_Check((p)->IsDirs, i)) #define SzArEx_IsDir(p, i) (SzBitArray_Check((p)->IsDirs, i))
#define SzArEx_GetFileSize(p, i) ((p)->UnpackPositions[(i) + 1] - (p)->UnpackPositions[i]) #define SzArEx_GetFileSize(p, i) ((p)->UnpackPositions[(i) + 1] - (p)->UnpackPositions[i])
void SzArEx_Init(CSzArEx *p); void SzArEx_Init(CSzArEx* p);
void SzArEx_Free(CSzArEx *p, ISzAlloc *alloc); void SzArEx_Free(CSzArEx* p, ISzAlloc* alloc);
UInt64 SzArEx_GetFolderStreamPos(const CSzArEx *p, UInt32 folderIndex, UInt32 indexInFolder); UInt64 SzArEx_GetFolderStreamPos(const CSzArEx* p, UInt32 folderIndex, UInt32 indexInFolder);
int SzArEx_GetFolderFullPackSize(const CSzArEx *p, UInt32 folderIndex, UInt64 *resSize); int SzArEx_GetFolderFullPackSize(const CSzArEx* p, UInt32 folderIndex, UInt64* resSize);
/* /*
if dest == NULL, the return value specifies the required size of the buffer, if dest == NULL, the return value specifies the required size of the buffer,
@ -141,7 +141,7 @@ if dest == NULL, the return value specifies the required size of the buffer,
if dest != NULL, the return value specifies the number of 16-bit characters that if dest != NULL, the return value specifies the number of 16-bit characters that
are written to the dest, including the null-terminating character. */ are written to the dest, including the null-terminating character. */
size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest); size_t SzArEx_GetFileNameUtf16(const CSzArEx* p, size_t fileIndex, UInt16* dest);
/* /*
size_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex); size_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex);
@ -149,7 +149,6 @@ UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16
*/ */
/* /*
SzArEx_Extract extracts file from archive SzArEx_Extract extracts file from archive
@ -171,16 +170,16 @@ UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16
*/ */
SRes SzArEx_Extract( SRes SzArEx_Extract(
const CSzArEx *db, const CSzArEx* db,
ILookInStream *inStream, ILookInStream* inStream,
UInt32 fileIndex, /* index of file */ UInt32 fileIndex, /* index of file */
UInt32 *blockIndex, /* index of solid block */ UInt32* blockIndex, /* index of solid block */
Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */ Byte** outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */
size_t *outBufferSize, /* buffer size for output buffer */ size_t* outBufferSize, /* buffer size for output buffer */
size_t *offset, /* offset of stream for required file in *outBuffer */ size_t* offset, /* offset of stream for required file in *outBuffer */
size_t *outSizeProcessed, /* size of file in *outBuffer */ size_t* outSizeProcessed, /* size of file in *outBuffer */
ISzAlloc *allocMain, ISzAlloc* allocMain,
ISzAlloc *allocTemp); ISzAlloc* allocTemp);
/* /*
@ -194,8 +193,8 @@ SZ_ERROR_INPUT_EOF
SZ_ERROR_FAIL SZ_ERROR_FAIL
*/ */
SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream, SRes SzArEx_Open(CSzArEx* p, ILookInStream* inStream,
ISzAlloc *allocMain, ISzAlloc *allocTemp); ISzAlloc* allocMain, ISzAlloc* allocTemp);
EXTERN_C_END EXTERN_C_END

View file

@ -20,59 +20,59 @@ int g_allocCountTemp = 0;
#endif #endif
void *SzAlloc(void *p, size_t size) void* SzAlloc(void* p, size_t size)
{ {
UNUSED_VAR(p); UNUSED_VAR(p);
if (size == 0) if (size == 0)
return 0; return 0;
#ifdef _SZ_ALLOC_DEBUG #ifdef _SZ_ALLOC_DEBUG
fprintf(stderr, "\nAlloc %10u bytes; count = %10d", (unsigned)size, g_allocCount); fprintf(stderr, "\nAlloc %10u bytes; count = %10d", (unsigned)size, g_allocCount);
g_allocCount++; g_allocCount++;
#endif #endif
return malloc(size); return malloc(size);
} }
void SzFree(void *p, void *address) void SzFree(void* p, void* address)
{ {
UNUSED_VAR(p); UNUSED_VAR(p);
#ifdef _SZ_ALLOC_DEBUG #ifdef _SZ_ALLOC_DEBUG
if (address != 0) if (address != 0)
{ {
g_allocCount--; g_allocCount--;
fprintf(stderr, "\nFree; count = %10d", g_allocCount); fprintf(stderr, "\nFree; count = %10d", g_allocCount);
} }
#endif #endif
free(address); free(address);
} }
void *SzAllocTemp(void *p, size_t size) void* SzAllocTemp(void* p, size_t size)
{ {
UNUSED_VAR(p); UNUSED_VAR(p);
if (size == 0) if (size == 0)
return 0; return 0;
#ifdef _SZ_ALLOC_DEBUG #ifdef _SZ_ALLOC_DEBUG
fprintf(stderr, "\nAlloc_temp %10u bytes; count = %10d", (unsigned)size, g_allocCountTemp); fprintf(stderr, "\nAlloc_temp %10u bytes; count = %10d", (unsigned)size, g_allocCountTemp);
g_allocCountTemp++; g_allocCountTemp++;
#ifdef _WIN32 #ifdef _WIN32
return HeapAlloc(GetProcessHeap(), 0, size); return HeapAlloc(GetProcessHeap(), 0, size);
#endif #endif
#endif #endif
return malloc(size); return malloc(size);
} }
void SzFreeTemp(void *p, void *address) void SzFreeTemp(void* p, void* address)
{ {
UNUSED_VAR(p); UNUSED_VAR(p);
#ifdef _SZ_ALLOC_DEBUG #ifdef _SZ_ALLOC_DEBUG
if (address != 0) if (address != 0)
{ {
g_allocCountTemp--; g_allocCountTemp--;
fprintf(stderr, "\nFree_temp; count = %10d", g_allocCountTemp); fprintf(stderr, "\nFree_temp; count = %10d", g_allocCountTemp);
} }
#ifdef _WIN32 #ifdef _WIN32
HeapFree(GetProcessHeap(), 0, address); HeapFree(GetProcessHeap(), 0, address);
return; return;
#endif #endif
#endif #endif
free(address); free(address);
} }

View file

@ -10,11 +10,11 @@
extern "C" { extern "C" {
#endif #endif
void *SzAlloc(void *p, size_t size); void* SzAlloc(void* p, size_t size);
void SzFree(void *p, void *address); void SzFree(void* p, void* address);
void *SzAllocTemp(void *p, size_t size); void* SzAllocTemp(void* p, size_t size);
void SzFreeTemp(void *p, void *address); void SzFreeTemp(void* p, void* address);
#ifdef __cplusplus #ifdef __cplusplus
} }

File diff suppressed because it is too large Load diff

View file

@ -5,32 +5,32 @@
#include "7zBuf.h" #include "7zBuf.h"
void Buf_Init(CBuf *p) void Buf_Init(CBuf* p)
{ {
p->data = 0; p->data = 0;
p->size = 0; p->size = 0;
} }
int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc) int Buf_Create(CBuf* p, size_t size, ISzAlloc* alloc)
{ {
p->size = 0; p->size = 0;
if (size == 0) if (size == 0)
{ {
p->data = 0; p->data = 0;
return 1; return 1;
} }
p->data = (Byte *)alloc->Alloc(alloc, size); p->data = (Byte*)alloc->Alloc(alloc, size);
if (p->data != 0) if (p->data != 0)
{ {
p->size = size; p->size = size;
return 1; return 1;
} }
return 0; return 0;
} }
void Buf_Free(CBuf *p, ISzAlloc *alloc) void Buf_Free(CBuf* p, ISzAlloc* alloc)
{ {
alloc->Free(alloc, p->data); alloc->Free(alloc, p->data);
p->data = 0; p->data = 0;
p->size = 0; p->size = 0;
} }

View file

@ -10,25 +10,25 @@ EXTERN_C_BEGIN
typedef struct typedef struct
{ {
Byte *data; Byte* data;
size_t size; size_t size;
} CBuf; } CBuf;
void Buf_Init(CBuf *p); void Buf_Init(CBuf* p);
int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc); int Buf_Create(CBuf* p, size_t size, ISzAlloc* alloc);
void Buf_Free(CBuf *p, ISzAlloc *alloc); void Buf_Free(CBuf* p, ISzAlloc* alloc);
typedef struct typedef struct
{ {
Byte *data; Byte* data;
size_t size; size_t size;
size_t pos; size_t pos;
} CDynBuf; } CDynBuf;
void DynBuf_Construct(CDynBuf *p); void DynBuf_Construct(CDynBuf* p);
void DynBuf_SeekToBeg(CDynBuf *p); void DynBuf_SeekToBeg(CDynBuf* p);
int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAlloc *alloc); int DynBuf_Write(CDynBuf* p, const Byte* buf, size_t size, ISzAlloc* alloc);
void DynBuf_Free(CDynBuf *p, ISzAlloc *alloc); void DynBuf_Free(CDynBuf* p, ISzAlloc* alloc);
EXTERN_C_END EXTERN_C_END

View file

@ -9,7 +9,7 @@
#define kCrcPoly 0xEDB88320 #define kCrcPoly 0xEDB88320
#ifdef MY_CPU_LE #ifdef MY_CPU_LE
#define CRC_NUM_TABLES 8 #define CRC_NUM_TABLES 8
#else #else
#define CRC_NUM_TABLES 9 #define CRC_NUM_TABLES 9
@ -20,11 +20,11 @@
#endif #endif
#ifndef MY_CPU_BE #ifndef MY_CPU_BE
UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table); UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void* data, size_t size, const UInt32* table);
UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table); UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void* data, size_t size, const UInt32* table);
#endif #endif
typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void *data, size_t size, const UInt32 *table); typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void* data, size_t size, const UInt32* table);
CRC_FUNC g_CrcUpdateT4; CRC_FUNC g_CrcUpdateT4;
CRC_FUNC g_CrcUpdateT8; CRC_FUNC g_CrcUpdateT8;
@ -32,82 +32,82 @@ CRC_FUNC g_CrcUpdate;
UInt32 g_CrcTable[256 * CRC_NUM_TABLES]; UInt32 g_CrcTable[256 * CRC_NUM_TABLES];
UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size) UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void* data, size_t size)
{ {
return g_CrcUpdate(v, data, size, g_CrcTable); return g_CrcUpdate(v, data, size, g_CrcTable);
} }
UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size) UInt32 MY_FAST_CALL CrcCalc(const void* data, size_t size)
{ {
return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL; return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL;
} }
#define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table) UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void* data, size_t size, const UInt32* table)
{ {
const Byte *p = (const Byte *)data; const Byte* p = (const Byte*)data;
const Byte *pEnd = p + size; const Byte* pEnd = p + size;
for (; p != pEnd; p++) for (; p != pEnd; p++)
v = CRC_UPDATE_BYTE_2(v, *p); v = CRC_UPDATE_BYTE_2(v, *p);
return v; return v;
} }
void MY_FAST_CALL CrcGenerateTable() void MY_FAST_CALL CrcGenerateTable()
{ {
UInt32 i; UInt32 i;
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
{ {
UInt32 r = i; UInt32 r = i;
unsigned j; unsigned j;
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1)); r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
g_CrcTable[i] = r; g_CrcTable[i] = r;
} }
for (; i < 256 * CRC_NUM_TABLES; i++) for (; i < 256 * CRC_NUM_TABLES; i++)
{ {
UInt32 r = g_CrcTable[i - 256]; UInt32 r = g_CrcTable[i - 256];
g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8); g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8);
} }
#if CRC_NUM_TABLES < 4 #if CRC_NUM_TABLES < 4
g_CrcUpdate = CrcUpdateT1; g_CrcUpdate = CrcUpdateT1;
#else #else
#ifdef MY_CPU_LE
g_CrcUpdateT4 = CrcUpdateT4; #ifdef MY_CPU_LE
g_CrcUpdate = CrcUpdateT4;
#if CRC_NUM_TABLES >= 8 g_CrcUpdateT4 = CrcUpdateT4;
g_CrcUpdateT8 = CrcUpdateT8; g_CrcUpdate = CrcUpdateT4;
#ifdef MY_CPU_X86_OR_AMD64
if (!CPU_Is_InOrder())
g_CrcUpdate = CrcUpdateT8;
#endif
#endif
#else #if CRC_NUM_TABLES >= 8
g_CrcUpdateT8 = CrcUpdateT8;
#ifdef MY_CPU_X86_OR_AMD64
if (!CPU_Is_InOrder())
g_CrcUpdate = CrcUpdateT8;
#endif
#endif
#else
{ {
#ifndef MY_CPU_BE #ifndef MY_CPU_BE
UInt32 k = 0x01020304; UInt32 k = 0x01020304;
const Byte *p = (const Byte *)&k; const Byte *p = (const Byte *)&k;
if (p[0] == 4 && p[1] == 3) if (p[0] == 4 && p[1] == 3)
{ {
g_CrcUpdateT4 = CrcUpdateT4; g_CrcUpdateT4 = CrcUpdateT4;
g_CrcUpdate = CrcUpdateT4; g_CrcUpdate = CrcUpdateT4;
#if CRC_NUM_TABLES >= 8 #if CRC_NUM_TABLES >= 8
g_CrcUpdateT8 = CrcUpdateT8; g_CrcUpdateT8 = CrcUpdateT8;
// g_CrcUpdate = CrcUpdateT8; // g_CrcUpdate = CrcUpdateT8;
#endif #endif
} }
else if (p[0] != 1 || p[1] != 2) else if (p[0] != 1 || p[1] != 2)
g_CrcUpdate = CrcUpdateT1; g_CrcUpdate = CrcUpdateT1;
else else
#endif #endif
{ {
for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--) for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--)
{ {
@ -116,13 +116,13 @@ void MY_FAST_CALL CrcGenerateTable()
} }
g_CrcUpdateT4 = CrcUpdateT1_BeT4; g_CrcUpdateT4 = CrcUpdateT1_BeT4;
g_CrcUpdate = CrcUpdateT1_BeT4; g_CrcUpdate = CrcUpdateT1_BeT4;
#if CRC_NUM_TABLES >= 8 #if CRC_NUM_TABLES >= 8
g_CrcUpdateT8 = CrcUpdateT1_BeT8; g_CrcUpdateT8 = CrcUpdateT1_BeT8;
// g_CrcUpdate = CrcUpdateT1_BeT8; // g_CrcUpdate = CrcUpdateT1_BeT8;
#endif #endif
} }
} }
#endif #endif
#endif #endif
} }

View file

@ -17,8 +17,8 @@ void MY_FAST_CALL CrcGenerateTable(void);
#define CRC_GET_DIGEST(crc) ((crc) ^ CRC_INIT_VAL) #define CRC_GET_DIGEST(crc) ((crc) ^ CRC_INIT_VAL)
#define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) #define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size); UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void* data, size_t size);
UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size); UInt32 MY_FAST_CALL CrcCalc(const void* data, size_t size);
EXTERN_C_END EXTERN_C_END

View file

@ -9,49 +9,49 @@
#define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table) UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void* data, size_t size, const UInt32* table)
{ {
const Byte *p = (const Byte *)data; const Byte* p = (const Byte*)data;
for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++) for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
v = CRC_UPDATE_BYTE_2(v, *p); v = CRC_UPDATE_BYTE_2(v, *p);
for (; size >= 4; size -= 4, p += 4) for (; size >= 4; size -= 4, p += 4)
{ {
v ^= *(const UInt32 *)p; v ^= *(const UInt32*)p;
v = v =
table[0x300 + ((v ) & 0xFF)] table[0x300 + ((v) & 0xFF)]
^ table[0x200 + ((v >> 8) & 0xFF)] ^ table[0x200 + ((v >> 8) & 0xFF)]
^ table[0x100 + ((v >> 16) & 0xFF)] ^ table[0x100 + ((v >> 16) & 0xFF)]
^ table[0x000 + ((v >> 24))]; ^ table[0x000 + ((v >> 24))];
} }
for (; size > 0; size--, p++) for (; size > 0; size--, p++)
v = CRC_UPDATE_BYTE_2(v, *p); v = CRC_UPDATE_BYTE_2(v, *p);
return v; return v;
} }
UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table) UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void* data, size_t size, const UInt32* table)
{ {
const Byte *p = (const Byte *)data; const Byte* p = (const Byte*)data;
for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++) for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++)
v = CRC_UPDATE_BYTE_2(v, *p); v = CRC_UPDATE_BYTE_2(v, *p);
for (; size >= 8; size -= 8, p += 8) for (; size >= 8; size -= 8, p += 8)
{ {
UInt32 d; UInt32 d;
v ^= *(const UInt32 *)p; v ^= *(const UInt32*)p;
v = v =
table[0x700 + ((v ) & 0xFF)] table[0x700 + ((v) & 0xFF)]
^ table[0x600 + ((v >> 8) & 0xFF)] ^ table[0x600 + ((v >> 8) & 0xFF)]
^ table[0x500 + ((v >> 16) & 0xFF)] ^ table[0x500 + ((v >> 16) & 0xFF)]
^ table[0x400 + ((v >> 24))]; ^ table[0x400 + ((v >> 24))];
d = *((const UInt32 *)p + 1); d = *((const UInt32*)p + 1);
v ^= v ^=
table[0x300 + ((d ) & 0xFF)] table[0x300 + ((d) & 0xFF)]
^ table[0x200 + ((d >> 8) & 0xFF)] ^ table[0x200 + ((d >> 8) & 0xFF)]
^ table[0x100 + ((d >> 16) & 0xFF)] ^ table[0x100 + ((d >> 16) & 0xFF)]
^ table[0x000 + ((d >> 24))]; ^ table[0x000 + ((d >> 24))];
} }
for (; size > 0; size--, p++) for (; size > 0; size--, p++)
v = CRC_UPDATE_BYTE_2(v, *p); v = CRC_UPDATE_BYTE_2(v, *p);
return v; return v;
} }
#endif #endif

View file

@ -131,461 +131,467 @@ static SRes SzDecodePpmd(const Byte *props, unsigned propsSize, UInt64 inSize, I
#endif #endif
static SRes SzDecodeLzma(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream, static SRes SzDecodeLzma(const Byte* props, unsigned propsSize, UInt64 inSize, ILookInStream* inStream,
Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain) Byte* outBuffer, SizeT outSize, ISzAlloc* allocMain)
{ {
CLzmaDec state; CLzmaDec state;
SRes res = SZ_OK; SRes res = SZ_OK;
LzmaDec_Construct(&state); LzmaDec_Construct(&state);
RINOK(LzmaDec_AllocateProbs(&state, props, propsSize, allocMain)); RINOK(LzmaDec_AllocateProbs(&state, props, propsSize, allocMain));
state.dic = outBuffer; state.dic = outBuffer;
state.dicBufSize = outSize; state.dicBufSize = outSize;
LzmaDec_Init(&state); LzmaDec_Init(&state);
for (;;) for (;;)
{ {
const void *inBuf = NULL; const void* inBuf = NULL;
size_t lookahead = (1 << 18); size_t lookahead = (1 << 18);
if (lookahead > inSize) if (lookahead > inSize)
lookahead = (size_t)inSize; lookahead = (size_t)inSize;
res = inStream->Look(inStream, &inBuf, &lookahead); res = inStream->Look(inStream, &inBuf, &lookahead);
if (res != SZ_OK) if (res != SZ_OK)
break; break;
{ {
SizeT inProcessed = (SizeT)lookahead, dicPos = state.dicPos; SizeT inProcessed = (SizeT)lookahead, dicPos = state.dicPos;
ELzmaStatus status; ELzmaStatus status;
res = LzmaDec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status); res = LzmaDec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status);
lookahead -= inProcessed; lookahead -= inProcessed;
inSize -= inProcessed; inSize -= inProcessed;
if (res != SZ_OK) if (res != SZ_OK)
break; break;
if (status == LZMA_STATUS_FINISHED_WITH_MARK) if (status == LZMA_STATUS_FINISHED_WITH_MARK)
{ {
if (outSize != state.dicPos || inSize != 0) if (outSize != state.dicPos || inSize != 0)
res = SZ_ERROR_DATA; res = SZ_ERROR_DATA;
break; break;
} }
if (outSize == state.dicPos && inSize == 0 && status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK) if (outSize == state.dicPos && inSize == 0 && status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
break; break;
if (inProcessed == 0 && dicPos == state.dicPos) if (inProcessed == 0 && dicPos == state.dicPos)
{ {
res = SZ_ERROR_DATA; res = SZ_ERROR_DATA;
break; break;
} }
res = inStream->Skip((void *)inStream, inProcessed); res = inStream->Skip((void*)inStream, inProcessed);
if (res != SZ_OK) if (res != SZ_OK)
break; break;
} }
} }
LzmaDec_FreeProbs(&state, allocMain); LzmaDec_FreeProbs(&state, allocMain);
return res; return res;
} }
#ifndef _7Z_NO_METHOD_LZMA2 #ifndef _7Z_NO_METHOD_LZMA2
static SRes SzDecodeLzma2(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream, static SRes SzDecodeLzma2(const Byte* props, unsigned propsSize, UInt64 inSize, ILookInStream* inStream,
Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain) Byte* outBuffer, SizeT outSize, ISzAlloc* allocMain)
{ {
CLzma2Dec state; CLzma2Dec state;
SRes res = SZ_OK; SRes res = SZ_OK;
Lzma2Dec_Construct(&state); Lzma2Dec_Construct(&state);
if (propsSize != 1) if (propsSize != 1)
return SZ_ERROR_DATA; return SZ_ERROR_DATA;
RINOK(Lzma2Dec_AllocateProbs(&state, props[0], allocMain)); RINOK(Lzma2Dec_AllocateProbs(&state, props[0], allocMain));
state.decoder.dic = outBuffer; state.decoder.dic = outBuffer;
state.decoder.dicBufSize = outSize; state.decoder.dicBufSize = outSize;
Lzma2Dec_Init(&state); Lzma2Dec_Init(&state);
for (;;) for (;;)
{ {
const void *inBuf = NULL; const void* inBuf = NULL;
size_t lookahead = (1 << 18); size_t lookahead = (1 << 18);
if (lookahead > inSize) if (lookahead > inSize)
lookahead = (size_t)inSize; lookahead = (size_t)inSize;
res = inStream->Look(inStream, &inBuf, &lookahead); res = inStream->Look(inStream, &inBuf, &lookahead);
if (res != SZ_OK) if (res != SZ_OK)
break; break;
{ {
SizeT inProcessed = (SizeT)lookahead, dicPos = state.decoder.dicPos; SizeT inProcessed = (SizeT)lookahead, dicPos = state.decoder.dicPos;
ELzmaStatus status; ELzmaStatus status;
res = Lzma2Dec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status); res = Lzma2Dec_DecodeToDic(&state, outSize, inBuf, &inProcessed, LZMA_FINISH_END, &status);
lookahead -= inProcessed; lookahead -= inProcessed;
inSize -= inProcessed; inSize -= inProcessed;
if (res != SZ_OK) if (res != SZ_OK)
break; break;
if (status == LZMA_STATUS_FINISHED_WITH_MARK) if (status == LZMA_STATUS_FINISHED_WITH_MARK)
{ {
if (outSize != state.decoder.dicPos || inSize != 0) if (outSize != state.decoder.dicPos || inSize != 0)
res = SZ_ERROR_DATA; res = SZ_ERROR_DATA;
break; break;
} }
if (inProcessed == 0 && dicPos == state.decoder.dicPos) if (inProcessed == 0 && dicPos == state.decoder.dicPos)
{ {
res = SZ_ERROR_DATA; res = SZ_ERROR_DATA;
break; break;
} }
res = inStream->Skip((void *)inStream, inProcessed); res = inStream->Skip((void*)inStream, inProcessed);
if (res != SZ_OK) if (res != SZ_OK)
break; break;
} }
} }
Lzma2Dec_FreeProbs(&state, allocMain); Lzma2Dec_FreeProbs(&state, allocMain);
return res; return res;
} }
#endif #endif
static SRes SzDecodeCopy(UInt64 inSize, ILookInStream *inStream, Byte *outBuffer) static SRes SzDecodeCopy(UInt64 inSize, ILookInStream* inStream, Byte* outBuffer)
{ {
while (inSize > 0) while (inSize > 0)
{ {
const void *inBuf; const void* inBuf;
size_t curSize = (1 << 18); size_t curSize = (1 << 18);
if (curSize > inSize) if (curSize > inSize)
curSize = (size_t)inSize; curSize = (size_t)inSize;
RINOK(inStream->Look(inStream, &inBuf, &curSize)); RINOK(inStream->Look(inStream, &inBuf, &curSize));
if (curSize == 0) if (curSize == 0)
return SZ_ERROR_INPUT_EOF; return SZ_ERROR_INPUT_EOF;
memcpy(outBuffer, inBuf, curSize); memcpy(outBuffer, inBuf, curSize);
outBuffer += curSize; outBuffer += curSize;
inSize -= curSize; inSize -= curSize;
RINOK(inStream->Skip((void *)inStream, curSize)); RINOK(inStream->Skip((void *)inStream, curSize));
} }
return SZ_OK; return SZ_OK;
} }
static Bool IS_MAIN_METHOD(UInt32 m) static Bool IS_MAIN_METHOD(UInt32 m)
{ {
switch (m) switch (m)
{ {
case k_Copy: case k_Copy:
case k_LZMA: case k_LZMA:
#ifndef _7Z_NO_METHOD_LZMA2 #ifndef _7Z_NO_METHOD_LZMA2
case k_LZMA2: case k_LZMA2:
#endif #endif
#ifdef _7ZIP_PPMD_SUPPPORT #ifdef _7ZIP_PPMD_SUPPPORT
case k_PPMD: case k_PPMD:
#endif #endif
return True; return True;
} }
return False; return False;
} }
static Bool IS_SUPPORTED_CODER(const CSzCoderInfo *c) static Bool IS_SUPPORTED_CODER(const CSzCoderInfo* c)
{ {
return return
c->NumStreams == 1 c->NumStreams == 1
/* && c->MethodID <= (UInt32)0xFFFFFFFF */ /* && c->MethodID <= (UInt32)0xFFFFFFFF */
&& IS_MAIN_METHOD((UInt32)c->MethodID); && IS_MAIN_METHOD((UInt32)c->MethodID);
} }
#define IS_BCJ2(c) ((c)->MethodID == k_BCJ2 && (c)->NumStreams == 4) #define IS_BCJ2(c) ((c)->MethodID == k_BCJ2 && (c)->NumStreams == 4)
static SRes CheckSupportedFolder(const CSzFolder *f) static SRes CheckSupportedFolder(const CSzFolder* f)
{ {
if (f->NumCoders < 1 || f->NumCoders > 4) if (f->NumCoders < 1 || f->NumCoders > 4)
return SZ_ERROR_UNSUPPORTED; return SZ_ERROR_UNSUPPORTED;
if (!IS_SUPPORTED_CODER(&f->Coders[0])) if (!IS_SUPPORTED_CODER(&f->Coders[0]))
return SZ_ERROR_UNSUPPORTED; return SZ_ERROR_UNSUPPORTED;
if (f->NumCoders == 1) if (f->NumCoders == 1)
{ {
if (f->NumPackStreams != 1 || f->PackStreams[0] != 0 || f->NumBonds != 0) if (f->NumPackStreams != 1 || f->PackStreams[0] != 0 || f->NumBonds != 0)
return SZ_ERROR_UNSUPPORTED; return SZ_ERROR_UNSUPPORTED;
return SZ_OK; return SZ_OK;
} }
#ifndef _7Z_NO_METHODS_FILTERS
if (f->NumCoders == 2)
{
const CSzCoderInfo *c = &f->Coders[1];
if (
/* c->MethodID > (UInt32)0xFFFFFFFF || */
c->NumStreams != 1
|| f->NumPackStreams != 1
|| f->PackStreams[0] != 0
|| f->NumBonds != 1
|| f->Bonds[0].InIndex != 1
|| f->Bonds[0].OutIndex != 0)
return SZ_ERROR_UNSUPPORTED;
switch ((UInt32)c->MethodID)
{
case k_Delta:
case k_BCJ:
case k_PPC:
case k_IA64:
case k_SPARC:
case k_ARM:
case k_ARMT:
break;
default:
return SZ_ERROR_UNSUPPORTED;
}
return SZ_OK;
}
#endif #ifndef _7Z_NO_METHODS_FILTERS
if (f->NumCoders == 2)
if (f->NumCoders == 4) {
{ const CSzCoderInfo* c = &f->Coders[1];
if (!IS_SUPPORTED_CODER(&f->Coders[1]) if (
|| !IS_SUPPORTED_CODER(&f->Coders[2]) /* c->MethodID > (UInt32)0xFFFFFFFF || */
|| !IS_BCJ2(&f->Coders[3])) c->NumStreams != 1
return SZ_ERROR_UNSUPPORTED; || f->NumPackStreams != 1
if (f->NumPackStreams != 4 || f->PackStreams[0] != 0
|| f->PackStreams[0] != 2 || f->NumBonds != 1
|| f->PackStreams[1] != 6 || f->Bonds[0].InIndex != 1
|| f->PackStreams[2] != 1 || f->Bonds[0].OutIndex != 0)
|| f->PackStreams[3] != 0 return SZ_ERROR_UNSUPPORTED;
|| f->NumBonds != 3 switch ((UInt32)c->MethodID)
|| f->Bonds[0].InIndex != 5 || f->Bonds[0].OutIndex != 0 {
|| f->Bonds[1].InIndex != 4 || f->Bonds[1].OutIndex != 1 case k_Delta:
|| f->Bonds[2].InIndex != 3 || f->Bonds[2].OutIndex != 2) case k_BCJ:
return SZ_ERROR_UNSUPPORTED; case k_PPC:
return SZ_OK; case k_IA64:
} case k_SPARC:
case k_ARM:
return SZ_ERROR_UNSUPPORTED; case k_ARMT:
break;
default:
return SZ_ERROR_UNSUPPORTED;
}
return SZ_OK;
}
#endif
if (f->NumCoders == 4)
{
if (!IS_SUPPORTED_CODER(&f->Coders[1])
|| !IS_SUPPORTED_CODER(&f->Coders[2])
|| !IS_BCJ2(&f->Coders[3]))
return SZ_ERROR_UNSUPPORTED;
if (f->NumPackStreams != 4
|| f->PackStreams[0] != 2
|| f->PackStreams[1] != 6
|| f->PackStreams[2] != 1
|| f->PackStreams[3] != 0
|| f->NumBonds != 3
|| f->Bonds[0].InIndex != 5 || f->Bonds[0].OutIndex != 0
|| f->Bonds[1].InIndex != 4 || f->Bonds[1].OutIndex != 1
|| f->Bonds[2].InIndex != 3 || f->Bonds[2].OutIndex != 2)
return SZ_ERROR_UNSUPPORTED;
return SZ_OK;
}
return SZ_ERROR_UNSUPPORTED;
} }
#define CASE_BRA_CONV(isa) case k_ ## isa: isa ## _Convert(outBuffer, outSize, 0, 0); break; #define CASE_BRA_CONV(isa) case k_ ## isa: isa ## _Convert(outBuffer, outSize, 0, 0); break;
static SRes SzFolder_Decode2(const CSzFolder *folder, static SRes SzFolder_Decode2(const CSzFolder* folder,
const Byte *propsData, const Byte* propsData,
const UInt64 *unpackSizes, const UInt64* unpackSizes,
const UInt64 *packPositions, const UInt64* packPositions,
ILookInStream *inStream, UInt64 startPos, ILookInStream* inStream, UInt64 startPos,
Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain, Byte* outBuffer, SizeT outSize, ISzAlloc* allocMain,
Byte *tempBuf[]) Byte* tempBuf[])
{ {
UInt32 ci; UInt32 ci;
SizeT tempSizes[3] = { 0, 0, 0}; SizeT tempSizes[3] = {0, 0, 0};
SizeT tempSize3 = 0; SizeT tempSize3 = 0;
Byte *tempBuf3 = 0; Byte* tempBuf3 = 0;
RINOK(CheckSupportedFolder(folder)); RINOK(CheckSupportedFolder(folder));
for (ci = 0; ci < folder->NumCoders; ci++) for (ci = 0; ci < folder->NumCoders; ci++)
{ {
const CSzCoderInfo *coder = &folder->Coders[ci]; const CSzCoderInfo* coder = &folder->Coders[ci];
if (IS_MAIN_METHOD((UInt32)coder->MethodID)) if (IS_MAIN_METHOD((UInt32)coder->MethodID))
{ {
UInt32 si = 0; UInt32 si = 0;
UInt64 offset; UInt64 offset;
UInt64 inSize; UInt64 inSize;
Byte *outBufCur = outBuffer; Byte* outBufCur = outBuffer;
SizeT outSizeCur = outSize; SizeT outSizeCur = outSize;
if (folder->NumCoders == 4) if (folder->NumCoders == 4)
{ {
UInt32 indices[] = { 3, 2, 0 }; UInt32 indices[] = {3, 2, 0};
UInt64 unpackSize = unpackSizes[ci]; UInt64 unpackSize = unpackSizes[ci];
si = indices[ci]; si = indices[ci];
if (ci < 2) if (ci < 2)
{ {
Byte *temp; Byte* temp;
outSizeCur = (SizeT)unpackSize; outSizeCur = (SizeT)unpackSize;
if (outSizeCur != unpackSize) if (outSizeCur != unpackSize)
return SZ_ERROR_MEM; return SZ_ERROR_MEM;
temp = (Byte *)IAlloc_Alloc(allocMain, outSizeCur); temp = (Byte*)IAlloc_Alloc(allocMain, outSizeCur);
if (!temp && outSizeCur != 0) if (!temp && outSizeCur != 0)
return SZ_ERROR_MEM; return SZ_ERROR_MEM;
outBufCur = tempBuf[1 - ci] = temp; outBufCur = tempBuf[1 - ci] = temp;
tempSizes[1 - ci] = outSizeCur; tempSizes[1 - ci] = outSizeCur;
} }
else if (ci == 2) else if (ci == 2)
{ {
if (unpackSize > outSize) /* check it */ if (unpackSize > outSize) /* check it */
return SZ_ERROR_PARAM; return SZ_ERROR_PARAM;
tempBuf3 = outBufCur = outBuffer + (outSize - (size_t)unpackSize); tempBuf3 = outBufCur = outBuffer + (outSize - (size_t)unpackSize);
tempSize3 = outSizeCur = (SizeT)unpackSize; tempSize3 = outSizeCur = (SizeT)unpackSize;
} }
else else
return SZ_ERROR_UNSUPPORTED; return SZ_ERROR_UNSUPPORTED;
} }
offset = packPositions[si]; offset = packPositions[si];
inSize = packPositions[si + 1] - offset; inSize = packPositions[si + 1] - offset;
RINOK(LookInStream_SeekTo(inStream, startPos + offset)); RINOK(LookInStream_SeekTo(inStream, startPos + offset));
if (coder->MethodID == k_Copy) if (coder->MethodID == k_Copy)
{ {
if (inSize != outSizeCur) /* check it */ if (inSize != outSizeCur) /* check it */
return SZ_ERROR_DATA; return SZ_ERROR_DATA;
RINOK(SzDecodeCopy(inSize, inStream, outBufCur)); RINOK(SzDecodeCopy(inSize, inStream, outBufCur));
} }
else if (coder->MethodID == k_LZMA) else if (coder->MethodID == k_LZMA)
{ {
RINOK(SzDecodeLzma(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain)); RINOK(SzDecodeLzma(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur
} , allocMain));
#ifndef _7Z_NO_METHOD_LZMA2 }
else if (coder->MethodID == k_LZMA2) #ifndef _7Z_NO_METHOD_LZMA2
{ else if (coder->MethodID == k_LZMA2)
RINOK(SzDecodeLzma2(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain)); {
} RINOK(SzDecodeLzma2(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur,
#endif outSizeCur, allocMain));
#ifdef _7ZIP_PPMD_SUPPPORT }
#endif
#ifdef _7ZIP_PPMD_SUPPPORT
else if (coder->MethodID == k_PPMD) else if (coder->MethodID == k_PPMD)
{ {
RINOK(SzDecodePpmd(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain)); RINOK(SzDecodePpmd(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain));
} }
#endif #endif
else else
return SZ_ERROR_UNSUPPORTED; return SZ_ERROR_UNSUPPORTED;
} }
else if (coder->MethodID == k_BCJ2) else if (coder->MethodID == k_BCJ2)
{ {
UInt64 offset = packPositions[1]; UInt64 offset = packPositions[1];
UInt64 s3Size = packPositions[2] - offset; UInt64 s3Size = packPositions[2] - offset;
if (ci != 3)
return SZ_ERROR_UNSUPPORTED;
tempSizes[2] = (SizeT)s3Size;
if (tempSizes[2] != s3Size)
return SZ_ERROR_MEM;
tempBuf[2] = (Byte *)IAlloc_Alloc(allocMain, tempSizes[2]);
if (!tempBuf[2] && tempSizes[2] != 0)
return SZ_ERROR_MEM;
RINOK(LookInStream_SeekTo(inStream, startPos + offset));
RINOK(SzDecodeCopy(s3Size, inStream, tempBuf[2]));
if ((tempSizes[0] & 3) != 0 || if (ci != 3)
(tempSizes[1] & 3) != 0 || return SZ_ERROR_UNSUPPORTED;
tempSize3 + tempSizes[0] + tempSizes[1] != outSize)
return SZ_ERROR_DATA;
{ tempSizes[2] = (SizeT)s3Size;
CBcj2Dec p; if (tempSizes[2] != s3Size)
return SZ_ERROR_MEM;
p.bufs[0] = tempBuf3; p.lims[0] = tempBuf3 + tempSize3; tempBuf[2] = (Byte*)IAlloc_Alloc(allocMain, tempSizes[2]);
p.bufs[1] = tempBuf[0]; p.lims[1] = tempBuf[0] + tempSizes[0]; if (!tempBuf[2] && tempSizes[2] != 0)
p.bufs[2] = tempBuf[1]; p.lims[2] = tempBuf[1] + tempSizes[1]; return SZ_ERROR_MEM;
p.bufs[3] = tempBuf[2]; p.lims[3] = tempBuf[2] + tempSizes[2];
p.dest = outBuffer;
p.destLim = outBuffer + outSize;
Bcj2Dec_Init(&p);
RINOK(Bcj2Dec_Decode(&p));
{ RINOK(LookInStream_SeekTo(inStream, startPos + offset));
unsigned i; RINOK(SzDecodeCopy(s3Size, inStream, tempBuf[2]));
for (i = 0; i < 4; i++)
if (p.bufs[i] != p.lims[i])
return SZ_ERROR_DATA;
if (!Bcj2Dec_IsFinished(&p))
return SZ_ERROR_DATA;
if (p.dest != p.destLim if ((tempSizes[0] & 3) != 0 ||
|| p.state != BCJ2_STREAM_MAIN) (tempSizes[1] & 3) != 0 ||
return SZ_ERROR_DATA; tempSize3 + tempSizes[0] + tempSizes[1] != outSize)
} return SZ_ERROR_DATA;
}
}
#ifndef _7Z_NO_METHODS_FILTERS
else if (ci == 1)
{
if (coder->MethodID == k_Delta)
{
if (coder->PropsSize != 1)
return SZ_ERROR_UNSUPPORTED;
{
Byte state[DELTA_STATE_SIZE];
Delta_Init(state);
Delta_Decode(state, (unsigned)(propsData[coder->PropsOffset]) + 1, outBuffer, outSize);
}
}
else
{
if (coder->PropsSize != 0)
return SZ_ERROR_UNSUPPORTED;
switch (coder->MethodID)
{
case k_BCJ:
{
UInt32 state;
x86_Convert_Init(state);
x86_Convert(outBuffer, outSize, 0, &state, 0);
break;
}
CASE_BRA_CONV(PPC)
CASE_BRA_CONV(IA64)
CASE_BRA_CONV(SPARC)
CASE_BRA_CONV(ARM)
CASE_BRA_CONV(ARMT)
default:
return SZ_ERROR_UNSUPPORTED;
}
}
}
#endif
else
return SZ_ERROR_UNSUPPORTED;
}
return SZ_OK; {
CBcj2Dec p;
p.bufs[0] = tempBuf3;
p.lims[0] = tempBuf3 + tempSize3;
p.bufs[1] = tempBuf[0];
p.lims[1] = tempBuf[0] + tempSizes[0];
p.bufs[2] = tempBuf[1];
p.lims[2] = tempBuf[1] + tempSizes[1];
p.bufs[3] = tempBuf[2];
p.lims[3] = tempBuf[2] + tempSizes[2];
p.dest = outBuffer;
p.destLim = outBuffer + outSize;
Bcj2Dec_Init(&p);
RINOK(Bcj2Dec_Decode(&p));
{
unsigned i;
for (i = 0; i < 4; i++)
if (p.bufs[i] != p.lims[i])
return SZ_ERROR_DATA;
if (!Bcj2Dec_IsFinished(&p))
return SZ_ERROR_DATA;
if (p.dest != p.destLim
|| p.state != BCJ2_STREAM_MAIN)
return SZ_ERROR_DATA;
}
}
}
#ifndef _7Z_NO_METHODS_FILTERS
else if (ci == 1)
{
if (coder->MethodID == k_Delta)
{
if (coder->PropsSize != 1)
return SZ_ERROR_UNSUPPORTED;
{
Byte state[DELTA_STATE_SIZE];
Delta_Init(state);
Delta_Decode(state, (unsigned)(propsData[coder->PropsOffset]) + 1, outBuffer, outSize);
}
}
else
{
if (coder->PropsSize != 0)
return SZ_ERROR_UNSUPPORTED;
switch (coder->MethodID)
{
case k_BCJ:
{
UInt32 state;
x86_Convert_Init(state);
x86_Convert(outBuffer, outSize, 0, &state, 0);
break;
}
CASE_BRA_CONV(PPC)
CASE_BRA_CONV(IA64)
CASE_BRA_CONV(SPARC)
CASE_BRA_CONV(ARM)
CASE_BRA_CONV(ARMT)
default:
return SZ_ERROR_UNSUPPORTED;
}
}
}
#endif
else
return SZ_ERROR_UNSUPPORTED;
}
return SZ_OK;
} }
SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex, SRes SzAr_DecodeFolder(const CSzAr* p, UInt32 folderIndex,
ILookInStream *inStream, UInt64 startPos, ILookInStream* inStream, UInt64 startPos,
Byte *outBuffer, size_t outSize, Byte* outBuffer, size_t outSize,
ISzAlloc *allocMain) ISzAlloc* allocMain)
{ {
SRes res; SRes res;
CSzFolder folder; CSzFolder folder;
CSzData sd; CSzData sd;
const Byte *data = p->CodersData + p->FoCodersOffsets[folderIndex];
sd.Data = data;
sd.Size = p->FoCodersOffsets[folderIndex + 1] - p->FoCodersOffsets[folderIndex];
res = SzGetNextFolderItem(&folder, &sd);
if (res != SZ_OK)
return res;
if (sd.Size != 0 const Byte* data = p->CodersData + p->FoCodersOffsets[folderIndex];
|| folder.UnpackStream != p->FoToMainUnpackSizeIndex[folderIndex] sd.Data = data;
|| outSize != SzAr_GetFolderUnpackSize(p, folderIndex)) sd.Size = p->FoCodersOffsets[folderIndex + 1] - p->FoCodersOffsets[folderIndex];
return SZ_ERROR_FAIL;
{
unsigned i;
Byte *tempBuf[3] = { 0, 0, 0};
res = SzFolder_Decode2(&folder, data, res = SzGetNextFolderItem(&folder, &sd);
&p->CoderUnpackSizes[p->FoToCoderUnpackSizes[folderIndex]],
p->PackPositions + p->FoStartPackStreamIndex[folderIndex],
inStream, startPos,
outBuffer, (SizeT)outSize, allocMain, tempBuf);
for (i = 0; i < 3; i++)
IAlloc_Free(allocMain, tempBuf[i]);
if (res == SZ_OK) if (res != SZ_OK)
if (SzBitWithVals_Check(&p->FolderCRCs, folderIndex)) return res;
if (CrcCalc(outBuffer, outSize) != p->FolderCRCs.Vals[folderIndex])
res = SZ_ERROR_CRC;
return res; if (sd.Size != 0
} || folder.UnpackStream != p->FoToMainUnpackSizeIndex[folderIndex]
|| outSize != SzAr_GetFolderUnpackSize(p, folderIndex))
return SZ_ERROR_FAIL;
{
unsigned i;
Byte* tempBuf[3] = {0, 0, 0};
res = SzFolder_Decode2(&folder, data,
&p->CoderUnpackSizes[p->FoToCoderUnpackSizes[folderIndex]],
p->PackPositions + p->FoStartPackStreamIndex[folderIndex],
inStream, startPos,
outBuffer, (SizeT)outSize, allocMain, tempBuf);
for (i = 0; i < 3; i++)
IAlloc_Free(allocMain, tempBuf[i]);
if (res == SZ_OK)
if (SzBitWithVals_Check(&p->FolderCRCs, folderIndex))
if (CrcCalc(outBuffer, outSize) != p->FolderCRCs.Vals[folderIndex])
res = SZ_ERROR_CRC;
return res;
}
} }

View file

@ -27,64 +27,65 @@
#endif #endif
void File_Construct(CSzFile *p) void File_Construct(CSzFile* p)
{ {
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
p->handle = INVALID_HANDLE_VALUE; p->handle = INVALID_HANDLE_VALUE;
#else #else
p->file = NULL; p->file = NULL;
#endif #endif
} }
#if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE) #if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE)
static WRes File_Open(CSzFile *p, const char *name, int writeMode) static WRes File_Open(CSzFile* p, const char* name, int writeMode)
{ {
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
p->handle = CreateFileA(name, p->handle = CreateFileA(name,
writeMode ? GENERIC_WRITE : GENERIC_READ, writeMode ? GENERIC_WRITE : GENERIC_READ,
FILE_SHARE_READ, NULL, FILE_SHARE_READ, NULL,
writeMode ? CREATE_ALWAYS : OPEN_EXISTING, writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL); FILE_ATTRIBUTE_NORMAL, NULL);
return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError(); return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
#else #else
p->file = fopen(name, writeMode ? "wb+" : "rb"); p->file = fopen(name, writeMode ? "wb+" : "rb");
return (p->file != 0) ? 0 : return (p->file != 0) ? 0 :
#ifdef UNDER_CE #ifdef UNDER_CE
2; /* ENOENT */ 2; /* ENOENT */
#else #else
errno; errno;
#endif #endif
#endif #endif
} }
WRes InFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 0); } WRes InFile_Open(CSzFile* p, const char* name) { return File_Open(p, name, 0); }
WRes OutFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 1); } WRes OutFile_Open(CSzFile* p, const char* name) { return File_Open(p, name, 1); }
#endif #endif
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
static WRes File_OpenW(CSzFile *p, const WCHAR *name, int writeMode) static WRes File_OpenW(CSzFile* p, const WCHAR* name, int writeMode)
{ {
p->handle = CreateFileW(name, p->handle = CreateFileW(name,
writeMode ? GENERIC_WRITE : GENERIC_READ, writeMode ? GENERIC_WRITE : GENERIC_READ,
FILE_SHARE_READ, NULL, FILE_SHARE_READ, NULL,
writeMode ? CREATE_ALWAYS : OPEN_EXISTING, writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL); FILE_ATTRIBUTE_NORMAL, NULL);
return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError(); return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
} }
WRes InFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 0); }
WRes OutFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 1); } WRes InFile_OpenW(CSzFile* p, const WCHAR* name) { return File_OpenW(p, name, 0); }
WRes OutFile_OpenW(CSzFile* p, const WCHAR* name) { return File_OpenW(p, name, 1); }
#endif #endif
WRes File_Close(CSzFile *p) WRes File_Close(CSzFile* p)
{ {
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
if (p->handle != INVALID_HANDLE_VALUE) if (p->handle != INVALID_HANDLE_VALUE)
{ {
if (!CloseHandle(p->handle)) if (!CloseHandle(p->handle))
return GetLastError(); return GetLastError();
p->handle = INVALID_HANDLE_VALUE; p->handle = INVALID_HANDLE_VALUE;
} }
#else #else
if (p->file != NULL) if (p->file != NULL)
{ {
int res = fclose(p->file); int res = fclose(p->file);
@ -92,106 +93,109 @@ WRes File_Close(CSzFile *p)
return res; return res;
p->file = NULL; p->file = NULL;
} }
#endif #endif
return 0; return 0;
} }
WRes File_Read(CSzFile *p, void *data, size_t *size) WRes File_Read(CSzFile* p, void* data, size_t* size)
{ {
size_t originalSize = *size; size_t originalSize = *size;
if (originalSize == 0) if (originalSize == 0)
return 0; return 0;
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
*size = 0; *size = 0;
do do
{ {
DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize; DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
DWORD processed = 0; DWORD processed = 0;
BOOL res = ReadFile(p->handle, data, curSize, &processed, NULL); BOOL res = ReadFile(p->handle, data, curSize, &processed, NULL);
data = (void *)((Byte *)data + processed); data = (void*)((Byte*)data + processed);
originalSize -= processed; originalSize -= processed;
*size += processed; *size += processed;
if (!res) if (!res)
return GetLastError(); return GetLastError();
if (processed == 0) if (processed == 0)
break; break;
} }
while (originalSize > 0); while (originalSize > 0);
return 0; return 0;
#else #else
*size = fread(data, 1, originalSize, p->file); *size = fread(data, 1, originalSize, p->file);
if (*size == originalSize) if (*size == originalSize)
return 0; return 0;
return ferror(p->file); return ferror(p->file);
#endif #endif
} }
WRes File_Write(CSzFile *p, const void *data, size_t *size) WRes File_Write(CSzFile* p, const void* data, size_t* size)
{ {
size_t originalSize = *size; size_t originalSize = *size;
if (originalSize == 0) if (originalSize == 0)
return 0; return 0;
#ifdef USE_WINDOWS_FILE
*size = 0; #ifdef USE_WINDOWS_FILE
do
{
DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
DWORD processed = 0;
BOOL res = WriteFile(p->handle, data, curSize, &processed, NULL);
data = (void *)((Byte *)data + processed);
originalSize -= processed;
*size += processed;
if (!res)
return GetLastError();
if (processed == 0)
break;
}
while (originalSize > 0);
return 0;
#else *size = 0;
do
{
DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
DWORD processed = 0;
BOOL res = WriteFile(p->handle, data, curSize, &processed, NULL);
data = (void*)((Byte*)data + processed);
originalSize -= processed;
*size += processed;
if (!res)
return GetLastError();
if (processed == 0)
break;
}
while (originalSize > 0);
return 0;
#else
*size = fwrite(data, 1, originalSize, p->file); *size = fwrite(data, 1, originalSize, p->file);
if (*size == originalSize) if (*size == originalSize)
return 0; return 0;
return ferror(p->file); return ferror(p->file);
#endif #endif
} }
WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin) WRes File_Seek(CSzFile* p, Int64* pos, ESzSeek origin)
{ {
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
LARGE_INTEGER value; LARGE_INTEGER value;
DWORD moveMethod; DWORD moveMethod;
value.LowPart = (DWORD)*pos; value.LowPart = (DWORD)*pos;
value.HighPart = (LONG)((UInt64)*pos >> 16 >> 16); /* for case when UInt64 is 32-bit only */ value.HighPart = (LONG)((UInt64)*pos >> 16 >> 16); /* for case when UInt64 is 32-bit only */
switch (origin) switch (origin)
{ {
case SZ_SEEK_SET: moveMethod = FILE_BEGIN; break; case SZ_SEEK_SET: moveMethod = FILE_BEGIN;
case SZ_SEEK_CUR: moveMethod = FILE_CURRENT; break; break;
case SZ_SEEK_END: moveMethod = FILE_END; break; case SZ_SEEK_CUR: moveMethod = FILE_CURRENT;
default: return ERROR_INVALID_PARAMETER; break;
} case SZ_SEEK_END: moveMethod = FILE_END;
value.LowPart = SetFilePointer(p->handle, value.LowPart, &value.HighPart, moveMethod); break;
if (value.LowPart == 0xFFFFFFFF) default: return ERROR_INVALID_PARAMETER;
{ }
WRes res = GetLastError(); value.LowPart = SetFilePointer(p->handle, value.LowPart, &value.HighPart, moveMethod);
if (res != NO_ERROR) if (value.LowPart == 0xFFFFFFFF)
return res; {
} WRes res = GetLastError();
*pos = ((Int64)value.HighPart << 32) | value.LowPart; if (res != NO_ERROR)
return 0; return res;
}
*pos = ((Int64)value.HighPart << 32) | value.LowPart;
return 0;
#else #else
int moveMethod; int moveMethod;
int res; int res;
@ -206,25 +210,25 @@ WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin)
*pos = ftell(p->file); *pos = ftell(p->file);
return res; return res;
#endif #endif
} }
WRes File_GetLength(CSzFile *p, UInt64 *length) WRes File_GetLength(CSzFile* p, UInt64* length)
{ {
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
DWORD sizeHigh; DWORD sizeHigh;
DWORD sizeLow = GetFileSize(p->handle, &sizeHigh); DWORD sizeLow = GetFileSize(p->handle, &sizeHigh);
if (sizeLow == 0xFFFFFFFF) if (sizeLow == 0xFFFFFFFF)
{ {
DWORD res = GetLastError(); DWORD res = GetLastError();
if (res != NO_ERROR) if (res != NO_ERROR)
return res; return res;
} }
*length = (((UInt64)sizeHigh) << 32) + sizeLow; *length = (((UInt64)sizeHigh) << 32) + sizeLow;
return 0; return 0;
#else #else
long pos = ftell(p->file); long pos = ftell(p->file);
int res = fseek(p->file, 0, SEEK_END); int res = fseek(p->file, 0, SEEK_END);
@ -232,55 +236,55 @@ WRes File_GetLength(CSzFile *p, UInt64 *length)
fseek(p->file, pos, SEEK_SET); fseek(p->file, pos, SEEK_SET);
return res; return res;
#endif #endif
} }
/* ---------- FileSeqInStream ---------- */ /* ---------- FileSeqInStream ---------- */
static SRes FileSeqInStream_Read(void *pp, void *buf, size_t *size) static SRes FileSeqInStream_Read(void* pp, void* buf, size_t* size)
{ {
CFileSeqInStream *p = (CFileSeqInStream *)pp; CFileSeqInStream* p = (CFileSeqInStream*)pp;
return File_Read(&p->file, buf, size) == 0 ? SZ_OK : SZ_ERROR_READ; return File_Read(&p->file, buf, size) == 0 ? SZ_OK : SZ_ERROR_READ;
} }
void FileSeqInStream_CreateVTable(CFileSeqInStream *p) void FileSeqInStream_CreateVTable(CFileSeqInStream* p)
{ {
p->s.Read = FileSeqInStream_Read; p->s.Read = FileSeqInStream_Read;
} }
/* ---------- FileInStream ---------- */ /* ---------- FileInStream ---------- */
static SRes FileInStream_Read(void *pp, void *buf, size_t *size) static SRes FileInStream_Read(void* pp, void* buf, size_t* size)
{ {
CFileInStream *p = (CFileInStream *)pp; CFileInStream* p = (CFileInStream*)pp;
return (File_Read(&p->file, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ; return (File_Read(&p->file, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ;
} }
static SRes FileInStream_Seek(void *pp, Int64 *pos, ESzSeek origin) static SRes FileInStream_Seek(void* pp, Int64* pos, ESzSeek origin)
{ {
CFileInStream *p = (CFileInStream *)pp; CFileInStream* p = (CFileInStream*)pp;
return File_Seek(&p->file, pos, origin); return File_Seek(&p->file, pos, origin);
} }
void FileInStream_CreateVTable(CFileInStream *p) void FileInStream_CreateVTable(CFileInStream* p)
{ {
p->s.Read = FileInStream_Read; p->s.Read = FileInStream_Read;
p->s.Seek = FileInStream_Seek; p->s.Seek = FileInStream_Seek;
} }
/* ---------- FileOutStream ---------- */ /* ---------- FileOutStream ---------- */
static size_t FileOutStream_Write(void *pp, const void *data, size_t size) static size_t FileOutStream_Write(void* pp, const void* data, size_t size)
{ {
CFileOutStream *p = (CFileOutStream *)pp; CFileOutStream* p = (CFileOutStream*)pp;
File_Write(&p->file, data, &size); File_Write(&p->file, data, &size);
return size; return size;
} }
void FileOutStream_CreateVTable(CFileOutStream *p) void FileOutStream_CreateVTable(CFileOutStream* p)
{ {
p->s.Write = FileOutStream_Write; p->s.Write = FileOutStream_Write;
} }

View file

@ -22,61 +22,61 @@ EXTERN_C_BEGIN
typedef struct typedef struct
{ {
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
HANDLE handle; HANDLE handle;
#else #else
FILE *file; FILE *file;
#endif #endif
} CSzFile; } CSzFile;
void File_Construct(CSzFile *p); void File_Construct(CSzFile* p);
#if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE) #if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE)
WRes InFile_Open(CSzFile *p, const char *name); WRes InFile_Open(CSzFile* p, const char* name);
WRes OutFile_Open(CSzFile *p, const char *name); WRes OutFile_Open(CSzFile* p, const char* name);
#endif #endif
#ifdef USE_WINDOWS_FILE #ifdef USE_WINDOWS_FILE
WRes InFile_OpenW(CSzFile *p, const WCHAR *name); WRes InFile_OpenW(CSzFile* p, const WCHAR* name);
WRes OutFile_OpenW(CSzFile *p, const WCHAR *name); WRes OutFile_OpenW(CSzFile* p, const WCHAR* name);
#endif #endif
WRes File_Close(CSzFile *p); WRes File_Close(CSzFile* p);
/* reads max(*size, remain file's size) bytes */ /* reads max(*size, remain file's size) bytes */
WRes File_Read(CSzFile *p, void *data, size_t *size); WRes File_Read(CSzFile* p, void* data, size_t* size);
/* writes *size bytes */ /* writes *size bytes */
WRes File_Write(CSzFile *p, const void *data, size_t *size); WRes File_Write(CSzFile* p, const void* data, size_t* size);
WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin); WRes File_Seek(CSzFile* p, Int64* pos, ESzSeek origin);
WRes File_GetLength(CSzFile *p, UInt64 *length); WRes File_GetLength(CSzFile* p, UInt64* length);
/* ---------- FileInStream ---------- */ /* ---------- FileInStream ---------- */
typedef struct typedef struct
{ {
ISeqInStream s; ISeqInStream s;
CSzFile file; CSzFile file;
} CFileSeqInStream; } CFileSeqInStream;
void FileSeqInStream_CreateVTable(CFileSeqInStream *p); void FileSeqInStream_CreateVTable(CFileSeqInStream* p);
typedef struct typedef struct
{ {
ISeekInStream s; ISeekInStream s;
CSzFile file; CSzFile file;
} CFileInStream; } CFileInStream;
void FileInStream_CreateVTable(CFileInStream *p); void FileInStream_CreateVTable(CFileInStream* p);
typedef struct typedef struct
{ {
ISeqOutStream s; ISeqOutStream s;
CSzFile file; CSzFile file;
} CFileOutStream; } CFileOutStream;
void FileOutStream_CreateVTable(CFileOutStream *p); void FileOutStream_CreateVTable(CFileOutStream* p);
EXTERN_C_END EXTERN_C_END

View file

@ -2,10 +2,10 @@
#include "7zMemBuffer.h" #include "7zMemBuffer.h"
#include <memory.h> #include <memory.h>
WRes MemBuffer_Read(CSzMemBuffer *p, void *data, size_t *size) WRes MemBuffer_Read(CSzMemBuffer* p, void* data, size_t* size)
{ {
size_t originalSize = *size; size_t originalSize = *size;
if(originalSize == 0) if (originalSize == 0)
return 0; return 0;
size_t length = (size_t)(p->pos + (Int64)(*size) > p->size ? p->size - p->pos - 1 : *size); size_t length = (size_t)(p->pos + (Int64)(*size) > p->size ? p->size - p->pos - 1 : *size);
@ -14,31 +14,35 @@ WRes MemBuffer_Read(CSzMemBuffer *p, void *data, size_t *size)
return 0; return 0;
} }
WRes MemBuffer_Seek(CSzMemBuffer *p, Int64 *pos, ESzSeek origin) WRes MemBuffer_Seek(CSzMemBuffer* p, Int64* pos, ESzSeek origin)
{ {
switch(origin) { switch (origin)
case SZ_SEEK_SET: p->pos = 0 + *pos; break; {
case SZ_SEEK_CUR: p->pos += *pos; break; case SZ_SEEK_SET: p->pos = 0 + *pos;
case SZ_SEEK_END: p->pos = p->size - *pos; break; break;
default: return 1; case SZ_SEEK_CUR: p->pos += *pos;
break;
case SZ_SEEK_END: p->pos = p->size - *pos;
break;
default: return 1;
} }
*pos = p->pos; *pos = p->pos;
return 0; return 0;
} }
static SRes MemBufferInStream_Read(void *pp, void *buf, size_t *size) static SRes MemBufferInStream_Read(void* pp, void* buf, size_t* size)
{ {
CMemBufferInStream *p = (CMemBufferInStream *)pp; CMemBufferInStream* p = (CMemBufferInStream*)pp;
return (MemBuffer_Read(&p->buffer, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ; return (MemBuffer_Read(&p->buffer, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ;
} }
static SRes MemBufferInStream_Seek(void *pp, Int64 *pos, ESzSeek origin) static SRes MemBufferInStream_Seek(void* pp, Int64* pos, ESzSeek origin)
{ {
CMemBufferInStream *p = (CMemBufferInStream *)pp; CMemBufferInStream* p = (CMemBufferInStream*)pp;
return MemBuffer_Seek(&p->buffer, pos, origin); return MemBuffer_Seek(&p->buffer, pos, origin);
} }
void MemBufferInit(CMemBufferInStream *memBuferStream, CLookToRead *lookStream, void* buffer, size_t size) void MemBufferInit(CMemBufferInStream* memBuferStream, CLookToRead* lookStream, void* buffer, size_t size)
{ {
memBuferStream->buffer.buffer = buffer; memBuferStream->buffer.buffer = buffer;
memBuferStream->buffer.pos = 0; memBuferStream->buffer.pos = 0;
@ -50,4 +54,4 @@ void MemBufferInit(CMemBufferInStream *memBuferStream, CLookToRead *lookStream,
LookToRead_CreateVTable(lookStream, False); LookToRead_CreateVTable(lookStream, False);
lookStream->realStream = &memBuferStream->s; lookStream->realStream = &memBuferStream->s;
LookToRead_Init(lookStream); LookToRead_Init(lookStream);
} }

View file

@ -14,16 +14,16 @@ typedef struct
} CSzMemBuffer; } CSzMemBuffer;
/* reads max(*size, remain file's size) bytes */ /* reads max(*size, remain file's size) bytes */
WRes MemBuffer_Read(CSzMemBuffer *p, void *data, size_t *size); WRes MemBuffer_Read(CSzMemBuffer* p, void* data, size_t* size);
WRes MemBuffer_Seek(CSzMemBuffer *p, Int64 *pos, ESzSeek origin); WRes MemBuffer_Seek(CSzMemBuffer* p, Int64* pos, ESzSeek origin);
/* ---------- FileInStream ---------- */ /* ---------- FileInStream ---------- */
typedef struct typedef struct
{ {
ISeekInStream s; ISeekInStream s;
CSzMemBuffer buffer; CSzMemBuffer buffer;
} CMemBufferInStream; } CMemBufferInStream;
void MemBufferInit(CMemBufferInStream *memBuferStream, CLookToRead *lookStream, void* buffer, size_t size); void MemBufferInit(CMemBufferInStream* memBuferStream, CLookToRead* lookStream, void* buffer, size_t size);
EXTERN_C_END EXTERN_C_END

View file

@ -7,165 +7,163 @@
#include "7zTypes.h" #include "7zTypes.h"
SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType) SRes SeqInStream_Read2(ISeqInStream* stream, void* buf, size_t size, SRes errorType)
{ {
while (size != 0) while (size != 0)
{ {
size_t processed = size; size_t processed = size;
RINOK(stream->Read(stream, buf, &processed)); RINOK(stream->Read(stream, buf, &processed));
if (processed == 0) if (processed == 0)
return errorType; return errorType;
buf = (void *)((Byte *)buf + processed); buf = (void*)((Byte*)buf + processed);
size -= processed; size -= processed;
} }
return SZ_OK; return SZ_OK;
} }
SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size) SRes SeqInStream_Read(ISeqInStream* stream, void* buf, size_t size)
{ {
return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
} }
SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf) SRes SeqInStream_ReadByte(ISeqInStream* stream, Byte* buf)
{ {
size_t processed = 1; size_t processed = 1;
RINOK(stream->Read(stream, buf, &processed)); RINOK(stream->Read(stream, buf, &processed));
return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF; return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
} }
SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset) SRes LookInStream_SeekTo(ILookInStream* stream, UInt64 offset)
{ {
Int64 t = offset; Int64 t = offset;
return stream->Seek(stream, &t, SZ_SEEK_SET); return stream->Seek(stream, &t, SZ_SEEK_SET);
} }
SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size) SRes LookInStream_LookRead(ILookInStream* stream, void* buf, size_t* size)
{ {
const void *lookBuf; const void* lookBuf;
if (*size == 0) if (*size == 0)
return SZ_OK; return SZ_OK;
RINOK(stream->Look(stream, &lookBuf, size)); RINOK(stream->Look(stream, &lookBuf, size));
memcpy(buf, lookBuf, *size); memcpy(buf, lookBuf, *size);
return stream->Skip(stream, *size); return stream->Skip(stream, *size);
} }
SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType) SRes LookInStream_Read2(ILookInStream* stream, void* buf, size_t size, SRes errorType)
{ {
while (size != 0) while (size != 0)
{ {
size_t processed = size; size_t processed = size;
RINOK(stream->Read(stream, buf, &processed)); RINOK(stream->Read(stream, buf, &processed));
if (processed == 0) if (processed == 0)
return errorType; return errorType;
buf = (void *)((Byte *)buf + processed); buf = (void*)((Byte*)buf + processed);
size -= processed; size -= processed;
} }
return SZ_OK; return SZ_OK;
} }
SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size) SRes LookInStream_Read(ILookInStream* stream, void* buf, size_t size)
{ {
return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
} }
static SRes LookToRead_Look_Lookahead(void *pp, const void **buf, size_t *size) static SRes LookToRead_Look_Lookahead(void* pp, const void** buf, size_t* size)
{ {
SRes res = SZ_OK; SRes res = SZ_OK;
CLookToRead *p = (CLookToRead *)pp; CLookToRead* p = (CLookToRead*)pp;
size_t size2 = p->size - p->pos; size_t size2 = p->size - p->pos;
if (size2 == 0 && *size > 0) if (size2 == 0 && *size > 0)
{ {
p->pos = 0; p->pos = 0;
size2 = LookToRead_BUF_SIZE; size2 = LookToRead_BUF_SIZE;
res = p->realStream->Read(p->realStream, p->buf, &size2); res = p->realStream->Read(p->realStream, p->buf, &size2);
p->size = size2; p->size = size2;
} }
if (size2 < *size) if (size2 < *size)
*size = size2; *size = size2;
*buf = p->buf + p->pos; *buf = p->buf + p->pos;
return res; return res;
} }
static SRes LookToRead_Look_Exact(void *pp, const void **buf, size_t *size) static SRes LookToRead_Look_Exact(void* pp, const void** buf, size_t* size)
{ {
SRes res = SZ_OK; SRes res = SZ_OK;
CLookToRead *p = (CLookToRead *)pp; CLookToRead* p = (CLookToRead*)pp;
size_t size2 = p->size - p->pos; size_t size2 = p->size - p->pos;
if (size2 == 0 && *size > 0) if (size2 == 0 && *size > 0)
{ {
p->pos = 0; p->pos = 0;
if (*size > LookToRead_BUF_SIZE) if (*size > LookToRead_BUF_SIZE)
*size = LookToRead_BUF_SIZE; *size = LookToRead_BUF_SIZE;
res = p->realStream->Read(p->realStream, p->buf, size); res = p->realStream->Read(p->realStream, p->buf, size);
size2 = p->size = *size; size2 = p->size = *size;
} }
if (size2 < *size) if (size2 < *size)
*size = size2; *size = size2;
*buf = p->buf + p->pos; *buf = p->buf + p->pos;
return res; return res;
} }
static SRes LookToRead_Skip(void *pp, size_t offset) static SRes LookToRead_Skip(void* pp, size_t offset)
{ {
CLookToRead *p = (CLookToRead *)pp; CLookToRead* p = (CLookToRead*)pp;
p->pos += offset; p->pos += offset;
return SZ_OK; return SZ_OK;
} }
static SRes LookToRead_Read(void *pp, void *buf, size_t *size) static SRes LookToRead_Read(void* pp, void* buf, size_t* size)
{ {
CLookToRead *p = (CLookToRead *)pp; CLookToRead* p = (CLookToRead*)pp;
size_t rem = p->size - p->pos; size_t rem = p->size - p->pos;
if (rem == 0) if (rem == 0)
return p->realStream->Read(p->realStream, buf, size); return p->realStream->Read(p->realStream, buf, size);
if (rem > *size) if (rem > *size)
rem = *size; rem = *size;
memcpy(buf, p->buf + p->pos, rem); memcpy(buf, p->buf + p->pos, rem);
p->pos += rem; p->pos += rem;
*size = rem; *size = rem;
return SZ_OK; return SZ_OK;
} }
static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin) static SRes LookToRead_Seek(void* pp, Int64* pos, ESzSeek origin)
{ {
CLookToRead *p = (CLookToRead *)pp; CLookToRead* p = (CLookToRead*)pp;
p->pos = p->size = 0; p->pos = p->size = 0;
return p->realStream->Seek(p->realStream, pos, origin); return p->realStream->Seek(p->realStream, pos, origin);
} }
void LookToRead_CreateVTable(CLookToRead *p, int lookahead) void LookToRead_CreateVTable(CLookToRead* p, int lookahead)
{ {
p->s.Look = lookahead ? p->s.Look = lookahead ? LookToRead_Look_Lookahead : LookToRead_Look_Exact;
LookToRead_Look_Lookahead : p->s.Skip = LookToRead_Skip;
LookToRead_Look_Exact; p->s.Read = LookToRead_Read;
p->s.Skip = LookToRead_Skip; p->s.Seek = LookToRead_Seek;
p->s.Read = LookToRead_Read;
p->s.Seek = LookToRead_Seek;
} }
void LookToRead_Init(CLookToRead *p) void LookToRead_Init(CLookToRead* p)
{ {
p->pos = p->size = 0; p->pos = p->size = 0;
} }
static SRes SecToLook_Read(void *pp, void *buf, size_t *size) static SRes SecToLook_Read(void* pp, void* buf, size_t* size)
{ {
CSecToLook *p = (CSecToLook *)pp; CSecToLook* p = (CSecToLook*)pp;
return LookInStream_LookRead(p->realStream, buf, size); return LookInStream_LookRead(p->realStream, buf, size);
} }
void SecToLook_CreateVTable(CSecToLook *p) void SecToLook_CreateVTable(CSecToLook* p)
{ {
p->s.Read = SecToLook_Read; p->s.Read = SecToLook_Read;
} }
static SRes SecToRead_Read(void *pp, void *buf, size_t *size) static SRes SecToRead_Read(void* pp, void* buf, size_t* size)
{ {
CSecToRead *p = (CSecToRead *)pp; CSecToRead* p = (CSecToRead*)pp;
return p->realStream->Read(p->realStream, buf, size); return p->realStream->Read(p->realStream, buf, size);
} }
void SecToRead_CreateVTable(CSecToRead *p) void SecToRead_CreateVTable(CSecToRead* p)
{ {
p->s.Read = SecToRead_Read; p->s.Read = SecToRead_Read;
} }

View file

@ -128,108 +128,108 @@ typedef int Bool;
typedef struct typedef struct
{ {
Byte (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */ Byte (*Read)(void* p); /* reads one byte, returns 0 in case of EOF or error */
} IByteIn; } IByteIn;
typedef struct typedef struct
{ {
void (*Write)(void *p, Byte b); void (*Write)(void* p, Byte b);
} IByteOut; } IByteOut;
typedef struct typedef struct
{ {
SRes (*Read)(void *p, void *buf, size_t *size); SRes (*Read)(void* p, void* buf, size_t* size);
/* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
(output(*size) < input(*size)) is allowed */ (output(*size) < input(*size)) is allowed */
} ISeqInStream; } ISeqInStream;
/* it can return SZ_ERROR_INPUT_EOF */ /* it can return SZ_ERROR_INPUT_EOF */
SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size); SRes SeqInStream_Read(ISeqInStream* stream, void* buf, size_t size);
SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType); SRes SeqInStream_Read2(ISeqInStream* stream, void* buf, size_t size, SRes errorType);
SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf); SRes SeqInStream_ReadByte(ISeqInStream* stream, Byte* buf);
typedef struct typedef struct
{ {
size_t (*Write)(void *p, const void *buf, size_t size); size_t (*Write)(void* p, const void* buf, size_t size);
/* Returns: result - the number of actually written bytes. /* Returns: result - the number of actually written bytes.
(result < size) means error */ (result < size) means error */
} ISeqOutStream; } ISeqOutStream;
typedef enum typedef enum
{ {
SZ_SEEK_SET = 0, SZ_SEEK_SET = 0,
SZ_SEEK_CUR = 1, SZ_SEEK_CUR = 1,
SZ_SEEK_END = 2 SZ_SEEK_END = 2
} ESzSeek; } ESzSeek;
typedef struct typedef struct
{ {
SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ SRes (*Read)(void* p, void* buf, size_t* size); /* same as ISeqInStream::Read */
SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin); SRes (*Seek)(void* p, Int64* pos, ESzSeek origin);
} ISeekInStream; } ISeekInStream;
typedef struct typedef struct
{ {
SRes (*Look)(void *p, const void **buf, size_t *size); SRes (*Look)(void* p, const void** buf, size_t* size);
/* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
(output(*size) > input(*size)) is not allowed (output(*size) > input(*size)) is not allowed
(output(*size) < input(*size)) is allowed */ (output(*size) < input(*size)) is allowed */
SRes (*Skip)(void *p, size_t offset); SRes (*Skip)(void* p, size_t offset);
/* offset must be <= output(*size) of Look */ /* offset must be <= output(*size) of Look */
SRes (*Read)(void *p, void *buf, size_t *size); SRes (*Read)(void* p, void* buf, size_t* size);
/* reads directly (without buffer). It's same as ISeqInStream::Read */ /* reads directly (without buffer). It's same as ISeqInStream::Read */
SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin); SRes (*Seek)(void* p, Int64* pos, ESzSeek origin);
} ILookInStream; } ILookInStream;
SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size); SRes LookInStream_LookRead(ILookInStream* stream, void* buf, size_t* size);
SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset); SRes LookInStream_SeekTo(ILookInStream* stream, UInt64 offset);
/* reads via ILookInStream::Read */ /* reads via ILookInStream::Read */
SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType); SRes LookInStream_Read2(ILookInStream* stream, void* buf, size_t size, SRes errorType);
SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size); SRes LookInStream_Read(ILookInStream* stream, void* buf, size_t size);
#define LookToRead_BUF_SIZE (1 << 14) #define LookToRead_BUF_SIZE (1 << 14)
typedef struct typedef struct
{ {
ILookInStream s; ILookInStream s;
ISeekInStream *realStream; ISeekInStream* realStream;
size_t pos; size_t pos;
size_t size; size_t size;
Byte buf[LookToRead_BUF_SIZE]; Byte buf[LookToRead_BUF_SIZE];
} CLookToRead; } CLookToRead;
void LookToRead_CreateVTable(CLookToRead *p, int lookahead); void LookToRead_CreateVTable(CLookToRead* p, int lookahead);
void LookToRead_Init(CLookToRead *p); void LookToRead_Init(CLookToRead* p);
typedef struct typedef struct
{ {
ISeqInStream s; ISeqInStream s;
ILookInStream *realStream; ILookInStream* realStream;
} CSecToLook; } CSecToLook;
void SecToLook_CreateVTable(CSecToLook *p); void SecToLook_CreateVTable(CSecToLook* p);
typedef struct typedef struct
{ {
ISeqInStream s; ISeqInStream s;
ILookInStream *realStream; ILookInStream* realStream;
} CSecToRead; } CSecToRead;
void SecToRead_CreateVTable(CSecToRead *p); void SecToRead_CreateVTable(CSecToRead* p);
typedef struct typedef struct
{ {
SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize); SRes (*Progress)(void* p, UInt64 inSize, UInt64 outSize);
/* Returns: result. (result != SZ_OK) means break. /* Returns: result. (result != SZ_OK) means break.
Value (UInt64)(Int64)-1 for size means unknown value. */ Value (UInt64)(Int64)-1 for size means unknown value. */
} ICompressProgress; } ICompressProgress;
typedef struct typedef struct
{ {
void *(*Alloc)(void *p, size_t size); void*(*Alloc)(void* p, size_t size);
void (*Free)(void *p, void *address); /* address can be 0 */ void (*Free)(void* p, void* address); /* address can be 0 */
} ISzAlloc; } ISzAlloc;
#define IAlloc_Alloc(p, size) (p)->Alloc((p), size) #define IAlloc_Alloc(p, size) (p)->Alloc((p), size)

View file

@ -17,240 +17,240 @@
#define _UPDATE_0 p->range = bound; *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); #define _UPDATE_0 p->range = bound; *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
#define _UPDATE_1 p->range -= bound; p->code -= bound; *prob = (CProb)(ttt - (ttt >> kNumMoveBits)); #define _UPDATE_1 p->range -= bound; p->code -= bound; *prob = (CProb)(ttt - (ttt >> kNumMoveBits));
void Bcj2Dec_Init(CBcj2Dec *p) void Bcj2Dec_Init(CBcj2Dec* p)
{ {
unsigned i; unsigned i;
p->state = BCJ2_DEC_STATE_OK; p->state = BCJ2_DEC_STATE_OK;
p->ip = 0; p->ip = 0;
p->temp[3] = 0; p->temp[3] = 0;
p->range = 0; p->range = 0;
p->code = 0; p->code = 0;
for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++) for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++)
p->probs[i] = kBitModelTotal >> 1; p->probs[i] = kBitModelTotal >> 1;
} }
SRes Bcj2Dec_Decode(CBcj2Dec *p) SRes Bcj2Dec_Decode(CBcj2Dec* p)
{ {
if (p->range <= 5) if (p->range <= 5)
{ {
p->state = BCJ2_DEC_STATE_OK; p->state = BCJ2_DEC_STATE_OK;
for (; p->range != 5; p->range++) for (; p->range != 5; p->range++)
{ {
if (p->range == 1 && p->code != 0) if (p->range == 1 && p->code != 0)
return SZ_ERROR_DATA; return SZ_ERROR_DATA;
if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
{
p->state = BCJ2_STREAM_RC;
return SZ_OK;
}
p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++; if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
} {
p->state = BCJ2_STREAM_RC;
if (p->code == 0xFFFFFFFF) return SZ_OK;
return SZ_ERROR_DATA; }
p->range = 0xFFFFFFFF;
}
else if (p->state >= BCJ2_DEC_STATE_ORIG_0)
{
while (p->state <= BCJ2_DEC_STATE_ORIG_3)
{
Byte *dest = p->dest;
if (dest == p->destLim)
return SZ_OK;
*dest = p->temp[p->state++ - BCJ2_DEC_STATE_ORIG_0];
p->dest = dest + 1;
}
}
/* p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
if (BCJ2_IS_32BIT_STREAM(p->state)) }
{
const Byte *cur = p->bufs[p->state];
if (cur == p->lims[p->state])
return SZ_OK;
p->bufs[p->state] = cur + 4;
{
UInt32 val;
Byte *dest;
SizeT rem;
p->ip += 4;
val = GetBe32(cur) - p->ip;
dest = p->dest;
rem = p->destLim - dest;
if (rem < 4)
{
SizeT i;
SetUi32(p->temp, val);
for (i = 0; i < rem; i++)
dest[i] = p->temp[i];
p->dest = dest + rem;
p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
return SZ_OK;
}
SetUi32(dest, val);
p->temp[3] = (Byte)(val >> 24);
p->dest = dest + 4;
p->state = BCJ2_DEC_STATE_OK;
}
}
*/
for (;;) if (p->code == 0xFFFFFFFF)
{ return SZ_ERROR_DATA;
if (BCJ2_IS_32BIT_STREAM(p->state))
p->state = BCJ2_DEC_STATE_OK;
else
{
if (p->range < kTopValue)
{
if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
{
p->state = BCJ2_STREAM_RC;
return SZ_OK;
}
p->range <<= 8;
p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
}
{ p->range = 0xFFFFFFFF;
const Byte *src = p->bufs[BCJ2_STREAM_MAIN]; }
const Byte *srcLim; else if (p->state >= BCJ2_DEC_STATE_ORIG_0)
Byte *dest; {
SizeT num = p->lims[BCJ2_STREAM_MAIN] - src; while (p->state <= BCJ2_DEC_STATE_ORIG_3)
{
if (num == 0) Byte* dest = p->dest;
{ if (dest == p->destLim)
p->state = BCJ2_STREAM_MAIN; return SZ_OK;
return SZ_OK; *dest = p->temp[p->state++ - BCJ2_DEC_STATE_ORIG_0];
} p->dest = dest + 1;
}
dest = p->dest; }
if (num > (SizeT)(p->destLim - dest))
{
num = p->destLim - dest;
if (num == 0)
{
p->state = BCJ2_DEC_STATE_ORIG;
return SZ_OK;
}
}
srcLim = src + num;
if (p->temp[3] == 0x0F && (src[0] & 0xF0) == 0x80) /*
*dest = src[0]; if (BCJ2_IS_32BIT_STREAM(p->state))
else for (;;) {
{ const Byte *cur = p->bufs[p->state];
Byte b = *src; if (cur == p->lims[p->state])
*dest = b; return SZ_OK;
if (b != 0x0F) p->bufs[p->state] = cur + 4;
{
if ((b & 0xFE) == 0xE8) {
break; UInt32 val;
dest++; Byte *dest;
if (++src != srcLim) SizeT rem;
continue;
break; p->ip += 4;
} val = GetBe32(cur) - p->ip;
dest++; dest = p->dest;
if (++src == srcLim) rem = p->destLim - dest;
break; if (rem < 4)
if ((*src & 0xF0) != 0x80) {
continue; SizeT i;
*dest = *src; SetUi32(p->temp, val);
break; for (i = 0; i < rem; i++)
} dest[i] = p->temp[i];
p->dest = dest + rem;
num = src - p->bufs[BCJ2_STREAM_MAIN]; p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
return SZ_OK;
if (src == srcLim) }
{ SetUi32(dest, val);
p->temp[3] = src[-1]; p->temp[3] = (Byte)(val >> 24);
p->bufs[BCJ2_STREAM_MAIN] = src; p->dest = dest + 4;
p->ip += (UInt32)num; p->state = BCJ2_DEC_STATE_OK;
p->dest += num; }
p->state = }
p->bufs[BCJ2_STREAM_MAIN] == */
p->lims[BCJ2_STREAM_MAIN] ?
(unsigned)BCJ2_STREAM_MAIN :
(unsigned)BCJ2_DEC_STATE_ORIG;
return SZ_OK;
}
{
UInt32 bound, ttt;
CProb *prob;
Byte b = src[0];
Byte prev = (Byte)(num == 0 ? p->temp[3] : src[-1]);
p->temp[3] = b;
p->bufs[BCJ2_STREAM_MAIN] = src + 1;
num++;
p->ip += (UInt32)num;
p->dest += num;
prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)prev : (b == 0xE9 ? 1 : 0));
_IF_BIT_0
{
_UPDATE_0
continue;
}
_UPDATE_1
}
}
}
{ for (;;)
UInt32 val; {
unsigned cj = (p->temp[3] == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP; if (BCJ2_IS_32BIT_STREAM(p->state))
const Byte *cur = p->bufs[cj]; p->state = BCJ2_DEC_STATE_OK;
Byte *dest; else
SizeT rem; {
if (p->range < kTopValue)
if (cur == p->lims[cj]) {
{ if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
p->state = cj; {
break; p->state = BCJ2_STREAM_RC;
} return SZ_OK;
}
val = GetBe32(cur); p->range <<= 8;
p->bufs[cj] = cur + 4; p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
}
p->ip += 4; {
val -= p->ip; const Byte* src = p->bufs[BCJ2_STREAM_MAIN];
dest = p->dest; const Byte* srcLim;
rem = p->destLim - dest; Byte* dest;
SizeT num = p->lims[BCJ2_STREAM_MAIN] - src;
if (rem < 4)
{
SizeT i;
SetUi32(p->temp, val);
for (i = 0; i < rem; i++)
dest[i] = p->temp[i];
p->dest = dest + rem;
p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
break;
}
SetUi32(dest, val);
p->temp[3] = (Byte)(val >> 24);
p->dest = dest + 4;
}
}
if (p->range < kTopValue && p->bufs[BCJ2_STREAM_RC] != p->lims[BCJ2_STREAM_RC]) if (num == 0)
{ {
p->range <<= 8; p->state = BCJ2_STREAM_MAIN;
p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++; return SZ_OK;
} }
return SZ_OK; dest = p->dest;
if (num > (SizeT)(p->destLim - dest))
{
num = p->destLim - dest;
if (num == 0)
{
p->state = BCJ2_DEC_STATE_ORIG;
return SZ_OK;
}
}
srcLim = src + num;
if (p->temp[3] == 0x0F && (src[0] & 0xF0) == 0x80)
*dest = src[0];
else
for (;;)
{
Byte b = *src;
*dest = b;
if (b != 0x0F)
{
if ((b & 0xFE) == 0xE8)
break;
dest++;
if (++src != srcLim)
continue;
break;
}
dest++;
if (++src == srcLim)
break;
if ((*src & 0xF0) != 0x80)
continue;
*dest = *src;
break;
}
num = src - p->bufs[BCJ2_STREAM_MAIN];
if (src == srcLim)
{
p->temp[3] = src[-1];
p->bufs[BCJ2_STREAM_MAIN] = src;
p->ip += (UInt32)num;
p->dest += num;
p->state =
p->bufs[BCJ2_STREAM_MAIN] ==
p->lims[BCJ2_STREAM_MAIN]
? (unsigned)BCJ2_STREAM_MAIN
: (unsigned)BCJ2_DEC_STATE_ORIG;
return SZ_OK;
}
{
UInt32 bound, ttt;
CProb* prob;
Byte b = src[0];
Byte prev = (Byte)(num == 0 ? p->temp[3] : src[-1]);
p->temp[3] = b;
p->bufs[BCJ2_STREAM_MAIN] = src + 1;
num++;
p->ip += (UInt32)num;
p->dest += num;
prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)prev : (b == 0xE9 ? 1 : 0));
_IF_BIT_0
{
_UPDATE_0
continue;
}
_UPDATE_1
}
}
}
{
UInt32 val;
unsigned cj = (p->temp[3] == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP;
const Byte* cur = p->bufs[cj];
Byte* dest;
SizeT rem;
if (cur == p->lims[cj])
{
p->state = cj;
break;
}
val = GetBe32(cur);
p->bufs[cj] = cur + 4;
p->ip += 4;
val -= p->ip;
dest = p->dest;
rem = p->destLim - dest;
if (rem < 4)
{
SizeT i;
SetUi32(p->temp, val);
for (i = 0; i < rem; i++)
dest[i] = p->temp[i];
p->dest = dest + rem;
p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
break;
}
SetUi32(dest, val);
p->temp[3] = (Byte)(val >> 24);
p->dest = dest + 4;
}
}
if (p->range < kTopValue && p->bufs[BCJ2_STREAM_RC] != p->lims[BCJ2_STREAM_RC])
{
p->range <<= 8;
p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
}
return SZ_OK;
} }

View file

@ -12,27 +12,27 @@ EXTERN_C_BEGIN
enum enum
{ {
BCJ2_STREAM_MAIN, BCJ2_STREAM_MAIN,
BCJ2_STREAM_CALL, BCJ2_STREAM_CALL,
BCJ2_STREAM_JUMP, BCJ2_STREAM_JUMP,
BCJ2_STREAM_RC BCJ2_STREAM_RC
}; };
enum enum
{ {
BCJ2_DEC_STATE_ORIG_0 = BCJ2_NUM_STREAMS, BCJ2_DEC_STATE_ORIG_0 = BCJ2_NUM_STREAMS,
BCJ2_DEC_STATE_ORIG_1, BCJ2_DEC_STATE_ORIG_1,
BCJ2_DEC_STATE_ORIG_2, BCJ2_DEC_STATE_ORIG_2,
BCJ2_DEC_STATE_ORIG_3, BCJ2_DEC_STATE_ORIG_3,
BCJ2_DEC_STATE_ORIG, BCJ2_DEC_STATE_ORIG,
BCJ2_DEC_STATE_OK BCJ2_DEC_STATE_OK
}; };
enum enum
{ {
BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS, BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS,
BCJ2_ENC_STATE_OK BCJ2_ENC_STATE_OK
}; };
@ -61,75 +61,74 @@ dest is allowed to overlap with bufs[BCJ2_STREAM_MAIN], with the following condi
typedef struct typedef struct
{ {
const Byte *bufs[BCJ2_NUM_STREAMS]; const Byte* bufs[BCJ2_NUM_STREAMS];
const Byte *lims[BCJ2_NUM_STREAMS]; const Byte* lims[BCJ2_NUM_STREAMS];
Byte *dest; Byte* dest;
const Byte *destLim; const Byte* destLim;
unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */ unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */
UInt32 ip; UInt32 ip;
Byte temp[4]; Byte temp[4];
UInt32 range; UInt32 range;
UInt32 code; UInt32 code;
UInt16 probs[2 + 256]; UInt16 probs[2 + 256];
} CBcj2Dec; } CBcj2Dec;
void Bcj2Dec_Init(CBcj2Dec *p); void Bcj2Dec_Init(CBcj2Dec* p);
/* Returns: SZ_OK or SZ_ERROR_DATA */ /* Returns: SZ_OK or SZ_ERROR_DATA */
SRes Bcj2Dec_Decode(CBcj2Dec *p); SRes Bcj2Dec_Decode(CBcj2Dec* p);
#define Bcj2Dec_IsFinished(_p_) ((_p_)->code == 0) #define Bcj2Dec_IsFinished(_p_) ((_p_)->code == 0)
typedef enum typedef enum
{ {
BCJ2_ENC_FINISH_MODE_CONTINUE, BCJ2_ENC_FINISH_MODE_CONTINUE,
BCJ2_ENC_FINISH_MODE_END_BLOCK, BCJ2_ENC_FINISH_MODE_END_BLOCK,
BCJ2_ENC_FINISH_MODE_END_STREAM BCJ2_ENC_FINISH_MODE_END_STREAM
} EBcj2Enc_FinishMode; } EBcj2Enc_FinishMode;
typedef struct typedef struct
{ {
Byte *bufs[BCJ2_NUM_STREAMS]; Byte* bufs[BCJ2_NUM_STREAMS];
const Byte *lims[BCJ2_NUM_STREAMS]; const Byte* lims[BCJ2_NUM_STREAMS];
const Byte *src; const Byte* src;
const Byte *srcLim; const Byte* srcLim;
unsigned state; unsigned state;
EBcj2Enc_FinishMode finishMode; EBcj2Enc_FinishMode finishMode;
Byte prevByte; Byte prevByte;
Byte cache; Byte cache;
UInt32 range; UInt32 range;
UInt64 low; UInt64 low;
UInt64 cacheSize; UInt64 cacheSize;
UInt32 ip; UInt32 ip;
/* 32-bit ralative offset in JUMP/CALL commands is /* 32-bit ralative offset in JUMP/CALL commands is
- (mod 4 GB) in 32-bit mode - (mod 4 GB) in 32-bit mode
- signed Int32 in 64-bit mode - signed Int32 in 64-bit mode
We use (mod 4 GB) check for fileSize. We use (mod 4 GB) check for fileSize.
Use fileSize up to 2 GB, if you want to support 32-bit and 64-bit code conversion. */ Use fileSize up to 2 GB, if you want to support 32-bit and 64-bit code conversion. */
UInt32 fileIp; UInt32 fileIp;
UInt32 fileSize; /* (fileSize <= ((UInt32)1 << 31)), 0 means no_limit */ UInt32 fileSize; /* (fileSize <= ((UInt32)1 << 31)), 0 means no_limit */
UInt32 relatLimit; /* (relatLimit <= ((UInt32)1 << 31)), 0 means desable_conversion */ UInt32 relatLimit; /* (relatLimit <= ((UInt32)1 << 31)), 0 means desable_conversion */
UInt32 tempTarget; UInt32 tempTarget;
unsigned tempPos; unsigned tempPos;
Byte temp[4 * 2]; Byte temp[4 * 2];
unsigned flushPos; unsigned flushPos;
UInt16 probs[2 + 256]; UInt16 probs[2 + 256];
} CBcj2Enc; } CBcj2Enc;
void Bcj2Enc_Init(CBcj2Enc *p); void Bcj2Enc_Init(CBcj2Enc* p);
void Bcj2Enc_Encode(CBcj2Enc *p); void Bcj2Enc_Encode(CBcj2Enc* p);
#define Bcj2Enc_Get_InputData_Size(p) ((SizeT)((p)->srcLim - (p)->src) + (p)->tempPos) #define Bcj2Enc_Get_InputData_Size(p) ((SizeT)((p)->srcLim - (p)->src) + (p)->tempPos)
#define Bcj2Enc_IsFinished(p) ((p)->flushPos == 5) #define Bcj2Enc_IsFinished(p) ((p)->flushPos == 5)

View file

@ -5,131 +5,131 @@
#include "Bra.h" #include "Bra.h"
SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) SizeT ARM_Convert(Byte* data, SizeT size, UInt32 ip, int encoding)
{ {
SizeT i; SizeT i;
if (size < 4) if (size < 4)
return 0; return 0;
size -= 4; size -= 4;
ip += 8; ip += 8;
for (i = 0; i <= size; i += 4) for (i = 0; i <= size; i += 4)
{ {
if (data[i + 3] == 0xEB) if (data[i + 3] == 0xEB)
{ {
UInt32 dest; UInt32 dest;
UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]); UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]);
src <<= 2; src <<= 2;
if (encoding) if (encoding)
dest = ip + (UInt32)i + src; dest = ip + (UInt32)i + src;
else else
dest = src - (ip + (UInt32)i); dest = src - (ip + (UInt32)i);
dest >>= 2; dest >>= 2;
data[i + 2] = (Byte)(dest >> 16); data[i + 2] = (Byte)(dest >> 16);
data[i + 1] = (Byte)(dest >> 8); data[i + 1] = (Byte)(dest >> 8);
data[i + 0] = (Byte)dest; data[i + 0] = (Byte)dest;
} }
} }
return i; return i;
} }
SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) SizeT ARMT_Convert(Byte* data, SizeT size, UInt32 ip, int encoding)
{ {
SizeT i; SizeT i;
if (size < 4) if (size < 4)
return 0; return 0;
size -= 4; size -= 4;
ip += 4; ip += 4;
for (i = 0; i <= size; i += 2) for (i = 0; i <= size; i += 2)
{ {
if ((data[i + 1] & 0xF8) == 0xF0 && if ((data[i + 1] & 0xF8) == 0xF0 &&
(data[i + 3] & 0xF8) == 0xF8) (data[i + 3] & 0xF8) == 0xF8)
{ {
UInt32 dest; UInt32 dest;
UInt32 src = UInt32 src =
(((UInt32)data[i + 1] & 0x7) << 19) | (((UInt32)data[i + 1] & 0x7) << 19) |
((UInt32)data[i + 0] << 11) | ((UInt32)data[i + 0] << 11) |
(((UInt32)data[i + 3] & 0x7) << 8) | (((UInt32)data[i + 3] & 0x7) << 8) |
(data[i + 2]); (data[i + 2]);
src <<= 1; src <<= 1;
if (encoding) if (encoding)
dest = ip + (UInt32)i + src; dest = ip + (UInt32)i + src;
else else
dest = src - (ip + (UInt32)i); dest = src - (ip + (UInt32)i);
dest >>= 1; dest >>= 1;
data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7)); data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
data[i + 0] = (Byte)(dest >> 11); data[i + 0] = (Byte)(dest >> 11);
data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7)); data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
data[i + 2] = (Byte)dest; data[i + 2] = (Byte)dest;
i += 2; i += 2;
} }
} }
return i; return i;
} }
SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) SizeT PPC_Convert(Byte* data, SizeT size, UInt32 ip, int encoding)
{ {
SizeT i; SizeT i;
if (size < 4) if (size < 4)
return 0; return 0;
size -= 4; size -= 4;
for (i = 0; i <= size; i += 4) for (i = 0; i <= size; i += 4)
{ {
if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1) if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1)
{ {
UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) | UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) |
((UInt32)data[i + 1] << 16) | ((UInt32)data[i + 1] << 16) |
((UInt32)data[i + 2] << 8) | ((UInt32)data[i + 2] << 8) |
((UInt32)data[i + 3] & (~3)); ((UInt32)data[i + 3] & (~3));
UInt32 dest; UInt32 dest;
if (encoding) if (encoding)
dest = ip + (UInt32)i + src; dest = ip + (UInt32)i + src;
else else
dest = src - (ip + (UInt32)i); dest = src - (ip + (UInt32)i);
data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3)); data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3));
data[i + 1] = (Byte)(dest >> 16); data[i + 1] = (Byte)(dest >> 16);
data[i + 2] = (Byte)(dest >> 8); data[i + 2] = (Byte)(dest >> 8);
data[i + 3] &= 0x3; data[i + 3] &= 0x3;
data[i + 3] |= dest; data[i + 3] |= dest;
} }
} }
return i; return i;
} }
SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) SizeT SPARC_Convert(Byte* data, SizeT size, UInt32 ip, int encoding)
{ {
UInt32 i; UInt32 i;
if (size < 4) if (size < 4)
return 0; return 0;
size -= 4; size -= 4;
for (i = 0; i <= size; i += 4) for (i = 0; i <= size; i += 4)
{ {
if ((data[i] == 0x40 && (data[i + 1] & 0xC0) == 0x00) || if ((data[i] == 0x40 && (data[i + 1] & 0xC0) == 0x00) ||
(data[i] == 0x7F && (data[i + 1] & 0xC0) == 0xC0)) (data[i] == 0x7F && (data[i + 1] & 0xC0) == 0xC0))
{ {
UInt32 src = UInt32 src =
((UInt32)data[i + 0] << 24) | ((UInt32)data[i + 0] << 24) |
((UInt32)data[i + 1] << 16) | ((UInt32)data[i + 1] << 16) |
((UInt32)data[i + 2] << 8) | ((UInt32)data[i + 2] << 8) |
((UInt32)data[i + 3]); ((UInt32)data[i + 3]);
UInt32 dest; UInt32 dest;
src <<= 2;
if (encoding)
dest = ip + i + src;
else
dest = src - (ip + i);
dest >>= 2;
dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;
data[i + 0] = (Byte)(dest >> 24); src <<= 2;
data[i + 1] = (Byte)(dest >> 16); if (encoding)
data[i + 2] = (Byte)(dest >> 8); dest = ip + i + src;
data[i + 3] = (Byte)dest; else
} dest = src - (ip + i);
} dest >>= 2;
return i;
dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;
data[i + 0] = (Byte)(dest >> 24);
data[i + 1] = (Byte)(dest >> 16);
data[i + 2] = (Byte)(dest >> 8);
data[i + 3] = (Byte)dest;
}
}
return i;
} }

View file

@ -52,12 +52,12 @@ in CALL instructions to increase the compression ratio.
*/ */
#define x86_Convert_Init(state) { state = 0; } #define x86_Convert_Init(state) { state = 0; }
SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding); SizeT x86_Convert(Byte* data, SizeT size, UInt32 ip, UInt32* state, int encoding);
SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); SizeT ARM_Convert(Byte* data, SizeT size, UInt32 ip, int encoding);
SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); SizeT ARMT_Convert(Byte* data, SizeT size, UInt32 ip, int encoding);
SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); SizeT PPC_Convert(Byte* data, SizeT size, UInt32 ip, int encoding);
SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); SizeT SPARC_Convert(Byte* data, SizeT size, UInt32 ip, int encoding);
SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); SizeT IA64_Convert(Byte* data, SizeT size, UInt32 ip, int encoding);
EXTERN_C_END EXTERN_C_END

View file

@ -7,76 +7,76 @@
#define Test86MSByte(b) ((((b) + 1) & 0xFE) == 0) #define Test86MSByte(b) ((((b) + 1) & 0xFE) == 0)
SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding) SizeT x86_Convert(Byte* data, SizeT size, UInt32 ip, UInt32* state, int encoding)
{ {
SizeT pos = 0; SizeT pos = 0;
UInt32 mask = *state & 7; UInt32 mask = *state & 7;
if (size < 5) if (size < 5)
return 0; return 0;
size -= 4; size -= 4;
ip += 5; ip += 5;
for (;;) for (;;)
{ {
Byte *p = data + pos; Byte* p = data + pos;
const Byte *limit = data + size; const Byte* limit = data + size;
for (; p < limit; p++) for (; p < limit; p++)
if ((*p & 0xFE) == 0xE8) if ((*p & 0xFE) == 0xE8)
break; break;
{ {
SizeT d = (SizeT)(p - data - pos); SizeT d = (SizeT)(p - data - pos);
pos = (SizeT)(p - data); pos = (SizeT)(p - data);
if (p >= limit) if (p >= limit)
{ {
*state = (d > 2 ? 0 : mask >> (unsigned)d); *state = (d > 2 ? 0 : mask >> (unsigned)d);
return pos; return pos;
} }
if (d > 2) if (d > 2)
mask = 0; mask = 0;
else else
{ {
mask >>= (unsigned)d; mask >>= (unsigned)d;
if (mask != 0 && (mask > 4 || mask == 3 || Test86MSByte(p[(mask >> 1) + 1]))) if (mask != 0 && (mask > 4 || mask == 3 || Test86MSByte(p[(mask >> 1) + 1])))
{ {
mask = (mask >> 1) | 4; mask = (mask >> 1) | 4;
pos++; pos++;
continue; continue;
} }
} }
} }
if (Test86MSByte(p[4])) if (Test86MSByte(p[4]))
{ {
UInt32 v = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]); UInt32 v = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
UInt32 cur = ip + (UInt32)pos; UInt32 cur = ip + (UInt32)pos;
pos += 5; pos += 5;
if (encoding) if (encoding)
v += cur; v += cur;
else else
v -= cur; v -= cur;
if (mask != 0) if (mask != 0)
{ {
unsigned sh = (mask & 6) << 2; unsigned sh = (mask & 6) << 2;
if (Test86MSByte((Byte)(v >> sh))) if (Test86MSByte((Byte)(v >> sh)))
{ {
v ^= (((UInt32)0x100 << sh) - 1); v ^= (((UInt32)0x100 << sh) - 1);
if (encoding) if (encoding)
v += cur; v += cur;
else else
v -= cur; v -= cur;
} }
mask = 0; mask = 0;
} }
p[1] = (Byte)v; p[1] = (Byte)v;
p[2] = (Byte)(v >> 8); p[2] = (Byte)(v >> 8);
p[3] = (Byte)(v >> 16); p[3] = (Byte)(v >> 16);
p[4] = (Byte)(0 - ((v >> 24) & 1)); p[4] = (Byte)(0 - ((v >> 24) & 1));
} }
else else
{ {
mask = (mask >> 1) | 4; mask = (mask >> 1) | 4;
pos++; pos++;
} }
} }
} }

View file

@ -7,63 +7,63 @@
static const Byte kBranchTable[32] = static const Byte kBranchTable[32] =
{ {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 4, 6, 6, 0, 0, 7, 7, 4, 4, 6, 6, 0, 0, 7, 7,
4, 4, 0, 0, 4, 4, 0, 0 4, 4, 0, 0, 4, 4, 0, 0
}; };
SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) SizeT IA64_Convert(Byte* data, SizeT size, UInt32 ip, int encoding)
{ {
SizeT i; SizeT i;
if (size < 16) if (size < 16)
return 0; return 0;
size -= 16; size -= 16;
for (i = 0; i <= size; i += 16) for (i = 0; i <= size; i += 16)
{ {
UInt32 instrTemplate = data[i] & 0x1F; UInt32 instrTemplate = data[i] & 0x1F;
UInt32 mask = kBranchTable[instrTemplate]; UInt32 mask = kBranchTable[instrTemplate];
UInt32 bitPos = 5; UInt32 bitPos = 5;
int slot; int slot;
for (slot = 0; slot < 3; slot++, bitPos += 41) for (slot = 0; slot < 3; slot++, bitPos += 41)
{ {
UInt32 bytePos, bitRes; UInt32 bytePos, bitRes;
UInt64 instruction, instNorm; UInt64 instruction, instNorm;
int j; int j;
if (((mask >> slot) & 1) == 0) if (((mask >> slot) & 1) == 0)
continue; continue;
bytePos = (bitPos >> 3); bytePos = (bitPos >> 3);
bitRes = bitPos & 0x7; bitRes = bitPos & 0x7;
instruction = 0; instruction = 0;
for (j = 0; j < 6; j++) for (j = 0; j < 6; j++)
instruction += (UInt64)data[i + j + bytePos] << (8 * j); instruction += (UInt64)data[i + j + bytePos] << (8 * j);
instNorm = instruction >> bitRes; instNorm = instruction >> bitRes;
if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0) if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0)
{ {
UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF); UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF);
UInt32 dest; UInt32 dest;
src |= ((UInt32)(instNorm >> 36) & 1) << 20; src |= ((UInt32)(instNorm >> 36) & 1) << 20;
src <<= 4; src <<= 4;
if (encoding) if (encoding)
dest = ip + (UInt32)i + src; dest = ip + (UInt32)i + src;
else else
dest = src - (ip + (UInt32)i); dest = src - (ip + (UInt32)i);
dest >>= 4; dest >>= 4;
instNorm &= ~((UInt64)(0x8FFFFF) << 13); instNorm &= ~((UInt64)(0x8FFFFF) << 13);
instNorm |= ((UInt64)(dest & 0xFFFFF) << 13); instNorm |= ((UInt64)(dest & 0xFFFFF) << 13);
instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20)); instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20));
instruction &= (1 << bitRes) - 1; instruction &= (1 << bitRes) - 1;
instruction |= (instNorm << bitRes); instruction |= (instNorm << bitRes);
for (j = 0; j < 6; j++) for (j = 0; j < 6; j++)
data[i + j + bytePos] = (Byte)(instruction >> (8 * j)); data[i + j + bytePos] = (Byte)(instruction >> (8 * j));
} }
} }
} }
return i; return i;
} }

View file

@ -6,23 +6,23 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#ifdef UNDER_CE #ifdef UNDER_CE
#define RPC_NO_WINDOWS_H #define RPC_NO_WINDOWS_H
/* #pragma warning(disable : 4115) // '_RPC_ASYNC_STATE' : named type definition in parentheses */ /* #pragma warning(disable : 4115) // '_RPC_ASYNC_STATE' : named type definition in parentheses */
#pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
#pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int #pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int
#endif #endif
#if _MSC_VER >= 1300 #if _MSC_VER >= 1300
#pragma warning(disable : 4996) // This function or variable may be unsafe #pragma warning(disable : 4996) // This function or variable may be unsafe
#else #else
#pragma warning(disable : 4511) // copy constructor could not be generated #pragma warning(disable : 4511) // copy constructor could not be generated
#pragma warning(disable : 4512) // assignment operator could not be generated #pragma warning(disable : 4512) // assignment operator could not be generated
#pragma warning(disable : 4514) // unreferenced inline function has been removed #pragma warning(disable : 4514) // unreferenced inline function has been removed
#pragma warning(disable : 4702) // unreachable code #pragma warning(disable : 4702) // unreachable code
#pragma warning(disable : 4710) // not inlined #pragma warning(disable : 4710) // not inlined
#pragma warning(disable : 4786) // identifier was truncated to '255' characters in the debug information #pragma warning(disable : 4786) // identifier was truncated to '255' characters in the debug information
#endif #endif
#endif #endif

View file

@ -18,7 +18,7 @@
#if defined(USE_ASM) && !defined(MY_CPU_AMD64) #if defined(USE_ASM) && !defined(MY_CPU_AMD64)
static UInt32 CheckFlag(UInt32 flag) static UInt32 CheckFlag(UInt32 flag)
{ {
#ifdef _MSC_VER #ifdef _MSC_VER
__asm pushfd; __asm pushfd;
__asm pop EAX; __asm pop EAX;
__asm mov EDX, EAX; __asm mov EDX, EAX;
@ -31,7 +31,7 @@ static UInt32 CheckFlag(UInt32 flag)
__asm push EDX; __asm push EDX;
__asm popfd; __asm popfd;
__asm and flag, EAX; __asm and flag, EAX;
#else #else
__asm__ __volatile__ ( __asm__ __volatile__ (
"pushf\n\t" "pushf\n\t"
"pop %%EAX\n\t" "pop %%EAX\n\t"
@ -47,7 +47,7 @@ static UInt32 CheckFlag(UInt32 flag)
"andl %%EAX, %0\n\t": "andl %%EAX, %0\n\t":
"=c" (flag) : "c" (flag) : "=c" (flag) : "c" (flag) :
"%eax", "%edx"); "%eax", "%edx");
#endif #endif
return flag; return flag;
} }
#define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False; #define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
@ -55,11 +55,11 @@ static UInt32 CheckFlag(UInt32 flag)
#define CHECK_CPUID_IS_SUPPORTED #define CHECK_CPUID_IS_SUPPORTED
#endif #endif
void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d) void MyCPUID(UInt32 function, UInt32* a, UInt32* b, UInt32* c, UInt32* d)
{ {
#ifdef USE_ASM #ifdef USE_ASM
#ifdef _MSC_VER #ifdef _MSC_VER
UInt32 a2, b2, c2, d2; UInt32 a2, b2, c2, d2;
__asm xor EBX, EBX; __asm xor EBX, EBX;
@ -77,100 +77,100 @@ void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
*c = c2; *c = c2;
*d = d2; *d = d2;
#else #else
__asm__ __volatile__ ( __asm__ __volatile__ (
#if defined(MY_CPU_AMD64) && defined(__PIC__) #if defined(MY_CPU_AMD64) && defined(__PIC__)
"mov %%rbx, %%rdi;" "mov %%rbx, %%rdi;"
"cpuid;" "cpuid;"
"xchg %%rbx, %%rdi;" "xchg %%rbx, %%rdi;"
: "=a" (*a) , : "=a" (*a) ,
"=D" (*b) , "=D" (*b) ,
#elif defined(MY_CPU_X86) && defined(__PIC__) #elif defined(MY_CPU_X86) && defined(__PIC__)
"mov %%ebx, %%edi;" "mov %%ebx, %%edi;"
"cpuid;" "cpuid;"
"xchgl %%ebx, %%edi;" "xchgl %%ebx, %%edi;"
: "=a" (*a) , : "=a" (*a) ,
"=D" (*b) , "=D" (*b) ,
#else #else
"cpuid" "cpuid"
: "=a" (*a) , : "=a" (*a) ,
"=b" (*b) , "=b" (*b) ,
#endif #endif
"=c" (*c) , "=c" (*c) ,
"=d" (*d) "=d" (*d)
: "0" (function)) ; : "0" (function)) ;
#endif #endif
#else
int CPUInfo[4]; #else
__cpuid(CPUInfo, function);
*a = CPUInfo[0];
*b = CPUInfo[1];
*c = CPUInfo[2];
*d = CPUInfo[3];
#endif int CPUInfo[4];
__cpuid(CPUInfo, function);
*a = CPUInfo[0];
*b = CPUInfo[1];
*c = CPUInfo[2];
*d = CPUInfo[3];
#endif
} }
Bool x86cpuid_CheckAndRead(Cx86cpuid *p) Bool x86cpuid_CheckAndRead(Cx86cpuid* p)
{ {
CHECK_CPUID_IS_SUPPORTED CHECK_CPUID_IS_SUPPORTED
MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]); MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
MyCPUID(1, &p->ver, &p->b, &p->c, &p->d); MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
return True; return True;
} }
static const UInt32 kVendors[][3] = static const UInt32 kVendors[][3] =
{ {
{ 0x756E6547, 0x49656E69, 0x6C65746E}, {0x756E6547, 0x49656E69, 0x6C65746E},
{ 0x68747541, 0x69746E65, 0x444D4163}, {0x68747541, 0x69746E65, 0x444D4163},
{ 0x746E6543, 0x48727561, 0x736C7561} {0x746E6543, 0x48727561, 0x736C7561}
}; };
int x86cpuid_GetFirm(const Cx86cpuid *p) int x86cpuid_GetFirm(const Cx86cpuid* p)
{ {
unsigned i; unsigned i;
for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++) for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
{ {
const UInt32 *v = kVendors[i]; const UInt32* v = kVendors[i];
if (v[0] == p->vendor[0] && if (v[0] == p->vendor[0] &&
v[1] == p->vendor[1] && v[1] == p->vendor[1] &&
v[2] == p->vendor[2]) v[2] == p->vendor[2])
return (int)i; return (int)i;
} }
return -1; return -1;
} }
Bool CPU_Is_InOrder() Bool CPU_Is_InOrder()
{ {
Cx86cpuid p; Cx86cpuid p;
int firm; int firm;
UInt32 family, model; UInt32 family, model;
if (!x86cpuid_CheckAndRead(&p)) if (!x86cpuid_CheckAndRead(&p))
return True; return True;
family = x86cpuid_GetFamily(p.ver); family = x86cpuid_GetFamily(p.ver);
model = x86cpuid_GetModel(p.ver); model = x86cpuid_GetModel(p.ver);
firm = x86cpuid_GetFirm(&p);
switch (firm) firm = x86cpuid_GetFirm(&p);
{
case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && ( switch (firm)
/* In-Order Atom CPU */ {
model == 0x1C /* 45 nm, N4xx, D4xx, N5xx, D5xx, 230, 330 */ case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && (
|| model == 0x26 /* 45 nm, Z6xx */ /* In-Order Atom CPU */
|| model == 0x27 /* 32 nm, Z2460 */ model == 0x1C /* 45 nm, N4xx, D4xx, N5xx, D5xx, 230, 330 */
|| model == 0x35 /* 32 nm, Z2760 */ || model == 0x26 /* 45 nm, Z6xx */
|| model == 0x36 /* 32 nm, N2xxx, D2xxx */ || model == 0x27 /* 32 nm, Z2460 */
))); || model == 0x35 /* 32 nm, Z2760 */
case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA))); || model == 0x36 /* 32 nm, N2xxx, D2xxx */
case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF)); )));
} case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
return True; case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
}
return True;
} }
#if !defined(MY_CPU_AMD64) && defined(_WIN32) #if !defined(MY_CPU_AMD64) && defined(_WIN32)
@ -190,11 +190,11 @@ static Bool CPU_Sys_Is_SSE_Supported()
Bool CPU_Is_Aes_Supported() Bool CPU_Is_Aes_Supported()
{ {
Cx86cpuid p; Cx86cpuid p;
CHECK_SYS_SSE_SUPPORT CHECK_SYS_SSE_SUPPORT
if (!x86cpuid_CheckAndRead(&p)) if (!x86cpuid_CheckAndRead(&p))
return False; return False;
return (p.c >> 25) & 1; return (p.c >> 25) & 1;
} }
#endif #endif

View file

@ -21,14 +21,14 @@ MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned mem
|| defined(__x86_64__) \ || defined(__x86_64__) \
|| defined(__AMD64__) \ || defined(__AMD64__) \
|| defined(__amd64__) || defined(__amd64__)
#define MY_CPU_AMD64 #define MY_CPU_AMD64
#endif #endif
#if defined(MY_CPU_AMD64) \ #if defined(MY_CPU_AMD64) \
|| defined(_M_IA64) \ || defined(_M_IA64) \
|| defined(__AARCH64EL__) \ || defined(__AARCH64EL__) \
|| defined(__AARCH64EB__) || defined(__AARCH64EB__)
#define MY_CPU_64BIT #define MY_CPU_64BIT
#endif #endif
#if defined(_M_IX86) || defined(__i386__) #if defined(_M_IX86) || defined(__i386__)
@ -67,7 +67,7 @@ MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned mem
|| defined(__MIPSEL) \ || defined(__MIPSEL) \
|| defined(_MIPSEL) \ || defined(_MIPSEL) \
|| (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
#define MY_CPU_LE #define MY_CPU_LE
#endif #endif
#if defined(__BIG_ENDIAN__) \ #if defined(__BIG_ENDIAN__) \
@ -91,10 +91,10 @@ Stop_Compiling_Bad_Endian
#ifdef MY_CPU_LE #ifdef MY_CPU_LE
#if defined(MY_CPU_X86_OR_AMD64) \ #if defined(MY_CPU_X86_OR_AMD64) \
/* || defined(__AARCH64EL__) */ /* || defined(__AARCH64EL__) */
#define MY_CPU_LE_UNALIGN #define MY_CPU_LE_UNALIGN
#endif #endif
#endif #endif
@ -183,30 +183,29 @@ Stop_Compiling_Bad_Endian
((const Byte *)(p))[1] )) ((const Byte *)(p))[1] ))
#ifdef MY_CPU_X86_OR_AMD64 #ifdef MY_CPU_X86_OR_AMD64
typedef struct typedef struct
{ {
UInt32 maxFunc; UInt32 maxFunc;
UInt32 vendor[3]; UInt32 vendor[3];
UInt32 ver; UInt32 ver;
UInt32 b; UInt32 b;
UInt32 c; UInt32 c;
UInt32 d; UInt32 d;
} Cx86cpuid; } Cx86cpuid;
enum enum
{ {
CPU_FIRM_INTEL, CPU_FIRM_INTEL,
CPU_FIRM_AMD, CPU_FIRM_AMD,
CPU_FIRM_VIA CPU_FIRM_VIA
}; };
void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d); void MyCPUID(UInt32 function, UInt32* a, UInt32* b, UInt32* c, UInt32* d);
Bool x86cpuid_CheckAndRead(Cx86cpuid *p); Bool x86cpuid_CheckAndRead(Cx86cpuid* p);
int x86cpuid_GetFirm(const Cx86cpuid *p); int x86cpuid_GetFirm(const Cx86cpuid* p);
#define x86cpuid_GetFamily(ver) (((ver >> 16) & 0xFF0) | ((ver >> 8) & 0xF)) #define x86cpuid_GetFamily(ver) (((ver >> 16) & 0xFF0) | ((ver >> 8) & 0xF))
#define x86cpuid_GetModel(ver) (((ver >> 12) & 0xF0) | ((ver >> 4) & 0xF)) #define x86cpuid_GetModel(ver) (((ver >> 12) & 0xF0) | ((ver >> 4) & 0xF))

View file

@ -5,60 +5,60 @@
#include "Delta.h" #include "Delta.h"
void Delta_Init(Byte *state) void Delta_Init(Byte* state)
{ {
unsigned i; unsigned i;
for (i = 0; i < DELTA_STATE_SIZE; i++) for (i = 0; i < DELTA_STATE_SIZE; i++)
state[i] = 0; state[i] = 0;
} }
static void MyMemCpy(Byte *dest, const Byte *src, unsigned size) static void MyMemCpy(Byte* dest, const Byte* src, unsigned size)
{ {
unsigned i; unsigned i;
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
dest[i] = src[i]; dest[i] = src[i];
} }
void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size) void Delta_Encode(Byte* state, unsigned delta, Byte* data, SizeT size)
{ {
Byte buf[DELTA_STATE_SIZE]; Byte buf[DELTA_STATE_SIZE];
unsigned j = 0; unsigned j = 0;
MyMemCpy(buf, state, delta); MyMemCpy(buf, state, delta);
{ {
SizeT i; SizeT i;
for (i = 0; i < size;) for (i = 0; i < size;)
{ {
for (j = 0; j < delta && i < size; i++, j++) for (j = 0; j < delta && i < size; i++, j++)
{ {
Byte b = data[i]; Byte b = data[i];
data[i] = (Byte)(b - buf[j]); data[i] = (Byte)(b - buf[j]);
buf[j] = b; buf[j] = b;
} }
} }
} }
if (j == delta) if (j == delta)
j = 0; j = 0;
MyMemCpy(state, buf + j, delta - j); MyMemCpy(state, buf + j, delta - j);
MyMemCpy(state + delta - j, buf, j); MyMemCpy(state + delta - j, buf, j);
} }
void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size) void Delta_Decode(Byte* state, unsigned delta, Byte* data, SizeT size)
{ {
Byte buf[DELTA_STATE_SIZE]; Byte buf[DELTA_STATE_SIZE];
unsigned j = 0; unsigned j = 0;
MyMemCpy(buf, state, delta); MyMemCpy(buf, state, delta);
{ {
SizeT i; SizeT i;
for (i = 0; i < size;) for (i = 0; i < size;)
{ {
for (j = 0; j < delta && i < size; i++, j++) for (j = 0; j < delta && i < size; i++, j++)
{ {
buf[j] = data[i] = (Byte)(buf[j] + data[i]); buf[j] = data[i] = (Byte)(buf[j] + data[i]);
} }
} }
} }
if (j == delta) if (j == delta)
j = 0; j = 0;
MyMemCpy(state, buf + j, delta - j); MyMemCpy(state, buf + j, delta - j);
MyMemCpy(state + delta - j, buf, j); MyMemCpy(state + delta - j, buf, j);
} }

View file

@ -10,9 +10,9 @@ EXTERN_C_BEGIN
#define DELTA_STATE_SIZE 256 #define DELTA_STATE_SIZE 256
void Delta_Init(Byte *state); void Delta_Init(Byte* state);
void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size); void Delta_Encode(Byte* state, unsigned delta, Byte* data, SizeT size);
void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size); void Delta_Decode(Byte* state, unsigned delta, Byte* data, SizeT size);
EXTERN_C_END EXTERN_C_END

View file

@ -48,331 +48,333 @@
typedef enum typedef enum
{ {
LZMA2_STATE_CONTROL, LZMA2_STATE_CONTROL,
LZMA2_STATE_UNPACK0, LZMA2_STATE_UNPACK0,
LZMA2_STATE_UNPACK1, LZMA2_STATE_UNPACK1,
LZMA2_STATE_PACK0, LZMA2_STATE_PACK0,
LZMA2_STATE_PACK1, LZMA2_STATE_PACK1,
LZMA2_STATE_PROP, LZMA2_STATE_PROP,
LZMA2_STATE_DATA, LZMA2_STATE_DATA,
LZMA2_STATE_DATA_CONT, LZMA2_STATE_DATA_CONT,
LZMA2_STATE_FINISHED, LZMA2_STATE_FINISHED,
LZMA2_STATE_ERROR LZMA2_STATE_ERROR
} ELzma2State; } ELzma2State;
static SRes Lzma2Dec_GetOldProps(Byte prop, Byte *props) static SRes Lzma2Dec_GetOldProps(Byte prop, Byte* props)
{ {
UInt32 dicSize; UInt32 dicSize;
if (prop > 40) if (prop > 40)
return SZ_ERROR_UNSUPPORTED; return SZ_ERROR_UNSUPPORTED;
dicSize = (prop == 40) ? 0xFFFFFFFF : LZMA2_DIC_SIZE_FROM_PROP(prop); dicSize = (prop == 40) ? 0xFFFFFFFF : LZMA2_DIC_SIZE_FROM_PROP(prop);
props[0] = (Byte)LZMA2_LCLP_MAX; props[0] = (Byte)LZMA2_LCLP_MAX;
props[1] = (Byte)(dicSize); props[1] = (Byte)(dicSize);
props[2] = (Byte)(dicSize >> 8); props[2] = (Byte)(dicSize >> 8);
props[3] = (Byte)(dicSize >> 16); props[3] = (Byte)(dicSize >> 16);
props[4] = (Byte)(dicSize >> 24); props[4] = (Byte)(dicSize >> 24);
return SZ_OK; return SZ_OK;
} }
SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAlloc *alloc) SRes Lzma2Dec_AllocateProbs(CLzma2Dec* p, Byte prop, ISzAlloc* alloc)
{ {
Byte props[LZMA_PROPS_SIZE]; Byte props[LZMA_PROPS_SIZE];
RINOK(Lzma2Dec_GetOldProps(prop, props)); RINOK(Lzma2Dec_GetOldProps(prop, props));
return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc); return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
} }
SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAlloc *alloc) SRes Lzma2Dec_Allocate(CLzma2Dec* p, Byte prop, ISzAlloc* alloc)
{ {
Byte props[LZMA_PROPS_SIZE]; Byte props[LZMA_PROPS_SIZE];
RINOK(Lzma2Dec_GetOldProps(prop, props)); RINOK(Lzma2Dec_GetOldProps(prop, props));
return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc); return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
} }
void Lzma2Dec_Init(CLzma2Dec *p) void Lzma2Dec_Init(CLzma2Dec* p)
{ {
p->state = LZMA2_STATE_CONTROL; p->state = LZMA2_STATE_CONTROL;
p->needInitDic = True; p->needInitDic = True;
p->needInitState = True; p->needInitState = True;
p->needInitProp = True; p->needInitProp = True;
LzmaDec_Init(&p->decoder); LzmaDec_Init(&p->decoder);
} }
static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b) static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec* p, Byte b)
{ {
switch (p->state) switch (p->state)
{ {
case LZMA2_STATE_CONTROL: case LZMA2_STATE_CONTROL:
p->control = b; p->control = b;
PRF(printf("\n %4X ", (unsigned)p->decoder.dicPos)); PRF(printf("\n %4X ", (unsigned)p->decoder.dicPos));
PRF(printf(" %2X", (unsigned)b)); PRF(printf(" %2X", (unsigned)b));
if (p->control == 0) if (p->control == 0)
return LZMA2_STATE_FINISHED; return LZMA2_STATE_FINISHED;
if (LZMA2_IS_UNCOMPRESSED_STATE(p)) if (LZMA2_IS_UNCOMPRESSED_STATE(p))
{ {
if ((p->control & 0x7F) > 2) if ((p->control & 0x7F) > 2)
return LZMA2_STATE_ERROR; return LZMA2_STATE_ERROR;
p->unpackSize = 0; p->unpackSize = 0;
} }
else else
p->unpackSize = (UInt32)(p->control & 0x1F) << 16; p->unpackSize = (UInt32)(p->control & 0x1F) << 16;
return LZMA2_STATE_UNPACK0; return LZMA2_STATE_UNPACK0;
case LZMA2_STATE_UNPACK0:
p->unpackSize |= (UInt32)b << 8;
return LZMA2_STATE_UNPACK1;
case LZMA2_STATE_UNPACK1:
p->unpackSize |= (UInt32)b;
p->unpackSize++;
PRF(printf(" %8u", (unsigned)p->unpackSize));
return (LZMA2_IS_UNCOMPRESSED_STATE(p)) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0;
case LZMA2_STATE_PACK0:
p->packSize = (UInt32)b << 8;
return LZMA2_STATE_PACK1;
case LZMA2_STATE_PACK1: case LZMA2_STATE_UNPACK0:
p->packSize |= (UInt32)b; p->unpackSize |= (UInt32)b << 8;
p->packSize++; return LZMA2_STATE_UNPACK1;
PRF(printf(" %8u", (unsigned)p->packSize));
return LZMA2_IS_THERE_PROP(LZMA2_GET_LZMA_MODE(p)) ? LZMA2_STATE_PROP:
(p->needInitProp ? LZMA2_STATE_ERROR : LZMA2_STATE_DATA);
case LZMA2_STATE_PROP: case LZMA2_STATE_UNPACK1:
{ p->unpackSize |= (UInt32)b;
unsigned lc, lp; p->unpackSize++;
if (b >= (9 * 5 * 5)) PRF(printf(" %8u", (unsigned)p->unpackSize));
return LZMA2_STATE_ERROR; return (LZMA2_IS_UNCOMPRESSED_STATE(p)) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0;
lc = b % 9;
b /= 9; case LZMA2_STATE_PACK0:
p->decoder.prop.pb = b / 5; p->packSize = (UInt32)b << 8;
lp = b % 5; return LZMA2_STATE_PACK1;
if (lc + lp > LZMA2_LCLP_MAX)
return LZMA2_STATE_ERROR; case LZMA2_STATE_PACK1:
p->decoder.prop.lc = lc; p->packSize |= (UInt32)b;
p->decoder.prop.lp = lp; p->packSize++;
p->needInitProp = False; PRF(printf(" %8u", (unsigned)p->packSize));
return LZMA2_STATE_DATA; return LZMA2_IS_THERE_PROP(LZMA2_GET_LZMA_MODE(p))
} ? LZMA2_STATE_PROP
} : (p->needInitProp ? LZMA2_STATE_ERROR : LZMA2_STATE_DATA);
return LZMA2_STATE_ERROR;
case LZMA2_STATE_PROP:
{
unsigned lc, lp;
if (b >= (9 * 5 * 5))
return LZMA2_STATE_ERROR;
lc = b % 9;
b /= 9;
p->decoder.prop.pb = b / 5;
lp = b % 5;
if (lc + lp > LZMA2_LCLP_MAX)
return LZMA2_STATE_ERROR;
p->decoder.prop.lc = lc;
p->decoder.prop.lp = lp;
p->needInitProp = False;
return LZMA2_STATE_DATA;
}
}
return LZMA2_STATE_ERROR;
} }
static void LzmaDec_UpdateWithUncompressed(CLzmaDec *p, const Byte *src, SizeT size) static void LzmaDec_UpdateWithUncompressed(CLzmaDec* p, const Byte* src, SizeT size)
{ {
memcpy(p->dic + p->dicPos, src, size); memcpy(p->dic + p->dicPos, src, size);
p->dicPos += size; p->dicPos += size;
if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size) if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size)
p->checkDicSize = p->prop.dicSize; p->checkDicSize = p->prop.dicSize;
p->processedPos += (UInt32)size; p->processedPos += (UInt32)size;
} }
void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState); void LzmaDec_InitDicAndState(CLzmaDec* p, Bool initDic, Bool initState);
SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit, SRes Lzma2Dec_DecodeToDic(CLzma2Dec* p, SizeT dicLimit,
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status) const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status)
{ {
SizeT inSize = *srcLen; SizeT inSize = *srcLen;
*srcLen = 0; *srcLen = 0;
*status = LZMA_STATUS_NOT_SPECIFIED; *status = LZMA_STATUS_NOT_SPECIFIED;
while (p->state != LZMA2_STATE_FINISHED) while (p->state != LZMA2_STATE_FINISHED)
{ {
SizeT dicPos = p->decoder.dicPos; SizeT dicPos = p->decoder.dicPos;
if (p->state == LZMA2_STATE_ERROR)
return SZ_ERROR_DATA;
if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
{
*status = LZMA_STATUS_NOT_FINISHED;
return SZ_OK;
}
if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT) if (p->state == LZMA2_STATE_ERROR)
{ return SZ_ERROR_DATA;
if (*srcLen == inSize)
{
*status = LZMA_STATUS_NEEDS_MORE_INPUT;
return SZ_OK;
}
(*srcLen)++;
p->state = Lzma2Dec_UpdateState(p, *src++);
if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED) if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
{ {
p->state = LZMA2_STATE_ERROR; *status = LZMA_STATUS_NOT_FINISHED;
return SZ_ERROR_DATA; return SZ_OK;
} }
continue;
}
{
SizeT destSizeCur = dicLimit - dicPos;
SizeT srcSizeCur = inSize - *srcLen;
ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
if (p->unpackSize <= destSizeCur)
{
destSizeCur = (SizeT)p->unpackSize;
curFinishMode = LZMA_FINISH_END;
}
if (LZMA2_IS_UNCOMPRESSED_STATE(p)) if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
{ {
if (*srcLen == inSize) if (*srcLen == inSize)
{ {
*status = LZMA_STATUS_NEEDS_MORE_INPUT; *status = LZMA_STATUS_NEEDS_MORE_INPUT;
return SZ_OK; return SZ_OK;
} }
(*srcLen)++;
p->state = Lzma2Dec_UpdateState(p, *src++);
if (p->state == LZMA2_STATE_DATA) if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED)
{ {
Bool initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC); p->state = LZMA2_STATE_ERROR;
if (initDic) return SZ_ERROR_DATA;
p->needInitProp = p->needInitState = True; }
else if (p->needInitDic) continue;
{ }
p->state = LZMA2_STATE_ERROR;
return SZ_ERROR_DATA;
}
p->needInitDic = False;
LzmaDec_InitDicAndState(&p->decoder, initDic, False);
}
if (srcSizeCur > destSizeCur) {
srcSizeCur = destSizeCur; SizeT destSizeCur = dicLimit - dicPos;
SizeT srcSizeCur = inSize - *srcLen;
ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
if (srcSizeCur == 0) if (p->unpackSize <= destSizeCur)
{ {
p->state = LZMA2_STATE_ERROR; destSizeCur = (SizeT)p->unpackSize;
return SZ_ERROR_DATA; curFinishMode = LZMA_FINISH_END;
} }
LzmaDec_UpdateWithUncompressed(&p->decoder, src, srcSizeCur); if (LZMA2_IS_UNCOMPRESSED_STATE(p))
{
if (*srcLen == inSize)
{
*status = LZMA_STATUS_NEEDS_MORE_INPUT;
return SZ_OK;
}
src += srcSizeCur; if (p->state == LZMA2_STATE_DATA)
*srcLen += srcSizeCur; {
p->unpackSize -= (UInt32)srcSizeCur; Bool initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC);
p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT; if (initDic)
} p->needInitProp = p->needInitState = True;
else else if (p->needInitDic)
{ {
SizeT outSizeProcessed; p->state = LZMA2_STATE_ERROR;
SRes res; return SZ_ERROR_DATA;
}
p->needInitDic = False;
LzmaDec_InitDicAndState(&p->decoder, initDic, False);
}
if (p->state == LZMA2_STATE_DATA) if (srcSizeCur > destSizeCur)
{ srcSizeCur = destSizeCur;
unsigned mode = LZMA2_GET_LZMA_MODE(p);
Bool initDic = (mode == 3);
Bool initState = (mode != 0);
if ((!initDic && p->needInitDic) || (!initState && p->needInitState))
{
p->state = LZMA2_STATE_ERROR;
return SZ_ERROR_DATA;
}
LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
p->needInitDic = False;
p->needInitState = False;
p->state = LZMA2_STATE_DATA_CONT;
}
if (srcSizeCur > p->packSize)
srcSizeCur = (SizeT)p->packSize;
res = LzmaDec_DecodeToDic(&p->decoder, dicPos + destSizeCur, src, &srcSizeCur, curFinishMode, status);
src += srcSizeCur;
*srcLen += srcSizeCur;
p->packSize -= (UInt32)srcSizeCur;
outSizeProcessed = p->decoder.dicPos - dicPos; if (srcSizeCur == 0)
p->unpackSize -= (UInt32)outSizeProcessed; {
p->state = LZMA2_STATE_ERROR;
return SZ_ERROR_DATA;
}
RINOK(res); LzmaDec_UpdateWithUncompressed(&p->decoder, src, srcSizeCur);
if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
return res;
if (srcSizeCur == 0 && outSizeProcessed == 0) src += srcSizeCur;
{ *srcLen += srcSizeCur;
if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK p->unpackSize -= (UInt32)srcSizeCur;
|| p->unpackSize != 0 p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
|| p->packSize != 0) }
{ else
p->state = LZMA2_STATE_ERROR; {
return SZ_ERROR_DATA; SizeT outSizeProcessed;
} SRes res;
p->state = LZMA2_STATE_CONTROL;
} if (p->state == LZMA2_STATE_DATA)
{
if (*status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK) unsigned mode = LZMA2_GET_LZMA_MODE(p);
*status = LZMA_STATUS_NOT_FINISHED; Bool initDic = (mode == 3);
} Bool initState = (mode != 0);
} if ((!initDic && p->needInitDic) || (!initState && p->needInitState))
} {
p->state = LZMA2_STATE_ERROR;
*status = LZMA_STATUS_FINISHED_WITH_MARK; return SZ_ERROR_DATA;
return SZ_OK; }
LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
p->needInitDic = False;
p->needInitState = False;
p->state = LZMA2_STATE_DATA_CONT;
}
if (srcSizeCur > p->packSize)
srcSizeCur = (SizeT)p->packSize;
res = LzmaDec_DecodeToDic(&p->decoder, dicPos + destSizeCur, src, &srcSizeCur, curFinishMode, status);
src += srcSizeCur;
*srcLen += srcSizeCur;
p->packSize -= (UInt32)srcSizeCur;
outSizeProcessed = p->decoder.dicPos - dicPos;
p->unpackSize -= (UInt32)outSizeProcessed;
RINOK(res);
if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
return res;
if (srcSizeCur == 0 && outSizeProcessed == 0)
{
if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
|| p->unpackSize != 0
|| p->packSize != 0)
{
p->state = LZMA2_STATE_ERROR;
return SZ_ERROR_DATA;
}
p->state = LZMA2_STATE_CONTROL;
}
if (*status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
*status = LZMA_STATUS_NOT_FINISHED;
}
}
}
*status = LZMA_STATUS_FINISHED_WITH_MARK;
return SZ_OK;
} }
SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status) SRes Lzma2Dec_DecodeToBuf(CLzma2Dec* p, Byte* dest, SizeT* destLen, const Byte* src, SizeT* srcLen,
ELzmaFinishMode finishMode, ELzmaStatus* status)
{ {
SizeT outSize = *destLen, inSize = *srcLen; SizeT outSize = *destLen, inSize = *srcLen;
*srcLen = *destLen = 0; *srcLen = *destLen = 0;
for (;;) for (;;)
{ {
SizeT srcSizeCur = inSize, outSizeCur, dicPos; SizeT srcSizeCur = inSize, outSizeCur, dicPos;
ELzmaFinishMode curFinishMode; ELzmaFinishMode curFinishMode;
SRes res; SRes res;
if (p->decoder.dicPos == p->decoder.dicBufSize) if (p->decoder.dicPos == p->decoder.dicBufSize)
p->decoder.dicPos = 0; p->decoder.dicPos = 0;
dicPos = p->decoder.dicPos; dicPos = p->decoder.dicPos;
if (outSize > p->decoder.dicBufSize - dicPos) if (outSize > p->decoder.dicBufSize - dicPos)
{ {
outSizeCur = p->decoder.dicBufSize; outSizeCur = p->decoder.dicBufSize;
curFinishMode = LZMA_FINISH_ANY; curFinishMode = LZMA_FINISH_ANY;
} }
else else
{ {
outSizeCur = dicPos + outSize; outSizeCur = dicPos + outSize;
curFinishMode = finishMode; curFinishMode = finishMode;
} }
res = Lzma2Dec_DecodeToDic(p, outSizeCur, src, &srcSizeCur, curFinishMode, status); res = Lzma2Dec_DecodeToDic(p, outSizeCur, src, &srcSizeCur, curFinishMode, status);
src += srcSizeCur; src += srcSizeCur;
inSize -= srcSizeCur; inSize -= srcSizeCur;
*srcLen += srcSizeCur; *srcLen += srcSizeCur;
outSizeCur = p->decoder.dicPos - dicPos; outSizeCur = p->decoder.dicPos - dicPos;
memcpy(dest, p->decoder.dic + dicPos, outSizeCur); memcpy(dest, p->decoder.dic + dicPos, outSizeCur);
dest += outSizeCur; dest += outSizeCur;
outSize -= outSizeCur; outSize -= outSizeCur;
*destLen += outSizeCur; *destLen += outSizeCur;
if (res != 0) if (res != 0)
return res; return res;
if (outSizeCur == 0 || outSize == 0) if (outSizeCur == 0 || outSize == 0)
return SZ_OK; return SZ_OK;
} }
} }
SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, SRes Lzma2Decode(Byte* dest, SizeT* destLen, const Byte* src, SizeT* srcLen,
Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc) Byte prop, ELzmaFinishMode finishMode, ELzmaStatus* status, ISzAlloc* alloc)
{ {
CLzma2Dec p; CLzma2Dec p;
SRes res; SRes res;
SizeT outSize = *destLen, inSize = *srcLen; SizeT outSize = *destLen, inSize = *srcLen;
*destLen = *srcLen = 0; *destLen = *srcLen = 0;
*status = LZMA_STATUS_NOT_SPECIFIED; *status = LZMA_STATUS_NOT_SPECIFIED;
Lzma2Dec_Construct(&p); Lzma2Dec_Construct(&p);
RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc)); RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc));
p.decoder.dic = dest; p.decoder.dic = dest;
p.decoder.dicBufSize = outSize; p.decoder.dicBufSize = outSize;
Lzma2Dec_Init(&p); Lzma2Dec_Init(&p);
*srcLen = inSize; *srcLen = inSize;
res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status); res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
*destLen = p.decoder.dicPos; *destLen = p.decoder.dicPos;
if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT) if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
res = SZ_ERROR_INPUT_EOF; res = SZ_ERROR_INPUT_EOF;
Lzma2Dec_FreeProbs(&p, alloc); Lzma2Dec_FreeProbs(&p, alloc);
return res; return res;
} }

View file

@ -12,23 +12,23 @@ EXTERN_C_BEGIN
typedef struct typedef struct
{ {
CLzmaDec decoder; CLzmaDec decoder;
UInt32 packSize; UInt32 packSize;
UInt32 unpackSize; UInt32 unpackSize;
unsigned state; unsigned state;
Byte control; Byte control;
Bool needInitDic; Bool needInitDic;
Bool needInitState; Bool needInitState;
Bool needInitProp; Bool needInitProp;
} CLzma2Dec; } CLzma2Dec;
#define Lzma2Dec_Construct(p) LzmaDec_Construct(&(p)->decoder) #define Lzma2Dec_Construct(p) LzmaDec_Construct(&(p)->decoder)
#define Lzma2Dec_FreeProbs(p, alloc) LzmaDec_FreeProbs(&(p)->decoder, alloc); #define Lzma2Dec_FreeProbs(p, alloc) LzmaDec_FreeProbs(&(p)->decoder, alloc);
#define Lzma2Dec_Free(p, alloc) LzmaDec_Free(&(p)->decoder, alloc); #define Lzma2Dec_Free(p, alloc) LzmaDec_Free(&(p)->decoder, alloc);
SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAlloc *alloc); SRes Lzma2Dec_AllocateProbs(CLzma2Dec* p, Byte prop, ISzAlloc* alloc);
SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAlloc *alloc); SRes Lzma2Dec_Allocate(CLzma2Dec* p, Byte prop, ISzAlloc* alloc);
void Lzma2Dec_Init(CLzma2Dec *p); void Lzma2Dec_Init(CLzma2Dec* p);
/* /*
@ -46,11 +46,11 @@ Returns:
SZ_ERROR_DATA - Data error SZ_ERROR_DATA - Data error
*/ */
SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit, SRes Lzma2Dec_DecodeToDic(CLzma2Dec* p, SizeT dicLimit,
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status);
SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, SRes Lzma2Dec_DecodeToBuf(CLzma2Dec* p, Byte* dest, SizeT* destLen,
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status);
/* ---------- One Call Interface ---------- */ /* ---------- One Call Interface ---------- */
@ -72,8 +72,8 @@ Returns:
SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
*/ */
SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, SRes Lzma2Decode(Byte* dest, SizeT* destLen, const Byte* src, SizeT* srcLen,
Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc); Byte prop, ELzmaFinishMode finishMode, ELzmaStatus* status, ISzAlloc* alloc);
EXTERN_C_END EXTERN_C_END

File diff suppressed because it is too large Load diff

View file

@ -25,8 +25,8 @@ EXTERN_C_BEGIN
typedef struct _CLzmaProps typedef struct _CLzmaProps
{ {
unsigned lc, lp, pb; unsigned lc, lp, pb;
UInt32 dicSize; UInt32 dicSize;
} CLzmaProps; } CLzmaProps;
/* LzmaProps_Decode - decodes properties /* LzmaProps_Decode - decodes properties
@ -35,7 +35,7 @@ Returns:
SZ_ERROR_UNSUPPORTED - Unsupported properties SZ_ERROR_UNSUPPORTED - Unsupported properties
*/ */
SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size); SRes LzmaProps_Decode(CLzmaProps* p, const Byte* data, unsigned size);
/* ---------- LZMA Decoder state ---------- */ /* ---------- LZMA Decoder state ---------- */
@ -47,28 +47,28 @@ SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
typedef struct typedef struct
{ {
CLzmaProps prop; CLzmaProps prop;
CLzmaProb *probs; CLzmaProb* probs;
Byte *dic; Byte* dic;
const Byte *buf; const Byte* buf;
UInt32 range, code; UInt32 range, code;
SizeT dicPos; SizeT dicPos;
SizeT dicBufSize; SizeT dicBufSize;
UInt32 processedPos; UInt32 processedPos;
UInt32 checkDicSize; UInt32 checkDicSize;
unsigned state; unsigned state;
UInt32 reps[4]; UInt32 reps[4];
unsigned remainLen; unsigned remainLen;
int needFlush; int needFlush;
int needInitState; int needInitState;
UInt32 numProbs; UInt32 numProbs;
unsigned tempBufSize; unsigned tempBufSize;
Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
} CLzmaDec; } CLzmaDec;
#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; } #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
void LzmaDec_Init(CLzmaDec *p); void LzmaDec_Init(CLzmaDec* p);
/* There are two types of LZMA streams: /* There are two types of LZMA streams:
0) Stream with end mark. That end mark adds about 6 bytes to compressed size. 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
@ -76,8 +76,9 @@ void LzmaDec_Init(CLzmaDec *p);
typedef enum typedef enum
{ {
LZMA_FINISH_ANY, /* finish at any point */ LZMA_FINISH_ANY,
LZMA_FINISH_END /* block must be finished at the end */ /* finish at any point */
LZMA_FINISH_END /* block must be finished at the end */
} ELzmaFinishMode; } ELzmaFinishMode;
/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
@ -97,11 +98,15 @@ typedef enum
typedef enum typedef enum
{ {
LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ LZMA_STATUS_NOT_SPECIFIED,
LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ /* use main error code instead */
LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ LZMA_STATUS_FINISHED_WITH_MARK,
LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */ /* stream was finished with end mark. */
LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */ LZMA_STATUS_NOT_FINISHED,
/* stream was not finished */
LZMA_STATUS_NEEDS_MORE_INPUT,
/* you must provide more input bytes */
LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
} ELzmaStatus; } ELzmaStatus;
/* ELzmaStatus is used only as output value for function call */ /* ELzmaStatus is used only as output value for function call */
@ -128,12 +133,12 @@ LzmaDec_Allocate* can return:
SZ_ERROR_MEM - Memory allocation error SZ_ERROR_MEM - Memory allocation error
SZ_ERROR_UNSUPPORTED - Unsupported properties SZ_ERROR_UNSUPPORTED - Unsupported properties
*/ */
SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc); SRes LzmaDec_AllocateProbs(CLzmaDec* p, const Byte* props, unsigned propsSize, ISzAlloc* alloc);
void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc); void LzmaDec_FreeProbs(CLzmaDec* p, ISzAlloc* alloc);
SRes LzmaDec_Allocate(CLzmaDec* state, const Byte* prop, unsigned propsSize, ISzAlloc* alloc);
void LzmaDec_Free(CLzmaDec* state, ISzAlloc* alloc);
/* ---------- Dictionary Interface ---------- */ /* ---------- Dictionary Interface ---------- */
@ -176,8 +181,8 @@ Returns:
SZ_ERROR_DATA - Data error SZ_ERROR_DATA - Data error
*/ */
SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, SRes LzmaDec_DecodeToDic(CLzmaDec* p, SizeT dicLimit,
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status);
/* ---------- Buffer Interface ---------- */ /* ---------- Buffer Interface ---------- */
@ -193,8 +198,8 @@ finishMode:
LZMA_FINISH_END - Stream must be finished after (*destLen). LZMA_FINISH_END - Stream must be finished after (*destLen).
*/ */
SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, SRes LzmaDec_DecodeToBuf(CLzmaDec* p, Byte* dest, SizeT* destLen,
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status);
/* ---------- One Call Interface ---------- */ /* ---------- One Call Interface ---------- */
@ -218,9 +223,9 @@ Returns:
SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
*/ */
SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, SRes LzmaDecode(Byte* dest, SizeT* destLen, const Byte* src, SizeT* srcLen,
const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, const Byte* propData, unsigned propSize, ELzmaFinishMode finishMode,
ELzmaStatus *status, ISzAlloc *alloc); ELzmaStatus* status, ISzAlloc* alloc);
EXTERN_C_END EXTERN_C_END

View file

@ -34,9 +34,9 @@ EXTERN_C_BEGIN
/* SEE-contexts for PPM-contexts with masked symbols */ /* SEE-contexts for PPM-contexts with masked symbols */
typedef struct typedef struct
{ {
UInt16 Summ; /* Freq */ UInt16 Summ; /* Freq */
Byte Shift; /* Speed of Freq change; low Shift is for fast change */ Byte Shift; /* Speed of Freq change; low Shift is for fast change */
Byte Count; /* Count to next change of Shift */ Byte Count; /* Count to next change of Shift */
} CPpmd_See; } CPpmd_See;
#define Ppmd_See_Update(p) if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \ #define Ppmd_See_Update(p) if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \
@ -44,42 +44,42 @@ typedef struct
typedef struct typedef struct
{ {
Byte Symbol; Byte Symbol;
Byte Freq; Byte Freq;
UInt16 SuccessorLow; UInt16 SuccessorLow;
UInt16 SuccessorHigh; UInt16 SuccessorHigh;
} CPpmd_State; } CPpmd_State;
#pragma pack(pop) #pragma pack(pop)
typedef typedef
#ifdef PPMD_32BIT #ifdef PPMD_32BIT
CPpmd_State * CPpmd_State *
#else #else
UInt32 UInt32
#endif #endif
CPpmd_State_Ref; CPpmd_State_Ref;
typedef typedef
#ifdef PPMD_32BIT #ifdef PPMD_32BIT
void * void *
#else #else
UInt32 UInt32
#endif #endif
CPpmd_Void_Ref; CPpmd_Void_Ref;
typedef typedef
#ifdef PPMD_32BIT #ifdef PPMD_32BIT
Byte * Byte *
#else #else
UInt32 UInt32
#endif #endif
CPpmd_Byte_Ref; CPpmd_Byte_Ref;
#define PPMD_SetAllBitsIn256Bytes(p) \ #define PPMD_SetAllBitsIn256Bytes(p) \
{ unsigned z; for (z = 0; z < 256 / sizeof(p[0]); z += 8) { \ { unsigned z; for (z = 0; z < 256 / sizeof(p[0]); z += 8) { \
p[z+7] = p[z+6] = p[z+5] = p[z+4] = p[z+3] = p[z+2] = p[z+1] = p[z+0] = ~(size_t)0; }} p[z+7] = p[z+6] = p[z+5] = p[z+4] = p[z+3] = p[z+2] = p[z+1] = p[z+0] = ~(size_t)0; }}
EXTERN_C_END EXTERN_C_END
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -22,47 +22,47 @@ EXTERN_C_BEGIN
struct CPpmd7_Context_; struct CPpmd7_Context_;
typedef typedef
#ifdef PPMD_32BIT #ifdef PPMD_32BIT
struct CPpmd7_Context_ * struct CPpmd7_Context_ *
#else #else
UInt32 UInt32
#endif #endif
CPpmd7_Context_Ref; CPpmd7_Context_Ref;
typedef struct CPpmd7_Context_ typedef struct CPpmd7_Context_
{ {
UInt16 NumStats; UInt16 NumStats;
UInt16 SummFreq; UInt16 SummFreq;
CPpmd_State_Ref Stats; CPpmd_State_Ref Stats;
CPpmd7_Context_Ref Suffix; CPpmd7_Context_Ref Suffix;
} CPpmd7_Context; } CPpmd7_Context;
#define Ppmd7Context_OneState(p) ((CPpmd_State *)&(p)->SummFreq) #define Ppmd7Context_OneState(p) ((CPpmd_State *)&(p)->SummFreq)
typedef struct typedef struct
{ {
CPpmd7_Context *MinContext, *MaxContext; CPpmd7_Context *MinContext, *MaxContext;
CPpmd_State *FoundState; CPpmd_State* FoundState;
unsigned OrderFall, InitEsc, PrevSuccess, MaxOrder, HiBitsFlag; unsigned OrderFall, InitEsc, PrevSuccess, MaxOrder, HiBitsFlag;
Int32 RunLength, InitRL; /* must be 32-bit at least */ Int32 RunLength, InitRL; /* must be 32-bit at least */
UInt32 Size; UInt32 Size;
UInt32 GlueCount; UInt32 GlueCount;
Byte *Base, *LoUnit, *HiUnit, *Text, *UnitsStart; Byte *Base, *LoUnit, *HiUnit, *Text, *UnitsStart;
UInt32 AlignOffset; UInt32 AlignOffset;
Byte Indx2Units[PPMD_NUM_INDEXES]; Byte Indx2Units[PPMD_NUM_INDEXES];
Byte Units2Indx[128]; Byte Units2Indx[128];
CPpmd_Void_Ref FreeList[PPMD_NUM_INDEXES]; CPpmd_Void_Ref FreeList[PPMD_NUM_INDEXES];
Byte NS2Indx[256], NS2BSIndx[256], HB2Flag[256]; Byte NS2Indx[256], NS2BSIndx[256], HB2Flag[256];
CPpmd_See DummySee, See[25][16]; CPpmd_See DummySee, See[25][16];
UInt16 BinSumm[128][64]; UInt16 BinSumm[128][64];
} CPpmd7; } CPpmd7;
void Ppmd7_Construct(CPpmd7 *p); void Ppmd7_Construct(CPpmd7* p);
Bool Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAlloc *alloc); Bool Ppmd7_Alloc(CPpmd7* p, UInt32 size, ISzAlloc* alloc);
void Ppmd7_Free(CPpmd7 *p, ISzAlloc *alloc); void Ppmd7_Free(CPpmd7* p, ISzAlloc* alloc);
void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder); void Ppmd7_Init(CPpmd7* p, unsigned maxOrder);
#define Ppmd7_WasAllocated(p) ((p)->Base != NULL) #define Ppmd7_WasAllocated(p) ((p)->Base != NULL)
@ -75,15 +75,15 @@ extern const Byte PPMD7_kExpEscape[16];
#define Ppmd7_GetContext(p, ptr) (ptr) #define Ppmd7_GetContext(p, ptr) (ptr)
#define Ppmd7_GetStats(p, ctx) ((ctx)->Stats) #define Ppmd7_GetStats(p, ctx) ((ctx)->Stats)
#else #else
#define Ppmd7_GetPtr(p, offs) ((void *)((p)->Base + (offs))) #define Ppmd7_GetPtr(p, offs) ((void *)((p)->Base + (offs)))
#define Ppmd7_GetContext(p, offs) ((CPpmd7_Context *)Ppmd7_GetPtr((p), (offs))) #define Ppmd7_GetContext(p, offs) ((CPpmd7_Context *)Ppmd7_GetPtr((p), (offs)))
#define Ppmd7_GetStats(p, ctx) ((CPpmd_State *)Ppmd7_GetPtr((p), ((ctx)->Stats))) #define Ppmd7_GetStats(p, ctx) ((CPpmd_State *)Ppmd7_GetPtr((p), ((ctx)->Stats)))
#endif #endif
void Ppmd7_Update1(CPpmd7 *p); void Ppmd7_Update1(CPpmd7* p);
void Ppmd7_Update1_0(CPpmd7 *p); void Ppmd7_Update1_0(CPpmd7* p);
void Ppmd7_Update2(CPpmd7 *p); void Ppmd7_Update2(CPpmd7* p);
void Ppmd7_UpdateBin(CPpmd7 *p); void Ppmd7_UpdateBin(CPpmd7* p);
#define Ppmd7_GetBinSumm(p) \ #define Ppmd7_GetBinSumm(p) \
&p->BinSumm[(unsigned)Ppmd7Context_OneState(p->MinContext)->Freq - 1][p->PrevSuccess + \ &p->BinSumm[(unsigned)Ppmd7Context_OneState(p->MinContext)->Freq - 1][p->PrevSuccess + \
@ -92,49 +92,49 @@ void Ppmd7_UpdateBin(CPpmd7 *p);
2 * p->HB2Flag[(unsigned)Ppmd7Context_OneState(p->MinContext)->Symbol] + \ 2 * p->HB2Flag[(unsigned)Ppmd7Context_OneState(p->MinContext)->Symbol] + \
((p->RunLength >> 26) & 0x20)] ((p->RunLength >> 26) & 0x20)]
CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *scale); CPpmd_See* Ppmd7_MakeEscFreq(CPpmd7* p, unsigned numMasked, UInt32* scale);
/* ---------- Decode ---------- */ /* ---------- Decode ---------- */
typedef struct typedef struct
{ {
UInt32 (*GetThreshold)(void *p, UInt32 total); UInt32 (*GetThreshold)(void* p, UInt32 total);
void (*Decode)(void *p, UInt32 start, UInt32 size); void (*Decode)(void* p, UInt32 start, UInt32 size);
UInt32 (*DecodeBit)(void *p, UInt32 size0); UInt32 (*DecodeBit)(void* p, UInt32 size0);
} IPpmd7_RangeDec; } IPpmd7_RangeDec;
typedef struct typedef struct
{ {
IPpmd7_RangeDec p; IPpmd7_RangeDec p;
UInt32 Range; UInt32 Range;
UInt32 Code; UInt32 Code;
IByteIn *Stream; IByteIn* Stream;
} CPpmd7z_RangeDec; } CPpmd7z_RangeDec;
void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec *p); void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec* p);
Bool Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec *p); Bool Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec* p);
#define Ppmd7z_RangeDec_IsFinishedOK(p) ((p)->Code == 0) #define Ppmd7z_RangeDec_IsFinishedOK(p) ((p)->Code == 0)
int Ppmd7_DecodeSymbol(CPpmd7 *p, IPpmd7_RangeDec *rc); int Ppmd7_DecodeSymbol(CPpmd7* p, IPpmd7_RangeDec* rc);
/* ---------- Encode ---------- */ /* ---------- Encode ---------- */
typedef struct typedef struct
{ {
UInt64 Low; UInt64 Low;
UInt32 Range; UInt32 Range;
Byte Cache; Byte Cache;
UInt64 CacheSize; UInt64 CacheSize;
IByteOut *Stream; IByteOut* Stream;
} CPpmd7z_RangeEnc; } CPpmd7z_RangeEnc;
void Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc *p); void Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc* p);
void Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc *p); void Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc* p);
void Ppmd7_EncodeSymbol(CPpmd7 *p, CPpmd7z_RangeEnc *rc, int symbol); void Ppmd7_EncodeSymbol(CPpmd7* p, CPpmd7z_RangeEnc* rc, int symbol);
EXTERN_C_END EXTERN_C_END
#endif #endif

View file

@ -8,182 +8,184 @@ This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
#define kTopValue (1 << 24) #define kTopValue (1 << 24)
Bool Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec *p) Bool Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec* p)
{ {
unsigned i; unsigned i;
p->Code = 0; p->Code = 0;
p->Range = 0xFFFFFFFF; p->Range = 0xFFFFFFFF;
if (p->Stream->Read((void *)p->Stream) != 0) if (p->Stream->Read((void*)p->Stream) != 0)
return False; return False;
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
p->Code = (p->Code << 8) | p->Stream->Read((void *)p->Stream); p->Code = (p->Code << 8) | p->Stream->Read((void*)p->Stream);
return (p->Code < 0xFFFFFFFF); return (p->Code < 0xFFFFFFFF);
} }
static UInt32 Range_GetThreshold(void *pp, UInt32 total) static UInt32 Range_GetThreshold(void* pp, UInt32 total)
{ {
CPpmd7z_RangeDec *p = (CPpmd7z_RangeDec *)pp; CPpmd7z_RangeDec* p = (CPpmd7z_RangeDec*)pp;
return (p->Code) / (p->Range /= total); return (p->Code) / (p->Range /= total);
} }
static void Range_Normalize(CPpmd7z_RangeDec *p) static void Range_Normalize(CPpmd7z_RangeDec* p)
{ {
if (p->Range < kTopValue) if (p->Range < kTopValue)
{ {
p->Code = (p->Code << 8) | p->Stream->Read((void *)p->Stream); p->Code = (p->Code << 8) | p->Stream->Read((void*)p->Stream);
p->Range <<= 8; p->Range <<= 8;
if (p->Range < kTopValue) if (p->Range < kTopValue)
{ {
p->Code = (p->Code << 8) | p->Stream->Read((void *)p->Stream); p->Code = (p->Code << 8) | p->Stream->Read((void*)p->Stream);
p->Range <<= 8; p->Range <<= 8;
} }
} }
} }
static void Range_Decode(void *pp, UInt32 start, UInt32 size) static void Range_Decode(void* pp, UInt32 start, UInt32 size)
{ {
CPpmd7z_RangeDec *p = (CPpmd7z_RangeDec *)pp; CPpmd7z_RangeDec* p = (CPpmd7z_RangeDec*)pp;
p->Code -= start * p->Range; p->Code -= start * p->Range;
p->Range *= size; p->Range *= size;
Range_Normalize(p); Range_Normalize(p);
} }
static UInt32 Range_DecodeBit(void *pp, UInt32 size0) static UInt32 Range_DecodeBit(void* pp, UInt32 size0)
{ {
CPpmd7z_RangeDec *p = (CPpmd7z_RangeDec *)pp; CPpmd7z_RangeDec* p = (CPpmd7z_RangeDec*)pp;
UInt32 newBound = (p->Range >> 14) * size0; UInt32 newBound = (p->Range >> 14) * size0;
UInt32 symbol; UInt32 symbol;
if (p->Code < newBound) if (p->Code < newBound)
{ {
symbol = 0; symbol = 0;
p->Range = newBound; p->Range = newBound;
} }
else else
{ {
symbol = 1; symbol = 1;
p->Code -= newBound; p->Code -= newBound;
p->Range -= newBound; p->Range -= newBound;
} }
Range_Normalize(p); Range_Normalize(p);
return symbol; return symbol;
} }
void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec *p) void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec* p)
{ {
p->p.GetThreshold = Range_GetThreshold; p->p.GetThreshold = Range_GetThreshold;
p->p.Decode = Range_Decode; p->p.Decode = Range_Decode;
p->p.DecodeBit = Range_DecodeBit; p->p.DecodeBit = Range_DecodeBit;
} }
#define MASK(sym) ((signed char *)charMask)[sym] #define MASK(sym) ((signed char *)charMask)[sym]
int Ppmd7_DecodeSymbol(CPpmd7 *p, IPpmd7_RangeDec *rc) int Ppmd7_DecodeSymbol(CPpmd7* p, IPpmd7_RangeDec* rc)
{ {
size_t charMask[256 / sizeof(size_t)]; size_t charMask[256 / sizeof(size_t)];
if (p->MinContext->NumStats != 1) if (p->MinContext->NumStats != 1)
{ {
CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext); CPpmd_State* s = Ppmd7_GetStats(p, p->MinContext);
unsigned i; unsigned i;
UInt32 count, hiCnt; UInt32 count, hiCnt;
if ((count = rc->GetThreshold(rc, p->MinContext->SummFreq)) < (hiCnt = s->Freq)) if ((count = rc->GetThreshold(rc, p->MinContext->SummFreq)) < (hiCnt = s->Freq))
{ {
Byte symbol; Byte symbol;
rc->Decode(rc, 0, s->Freq); rc->Decode(rc, 0, s->Freq);
p->FoundState = s; p->FoundState = s;
symbol = s->Symbol; symbol = s->Symbol;
Ppmd7_Update1_0(p); Ppmd7_Update1_0(p);
return symbol; return symbol;
} }
p->PrevSuccess = 0; p->PrevSuccess = 0;
i = p->MinContext->NumStats - 1; i = p->MinContext->NumStats - 1;
do do
{ {
if ((hiCnt += (++s)->Freq) > count) if ((hiCnt += (++s)->Freq) > count)
{ {
Byte symbol; Byte symbol;
rc->Decode(rc, hiCnt - s->Freq, s->Freq); rc->Decode(rc, hiCnt - s->Freq, s->Freq);
p->FoundState = s; p->FoundState = s;
symbol = s->Symbol; symbol = s->Symbol;
Ppmd7_Update1(p); Ppmd7_Update1(p);
return symbol; return symbol;
} }
} }
while (--i); while (--i);
if (count >= p->MinContext->SummFreq) if (count >= p->MinContext->SummFreq)
return -2; return -2;
p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol]; p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol];
rc->Decode(rc, hiCnt, p->MinContext->SummFreq - hiCnt); rc->Decode(rc, hiCnt, p->MinContext->SummFreq - hiCnt);
PPMD_SetAllBitsIn256Bytes(charMask); PPMD_SetAllBitsIn256Bytes(charMask);
MASK(s->Symbol) = 0; MASK(s->Symbol) = 0;
i = p->MinContext->NumStats - 1; i = p->MinContext->NumStats - 1;
do { MASK((--s)->Symbol) = 0; } while (--i); do { MASK((--s)->Symbol) = 0; }
} while (--i);
else }
{ else
UInt16 *prob = Ppmd7_GetBinSumm(p); {
if (rc->DecodeBit(rc, *prob) == 0) UInt16* prob = Ppmd7_GetBinSumm(p);
{ if (rc->DecodeBit(rc, *prob) == 0)
Byte symbol; {
*prob = (UInt16)PPMD_UPDATE_PROB_0(*prob); Byte symbol;
symbol = (p->FoundState = Ppmd7Context_OneState(p->MinContext))->Symbol; *prob = (UInt16)PPMD_UPDATE_PROB_0(*prob);
Ppmd7_UpdateBin(p); symbol = (p->FoundState = Ppmd7Context_OneState(p->MinContext))->Symbol;
return symbol; Ppmd7_UpdateBin(p);
} return symbol;
*prob = (UInt16)PPMD_UPDATE_PROB_1(*prob); }
p->InitEsc = PPMD7_kExpEscape[*prob >> 10]; *prob = (UInt16)PPMD_UPDATE_PROB_1(*prob);
PPMD_SetAllBitsIn256Bytes(charMask); p->InitEsc = PPMD7_kExpEscape[*prob >> 10];
MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0; PPMD_SetAllBitsIn256Bytes(charMask);
p->PrevSuccess = 0; MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0;
} p->PrevSuccess = 0;
for (;;) }
{ for (;;)
CPpmd_State *ps[256], *s; {
UInt32 freqSum, count, hiCnt; CPpmd_State *ps[256], *s;
CPpmd_See *see; UInt32 freqSum, count, hiCnt;
unsigned i, num, numMasked = p->MinContext->NumStats; CPpmd_See* see;
do unsigned i, num, numMasked = p->MinContext->NumStats;
{ do
p->OrderFall++; {
if (!p->MinContext->Suffix) p->OrderFall++;
return -1; if (!p->MinContext->Suffix)
p->MinContext = Ppmd7_GetContext(p, p->MinContext->Suffix); return -1;
} p->MinContext = Ppmd7_GetContext(p, p->MinContext->Suffix);
while (p->MinContext->NumStats == numMasked); }
hiCnt = 0; while (p->MinContext->NumStats == numMasked);
s = Ppmd7_GetStats(p, p->MinContext); hiCnt = 0;
i = 0; s = Ppmd7_GetStats(p, p->MinContext);
num = p->MinContext->NumStats - numMasked; i = 0;
do num = p->MinContext->NumStats - numMasked;
{ do
int k = (int)(MASK(s->Symbol)); {
hiCnt += (s->Freq & k); int k = (int)(MASK(s->Symbol));
ps[i] = s++; hiCnt += (s->Freq & k);
i -= k; ps[i] = s++;
} i -= k;
while (i != num); }
while (i != num);
see = Ppmd7_MakeEscFreq(p, numMasked, &freqSum);
freqSum += hiCnt; see = Ppmd7_MakeEscFreq(p, numMasked, &freqSum);
count = rc->GetThreshold(rc, freqSum); freqSum += hiCnt;
count = rc->GetThreshold(rc, freqSum);
if (count < hiCnt)
{ if (count < hiCnt)
Byte symbol; {
CPpmd_State **pps = ps; Byte symbol;
for (hiCnt = 0; (hiCnt += (*pps)->Freq) <= count; pps++); CPpmd_State** pps = ps;
s = *pps; for (hiCnt = 0; (hiCnt += (*pps)->Freq) <= count; pps++);
rc->Decode(rc, hiCnt - s->Freq, s->Freq); s = *pps;
Ppmd_See_Update(see); rc->Decode(rc, hiCnt - s->Freq, s->Freq);
p->FoundState = s; Ppmd_See_Update(see);
symbol = s->Symbol; p->FoundState = s;
Ppmd7_Update2(p); symbol = s->Symbol;
return symbol; Ppmd7_Update2(p);
} return symbol;
if (count >= freqSum) }
return -2; if (count >= freqSum)
rc->Decode(rc, hiCnt, freqSum - hiCnt); return -2;
see->Summ = (UInt16)(see->Summ + freqSum); rc->Decode(rc, hiCnt, freqSum - hiCnt);
do { MASK(ps[--i]->Symbol) = 0; } while (i != 0); see->Summ = (UInt16)(see->Summ + freqSum);
} do { MASK(ps[--i]->Symbol) = 0; }
while (i != 0);
}
} }