壓縮軟件程序源代碼_第1頁
壓縮軟件程序源代碼_第2頁
壓縮軟件程序源代碼_第3頁
壓縮軟件程序源代碼_第4頁
壓縮軟件程序源代碼_第5頁
已閱讀5頁,還剩40頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

壓縮軟件程序源代碼

//7ZipDecode.cpp:Definestheentrypointfortheconsoleapplication.//

#include"stdafx.h"

#include"7ZipDecode.h"

#ifdef_DEBUG

#definenewDEBUG_NEW

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

//Theoneandonlyapplicationobject

#include

#include

#include

#ifdef_WIN32

#include

#endif

#ifndefEXTERN_C_BEGIN

#ifdef__cplusplus

#defineEXTERN_C_BEGINextern"C"{

#defineEXTERN_C_END}

#else

#defineEXTERN_C_BEGIN

#defineEXTERN_C_END

#endif

#endif

EXTERN_C_BEGIN

#defineSZ_OK0

#defineSZ_ERROR_DATA1

#defineSZ_ERROR_MEM2

#defineSZ_ERROR_CRC3

#defineSZ_ERROR_UNSUPPORTED4

#defineSZ_ERROR_PARAM5

#defineSZ_ERROR_INPUT_EOF6

#defineSZ_ERROR_OUTPUT_EOF7

#defineSZ_ERROR_READ8

#defineSZ_ERROR_WRITE9

#defineSZ_ERROR_PROGRESS10

#defineSZ_ERROR_FAIL11

#defineSZ_ERROR_THREAD12

#defineSZ_ERROR_ARCHIVE16

#defineSZ_ERROR_NO_ARCHIVE17

typedefintSRes;

#ifdef_WIN32

typedefDWORDWRes;

#else

typedefintWRes;

#endif

#ifndefRINOK

#defineRINOK(x){int__result__=(x);if(__result__!=0)return

__result__;}

#endif

typedefunsignedcharByte;

typedefshortInt16;

typedefunsignedshortUInt16;

#ifdef_LZMA_UINT32_IS_ULONG

typedeflongInt32;

typedefunsignedlongUInt32;

#else

typedefintInt32;

typedefunsignedintUInt32;

#endif

#ifdef_SZ_NO_INT_64

/*define_SZ_NO_INT_64,ifyourcompilerdoesn'tsupport64-bitintegers.NOTES:Somecodewillworkincorrectlyinthatcase!*/

typedeflongInt64;

typedefunsignedlongUInt64;

#else

#ifdefined(_MSC_VER)||defined(__BORLANDC__)

typedef__int64Int64;

typedefunsigned__int64UInt64;

#defineUINT64_CONST(n)n

#else

typedeflonglongintInt64;

typedefunsignedlonglongintUInt64;

#defineUINT64_CONST(n)n##ULL

#endif

#endif

#ifdef_LZMA_NO_SYSTEM_SIZE_T

typedefUInt32SizeT;

#else

typedefsize_tSizeT;

#endif

typedefintBool;

#defineTrue1

#defineFalse0

#ifdef_WIN32

#defineMY_STD_CALL__stdcall

#else

#defineMY_STD_CALL

#endif

#ifdef_MSC_VER

#if_MSC_VER>=1300

#defineMY_NO_INLINE__declspec(noinline)

#else

#defineMY_NO_INLINE

#endif

#define#define#else#define#define

#endif

MY_CDECL__cdecl

MY_FAST_CALL__fastcall

MY_CDECL

MY_FAST_CALL

/*Thefollowinginterfacesusefirstparameteraspointertostructure*/typedefstruct

{

Byte(*Read)(void*p);/*readsonebyte,returns0incaseofEOForerror*/

}IByteIn;

typedefstruct

{

void(*Write)(void*p,Byteb);

}IByteOut;

typedefstruct

{

SRes(*Read)(void*p,void*buf,size_t*size);

/*if(input(*size)!=0&&output(*size)==0)meansend_of_stream.

(output(*size)<input(*size))isallowed*/

}ISeqInStream;

/*itcanreturnSZ_ERROR_INPUT_EOF*/

SResSeqInStream_Read(ISeqInStream*stream,void*buf,size_tsize);SResSeqInStream_Read2(ISeqInStream*stream,void*buf,size_tsize,SReserrorType);

SResSeqInStream_ReadByte(ISeqInStream*stream,Byte*buf);

typedefstruct

{

size_t(*Write)(void*p,constvoid*buf,size_tsize);

/*Returns:result-thenumberofactuallywrittenbytes.

(result<size)meanserror*/

}ISeqOutStream;

typedefenum

{

SZ_SEEK_SET=0,

SZ_SEEK_CUR=1,

SZ_SEEK_END=2

}ESzSeek;

typedefstruct

{

SRes(*Read)(void*p,void*buf,size_t*size);/*sameasISeqInStream::Read*/

SRes(*Seek)(void*p,Int64*pos,ESzSeekorigin);

}ISeekInStream;

typedefstruct

{

SRes(*Look)(void*p,constvoid**buf,size_t*size);

/*if(input(*size)!=0&&output(*size)==0)meansend_of_stream.

(output(*size)>input(*size))isnotallowed

(output(*size)<input(*size))isallowed*/

SRes(*Skip)(void*p,size_toffset);

/*offsetmustbe<=output(*size)ofLook*/

SRes(*Read)(void*p,void*buf,size_t*size);

/*readsdirectly(withoutbuffer).It'ssameasISeqInStream::Read*/SRes(*Seek)(void*p,Int64*pos,ESzSeekorigin);

}ILookInStream;

SResLookInStream_LookRead(ILookInStream*stream,void*buf,size_t*size);SResLookInStream_SeekTo(ILookInStream*stream,UInt64offset);

/*readsviaILookInStream::Read*/

SResLookInStream_Read2(ILookInStream*stream,void*buf,size_tsize,SReserrorType);

SResLookInStream_Read(ILookInStream*stream,void*buf,size_tsize);

#defineLookToRead_BUF_SIZE(1<<14)

typedefstruct

{

ILookInStreams;

ISeekInStream*realStream;

size_tpos;

size_tsize;

Bytebuf[LookToRead_BUF_SIZE];

}CLookToRead;

voidLookToRead_CreateVTable(CLookToRead*p,intlookahead);

voidLookToRead_Init(CLookToRead*p);

typedefstruct

{

ISeqInStreams;

ILookInStream*realStream;

}CSecToLook;

voidSecToLook_CreateVTable(CSecToLook*p);

typedefstruct

{

ISeqInStreams;

ILookInStream*realStream;

}CSecToRead;

voidSecToRead_CreateVTable(CSecToRead*p);

typedefstruct

{

SRes(*Progress)(void*p,UInt64inSize,UInt64outSize);

/*Returns:result.(result!=SZ_OK)meansbreak.

Value(UInt64)(Int64)-1forsizemeansunknownvalue.*/

}ICompressProgress;

typedefstruct

{

void*(*Alloc)(void*p,size_tsize);

void(*Free)(void*p,void*address);/*addresscanbe0*/

}ISzAlloc;

#defineIAlloc_Alloc(p,size)(p)->Alloc((p),size)

#defineIAlloc_Free(p,a)(p)->Free((p),a)

#ifdef_WIN32

#defineCHAR_PATH_SEPARATOR'\\'

#defineWCHAR_PATH_SEPARATORL'\\'

#defineSTRING_PATH_SEPARATOR"\\"

#defineWSTRING_PATH_SEPARATORL"\\"

#else

#defineCHAR_PATH_SEPARATOR'/'

#defineWCHAR_PATH_SEPARATORL'/'

#defineSTRING_PATH_SEPARATOR"/"

#defineWSTRING_PATH_SEPARATORL"/"

#endif

externUInt32g_CrcTable[];

/*CallCrcGenerateTableonetimebeforeotherCRCfunctions*/

voidMY_FAST_CALLCrcGenerateTable(void);

#defineCRC_INIT_VAL0xFFFFFFFF

#defineCRC_GET_DIGEST(crc)((crc)^CRC_INIT_VAL)

#defineCRC_UPDATE_BYTE(crc,b)(g_CrcTable[((crc)^(b))&0xFF]^((crc)>>8))

UInt32MY_FAST_CALLCrcUpdate(UInt32crc,constvoid*data,size_tsize);UInt32MY_FAST_CALLCrcCalc(constvoid*data,size_tsize);

typedefstruct

{

#ifdefUSE_WINDOWS_FILE

HANDLEhandle;

#else

FILE*file;

#endif

}CSzFile;

voidFile_Construct(CSzFile*p);

#if!defined(UNDER_CE)||!defined(USE_WINDOWS_FILE)

WResInFile_Open(CSzFile*p,constchar*name);WResOutFile_Open(CSzFile*p,constchar*name);#endif

#ifdefUSE_WINDOWS_FILE

WResInFile_OpenW(CSzFile*p,constWCHAR*name);WResOutFile_OpenW(CSzFile*p,constWCHAR*name);#endif

WResFile_Close(CSzFile*p);

/*readsmax(*size,remainfile'ssize)bytes*/WResFile_Read(CSzFile*p,void*data,size_t*size);/*writes*sizebytes*/

WResFile_Write(CSzFile*p,constvoid*data,size_t*size);WResFile_Seek(CSzFile*p,Int64*pos,ESzSeekorigin);

WResFile_GetLength(CSzFile*p,UInt64*length);/*----------FileInStream----------*/

typedefstruct

{

ISeqInStreams;

CSzFilefile;

}CFileSeqInStream;

voidFileSeqInStream_CreateVTable(CFileSeqInStream*p);typedefstruct

{

ISeekInStreams;

CSzFilefile;

}CFileInStream;

voidFileInStream_CreateVTable(CFileInStream*p);typedefstruct

{

ISeqOutStreams;

CSzFilefile;

}CFileOutStream;

voidFileOutStream_CreateVTable(CFileOutStream*p);

#ifdefined(_M_X64)||defined(_M_AMD64)defined(__x86_64__)#defineMY_CPU_AMD64

#endif

#ifdefined(MY_CPU_AMD64)||defined(_M_IA64)

#defineMY_CPU_64BIT

#endif

#ifdefined(_M_IX86)||defined(__i386__)

#defineMY_CPU_X86

#endif

#ifdefined(MY_CPU_X86)||defined(MY_CPU_AMD64)

#defineMY_CPU_X86_OR_AMD64

#endif

#ifdefined(MY_CPU_X86)||defined(_M_ARM)

#defineMY_CPU_32BIT

#endif

#ifdefined(_WIN32)&&defined(_M_ARM)

#defineMY_CPU_ARM_LE

#endif

#ifdefined(_WIN32)&&defined(_M_IA64)

#defineMY_CPU_IA64_LE

#endif

#ifdefined(MY_CPU_X86_OR_AMD64)

#defineMY_CPU_LE_UNALIGN

#endif

#ifdefined(MY_CPU_X86_OR_AMD64)defined(MY_CPU_ARM_LE)||

defined(MY_CPU_IA64_LE)||defined(__ARMEL__)

||

||

||

defined(__MIPSEL__)||defined(__LITTLE_ENDIAN__)

#defineMY_CPU_LE

#endif

#ifdefined(__BIG_ENDIAN__)

#defineMY_CPU_BE

#endif

#ifdefined(MY_CPU_LE)&&defined(MY_CPU_BE)

Stop_Compiling_Bad_Endian

#endif

#ifdefMY_CPU_LE_UNALIGN

#defineGetUi16(p)(*(constUInt16*)(p))

#defineGetUi32(p)(*(constUInt32*)(p))

#defineGetUi64(p)(*(constUInt64*)(p))

#defineSetUi16(p,d)*(UInt16*)(p)=(d);

#defineSetUi32(p,d)*(UInt32*)(p)=(d);

#defineSetUi64(p,d)*(UInt64*)(p)=(d);

#else

#defineGetUi16(p)(((constByte*)(p))[0]|((UInt16)((constByte*)(p))[1]<<8))

#defineGetUi32(p)(\

((constByte*)(p))[0]|\

((UInt32)((constByte*)(p))[1]<<8)|\

((UInt32)((constByte*)(p))[2]<<16)|\

((UInt32)((constByte*)(p))[3]<<24))

#defineGetUi64(p)(GetUi32(p)|((UInt64)GetUi32(((constByte*)(p))+4)<<32))

#defineSetUi16(p,d){UInt32_x_=(d);\

((Byte*)(p))[0]=(Byte)_x_;\

((Byte*)(p))[1]=(Byte)(_x_>>8);}

#defineSetUi32(p,d){UInt32_x_=(d);\

((Byte*)(p))[0]=(Byte)_x_;\

((Byte*)(p))[1]=(Byte)(_x_>>8);\

((Byte*)(p))[2]=(Byte)(_x_>>16);\

((Byte*)(p))[3]=(Byte)(_x_>>24);}

#defineSetUi64(p,d){UInt64_x64_=(d);\

SetUi32(p,(UInt32)_x64_);\

SetUi32(((Byte*)(p))+4,(UInt32)(_x64_>>32));}#endif

#ifdefined(MY_CPU_LE_UNALIGN)&&defined(_WIN64)&&(_MSC_VER>=1300)#pragmaintrinsic(_byteswap_ulong)

#pragmaintrinsic(_byteswap_uint64)

#defineGetBe32(p)_byteswap_ulong(*(constUInt32*)(constByte*)(p))

#defineGetBe64(p)_byteswap_uint64(*(constUInt64*)(constByte*)(p))#else

#defineGetBe32(p)(\

((UInt32)((constByte*)(p))[0]<<24)|\

((UInt32)((constByte*)(p))[1]<<16)|\

((UInt32)((constByte*)(p))[2]<<8)|\

((constByte*)(p))[3])

#defineGetBe64(p)(((UInt64)GetBe32(p)<<32)|GetBe32(((constByte*)(p))+4))

#endif

#defineGetBe16(p)(((UInt16)((constByte*)(p))[0]<<8)|((constByte*)(p))[1])

#ifdefMY_CPU_X86_OR_AMD64

typedefstruct

{

UInt32maxFunc;

UInt32vendor[3];

UInt32ver;

UInt32b;

UInt32c;

UInt32d;

}Cx86cpuid;

enum

{

CPU_FIRM_INTEL,

CPU_FIRM_AMD,

CPU_FIRM_VIA

};

Boolx86cpuid_CheckAndRead(Cx86cpuid*p);

intx86cpuid_GetFirm(constCx86cpuid*p);

#definex86cpuid_GetFamily(p)(((p)->ver>>8)&0xFF00F)#definex86cpuid_GetModel(p)(((p)->ver>>4)&0xF00F)#definex86cpuid_GetStepping(p)((p)->ver&0xF)

BoolCPU_Is_InOrder();

BoolCPU_Is_Aes_Supported();

#endif

#ifdefMY_CPU_32BIT

#definePPMD_32BIT

#endif

#definePPMD_INT_BITS7

#definePPMD_PERIOD_BITS7

#definePPMD_BIN_SCALE(1<<(PPMD_INT_BITS+PPMD_PERIOD_BITS))

#definePPMD_GET_MEAN_SPEC(summ,shift,round)

(((summ)+(1<<((shift)-(round))))>>(shift))

#define

PPMD_GET_MEAN_SPEC((summ),

PPMD_GET_MEAN(summ)

PPMD_PERIOD_BITS,2)

#definePPMD_UPDATE_PROB_0(prob)((prob)+(1<<

PPMD_INT_BITS)-

PPMD_GET_MEAN(prob))

#definePPMD_UPDATE_PROB_1(prob)((prob)-PPMD_GET_MEAN(prob))

#definePPMD_N14

#definePPMD_N24

#definePPMD_N34

#definePPMD_N4((128+3-1*PPMD_N1-2*PPMD_N2-3*PPMD_N3)/4)#definePPMD_NUM_INDEXES(PPMD_N1+PPMD_N2+PPMD_N3+PPMD_N4)

/*SEE-contextsforPPM-contextswithmaskedsymbols*/

typedefstruct

{

UInt16Summ;/*Freq*/

ByteShift;/*SpeedofFreqchange;lowShiftisforfastchange*/ByteCount;/*CounttonextchangeofShift*/

}CPpmd_See;

#definePpmd_See_Update(p)if((p)->Shift<PPMD_PERIOD_BITS&&

--(p)->Count==0)\

{(p)->Summ<<=1;(p)->Count=(Byte)(3<<(p)->Shift++);}

typedefstruct

{

ByteSymbol;

ByteFreq;

UInt16SuccessorLow;

UInt16SuccessorHigh;

}CPpmd_State;

typedef

#ifdefPPMD_32BIT

CPpmd_State*

#else

UInt32

#endif

CPpmd_State_Ref;

typedef

#ifdefPPMD_32BIT

void*

#else

UInt32

#endif

CPpmd_Void_Ref;

typedef

#ifdefPPMD_32BIT

Byte*

#else

UInt32

#endif

CPpmd_Byte_Ref;

#definePPMD_SetAllBitsIn256Bytes(p)\

{unsignedi;for(i=0;i<256/sizeof(p[0]);i+=8){\

p[i+7]=p[i+6]=p[i+5]=p[i+4]=p[i+3]=p[i+2]=p[i+1]=p[i+0]=~(size_t)0;}}

#definePPMD7_MIN_ORDER2

#definePPMD7_MAX_ORDER64

#definePPMD7_MIN_MEM_SIZE(1<<11)

#definePPMD7_MAX_MEM_SIZE(0xFFFFFFFF-12*3)structCPpmd7_Context_;

typedef

#ifdefPPMD_32BIT

structCPpmd7_Context_*

#else

UInt32

#endif

CPpmd7_Context_Ref;

typedefstructCPpmd7_Context_

{

UInt16NumStats;

UInt16SummFreq;

CPpmd_State_RefStats;

CPpmd7_Context_RefSuffix;

}CPpmd7_Context;

#definePpmd7Context_OneState(p)((CPpmd_State*)&(p)->SummFreq)

typedefstruct

CPpmd7_Context*MinContext,*MaxContext;

CPpmd_State*FoundState;

unsignedOrderFall,InitEsc,PrevSuccess,MaxOrder,HiBitsFlag;

Int32RunLength,InitRL;/*mustbe32-bitatleast*/UInt32Size;

UInt32GlueCount;

Byte*Base,*LoUnit,*HiUnit,*Text,*UnitsStart;

UInt32AlignOffset;

ByteIndx2Units[PPMD_NUM_INDEXES];

ByteUnits2Indx[128];

CPpmd_Void_RefFreeList[PPMD_NUM_INDEXES];ByteNS2Indx[256],NS2BSIndx[256],HB2Flag[256];

CPpmd_SeeDummySee,See[25][16];

UInt16BinSumm[128][64];

}CPpmd7;

voidPpmd7_Construct(CPpmd7*p);

BoolPpmd7_Alloc(CPpmd7*p,UInt32size,ISzAlloc*alloc);voidPpmd7_Free(CPpmd7*p,ISzAlloc*alloc);

voidPpmd7_Init(CPpmd7*p,unsignedmaxOrder);#definePpmd7_WasAllocated(p)((p)->Base!=NULL)

/*----------InternalFunctions----------*/

externconstBytePPMD7_kExpEscape[16];

#ifdefPPMD_32BIT

#definePpmd7_GetPtr(p,ptr)(ptr)

#definePpmd7_GetContext(p,ptr)(ptr)

#definePpmd7_GetStats(p,ctx)((ctx)->Stats)

#else

#definePpmd7_GetPtr(p,offs)((void*)((p)->Base+(offs)))

#definePpmd7_GetContext(p,offs)((CPpmd7_Context*)Ppmd7_GetPtr((p),(offs)))

#definePpmd7_GetStats(p,ctx)((CPpmd_State*)Ppmd7_GetPtr((p),

((ctx)->Stats)))

#endif

voidPpmd7_Update1(CPpmd7*p);

voidPpmd7_Update1_0(CPpmd7*p);

voidPpmd7_Update2(CPpmd7*p);

voidPpmd7_UpdateBin(CPpmd7*p);

#definePpmd7_GetBinSumm(p)\

&p->BinSumm[Ppmd7Context_OneState(p->MinContext)->Freq-

1][p->PrevSuccess+\

p->NS2BSIndx[Ppmd7_GetContext(p,

p->MinContext->Suffix)->NumStats-1]+\

(p->HiBitsFlag=p->HB2Flag[p->FoundState->Symbol])+\

2*p->HB2Flag[Ppmd7Context_OneState(p->MinContext)->Symbo

l]+\

((p->RunLength>>26)&0x20)]

CPpmd_See*Ppmd7_MakeEscFreq(CPpmd7*p,unsignednumMasked,UInt32*scale);

/*----------Decode----------*/

typedefstruct

{

UInt32(*GetThreshold)(void*p,UInt32total);void(*Decode)(void*p,UInt32start,UInt32size);UInt32(*DecodeBit)(void*p,UInt32size0);}IPpmd7_RangeDec;

typedefstruct

{

IPpmd7_RangeDecp;

UInt32Range;

UInt32Code;

IByteIn*Stream;

}CPpmd7z_RangeDec;

voidPpmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec*p);

BoolPpmd7z_RangeDec_Init(CPpmd7z_RangeDec*p);

#definePpmd7z_RangeDec_IsFinishedOK(p)((p)->Code==0)

intPpmd7_DecodeSymbol(CPpmd7*p,IPpmd7_RangeDec*rc);

/*----------Encode----------*/

typedefstruct

{

UInt64Low;

UInt32Range;

ByteCache;

UInt64CacheSize;

IByteOut*Stream;

}CPpmd7z_RangeEnc;

voidPpmd7z_RangeEnc_Init(CPpmd7z_RangeEnc*p);

voidPpmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc*p);

voidPpmd7_EncodeSymbol(CPpmd7*p,CPpmd7z_RangeEnc*rc,intsymbol);EXTERN_C_END

#ifdef__cplusplus

extern"C"{

#endif

typedefstruct

{

Byte*data;

size_tsize;

}CBuf;

voidBuf_Init(CBuf*p);

intBuf_Create(CBuf*p,size_tsize,ISzAlloc*alloc);voidBuf_Free(CBuf*p,ISzAlloc*alloc);

typedefstruct

{

Byte*data;

size_tsize;

size_tpos;

}CDynBuf;

voidDynBuf_Construct(CDynBuf*p);

voidDynBuf_SeekToBeg(CDynBuf*p);

intDynBuf_Write(CDynBuf*p,constByte*buf,size_tsize,ISzAlloc*alloc);

voidDynBuf_Free(CDynBuf*p,ISzAlloc*alloc);

#ifdef__cplusplus

}

#endif

#include

void*SzAlloc(void*p,size_tsize);

voidSzFree(void*p,void*address);

void*SzAllocTemp(void*p,size_tsize);

voidSzFreeTemp(void*p,void*address);

#ifdef_SZ_ALLOC_DEBUG

#ifdef_WIN32

#include

#endif

intg_allocCount=0;

intg_allocCountTemp=0;

#endif

void*SzAlloc(void*p,size_tsize)

{

p=p;

if(size==0)

return0;

#ifdef_SZ_ALLOC_DEBUG

fprintf(stderr,"\nAlloc%10dbytes;count=%10d",size,g_allocCount);

g_allocCount++;

#endif

returnmalloc(size);

}

voidSzFree(void*p,void*address)

{

p=p;

#ifdef_SZ_ALLOC_DEBUG

if(address!=0)

{

g_allocCount--;

fprintf(stderr,"\nFree;count=%10d",g_allocCount);

}

free(address);

}

void*SzAllocTemp(void*p,size_tsize)

{

p=p;

if(size==0)

return0;

#ifdef_SZ_ALLOC_DEBUG

fprintf(stderr,"\nAlloc_temp%10dbytes;count=%10d",size,g_allocCountTemp);

g_allocCountTemp++;

#ifdef_WIN32

returnHeapAlloc(GetProcessHeap(),0,size);

#endif

#endif

returnmalloc(size);

}

voidSzFreeTemp(void*p,void*address)

{

p=p;

#ifdef_SZ_ALLOC_DEBUG

if(address!=0)

{

g_allocCountTemp--;

fprintf(stderr,"\nFree_temp;count=%10d",

g_allocCountTemp);

}

#ifdef_WIN32

HeapFree(GetProcessHeap(),0,address);

return;

#endif

#endif

free(address);

}

voidBuf_Init(CBuf*p)

{

p->data=0;

p->size=0;

}

intBuf_Create(CBuf*p,size_tsize,ISzAlloc*alloc){

p->size=0;

if(size==0)

{

p->data=0;

return1;

}

p->data=(Byte*)alloc->Alloc(alloc,size);

if(p->data!=0)

{

p->size=size;

return1;

}

return0;

}

voidBuf_Free(CBuf*p,ISzAlloc*alloc)

{

alloc->Free(alloc,p->data);

p->data=0;

p->size=0;

}

#ifndefUSE_WINDOWS_FILE

#ifndefUNDER_CE

#include

#endif

#else

/*

ReadFileandWriteFilefunctionsinWindowshaveBUG:

IfyouReadorWrite64MBormore(probablymin_failure_size=64MB-32KB+1)

from/toNetworkfile,itreturnsERROR_NO_SYSTEM_RESOURCES

(Insufficientsystemresourcesexisttocompletetherequestedservice).ProbablyinsomeversionofWindowsthereareproblemswithothersizes:for32MB(maybealsofor16MB).

Andmessagecanbe"Networkconnectionwaslost"

*/

#definekChunkSizeMax(1<<22)

#endif

voidFile_Construct(CSzFile*p)

{

#ifdefUSE_WINDOWS_FILE

p->handle=INVALID_HANDLE_VALUE;

#else

p->file=NULL;

#endif

}

#if!defined(UNDER_CE)||!defined(USE_WINDOWS_FILE)

staticWResFile_Open(CSzFile*p,constchar*name,intwriteMode)

{

#ifdefUSE_WINDOWS_FILE

p->handle=CreateFileA(name,

writeMode?GENERIC_WRITE:GENERIC_READ,

FILE_SHARE_READ,NULL,

writeMode?CREATE_ALWAYS:OPEN_EXISTING,

FILE_ATTRIBUTE_NORMAL,NULL);

return(p->handle!=INVALID_HANDLE_VALUE)?0:GetLastError();

#else

p->file=fopen(name,writeMode?"wb+":"rb");

return(p->file!=0)?0:

#ifdefUNDER_CE

2;/*ENOENT*/

#else

errno;

#endif

}

WResInFile_Open(CSzFile*p,constchar*name){return

File_Open(p,name,0);}

WResOutFile_Open(CSzFile*p,constchar*name){returnFile_Open(p,name,1);}

#endif

#ifdefUSE_WINDOWS_FILE

staticWResFile_OpenW(CSzFile*p,constWCHAR*name,intwriteMode)

{

p->handle=CreateFileW(name,

writeMode?GENERIC_WRITE:GENERIC_READ,

FILE_SHARE_READ,NULL,

writeMode?CREATE_ALWAYS:OPEN_EXISTING,

FILE_ATTRIBUTE_NORMAL,NULL);

return(p->handle!=INVALID_HANDLE_VALUE)?0:GetLastError();

}

WResInFile_OpenW(CSzFile*p,constWCHAR*name){returnFile_OpenW(p,name,0);}

WResOutFile_OpenW(CSzFile*p,constWCHAR*name){returnFile_OpenW(p,name,1);}

#endif

WResFile_Close(CSzFile*p)

{

#ifdefUSE_WINDOWS_FILE

if(p->handle!=INVALID_HANDLE_VALUE)

{

if(!CloseHandle(p->handle))

returnGetLastError();

p->handle=INVALID_HANDLE_VALUE;

}

#else

if(p->file!=NULL)

{

intres=fclose(p->file);

if(res!=0)

returnres;

p->file=NULL;

}

#endif

return0;

}

WResFile_Read(CSzFile*p,void*data,size_t*size){

size_toriginalSize=*size;

if(originalSize==0)

return0;

#ifdefUSE_WINDOWS_FILE

*size=0;

do

DWORDcurSize=(originalSize>kChunkSizeMax)?kChunkSizeMax:(DWORD)originalSize;

DWORDprocessed=0;

BOOLres=ReadFile(p->handle,data,curSize,&processed,NULL);

data=(void*)((Byte*)data+processed);

originalSize-=processed;

*size+=processed;

if(!res)

returnGetLastError();

if(processed==0)

break;

}

while(originalSize>0);

return0;

#else

*size=fread(data,1,originalSize,p->file);

if(*size==originalSize)

return0;

returnferror(p->file);

#endif

}

WResFile_Write(CSzFile*p,constvoid*data,size_t*size)

{

size_toriginalSize=*size;

if(originalSize==0)

return0;

#ifdefUSE_WINDOWS_FILE

*size=0;

do

{

DWORDcurSize=(originalSize>kChunkSizeMax)?kChunkSizeMax:(DWORD)originalSize;

DWORDprocessed=0;

BOOLres=WriteFile(p->handle,data,curSize,&processed,NULL);

data=(void*)((Byte*)data+processed);

originalSize-=processed;

*size+=processed;

if(!res)

returnGetLastError();

if(processed==0)

break;

}

while(originalSize>0);

return0;

#else

*size=fwrite(data,1,originalSize,p->file);

if(*size==originalSize)

return0;

returnferror(p->file);

#endif

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論