|
| 1 | +#ifndef ZSYNC3_CLIENT_PRIVATE_HPP_INCLUDED |
| 2 | +#define ZSYNC3_CLIENT_PRIVATE_HPP_INCLUDED |
| 3 | +#include <cstdint> |
| 4 | +#include <cstddef> |
| 5 | +#include <boost/filesystem.hpp> |
| 6 | +#include <openssl/sha.h> |
| 7 | +#include <openssl/md4.h> |
| 8 | +#include <fstream> |
| 9 | +#include <memory> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace Zsync3 { |
| 14 | +class ClientPrivate { |
| 15 | + /// Constants |
| 16 | + const int kBitHashBits = 3; |
| 17 | + /// --- |
| 18 | + |
| 19 | + class RollingChecksum { |
| 20 | + uint16_t a,b; |
| 21 | + public: |
| 22 | + RollingChecksum(); |
| 23 | + //// Special Constructor which expects the given variables as |
| 24 | + //// A anb B which is in network endian order. |
| 25 | + RollingChecksum(uint16_t, uint16_t); |
| 26 | + RollingChecksum(const uint8_t*, std::size_t); |
| 27 | + RollingChecksum(const RollingChecksum&); |
| 28 | + uint16_t GetA(); |
| 29 | + uint16_t GetB(); |
| 30 | + void update(uint8_t,uint8_t,int32_t); |
| 31 | + |
| 32 | + void operator = (const RollingChecksum&); |
| 33 | + bool operator == (const RollingChecksum&); |
| 34 | + bool operator != (const RollingChecksum&); |
| 35 | + }; |
| 36 | + |
| 37 | + struct HashEntry { |
| 38 | + int64_t block_id = 0; |
| 39 | + RollingChecksum rsum; |
| 40 | + std::string md4; |
| 41 | + |
| 42 | + //// Next hash entry with the same rsum |
| 43 | + HashEntry *next = nullptr; |
| 44 | + }; |
| 45 | + |
| 46 | + int64_t num_blocks; |
| 47 | + int blocksize = 0, |
| 48 | + blockshift = 0; /// log2(blocksize) |
| 49 | + int64_t num_bytes_written = 0, |
| 50 | + context = 0, /* precalculated blocksize * seq_matches */ |
| 51 | + num_weak_checksum_bytes = 0, |
| 52 | + num_strong_checksum_bytes = 0, /* no. of bytes available for the strong checksum. */ |
| 53 | + num_seq_matches = 0, |
| 54 | + target_file_length = 0; |
| 55 | + |
| 56 | + int64_t bit_hash_mask = 0; |
| 57 | + int64_t hash_mask = 0; |
| 58 | + uint16_t weak_checksum_mask = 0; /* This will be applied to the first 16 bits of the weak checksum. */ |
| 59 | + |
| 60 | + //// Version string. |
| 61 | + std::string zs_version; |
| 62 | + |
| 63 | + //// Date Time string. |
| 64 | + std::string mtime; |
| 65 | + |
| 66 | + //// Has all block's hashes. |
| 67 | + std::unique_ptr<std::vector<HashEntry>> block_hashes; |
| 68 | + |
| 69 | + //// Has the mapping of rsum to specific HashEntry present in the |
| 70 | + //// block_hashes. |
| 71 | + //// The HashEntry is a chain which is a collection of same rsum |
| 72 | + //// mapping. |
| 73 | + std::unique_ptr<std::vector<HashEntry*>> rsum_mapping; |
| 74 | + |
| 75 | + /// This bit hash as 1 bit per rsum value. If a rsum value exists in the |
| 76 | + /// target file then we will have the relevant bit set to 1 if not |
| 77 | + /// 0. So this provides us a fast negative lookup. |
| 78 | + std::unique_ptr<std::vector<uint8_t>> bit_hashes; |
| 79 | + |
| 80 | + std::unique_ptr<MD4_CTX> md4_ctx; |
| 81 | + std::unique_ptr<SHA_CTX> sha1_ctx; |
| 82 | + std::unique_ptr<std::fstream> temp_fs; |
| 83 | + |
| 84 | + std::string target_file_sha1; |
| 85 | + std::string target_file_url; |
| 86 | + std::string target_filename; |
| 87 | + boost::filesystem::path temp_filename; |
| 88 | + |
| 89 | + public: |
| 90 | + ClientPrivate(); |
| 91 | + ~ClientPrivate(); |
| 92 | + |
| 93 | + //// Read a Zsync Meta file and create all neccessary |
| 94 | + //// data structures to be constructed. |
| 95 | + bool SetMetaFile(const std::string&); |
| 96 | + |
| 97 | + //// Read a file and write useable blocks |
| 98 | + //// to temporary target file. |
| 99 | + bool SubmitSeedFile(const std::string&); |
| 100 | + private: |
| 101 | + |
| 102 | + //// The mapping function for the rsum hashes. |
| 103 | + //// This function returns the index in where the HashEntry |
| 104 | + //// resides in the rsum_hashes. |
| 105 | + uint32_t RsumHash(HashEntry&); |
| 106 | + |
| 107 | + //// Builds the mapping of rsum to hash entries |
| 108 | + bool BuildRsumHashTable(); |
| 109 | + |
| 110 | + //// Submit a fragment of data to be zsynced. |
| 111 | + int64_t SubmitSourceData(const char *, size_t, int64_t); |
| 112 | +}; |
| 113 | +} |
| 114 | + |
| 115 | +#endif |
0 commit comments