Skip to content

Commit 3294b8d

Browse files
committed
Migrate the project from modules to headers
C++20 Module support varies by compilers, and is quite unstable across multiple compilers as of now (Feb. 2025)
1 parent f41889a commit 3294b8d

26 files changed

Lines changed: 946 additions & 942 deletions

CMakeLists.txt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
# Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
# Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see https://www.gnu.org/licenses.
1616

17-
# CMake 3.28+ is required for C++20 modules
1817
cmake_minimum_required(VERSION 3.28)
1918

2019
project(privacyShield
@@ -50,44 +49,33 @@ cmake_dependent_option(ENABLE_SANITIZERS
5049
# Valgrind support
5150
option(VALGRIND_BUILD "Build with Valgrind support" OFF)
5251

52+
# Add the executable target
53+
add_executable(privacyShield)
54+
5355
# Additional checks for the Debug build
5456
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
55-
add_compile_options(
57+
target_compile_options(privacyShield PRIVATE
5658
-Wall
5759
-Wextra
5860
-Werror
5961
-Wpedantic
6062
)
6163
endif ()
6264

63-
# Add the executable target
64-
add_executable(privacyShield)
65-
66-
# Add sources for the target
65+
# project source files
6766
target_sources(privacyShield PRIVATE
67+
src/duplicateFinder/duplicateFinder.cpp
6868
src/encryption/encryptDecrypt.cpp
6969
src/encryption/encryptFiles.cpp
7070
src/encryption/encryptStrings.cpp
71+
src/fileShredder/fileShredder.cpp
7172
src/passwordManager/passwordManager.cpp
7273
src/passwordManager/passwords.cpp
74+
src/privacyTracks/privacyTracks.cpp
75+
src/utils/utils.cpp
7376
src/main.cpp
7477
)
7578

76-
# C++20 Modules
77-
target_sources(privacyShield PRIVATE
78-
FILE_SET CXX_MODULES FILES
79-
src/duplicateFinder/duplicateFinder.cppm
80-
src/encryption/cryptoCipher.cppm
81-
src/encryption/encryption.cppm
82-
src/fileShredder/fileShredder.cppm
83-
src/passwordManager/FuzzyMatcher.cppm
84-
src/passwordManager/passwordManager.cppm
85-
src/privacyTracks/privacyTracks.cppm
86-
src/utils/utils.cppm
87-
src/secureAllocator.cppm
88-
src/mimallocSTL.cppm
89-
)
90-
9179
# Sanitizers for debugging and testing
9280
# Requires llvm-symbolizer and sanitizer libraries (asan, ubsan, msan, tsan)
9381
if (ENABLE_SANITIZERS)
@@ -111,6 +99,7 @@ if (ENABLE_SANITIZERS)
11199

112100
# Track mimalloc allocations for AddressSanitizer
113101
set(MI_TRACK_ASAN ON)
102+
set(MI_DEBUG_UBSAN ON)
114103

115104
# Link the enabled sanitizers.
116105
target_link_libraries(privacyShield PRIVATE asan ubsan)
@@ -161,6 +150,7 @@ if (NOT TARGET mimalloc-static OR NOT TARGET mimalloc)
161150
)
162151
endif ()
163152

153+
set(MI_SECURE ON)
164154
set(MI_BUILD_TESTS OFF) # Do not build tests
165155

166156
FetchContent_MakeAvailable(mimalloc)

CMakeModules/FindGcrypt.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
# Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
# Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by

CMakeModules/FindReadline.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
# Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
# Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by

CMakeModules/Packing.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
# Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
# Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
// Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
// Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
//
44
// This program is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU General Public License as published by
@@ -14,23 +14,16 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see https://www.gnu.org/licenses.
1616

17-
module;
18-
19-
#include <print>
2017
#include <fstream>
2118
#include <system_error>
2219
#include <thread>
23-
#include <vector>
24-
#include <unordered_map>
25-
#include <filesystem>
2620
#include <blake3.h>
2721
#include <cstring>
28-
#include <format>
2922
#include <ranges>
3023

31-
export module duplicateFinder;
32-
import utils;
33-
import mimallocSTL;
24+
#include "duplicateFinder.hpp"
25+
#include "../utils/utils.hpp"
26+
#include "../mimallocSTL.hpp"
3427

3528
namespace fs = std::filesystem;
3629

@@ -222,7 +215,7 @@ std::size_t findDuplicates(const fs::path &directoryPath) {
222215
}
223216

224217
/// \brief A simple duplicate file detective.
225-
export void duplicateFinder() {
218+
void duplicateFinder() {
226219
while (true) {
227220
std::print("\n-------------------");
228221
printColoredOutput('m', " Duplicate Finder ");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2+
// Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see https://www.gnu.org/licenses.
16+
17+
#pragma once
18+
19+
void duplicateFinder();
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
// Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
// Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
//
44
// This program is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU General Public License as published by
@@ -13,14 +13,12 @@
1313
//
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see https://www.gnu.org/licenses.
16-
module;
16+
#pragma once
1717

1818
#include <openssl/evp.h>
1919

20-
export module cryptoCipher;
21-
2220
/// \brief A class wrapper for OpenSSL cipher implementations and contexts.
23-
export class CryptoCipher final {
21+
class CryptoCipher final {
2422
public:
2523
// Default constructor
2624
constexpr CryptoCipher() noexcept = default;

src/encryption/encryptDecrypt.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
// Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
// Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
//
44
// This program is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU General Public License as published by
@@ -14,28 +14,22 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see https://www.gnu.org/licenses.
1616

17-
module;
1817

1918
#include <algorithm>
2019
#include <system_error>
2120
#include <utility>
22-
#include <format>
2321
#include <cmath>
24-
#include <unordered_map>
25-
#include <filesystem>
2622
#include <gcrypt.h>
2723
#include <sodium.h>
28-
#include <print>
2924

30-
import utils;
31-
import secureAllocator;
32-
import mimallocSTL;
33-
import passwordManager;
34-
35-
module encryption;
25+
#include "encryption.hpp"
26+
#include "../passwordManager/passwordManager.hpp"
27+
#include "../utils/utils.hpp"
3628

3729
namespace fs = std::filesystem;
3830

31+
// clang-format off
32+
3933
/// \brief Available encryption/decryption ciphers.
4034
enum class Algorithms : std::uint_fast8_t {
4135
AES = 1 << 0,
@@ -60,17 +54,18 @@ constexpr struct {
6054
const gcry_cipher_algos Twofish = GCRY_CIPHER_TWOFISH;
6155
} AlgoSelection;
6256

57+
// clang-format on
58+
6359

6460
/// \brief Formats a file size into a human-readable string.
6561
/// \param size The file size as an unsigned integer.
6662
/// \return A string representing the formatted file size.
67-
miSTL::string formatFileSize(const std::uintmax_t &size) {
63+
miSTL::string formatFileSize(const std::uintmax_t& size) {
6864
int i{};
6965
auto mantissa = static_cast<double>(size);
70-
for (; mantissa >= 1024.; mantissa /= 1024., ++i) {
71-
}
66+
for (; mantissa >= 1024.; mantissa /= 1024., ++i) {}
7267
mantissa = std::ceil(mantissa * 10.) / 10.;
73-
miSTL::string result { std::to_string(mantissa) + "BKMGTPE"[i]};
68+
miSTL::string result{std::to_string(mantissa) + "BKMGTPE"[i]};
7469
return i == 0 ? result : result + "B (" + std::to_string(size).c_str() + ')';
7570
}
7671

@@ -80,7 +75,7 @@ miSTL::string formatFileSize(const std::uintmax_t &size) {
8075
/// \throws std::invalid_argument if \p mode is invalid.
8176
/// \throws std::runtime_error if the input file does not exist, is a directory,
8277
/// is not a regular file, or is not readable.
83-
void checkInputFile(const fs::path &inFile, const OperationMode &mode) {
78+
void checkInputFile(const fs::path& inFile, const OperationMode& mode) {
8479
if (mode != OperationMode::Encryption && mode != OperationMode::Decryption)
8580
throw std::invalid_argument("Invalid mode of operation.");
8681

@@ -109,7 +104,7 @@ void checkInputFile(const fs::path &inFile, const OperationMode &mode) {
109104
/// \brief Creates non-existing parent directories for a file.
110105
/// \param filePath The file path for which the directory path needs to be created.
111106
/// \return True if the directory path is created successfully or already exists, false otherwise.
112-
bool createPath(const fs::path &filePath) noexcept {
107+
bool createPath(const fs::path& filePath) noexcept {
113108
if (filePath.string().empty()) return false; // Can't create empty paths
114109

115110
std::error_code ec;
@@ -136,7 +131,7 @@ bool createPath(const fs::path &filePath) noexcept {
136131
/// \param mode the mode of operation: encryption or decryption.
137132
/// \throws std::invalid_argument if \p mode is invalid.
138133
/// \throws std::runtime_error if the output file is not writable, readable, or there is not enough space to save it.
139-
inline void checkOutputFile(const fs::path &inFile, fs::path &outFile, const OperationMode &mode) {
134+
inline void checkOutputFile(const fs::path& inFile, fs::path& outFile, const OperationMode& mode) {
140135
if (mode != OperationMode::Encryption && mode != OperationMode::Decryption)
141136
throw std::invalid_argument("Invalid mode of operation.");
142137

@@ -212,8 +207,8 @@ inline void copyLastWrite(const std::string_view srcFile, const std::string_view
212207
/// \param password the password to use for encryption/decryption.
213208
/// \param algo the algorithm to use for encryption/decryption.
214209
/// \param mode the mode of operation: encryption or decryption.
215-
void fileEncryptionDecryption(const miSTL::string &inputFileName, const miSTL::string &outputFileName,
216-
const privacy::string &password, const Algorithms &algo, const OperationMode &mode) {
210+
void fileEncryptionDecryption(const miSTL::string& inputFileName, const miSTL::string& outputFileName,
211+
const privacy::string& password, const Algorithms& algo, const OperationMode& mode) {
217212
// The mode must be valid: must be either encryption or decryption
218213
if (mode != OperationMode::Encryption && mode != OperationMode::Decryption) [[unlikely]] {
219214
printColoredErrorln('r', "Invalid mode of operation.");
@@ -222,15 +217,15 @@ void fileEncryptionDecryption(const miSTL::string &inputFileName, const miSTL::s
222217

223218
try {
224219
/// Encrypts/decrypts a file based on the passed mode and algorithm.
225-
auto encryptDecrypt = [&](const miSTL::string &algorithm) -> void {
220+
auto encryptDecrypt = [&](const miSTL::string& algorithm) -> void {
226221
if (mode == OperationMode::Encryption) // Encryption
227222
encryptFile(inputFileName, outputFileName, password, algorithm);
228223
else // Decryption
229224
decryptFile(inputFileName, outputFileName, password, algorithm);
230225
};
231226

232227
/// Encrypts/decrypts a file using a cipher with more rounds.
233-
auto encryptDecryptMoreRounds = [&](const gcry_cipher_algos &algorithm) -> void {
228+
auto encryptDecryptMoreRounds = [&](const gcry_cipher_algos& algorithm) -> void {
234229
if (mode == OperationMode::Encryption) // Encryption
235230
encryptFileWithMoreRounds(inputFileName, outputFileName, password, algorithm);
236231
else // Decryption
@@ -264,11 +259,11 @@ void fileEncryptionDecryption(const miSTL::string &inputFileName, const miSTL::s
264259
// Preserve file permissions
265260
if (!copyFilePermissions(inputFileName, outputFileName))
266261
[[unlikely]]
267-
printColoredOutputln('m', "Check the permissions of the {}crypted file.", pre);
262+
printColoredOutputln('m', "Check the permissions of the {}crypted file.", pre);
268263

269264
// Try to preserve the time of last modification
270265
copyLastWrite(inputFileName, outputFileName);
271-
} catch (const std::exception &ex) {
266+
} catch (const std::exception& ex) {
272267
printColoredErrorln('r', "Error: {}", ex.what());
273268
}
274269
}
@@ -371,10 +366,11 @@ void encryptDecrypt() {
371366
printColoredOutput('c', "{}", algoDescription.find(cipher)->second);
372367
printColoredOutputln('g', "...");
373368

374-
fileEncryptionDecryption(canonical(inputPath).string().c_str(), weakly_canonical(outputPath).string().c_str(),
369+
fileEncryptionDecryption(canonical(inputPath).string().c_str(),
370+
weakly_canonical(outputPath).string().c_str(),
375371
password, cipher, static_cast<OperationMode>(choice));
376372
std::println("");
377-
} catch (const std::exception &ex) {
373+
} catch (const std::exception& ex) {
378374
printColoredError('y', "Error: ");
379375
printColoredErrorln('r', "{}", ex.what());
380376
}

src/encryption/encryptFiles.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2-
// Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+
// Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33
//
44
// This program is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU General Public License as published by
@@ -14,30 +14,27 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see https://www.gnu.org/licenses.
1616

17-
module;
18-
1917
#include <iostream>
2018
#include <fstream>
2119
#include <memory>
2220
#include <openssl/kdf.h>
2321
#include <openssl/core_names.h>
2422
#include <openssl/rand.h>
2523
#include <sodium.h>
26-
#include <format>
2724
#include <mutex>
28-
#include <vector>
25+
#include "encryption.hpp"
26+
#include "cryptoCipher.hpp"
2927
#include <gcrypt.h>
3028

31-
import cryptoCipher;
32-
import secureAllocator;
33-
import mimallocSTL;
34-
35-
module encryption;
3629

3730
constexpr int MAX_KEY_SIZE = EVP_MAX_KEY_LENGTH; ///< Maximum length of a key
3831
constexpr std::streamsize CHUNK_SIZE = 4096; ///< Read/Write files in chunks of 4 kB
3932
constexpr unsigned int PBKDF2_ITERATIONS = 100'000; ///< Iterations for PBKDF2 key derivation
4033

34+
// OpenSSL's library context and property query string
35+
static OSSL_LIB_CTX* libContext = nullptr;
36+
constexpr static char* propertyQuery = nullptr;
37+
4138

4239
/// \brief Generates random bytes using a CSPRNG.
4340
/// \param saltSize number of bytes of salt to generate.

0 commit comments

Comments
 (0)