refactor: use span API in AsarFileValidator::OnRead()

This commit is contained in:
Charles Kerr
2024-10-01 10:33:22 -05:00
parent d5b2919494
commit 6e9f3105aa
2 changed files with 26 additions and 27 deletions

View File

@@ -10,6 +10,7 @@
#include <utility>
#include <vector>
#include "base/containers/span.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
@@ -26,7 +27,7 @@ AsarFileValidator::AsarFileValidator(IntegrityPayload integrity,
AsarFileValidator::~AsarFileValidator() = default;
void AsarFileValidator::EnsureHashExists() {
void AsarFileValidator::EnsureBlockHashExists() {
if (current_hash_)
return;
@@ -45,34 +46,32 @@ void AsarFileValidator::OnRead(base::span<char> buffer,
mojo::FileDataSource::ReadResult* result) {
DCHECK(!done_reading_);
const uint64_t buffer_size = result->bytes_read;
// Compute how many bytes we should hash, and add them to the current hash.
const uint32_t block_size = integrity_.block_size;
uint64_t bytes_added = 0;
while (bytes_added < buffer_size) {
if (current_block_ > max_block_) {
LOG(FATAL)
<< "Unexpected number of blocks while validating ASAR file stream";
}
EnsureHashExists();
// |buffer| contains the read buffer. |result->bytes_read| is the actual
// bytes number that |source| read that should be less than buffer.size().
auto hashme = base::as_bytes(buffer.subspan(0U, result->bytes_read));
// Compute how many bytes we should hash, and add them to the current hash.
// We need to either add just enough bytes to fill up a block (block_size -
// current_bytes) or use every remaining byte (buffer_size - bytes_added)
int bytes_to_hash = std::min(block_size - current_hash_byte_count_,
buffer_size - bytes_added);
DCHECK_GT(bytes_to_hash, 0);
current_hash_->Update(buffer.data() + bytes_added, bytes_to_hash);
bytes_added += bytes_to_hash;
current_hash_byte_count_ += bytes_to_hash;
total_hash_byte_count_ += bytes_to_hash;
while (!std::empty(hashme)) {
if (current_block_ > max_block_)
LOG(FATAL) << "Unexpected block count while validating ASAR file stream";
if (current_hash_byte_count_ == block_size && !FinishBlock()) {
LOG(FATAL) << "Failed to validate block while streaming ASAR file: "
<< current_block_;
}
EnsureBlockHashExists();
// hash as many bytes as will fit in the current block.
const auto n_left_in_block = block_size - current_hash_byte_count_;
const auto n_now = std::min(n_left_in_block, std::size(hashme));
DCHECK_GT(n_now, 0U);
const auto [hashme_now, hashme_next] = hashme.split_at(n_now);
current_hash_->Update(hashme_now);
current_hash_byte_count_ += n_now;
total_hash_byte_count_ += n_now;
if (current_hash_byte_count_ == block_size && !FinishBlock())
LOG(FATAL) << "Streaming ASAR file block hash failed: " << current_block_;
hashme = hashme_next;
}
}

View File

@@ -36,7 +36,7 @@ class AsarFileValidator : public mojo::FilteredDataSource::Filter {
bool FinishBlock();
private:
void EnsureHashExists();
void EnsureBlockHashExists();
base::File file_;
IntegrityPayload integrity_;
@@ -54,7 +54,7 @@ class AsarFileValidator : public mojo::FilteredDataSource::Filter {
bool done_reading_ = false;
int current_block_;
int max_block_;
uint64_t current_hash_byte_count_ = 0;
uint64_t current_hash_byte_count_ = 0U;
uint64_t total_hash_byte_count_ = 0;
std::unique_ptr<crypto::SecureHash> current_hash_;
};