Skip to content

Commit

Permalink
Remove ubuntu 18.04 and cleanup code
Browse files Browse the repository at this point in the history
IB-7398

Signed-off-by: Raul Metsma <raul@metsma.ee>
  • Loading branch information
metsma committed Jan 4, 2023
1 parent 58ef5e0 commit c89e256
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 151 deletions.
14 changes: 4 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,18 @@ jobs:
container: ${{ matrix.container }}
strategy:
matrix:
container: ['ubuntu:18.04', 'ubuntu:20.04', 'ubuntu:22.04', 'ubuntu:22.10']
container: ['ubuntu:20.04', 'ubuntu:22.04', 'ubuntu:22.10']
env:
DEBIAN_FRONTEND: noninteractive
DEBFULLNAME: github-actions
DEBEMAIL: github-actions@github.com
steps:
- name: Install dependencies
run: apt update -qq && apt install --no-install-recommends -y git lsb-release fakeroot build-essential devscripts cdbs cmake xxd xsdcxx libxml-security-c-dev zlib1g-dev doxygen swig openjdk-8-jdk-headless libpython3-dev python3-distutils libboost-test-dev lintian
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v3
with:
submodules: recursive
- name: Add open-eid repo
if: matrix.container == 'ubuntu:18.04'
run: |
apt update -qq && apt install --no-install-recommends -y curl ca-certificates gnupg2 lsb-release
curl https://installer.id.ee/media/install-scripts/C6C83D68.pub | gpg --dearmor | tee /etc/apt/trusted.gpg.d/ria-repository.gpg > /dev/null
echo "deb https://installer.id.ee/media/ubuntu/ $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/ria-repository.list
- name: Install dependencies
run: apt update -qq && apt install --no-install-recommends -y lsb-release fakeroot build-essential devscripts cdbs cmake xxd xsdcxx libxml-security-c-dev zlib1g-dev doxygen swig openjdk-8-jdk-headless libpython3-dev python3-distutils libboost-test-dev lintian
- name: Setup changelog
run: |
export VERSION=$(grep project CMakeLists.txt | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
Expand Down
4 changes: 1 addition & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ target_link_libraries(digidocpp PRIVATE ${CMAKE_DL_LIBS} minizip digidocpp_priv)

if( BUILD_TOOLS )
add_executable(digidoc-tool digidoc-tool.rc digidoc-tool.cpp)
target_link_libraries(digidoc-tool digidocpp digidocpp_priv Threads::Threads
"$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:-lstdc++fs>"
)
target_link_libraries(digidoc-tool digidocpp digidocpp_priv Threads::Threads)
configure_file( digidoc-tool.1.cmake digidoc-tool.1 )
endif()

Expand Down
10 changes: 3 additions & 7 deletions src/DataFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "util/File.h"
#include "util/log.h"

#include <array>
#include <fstream>

using namespace digidoc;
Expand Down Expand Up @@ -86,13 +87,11 @@ DataFile::DataFile() = default;
DataFile::~DataFile() = default;


DataFilePrivate::DataFilePrivate(unique_ptr<istream> is, string filename, string mediatype,
string id, vector<unsigned char> digestValue)
DataFilePrivate::DataFilePrivate(unique_ptr<istream> &&is, string filename, string mediatype, string id)
: m_is(move(is))
, m_id(move(id))
, m_filename(move(filename))
, m_mediatype(move(mediatype))
, m_digestValue(move(digestValue))
{
m_is->seekg(0, istream::end);
istream::pos_type pos = m_is->tellg();
Expand All @@ -101,16 +100,14 @@ DataFilePrivate::DataFilePrivate(unique_ptr<istream> is, string filename, string

vector<unsigned char> DataFilePrivate::calcDigest(const string &method) const
{
if(!m_digestValue.empty())
return m_digestValue;
Digest calc(method);
calcDigest(&calc);
return calc.result();
}

void DataFilePrivate::calcDigest(Digest *digest) const
{
vector<unsigned char> buf(10240, 0);
array<unsigned char, 10240> buf{};
m_is->clear();
m_is->seekg(0);
while(*m_is)
Expand All @@ -125,7 +122,6 @@ void DataFilePrivate::saveAs(const string& path) const
{
ofstream ofs(File::encodeName(path).c_str(), ofstream::binary);
saveAs(ofs);
ofs.close();
}

void DataFilePrivate::saveAs(ostream &os) const
Expand Down
20 changes: 9 additions & 11 deletions src/DataFile_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,23 @@ namespace digidoc
{

class Digest;
class DataFilePrivate: public DataFile
class DataFilePrivate final: public DataFile
{
public:
DataFilePrivate(std::unique_ptr<std::istream> is, std::string filename, std::string mediatype, std::string id = {},
std::vector<unsigned char> digestValue = {});
DataFilePrivate(std::unique_ptr<std::istream> &&is, std::string filename, std::string mediatype, std::string id = {});

std::string id() const override { return m_id.empty() ? m_filename : m_id; }
std::string fileName() const override { return m_filename; }
unsigned long fileSize() const override { return m_size; }
std::string mediaType() const override { return m_mediatype; }
std::string id() const final { return m_id.empty() ? m_filename : m_id; }
std::string fileName() const final { return m_filename; }
unsigned long fileSize() const final { return m_size; }
std::string mediaType() const final { return m_mediatype; }

std::vector<unsigned char> calcDigest(const std::string &method) const override;
std::vector<unsigned char> calcDigest(const std::string &method) const final;
void calcDigest(Digest *method) const;
void saveAs(std::ostream &os) const override;
void saveAs(const std::string& path) const override;
void saveAs(std::ostream &os) const final;
void saveAs(const std::string& path) const final;

std::unique_ptr<std::istream> m_is;
std::string m_id, m_filename, m_mediatype;
std::vector<unsigned char> m_digestValue;
unsigned long m_size;
};
}
37 changes: 12 additions & 25 deletions src/digidoc-tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@
#include "util/log.h"

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <filesystem>
#include <iostream>
#include <map>
#include <optional>
#include <sstream>

Expand All @@ -51,13 +48,7 @@
using namespace digidoc;
using namespace digidoc::util;
using namespace std;
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
namespace fs = filesystem;

namespace std
{
Expand Down Expand Up @@ -128,11 +119,6 @@ static ostream &operator<<(ostream &os, Signature::Validator::Status status)
}
return os;
}

static string& operator+(string_view lhs, string rhs)
{
return rhs.insert(0, lhs);
}
}

/**
Expand Down Expand Up @@ -495,9 +481,8 @@ unique_ptr<Signer> ToolConfig::getSigner(bool getwebsigner) const
win->setThumbprint(thumbprint);
signer = unique_ptr<Signer>(win.release());
}
else
#endif
if(!pkcs12.empty())
else if(!pkcs12.empty())
signer = make_unique<PKCS12Signer>(pkcs12, pin);
else
signer = make_unique<ConsolePinSigner>(pkcs11, pin);
Expand Down Expand Up @@ -556,7 +541,7 @@ static int open(int argc, char* argv[])
{
ToolConfig::Warning reportwarnings = ToolConfig::WWarning;
string path, policy;
string_view extractPath;
fs::path extractPath;
bool validateOnExtract = false;
int returnCode = EXIT_SUCCESS;

Expand All @@ -573,11 +558,13 @@ static int open(int argc, char* argv[])
}
else if(arg.find("--extractAll") == 0)
{
extractPath = ".";
size_t pos = arg.find('=');
if(pos != string::npos)
extractPath = arg.substr(pos + 1);
if(!fs::is_directory(fs::u8path(extractPath)))
extractPath = fs::current_path();
if(auto pos = arg.find('='); pos != string::npos)
{
fs::path newPath = fs::u8path(arg.substr(pos + 1));
extractPath = newPath.is_relative() ? extractPath / newPath : newPath;
}
if(!fs::is_directory(extractPath))
THROW("Path is not directory");
}
else if(arg == "--validateOnExtract")
Expand Down Expand Up @@ -605,7 +592,7 @@ static int open(int argc, char* argv[])
for(const DataFile *file: doc->dataFiles())
{
try {
string dst = extractPath + "/" + File::fileName(file->fileName());
string dst = (extractPath / fs::u8path(file->fileName()).filename()).u8string();
file->saveAs(dst);
cout << " Document(" << file->mediaType() << ") extracted to " << dst << " (" << file->fileSize() << " bytes)" << endl;
} catch(const Exception &e) {
Expand Down
101 changes: 9 additions & 92 deletions src/util/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <algorithm>
#include <ctime>
#include <filesystem>
#include <locale>
#include <sstream>
#include <sys/stat.h>
Expand All @@ -45,6 +46,7 @@
using namespace digidoc;
using namespace digidoc::util;
using namespace std;
namespace fs = filesystem;

#ifdef _WIN32
#define f_stat _wstat64
Expand All @@ -58,77 +60,6 @@ using f_statbuf = struct stat;
using f_utimbuf = struct utimbuf;
#endif

#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__)
#include <cerrno>
#include <iconv.h>
#include <cstdlib>
#include <cstring>
#include <langinfo.h>

/**
* Helper method for converting from non-UTF-8 encoded strings to UTF-8.
* Supported LANG values for Linux: see /usr/share/i18n/SUPPORTED.
* Supported encodings for libiconv: see iconv --list .
*
* Note! If non-ASCII characters are used we assume a proper LANG value!!!
*
* @param str_in The string to be converted.
* @return Returns the input string in UTF-8.
*/
string File::convertUTF8(string_view str_in, bool to_UTF)
{
string charset = nl_langinfo(CODESET);
// no conversion needed for UTF-8
if(charset == "UTF-8" || charset == "utf-8")
return string(str_in);

iconv_t ic_descr = iconv_t(-1);
try
{
ic_descr = to_UTF ? iconv_open("UTF-8", charset.c_str()) : iconv_open(charset.c_str(), "UTF-8");
}
catch(exception &) {}

if(ic_descr == iconv_t(-1))
return string(str_in);

char* inptr = (char*)str_in.data();
size_t inleft = str_in.size();

string out;
char outbuf[64];
char* outptr;
size_t outleft;

while(inleft > 0)
{
outbuf[0] = '\0';
outptr = (char *)outbuf;
outleft = sizeof(outbuf) - sizeof(outbuf[0]);

size_t result = iconv(ic_descr, &inptr, &inleft, &outptr, &outleft);
if(result == size_t(-1))
{
switch(errno)
{
case E2BIG: break;
case EILSEQ:
case EINVAL:
default:
iconv_close(ic_descr);
return string(str_in);
break;
}
}
*outptr = '\0';
out += outbuf;
}
iconv_close(ic_descr);

return out;
}
#endif

stack<string> File::tempFiles;

string File::confPath()
Expand Down Expand Up @@ -167,23 +98,15 @@ File::f_string File::encodeName(string_view fileName)
{
if(fileName.empty())
return {};
#if defined(_WIN32)
int len = MultiByteToWideChar(CP_UTF8, 0, fileName.data(), int(fileName.size()), nullptr, 0);
f_string out(size_t(len), 0);
len = MultiByteToWideChar(CP_UTF8, 0, fileName.data(), int(fileName.size()), out.data(), len);
#elif defined(__APPLE__)
CFMutableStringRef ref = CFStringCreateMutable(nullptr, 0);
CFStringAppendCString(ref, fileName.data(), kCFStringEncodingUTF8);
CFStringNormalize(ref, kCFStringNormalizationFormD);

#ifdef __APPLE__
CFStringRef ref = CFStringCreateWithBytesNoCopy({}, (UInt8 *)fileName.data(),
CFIndex(fileName.size()), kCFStringEncodingUTF8, FALSE, kCFAllocatorNull);
string out(fileName.size() * 2, 0);
CFStringGetCString(ref, out.data(), CFIndex(out.size()), kCFStringEncodingUTF8);
CFStringGetFileSystemRepresentation(ref, out.data(), CFIndex(out.size()));
CFRelease(ref);
out.resize(strlen(out.c_str()));
#elif defined(__ANDROID__)
f_string out = string(fileName);
#else
f_string out = convertUTF8(fileName,false);
f_string out = fs::u8path(fileName);
#endif
return out;
}
Expand All @@ -197,11 +120,7 @@ string File::decodeName(const f_string_view &localFileName)
{
if(localFileName.empty())
return {};
#if defined(_WIN32)
int len = WideCharToMultiByte(CP_UTF8, 0, localFileName.data(), int(localFileName.size()), nullptr, 0, nullptr, nullptr);
string out(size_t(len), 0);
WideCharToMultiByte(CP_UTF8, 0, localFileName.data(), int(localFileName.size()), out.data(), len, nullptr, nullptr);
#elif defined(__APPLE__)
#ifdef __APPLE__
CFMutableStringRef ref = CFStringCreateMutable(nullptr, 0);
CFStringAppendCString(ref, localFileName.data(), kCFStringEncodingUTF8);
CFStringNormalize(ref, kCFStringNormalizationFormC);
Expand All @@ -210,10 +129,8 @@ string File::decodeName(const f_string_view &localFileName)
CFStringGetCString(ref, out.data(), CFIndex(out.size()), kCFStringEncodingUTF8);
CFRelease(ref);
out.resize(strlen(out.c_str()));
#elif defined(__ANDROID__)
string out = string(localFileName);
#else
string out = convertUTF8(localFileName,true);
string out = fs::path(localFileName).u8string();
#endif
return out;
}
Expand Down
3 changes: 0 additions & 3 deletions src/util/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ namespace digidoc
#endif

private:
#if !defined(_WIN32) && !defined(__APPLE__)
static std::string convertUTF8(std::string_view str_in, bool to_UTF);
#endif
static std::stack<std::string> tempFiles;
};

Expand Down

0 comments on commit c89e256

Please sign in to comment.