📄 7zin.c
字号:
/* 7zIn.c -- 7z Input functions
2008-12-31 : Igor Pavlov : Public domain */
#include "../../7zCrc.h"
#include "../../CpuArch.h"
#include "7zDecode.h"
#include "7zIn.h"
#define RINOM(x) { if ((x) == 0) return SZ_ERROR_MEM; }
#define NUM_FOLDER_CODERS_MAX 32
#define NUM_CODER_STREAMS_MAX 32
void SzArEx_Init(CSzArEx *p)
{
SzAr_Init(&p->db);
p->FolderStartPackStreamIndex = 0;
p->PackStreamStartPositions = 0;
p->FolderStartFileIndex = 0;
p->FileIndexToFolderIndexMap = 0;
}
void SzArEx_Free(CSzArEx *p, ISzAlloc *alloc)
{
IAlloc_Free(alloc, p->FolderStartPackStreamIndex);
IAlloc_Free(alloc, p->PackStreamStartPositions);
IAlloc_Free(alloc, p->FolderStartFileIndex);
IAlloc_Free(alloc, p->FileIndexToFolderIndexMap);
SzAr_Free(&p->db, alloc);
SzArEx_Init(p);
}
/*
UInt64 GetFolderPackStreamSize(int folderIndex, int streamIndex) const
{
return PackSizes[FolderStartPackStreamIndex[folderIndex] + streamIndex];
}
UInt64 GetFilePackSize(int fileIndex) const
{
int folderIndex = FileIndexToFolderIndexMap[fileIndex];
if (folderIndex >= 0)
{
const CSzFolder &folderInfo = Folders[folderIndex];
if (FolderStartFileIndex[folderIndex] == fileIndex)
return GetFolderFullPackSize(folderIndex);
}
return 0;
}
*/
#define MY_ALLOC(T, p, size, alloc) { if ((size) == 0) p = 0; else \
if ((p = (T *)IAlloc_Alloc(alloc, (size) * sizeof(T))) == 0) return SZ_ERROR_MEM; }
static SRes SzArEx_Fill(CSzArEx *p, ISzAlloc *alloc)
{
UInt32 startPos = 0;
UInt64 startPosSize = 0;
UInt32 i;
UInt32 folderIndex = 0;
UInt32 indexInFolder = 0;
MY_ALLOC(UInt32, p->FolderStartPackStreamIndex, p->db.NumFolders, alloc);
for (i = 0; i < p->db.NumFolders; i++)
{
p->FolderStartPackStreamIndex[i] = startPos;
startPos += p->db.Folders[i].NumPackStreams;
}
MY_ALLOC(UInt64, p->PackStreamStartPositions, p->db.NumPackStreams, alloc);
for (i = 0; i < p->db.NumPackStreams; i++)
{
p->PackStreamStartPositions[i] = startPosSize;
startPosSize += p->db.PackSizes[i];
}
MY_ALLOC(UInt32, p->FolderStartFileIndex, p->db.NumFolders, alloc);
MY_ALLOC(UInt32, p->FileIndexToFolderIndexMap, p->db.NumFiles, alloc);
for (i = 0; i < p->db.NumFiles; i++)
{
CSzFileItem *file = p->db.Files + i;
int emptyStream = !file->HasStream;
if (emptyStream && indexInFolder == 0)
{
p->FileIndexToFolderIndexMap[i] = (UInt32)-1;
continue;
}
if (indexInFolder == 0)
{
/*
v3.13 incorrectly worked with empty folders
v4.07: Loop for skipping empty folders
*/
for (;;)
{
if (folderIndex >= p->db.NumFolders)
return SZ_ERROR_ARCHIVE;
p->FolderStartFileIndex[folderIndex] = i;
if (p->db.Folders[folderIndex].NumUnpackStreams != 0)
break;
folderIndex++;
}
}
p->FileIndexToFolderIndexMap[i] = folderIndex;
if (emptyStream)
continue;
indexInFolder++;
if (indexInFolder >= p->db.Folders[folderIndex].NumUnpackStreams)
{
folderIndex++;
indexInFolder = 0;
}
}
return SZ_OK;
}
UInt64 SzArEx_GetFolderStreamPos(const CSzArEx *p, UInt32 folderIndex, UInt32 indexInFolder)
{
return p->dataPos +
p->PackStreamStartPositions[p->FolderStartPackStreamIndex[folderIndex] + indexInFolder];
}
int SzArEx_GetFolderFullPackSize(const CSzArEx *p, UInt32 folderIndex, UInt64 *resSize)
{
UInt32 packStreamIndex = p->FolderStartPackStreamIndex[folderIndex];
CSzFolder *folder = p->db.Folders + folderIndex;
UInt64 size = 0;
UInt32 i;
for (i = 0; i < folder->NumPackStreams; i++)
{
UInt64 t = size + p->db.PackSizes[packStreamIndex + i];
if (t < size) /* check it */
return SZ_ERROR_FAIL;
size = t;
}
*resSize = size;
return SZ_OK;
}
/*
SRes SzReadTime(const CObjectVector<CBuf> &dataVector,
CObjectVector<CSzFileItem> &files, UInt64 type)
{
CBoolVector boolVector;
RINOK(ReadBoolVector2(files.Size(), boolVector))
CStreamSwitch streamSwitch;
RINOK(streamSwitch.Set(this, &dataVector));
for (int i = 0; i < files.Size(); i++)
{
CSzFileItem &file = files[i];
CArchiveFileTime fileTime;
bool defined = boolVector[i];
if (defined)
{
UInt32 low, high;
RINOK(SzReadUInt32(low));
RINOK(SzReadUInt32(high));
fileTime.dwLowDateTime = low;
fileTime.dwHighDateTime = high;
}
switch(type)
{
case k7zIdCTime: file.IsCTimeDefined = defined; if (defined) file.CTime = fileTime; break;
case k7zIdATime: file.IsATimeDefined = defined; if (defined) file.ATime = fileTime; break;
case k7zIdMTime: file.IsMTimeDefined = defined; if (defined) file.MTime = fileTime; break;
}
}
return SZ_OK;
}
*/
static int TestSignatureCandidate(Byte *testBytes)
{
size_t i;
for (i = 0; i < k7zSignatureSize; i++)
if (testBytes[i] != k7zSignature[i])
return 0;
return 1;
}
typedef struct _CSzState
{
Byte *Data;
size_t Size;
}CSzData;
static SRes SzReadByte(CSzData *sd, Byte *b)
{
if (sd->Size == 0)
return SZ_ERROR_ARCHIVE;
sd->Size--;
*b = *sd->Data++;
return SZ_OK;
}
static SRes SzReadBytes(CSzData *sd, Byte *data, size_t size)
{
size_t i;
for (i = 0; i < size; i++)
{
RINOK(SzReadByte(sd, data + i));
}
return SZ_OK;
}
static SRes SzReadUInt32(CSzData *sd, UInt32 *value)
{
int i;
*value = 0;
for (i = 0; i < 4; i++)
{
Byte b;
RINOK(SzReadByte(sd, &b));
*value |= ((UInt32)(b) << (8 * i));
}
return SZ_OK;
}
static SRes SzReadNumber(CSzData *sd, UInt64 *value)
{
Byte firstByte;
Byte mask = 0x80;
int i;
RINOK(SzReadByte(sd, &firstByte));
*value = 0;
for (i = 0; i < 8; i++)
{
Byte b;
if ((firstByte & mask) == 0)
{
UInt64 highPart = firstByte & (mask - 1);
*value += (highPart << (8 * i));
return SZ_OK;
}
RINOK(SzReadByte(sd, &b));
*value |= ((UInt64)b << (8 * i));
mask >>= 1;
}
return SZ_OK;
}
static SRes SzReadNumber32(CSzData *sd, UInt32 *value)
{
UInt64 value64;
RINOK(SzReadNumber(sd, &value64));
if (value64 >= 0x80000000)
return SZ_ERROR_UNSUPPORTED;
if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 2)))
return SZ_ERROR_UNSUPPORTED;
*value = (UInt32)value64;
return SZ_OK;
}
static SRes SzReadID(CSzData *sd, UInt64 *value)
{
return SzReadNumber(sd, value);
}
static SRes SzSkeepDataSize(CSzData *sd, UInt64 size)
{
if (size > sd->Size)
return SZ_ERROR_ARCHIVE;
sd->Size -= (size_t)size;
sd->Data += (size_t)size;
return SZ_OK;
}
static SRes SzSkeepData(CSzData *sd)
{
UInt64 size;
RINOK(SzReadNumber(sd, &size));
return SzSkeepDataSize(sd, size);
}
static SRes SzReadArchiveProperties(CSzData *sd)
{
for (;;)
{
UInt64 type;
RINOK(SzReadID(sd, &type));
if (type == k7zIdEnd)
break;
SzSkeepData(sd);
}
return SZ_OK;
}
static SRes SzWaitAttribute(CSzData *sd, UInt64 attribute)
{
for (;;)
{
UInt64 type;
RINOK(SzReadID(sd, &type));
if (type == attribute)
return SZ_OK;
if (type == k7zIdEnd)
return SZ_ERROR_ARCHIVE;
RINOK(SzSkeepData(sd));
}
}
static SRes SzReadBoolVector(CSzData *sd, size_t numItems, Byte **v, ISzAlloc *alloc)
{
Byte b = 0;
Byte mask = 0;
size_t i;
MY_ALLOC(Byte, *v, numItems, alloc);
for (i = 0; i < numItems; i++)
{
if (mask == 0)
{
RINOK(SzReadByte(sd, &b));
mask = 0x80;
}
(*v)[i] = (Byte)(((b & mask) != 0) ? 1 : 0);
mask >>= 1;
}
return SZ_OK;
}
static SRes SzReadBoolVector2(CSzData *sd, size_t numItems, Byte **v, ISzAlloc *alloc)
{
Byte allAreDefined;
size_t i;
RINOK(SzReadByte(sd, &allAreDefined));
if (allAreDefined == 0)
return SzReadBoolVector(sd, numItems, v, alloc);
MY_ALLOC(Byte, *v, numItems, alloc);
for (i = 0; i < numItems; i++)
(*v)[i] = 1;
return SZ_OK;
}
static SRes SzReadHashDigests(
CSzData *sd,
size_t numItems,
Byte **digestsDefined,
UInt32 **digests,
ISzAlloc *alloc)
{
size_t i;
RINOK(SzReadBoolVector2(sd, numItems, digestsDefined, alloc));
MY_ALLOC(UInt32, *digests, numItems, alloc);
for (i = 0; i < numItems; i++)
if ((*digestsDefined)[i])
{
RINOK(SzReadUInt32(sd, (*digests) + i));
}
return SZ_OK;
}
static SRes SzReadPackInfo(
CSzData *sd,
UInt64 *dataOffset,
UInt32 *numPackStreams,
UInt64 **packSizes,
Byte **packCRCsDefined,
UInt32 **packCRCs,
ISzAlloc *alloc)
{
UInt32 i;
RINOK(SzReadNumber(sd, dataOffset));
RINOK(SzReadNumber32(sd, numPackStreams));
RINOK(SzWaitAttribute(sd, k7zIdSize));
MY_ALLOC(UInt64, *packSizes, (size_t)*numPackStreams, alloc);
for (i = 0; i < *numPackStreams; i++)
{
RINOK(SzReadNumber(sd, (*packSizes) + i));
}
for (;;)
{
UInt64 type;
RINOK(SzReadID(sd, &type));
if (type == k7zIdEnd)
break;
if (type == k7zIdCRC)
{
RINOK(SzReadHashDigests(sd, (size_t)*numPackStreams, packCRCsDefined, packCRCs, alloc));
continue;
}
RINOK(SzSkeepData(sd));
}
if (*packCRCsDefined == 0)
{
MY_ALLOC(Byte, *packCRCsDefined, (size_t)*numPackStreams, alloc);
MY_ALLOC(UInt32, *packCRCs, (size_t)*numPackStreams, alloc);
for (i = 0; i < *numPackStreams; i++)
{
(*packCRCsDefined)[i] = 0;
(*packCRCs)[i] = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -