Skip to content

Commit 3f5568e

Browse files
committed
ported zsync meta file parsing
1 parent 1ef1f3a commit 3f5568e

11 files changed

+598
-0
lines changed

Diff for: CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.4)
2+
project(zsync3)
3+
4+
set(V_MAJOR 0)
5+
set(V_MINOR 1)
6+
set(V_PATCH 0)
7+
set(V_SUFFIX "")
8+
9+
set(zsync3_VERSION ${V_MAJOR}.${V_MINOR}.${V_PATCH}${V_SUFFIX})
10+
set(zsync3_SOVERSION ${V_MAJOR}.${V_MINOR})
11+
12+
set(CMAKE_CXX_STANDARD 14)
13+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
14+
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
15+
set(CMAKE_CXX_FLAGS_DEBUG "-g")
16+
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
17+
18+
include_directories(.)
19+
add_subdirectory(src)
20+
add_subdirectory(tools)

Diff for: Zsync3

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//// Proxy file
2+
#include "include/client.hpp"

Diff for: include/client.hpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef ZSYNC3_CLIENT_HPP_INCLUDED
2+
#define ZSYNC3_CLIENT_HPP_INCLUDED
3+
#include <memory>
4+
#include <string>
5+
6+
namespace Zsync3 {
7+
class ClientPrivate;
8+
class Client {
9+
std::unique_ptr<ClientPrivate> d;
10+
11+
public:
12+
Client();
13+
Client(const std::string&);
14+
Client(const Client&);
15+
~Client();
16+
17+
bool SetMetaFile(const std::string&);
18+
19+
bool SubmitSeedFile(const std::string&);
20+
};
21+
}
22+
23+
#endif // ZSYNC3_CLIENT_HPP_INCLUDED

Diff for: include/client_p.hpp

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Diff for: src/CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.4)
2+
3+
set(source)
4+
list(APPEND source
5+
client_p.cpp
6+
client.cpp
7+
rollingchecksum.cpp)
8+
9+
if(BUILD_SHARED_LIB)
10+
add_library(zsync3 SHARED ${source})
11+
endif()
12+
13+
if(NOT BUILD_SHARED_LIB)
14+
add_library(zsync3 ${source})
15+
endif()
16+
17+
target_link_libraries(zsync3 crypto pthread)

Diff for: src/client.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "../include/client.hpp"
2+
#include "../include/client_p.hpp"
3+
4+
using namespace Zsync3;
5+
6+
Client::Client() {
7+
d.reset(new ClientPrivate);
8+
}
9+
10+
Client::Client(const Client &other) {
11+
this->d.reset(other.d.get());
12+
}
13+
14+
Client::Client(const std::string &meta_file_path)
15+
: Client() {
16+
SetMetaFile(meta_file_path);
17+
}
18+
19+
Client::~Client() { }
20+
21+
bool Client::SetMetaFile(const std::string &path) {
22+
return d->SetMetaFile(path);
23+
}
24+
25+
bool Client::SubmitSeedFile(const std::string &path) {
26+
return d->SubmitSeedFile(path);
27+
}

0 commit comments

Comments
 (0)