Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Sep 29, 2023
1 parent 89f7691 commit 08a3d41
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 129 deletions.
1 change: 0 additions & 1 deletion crate/capi/examples/abcrypt.h

This file was deleted.

62 changes: 19 additions & 43 deletions crate/capi/examples/decrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <termios.h>
#include <unistd.h>

#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
Expand Down Expand Up @@ -47,45 +49,24 @@ int main(int argc, char *argv[]) {

char *input_filename;
char *output_filename;
switch (argc - optind) {
case 0:
case 1:
std::clog
<< "Error: the following required arguments were not provided:\n";
if ((argc - optind) == 0) {
std::clog << " <INFILE>\n";
}
std::clog << " <OUTFILE>\n\n";
std::clog << "Usage: decrypt <INFILE> <OUTFILE>\n\n";
std::clog << "For more information, try '-h'." << std::endl;
return EXIT_FAILURE;
case 2:
input_filename = argv[optind];
output_filename = argv[optind + 1];
break;
default:
std::clog << fmt::format("Error: unexpected argument '{}' found\n\n",
argv[optind + 2]);
std::clog << "Usage: decrypt <INFILE> <OUTFILE>\n\n";
std::clog << "For more information, try '-h'." << std::endl;
return EXIT_FAILURE;
if ((argc - optind) == 2) {
input_filename = argv[optind];
output_filename = argv[optind + 1];
} else {
print_help();
return EXIT_FAILURE;
}

std::ifstream input_file(input_filename);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}", input_filename)
std::clog << fmt::format("Error: could not open {}: {}", input_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
std::vector<std::uint8_t> ciphertext(
(std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>());
if (!input_file) {
std::clog << fmt::format("Error: could not read data from {}",
input_filename)
<< std::endl;
return EXIT_FAILURE;
}

struct termios term;
struct termios old_term;
Expand All @@ -109,38 +90,33 @@ int main(int argc, char *argv[]) {
std::vector<std::uint8_t> buf(abcrypt_error_message_out_len(error_code));
abcrypt_error_message(error_code, buf.data(), buf.size());
std::string error_message(std::cbegin(buf), std::cend(buf));
std::clog << "Error: ";
switch (error_code) {
case ABCRYPT_ERROR_CODE_INVALID_HEADER_MAC:
std::clog << "passphrase is incorrect";
std::clog << fmt::format("Error: passphrase is incorrect: {}",
error_message)
<< std::endl;
break;
case ABCRYPT_ERROR_CODE_INVALID_MAC:
std::clog << fmt::format("{} is corrupted", input_filename)
std::clog << fmt::format("Error: {} is corrupted: {}", input_filename,
error_message)
<< std::endl;
break;
default:
std::clog << fmt::format("the header in {} is invalid", input_filename)
std::clog << fmt::format("Error: the header in {} is invalid: {}",
input_filename, error_message)
<< std::endl;
break;
}
std::clog << "\n\n";
std::clog << "Caused by:\n";
std::clog << " " << error_message << std::endl;
return EXIT_FAILURE;
}

std::ofstream output_file(output_filename);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}", output_filename)
std::clog << fmt::format("Error: could not open {}: {}", output_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
std::ostreambuf_iterator<char> output_file_iter(output_file);
std::copy(std::cbegin(plaintext), std::cend(plaintext), output_file_iter);
if (!input_file) {
std::clog << fmt::format("Error: could not write the result to {}",
output_filename)
<< std::endl;
return EXIT_FAILURE;
}
}
48 changes: 12 additions & 36 deletions crate/capi/examples/encrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <termios.h>
#include <unistd.h>

#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
Expand Down Expand Up @@ -63,45 +65,24 @@ int main(int argc, char *argv[]) {

char *input_filename;
char *output_filename;
switch (argc - optind) {
case 0:
case 1:
std::clog
<< "Error: the following required arguments were not provided:\n";
if ((argc - optind) == 0) {
std::clog << " <INFILE>\n";
}
std::clog << " <OUTFILE>\n\n";
std::clog << "Usage: encrypt [OPTIONS] <INFILE> <OUTFILE>\n\n";
std::clog << "For more information, try '-h'." << std::endl;
return EXIT_FAILURE;
case 2:
input_filename = argv[optind];
output_filename = argv[optind + 1];
break;
default:
std::clog << fmt::format("Error: unexpected argument '{}' found\n\n",
argv[optind + 2]);
std::clog << "Usage: encrypt [OPTIONS] <INFILE> <OUTFILE>\n\n";
std::clog << "For more information, try '-h'." << std::endl;
return EXIT_FAILURE;
if ((argc - optind) == 2) {
input_filename = argv[optind];
output_filename = argv[optind + 1];
} else {
print_help();
return EXIT_FAILURE;
}

std::ifstream input_file(input_filename);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}", input_filename)
std::clog << fmt::format("Error: could not open {}: {}", input_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
std::vector<std::uint8_t> plaintext(
(std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>());
if (!input_file) {
std::clog << fmt::format("Error: could not read data from {}",
input_filename)
<< std::endl;
return EXIT_FAILURE;
}

struct termios term;
struct termios old_term;
Expand Down Expand Up @@ -141,16 +122,11 @@ int main(int argc, char *argv[]) {

std::ofstream output_file(output_filename);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}", output_filename)
std::clog << fmt::format("Error: could not open {}: {}", output_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
std::ostreambuf_iterator<char> output_file_iter(output_file);
std::copy(std::cbegin(ciphertext), std::cend(ciphertext), output_file_iter);
if (!input_file) {
std::clog << fmt::format("Error: could not write the result to {}",
output_filename)
<< std::endl;
return EXIT_FAILURE;
}
}
39 changes: 12 additions & 27 deletions crate/capi/examples/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include <fmt/core.h>
#include <unistd.h>

#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
Expand Down Expand Up @@ -44,40 +46,23 @@ int main(int argc, char *argv[]) {
}

char *input_filename;
switch (argc - optind) {
case 0:
std::clog
<< "Error: the following required arguments were not provided:\n";
std::clog << " <FILE>\n\n";
std::clog << "Usage: info <FILE>\n\n";
std::clog << "For more information, try '-h'." << std::endl;
return EXIT_FAILURE;
case 1:
input_filename = argv[optind];
break;
default:
std::clog << fmt::format("Error: unexpected argument '{}' found\n\n",
argv[optind + 1]);
std::clog << "Usage: info <FILE>\n\n";
std::clog << "For more information, try '-h'." << std::endl;
return EXIT_FAILURE;
if ((argc - optind) == 1) {
input_filename = argv[optind];
} else {
print_help();
return EXIT_FAILURE;
}

std::ifstream input_file(input_filename);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}", input_filename)
std::clog << fmt::format("Error: could not open {}: {}", input_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
std::vector<std::uint8_t> contents{
(std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>()};
if (!input_file) {
std::clog << fmt::format("Error: could not read data from {}",
input_filename)
<< std::endl;
return EXIT_FAILURE;
}

auto params = abcrypt_params_new();
auto error_code =
Expand All @@ -87,9 +72,9 @@ int main(int argc, char *argv[]) {
abcrypt_error_message(error_code, buf.data(), buf.size());
std::string error_message(std::cbegin(buf), std::cend(buf));
std::clog << fmt::format(
"Error: {} is not a valid Argon2 encrypted file\n\n", input_filename);
std::clog << "Caused by:\n";
std::clog << " " << error_message << std::endl;
"Error: {} is not a valid Argon2 encrypted file: {}",
input_filename, error_message)
<< std::endl;
abcrypt_params_free(params);
return EXIT_FAILURE;
}
Expand Down
25 changes: 10 additions & 15 deletions crate/capi/examples/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ else
libdir = libdir / 'debug'
endif
libabcrypt_capi = cpp.find_library('libabcrypt_capi', dirs: libdir, static: true)
incdir = include_directories('../include')

executable('encrypt',
'encrypt.cpp',
'version.cpp',
dependencies: [fmtdep, libabcrypt_capi],
)
executable('decrypt',
'decrypt.cpp',
'version.cpp',
dependencies: [fmtdep, libabcrypt_capi],
)
executable('info',
'info.cpp',
'version.cpp',
dependencies: [fmtdep, libabcrypt_capi],
)
example_names = ['encrypt', 'decrypt', 'info']
foreach example_name : example_names
executable(example_name,
example_name + '.cpp',
'version.cpp',
dependencies: [fmtdep, libabcrypt_capi],
include_directories: incdir,
)
endforeach
16 changes: 9 additions & 7 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,29 @@ default: build
@clippy-fix:
cargo clippy --workspace --fix --allow-dirty --allow-staged --lib --tests --examples -- -D warnings

# Build examples for the C API
build-capi-examples:
# Configure the Meson project
setup-meson:
#!/usr/bin/env bash
cargo build -p abcrypt-capi
cd crate/capi/examples
meson setup builddir
# Build examples for the C API
build-capi-examples: setup-meson
#!/usr/bin/env bash
cd crate/capi/examples
meson compile -C builddir
# Run clang-format
clang-format:
clang-format: setup-meson
#!/usr/bin/env bash
cd crate/capi/examples
meson setup builddir
ninja -C builddir clang-format
git restore abcrypt.h
# Run clang-tidy
clang-tidy:
clang-tidy: setup-meson
#!/usr/bin/env bash
cd crate/capi/examples
meson setup builddir
ninja -C builddir clang-tidy
# Run the linter for GitHub Actions workflow files
Expand Down

0 comments on commit 08a3d41

Please sign in to comment.