| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// ChunkedFile.h |
| 3 |
//--------------------------------------------------------------------------- |
| 4 |
// -=* VPatch *=- |
| 5 |
//--------------------------------------------------------------------------- |
| 6 |
// Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions |
| 7 |
//--------------------------------------------------------------------------- |
| 8 |
// Website: http://www.tibed.net/vpatch |
| 9 |
// |
| 10 |
// This software is provided 'as-is', without any express or implied |
| 11 |
// warranty. In no event will the authors be held liable for any damages |
| 12 |
// arising from the use of this software. |
| 13 |
// |
| 14 |
// Permission is granted to anyone to use this software for any purpose, |
| 15 |
// including commercial applications, and to alter it and redistribute it |
| 16 |
// freely, subject to the following restrictions: |
| 17 |
// |
| 18 |
// 1. The origin of this software must not be misrepresented; you must not |
| 19 |
// claim that you wrote the original software. If you use this software |
| 20 |
// in a product, an acknowledgment in the product documentation would be |
| 21 |
// appreciated but is not required. |
| 22 |
// 2. Altered source versions must be plainly marked as such, and must not be |
| 23 |
// misrepresented as being the original software. |
| 24 |
// 3. This notice may not be removed or altered from any source distribution. |
| 25 |
// |
| 26 |
// Reviewed for Unicode support by Jim Park -- 08/29/2007 |
| 27 |
|
| 28 |
#if !defined(ChunkedFile_H) |
| 29 |
#define ChunkedFile_H |
| 30 |
|
| 31 |
#include "GlobalTypes.h" |
| 32 |
#include "adler32.h" |
| 33 |
#include <iostream> |
| 34 |
#include <algorithm> |
| 35 |
|
| 36 |
// private data type: the data tree information |
| 37 |
typedef struct TChunkChecksum { |
| 38 |
Checksum::uLong adler32; |
| 39 |
CHECKSUM_BLOCK v; |
| 40 |
} TChunkChecksum; |
| 41 |
|
| 42 |
inline bool operator<(const TChunkChecksum& a, const TChunkChecksum& b) { |
| 43 |
return (a.adler32 < b.adler32) ? true : ( |
| 44 |
(a.adler32 == b.adler32) ? (a.v < b.v) : false |
| 45 |
); |
| 46 |
} |
| 47 |
inline bool operator==(const TChunkChecksum& a, const TChunkChecksum& b) { |
| 48 |
return (a.v == b.v) && (a.adler32 == b.adler32); |
| 49 |
} |
| 50 |
|
| 51 |
typedef struct FileChunk { |
| 52 |
TFileOffset offset; |
| 53 |
TChunkChecksum checksum; |
| 54 |
} FileChunk; |
| 55 |
|
| 56 |
inline bool operator<(const FileChunk& a, const FileChunk& b) { |
| 57 |
return a.checksum < b.checksum; |
| 58 |
} |
| 59 |
|
| 60 |
class ChunkedFile { |
| 61 |
public: |
| 62 |
TFileOffset chunkCount; |
| 63 |
FileChunk* chunks; |
| 64 |
|
| 65 |
ChunkedFile(bistream& f, TFileOffset fSize, TFileOffset chunkSize); |
| 66 |
|
| 67 |
~ChunkedFile() { |
| 68 |
if(chunks != NULL) delete[] chunks; |
| 69 |
} |
| 70 |
|
| 71 |
bool search(TChunkChecksum key, TFileOffset* start); |
| 72 |
|
| 73 |
inline void calculateChecksum(unsigned char* data, TFileOffset size, TChunkChecksum& K) { |
| 74 |
K.v = *reinterpret_cast<CHECKSUM_BLOCK*>(data); |
| 75 |
K.adler32 = Checksum::adler32(1L,data,size); |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
#endif // ChunkedFile_H |