int LoadFile(PHGlobal *global,PH_parameter *parameter) { if( access(parameter->szconfig,0) >= 0 && MyReadFile(parameter->szconfig,global,parameter) == 0 ) { int flag = -1; // memset(global->szHost,0,sizeof(global->szHost)); // GetValue("settings","szHost",global->szHost,global); if( strlen(global->szHost) == 0 ) { printf("no appointing szHost\n"); NewHost(global); flag=1; } // memset(global->szUserID,0,sizeof(global->szUserID)); // GetValue("settings","szUserID",&global->szUserID,global); if( strlen(global->szUserID) == 0 ) { printf("no appointing szUserID\n"); NewUserID(global); flag=1; } // memset(global->szUserPWD,0,sizeof(global->szUserPWD)); // GetValue("settings","szUserPWD",&global->szUserPWD,global); if( strlen(global->szUserPWD) == 0 ) { printf("no appointing szUserPWD\n"); NewUserPWD(global); flag=1; } // memset(global->nicName,0,sizeof(global->nicName)); // GetValue("settings","nicName",&global->nicName,global); if( strlen(parameter->nicName) == 0 ) { printf("no appointing nicName\n"); NewnicName(global,parameter); flag=1; } if( strlen(parameter->logfile) == 0 ) { printf("no appointing logfile\n"); NewFile(parameter); flag=1; } if(flag == 1) if( SaveFile(parameter->szconfig,global,parameter) != 0 ) return -1; } else { if( NewIni(parameter->szconfig,global,parameter) != 0 ) return -1; } return 0; }
SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size, size_t *processedSize) { CFileInStream *s = (CFileInStream *)object; size_t processedSizeLoc = MyReadFile(s->File, buffer, size); if (processedSize != 0) *processedSize = processedSizeLoc; return SZ_OK; }
SRes SzFileReadImp(void *object, void **buffer, size_t *size) { CFileInStream *s = (CFileInStream *)object; if (*size > kBufferSize) *size = kBufferSize; *size = MyReadFile(s->File, g_Buffer, *size); *buffer = g_Buffer; return SZ_OK; }
static SRes MyRead(void *p, void *buf, size_t *size) { if (*size == 0) return SZ_OK; *size = MyReadFile(((CFileSeqInStream*)p)->file, buf, *size); /* if (*size == 0) return SZE_FAIL; */ return SZ_OK; }
SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxRequiredSize, size_t *processedSize) { CFileInStream *s = (CFileInStream *)object; size_t processedSizeLoc; if (maxRequiredSize > kBufferSize) maxRequiredSize = kBufferSize; processedSizeLoc = MyReadFile(s->File, g_Buffer, maxRequiredSize); *buffer = g_Buffer; if (processedSize != 0) *processedSize = processedSizeLoc; return SZ_OK; }
int main(int argc, char **argv) { RTR3InitExe(argc, &argv, 0); RTDIGESTTYPE enmDigestType = RTDIGESTTYPE_INVALID; const char *pszDigestType = "NotSpecified"; enum { kMethod_Full, kMethod_Block, kMethod_File, kMethod_CVAS } enmMethod = kMethod_Block; uint64_t offStart = 0; uint64_t cbMax = UINT64_MAX; bool fTestcase = false; static const RTGETOPTDEF s_aOptions[] = { { "--type", 't', RTGETOPT_REQ_STRING }, { "--method", 'm', RTGETOPT_REQ_STRING }, { "--help", 'h', RTGETOPT_REQ_NOTHING }, { "--length", 'l', RTGETOPT_REQ_UINT64 }, { "--offset", 'o', RTGETOPT_REQ_UINT64 }, { "--testcase", 'x', RTGETOPT_REQ_NOTHING }, }; int ch; RTGETOPTUNION ValueUnion; RTGETOPTSTATE GetState; RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST); while ((ch = RTGetOpt(&GetState, &ValueUnion))) { switch (ch) { case 't': if (!RTStrICmp(ValueUnion.psz, "crc32")) { pszDigestType = "CRC32"; enmDigestType = RTDIGESTTYPE_CRC32; } else if (!RTStrICmp(ValueUnion.psz, "crc64")) { pszDigestType = "CRC64"; enmDigestType = RTDIGESTTYPE_CRC64; } else if (!RTStrICmp(ValueUnion.psz, "md2")) { pszDigestType = "MD2"; enmDigestType = RTDIGESTTYPE_MD2; } else if (!RTStrICmp(ValueUnion.psz, "md5")) { pszDigestType = "MD5"; enmDigestType = RTDIGESTTYPE_MD5; } else if (!RTStrICmp(ValueUnion.psz, "sha1")) { pszDigestType = "SHA-1"; enmDigestType = RTDIGESTTYPE_SHA1; } else if (!RTStrICmp(ValueUnion.psz, "sha224")) { pszDigestType = "SHA-224"; enmDigestType = RTDIGESTTYPE_SHA224; } else if (!RTStrICmp(ValueUnion.psz, "sha256")) { pszDigestType = "SHA-256"; enmDigestType = RTDIGESTTYPE_SHA256; } else if (!RTStrICmp(ValueUnion.psz, "sha384")) { pszDigestType = "SHA-384"; enmDigestType = RTDIGESTTYPE_SHA384; } else if (!RTStrICmp(ValueUnion.psz, "sha512")) { pszDigestType = "SHA-512"; enmDigestType = RTDIGESTTYPE_SHA512; } else if (!RTStrICmp(ValueUnion.psz, "sha512/224")) { pszDigestType = "SHA-512/224"; enmDigestType = RTDIGESTTYPE_SHA512T224; } else if (!RTStrICmp(ValueUnion.psz, "sha512/256")) { pszDigestType = "SHA-512/256"; enmDigestType = RTDIGESTTYPE_SHA512T256; } else { Error("Invalid digest type: %s\n", ValueUnion.psz); return 1; } break; case 'm': if (!RTStrICmp(ValueUnion.psz, "full")) enmMethod = kMethod_Full; else if (!RTStrICmp(ValueUnion.psz, "block")) enmMethod = kMethod_Block; else if (!RTStrICmp(ValueUnion.psz, "file")) enmMethod = kMethod_File; else if (!RTStrICmp(ValueUnion.psz, "cvas")) enmMethod = kMethod_CVAS; else { Error("Invalid digest method: %s\n", ValueUnion.psz); return 1; } break; case 'l': cbMax = ValueUnion.u64; break; case 'o': offStart = ValueUnion.u64; break; case 'x': fTestcase = true; break; case 'h': RTPrintf("usage: tstRTDigest -t <digest-type> [-o <offset>] [-l <length>] [-x] file [file2 [..]]\n"); return 1; case VINF_GETOPT_NOT_OPTION: { if (enmDigestType == RTDIGESTTYPE_INVALID) return Error("No digest type was specified\n"); switch (enmMethod) { case kMethod_Full: return Error("Full file method is not implemented\n"); case kMethod_File: if (offStart != 0 || cbMax != UINT64_MAX) return Error("The -l and -o options do not work with the 'file' method."); switch (enmDigestType) { case RTDIGESTTYPE_SHA1: { char *pszDigest; int rc = RTSha1DigestFromFile(ValueUnion.psz, &pszDigest, NULL, NULL); if (RT_FAILURE(rc)) return Error("RTSha1Digest(%s,) -> %Rrc\n", ValueUnion.psz, rc); RTPrintf("%s %s\n", pszDigest, ValueUnion.psz); RTStrFree(pszDigest); break; } case RTDIGESTTYPE_SHA256: { char *pszDigest; int rc = RTSha256DigestFromFile(ValueUnion.psz, &pszDigest, NULL, NULL); if (RT_FAILURE(rc)) return Error("RTSha256Digest(%s,) -> %Rrc\n", ValueUnion.psz, rc); RTPrintf("%s %s\n", pszDigest, ValueUnion.psz); RTStrFree(pszDigest); break; } default: return Error("The file method isn't implemented for this digest\n"); } break; case kMethod_Block: { RTFILE hFile; int rc = RTFileOpen(&hFile, ValueUnion.psz, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE); if (RT_FAILURE(rc)) return Error("RTFileOpen(,%s,) -> %Rrc\n", ValueUnion.psz, rc); if (offStart != 0) { rc = RTFileSeek(hFile, offStart, RTFILE_SEEK_BEGIN, NULL); if (RT_FAILURE(rc)) return Error("RTFileSeek(%s,%ull) -> %Rrc\n", ValueUnion.psz, offStart, rc); } uint64_t cbMaxLeft = cbMax; size_t cbRead; uint8_t abBuf[_64K]; char *pszDigest = (char *)&abBuf[0]; switch (enmDigestType) { case RTDIGESTTYPE_CRC32: { uint32_t uCRC32 = RTCrc32Start(); for (;;) { rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); if (RT_FAILURE(rc) || !cbRead) break; uCRC32 = RTCrc32Process(uCRC32, abBuf, cbRead); } uCRC32 = RTCrc32Finish(uCRC32); RTStrPrintf(pszDigest, sizeof(abBuf), "%08RX32", uCRC32); break; } case RTDIGESTTYPE_CRC64: { uint64_t uCRC64 = RTCrc64Start(); for (;;) { rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); if (RT_FAILURE(rc) || !cbRead) break; uCRC64 = RTCrc64Process(uCRC64, abBuf, cbRead); } uCRC64 = RTCrc64Finish(uCRC64); RTStrPrintf(pszDigest, sizeof(abBuf), "%016RX64", uCRC64); break; } case RTDIGESTTYPE_MD2: { RTMD2CONTEXT Ctx; RTMd2Init(&Ctx); for (;;) { rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); if (RT_FAILURE(rc) || !cbRead) break; RTMd2Update(&Ctx, abBuf, cbRead); } uint8_t abDigest[RTMD2_HASH_SIZE]; RTMd2Final(&Ctx, abDigest); RTMd2ToString(abDigest, pszDigest, sizeof(abBuf)); break; } case RTDIGESTTYPE_MD5: { RTMD5CONTEXT Ctx; RTMd5Init(&Ctx); for (;;) { rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); if (RT_FAILURE(rc) || !cbRead) break; RTMd5Update(&Ctx, abBuf, cbRead); } uint8_t abDigest[RTMD5HASHSIZE]; RTMd5Final(abDigest, &Ctx); RTMd5ToString(abDigest, pszDigest, sizeof(abBuf)); break; } case RTDIGESTTYPE_SHA1: { RTSHA1CONTEXT Ctx; RTSha1Init(&Ctx); for (;;) { rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); if (RT_FAILURE(rc) || !cbRead) break; RTSha1Update(&Ctx, abBuf, cbRead); } uint8_t abDigest[RTSHA1_HASH_SIZE]; RTSha1Final(&Ctx, abDigest); RTSha1ToString(abDigest, pszDigest, sizeof(abBuf)); break; } case RTDIGESTTYPE_SHA256: { RTSHA256CONTEXT Ctx; RTSha256Init(&Ctx); for (;;) { rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); if (RT_FAILURE(rc) || !cbRead) break; RTSha256Update(&Ctx, abBuf, cbRead); } uint8_t abDigest[RTSHA256_HASH_SIZE]; RTSha256Final(&Ctx, abDigest); RTSha256ToString(abDigest, pszDigest, sizeof(abBuf)); break; } case RTDIGESTTYPE_SHA512: { RTSHA512CONTEXT Ctx; RTSha512Init(&Ctx); for (;;) { rc = MyReadFile(hFile, abBuf, sizeof(abBuf), &cbRead, &cbMaxLeft); if (RT_FAILURE(rc) || !cbRead) break; RTSha512Update(&Ctx, abBuf, cbRead); } uint8_t abDigest[RTSHA512_HASH_SIZE]; RTSha512Final(&Ctx, abDigest); RTSha512ToString(abDigest, pszDigest, sizeof(abBuf)); break; } default: return Error("Internal error #1\n"); } RTFileClose(hFile); if (RT_FAILURE(rc) && rc != VERR_EOF) { RTPrintf("Partial: %s %s\n", pszDigest, ValueUnion.psz); return Error("RTFileRead(%s) -> %Rrc\n", ValueUnion.psz, rc); } if (!fTestcase) RTPrintf("%s %s\n", pszDigest, ValueUnion.psz); else if (offStart) RTPrintf(" { &g_abRandom72KB[%#4llx], %5llu, \"%s\", \"%s %llu bytes @%llu\" },\n", offStart, cbMax - cbMaxLeft, pszDigest, pszDigestType, offStart, cbMax - cbMaxLeft); else RTPrintf(" { &g_abRandom72KB[0], %5llu, \"%s\", \"%s %llu bytes\" },\n", cbMax - cbMaxLeft, pszDigest, pszDigestType, cbMax - cbMaxLeft); break; } /* * Process a SHS response file: * http://csrc.nist.gov/groups/STM/cavp/index.html#03 */ case kMethod_CVAS: { RTCRDIGEST hDigest; int rc = RTCrDigestCreateByType(&hDigest, enmDigestType); if (RT_FAILURE(rc)) return Error("Failed to create digest calculator for %s: %Rrc", pszDigestType, rc); uint32_t const cbDigest = RTCrDigestGetHashSize(hDigest); if (!cbDigest || cbDigest >= _1K) return Error("Unexpected hash size: %#x\n", cbDigest); PRTSTREAM pFile; rc = RTStrmOpen(ValueUnion.psz, "r", &pFile); if (RT_FAILURE(rc)) return Error("Failed to open CVAS file '%s': %Rrc", ValueUnion.psz, rc); /* * Parse the input file. * ASSUME order: Len, Msg, MD. */ static char s_szLine[_256K]; char *psz; uint32_t cPassed = 0; uint32_t cErrors = 0; uint32_t iLine = 1; for (;;) { psz = MyGetNextSignificantLine(pFile, s_szLine, sizeof(s_szLine), &iLine, &rc); if (!psz) break; /* Skip [L = 20] stuff. */ if (*psz == '[') continue; /* Message length. */ uint64_t cMessageBits; if (RTStrNICmp(psz, RT_STR_TUPLE("Len ="))) return Error("%s(%d): Expected 'Len =' found '%.10s...'", ValueUnion.psz, iLine, psz); psz = RTStrStripL(psz + 5); rc = RTStrToUInt64Full(psz, 0, &cMessageBits); if (rc != VINF_SUCCESS) return Error("%s(%d): Error parsing length '%s': %Rrc\n", ValueUnion.psz, iLine, psz, rc); /* The message text. */ psz = MyGetNextSignificantLine(pFile, s_szLine, sizeof(s_szLine), &iLine, &rc); if (!psz) return Error("%s(%d): Expected message text not EOF.", ValueUnion.psz, iLine); if (RTStrNICmp(psz, RT_STR_TUPLE("Msg ="))) return Error("%s(%d): Expected 'Msg =' found '%.10s...'", ValueUnion.psz, iLine, psz); psz = RTStrStripL(psz + 5); size_t const cbMessage = (cMessageBits + 7) / 8; static uint8_t s_abMessage[sizeof(s_szLine) / 2]; if (cbMessage > 0) { rc = RTStrConvertHexBytes(psz, s_abMessage, cbMessage, 0 /*fFlags*/); if (rc != VINF_SUCCESS) return Error("%s(%d): Error parsing message '%.10s...': %Rrc\n", ValueUnion.psz, iLine, psz, rc); } /* The message digest. */ psz = MyGetNextSignificantLine(pFile, s_szLine, sizeof(s_szLine), &iLine, &rc); if (!psz) return Error("%s(%d): Expected message digest not EOF.", ValueUnion.psz, iLine); if (RTStrNICmp(psz, RT_STR_TUPLE("MD ="))) return Error("%s(%d): Expected 'MD =' found '%.10s...'", ValueUnion.psz, iLine, psz); psz = RTStrStripL(psz + 4); static uint8_t s_abExpectedDigest[_1K]; rc = RTStrConvertHexBytes(psz, s_abExpectedDigest, cbDigest, 0 /*fFlags*/); if (rc != VINF_SUCCESS) return Error("%s(%d): Error parsing message digest '%.10s...': %Rrc\n", ValueUnion.psz, iLine, psz, rc); /* * Do the testing. */ rc = RTCrDigestReset(hDigest); if (rc != VINF_SUCCESS) return Error("RTCrDigestReset failed: %Rrc", rc); rc = RTCrDigestUpdate(hDigest, s_abMessage, cbMessage); if (rc != VINF_SUCCESS) return Error("RTCrDigestUpdate failed: %Rrc", rc); static uint8_t s_abActualDigest[_1K]; rc = RTCrDigestFinal(hDigest, s_abActualDigest, cbDigest); if (rc != VINF_SUCCESS) return Error("RTCrDigestFinal failed: %Rrc", rc); if (memcmp(s_abActualDigest, s_abExpectedDigest, cbDigest) == 0) cPassed++; else { Error("%s(%d): Message digest mismatch. Expected %.*RThxs, got %.*RThxs.", ValueUnion.psz, iLine, cbDigest, s_abExpectedDigest, cbDigest, s_abActualDigest); cErrors++; } } RTStrmClose(pFile); if (cErrors > 0) return Error("Failed: %u error%s (%u passed)", cErrors, cErrors == 1 ? "" : "s", cPassed); RTPrintf("Passed %u test%s.\n", cPassed, cPassed == 1 ? "" : "s"); if (RT_FAILURE(rc)) return Error("Failed: %Rrc", rc); break; } default: return Error("Internal error #2\n"); } break; } default: return RTGetOptPrintError(ch, &ValueUnion); } } return 0; }
int main(int argc, char* argv[]) { wchar_t* ModuleName = L"klick.sys"; ULONG FileSize; MyReadFile ( argv[1], g_File, &FileSize ); PCHAR pKey1, pKey2, pSign; ULONG Key1Size, Key2Size, RegSize; char *pBasePtr = &g_File[0]; CheckForSignOld( pBasePtr, g_File.size(), &FileSize ); GetCryptoCPtrs( pBasePtr, FileSize, &pKey1, &Key1Size, &pKey2, &Key2Size, &pSign, &RegSize, &FileSize ); PIMAGE_DOS_HEADER Dos_header = (PIMAGE_DOS_HEADER)pBasePtr; PIMAGE_NT_HEADERS PE_header = (PIMAGE_NT_HEADERS)( (char*)pBasePtr + Dos_header->e_lfanew ); PE_header->OptionalHeader.CheckSum = 0; bool RetVal = false; HDSKM libDSKM; ULONG dskm_err = DSKM_ERR_OK ; libDSKM = DSKM_InitLibraryEx(Kl1Alloc, Kl1Free, NULL, TRUE) ; HDSKMLIST list1; dskm_err = DSKM_ParList_Create(&list1); CReadObj Key1( pKey1, Key1Size ); CReadObj Key2( pKey2, Key2Size ); CReadObj Sign( pSign, RegSize ); HDSKMLISTOBJ param1 = DSKM_ParList_AddBufferedReg(list1, 0, Key1.m_pBase, Key1.m_Size, (pfnDSKM_GetBuffer_CallBack)ReadBuffByFile, &Key1 ) ; HDSKMLISTOBJ param2 = DSKM_ParList_AddBufferedReg(list1, 0, Key2.m_pBase, Key2.m_Size, (pfnDSKM_GetBuffer_CallBack)ReadBuffByFile, &Key2 ) ; HDSKMLISTOBJ param3 = DSKM_ParList_AddBufferedReg(list1, 0, Sign.m_pBase, Sign.m_Size, (pfnDSKM_GetBuffer_CallBack)ReadBuffByFile, &Sign ) ; dskm_err = DSKM_PrepareRegsSet(libDSKM, list1, 0); dskm_err = DSKM_ParList_Delete(list1) ; // Check HDSKMLIST list2 = 0 ; DSKM_ParList_Create(&list2); CReadObj File( pBasePtr, FileSize ); HDSKMLISTOBJ param4 = DSKM_ParList_AddBufferedObject(list2, 0, File.m_pBase, File.m_Size, (pfnDSKM_GetBuffer_CallBack)ReadBuffByFile, &File ) ; dskm_err = DSKM_ParList_SetObjectHashingProp(list2, param4, ModuleName, 2 *( wcslen ( ModuleName ) + 1 ) ) ; dskm_err = DSKM_CheckObjectsUsingRegsSet( libDSKM, list2, 0) ; if ( dskm_err == DSKM_ERR_OK ) RetVal = true; DSKM_ParList_Delete(list2) ; DSKM_DeInitLibrary( libDSKM, TRUE ) ; return 0; }
static int Decode(FILE *inFile, FILE *outFile, char *rs) { UInt64 unpackSize; int thereIsSize; /* = 1, if there is uncompressed size in headers */ int i; int res = 0; CLzmaDec state; /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ unsigned char header[LZMA_PROPS_SIZE + 8]; /* Read and parse header */ if (!MyReadFileAndCheck(inFile, header, sizeof(header))) return PrintError(rs, kCantReadMessage); unpackSize = 0; thereIsSize = 0; for (i = 0; i < 8; i++) { unsigned char b = header[LZMA_PROPS_SIZE + i]; if (b != 0xFF) thereIsSize = 1; unpackSize += (UInt64)b << (i * 8); } LzmaDec_Construct(&state); res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc); if (res != SZ_OK) return res; { Byte inBuf[IN_BUF_SIZE]; Byte outBuf[OUT_BUF_SIZE]; size_t inPos = 0, inSize = 0, outPos = 0; LzmaDec_Init(&state); for (;;) { if (inPos == inSize) { inSize = MyReadFile(inFile, inBuf, IN_BUF_SIZE); inPos = 0; } { SizeT inProcessed = inSize - inPos; SizeT outProcessed = OUT_BUF_SIZE - outPos; ELzmaFinishMode finishMode = LZMA_FINISH_ANY; ELzmaStatus status; if (thereIsSize && outProcessed > unpackSize) { outProcessed = (SizeT)unpackSize; finishMode = LZMA_FINISH_END; } res = LzmaDec_DecodeToBuf(&state, outBuf + outPos, &outProcessed, inBuf + inPos, &inProcessed, finishMode, &status); inPos += (UInt32)inProcessed; outPos += outProcessed; unpackSize -= outProcessed; if (outFile != 0) MyWriteFile(outFile, outBuf, outPos); outPos = 0; if (res != SZ_OK || thereIsSize && unpackSize == 0) break; if (inProcessed == 0 && outProcessed == 0) { if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK) res = SZ_ERROR_DATA; break; } } } } LzmaDec_Free(&state, &g_Alloc); return res; }
int MyReadFileAndCheck(FILE *file, void *data, size_t size) { return (MyReadFile(file, data, size) == size); }
int main2(int numargs, const char *args[], char *rs) { FILE *inputHandle, *outputHandle; unsigned int length, processedSize; unsigned int compressedSize, outSize, outSizeProcessed, lzmaInternalSize; void *inStream, *outStream, *lzmaInternalData; unsigned char properties[5]; unsigned char prop0; int ii; int lc, lp, pb; int res; #ifdef _LZMA_IN_CB CBuffer bo; #endif sprintf(rs + strlen(rs), "\nLZMA Decoder 4.02 Copyright (c) 1999-2004 Igor Pavlov 2004-06-10\n"); if (numargs < 2 || numargs > 3) { sprintf(rs + strlen(rs), "\nUsage: lzmaDec file.lzma [outFile]\n"); return 1; } inputHandle = fopen(args[1], "rb"); if (inputHandle == 0) { sprintf(rs + strlen(rs), "\n Open input file error"); return 1; } fseek(inputHandle, 0, SEEK_END); length = ftell(inputHandle); fseek(inputHandle, 0, SEEK_SET); if (!MyReadFile(inputHandle, properties, sizeof(properties))) return 1; outSize = 0; for (ii = 0; ii < 4; ii++) { unsigned char b; if (!MyReadFile(inputHandle, &b, sizeof(b))) return 1; outSize += (unsigned int)(b) << (ii * 8); } if (outSize == 0xFFFFFFFF) { sprintf(rs + strlen(rs), "\nstream version is not supported"); return 1; } for (ii = 0; ii < 4; ii++) { unsigned char b; if (!MyReadFile(inputHandle, &b, sizeof(b))) return 1; if (b != 0) { sprintf(rs + strlen(rs), "\n too long file"); return 1; } } compressedSize = length - 13; inStream = malloc(compressedSize); if (inStream == 0) { sprintf(rs + strlen(rs), "\n can't allocate"); return 1; } if (!MyReadFile(inputHandle, inStream, compressedSize)) { sprintf(rs + strlen(rs), "\n can't read"); return 1; } fclose(inputHandle); prop0 = properties[0]; if (prop0 >= (9*5*5)) { sprintf(rs + strlen(rs), "\n Properties error"); return 1; } for (pb = 0; prop0 >= (9 * 5); pb++, prop0 -= (9 * 5)); for (lp = 0; prop0 >= 9; lp++, prop0 -= 9); lc = prop0; lzmaInternalSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb); #ifdef _LZMA_OUT_READ lzmaInternalSize += 100; #endif outStream = malloc(outSize); lzmaInternalData = malloc(lzmaInternalSize); if (outStream == 0 || lzmaInternalData == 0) { sprintf(rs + strlen(rs), "\n can't allocate"); return 1; } #ifdef _LZMA_IN_CB bo.InCallback.Read = LzmaReadCompressed; bo.Buffer = (unsigned char *)inStream; bo.Size = compressedSize; #endif #ifdef _LZMA_OUT_READ { UInt32 nowPos; unsigned char *dictionary; UInt32 dictionarySize = 0; int i; for (i = 0; i < 4; i++) dictionarySize += (UInt32)(properties[1 + i]) << (i * 8); dictionary = malloc(dictionarySize); if (dictionary == 0) { sprintf(rs + strlen(rs), "\n can't allocate"); return 1; } LzmaDecoderInit((unsigned char *)lzmaInternalData, lzmaInternalSize, lc, lp, pb, dictionary, dictionarySize, #ifdef _LZMA_IN_CB &bo.InCallback #else (unsigned char *)inStream, compressedSize #endif ); for (nowPos = 0; nowPos < outSize;) { UInt32 blockSize = outSize - nowPos; UInt32 kBlockSize = 0x10000; if (blockSize > kBlockSize) blockSize = kBlockSize; res = LzmaDecode((unsigned char *)lzmaInternalData, ((unsigned char *)outStream) + nowPos, blockSize, &outSizeProcessed); if (res != 0) { sprintf(rs + strlen(rs), "\nerror = %d\n", res); return 1; } if (outSizeProcessed == 0) { outSize = nowPos; break; } nowPos += outSizeProcessed; } free(dictionary); } #else res = LzmaDecode((unsigned char *)lzmaInternalData, lzmaInternalSize, lc, lp, pb, #ifdef _LZMA_IN_CB &bo.InCallback, #else (unsigned char *)inStream, compressedSize, #endif (unsigned char *)outStream, outSize, &outSizeProcessed); outSize = outSizeProcessed; #endif if (res != 0) { sprintf(rs + strlen(rs), "\nerror = %d\n", res); return 1; } if (numargs > 2) { outputHandle = fopen(args[2], "wb+"); if (outputHandle == 0) { sprintf(rs + strlen(rs), "\n Open output file error"); return 1; } processedSize = fwrite(outStream, 1, outSize, outputHandle); if (processedSize != outSize) { sprintf(rs + strlen(rs), "\n can't write"); return 1; } fclose(outputHandle); } free(lzmaInternalData); free(outStream); free(inStream); return 0; }