Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
File_Hasher.cpp
Go to the documentation of this file.
4
5#include <vector>
6
7namespace joedb
8{
9 ////////////////////////////////////////////////////////////////////////////
11 ////////////////////////////////////////////////////////////////////////////
12 (
13 const Abstract_File &file,
14 int64_t start,
15 int64_t size
16 )
17 {
18 SHA_256 sha_256;
19
20 constexpr uint32_t chunks = 2048;
21 std::vector<char> hashing_buffer(SHA_256::chunk_size * chunks);
22
23 int64_t current_size = 0;
24
25 while (true)
26 {
27 size_t block_size = SHA_256::chunk_size * chunks;
28 if (current_size + int64_t(block_size) > size)
29 block_size = size_t(size - current_size);
30
31 const size_t read_count = file.pread(hashing_buffer.data(), block_size, start + current_size);
32 current_size += int64_t(read_count);
33 const uint32_t full_chunks = uint32_t(read_count / SHA_256::chunk_size);
34 for (uint32_t i = 0; i < full_chunks; i++)
35 sha_256.process_chunk(&hashing_buffer[i * SHA_256::chunk_size]);
36
37 const uint32_t remainder = uint32_t(read_count % SHA_256::chunk_size);
38 if (remainder || current_size >= size || read_count == 0)
39 {
41 (
42 &hashing_buffer[full_chunks * SHA_256::chunk_size],
43 uint64_t(current_size)
44 );
45 break;
46 }
47 }
48
49 return sha_256.get_hash();
50 }
51
52 ////////////////////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////////////////////
55 {
56 return File_Hasher::get_hash(file, 0, file.get_size());
57 }
58
59 ////////////////////////////////////////////////////////////////////////////
61 ////////////////////////////////////////////////////////////////////////////
62 {
63 Readonly_Memory_File file(s.data(), s.size());
64 return File_Hasher::get_hash(file);
65 }
66
67 ////////////////////////////////////////////////////////////////////////////
69 ////////////////////////////////////////////////////////////////////////////
70 (
71 const Abstract_File &file,
72 int64_t start,
73 int64_t size
74 )
75 {
76 std::array<char, 1 << 12> buffer;
77
78 constexpr int buffer_count = 256;
79
80 if (size < 4 * int64_t(buffer.size()) * buffer_count)
81 return get_hash(file, start, size);
82
83 SHA_256 sha_256;
84
85 for (int i = 0; i < buffer_count; i++)
86 {
87 int64_t buffer_position;
88
89 if (i == 0)
90 buffer_position = start;
91 else if (i == buffer_count - 1)
92 buffer_position = start + size - int64_t(buffer.size());
93 else
94 {
95 buffer_position = int64_t(buffer.size()) *
96 (
97 (start + i * size) / (int64_t(buffer.size()) * (buffer_count - 1))
98 );
99 }
100
101 file.pread(buffer.data(), buffer.size(), buffer_position);
102
103 for (size_t j = 0; j < buffer.size(); j += SHA_256::chunk_size)
104 sha_256.process_chunk(buffer.data() + j);
105 }
106
107 return sha_256.get_hash();
108 }
109
110 ////////////////////////////////////////////////////////////////////////////
112 ////////////////////////////////////////////////////////////////////////////
113 (
114 const Readonly_Journal &journal,
115 int64_t checkpoint
116 )
117 {
119 (
120 journal.get_file(),
122 checkpoint - Header::ssize
123 );
124 }
125
126 ////////////////////////////////////////////////////////////////////////////
128 ////////////////////////////////////////////////////////////////////////////
129 (
130 const Readonly_Journal &journal,
131 int64_t checkpoint
132 )
133 {
135 (
136 journal.get_file(),
138 checkpoint - Header::ssize
139 );
140 }
141}
virtual int64_t get_size() const
Get the size of the file, or -1 if it is unknown.
static SHA_256::Hash get_fast_hash(const Abstract_File &file, int64_t start, int64_t size)
static SHA_256::Hash get_hash(const Abstract_File &file, int64_t start, int64_t size)
static SHA_256::Hash get_full_hash(const Readonly_Journal &journal, int64_t checkpoint)
static SHA_256::Hash get_fast_hash(const Readonly_Journal &journal, int64_t checkpoint)
Compute SHA 256 hash code: https://en.wikipedia.org/wiki/SHA-2.
Definition SHA_256.h:20
const Hash & get_hash() const
Definition SHA_256.h:66
void process_chunk(const char *data)
process 512 bits (32 * 16, 8 * 64) of data, SHA_256::chunk_size bytes
Definition SHA_256.h:70
static constexpr size_t chunk_size
Definition SHA_256.h:67
std::array< uint32_t, 8 > Hash
Definition SHA_256.h:59
void process_final_chunk(const char *const data, const uint64_t total_length_in_bytes)
process last bytes of the sequence
Definition SHA_256.h:125
static constexpr int64_t ssize
Definition Header.h:18
static constexpr size_t size
Definition Header.h:19