commit
ad62a914e6
127 files changed
+38545
No files matched your search
@@ -0,0 +1,30 @@
|
||||
#include "foundation/bit_reader.h"
|
||||
|
||||
namespace joc::bits {
|
||||
|
||||
bool variable_bits(BitReader& reader, unsigned width, unsigned max_groups, std::uint32_t* out_value) {
|
||||
std::uint32_t value = 0;
|
||||
for (unsigned group = 0; group < max_groups; ++group) {
|
||||
value += reader.read(width);
|
||||
if (reader.failed()) {
|
||||
return false;
|
||||
}
|
||||
const std::uint32_t more = reader.read(1);
|
||||
if (reader.failed()) {
|
||||
return false;
|
||||
}
|
||||
if (more == 0u) {
|
||||
if (out_value != nullptr) {
|
||||
*out_value = value;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
value = (value + 1u) << width;
|
||||
}
|
||||
// Same failure mode as the reference implementation: an extension chain
|
||||
// that never terminates is a syntax error, not a truncation.
|
||||
reader.fail(JOC_ERR_EMDF_SYNTAX, "variable_bits extension groups exceeded");
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace joc::bits
|
||||
@@ -0,0 +1,125 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace joc::bits {
|
||||
|
||||
class BitReader {
|
||||
public:
|
||||
BitReader() = default;
|
||||
BitReader(const std::uint8_t* data, std::size_t size) { reset(data, size); }
|
||||
|
||||
void reset(const std::uint8_t* data, std::size_t size, std::size_t start_bit = 0) {
|
||||
data_ = data;
|
||||
size_bits_ = size * 8u;
|
||||
pos_ = start_bit;
|
||||
limit_ = size_bits_;
|
||||
error_ = JOC_OK;
|
||||
message_ = "";
|
||||
}
|
||||
|
||||
void set_limit_bits(std::size_t limit_bits) {
|
||||
limit_ = limit_bits < size_bits_ ? limit_bits : size_bits_;
|
||||
}
|
||||
|
||||
std::size_t position() const { return pos_; }
|
||||
std::size_t limit() const { return limit_; }
|
||||
std::size_t remaining_bits() const { return pos_ <= limit_ ? limit_ - pos_ : 0; }
|
||||
const std::uint8_t* data() const { return data_; }
|
||||
|
||||
bool failed() const { return error_ != JOC_OK; }
|
||||
joc_error error() const { return error_; }
|
||||
const char* error_message() const { return message_; }
|
||||
|
||||
std::uint32_t read(unsigned count) {
|
||||
if (count == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (!can_read(count)) {
|
||||
fail_truncated(count);
|
||||
return 0;
|
||||
}
|
||||
std::uint32_t value = 0;
|
||||
if ((pos_ & 7u) == 0u && count >= 8u) {
|
||||
while (count >= 8u) {
|
||||
value = (value << 8) | data_[pos_ >> 3];
|
||||
pos_ += 8u;
|
||||
count -= 8u;
|
||||
}
|
||||
}
|
||||
while (count-- > 0u) {
|
||||
const std::uint32_t bit = (data_[pos_ >> 3] >> (7u - (pos_ & 7u))) & 1u;
|
||||
value = (value << 1) | bit;
|
||||
++pos_;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
std::uint64_t read64(unsigned count) {
|
||||
if (count <= 32u) {
|
||||
return static_cast<std::uint64_t>(read(count));
|
||||
}
|
||||
const std::uint64_t high = static_cast<std::uint64_t>(read(count - 32u));
|
||||
const std::uint64_t low = static_cast<std::uint64_t>(read(32u));
|
||||
return (high << 32) | low;
|
||||
}
|
||||
|
||||
bool skip(std::size_t count) {
|
||||
if (!can_read(count)) {
|
||||
fail_truncated(count);
|
||||
return false;
|
||||
}
|
||||
pos_ += count;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_bytes(std::uint8_t* out, std::size_t count) {
|
||||
if (count == 0) {
|
||||
return true;
|
||||
}
|
||||
if (!can_read(count * 8u)) {
|
||||
fail_truncated(count * 8u);
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
out[i] = static_cast<std::uint8_t>(read(8u));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool can_read(std::size_t count) const {
|
||||
return !failed() && count <= limit_ && pos_ <= limit_ - count;
|
||||
}
|
||||
|
||||
// semantic check fails, so the reader never continues past it).
|
||||
void fail(joc_error code, const char* message) {
|
||||
if (!failed()) {
|
||||
error_ = code;
|
||||
message_ = message;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void fail_truncated(std::size_t count) {
|
||||
fail(JOC_ERR_BITSTREAM_TRUNCATED, "bit read past end of buffer");
|
||||
last_request_ = count;
|
||||
}
|
||||
|
||||
const std::uint8_t* data_ = nullptr;
|
||||
std::size_t size_bits_ = 0;
|
||||
std::size_t pos_ = 0;
|
||||
std::size_t limit_ = 0;
|
||||
std::size_t last_request_ = 0;
|
||||
joc_error error_ = JOC_OK;
|
||||
const char* message_ = "";
|
||||
};
|
||||
|
||||
// followed by a continuation bit. Mirrors src/emdf.py:variable_bits().
|
||||
bool variable_bits(BitReader& reader, unsigned width, unsigned max_groups,
|
||||
std::uint32_t* out_value);
|
||||
|
||||
} // namespace joc::bits
|
||||
@@ -0,0 +1,206 @@
|
||||
#include "foundation/fft.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "simd/simd.h"
|
||||
|
||||
namespace joc::dsp {
|
||||
|
||||
// The dispatched kernels read and write the spectrum as interleaved doubles, and
|
||||
// an array of std::complex<double> is exactly that: two doubles per element, no
|
||||
// padding, no vtable.
|
||||
static_assert(sizeof(Complex) == 2u * sizeof(double), "complex layout");
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr double kPi = 3.14159265358979323846;
|
||||
|
||||
template <typename Container>
|
||||
void fft_in_place(Container* data, bool inverse) {
|
||||
const std::size_t count = data->size();
|
||||
if (count < 2u) {
|
||||
return;
|
||||
}
|
||||
for (std::size_t index = 1u, reversed = 0u; index < count; ++index) {
|
||||
std::size_t bit = count >> 1u;
|
||||
for (; (reversed & bit) != 0u; bit >>= 1u) {
|
||||
reversed ^= bit;
|
||||
}
|
||||
reversed ^= bit;
|
||||
if (index < reversed) {
|
||||
std::swap((*data)[index], (*data)[reversed]);
|
||||
}
|
||||
}
|
||||
for (std::size_t length = 2u; length <= count; length <<= 1u) {
|
||||
const double angle = (inverse ? 2.0 : -2.0) * kPi / static_cast<double>(length);
|
||||
const Complex step(std::cos(angle), std::sin(angle));
|
||||
for (std::size_t start = 0u; start < count; start += length) {
|
||||
Complex factor(1.0, 0.0);
|
||||
for (std::size_t offset = 0u; offset < length / 2u; ++offset) {
|
||||
const Complex even = (*data)[start + offset];
|
||||
const Complex odd = (*data)[start + offset + length / 2u] * factor;
|
||||
(*data)[start + offset] = even + odd;
|
||||
(*data)[start + offset + length / 2u] = even - odd;
|
||||
factor *= step;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inverse) {
|
||||
for (Complex& value : *data) {
|
||||
value /= static_cast<double>(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool is_power_of_two(std::size_t value) { return value != 0u && (value & (value - 1u)) == 0u; }
|
||||
|
||||
} // namespace
|
||||
|
||||
FftPlan::FftPlan(std::size_t size, bool inverse) : size_(size), inverse_(inverse) {
|
||||
reverse_.resize(size);
|
||||
for (std::size_t index = 1u, reversed = 0u; index < size; ++index) {
|
||||
std::size_t bit = size >> 1u;
|
||||
for (; (reversed & bit) != 0u; bit >>= 1u) {
|
||||
reversed ^= bit;
|
||||
}
|
||||
reversed ^= bit;
|
||||
reverse_[index] = reversed;
|
||||
}
|
||||
for (std::size_t length = 2u; length <= size; length <<= 1u) {
|
||||
const double angle = (inverse ? 2.0 : -2.0) * kPi / static_cast<double>(length);
|
||||
const Complex step(std::cos(angle), std::sin(angle));
|
||||
stage_begin_.push_back(twiddle_.size());
|
||||
Complex factor(1.0, 0.0);
|
||||
for (std::size_t offset = 0u; offset < length / 2u; ++offset) {
|
||||
twiddle_.push_back(factor);
|
||||
factor *= step;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Exactly the operations fft_in_place performs, in the same order, with the
|
||||
// twiddles read from the precomputed recurrence instead of being re-derived.
|
||||
template <typename Container>
|
||||
void FftPlan::apply(Container* data) const {
|
||||
const std::size_t count = data->size();
|
||||
if (count < 2u) {
|
||||
return;
|
||||
}
|
||||
const std::size_t* reverse = reverse_.data();
|
||||
for (std::size_t index = 1u; index < count; ++index) {
|
||||
const std::size_t reversed = reverse[index];
|
||||
if (index < reversed) {
|
||||
std::swap((*data)[index], (*data)[reversed]);
|
||||
}
|
||||
}
|
||||
// The cascade is dispatched for every power-of-two size the kernels can pack
|
||||
// whole groups into a vector (JOC_SIMD pins one tier for verification). A
|
||||
// kernel only ever puts independent butterflies in the same vector, so every
|
||||
// output keeps the operation sequence and the roundings written below; small
|
||||
// transforms -- and the caller's own table -- keep the portable loop.
|
||||
if (count >= simd::kMinVectorFftSize && (count & (count - 1u)) == 0u) {
|
||||
simd::fft_butterflies(reinterpret_cast<double*>(data->data()), count,
|
||||
reinterpret_cast<const double*>(twiddle_.data()),
|
||||
stage_begin_.data());
|
||||
} else {
|
||||
std::size_t stage = 0u;
|
||||
for (std::size_t length = 2u; length <= count; length <<= 1u, ++stage) {
|
||||
const Complex* table = twiddle_.data() + stage_begin_[stage];
|
||||
for (std::size_t start = 0u; start < count; start += length) {
|
||||
for (std::size_t offset = 0u; offset < length / 2u; ++offset) {
|
||||
const Complex even = (*data)[start + offset];
|
||||
const Complex odd = (*data)[start + offset + length / 2u] * table[offset];
|
||||
(*data)[start + offset] = even + odd;
|
||||
(*data)[start + offset + length / 2u] = even - odd;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inverse_) {
|
||||
for (Complex& value : *data) {
|
||||
value /= static_cast<double>(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fft_radix2(std::vector<Complex>* data, const FftPlan& plan) { plan.apply(data); }
|
||||
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, const FftPlan& plan) { plan.apply(data); }
|
||||
|
||||
void fft_radix2(std::vector<Complex>* data, bool inverse) { fft_in_place(data, inverse); }
|
||||
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, bool inverse) {
|
||||
fft_in_place(data, inverse);
|
||||
}
|
||||
|
||||
void fft_any(const std::vector<Complex>& input, bool inverse, std::vector<Complex>* output) {
|
||||
const std::size_t count = input.size();
|
||||
if (is_power_of_two(count)) {
|
||||
*output = input;
|
||||
fft_radix2(output, inverse);
|
||||
return;
|
||||
}
|
||||
std::size_t size = 1u;
|
||||
while (size < 2u * count + 1u) {
|
||||
size <<= 1u;
|
||||
}
|
||||
const double sign = inverse ? 1.0 : -1.0;
|
||||
std::vector<Complex> left(size, Complex(0.0, 0.0));
|
||||
std::vector<Complex> right(size, Complex(0.0, 0.0));
|
||||
for (std::size_t index = 0u; index < count; ++index) {
|
||||
const std::size_t wrapped = (index * index) % (2u * count);
|
||||
const double angle = kPi * static_cast<double>(wrapped) / static_cast<double>(count);
|
||||
const Complex chirp(std::cos(angle), sign * std::sin(angle));
|
||||
left[index] = input[index] * chirp;
|
||||
right[index] = std::conj(chirp);
|
||||
if (index != 0u) {
|
||||
right[size - index] = std::conj(chirp);
|
||||
}
|
||||
}
|
||||
fft_radix2(&left, false);
|
||||
fft_radix2(&right, false);
|
||||
for (std::size_t index = 0u; index < size; ++index) {
|
||||
left[index] *= right[index];
|
||||
}
|
||||
fft_radix2(&left, true);
|
||||
output->resize(count);
|
||||
for (std::size_t index = 0u; index < count; ++index) {
|
||||
const std::size_t wrapped = (index * index) % (2u * count);
|
||||
const double angle = kPi * static_cast<double>(wrapped) / static_cast<double>(count);
|
||||
const Complex chirp(std::cos(angle), sign * std::sin(angle));
|
||||
(*output)[index] = left[index] * chirp;
|
||||
if (inverse) {
|
||||
(*output)[index] /= static_cast<double>(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t next_fast_len(std::size_t value) {
|
||||
if (value <= 6u) {
|
||||
return value;
|
||||
}
|
||||
std::size_t best = value;
|
||||
for (std::size_t power2 = 1u; power2 < value * 2u; power2 *= 2u) {
|
||||
for (std::size_t power3 = power2; power3 < value * 2u; power3 *= 3u) {
|
||||
std::size_t power5 = power3;
|
||||
while (power5 < value) {
|
||||
power5 *= 5u;
|
||||
}
|
||||
best = std::min(best, power5);
|
||||
if (power3 >= value) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
std::size_t next_power_of_two(std::size_t value) {
|
||||
std::size_t result = 1u;
|
||||
while (result < value) {
|
||||
result <<= 1u;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace joc::dsp
|
||||
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <complex>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
// Complex transforms shared by the HRTF and Rosella DSP cores. The convention is
|
||||
// NumPy's: the forward transform is unnormalised and the inverse scales by 1/N,
|
||||
// so a ported pipeline keeps the reference's arithmetic bit for bit.
|
||||
namespace joc::dsp {
|
||||
|
||||
using Complex = std::complex<double>;
|
||||
|
||||
inline constexpr std::size_t kQmfFftSize = 128;
|
||||
|
||||
// Precomputed radix-2 plan for one size and direction.
|
||||
//
|
||||
// The transform derives each butterfly's twiddle by multiplying the previous one
|
||||
// by the stage step, so the twiddle at offset k is `step` multiplied k times in
|
||||
// that order, independently of the group. Materialising that exact recurrence --
|
||||
// and the bit-reversal permutation -- removes one complex multiply and a
|
||||
// (length/2)-deep serial dependency from every stage's inner loop. The table
|
||||
// entries are the recurrence's own values, so the transform is bit-identical.
|
||||
//
|
||||
// The 128-point cascade is executed by the runtime-dispatched SIMD kernel
|
||||
// (src/simd/simd.h): it computes independent butterflies in parallel lanes,
|
||||
// which leaves both the table and every output's summation order untouched.
|
||||
class FftPlan {
|
||||
public:
|
||||
FftPlan(std::size_t size, bool inverse);
|
||||
|
||||
std::size_t size() const { return size_; }
|
||||
bool inverse() const { return inverse_; }
|
||||
|
||||
private:
|
||||
template <typename Container>
|
||||
void apply(Container* data) const;
|
||||
|
||||
friend void fft_radix2(std::vector<Complex>* data, const FftPlan& plan);
|
||||
friend void fft_radix2(std::array<Complex, kQmfFftSize>* data, const FftPlan& plan);
|
||||
|
||||
std::size_t size_ = 0;
|
||||
bool inverse_ = false;
|
||||
std::vector<std::size_t> reverse_; // bit-reversal permutation, [size]
|
||||
std::vector<std::size_t> stage_begin_; // twiddle offset of each stage
|
||||
std::vector<Complex> twiddle_; // per stage, length/2 entries, concatenated
|
||||
};
|
||||
|
||||
// In-place radix-2 transform; the size must be a power of two.
|
||||
void fft_radix2(std::vector<Complex>* data, bool inverse);
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, bool inverse);
|
||||
|
||||
// Plan-driven forms: the plan carries the size and the direction, so a caller that
|
||||
// transforms the same length repeatedly builds it once.
|
||||
void fft_radix2(std::vector<Complex>* data, const FftPlan& plan);
|
||||
void fft_radix2(std::array<Complex, kQmfFftSize>* data, const FftPlan& plan);
|
||||
|
||||
// Exact-length transform: radix-2 when the size allows it, Bluestein otherwise.
|
||||
// scipy/numpy use a mixed-radix transform, which is the same transform.
|
||||
void fft_any(const std::vector<Complex>& input, bool inverse, std::vector<Complex>* output);
|
||||
|
||||
// scipy's next_fast_len: the smallest 5-smooth number that is not smaller.
|
||||
std::size_t next_fast_len(std::size_t value);
|
||||
|
||||
std::size_t next_power_of_two(std::size_t value);
|
||||
|
||||
} // namespace joc::dsp
|
||||
@@ -0,0 +1,164 @@
|
||||
#include "foundation/fs_utf8.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace joc::fs_utf8 {
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
fs::path to_path(const std::string& utf8) {
|
||||
return fs::path(std::u8string(reinterpret_cast<const char8_t*>(utf8.data()), utf8.size()));
|
||||
}
|
||||
|
||||
std::string from_path(const fs::path& path) {
|
||||
const std::u8string text = path.u8string();
|
||||
return std::string(reinterpret_cast<const char*>(text.data()), text.size());
|
||||
}
|
||||
|
||||
std::FILE* fopen(const std::string& utf8_path, const char* mode) {
|
||||
#if defined(_WIN32)
|
||||
const std::wstring wide_mode(mode, mode + std::strlen(mode));
|
||||
return ::_wfopen(to_path(utf8_path).c_str(), wide_mode.c_str());
|
||||
#else
|
||||
return std::fopen(utf8_path.c_str(), mode);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::FILE* fopen_spool(const std::string& utf8_path) {
|
||||
#if defined(_WIN32)
|
||||
// Delete-on-close handed to the CRT: if the process is killed the file goes with
|
||||
// it, which is what stops an aborted run from leaving hundreds of gigabytes.
|
||||
HANDLE handle = ::CreateFileW(
|
||||
to_path(utf8_path).c_str(), GENERIC_READ | GENERIC_WRITE | DELETE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS,
|
||||
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
return nullptr;
|
||||
}
|
||||
const int descriptor = ::_open_osfhandle(reinterpret_cast<std::intptr_t>(handle), 0);
|
||||
if (descriptor == -1) {
|
||||
::CloseHandle(handle);
|
||||
return nullptr;
|
||||
}
|
||||
return ::_fdopen(descriptor, "wb+");
|
||||
#else
|
||||
return std::fopen(utf8_path.c_str(), "wb+");
|
||||
#endif
|
||||
}
|
||||
|
||||
int remove(const std::string& utf8_path) {
|
||||
#if defined(_WIN32)
|
||||
return ::_wremove(to_path(utf8_path).c_str());
|
||||
#else
|
||||
return std::remove(utf8_path.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
bool exists(const std::string& utf8_path) {
|
||||
std::error_code error;
|
||||
return fs::exists(to_path(utf8_path), error);
|
||||
}
|
||||
|
||||
bool is_directory(const std::string& utf8_path) {
|
||||
std::error_code error;
|
||||
return fs::is_directory(to_path(utf8_path), error);
|
||||
}
|
||||
|
||||
std::uintmax_t file_size(const std::string& utf8_path, std::error_code& error) {
|
||||
return fs::file_size(to_path(utf8_path), error);
|
||||
}
|
||||
|
||||
std::string temp_directory() {
|
||||
std::error_code error;
|
||||
const fs::path directory = fs::temp_directory_path(error);
|
||||
return error ? std::string(".") : from_path(directory);
|
||||
}
|
||||
|
||||
std::string executable_path() {
|
||||
#if defined(_WIN32)
|
||||
std::vector<wchar_t> buffer(MAX_PATH);
|
||||
while (true) {
|
||||
const DWORD written =
|
||||
::GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
|
||||
if (written == 0) {
|
||||
return std::string();
|
||||
}
|
||||
if (written < buffer.size()) {
|
||||
return from_path(fs::path(std::wstring(buffer.data(), written)));
|
||||
}
|
||||
buffer.resize(buffer.size() * 2u);
|
||||
}
|
||||
#elif defined(__linux__)
|
||||
std::vector<char> buffer(4096u, '\0');
|
||||
const ssize_t written = ::readlink("/proc/self/exe", buffer.data(), buffer.size() - 1u);
|
||||
return written > 0 ? std::string(buffer.data(), static_cast<std::size_t>(written))
|
||||
: std::string();
|
||||
#else
|
||||
return std::string();
|
||||
#endif
|
||||
}
|
||||
|
||||
std::ifstream open_input(const std::string& utf8_path) {
|
||||
return std::ifstream(to_path(utf8_path), std::ios::binary);
|
||||
}
|
||||
|
||||
std::ofstream open_output(const std::string& utf8_path) {
|
||||
return std::ofstream(to_path(utf8_path), std::ios::binary);
|
||||
}
|
||||
|
||||
std::vector<std::string> command_line_arguments(int argc, char** argv) {
|
||||
#if defined(_WIN32)
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
int count = 0;
|
||||
LPWSTR* wide = ::CommandLineToArgvW(::GetCommandLineW(), &count);
|
||||
std::vector<std::string> arguments;
|
||||
if (wide == nullptr) {
|
||||
return arguments;
|
||||
}
|
||||
arguments.reserve(static_cast<std::size_t>(count));
|
||||
for (int index = 0; index < count; ++index) {
|
||||
const std::wstring_view text(wide[index]);
|
||||
const int size = ::WideCharToMultiByte(CP_UTF8, 0, text.data(),
|
||||
static_cast<int>(text.size()), nullptr, 0, nullptr,
|
||||
nullptr);
|
||||
std::string utf8(static_cast<std::size_t>(size), '\0');
|
||||
if (size > 0) {
|
||||
::WideCharToMultiByte(CP_UTF8, 0, text.data(), static_cast<int>(text.size()),
|
||||
utf8.data(), size, nullptr, nullptr);
|
||||
}
|
||||
arguments.push_back(std::move(utf8));
|
||||
}
|
||||
::LocalFree(wide);
|
||||
return arguments;
|
||||
#else
|
||||
std::vector<std::string> arguments;
|
||||
arguments.reserve(static_cast<std::size_t>(argc));
|
||||
for (int index = 0; index < argc; ++index) {
|
||||
arguments.emplace_back(argv[index]);
|
||||
}
|
||||
return arguments;
|
||||
#endif
|
||||
}
|
||||
|
||||
void configure_console() {
|
||||
#if defined(_WIN32)
|
||||
::SetConsoleOutputCP(CP_UTF8);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace joc::fs_utf8
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
||||
// Paths inside this library are always UTF-8, on every platform. std::filesystem
|
||||
// stores UTF-16 on Windows and bytes elsewhere, and the narrow CRT uses the ANSI
|
||||
// code page on Windows, so every path crosses into the OS through this shim: that
|
||||
// is what makes non-ASCII names (Japanese, Chinese, ...) work.
|
||||
namespace joc::fs_utf8 {
|
||||
|
||||
std::filesystem::path to_path(const std::string& utf8);
|
||||
std::string from_path(const std::filesystem::path& path);
|
||||
|
||||
// File handles and queries take a UTF-8 path: _wfopen on Windows, plain calls
|
||||
// elsewhere. Nothing else in the library may call the narrow CRT with a path.
|
||||
std::FILE* fopen(const std::string& utf8_path, const char* mode);
|
||||
// Temporary spool handle: on Windows the file is opened delete-on-close, so killing
|
||||
// the process removes it instead of leaving a multi-gigabyte leftover behind.
|
||||
std::FILE* fopen_spool(const std::string& utf8_path);
|
||||
int remove(const std::string& utf8_path);
|
||||
bool exists(const std::string& utf8_path);
|
||||
bool is_directory(const std::string& utf8_path);
|
||||
std::uintmax_t file_size(const std::string& utf8_path, std::error_code& error);
|
||||
std::string temp_directory();
|
||||
|
||||
// The running executable's own path, UTF-8, or empty when the platform cannot
|
||||
// report it. Defaults are anchored here so they never depend on the CWD.
|
||||
std::string executable_path();
|
||||
|
||||
// Streams: std::ifstream/ofstream accept a std::filesystem::path, which is the
|
||||
// portable way to open a UTF-8 path.
|
||||
std::ifstream open_input(const std::string& utf8_path);
|
||||
std::ofstream open_output(const std::string& utf8_path);
|
||||
|
||||
// Command line arguments as UTF-8. Windows hands the process UTF-16 and the
|
||||
// narrow CRT would convert it through the ANSI code page, so the wide command
|
||||
// line is re-parsed there; on POSIX argv is already bytes in the user's locale.
|
||||
std::vector<std::string> command_line_arguments(int argc, char** argv);
|
||||
void configure_console();
|
||||
|
||||
} // namespace joc::fs_utf8
|
||||
@@ -0,0 +1,25 @@
|
||||
// Port of the reference's adm_atmos.q_to_adm_xyz: OAMD Q15 coordinates to the ADM
|
||||
// cartesian triple. It lives in foundation because both the ADM writer and the
|
||||
// object position timeline need it, and the timeline must not depend on output.
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "foundation/py_num.h"
|
||||
|
||||
namespace joc::geometry {
|
||||
|
||||
inline void q_to_adm_xyz(int q1, int q2, int q3, double* x, double* y, double* z) {
|
||||
const double posX = std::min(1.0, static_cast<double>(pynum::py_round(
|
||||
static_cast<double>(q1) * 62.0 / 32767.0)) / 62.0);
|
||||
const double posY = std::min(1.0, static_cast<double>(pynum::py_round(
|
||||
static_cast<double>(q2) * 62.0 / 32767.0)) / 62.0);
|
||||
double posZ = static_cast<double>(pynum::py_round(
|
||||
static_cast<double>(q3) * 15.0 / 32767.0)) / 15.0;
|
||||
posZ = std::max(-1.0, std::min(1.0, posZ));
|
||||
*x = posX * 2.0 - 1.0;
|
||||
*y = 1.0 - posY * 2.0;
|
||||
*z = posZ;
|
||||
}
|
||||
|
||||
} // namespace joc::geometry
|
||||
@@ -0,0 +1,227 @@
|
||||
#include "foundation/mini_json.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace joc::json {
|
||||
|
||||
namespace {
|
||||
|
||||
void skip_space(const std::string& text, std::size_t* index) {
|
||||
while (*index < text.size() &&
|
||||
(text[*index] == ' ' || text[*index] == '\t' || text[*index] == '\n' ||
|
||||
text[*index] == '\r')) {
|
||||
++(*index);
|
||||
}
|
||||
}
|
||||
|
||||
bool read_string(const std::string& text, std::size_t* index, std::string* out) {
|
||||
if (*index >= text.size() || text[*index] != '"') {
|
||||
return false;
|
||||
}
|
||||
++(*index);
|
||||
out->clear();
|
||||
while (*index < text.size()) {
|
||||
const char c = text[*index];
|
||||
if (c == '\\') {
|
||||
if (*index + 1 >= text.size()) {
|
||||
return false;
|
||||
}
|
||||
const char escape = text[*index + 1];
|
||||
*index += 2;
|
||||
switch (escape) {
|
||||
case '"': out->push_back('"'); break;
|
||||
case '\\': out->push_back('\\'); break;
|
||||
case '/': out->push_back('/'); break;
|
||||
case 'b': out->push_back('\b'); break;
|
||||
case 'f': out->push_back('\f'); break;
|
||||
case 'n': out->push_back('\n'); break;
|
||||
case 'r': out->push_back('\r'); break;
|
||||
case 't': out->push_back('\t'); break;
|
||||
case 'u': {
|
||||
if (*index + 4 > text.size()) {
|
||||
return false;
|
||||
}
|
||||
unsigned code = 0;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const char digit = text[*index + static_cast<std::size_t>(i)];
|
||||
code <<= 4;
|
||||
if (digit >= '0' && digit <= '9') { code |= static_cast<unsigned>(digit - '0'); }
|
||||
else if (digit >= 'a' && digit <= 'f') { code |= static_cast<unsigned>(digit - 'a' + 10); }
|
||||
else if (digit >= 'A' && digit <= 'F') { code |= static_cast<unsigned>(digit - 'A' + 10); }
|
||||
else { return false; }
|
||||
}
|
||||
*index += 4;
|
||||
if (code < 0x80u) {
|
||||
out->push_back(static_cast<char>(code));
|
||||
} else if (code < 0x800u) {
|
||||
out->push_back(static_cast<char>(0xC0u | (code >> 6)));
|
||||
out->push_back(static_cast<char>(0x80u | (code & 0x3Fu)));
|
||||
} else {
|
||||
out->push_back(static_cast<char>(0xE0u | (code >> 12)));
|
||||
out->push_back(static_cast<char>(0x80u | ((code >> 6) & 0x3Fu)));
|
||||
out->push_back(static_cast<char>(0x80u | (code & 0x3Fu)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == '"') {
|
||||
++(*index);
|
||||
return true;
|
||||
}
|
||||
out->push_back(c);
|
||||
++(*index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool read_compound(const std::string& text, std::size_t* index, std::string* out) {
|
||||
const char open = text[*index];
|
||||
const char close = open == '{' ? '}' : ']';
|
||||
int depth = 0;
|
||||
const std::size_t start = *index;
|
||||
while (*index < text.size()) {
|
||||
const char c = text[*index];
|
||||
if (c == '"') {
|
||||
std::string ignored;
|
||||
if (!read_string(text, index, &ignored)) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == open) {
|
||||
++depth;
|
||||
} else if (c == close) {
|
||||
--depth;
|
||||
if (depth == 0) {
|
||||
++(*index);
|
||||
*out = text.substr(start, *index - start);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
++(*index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error) {
|
||||
out->clear();
|
||||
std::size_t index = 0;
|
||||
skip_space(text, &index);
|
||||
if (index >= text.size() || text[index] != '{') {
|
||||
if (error != nullptr) { *error = "metadata is not a JSON object"; }
|
||||
return false;
|
||||
}
|
||||
++index;
|
||||
for (;;) {
|
||||
skip_space(text, &index);
|
||||
if (index < text.size() && text[index] == '}') {
|
||||
++index;
|
||||
break;
|
||||
}
|
||||
if (index >= text.size() || text[index] == ',') {
|
||||
if (index >= text.size()) {
|
||||
if (error != nullptr) { *error = "unterminated JSON object"; }
|
||||
return false;
|
||||
}
|
||||
++index;
|
||||
continue;
|
||||
}
|
||||
Member member;
|
||||
if (!read_string(text, &index, &member.key)) {
|
||||
if (error != nullptr) { *error = "expected a JSON key"; }
|
||||
return false;
|
||||
}
|
||||
skip_space(text, &index);
|
||||
if (index >= text.size() || text[index] != ':') {
|
||||
if (error != nullptr) { *error = "expected ':' after JSON key " + member.key; }
|
||||
return false;
|
||||
}
|
||||
++index;
|
||||
skip_space(text, &index);
|
||||
if (index >= text.size()) {
|
||||
if (error != nullptr) { *error = "missing JSON value for " + member.key; }
|
||||
return false;
|
||||
}
|
||||
if (text[index] == '"') {
|
||||
member.is_string = true;
|
||||
if (!read_string(text, &index, &member.raw)) {
|
||||
if (error != nullptr) { *error = "bad JSON string for " + member.key; }
|
||||
return false;
|
||||
}
|
||||
} else if (text[index] == '{' || text[index] == '[') {
|
||||
if (!read_compound(text, &index, &member.raw)) {
|
||||
if (error != nullptr) { *error = "bad JSON container for " + member.key; }
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
const std::size_t start = index;
|
||||
while (index < text.size() && text[index] != ',' && text[index] != '}') {
|
||||
++index;
|
||||
}
|
||||
member.raw = text.substr(start, index - start);
|
||||
while (!member.raw.empty() &&
|
||||
(member.raw.back() == ' ' || member.raw.back() == '\n' ||
|
||||
member.raw.back() == '\r' || member.raw.back() == '\t')) {
|
||||
member.raw.pop_back();
|
||||
}
|
||||
}
|
||||
for (const Member& existing : *out) {
|
||||
if (existing.key == member.key) {
|
||||
if (error != nullptr) { *error = "duplicate JSON key " + member.key; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out->push_back(std::move(member));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const Member* find(const std::vector<Member>& members, const std::string& key) {
|
||||
for (const Member& member : members) {
|
||||
if (member.key == key) {
|
||||
return &member;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool as_string(const Member& member, std::string* out) {
|
||||
if (!member.is_string || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
*out = member.raw;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool as_number(const Member& member, double* out) {
|
||||
if (member.is_string || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
char* end = nullptr;
|
||||
const double value = std::strtod(member.raw.c_str(), &end);
|
||||
if (end == member.raw.c_str() || !std::isfinite(value)) {
|
||||
return false;
|
||||
}
|
||||
*out = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool as_integer(const Member& member, long long* out) {
|
||||
double value = 0.0;
|
||||
if (!as_number(member, &value) || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (value != std::floor(value)) {
|
||||
return false;
|
||||
}
|
||||
*out = static_cast<long long>(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace joc::json
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace joc::json {
|
||||
|
||||
struct Member {
|
||||
std::string key;
|
||||
std::string raw;
|
||||
bool is_string = false;
|
||||
};
|
||||
|
||||
// Parses a top-level JSON object. Rejects non-objects and duplicate keys.
|
||||
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error);
|
||||
|
||||
const Member* find(const std::vector<Member>& members, const std::string& key);
|
||||
|
||||
bool as_string(const Member& member, std::string* out);
|
||||
bool as_number(const Member& member, double* out);
|
||||
bool as_integer(const Member& member, long long* out);
|
||||
|
||||
} // namespace joc::json
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cfenv>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
namespace joc::pynum {
|
||||
|
||||
inline long long py_round(double value) {
|
||||
return static_cast<long long>(std::nearbyint(value));
|
||||
}
|
||||
|
||||
inline std::string format_fixed(double value, int decimals) {
|
||||
char buffer[64];
|
||||
std::snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
inline long long trunc_to_ll(double value) {
|
||||
return static_cast<long long>(value);
|
||||
}
|
||||
|
||||
} // namespace joc::pynum
|
||||
@@ -0,0 +1,165 @@
|
||||
#include "foundation/sha256.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace joc::crypto {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::uint32_t kK[64] = {
|
||||
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u,
|
||||
0xab1c5ed5u, 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu,
|
||||
0x9bdc06a7u, 0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu,
|
||||
0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
|
||||
0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu,
|
||||
0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, 0xa2bfe8a1u, 0xa81a664bu,
|
||||
0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u,
|
||||
0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
|
||||
0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u,
|
||||
0xc67178f2u};
|
||||
|
||||
inline std::uint32_t rotr(std::uint32_t value, unsigned count) {
|
||||
return (value >> count) | (value << (32u - count));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Sha256::reset() {
|
||||
state_[0] = 0x6a09e667u;
|
||||
state_[1] = 0xbb67ae85u;
|
||||
state_[2] = 0x3c6ef372u;
|
||||
state_[3] = 0xa54ff53au;
|
||||
state_[4] = 0x510e527fu;
|
||||
state_[5] = 0x9b05688cu;
|
||||
state_[6] = 0x1f83d9abu;
|
||||
state_[7] = 0x5be0cd19u;
|
||||
bit_count_ = 0;
|
||||
buffer_used_ = 0;
|
||||
std::memset(buffer_, 0, sizeof(buffer_));
|
||||
}
|
||||
|
||||
void Sha256::transform(const std::uint8_t block[64]) {
|
||||
std::uint32_t w[64];
|
||||
for (unsigned i = 0; i < 16; ++i) {
|
||||
w[i] = (static_cast<std::uint32_t>(block[i * 4]) << 24) |
|
||||
(static_cast<std::uint32_t>(block[i * 4 + 1]) << 16) |
|
||||
(static_cast<std::uint32_t>(block[i * 4 + 2]) << 8) |
|
||||
static_cast<std::uint32_t>(block[i * 4 + 3]);
|
||||
}
|
||||
for (unsigned i = 16; i < 64; ++i) {
|
||||
const std::uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||
const std::uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||
}
|
||||
std::uint32_t a = state_[0];
|
||||
std::uint32_t b = state_[1];
|
||||
std::uint32_t c = state_[2];
|
||||
std::uint32_t d = state_[3];
|
||||
std::uint32_t e = state_[4];
|
||||
std::uint32_t f = state_[5];
|
||||
std::uint32_t g = state_[6];
|
||||
std::uint32_t h = state_[7];
|
||||
for (unsigned i = 0; i < 64; ++i) {
|
||||
const std::uint32_t s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
||||
const std::uint32_t ch = (e & f) ^ (~e & g);
|
||||
const std::uint32_t temp1 = h + s1 + ch + kK[i] + w[i];
|
||||
const std::uint32_t s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
||||
const std::uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
||||
const std::uint32_t temp2 = s0 + maj;
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + temp1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = temp1 + temp2;
|
||||
}
|
||||
state_[0] += a;
|
||||
state_[1] += b;
|
||||
state_[2] += c;
|
||||
state_[3] += d;
|
||||
state_[4] += e;
|
||||
state_[5] += f;
|
||||
state_[6] += g;
|
||||
state_[7] += h;
|
||||
}
|
||||
|
||||
void Sha256::update(const void* data, std::size_t size) {
|
||||
const std::uint8_t* bytes = static_cast<const std::uint8_t*>(data);
|
||||
bit_count_ += static_cast<std::uint64_t>(size) * 8u;
|
||||
while (size > 0) {
|
||||
const std::size_t space = 64u - buffer_used_;
|
||||
const std::size_t take = size < space ? size : space;
|
||||
std::memcpy(buffer_ + buffer_used_, bytes, take);
|
||||
buffer_used_ += take;
|
||||
bytes += take;
|
||||
size -= take;
|
||||
if (buffer_used_ == 64u) {
|
||||
transform(buffer_);
|
||||
buffer_used_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Sha256::finish(std::uint8_t out[32]) {
|
||||
const std::uint64_t total_bits = bit_count_;
|
||||
const std::uint8_t pad = 0x80u;
|
||||
update(&pad, 1);
|
||||
const std::uint8_t zero = 0x00u;
|
||||
while (buffer_used_ != 56u) {
|
||||
update(&zero, 1);
|
||||
}
|
||||
std::uint8_t length_bytes[8];
|
||||
for (unsigned i = 0; i < 8; ++i) {
|
||||
length_bytes[i] = static_cast<std::uint8_t>((total_bits >> (56u - i * 8u)) & 0xFFu);
|
||||
}
|
||||
std::memcpy(buffer_ + buffer_used_, length_bytes, 8);
|
||||
buffer_used_ += 8;
|
||||
transform(buffer_);
|
||||
buffer_used_ = 0;
|
||||
for (unsigned i = 0; i < 8; ++i) {
|
||||
out[i * 4 + 0] = static_cast<std::uint8_t>((state_[i] >> 24) & 0xFFu);
|
||||
out[i * 4 + 1] = static_cast<std::uint8_t>((state_[i] >> 16) & 0xFFu);
|
||||
out[i * 4 + 2] = static_cast<std::uint8_t>((state_[i] >> 8) & 0xFFu);
|
||||
out[i * 4 + 3] = static_cast<std::uint8_t>(state_[i] & 0xFFu);
|
||||
}
|
||||
}
|
||||
|
||||
std::string Sha256::finish_hex() {
|
||||
std::uint8_t digest[32];
|
||||
finish(digest);
|
||||
static const char* kHex = "0123456789abcdef";
|
||||
std::string text;
|
||||
text.resize(64);
|
||||
for (unsigned i = 0; i < 32; ++i) {
|
||||
text[i * 2] = kHex[(digest[i] >> 4) & 0x0Fu];
|
||||
text[i * 2 + 1] = kHex[digest[i] & 0x0Fu];
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
std::string sha256_hex(const void* data, std::size_t size) {
|
||||
Sha256 hash;
|
||||
hash.update(data, size);
|
||||
return hash.finish_hex();
|
||||
}
|
||||
|
||||
bool sha256_hex_matches(const void* data, std::size_t size, const std::string& expected_hex) {
|
||||
if (expected_hex.size() != 64) {
|
||||
return false;
|
||||
}
|
||||
std::string actual = sha256_hex(data, size);
|
||||
for (std::size_t i = 0; i < 64; ++i) {
|
||||
char expected = expected_hex[i];
|
||||
if (expected >= 'A' && expected <= 'F') {
|
||||
expected = static_cast<char>(expected - 'A' + 'a');
|
||||
}
|
||||
if (actual[i] != expected) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace joc::crypto
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace joc::crypto {
|
||||
|
||||
class Sha256 {
|
||||
public:
|
||||
Sha256() { reset(); }
|
||||
|
||||
void reset();
|
||||
void update(const void* data, std::size_t size);
|
||||
void finish(std::uint8_t out[32]);
|
||||
std::string finish_hex();
|
||||
|
||||
private:
|
||||
void transform(const std::uint8_t block[64]);
|
||||
|
||||
std::uint32_t state_[8] = {};
|
||||
std::uint64_t bit_count_ = 0;
|
||||
std::uint8_t buffer_[64] = {};
|
||||
std::size_t buffer_used_ = 0;
|
||||
};
|
||||
|
||||
std::string sha256_hex(const void* data, std::size_t size);
|
||||
bool sha256_hex_matches(const void* data, std::size_t size, const std::string& expected_hex);
|
||||
|
||||
} // namespace joc::crypto
|
||||
@@ -0,0 +1,48 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "joc_core.h"
|
||||
|
||||
namespace joc {
|
||||
|
||||
class Status {
|
||||
public:
|
||||
Status() = default;
|
||||
|
||||
static Status success() { return Status(); }
|
||||
|
||||
static Status fail(joc_error code, std::string stage, std::string message) {
|
||||
Status s;
|
||||
s.code_ = code;
|
||||
s.stage_ = std::move(stage);
|
||||
s.message_ = std::move(message);
|
||||
return s;
|
||||
}
|
||||
|
||||
bool ok() const { return code_ == JOC_OK; }
|
||||
joc_error code() const { return code_; }
|
||||
const std::string& stage() const { return stage_; }
|
||||
const std::string& message() const { return message_; }
|
||||
|
||||
private:
|
||||
joc_error code_ = JOC_OK;
|
||||
std::string stage_ = "none";
|
||||
std::string message_;
|
||||
};
|
||||
|
||||
// Stage names are kept as plain literals so that C++ and the Python frontend
|
||||
namespace stage {
|
||||
inline constexpr const char* kFoundation = "foundation";
|
||||
inline constexpr const char* kEac3 = "eac3_transport";
|
||||
inline constexpr const char* kEmdf = "emdf";
|
||||
inline constexpr const char* kJoc = "joc";
|
||||
inline constexpr const char* kOamd = "oamd";
|
||||
inline constexpr const char* kDsp = "dsp";
|
||||
inline constexpr const char* kRender = "render";
|
||||
inline constexpr const char* kOutput = "output";
|
||||
} // namespace stage
|
||||
|
||||
} // namespace joc
|
||||
Reference in new issue
Block a user