1 // FindSignature.cpp
2
3 #include "StdAfx.h"
4
5 #include <string.h>
6
7 #include "../../../Common/MyBuffer.h"
8
9 #include "../../Common/StreamUtils.h"
10
11 #include "FindSignature.h"
12
FindSignatureInStream(ISequentialInStream * stream,const Byte * signature,unsigned signatureSize,const UInt64 * limit,UInt64 & resPos)13 HRESULT FindSignatureInStream(ISequentialInStream *stream,
14 const Byte *signature, unsigned signatureSize,
15 const UInt64 *limit, UInt64 &resPos)
16 {
17 resPos = 0;
18 CByteBuffer byteBuffer2(signatureSize);
19 RINOK(ReadStream_FALSE(stream, byteBuffer2, signatureSize))
20
21 if (memcmp(byteBuffer2, signature, signatureSize) == 0)
22 return S_OK;
23
24 const size_t kBufferSize = 1 << 16;
25 CByteBuffer byteBuffer(kBufferSize);
26 Byte *buffer = byteBuffer;
27 size_t numPrevBytes = signatureSize - 1;
28 memcpy(buffer, (const Byte *)byteBuffer2 + 1, numPrevBytes);
29 resPos = 1;
30 for (;;)
31 {
32 if (limit)
33 if (resPos > *limit)
34 return S_FALSE;
35 do
36 {
37 const size_t numReadBytes = kBufferSize - numPrevBytes;
38 UInt32 processedSize;
39 RINOK(stream->Read(buffer + numPrevBytes, (UInt32)numReadBytes, &processedSize))
40 numPrevBytes += (size_t)processedSize;
41 if (processedSize == 0)
42 return S_FALSE;
43 }
44 while (numPrevBytes < signatureSize);
45 const size_t numTests = numPrevBytes - signatureSize + 1;
46 for (size_t pos = 0; pos < numTests; pos++)
47 {
48 const 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 }
63
64 namespace NArchive {
65 HRESULT ReadZeroTail(ISequentialInStream *stream, bool &areThereNonZeros, UInt64 &numZeros, UInt64 maxSize);
ReadZeroTail(ISequentialInStream * stream,bool & areThereNonZeros,UInt64 & numZeros,UInt64 maxSize)66 HRESULT ReadZeroTail(ISequentialInStream *stream, bool &areThereNonZeros, UInt64 &numZeros, UInt64 maxSize)
67 {
68 areThereNonZeros = false;
69 numZeros = 0;
70 const size_t kBufSize = 1 << 11;
71 Byte buf[kBufSize];
72 for (;;)
73 {
74 UInt32 size = 0;
75 RINOK(stream->Read(buf, kBufSize, &size))
76 if (size == 0)
77 return S_OK;
78 for (UInt32 i = 0; i < size; i++)
79 if (buf[i] != 0)
80 {
81 areThereNonZeros = true;
82 numZeros += i;
83 return S_OK;
84 }
85 numZeros += size;
86 if (numZeros > maxSize)
87 return S_OK;
88 }
89 }
90 }
91