Mercurial > vba-linux
view 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 source
1 // FindSignature.cpp3 #include "StdAfx.h"5 #include "Common/Buffer.h"7 #include "FindSignature.h"9 #include "../../Common/StreamUtils.h"11 HRESULT FindSignatureInStream(ISequentialInStream *stream,12 const Byte *signature, unsigned signatureSize,13 const UInt64 *limit, UInt64 &resPos)14 {15 resPos = 0;16 CByteBuffer byteBuffer2;17 byteBuffer2.SetCapacity(signatureSize);18 RINOK(ReadStream_FALSE(stream, byteBuffer2, signatureSize));20 if (memcmp(byteBuffer2, signature, signatureSize) == 0)21 return S_OK;23 const UInt32 kBufferSize = (1 << 16);24 CByteBuffer byteBuffer;25 byteBuffer.SetCapacity(kBufferSize);26 Byte *buffer = byteBuffer;27 UInt32 numPrevBytes = signatureSize - 1;28 memcpy(buffer, (const Byte *)byteBuffer2 + 1, numPrevBytes);29 resPos = 1;30 for (;;)31 {32 if (limit != NULL)33 if (resPos > *limit)34 return S_FALSE;35 do36 {37 UInt32 numReadBytes = kBufferSize - numPrevBytes;38 UInt32 processedSize;39 RINOK(stream->Read(buffer + numPrevBytes, numReadBytes, &processedSize));40 numPrevBytes += processedSize;41 if (processedSize == 0)42 return S_FALSE;43 }44 while (numPrevBytes < signatureSize);45 UInt32 numTests = numPrevBytes - signatureSize + 1;46 for (UInt32 pos = 0; pos < numTests; pos++)47 {48 Byte b = signature[0];49 for (; buffer[pos] != b && pos < numTests; pos++);50 if (pos == numTests)51 break;52 if (memcmp(buffer + pos, signature, signatureSize) == 0)53 {54 resPos += pos;55 return S_OK;56 }57 }58 resPos += numTests;59 numPrevBytes -= numTests;60 memmove(buffer, buffer + numTests, numPrevBytes);61 }62 }