From 2edc9c6fd10e34ca1da0d25d3ceb9b67a6b9c73c Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Fri, 7 Jul 2023 12:25:14 +0200 Subject: Add a C++ backend for LibertyXDR Also change the C backend so that it also de/serializes unions without any other fields besides the tag. --- tools/lxdrgen-cpp-win32.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tools/lxdrgen-cpp-win32.cpp (limited to 'tools/lxdrgen-cpp-win32.cpp') diff --git a/tools/lxdrgen-cpp-win32.cpp b/tools/lxdrgen-cpp-win32.cpp new file mode 100644 index 0000000..778a988 --- /dev/null +++ b/tools/lxdrgen-cpp-win32.cpp @@ -0,0 +1,47 @@ +// lxdrgen-cpp-win32.cpp: Win32 support code for lxdrgen-cpp.awk. +// +// Copyright (c) 2023, Přemysl Eric Janouch +// SPDX-License-Identifier: 0BSD +#include + +#include +#include +#include + +namespace LibertyXDR { + +bool utf8_to_wstring(const uint8_t *utf8, size_t length, std::wstring &wide) { + wide.clear(); + if (!length) + return true; + if (length > INT_MAX) + return false; + + int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + (LPCCH) utf8, length, nullptr, 0); + if (size <= 0) + return false; + + wide.resize(size); + return !MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + (LPCCH) utf8, length, wide.data(), size); +} + +bool wstring_to_utf8(const std::wstring &wide, std::string &utf8) { + utf8.clear(); + if (wide.empty()) + return true; + if (wide.size() > INT_MAX) + return false; + + int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + (LPCWCH) wide.data(), wide.size(), nullptr, 0, NULL, NULL); + if (size <= 0) + return false; + + utf8.resize(size); + return !WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + (LPCWCH) wide.data(), wide.size(), utf8.data(), size, NULL, NULL); +} + +} // namespace LibertyXDR -- cgit v1.2.3-54-g00ecf