Bitmask::encode bugfix

GitOrigin-RevId: 14b62438b50a9806f27d5e5c028a092cc517e6ac
This commit is contained in:
Arseny Smirnov 2018-12-27 21:06:30 +03:00
parent aecbbf47f9
commit 86bf606666
5 changed files with 23 additions and 8 deletions

View File

@ -37,8 +37,8 @@ eval emconfigure cmake -DCMAKE_BUILD_TYPE=Release $OPENSSL_OPTIONS -DASMJS=1 $TD
cd ../..
echo "Generating TDLib autogenerated source files..."
cmake --build build/generate --target prepare_cross_compiling || exit 1
cmake --build build/generate -j --target prepare_cross_compiling || exit 1
echo "Building TDLib to WebAssembly..."
cmake --build build/wasm --target td_wasm || exit 1
cmake --build build/wasm -j --target td_wasm || exit 1
echo "Building TDLib to asm.js..."
cmake --build build/asmjs --target td_asmjs || exit 1
cmake --build build/asmjs -j --target td_asmjs || exit 1

View File

@ -20,12 +20,27 @@ Bitmask::Bitmask(Ones, int64 count) : data_(narrow_cast<size_t>((count + 7) / 8)
}
}
std::string Bitmask::encode(int32 prefix_count) const {
std::string Bitmask::encode(int32 prefix_count) {
// remove zeroes in the end to make encoding deterministic
td::Slice data(data_);
int save_i = -1;
char save_c;
if (prefix_count != -1) {
data.truncate(prefix_count);
auto truncated_size = (prefix_count + 7) / 8;
data.truncate(truncated_size);
if (prefix_count % 8 != 0) {
save_i = truncated_size - 1;
save_c = data_[save_i];
uint8 mask = 0xff >> (8 - prefix_count % 8);
data_[save_i] &= mask;
}
}
SCOPE_EXIT {
if (save_i != -1) {
data_[save_i] = save_c;
}
};
while (!data.empty() && data.back() == '\0') {
data.remove_suffix(1);
}

View File

@ -19,7 +19,7 @@ class Bitmask {
Bitmask() = default;
Bitmask(Decode, Slice data);
Bitmask(Ones, int64 count);
std::string encode(int32 prefix_count = -1) const;
std::string encode(int32 prefix_count = -1);
int64 get_ready_prefix_size(int64 offset, int64 part_size, int64 file_size) const;
int64 get_total_size(int64 part_size, int64 file_size) const;
bool get(int64 offset_part) const;

View File

@ -169,7 +169,7 @@ int32 PartsManager::get_ready_prefix_count() {
}
return res;
}
string PartsManager::get_bitmask() const {
string PartsManager::get_bitmask() {
int32 prefix_count = -1;
if (need_check_) {
prefix_count = narrow_cast<int32>(checked_prefix_size_ / part_size_);

View File

@ -47,7 +47,7 @@ class PartsManager {
int32 get_part_count() const;
int32 get_unchecked_ready_prefix_count();
int32 get_ready_prefix_count();
string get_bitmask() const;
string get_bitmask();
private:
static constexpr int MAX_PART_COUNT = 3000;