Mercurial > vba-linux
diff src/win32/7zip/7z/CPP/7zip/Archive/Common/FindSignature.cpp @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/FindSignature.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,62 @@ 1.4 +// FindSignature.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#include "Common/Buffer.h" 1.9 + 1.10 +#include "FindSignature.h" 1.11 + 1.12 +#include "../../Common/StreamUtils.h" 1.13 + 1.14 +HRESULT FindSignatureInStream(ISequentialInStream *stream, 1.15 + const Byte *signature, unsigned signatureSize, 1.16 + const UInt64 *limit, UInt64 &resPos) 1.17 +{ 1.18 + resPos = 0; 1.19 + CByteBuffer byteBuffer2; 1.20 + byteBuffer2.SetCapacity(signatureSize); 1.21 + RINOK(ReadStream_FALSE(stream, byteBuffer2, signatureSize)); 1.22 + 1.23 + if (memcmp(byteBuffer2, signature, signatureSize) == 0) 1.24 + return S_OK; 1.25 + 1.26 + const UInt32 kBufferSize = (1 << 16); 1.27 + CByteBuffer byteBuffer; 1.28 + byteBuffer.SetCapacity(kBufferSize); 1.29 + Byte *buffer = byteBuffer; 1.30 + UInt32 numPrevBytes = signatureSize - 1; 1.31 + memcpy(buffer, (const Byte *)byteBuffer2 + 1, numPrevBytes); 1.32 + resPos = 1; 1.33 + for (;;) 1.34 + { 1.35 + if (limit != NULL) 1.36 + if (resPos > *limit) 1.37 + return S_FALSE; 1.38 + do 1.39 + { 1.40 + UInt32 numReadBytes = kBufferSize - numPrevBytes; 1.41 + UInt32 processedSize; 1.42 + RINOK(stream->Read(buffer + numPrevBytes, numReadBytes, &processedSize)); 1.43 + numPrevBytes += processedSize; 1.44 + if (processedSize == 0) 1.45 + return S_FALSE; 1.46 + } 1.47 + while (numPrevBytes < signatureSize); 1.48 + UInt32 numTests = numPrevBytes - signatureSize + 1; 1.49 + for (UInt32 pos = 0; pos < numTests; pos++) 1.50 + { 1.51 + Byte b = signature[0]; 1.52 + for (; buffer[pos] != b && pos < numTests; pos++); 1.53 + if (pos == numTests) 1.54 + break; 1.55 + if (memcmp(buffer + pos, signature, signatureSize) == 0) 1.56 + { 1.57 + resPos += pos; 1.58 + return S_OK; 1.59 + } 1.60 + } 1.61 + resPos += numTests; 1.62 + numPrevBytes -= numTests; 1.63 + memmove(buffer, buffer + numTests, numPrevBytes); 1.64 + } 1.65 +}