Skip to content

Commit

Permalink
Cleanup and fix password
Browse files Browse the repository at this point in the history
  • Loading branch information
thebrandre committed Feb 11, 2024
1 parent 3e66e8c commit 30dbc60
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 29 deletions.
1 change: 1 addition & 0 deletions InMemoryArchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class InMemoryArchive {
InMemoryArchive();

InMemoryFileSystem FileSystem;
std::u8string Password;
};
11 changes: 6 additions & 5 deletions InMemoryArchiveUpdateCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ InMemoryArchiveUpdateCallback::~InMemoryArchiveUpdateCallback()
fmt::print("Cleanup InMemoryArchiveUpdateCallback");
}

InMemoryArchiveUpdateCallback::InMemoryArchiveUpdateCallback(InMemoryArchive* Archive) : Archive(Archive) //, Password(u8"1234")
InMemoryArchiveUpdateCallback::InMemoryArchiveUpdateCallback(InMemoryArchive* Archive) : Archive(Archive)
{}

Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::SetTotal(UInt64 size))
Expand Down Expand Up @@ -157,12 +157,13 @@ Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::GetVolumeStream(UInt32 index, ISeque
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::CryptoGetTextPassword2(Int32* passwordIsDefined, BSTR* password))
{
fmt::print("InMemoryArchiveUpdateCallback::CryptoGetTextPassword2()\n");
if (!Password.empty())
const bool PasswordIsDefined = !Archive->Password.empty();
if (PasswordIsDefined && password != nullptr)
{
const auto PasswordW = boost::nowide::widen(Password);
const auto PasswordW = boost::nowide::widen(Archive->Password);
*password = ::SysAllocStringLen(PasswordW.c_str(), PasswordW.size());
*passwordIsDefined = 1;
}
*passwordIsDefined = 0;
if (passwordIsDefined)
*passwordIsDefined = PasswordIsDefined;
return S_OK;
}
1 change: 0 additions & 1 deletion InMemoryArchiveUpdateCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ class InMemoryArchiveUpdateCallback Z7_final :

private:
InMemoryArchive* Archive{};
std::u8string Password;
};
36 changes: 22 additions & 14 deletions InMemoryOutStream.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
#include "InMemoryOutStream.h"

#include <cassert>
#include <span>
#include <fmt/format.h>

InMemoryOutStream::InMemoryOutStream(std::vector<std::byte>& Data): Data(&Data)
InMemoryOutStream::InMemoryOutStream(std::vector<std::byte>& Data) : Data(&Data)
{}

InMemoryOutStream::~InMemoryOutStream() = default;

Z7_COM7F_IMF(InMemoryOutStream::Write(const void* data, UInt32 size, UInt32* processedSize))
static std::size_t writeBytesToBuffer(auto& Buffer, std::size_t Position, std::span<const std::byte> BytesToWrite)
{
const auto BytesToWrite = static_cast<size_t>(size);
const auto OverWriteBytes = std::min(Data->size() - Position, BytesToWrite);
std::memcpy(Data->data() + Position, data, OverWriteBytes);
fmt::print("InMemoryOutStream::Write overwrite {} of {} bytes of current buffer of size {}.\n", OverWriteBytes, BytesToWrite, Data->size());
Position += OverWriteBytes;

const auto RemainingBytes = BytesToWrite - OverWriteBytes;
if (RemainingBytes != 0)
const auto BytesToOverwrite = BytesToWrite.subspan(0, std::min(std::ranges::size(Buffer) - Position, BytesToWrite.size()));
const auto BytesToAppend = BytesToWrite.subspan(BytesToOverwrite.size());
fmt::print("InMemoryOutStream::Write overwrite {} of {} bytes of current buffer of size {}.\n", BytesToOverwrite.size(), BytesToWrite.size(), std::ranges::size(Buffer));
const auto CurrentPositionIterator = std::ranges::copy(BytesToOverwrite, std::ranges::next(std::ranges::begin(Buffer), Position)).out;
if (!BytesToAppend.empty())
{
const auto RemainingBytesView = std::span{ static_cast<const std::byte*>(data) + OverWriteBytes, RemainingBytes };
Data->insert(Data->end(), RemainingBytesView.begin(), RemainingBytesView.end());
fmt::print("InMemoryOutStream::Write append remaining {} of {} bytes of current buffer of size {}.\n", RemainingBytes, BytesToWrite, Data->size());
Position = Data->size();
assert(CurrentPositionIterator == std::ranges::end(Buffer));
fmt::print("InMemoryOutStream::Write append remaining {} of {} bytes of current buffer of size {}.\n", BytesToAppend.size(), BytesToWrite.size(), std::ranges::size(Buffer));
std::ranges::copy(BytesToAppend, std::back_inserter(Buffer));
return std::ranges::size(Buffer);
}

const auto NewPosition = std::ranges::distance(std::ranges::begin(Buffer), CurrentPositionIterator);
fmt::print("InMemoryOutStream::Write move position from {} to {}.\n", Position, NewPosition);
return NewPosition;
}


Z7_COM7F_IMF(InMemoryOutStream::Write(const void* data, UInt32 size, UInt32* processedSize))
{
const auto BytesToWrite = std::span{ static_cast<const std::byte*>(data), static_cast<size_t>(size) };
Position = ::writeBytesToBuffer(*Data, Position, BytesToWrite);
if (processedSize)
*processedSize = size;
return S_OK;
Expand Down
2 changes: 1 addition & 1 deletion InMemoryOutStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class InMemoryOutStream Z7_final :

private:
std::vector<std::byte>* Data;
size_t Position{};
std::size_t Position{};
};
17 changes: 9 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ size_t getUncompressedSizeOfFile(IInArchive* Archive, UInt32 Index)
Archive->GetProperty(Index, kpidSize, &prop);
if (prop.vt == VT_UI8)
return prop.uhVal.QuadPart;
throw std::runtime_error("");

throw std::runtime_error("");
}

std::string getFileName(IInArchive* Archive, UInt32 Index)
Expand All @@ -34,7 +34,7 @@ std::string getFileName(IInArchive* Archive, UInt32 Index)
return boost::nowide::narrow(prop.bstrVal);
else if (prop.vt != VT_EMPTY)
return std::to_string(prop.vt);
return {};
return {};
}


Expand All @@ -44,7 +44,7 @@ void extractStuff(ArchiveFactory& Factory)

CMyComPtr<IInArchive> Archive = Factory.createIInArchive();
CMyComPtr<IArchiveOpenCallback> OpenCallback(new InMemoryArchiveOpenCallback(u8"Password"));
CMyComPtr<IInStream> InMemoryInStreamCallback (new StdFileInStream(TestArchive));
CMyComPtr<IInStream> InMemoryInStreamCallback(new StdFileInStream(TestArchive));

const UInt64 scanSize = 1 << 23;
if (const auto OpenResult = Archive->Open(InMemoryInStreamCallback, &scanSize, OpenCallback); OpenResult != S_OK)
Expand Down Expand Up @@ -73,16 +73,17 @@ void compressStuff(ArchiveFactory& Factory)
{
CMyComPtr<IOutArchive> OutArchive = Factory.createIOutArchive();
InMemoryArchive TempArchive;
unsigned char FileContent[] = "ASCII";
const std::span<std::byte> FileContentView{ (std::byte*)FileContent, std::size(FileContent)-1 };
TempArchive.Password = u8"Password";
unsigned char FileContent[] = "ASCII and stuff";
const std::span<std::byte> FileContentView{ (std::byte*)FileContent, std::size(FileContent) - 1 };
TempArchive.FileSystem.createFile(u8"SomeFile.txt", FileContentView);

auto UpdateCallback = TempArchive.getUpdateCallback();
std::vector<std::byte> Buffer;
CMyComPtr<ISequentialOutStream> InMemoryOutStreamInstance (new InMemoryOutStream(Buffer));
CMyComPtr<ISequentialOutStream> InMemoryOutStreamInstance(new InMemoryOutStream(Buffer));
OutArchive->UpdateItems(InMemoryOutStreamInstance, 1, UpdateCallback);

std::ofstream OutFile("generatedArchive.7z",std::ios_base::trunc| std::ios_base::binary);
std::ofstream OutFile("generatedArchive.7z", std::ios_base::trunc | std::ios_base::binary);
OutFile.write((const char*)Buffer.data(), Buffer.size());
}

Expand Down

0 comments on commit 30dbc60

Please sign in to comment.