1
0
Fork 0
forked from eden-emu/eden

Move dead submodules in-tree

Signed-off-by: swurl <swurl@swurl.xyz>
This commit is contained in:
swurl 2025-05-31 02:33:02 -04:00
parent c0cceff365
commit 6c655321e6
Signed by untrusted user: crueter
GPG key ID: A5A7629F109C8FD1
4081 changed files with 1185566 additions and 45 deletions

View file

@ -0,0 +1,6 @@
add_executable(tzdb2nx
main.cpp
tzif.cpp
tzif.h)
set(TZDB2NX_PATH "$<TARGET_FILE:tzdb2nx>" CACHE PATH "Path to tzdb2nx path")

View file

@ -0,0 +1,158 @@
#include "tzif.h"
#include <array>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
constexpr std::size_t ten_megabytes{(1 << 20) * 10};
static void ShortHelp(const char *argv0) {
std::fprintf(stderr, "Usage: %s [INFILE] [OUTFILE]\n", argv0);
}
static void PrintArg(const char *short_arg, const char *long_arg,
const char *text) {
std::fprintf(stderr, "%5s, %-20s %s\n", short_arg, long_arg, text);
}
static void PrintHelp(const char *argv0) {
ShortHelp(argv0);
std::fprintf(stderr,
"Converts a TZif file INFILE from the RFC8536 format to a "
"Nintendo Switch compatible file OUTFILE.\nWith no arguments, "
"tzdb2nx can read and write from stdin/stdout, "
"respectively.\nGiving no arguments without input will print "
"usage information and exit the program.\n\nArguments:\n");
PrintArg("-h", "--help", "Print this help text and exit");
}
int main(int argc, char *argv[]) {
int f{STDIN_FILENO};
const char *filename{"(stdin)"};
std::size_t filesize{ten_megabytes};
const char *optstring = "h";
int c;
const struct option longopts[] = {
{
"help",
no_argument,
nullptr,
'h',
},
{
nullptr,
0,
nullptr,
0,
},
};
while ((c = getopt_long(argc, argv, optstring, longopts, nullptr)) != -1) {
switch (c) {
case 'h':
PrintHelp(argv[0]);
return -1;
case '?':
ShortHelp(argv[0]);
return -1;
}
}
if (argc > 1) {
filename = argv[1];
f = open(filename, O_RDONLY);
if (f == -1) {
const int err = errno;
std::fprintf(stderr, "%s: %s\n", filename, std::strerror(err));
return err;
}
struct stat statbuf;
fstat(f, &statbuf);
filesize = statbuf.st_size;
} else {
struct pollfd fds {
f, POLLIN, 0,
};
const int result = poll(&fds, 1, 0);
if (result == 0) {
std::fprintf(stderr, "%s: No input\n", filename);
ShortHelp(argv[0]);
return -1;
}
}
u_int8_t *buf = new u_int8_t[filesize];
filesize = read(f, buf, filesize);
if (filesize == static_cast<std::size_t>(-1)) {
const int err = errno;
std::fprintf(stderr, "%s: %s\n", filename, std::strerror(err));
return err;
}
int result = close(f);
if (result == -1) {
const int err = errno;
std::fprintf(stderr, "%s: %s\n", filename, std::strerror(err));
return err;
}
if (filesize < 4) {
std::fprintf(stderr, "%s: Too small\n", filename);
return -1;
}
if (std::strncmp(reinterpret_cast<const char *>(buf), "TZif", 4) != 0) {
std::fprintf(stderr, "%s: Bad magic number\n", filename);
return -1;
}
const std::unique_ptr<Tzif::Data> tzif_data = Tzif::ReadData(buf, filesize);
if (tzif_data == nullptr) {
std::fprintf(stderr, "%s: Error occured while reading data\n", filename);
return -1;
}
delete[] buf;
std::vector<u_int8_t> output_buffer;
tzif_data->ReformatNintendo(output_buffer);
filename = "(stdout)";
f = STDOUT_FILENO;
if (argc > 2) {
filename = argv[2];
f = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (f == -1) {
const int err = errno;
std::fprintf(stderr, "%s: %s\n", filename, std::strerror(err));
return err;
}
}
result = write(f, output_buffer.data(), output_buffer.size());
if (result == -1) {
const int err = errno;
std::fprintf(stderr, "%s: %s\n", filename, std::strerror(err));
return err;
}
result = close(f);
if (result == -1) {
const int err = errno;
std::fprintf(stderr, "%s: %s\n", filename, std::strerror(err));
return err;
}
return 0;
}

View file

@ -0,0 +1,146 @@
#include "tzif.h"
#include <cstdint>
#include <cstring>
#include <memory>
#include <sys/types.h>
namespace Tzif {
static std::size_t SkipToVersion2(const u_int8_t *data, std::size_t size) {
char magic[5];
const u_int8_t *p{data};
std::memcpy(magic, data, 4);
magic[4] = '\0';
if (std::strcmp(magic, "TZif") != 0) {
return -1;
}
do {
p++;
if (p >= data + size) {
return -1;
}
} while (std::strncmp(reinterpret_cast<const char *>(p), "TZif", 4) != 0);
return p - data;
}
template <typename Type> constexpr static void SwapEndianess(Type *value) {
u_int8_t *data = reinterpret_cast<u_int8_t *>(value);
union {
u_int8_t data[sizeof(Type)];
Type value;
} temp;
for (u_int32_t i = 0; i < sizeof(Type); i++) {
u_int32_t alt_index = sizeof(Type) - i - 1;
temp.data[alt_index] = data[i];
}
*value = temp.value;
}
static void FlipHeader(Header &header) {
SwapEndianess(&header.isutcnt);
SwapEndianess(&header.isstdcnt);
SwapEndianess(&header.leapcnt);
SwapEndianess(&header.timecnt);
SwapEndianess(&header.typecnt);
SwapEndianess(&header.charcnt);
}
std::unique_ptr<DataImpl> ReadData(const u_int8_t *data, std::size_t size) {
const std::size_t v2_offset = SkipToVersion2(data, size);
if (v2_offset == static_cast<std::size_t>(-1)) {
return nullptr;
}
const u_int8_t *p = data + v2_offset;
Header header;
std::memcpy(&header, p, sizeof(header));
p += sizeof(header);
FlipHeader(header);
const std::size_t data_block_length =
header.timecnt * sizeof(int64_t) + header.timecnt * sizeof(u_int8_t) +
header.typecnt * sizeof(TimeTypeRecord) +
header.charcnt * sizeof(int8_t) + header.isstdcnt * sizeof(u_int8_t) +
header.isutcnt * sizeof(u_int8_t);
if (v2_offset + data_block_length + sizeof(Header) > size) {
return nullptr;
}
std::unique_ptr<DataImpl> impl = std::make_unique<DataImpl>();
impl->header = header;
const auto copy =
[]<typename Type>(std::unique_ptr<Type[]> &array, int length,
const u_int8_t *const &ptr) -> const u_int8_t * {
const std::size_t region_length = length * sizeof(Type);
array = std::make_unique<Type[]>(length);
std::memcpy(array.get(), ptr, region_length);
return ptr + region_length;
};
p = copy(impl->transition_times, header.timecnt, p);
p = copy(impl->transition_types, header.timecnt, p);
p = copy(impl->local_time_type_records, header.typecnt, p);
p = copy(impl->time_zone_designations, header.charcnt, p);
p = copy(impl->standard_indicators, header.isstdcnt, p);
p = copy(impl->ut_indicators, header.isutcnt, p);
const std::size_t footer_string_length = data + size - p - 2;
p++;
if (p + footer_string_length > data + size ||
p + footer_string_length < data) {
return nullptr;
}
impl->footer.tz_string = std::make_unique<char[]>(footer_string_length);
std::memcpy(impl->footer.tz_string.get(), p, footer_string_length);
impl->footer.footer_string_length = footer_string_length;
return impl;
}
static void PushToBuffer(std::vector<u_int8_t> &buffer, const void *data,
std::size_t size) {
const u_int8_t *p{reinterpret_cast<const u_int8_t *>(data)};
for (std::size_t i = 0; i < size; i++) {
buffer.push_back(*p);
p++;
}
}
void DataImpl::ReformatNintendo(std::vector<u_int8_t> &buffer) const {
buffer.clear();
Header header_copy{header};
header_copy.isstdcnt = 0;
header_copy.isutcnt = 0;
FlipHeader(header_copy);
PushToBuffer(buffer, &header_copy, sizeof(Header));
PushToBuffer(buffer, transition_times.get(),
header.timecnt * sizeof(int64_t));
PushToBuffer(buffer, transition_types.get(),
header.timecnt * sizeof(u_int8_t));
PushToBuffer(buffer, local_time_type_records.get(),
header.typecnt * sizeof(TimeTypeRecord));
PushToBuffer(buffer, time_zone_designations.get(),
header.charcnt * sizeof(int8_t));
// omit standard_indicators
// omit ut_indicators
PushToBuffer(buffer, &footer.nl_a, 1);
PushToBuffer(buffer, footer.tz_string.get(), footer.footer_string_length);
PushToBuffer(buffer, &footer.nl_b, 1);
}
} // namespace Tzif

View file

@ -0,0 +1,72 @@
#pragma once
#include <array>
#include <memory>
#include <sys/types.h>
#include <vector>
namespace Tzif {
typedef struct {
char magic[4];
u_int8_t version;
u_int8_t reserved[15];
u_int32_t isutcnt;
u_int32_t isstdcnt;
u_int32_t leapcnt;
u_int32_t timecnt;
u_int32_t typecnt;
u_int32_t charcnt;
} Header;
static_assert(sizeof(Header) == 0x2c);
class Footer {
public:
explicit Footer() = default;
~Footer() = default;
const char nl_a{'\n'};
std::unique_ptr<char[]> tz_string;
const char nl_b{'\n'};
std::size_t footer_string_length;
};
#pragma pack(push, 1)
typedef struct {
u_int32_t utoff;
u_int8_t dst;
u_int8_t idx;
} TimeTypeRecord;
#pragma pack(pop)
static_assert(sizeof(TimeTypeRecord) == 0x6);
class Data {
public:
explicit Data() = default;
virtual ~Data() = default;
virtual void ReformatNintendo(std::vector<u_int8_t> &buffer) const = 0;
};
class DataImpl : public Data {
public:
explicit DataImpl() = default;
~DataImpl() override = default;
void ReformatNintendo(std::vector<u_int8_t> &buffer) const override;
Header header;
Footer footer;
std::unique_ptr<int64_t[]> transition_times;
std::unique_ptr<u_int8_t[]> transition_types;
std::unique_ptr<TimeTypeRecord[]> local_time_type_records;
std::unique_ptr<int8_t[]> time_zone_designations;
std::unique_ptr<u_int8_t[]> standard_indicators;
std::unique_ptr<u_int8_t[]> ut_indicators;
};
std::unique_ptr<DataImpl> ReadData(const u_int8_t *data, std::size_t size);
} // namespace Tzif