forked from defuse/crackstation-hashdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filearray.cpp
188 lines (144 loc) · 5.04 KB
/
filearray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "filearray.h"
FileArray::posType FileArray::getFileSize( const std::string & fileName ) {
return std::ifstream( fileName, std::ios::in | std::ios::out | std::ios::binary | std::ios::ate ).tellg();
}
FileArray::posType FileArray::getSize( const std::string & fileName ) {
return getFileSize( fileName ) / FileArray::IndexEntry::indexSize;
}
FileArray::FileArray( const std::string & fileName, bool autoLoad ) :
FileArray( fileName, 0, NULL, autoLoad ) {}
FileArray::FileArray( const std::string & fileName, FileArray::posType cacheSize, bool autoLoad ) :
FileArray( fileName, cacheSize, NULL, autoLoad ) {}
FileArray::FileArray( const std::string & fileName, ProgressBar* progressBar, bool autoLoad ) :
FileArray( fileName, 0, progressBar, autoLoad ) {}
FileArray::FileArray( const std::string & fileName, posType cacheSize, ProgressBar* progressBar, bool autoLoad ) :
file( fileName, std::ios::in | std::ios::out | std::ios::binary | std::ios::ate ),
fileSize( file.tellg() ),
size( fileSize / FileArray::IndexEntry::indexSize ),
cacheSize( std::min( cacheSize, size ) ),
cache( this->cacheSize ),
progressBar( progressBar ),
autoLoad( autoLoad ) {
if ( !file.good() ) {
throw std::invalid_argument( "File \"" + fileName + "\" does not exist!" );
} else if ( (FileArray::IndexEntry::indexSize * size) != fileSize ) {
throw std::invalid_argument(
"The file size needs to be divisible by " + std::to_string( FileArray::IndexEntry::indexSize ) + "!\n" +
"File size of \"" + fileName + "\" is " + std::to_string( fileSize ) + "!" );
}
if ( autoLoad )
loadCacheFromFile();
}
FileArray::~FileArray() {
if ( autoLoad )
writeCacheToFile();
}
FileArray::posType FileArray::getFileSize() const {
return fileSize;
}
FileArray::posType FileArray::getSize() const {
return size;
}
FileArray::posType FileArray::getCacheSize() const {
return cacheSize;
}
void FileArray::readEntry( IndexEntry & entry, FileArray::posType index ) {
if ( index < cacheSize ) {
entry = cache[index];
} else if ( index < size ) {
if ( readPos != index )
file.seekg( FileArray::IndexEntry::indexSize * (readPos = index) );
file >> entry;
++readPos;
}
}
void FileArray::writeEntry( const IndexEntry & entry, FileArray::posType index ) {
if ( index < cacheSize ) {
cache[index] = entry;
} else if ( index < size ) {
if ( writePos != index )
file.seekp( FileArray::IndexEntry::indexSize * (writePos = index) );
file << entry;
++writePos;
}
}
void FileArray::loadCacheFromFile() {
size_t i = 0;
file.seekg( 0 );
for ( IndexEntry & entry : cache ) {
file >> entry;
if ( progressBar != NULL )
progressBar->updateProgress( 0, ++i, cacheSize );
}
}
void FileArray::writeCacheToFile() {
const size_t lastSegment = (progressBar != NULL) ? (progressBar->getNumSegments() - 1) : 0;
size_t i = 0;
file.seekp( 0 );
for ( const IndexEntry & entry : cache ) {
file << entry;
if ( progressBar != NULL )
progressBar->updateProgress( lastSegment, ++i, cacheSize );
}
}
void FileArray::IndexEntry::setHash( const HashLib::Hash & hashSource ) {
for ( size_t i = 0; i < FileArray::IndexEntry::hashSize; i++ ) {
hash[i] = hashSource[i];
}
}
void FileArray::IndexEntry::setOffset( std::streampos pos ) {
for ( size_t i = 0; i < FileArray::IndexEntry::offsetSize; i++ ) {
offset[i] = getNthByte( pos, i );
}
}
std::streampos FileArray::IndexEntry::getOffset() const {
std::streampos pos = 0;
for ( size_t i = 0; i < FileArray::IndexEntry::offsetSize; i++ ) {
pos = (pos << 8) | offset[FileArray::IndexEntry::offsetSize - i - 1];
}
return pos;
}
FileArray::IndexEntry& FileArray::IndexEntry::operator=( const FileArray::IndexEntry & copy ) {
FileArray::posType i;
for ( i = 0; i < hashSize; i++ )
hash[i] = copy.hash[i];
for ( i = 0; i < offsetSize; i++ )
offset[i] = copy.offset[i];
return *this;
}
bool FileArray::IndexEntry::operator==( const FileArray::IndexEntry & lhs ) const {
for ( FileArray::posType i = 0; i < hashSize; i++ ) {
if ( hash[i] != lhs.hash[i] )
return false;
}
return true;
}
bool FileArray::IndexEntry::operator!=( const FileArray::IndexEntry & lhs ) const {
return !(*this == lhs);
}
bool FileArray::IndexEntry::operator<( const FileArray::IndexEntry &lhs ) const {
for ( FileArray::posType i = 0; i < hashSize; i++ ) {
if ( hash[i] < lhs.hash[i] )
return true;
else if ( hash[i] > lhs.hash[i] )
return false;
}
return false;
}
bool FileArray::IndexEntry::operator>( const FileArray::IndexEntry & lhs ) const {
return lhs < *this;
}
bool FileArray::IndexEntry::operator<=( const FileArray::IndexEntry & lhs ) const {
return !(lhs < *this);
}
bool FileArray::IndexEntry::operator>=( const FileArray::IndexEntry & lhs ) const {
return lhs <= *this;
}
std::ostream & operator<<( std::ostream & rhs, const FileArray::IndexEntry & lhs ) {
rhs.write( reinterpret_cast<const char*>(&lhs), FileArray::IndexEntry::indexSize );
return rhs;
}
std::istream & operator >> ( std::istream & rhs, FileArray::IndexEntry & lhs ) {
rhs.read( reinterpret_cast<char*>(&lhs), FileArray::IndexEntry::indexSize );
return rhs;
}