Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #617
Browse files Browse the repository at this point in the history
5d0fc9f RouterInfo: refactor RI reader/parser (anonimal)
4582eb6 RouterInfo: initialize Address class integer members (anonimal)
77323b6 RouterInfo: add RI trait enum + getters (anonimal)
c808888 StringStream: move ctor impl into source file (anonimal)
67042e1 StringStream: pass pointer to buffer when reading (anonimal)
48d1aac RouterInfo: handle if not EINVAL + handle unknown transport (anonimal)
  • Loading branch information
anonimal committed Apr 13, 2017
2 parents 740ea3a + 5d0fc9f commit fc8cd8b
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 171 deletions.
24 changes: 12 additions & 12 deletions src/client/reseed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,22 @@ bool SU3::SU3Impl() {
bool SU3::PrepareStream() {
try {
// Validate stream as an SU3
m_Stream.Read(*m_Data->magic_number.data(), Size::magic_number);
m_Stream.Read(m_Data->magic_number.data(), Size::magic_number);
if (m_Data->magic_number.data() != m_MagicValue) {
LOG(error) << "SU3: invalid magic value";
return false;
}
// File format version offset (spec defines it as 0, so we don't need it)
m_Stream.Seekg(Offset::version, std::ios::cur);
// Prepare signature type
m_Stream.Read(m_Data->signature_type, Size::signature_type);
m_Stream.Read(&m_Data->signature_type, Size::signature_type);
m_Data->signature_type = be16toh(m_Data->signature_type);
if (m_Data->signature_type != kovri::core::SIGNING_KEY_TYPE_RSA_SHA512_4096) { // Temporary (see #160)
LOG(error) << "SU3: signature type not supported";
return false;
}
// Prepare signature length
m_Stream.Read(m_Data->signature_length, Size::signature_length);
m_Stream.Read(&m_Data->signature_length, Size::signature_length);
m_Data->signature_length = be16toh(m_Data->signature_length);
if (m_Data->signature_length != sizeof(kovri::core::PublicKey)) { // Temporary (see #160)
LOG(error) << "SU3: invalid signature length";
Expand All @@ -288,7 +288,7 @@ bool SU3::PrepareStream() {
// Unused offset
m_Stream.Seekg(Offset::unused, std::ios::cur);
// Get version length
m_Stream.Read(m_Data->version_length, Size::version_length);
m_Stream.Read(&m_Data->version_length, Size::version_length);
if (m_Data->version_length <
static_cast<std::size_t>(Size::minimal_version)) {
LOG(error) << "SU3: version length too short";
Expand All @@ -297,13 +297,13 @@ bool SU3::PrepareStream() {
// Unused offset
m_Stream.Seekg(Offset::unused, std::ios::cur);
// Get signer ID length
m_Stream.Read(m_Data->signer_id_length, Size::signer_id_length);
m_Stream.Read(&m_Data->signer_id_length, Size::signer_id_length);
if (!m_Data->signer_id_length) {
LOG(error) << "SU3: invalid signer ID length";
return false;
}
// Prepare content length
m_Stream.Read(m_Data->content_length, Size::content_length);
m_Stream.Read(&m_Data->content_length, Size::content_length);
m_Data->content_length = be64toh(m_Data->content_length);
if (!m_Data->content_length) {
LOG(error) << "SU3: invalid content length";
Expand All @@ -312,7 +312,7 @@ bool SU3::PrepareStream() {
// Unused offset
m_Stream.Seekg(Offset::unused, std::ios::cur);
// Get file type that contains non-su3 data
m_Stream.Read(m_Data->file_type, Size::file_type);
m_Stream.Read(&m_Data->file_type, Size::file_type);
switch (m_Data->file_type) {
case static_cast<std::size_t>(FileType::zip_file):
break;
Expand All @@ -334,7 +334,7 @@ bool SU3::PrepareStream() {
// Unused offset
m_Stream.Seekg(Offset::unused, std::ios::cur);
// Get content type that contains the RI's
m_Stream.Read(m_Data->content_type, Size::content_type);
m_Stream.Read(&m_Data->content_type, Size::content_type);
switch (m_Data->content_type) {
case static_cast<std::size_t>(ContentType::unknown):
break;
Expand All @@ -359,9 +359,9 @@ bool SU3::PrepareStream() {
// Unused offset
m_Stream.Seekg(static_cast<std::size_t>(Offset::unused) * 12, std::ios::cur);
// SU3 version (we *could* test against this if we want)
m_Stream.Read(*m_Data->version.data(), m_Data->version_length);
m_Stream.Read(m_Data->version.data(), m_Data->version_length);
// Get signer ID
m_Stream.Read(*m_Data->signer_id.data(), m_Data->signer_id_length);
m_Stream.Read(m_Data->signer_id.data(), m_Data->signer_id_length);
// Currently enforces signer ID as an email address (not spec-defined)
// Note: do not rely on [a-z] to catch all letters as it will fail on some locales
const std::string alpha = "abcdefghijklmnopqrstuvwxyz";
Expand All @@ -378,8 +378,8 @@ bool SU3::PrepareStream() {
m_Data->signature.resize(m_Data->signature_length);
// Read in content and signature for verification against signer ID
m_Stream.Seekg(0, std::ios::beg);
m_Stream.Read(*m_Data->content.data(), m_Data->content.size());
m_Stream.Read(*m_Data->signature.data(), m_Data->signature.size());
m_Stream.Read(m_Data->content.data(), m_Data->content.size());
m_Stream.Read(m_Data->signature.data(), m_Data->signature.size());
// Go back to prepare for RI extraction
m_Stream.Seekg(m_Data->signature_position, std::ios::beg);
// Our content position is the same as signature position
Expand Down
28 changes: 14 additions & 14 deletions src/client/util/zip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool ZIP::Unzip() {
// Process local files, one after another
while (!m_Stream.EndOfFile()) {
// Validate local file's header signature
m_Stream.Read(m_Data->header_signature, Size::header_signature);
m_Stream.Read(&m_Data->header_signature, Size::header_signature);
m_Data->header_signature = le32toh(m_Data->header_signature);
if (m_Data->header_signature ==
static_cast<std::size_t>(Signature::header)) {
Expand Down Expand Up @@ -99,29 +99,29 @@ bool ZIP::PrepareLocalFile() {
// Skip version needed to extract
m_Stream.Seekg(Offset::version, std::ios::cur);
// Prepare bit flag
m_Stream.Read(m_Data->bit_flag, Size::bit_flag);
m_Stream.Read(&m_Data->bit_flag, Size::bit_flag);
m_Data->bit_flag = le16toh(m_Data->bit_flag);
// Prepare compression method (sanity test done later)
m_Stream.Read(m_Data->compression_method, Size::compression_method);
m_Stream.Read(&m_Data->compression_method, Size::compression_method);
m_Data->compression_method = le16toh(m_Data->compression_method);
// Unused offset
m_Stream.Seekg(Offset::last_mod, std::ios::cur);
// Get CRC-32 checksum
m_Stream.Read(*m_Data->crc_32.data(), Size::crc_32);
m_Stream.Read(m_Data->crc_32.data(), Size::crc_32);
if (!m_Data->crc_32.data()) {
LOG(warning) << "ZIP: CRC-32 checksum was null";
return false;
}
// Prepare compressed file size
m_Stream.Read(m_Data->compressed_size, Size::compressed_size);
m_Stream.Read(&m_Data->compressed_size, Size::compressed_size);
m_Data->compressed_size = le32toh(m_Data->compressed_size);
if (!m_Data->compressed_size)
LOG(warning) << "ZIP: compressed file size was null";
// Prepare uncompressed file size
m_Stream.Read(m_Data->uncompressed_size, Size::uncompressed_size);
m_Stream.Read(&m_Data->uncompressed_size, Size::uncompressed_size);
m_Data->uncompressed_size = le32toh(m_Data->uncompressed_size);
// Prepare local filename length
m_Stream.Read(m_Data->local_filename_length, Size::local_filename_length);
m_Stream.Read(&m_Data->local_filename_length, Size::local_filename_length);
m_Data->local_filename_length = le16toh(m_Data->local_filename_length);
// If we expand ZIP beyond SU3, we'll have to remove this check
if ((m_Data->local_filename_length !=
Expand All @@ -132,13 +132,13 @@ bool ZIP::PrepareLocalFile() {
return false;
}
// Prepare extra field length
m_Stream.Read(m_Data->extra_field_length, Size::extra_field_length);
m_Stream.Read(&m_Data->extra_field_length, Size::extra_field_length);
m_Data->extra_field_length = le16toh(m_Data->extra_field_length);
// Get local filename
// TODO(unassigned): we don't check if filename is base64 standard
// ex., 'routerInfo-asdfj23kjf2lk3nfnakjlsnfjklsdnfln23f.dat' (as defined in SU3 spec).
// If we don't extend ZIP beyond SU3, we may wish to enforce a regex check.
m_Stream.Read(*m_Data->local_filename.data(), m_Data->local_filename_length);
m_Stream.Read(m_Data->local_filename.data(), m_Data->local_filename_length);
// Skip extra field
m_Stream.Seekg(m_Data->extra_field_length, std::ios::cur);
// Verify if data descriptor is present
Expand All @@ -148,9 +148,9 @@ bool ZIP::PrepareLocalFile() {
LOG(error) << "ZIP: archive data descriptor not found";
return false;
}
m_Stream.Read(*m_Data->crc_32.data(), Size::crc_32);
m_Stream.Read(m_Data->compressed_size, Size::compressed_size);
m_Stream.Read(m_Data->uncompressed_size, Size::uncompressed_size);
m_Stream.Read(m_Data->crc_32.data(), Size::crc_32);
m_Stream.Read(&m_Data->compressed_size, Size::compressed_size);
m_Stream.Read(&m_Data->uncompressed_size, Size::uncompressed_size);
// We consider the signature as part of compressed data
m_Data->compressed_size +=
static_cast<std::size_t>(Size::header_signature);
Expand All @@ -169,7 +169,7 @@ bool ZIP::FindDataDescriptor() {
std::size_t next_ind = 0;
while (!m_Stream.EndOfFile()) {
std::uint8_t next_byte;
m_Stream.Read(next_byte, 1);
m_Stream.Read(&next_byte, 1);
if (next_byte == Descriptor.signature.at(next_ind)) {
next_ind++;
if (next_ind >= Descriptor.signature.size())
Expand All @@ -189,7 +189,7 @@ bool ZIP::DecompressLocalFile() {
// Resize for next file
m_Data->compressed.resize(m_Data->compressed_size);
// Read in compressed data
m_Stream.Read(*m_Data->compressed.data(), m_Data->compressed.size());
m_Stream.Read(m_Data->compressed.data(), m_Data->compressed.size());
switch (m_Data->compression_method) {
case static_cast<std::size_t>(Method::deflate): {
LOG(debug) << "ZIP: file uses compression method 'deflate'";
Expand Down
Loading

0 comments on commit fc8cd8b

Please sign in to comment.