Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions zip/converters.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "converters.h"

#include <cstdint>
#include <string>
#include <utility>

Expand Down Expand Up @@ -51,6 +52,14 @@ absl::Status FromRustResultUnit(
return absl::OkStatus();
}

absl::StatusOr<uint64_t> FromRustResultU64(
rs_std::Result<uint64_t, rust::ZipError> result_u64) {
if (!result_u64.has_value()) {
return ZipErrorToStatus(std::move(result_u64).err());
}
return std::move(result_u64).value();
}

absl::StatusOr<rust::BufferedZipArchive> FromRustBufferedZipArchive(
rs_std::Result<rust::BufferedZipArchive, rust::ZipError>
result_buffered_zip_archive) {
Expand Down Expand Up @@ -87,6 +96,15 @@ absl::StatusOr<rust::FsZipFile> FromRustFsZipFile(
return std::move(result_fs_zip_file).value();
}

absl::StatusOr<rust::ZipEntryMetadata> FromRustZipEntryMetadata(
rs_std::Result<rust::ZipEntryMetadata, rust::ZipError>
result_zip_entry_metadata) {
if (!result_zip_entry_metadata.has_value()) {
return ZipErrorToStatus(std::move(result_zip_entry_metadata).err());
}
return std::move(result_zip_entry_metadata).value();
}

absl::StatusOr<rust::BufferedZipWriter> FromRustBufferedZipWriter(
rs_std::Result<rust::BufferedZipWriter, rust::ZipError>
result_buffered_zip_writer) {
Expand All @@ -105,4 +123,32 @@ absl::StatusOr<rust::FsZipWriter> FromRustFsZipWriter(
return std::move(result_fs_zip_writer).value();
}

absl::StatusOr<rust::BufferedZipStreamReader>
FromRustBufferedZipStreamReader(
rs_std::Result<rust::BufferedZipStreamReader, rust::ZipError>
result_buffered_zip_stream_reader) {
if (!result_buffered_zip_stream_reader.has_value()) {
return ZipErrorToStatus(std::move(result_buffered_zip_stream_reader).err());
}
return std::move(result_buffered_zip_stream_reader).value();
}

absl::StatusOr<rust::FsZipStreamReader> FromRustFsZipStreamReader(
rs_std::Result<rust::FsZipStreamReader, rust::ZipError>
result_fs_zip_stream_reader) {
if (!result_fs_zip_stream_reader.has_value()) {
return ZipErrorToStatus(std::move(result_fs_zip_stream_reader).err());
}
return std::move(result_fs_zip_stream_reader).value();
}

absl::StatusOr<rust::FsZipStreamWriter> FromRustFsZipStreamWriter(
rs_std::Result<rust::FsZipStreamWriter, rust::ZipError>
result_fs_zip_stream_writer) {
if (!result_fs_zip_stream_writer.has_value()) {
return ZipErrorToStatus(std::move(result_fs_zip_stream_writer).err());
}
return std::move(result_fs_zip_stream_writer).value();
}

} // namespace security::zip
35 changes: 34 additions & 1 deletion zip/converters.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SECURITY_ZIP_CONVERTERS_H_
#define SECURITY_ZIP_CONVERTERS_H_

#include <cstddef>
#include <utility>

#include "crubit_helpers/string_conversions.h"
Expand Down Expand Up @@ -49,11 +50,24 @@ class RustVecU8Wrapper {
//
// Consuming inside the full-expression is safe:
// std::string owned(f.GetFileData()->AsStringView()); // OK
absl::string_view AsStringView() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
[[nodiscard]] absl::string_view AsStringView()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
return security::crubit_helpers::StringViewFromVecU8(vec_u8_);
}
absl::string_view AsStringView() const&& = delete;

[[nodiscard]] const char* data() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsStringView().data();
}

[[nodiscard]] size_t size() const { return AsStringView().size(); }

[[nodiscard]] bool empty() const { return AsStringView().empty(); }

explicit operator absl::string_view() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsStringView();
}

private:
rust::VecU8 vec_u8_;
};
Expand All @@ -62,6 +76,10 @@ absl::StatusOr<RustVecU8Wrapper> FromRustResultVecU8(
rs_std::Result<rust::VecU8, rust::ZipError> result_vec_u8);
absl::Status FromRustResultUnit(
rs_std::Result<rs_std::unit_t, rust::ZipError> result_unit);
// Converts a byte count, as returned by the reads that write into a buffer the
// caller owns rather than returning one.
absl::StatusOr<uint64_t> FromRustResultU64(
rs_std::Result<uint64_t, rust::ZipError> result_u64);

absl::StatusOr<rust::BufferedZipArchive> FromRustBufferedZipArchive(
rs_std::Result<rust::BufferedZipArchive, rust::ZipError>
Expand All @@ -76,6 +94,9 @@ absl::StatusOr<rust::BufferedZipFile> FromRustBufferedZipFile(
absl::StatusOr<rust::FsZipFile> FromRustFsZipFile(
rs_std::Result<rust::FsZipFile, rust::ZipError>
result_fs_zip_file);
absl::StatusOr<rust::ZipEntryMetadata> FromRustZipEntryMetadata(
rs_std::Result<rust::ZipEntryMetadata, rust::ZipError>
result_zip_entry_metadata);

absl::StatusOr<rust::BufferedZipWriter> FromRustBufferedZipWriter(
rs_std::Result<rust::BufferedZipWriter, rust::ZipError>
Expand All @@ -84,6 +105,18 @@ absl::StatusOr<rust::FsZipWriter> FromRustFsZipWriter(
rs_std::Result<rust::FsZipWriter, rust::ZipError>
result_fs_zip_writer);

absl::StatusOr<rust::BufferedZipStreamReader>
FromRustBufferedZipStreamReader(
rs_std::Result<rust::BufferedZipStreamReader, rust::ZipError>
result_buffered_zip_stream_reader);
absl::StatusOr<rust::FsZipStreamReader> FromRustFsZipStreamReader(
rs_std::Result<rust::FsZipStreamReader, rust::ZipError>
result_fs_zip_stream_reader);

absl::StatusOr<rust::FsZipStreamWriter> FromRustFsZipStreamWriter(
rs_std::Result<rust::FsZipStreamWriter, rust::ZipError>
result_fs_zip_stream_writer);

} // namespace security::zip

#endif // SECURITY_ZIP_CONVERTERS_H_
106 changes: 106 additions & 0 deletions zip/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "absl/status/status.h"
#include "absl/status/status_macros.h"
#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "support/rs_std/slice_ref.h"
namespace security::zip {

namespace {
Expand All @@ -36,8 +38,51 @@ absl::StatusOr<CompressionMethod> ToSecurityZipCompressionMethod(
return absl::InvalidArgumentError("Unknown compression method");
}

// Rust writes into the caller's buffer through this; the slice borrows it and
// must not outlive the call it is passed to.
rs_std::SliceRef<uint8_t> AsMutableSlice(absl::Span<char> buf) {
return rs_std::SliceRef<uint8_t>(
absl::Span<uint8_t>(reinterpret_cast<uint8_t*>(buf.data()), buf.size()));
}

} // namespace

bool ZipEntryMetadata::IsFile() const { return metadata_.is_file(); }
bool ZipEntryMetadata::IsDir() const { return metadata_.is_dir(); }
bool ZipEntryMetadata::IsEncrypted() const { return metadata_.is_encrypted(); }
std::string ZipEntryMetadata::GetFileName() const {
rust::VecU8 name_vec = metadata_.get_file_name();
return std::string(security::crubit_helpers::StringViewFromVecU8(name_vec));
}
absl::StatusOr<CompressionMethod> ZipEntryMetadata::GetCompressionMethod()
const {
return ToSecurityZipCompressionMethod(metadata_.get_compression_method());
}
std::string ZipEntryMetadata::GetComment() const {
rust::VecU8 comment_vec = metadata_.get_comment();
return std::string(
security::crubit_helpers::StringViewFromVecU8(comment_vec));
}
uint64_t ZipEntryMetadata::GetUncompressedSize() const {
return metadata_.get_uncompressed_size();
}
uint64_t ZipEntryMetadata::GetCompressedSize() const {
return metadata_.get_compressed_size();
}
uint32_t ZipEntryMetadata::GetCrc32() const { return metadata_.get_crc32(); }
uint16_t ZipEntryMetadata::GetLastModifiedDate() const {
return metadata_.get_last_modified_date();
}
uint16_t ZipEntryMetadata::GetLastModifiedTime() const {
return metadata_.get_last_modified_time();
}
uint32_t ZipEntryMetadata::GetUnixMode() const {
return metadata_.get_unix_mode();
}
uint64_t ZipEntryMetadata::GetHeaderStart() const {
return metadata_.get_header_start();
}

absl::Status BufferedZipFile::CheckNone() const {
if (IsNone()) {
return absl::InternalError("ZipFile object used after being moved-out");
Expand Down Expand Up @@ -98,6 +143,14 @@ absl::StatusOr<RustVecU8Wrapper> BufferedZipFile::GetExtraData() const {
ABSL_RETURN_IF_ERROR(CheckNone());
return RustVecU8Wrapper(zip_.get_extra_data());
}
absl::StatusOr<uint64_t> BufferedZipFile::GetHeaderStart() const {
ABSL_RETURN_IF_ERROR(CheckNone());
return zip_.get_header_start();
}
absl::StatusOr<uint64_t> BufferedZipFile::GetDataStart() const {
ABSL_RETURN_IF_ERROR(CheckNone());
return zip_.get_data_start();
}
absl::StatusOr<RustVecU8Wrapper> BufferedZipFile::GetFileData() {
ABSL_RETURN_IF_ERROR(CheckNone());
return FromRustResultVecU8(zip_.get_file_data());
Expand All @@ -108,6 +161,20 @@ absl::StatusOr<RustVecU8Wrapper> BufferedZipFile::GetFileData(size_t max_size) {
}
return FromRustResultVecU8(zip_.get_file_data_with_max_size(max_size));
}
absl::StatusOr<RustVecU8Wrapper> BufferedZipFile::ReadBytes(size_t max_bytes) {
ABSL_RETURN_IF_ERROR(CheckNone());
return FromRustResultVecU8(zip_.read_bytes(max_bytes));
}
absl::StatusOr<size_t> BufferedZipFile::ReadInto(absl::Span<char> buf) {
ABSL_RETURN_IF_ERROR(CheckNone());
ABSL_ASSIGN_OR_RETURN(const uint64_t read,
FromRustResultU64(zip_.read_into(AsMutableSlice(buf))));
return static_cast<size_t>(read);
}
absl::StatusOr<uint64_t> BufferedZipFile::Skip(uint64_t max_bytes) {
ABSL_RETURN_IF_ERROR(CheckNone());
return FromRustResultU64(zip_.skip(max_bytes));
}

absl::Status FsZipFile::CheckNone() const {
if (IsNone()) {
Expand Down Expand Up @@ -168,6 +235,14 @@ absl::StatusOr<RustVecU8Wrapper> FsZipFile::GetExtraData() const {
ABSL_RETURN_IF_ERROR(CheckNone());
return RustVecU8Wrapper(zip_.get_extra_data());
}
absl::StatusOr<uint64_t> FsZipFile::GetHeaderStart() const {
ABSL_RETURN_IF_ERROR(CheckNone());
return zip_.get_header_start();
}
absl::StatusOr<uint64_t> FsZipFile::GetDataStart() const {
ABSL_RETURN_IF_ERROR(CheckNone());
return zip_.get_data_start();
}
absl::StatusOr<RustVecU8Wrapper> FsZipFile::GetFileData() {
ABSL_RETURN_IF_ERROR(CheckNone());
return FromRustResultVecU8(zip_.get_file_data());
Expand All @@ -178,6 +253,20 @@ absl::StatusOr<RustVecU8Wrapper> FsZipFile::GetFileData(size_t max_size) {
}
return FromRustResultVecU8(zip_.get_file_data_with_max_size(max_size));
}
absl::StatusOr<RustVecU8Wrapper> FsZipFile::ReadBytes(size_t max_bytes) {
ABSL_RETURN_IF_ERROR(CheckNone());
return FromRustResultVecU8(zip_.read_bytes(max_bytes));
}
absl::StatusOr<size_t> FsZipFile::ReadInto(absl::Span<char> buf) {
ABSL_RETURN_IF_ERROR(CheckNone());
ABSL_ASSIGN_OR_RETURN(const uint64_t read,
FromRustResultU64(zip_.read_into(AsMutableSlice(buf))));
return static_cast<size_t>(read);
}
absl::StatusOr<uint64_t> FsZipFile::Skip(uint64_t max_bytes) {
ABSL_RETURN_IF_ERROR(CheckNone());
return FromRustResultU64(zip_.skip(max_bytes));
}

absl::StatusOr<bool> ZipFile::IsFile() const {
return std::visit([](const auto& zip) { return zip.IsFile(); }, zip_);
Expand Down Expand Up @@ -223,12 +312,29 @@ absl::StatusOr<uint32_t> ZipFile::GetUnixMode() const {
absl::StatusOr<RustVecU8Wrapper> ZipFile::GetExtraData() const {
return std::visit([](const auto& zip) { return zip.GetExtraData(); }, zip_);
}
absl::StatusOr<uint64_t> ZipFile::GetHeaderStart() const {
return std::visit([](const auto& zip) { return zip.GetHeaderStart(); }, zip_);
}
absl::StatusOr<uint64_t> ZipFile::GetDataStart() const {
return std::visit([](const auto& zip) { return zip.GetDataStart(); }, zip_);
}
absl::StatusOr<RustVecU8Wrapper> ZipFile::GetFileData() {
return std::visit([](auto& zip) { return zip.GetFileData(); }, zip_);
}
absl::StatusOr<RustVecU8Wrapper> ZipFile::GetFileData(size_t max_size) {
return std::visit([max_size](auto& zip) { return zip.GetFileData(max_size); },
zip_);
}
absl::StatusOr<RustVecU8Wrapper> ZipFile::ReadBytes(size_t max_bytes) {
return std::visit([max_bytes](auto& zip) { return zip.ReadBytes(max_bytes); },
zip_);
}
absl::StatusOr<size_t> ZipFile::ReadInto(absl::Span<char> buf) {
return std::visit([buf](auto& zip) { return zip.ReadInto(buf); }, zip_);
}
absl::StatusOr<uint64_t> ZipFile::Skip(uint64_t max_bytes) {
return std::visit([max_bytes](auto& zip) { return zip.Skip(max_bytes); },
zip_);
}

} // namespace security::zip
Loading
Loading