Skip to content

Commit

Permalink
Add command to copy an archive without encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
kimci86 committed May 17, 2024
1 parent df61ea8 commit b1e0d7b
Show file tree
Hide file tree
Showing 5 changed files with 626 additions and 118 deletions.
4 changes: 4 additions & 0 deletions include/Arguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class Arguments
/// Tell whether to keep the encryption header or discard it when writing the deciphered text
bool keepHeader = false;

/// File to write an unencrypted copy of the encrypted archive
std::optional<std::string> decryptedArchive;

/// Arguments needed to change an archive's password
struct ChangePassword
{
Expand Down Expand Up @@ -152,6 +155,7 @@ class Arguments
keys,
decipheredFile,
keepHeader,
decryptedArchive,
changePassword,
changeKeys,
bruteforce,
Expand Down
4 changes: 4 additions & 0 deletions include/Zip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ class Zip
/// \exception Error if the archive is not a valid zip archive
void changeKeys(std::ostream& os, const Keys& oldKeys, const Keys& newKeys, Progress& progress) const;

/// \brief Copy the zip file into \a os removing encryption using the given keys
/// \exception Error if the archive is not a valid zip archive
void decrypt(std::ostream& os, const Keys& keys, Progress& progress) const;

private:
std::optional<std::ifstream> m_file; // optionally own the stream
std::istream& m_is;
Expand Down
13 changes: 11 additions & 2 deletions src/Arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Arguments::Arguments(int argc, const char* argv[])
// check constraints on arguments
if (keys)
{
if (!decipheredFile && !changePassword && !changeKeys && !bruteforce)
throw Error{"-d, -U, --change-keys or --bruteforce parameter is missing (required by -k)"};
if (!decipheredFile && !decryptedArchive && !changePassword && !changeKeys && !bruteforce)
throw Error{"-d, -D, -U, --change-keys or --bruteforce parameter is missing (required by -k)"};
}
else if (!password)
{
Expand Down Expand Up @@ -131,6 +131,11 @@ Arguments::Arguments(int argc, const char* argv[])
if (decipheredFile && !cipherArchive && decipheredFile == cipherFile)
throw Error{"-c and -d parameters must point to different files"};

if (decryptedArchive && !cipherArchive)
throw Error{"-C parameter is missing (required by -D)"};
if (decryptedArchive && decryptedArchive == cipherArchive)
throw Error{"-C and -D parameters must point to different files"};

if (changePassword && !cipherArchive)
throw Error{"-C parameter is missing (required by -U)"};
if (changePassword && changePassword->unlockedArchive == cipherArchive)
Expand Down Expand Up @@ -250,6 +255,9 @@ void Arguments::parseArgument()
case Option::keepHeader:
keepHeader = true;
break;
case Option::decryptedArchive:
decryptedArchive = readString("decipheredzip");
break;
case Option::changePassword:
changePassword = {readString("unlockedzip"), readString("password")};
break;
Expand Down Expand Up @@ -338,6 +346,7 @@ auto Arguments::readOption(const std::string& description) -> Arguments::Option
PAIRS(-k, --keys, keys),
PAIRS(-d, --decipher, decipheredFile),
PAIR ( --keep-header, keepHeader),
PAIRS(-D, --decrypt, decryptedArchive),
PAIRS(-U, --change-password, changePassword),
PAIR ( --change-keys, changeKeys),
PAIRS(-b, --bruteforce, bruteforce),
Expand Down
Loading

0 comments on commit b1e0d7b

Please sign in to comment.