Skip to content

Commit 77d3376

Browse files
committed
Various cleanups
Signed-off-by: Raul Metsma <[email protected]>
1 parent 86b154a commit 77d3376

File tree

13 files changed

+44
-124
lines changed

13 files changed

+44
-124
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ jobs:
6969
- name: Prepare vcpkg
7070
uses: lukka/run-vcpkg@v11
7171
with:
72-
vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33
7372
vcpkgJsonGlob: ./vcpkg.json
7473
runVcpkgInstall: true
7574
env:
@@ -116,7 +115,6 @@ jobs:
116115
if: matrix.target != 'macos'
117116
uses: lukka/run-vcpkg@v11
118117
with:
119-
vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33
120118
vcpkgJsonGlob: ./vcpkg.json
121119
runVcpkgInstall: true
122120
env:
@@ -158,7 +156,6 @@ jobs:
158156
- name: Prepare vcpkg
159157
uses: lukka/run-vcpkg@v11
160158
with:
161-
vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33
162159
vcpkgJsonGlob: ./vcpkg.json
163160
runVcpkgInstall: true
164161
runVcpkgFormatString: "[`install`, `--recurse`, `--clean-after-build`, `--x-install-root`, `$[env.VCPKG_INSTALLED_DIR]`, `--triplet`, `$[env.VCPKG_DEFAULT_TRIPLET]`, `--x-feature`, `tests`]"

CMakePresets.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"cacheVariables": {
3030
"CMAKE_OSX_ARCHITECTURES": "arm64",
31-
"CMAKE_OSX_DEPLOYMENT_TARGET": "12.0",
31+
"CMAKE_OSX_DEPLOYMENT_TARGET": "13.3",
3232
"CMAKE_FIND_ROOT_PATH": "$env{DEST};/opt/homebrew",
3333
"FRAMEWORK_DESTINATION": "$env{DEST}/lib"
3434
},
@@ -56,7 +56,8 @@
5656
"CMAKE_SYSTEM_NAME": "iOS",
5757
"CMAKE_OSX_SYSROOT": "${presetName}",
5858
"CMAKE_OSX_DEPLOYMENT_TARGET": "15.0",
59-
"CMAKE_DISABLE_FIND_PACKAGE_SWIG": "YES"
59+
"CMAKE_DISABLE_FIND_PACKAGE_SWIG": "YES",
60+
"CMAKE_CXX_FLAGS": "-D_LIBCPP_DISABLE_AVAILABILITY"
6061
}
6162
},
6263
{
@@ -140,4 +141,4 @@
140141
"configurePreset": "androidx86_64"
141142
}
142143
]
143-
}
144+
}

cdoc/CDoc.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ libcdoc::CDocReader::getCDocFileVersion(const std::string& path)
9898
libcdoc::CDocReader *
9999
libcdoc::CDocReader::createReader(DataSource *src, bool take_ownership, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network)
100100
{
101+
if(!src)
102+
return nullptr;
101103
int version = getCDocFileVersion(src);
102104
LOG_DBG("CDocReader::createReader: version {}", version);
103105
if (src->seek(0) != libcdoc::OK) return nullptr;
@@ -106,7 +108,9 @@ libcdoc::CDocReader::createReader(DataSource *src, bool take_ownership, Configur
106108
reader = new CDoc1Reader(src, take_ownership);
107109
} else if (version == 2) {
108110
reader = new CDoc2Reader(src, take_ownership);
109-
} else {
111+
} else {
112+
if(take_ownership)
113+
delete src;
110114
return nullptr;
111115
}
112116
reader->conf = conf;
@@ -118,12 +122,18 @@ libcdoc::CDocReader::createReader(DataSource *src, bool take_ownership, Configur
118122
libcdoc::CDocReader *
119123
libcdoc::CDocReader::createReader(const std::string& path, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network)
120124
{
121-
int version = getCDocFileVersion(path);
125+
if(path.empty())
126+
return nullptr;
127+
auto isrc = make_unique<IStreamSource>(path);
128+
int version = getCDocFileVersion(isrc.get());
129+
LOG_DBG("CDocReader::createReader: version {}", version);
130+
if (isrc->seek(0) != libcdoc::OK)
131+
return nullptr;
122132
CDocReader *reader;
123133
if (version == 1) {
124-
reader = new CDoc1Reader(path);
134+
reader = new CDoc1Reader(isrc.release(), true);
125135
} else if (version == 2) {
126-
reader = new CDoc2Reader(path);
136+
reader = new CDoc2Reader(isrc.release(), true);
127137
} else {
128138
return nullptr;
129139
}
@@ -138,6 +148,7 @@ libcdoc::CDocReader::createReader(std::istream& ifs, Configuration *conf, Crypto
138148
{
139149
libcdoc::IStreamSource *isrc = new libcdoc::IStreamSource(&ifs, false);
140150
int version = getCDocFileVersion(isrc);
151+
LOG_DBG("CDocReader::createReader: version {}", version);
141152
CDocReader *reader;
142153
if (version == 1) {
143154
reader = new CDoc1Reader(isrc, true);

cdoc/CDoc1Reader.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,6 @@ CDoc1Reader::~CDoc1Reader()
340340
delete d;
341341
}
342342

343-
CDoc1Reader::CDoc1Reader(const std::string &path)
344-
: CDoc1Reader(new libcdoc::IStreamSource(path), true)
345-
{
346-
}
347-
348343
bool
349344
CDoc1Reader::isCDoc1File(libcdoc::DataSource *src)
350345
{

cdoc/CDoc1Reader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class CDoc1Reader : public libcdoc::CDocReader
2626
{
2727
public:
2828
CDoc1Reader(libcdoc::DataSource *src, bool take_ownership = false);
29-
CDoc1Reader(const std::string& path);
3029
~CDoc1Reader();
3130

3231
const std::vector<libcdoc::Lock>& getLocks() override final;

cdoc/CDoc2Reader.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -677,21 +677,15 @@ CDoc2Reader::CDoc2Reader(libcdoc::DataSource *src, bool take_ownership)
677677
}
678678
}
679679

680-
CDoc2Reader::CDoc2Reader(const std::string &path)
681-
: CDoc2Reader(new libcdoc::IStreamSource(path), true)
682-
{
683-
}
684-
685680
bool
686681
CDoc2Reader::isCDoc2File(libcdoc::DataSource *src)
687682
{
688-
uint8_t in[libcdoc::CDoc2::LABEL.size()];
689-
constexpr size_t len = libcdoc::CDoc2::LABEL.size();
690-
if (src->read(&in[0], len) != len) {
691-
LOG_DBG("CDoc2Reader::isCDoc1File: Cannot read tag");
683+
std::array<uint8_t,libcdoc::CDoc2::LABEL.size()> in {};
684+
if (src->read(in.data(), in.size()) != in.size()) {
685+
LOG_DBG("CDoc2Reader::isCDoc2File: Cannot read tag");
692686
return false;
693687
}
694-
if (libcdoc::CDoc2::LABEL.compare(0, len, (char *) &in[0], len)) {
688+
if (libcdoc::CDoc2::LABEL.compare(0, in.size(), (char *) in.data(), in.size())) {
695689
LOG_DBG("CDoc2Reader::isCDoc2File: Invalid tag: {}", toHex(in));
696690
return false;
697691
}

cdoc/CDoc2Reader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class CDoc2Reader final: public libcdoc::CDocReader {
3939
libcdoc::result_t finishDecryption() override final;
4040

4141
CDoc2Reader(libcdoc::DataSource *src, bool take_ownership = false);
42-
CDoc2Reader(const std::string &path);
4342

4443
static bool isCDoc2File(const std::string& path);
4544
static bool isCDoc2File(libcdoc::DataSource *src);

cdoc/Crypto.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
#define OPENSSL_SUPPRESS_DEPRECATED
2525

26+
#ifdef _WIN32
27+
#include <windows.h> // For RAND_screen
28+
#endif
29+
2630
#include <openssl/aes.h>
2731
#include <openssl/err.h>
2832
#include <openssl/kdf.h>

cdoc/PKCS11Backend.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
#include <functional>
3838

3939
#ifdef _WIN32
40-
//#include <Windows.h>
41-
//#include <wincrypt.h>
42-
//#include <cryptuiapi.h>
40+
#include <Windows.h>
4341
#else
4442
#include <dlfcn.h>
4543
#endif

cdoc/Utils.h

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
#include <iostream>
2626
#include <sstream>
2727

28-
#ifdef _WIN32
29-
#include <Windows.h>
30-
#endif
31-
3228
namespace libcdoc {
3329

3430
std::string toBase64(const uint8_t *data, size_t len);
@@ -79,7 +75,7 @@ join(const std::vector<std::string> &parts, const std::string_view sep)
7975
if (part != parts.front()) result += sep;
8076
result += part;
8177
}
82-
return std::move(result);
78+
return result;
8379
}
8480

8581
std::vector<std::string> JsonToStringArray(std::string_view json);
@@ -123,83 +119,7 @@ std::string buildURL(const std::string& host, int port);
123119
std::string urlEncode(std::string_view src);
124120
std::string urlDecode(const std::string &src);
125121

126-
#ifdef _WIN32
127-
128-
static std::wstring toWide(UINT codePage, const std::string &in)
129-
{
130-
std::wstring result;
131-
if(in.empty())
132-
return result;
133-
int len = MultiByteToWideChar(codePage, 0, in.data(), int(in.size()), nullptr, 0);
134-
result.resize(size_t(len), 0);
135-
len = MultiByteToWideChar(codePage, 0, in.data(), int(in.size()), &result[0], len);
136-
return result;
137-
}
138-
139-
static std::wstring
140-
toWide(const std::string& in)
141-
{
142-
return toWide(CP_UTF8, in);
143-
}
144-
145-
static std::string toMultiByte(UINT codePage, const std::wstring &in)
146-
{
147-
std::string result;
148-
if(in.empty())
149-
return result;
150-
int len = WideCharToMultiByte(codePage, 0, in.data(), int(in.size()), nullptr, 0, nullptr, nullptr);
151-
result.resize(size_t(len), 0);
152-
len = WideCharToMultiByte(codePage, 0, in.data(), int(in.size()), &result[0], len, nullptr, nullptr);
153-
return result;
154-
}
155-
156-
static std::string
157-
toUTF8(const std::wstring& in)
158-
{
159-
return toMultiByte(CP_UTF8, in);
160-
}
161-
162-
163-
#endif
164-
165-
static std::string toUTF8(const std::string &in)
166-
{
167-
#ifdef _WIN32
168-
return toMultiByte(CP_UTF8, toWide(CP_ACP, in));
169-
#else
170-
return in;
171-
#endif
172-
}
173-
174-
static std::vector<unsigned char> readFile(const std::string &path)
175-
{
176-
std::vector<unsigned char> data;
177-
#ifdef _WIN32
178-
std::ifstream f(toWide(CP_UTF8, path).c_str(), std::ifstream::binary);
179-
#else
180-
std::ifstream f(path, std::ifstream::binary);
181-
#endif
182-
if (!f)
183-
return data;
184-
f.seekg(0, std::ifstream::end);
185-
data.resize(size_t(f.tellg()));
186-
f.clear();
187-
f.seekg(0);
188-
f.read((char*)data.data(), std::streamsize(data.size()));
189-
return data;
190-
}
191-
192-
static void writeFile(const std::string &path, const std::vector<unsigned char> &data)
193-
{
194-
#ifdef _WIN32
195-
std::ofstream f(toWide(CP_UTF8, path).c_str(), std::ofstream::binary);
196-
#else
197-
std::ofstream f(path.c_str(), std::ofstream::binary);
198-
#endif
199-
f.write((const char*)data.data(), std::streamsize(data.size()));
200-
}
201-
202-
} // vectorwrapbuf
122+
} // namespace libcdoc
203123

204124
// A source implementation that always keeps last 16 bytes in tag
205125

0 commit comments

Comments
 (0)