#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSFORMATTING_HPP #define CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSFORMATTING_HPP #include "SceTypes.hpp" #if defined(__x86_64__) #include "prx/libc/include/ExtendedPrecisionParts.hpp" #endif #include "prx/libc/include/GuestLongDouble.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include extern "C" std::FILE* GuestStandardOutputFile_nid_no_patch(); extern "C" std::FILE* GuestStandardOutputForWriting_nid_no_patch(); namespace LibcDetail { enum class ArgumentClass : unsigned char { Integer, Double, LongDouble }; class FormatConstraintViolation : public std::invalid_argument { public: using std::invalid_argument::invalid_argument; }; class FormatArguments { VaList args; std::vector> numbered; size_t position = 1; public: explicit FormatArguments(const void* source) { std::memcpy(&args, source, sizeof(args)); } void Number(const std::vector& classes) { numbered.resize(classes.size()); for (size_t index = 0; index < classes.size(); ++index) { const auto store = [&](auto value) { std::memcpy(numbered[index].data(), &value, sizeof(value)); }; if (classes[index] == ArgumentClass::LongDouble) store(Fetch()); else if (classes[index] == ArgumentClass::Double) store(Fetch()); else store(Fetch()); } } void Seek(size_t index) { position = index; } template T At(size_t index) { if (index == 0 || index > numbered.size()) throw std::out_of_range("Format argument position out of range"); T value; std::memcpy(&value, numbered[index - 1].data(), sizeof(value)); return value; } template T Next() { return numbered.empty() ? Fetch() : At(position++); } private: template T Fetch() { const void* address; if constexpr (std::is_same_v) { if (args.fp_offset < 176) { address = static_cast(args.reg_save_area) + args.fp_offset; args.fp_offset += 16; } else { address = args.overflow_arg_area; args.overflow_arg_area = static_cast(args.overflow_arg_area) + 8; } } else if constexpr (std::is_same_v) { const auto aligned = (reinterpret_cast(args.overflow_arg_area) + 15) & ~std::uintptr_t(15); address = reinterpret_cast(aligned); args.overflow_arg_area = reinterpret_cast(aligned + 16); } else { static_assert(sizeof(T) <= 8); if (args.gp_offset < 48) { address = static_cast(args.reg_save_area) + args.gp_offset; args.gp_offset += 8; } else { address = args.overflow_arg_area; args.overflow_arg_area = static_cast(args.overflow_arg_area) + 8; } } if constexpr (std::is_same_v) { return GuestLongDoubleLoad(address); } else { T value; std::memcpy(&value, address, sizeof(value)); return value; } } }; constexpr unsigned long long hostSpecLimit = 4096; constexpr unsigned long long clampedPrecision = 30; struct LargeSpec { std::string flags; std::string tail; unsigned long long width = 0; unsigned long long precision = 0; bool hasPrecision = false; }; inline bool SplitLargeSpec(const std::string& format, LargeSpec& result) { constexpr unsigned long long saturated = 1000000000000000ULL; std::size_t index = 1; for (; index < format.size() && std::strchr("-+ #0", format[index]); ++index) result.flags += format[index]; for (; index < format.size() && format[index] >= '0' && format[index] <= '9'; ++index) if (result.width < saturated) result.width = result.width * 10 + static_cast(format[index] - '0'); if (index < format.size() && format[index] == '.') { result.hasPrecision = true; for (++index; index < format.size() && format[index] >= '0' && format[index] <= '9'; ++index) if (result.precision < saturated) result.precision = result.precision * 10 + static_cast(format[index] - '0'); } result.tail = format.substr(index); return !result.tail.empty() && (result.width > hostSpecLimit || result.precision > hostSpecLimit); } template unsigned long long LargePrecision(unsigned long long precision, T) { return precision < clampedPrecision ? precision : clampedPrecision; } class FormatOutput { char* destination; size_t capacity; size_t count = 0; std::string* complete; public: FormatOutput(char* buffer, size_t size, std::string* text) : destination(buffer), capacity(size), complete(text) { if (size && !buffer) throw std::invalid_argument("Null formatting buffer"); if (capacity) destination[0] = 0; } void Append(const char* text, size_t size) { if (size > static_cast(INT_MAX) - count) throw std::overflow_error("Formatted output exceeds INT_MAX"); if (capacity && count < capacity - 1) { const size_t available = capacity - 1 - count; const size_t copied = size < available ? size : available; std::memcpy(destination + count, text, copied); destination[count + copied] = 0; } if (complete) complete->append(text, size); count += size; } template void LargeValue(const LargeSpec& large, T value) { if (large.width > static_cast(INT_MAX) || large.precision > static_cast(INT_MAX)) throw std::overflow_error("Formatted output exceeds INT_MAX"); const char conversion = large.tail.back(); const bool integer = std::strchr("diouxX", conversion) != nullptr; const bool leftAligned = large.flags.find('-') != std::string::npos; const bool zeroPadded = large.flags.find('0') != std::string::npos && integer && !large.hasPrecision; std::string core = "%"; for (const char flag : large.flags) if (flag != '-' && flag != '0') core += flag; unsigned long long extraZeros = 0; std::string body; if constexpr (std::is_convertible_v) { const char* text = value ? value : "(null)"; size_t length = 0; while ((!large.hasPrecision || length < large.precision) && text[length]) ++length; body.assign(text, length); } else { if (large.hasPrecision) { const unsigned long long inner = LargePrecision(large.precision, value); if (integer && large.precision > inner) extraZeros = large.precision - inner; core += "." + std::to_string(inner); } core += large.tail; const int size = std::snprintf(nullptr, 0, core.c_str(), value); if (size < 0) throw std::runtime_error("Formatting conversion failed"); std::vector text(static_cast(size) + 1); if (std::snprintf(text.data(), text.size(), core.c_str(), value) != size) throw std::runtime_error("Inconsistent formatting conversion"); body.assign(text.data(), static_cast(size)); } size_t digits = 0; if (integer) { if (digits < body.size() && std::strchr("+- ", body[digits])) ++digits; if ((conversion == 'x' || conversion == 'X') && digits + 1 < body.size() && body[digits] == '0' && (body[digits + 1] == 'x' || body[digits + 1] == 'X')) digits += 2; } const unsigned long long natural = static_cast(body.size()) + extraZeros; const unsigned long long padding = large.width > natural ? large.width - natural : 0; const unsigned long long total = natural + padding; if (total > static_cast(INT_MAX) - count) throw std::overflow_error("Formatted output exceeds INT_MAX"); const auto repeat = [&](char fill, unsigned long long amount) { const std::string chunk(static_cast(amount < hostSpecLimit ? amount : hostSpecLimit), fill); while (amount) { const size_t piece = static_cast(amount < chunk.size() ? amount : chunk.size()); Append(chunk.data(), piece); amount -= piece; } }; if (!leftAligned && !zeroPadded) repeat(' ', padding); Append(body.data(), digits); repeat('0', extraZeros + (zeroPadded && !leftAligned ? padding : 0)); Append(body.data() + digits, body.size() - digits); if (leftAligned) repeat(' ', padding); } template void Value(const std::string& format, T value) { LargeSpec large; if (SplitLargeSpec(format, large)) { LargeValue(large, value); return; } const int size = std::snprintf(nullptr, 0, format.c_str(), value); if (size < 0) throw std::runtime_error("Formatting conversion failed"); if (static_cast(size) > static_cast(INT_MAX) - count) throw std::overflow_error("Formatted output exceeds INT_MAX"); if (complete) { std::vector text(static_cast(size) + 1); const int written = std::snprintf(text.data(), text.size(), format.c_str(), value); if (written != size) throw std::runtime_error("Inconsistent formatting conversion"); complete->append(text.data(), static_cast(size)); } if (capacity && count < capacity - 1) { const size_t remaining = capacity - count; const size_t required = static_cast(size) + 1; const int written = std::snprintf(destination + count, remaining < required ? remaining : required, format.c_str(), value); if (written != size) throw std::runtime_error("Inconsistent formatting conversion"); } count += static_cast(size); } int Count() const { return static_cast(count); } }; // The guest's wchar_t is 16 bits (UTF-16, as on the PS4/PS5 toolchain); wide conversions are // formatted as UTF-8 through %s. inline void AppendUtf8(std::string& utf8, char32_t code) { if (code > 0x10ffff) code = 0xfffd; if (code < 0x80) { utf8 += static_cast(code); } else if (code < 0x800) { utf8 += static_cast(0xc0 | (code >> 6)); utf8 += static_cast(0x80 | (code & 0x3f)); } else if (code < 0x10000) { utf8 += static_cast(0xe0 | (code >> 12)); utf8 += static_cast(0x80 | ((code >> 6) & 0x3f)); utf8 += static_cast(0x80 | (code & 0x3f)); } else { utf8 += static_cast(0xf0 | (code >> 18)); utf8 += static_cast(0x80 | ((code >> 12) & 0x3f)); utf8 += static_cast(0x80 | ((code >> 6) & 0x3f)); utf8 += static_cast(0x80 | (code & 0x3f)); } } struct FloatSpec { bool left = false; bool plus = false; bool space = false; bool alternate = false; bool zero = false; std::size_t width = 0; int precision = -1; }; inline std::string ApplicableLength(const std::string& length, char conversion) { if (std::strchr("diouxXn", conversion)) return length == "L" ? std::string() : length; if (std::strchr("aAeEfFgG", conversion)) return length == "L" ? length : std::string(); if (conversion == 'c' || conversion == 's') return length == "l" ? length : std::string(); if (conversion == 'p' || conversion == 'S') return std::string(); return length; } inline FloatSpec ParseFloatSpec(const std::string& spec) { FloatSpec result; std::size_t index = 1; for (; index < spec.size() && std::strchr("-+ #0", spec[index]); ++index) { result.left |= spec[index] == '-'; result.plus |= spec[index] == '+'; result.space |= spec[index] == ' '; result.alternate |= spec[index] == '#'; result.zero |= spec[index] == '0'; } for (; index < spec.size() && spec[index] >= '0' && spec[index] <= '9'; ++index) { const auto digit = static_cast(spec[index] - '0'); if (result.width > (static_cast(INT_MAX) - digit) / 10) throw std::overflow_error("Formatted output exceeds INT_MAX"); result.width = result.width * 10 + digit; } if (index < spec.size() && spec[index] == '.') { result.precision = 0; for (++index; index < spec.size(); ++index) { const int digit = spec[index] - '0'; if (result.precision > (INT_MAX - digit) / 10) throw std::overflow_error("Floating precision exceeds int range"); result.precision = result.precision * 10 + digit; } } return result; } inline std::string PadFloat(const FloatSpec& spec, const std::string& prefix, const std::string& body) { const std::size_t length = prefix.size() + body.size(); if (length >= spec.width) return prefix + body; const std::string padding(spec.width - length, spec.left || !spec.zero ? ' ' : '0'); if (spec.left) return prefix + body + padding; return spec.zero ? prefix + padding + body : padding + prefix + body; } inline std::string HexDigits(const std::string& spec, char conversion, bool negative, std::uint64_t lead, std::uint64_t fraction, int total, int exponent, std::size_t maximumLength) { const FloatSpec parsed = ParseFloatSpec(spec); const int precision = parsed.precision; const bool upper = conversion == 'A'; int digits = total; if (precision >= 0 && precision < total) { const int dropped = (total - precision) * 4; const std::uint64_t remainder = dropped == 64 ? fraction : fraction & ((std::uint64_t{1} << dropped) - 1); fraction = dropped == 64 ? 0 : fraction >> dropped; if (remainder >= std::uint64_t{1} << (dropped - 1) && ++fraction >> (precision * 4)) { fraction &= (std::uint64_t{1} << (precision * 4)) - 1; ++lead; } digits = precision; } else if (precision < 0) { for (; digits > 0 && (fraction & 0xf) == 0; --digits) fraction >>= 4; } const char* hex = upper ? "0123456789ABCDEF" : "0123456789abcdef"; std::string suffix = upper ? "P" : "p"; suffix += exponent < 0 ? '-' : '+'; suffix += std::to_string(exponent < 0 ? -exponent : exponent); std::string prefix = negative ? "-" : parsed.plus ? "+" : parsed.space ? " " : ""; prefix += upper ? "0X" : "0x"; const auto fractionalDigits = static_cast(precision > total ? precision : digits); const bool point = fractionalDigits != 0 || parsed.alternate; const std::size_t length = prefix.size() + 1 + point + fractionalDigits + suffix.size(); if (length > maximumLength || parsed.width > maximumLength) throw std::overflow_error("Formatted output exceeds INT_MAX"); std::string body(1, hex[lead]); if (point) body += '.'; for (int digit = digits - 1; digit >= 0; --digit) body += hex[(fraction >> (digit * 4)) & 0xf]; if (precision > total) body.append(static_cast(precision - total), '0'); body += suffix; return PadFloat(parsed, prefix, body); } inline std::string HexFloat(const std::string& spec, char conversion, double value, std::size_t maximumLength = INT_MAX) { const std::uint64_t bits = std::bit_cast(value); int exponent = static_cast((bits >> 52) & 0x7ff); std::uint64_t fraction = bits & ((std::uint64_t{1} << 52) - 1); std::uint64_t lead = 1; if (exponent != 0) { exponent -= 1023; } else if (fraction == 0) { lead = 0; } else { exponent = -1022; while ((fraction & (std::uint64_t{1} << 52)) == 0) { fraction <<= 1; --exponent; } fraction &= (std::uint64_t{1} << 52) - 1; } return HexDigits(spec, conversion, (bits >> 63) != 0, lead, fraction, 13, exponent, maximumLength); } inline std::string HexFloat(const std::string& spec, char conversion, long double value, std::size_t maximumLength = INT_MAX) { if (value == 0) return HexDigits(spec, conversion, std::signbit(value), 0, 0, 16, 0, maximumLength); int exponent = 0; const auto mantissa = static_cast(std::ldexp(std::frexp(std::fabs(value), &exponent), 64)); return HexDigits(spec, conversion, std::signbit(value), 1, mantissa << 1, 16, exponent - 1, maximumLength); } struct DecimalDigits { std::string digits; long long exponent = 0; bool truncated = false; bool tiesDown = false; }; inline std::string DecimalChunks(std::vector words) { std::string text; while (!words.empty()) { std::uint64_t remainder = 0; for (std::size_t index = words.size(); index-- > 0;) { const std::uint64_t current = (remainder << 32) | words[index]; words[index] = static_cast(current / 1000000000u); remainder = current % 1000000000u; } while (!words.empty() && words.back() == 0) words.pop_back(); std::string chunk = std::to_string(remainder); if (!words.empty()) chunk.insert(0, 9 - chunk.size(), '0'); text.insert(0, chunk); } return text; } inline DecimalDigits ConsoleDecimalDigits(long double value) { DecimalDigits result; if (value == 0) return result; int binary = 0; const auto mantissa = static_cast(std::ldexp(std::frexp(std::fabs(value), &binary), 64)); const long long estimate = static_cast(binary) * 30103 / 100000; const long long last = (estimate >= 0 ? estimate / 4 : -((3 - estimate) / 4)) * 4 - 20; result.tiesDown = estimate >= 8; binary -= 64; std::string integer; std::string fraction; bool remaining = false; if (binary >= 0) { const auto word = static_cast(binary) / 32; const auto bit = static_cast(binary) % 32; std::vector words(word + 3, 0); for (std::size_t part = 0; part < 2; ++part) { const std::uint64_t shifted = ((mantissa >> (32 * part)) & 0xffffffffu) << bit; words[word + part] |= static_cast(shifted); words[word + part + 1] |= static_cast(shifted >> 32); } while (!words.empty() && words.back() == 0) words.pop_back(); integer = DecimalChunks(std::move(words)); } else { const auto shift = static_cast(-static_cast(binary)); const std::uint64_t whole = shift >= 64 ? 0 : mantissa >> shift; if (whole != 0) integer = std::to_string(whole); std::vector words(shift / 32 + 2, 0); const std::uint64_t remainder = shift >= 64 ? mantissa : mantissa & ((std::uint64_t{1} << shift) - 1); words[0] = static_cast(remainder); words[1] = static_cast(remainder >> 32); const std::size_t top = shift / 32; const auto bit = static_cast(shift % 32); const auto significant = [&] { const std::size_t leading = fraction.find_first_not_of('0'); return integer.size() + (leading == std::string::npos ? 0 : fraction.size() - leading); }; remaining = remainder != 0; while (remaining && significant() < 30) { std::uint64_t carry = 0; for (auto& word : words) { const std::uint64_t product = static_cast(word) * 1000000000u + carry; word = static_cast(product); carry = product >> 32; } const std::uint64_t window = (static_cast(words[top + 1]) << 32) | words[top]; const std::string chunk = std::to_string(static_cast(window >> bit)); words[top + 1] = 0; words[top] &= bit == 0 ? 0 : (std::uint32_t{1} << bit) - 1; fraction += std::string(9 - chunk.size(), '0') + chunk; remaining = std::any_of(words.begin(), words.end(), [](std::uint32_t word) { return word != 0; }); } } if (!integer.empty()) { result.exponent = static_cast(integer.size()) - 1; result.digits = integer + fraction; } else { const std::size_t leading = fraction.find_first_not_of('0'); result.exponent = -static_cast(leading) - 1; result.digits = fraction.substr(leading); } const long long kept = result.exponent - last + 1; if (static_cast(result.digits.size()) > kept) { result.truncated = result.digits.find_first_not_of('0', static_cast(kept)) != std::string::npos; result.digits.resize(static_cast(kept)); } result.truncated |= remaining; return result; } inline DecimalDigits RoundDecimal(const DecimalDigits& value, long long count) { if (value.digits.empty()) return value; if (count <= 0) { if (count == 0 && value.digits[0] >= '5') return {"1", value.exponent + 1}; return {}; } if (count >= static_cast(value.digits.size())) return value; DecimalDigits result{value.digits.substr(0, static_cast(count)), value.exponent}; const auto next = static_cast(count); if (value.digits[next] < '5') return result; if (value.digits[next] == '5' && value.tiesDown && !value.truncated && value.digits.find_first_not_of('0', next + 1) == std::string::npos) return result; for (std::size_t index = result.digits.size(); index-- > 0;) { if (result.digits[index] != '9') { ++result.digits[index]; return result; } result.digits[index] = '0'; } result.digits.insert(result.digits.begin(), '1'); result.digits.pop_back(); ++result.exponent; return result; } #if defined(__x86_64__) inline DecimalDigits GeneratedDigits(long double value, char lower, long long precision) { static constexpr long double Tenth[3] = {0x19999999p-32L, 0x99999999p-64L, 0x99999999p-96L}; DecimalDigits result; if (value == 0) return result; ExtendedWords words = WordsOf(value); words.signExponent &= 0x7fff; long double magnitude = 0; StoreWords(magnitude, words); long double fraction = magnitude; short binary = 0; UnscaleExtended(binary, fraction); std::array parts{}, factor{}, copy{}; std::array scratch{}; SetWide(parts.data(), 3, magnitude); long long exponent = static_cast(binary) * 30103 / 100000; long long steps = 0; if (exponent <= 3) { steps = (7 - exponent) & ~3LL; exponent = -steps; if (steps != 0) SetWide(factor.data(), 3, 10.0L); } else { exponent = (exponent - 4) & ~3LL; steps = exponent; if (steps != 0) std::copy_n(Tenth, 3, factor.begin()); } for (; steps > 0; steps >>= 1) { if ((steps & 1) != 0) MultiplyWide(parts.data(), 3, factor.data(), 3, scratch.data()); if (steps < 2) break; copy = factor; MultiplyWide(factor.data(), 3, copy.data(), 3, scratch.data()); } std::string text; for (long long generate = std::min(precision + (lower == 'f' ? exponent + 11 : 7), 22LL); generate > 0; generate -= 8) { std::int64_t chunk = 0; while (parts[0] >= 1.0L) { const auto whole = static_cast(parts[0]); chunk += whole; AddHalf(parts.data(), 3, -static_cast(whole)); } if (generate >= 9) MultiplyHalf(parts.data(), 3, 100000000.0L); const std::string block = std::to_string(chunk % 100000000); text.append(8 - block.size(), '0').append(block); } exponent += 7; const std::size_t leading = std::min(text.find_first_not_of('0'), text.size()); text.erase(0, leading); exponent -= static_cast(leading); long long count = std::min(precision + (lower == 'f' ? exponent + 1 : lower == 'e' ? 1 : 0), static_cast(text.size())); if (count >= 0) { const bool up = count < static_cast(text.size()) && text[static_cast(count)] >= '5'; while (count > 0 && text[static_cast(count) - 1] == (up ? '9' : '0')) --count; if (up && count > 0) { ++text[static_cast(count) - 1]; } else if (up) { text.insert(0, 1, '1'); count = 1; ++exponent; } } text.resize(static_cast(std::max(count, 0LL))); result.digits = std::move(text); result.exponent = exponent; return result; } #endif inline DecimalDigits RoundedDigits(long double value, char lower, long long precision) { #if defined(__x86_64__) if (std::numeric_limits::digits == 64) return GeneratedDigits(value, lower, precision); #endif const DecimalDigits exact = ConsoleDecimalDigits(value); return RoundDecimal(exact, lower == 'g' ? precision : lower == 'e' ? precision + 1 : exact.exponent + 1 + precision); } inline char DigitAt(const DecimalDigits& value, long long position) { const long long index = value.exponent - position; if (value.digits.empty() || index < 0 || index >= static_cast(value.digits.size())) return '0'; return value.digits[static_cast(index)]; } inline std::string DecimalFloat(const std::string& spec, char conversion, long double value, std::size_t maximumLength = INT_MAX) { const FloatSpec parsed = ParseFloatSpec(spec); const bool upper = conversion == 'E' || conversion == 'G'; const char lower = static_cast(conversion | 0x20); long long precision = parsed.precision < 0 ? 6 : parsed.precision; if (lower == 'g' && precision == 0) precision = 1; const DecimalDigits rounded = RoundedDigits(value, lower, precision); const long long exponent = rounded.digits.empty() ? 0 : rounded.exponent; bool exponential = lower == 'e'; bool strip = false; if (lower == 'g') { exponential = !(precision > exponent && exponent >= -4); precision = exponential ? precision - 1 : precision - 1 - exponent; strip = !parsed.alternate; } if (strip) { const long long last = rounded.digits.empty() ? 0 : rounded.exponent - static_cast(rounded.digits.size()) + 1; long long needed = exponential ? exponent - last : -last; while (needed > 0 && DigitAt(rounded, (exponential ? exponent : 0) - needed) == '0') --needed; precision = std::max(0LL, std::min(precision, needed)); } const std::string prefix = std::signbit(value) ? "-" : parsed.plus ? "+" : parsed.space ? " " : ""; const bool point = precision > 0 || parsed.alternate; const std::string magnitude = std::to_string(exponent < 0 ? -exponent : exponent); const long long whole = exponential || exponent < 0 ? 1 : exponent + 1; const long long exponentLength = exponential ? 2 + static_cast(std::max(magnitude.size(), 2)) : 0; const long long length = static_cast(prefix.size()) + whole + point + precision + exponentLength; if (length > static_cast(maximumLength) || parsed.width > maximumLength) throw std::overflow_error("Formatted output exceeds INT_MAX"); std::string body; body.reserve(static_cast(length)); const long long first = exponential ? exponent : std::max(exponent, 0LL); for (long long position = first; position >= (exponential ? exponent : 0); --position) body += DigitAt(rounded, position); if (point) body += '.'; const long long start = exponential ? exponent - 1 : -1; for (long long position = start; position > start - precision; --position) body += DigitAt(rounded, position); if (exponential) { body += upper ? 'E' : 'e'; body += exponent < 0 ? '-' : '+'; if (magnitude.size() < 2) body += '0'; body += magnitude; } return PadFloat(parsed, prefix, body); } inline std::string PaddedStringSpec(const std::string& spec) { std::string result = "%"; std::size_t index = 1; for (; index < spec.size() && std::strchr("-+ #0", spec[index]); ++index) if (spec[index] == '-' && result.size() == 1) result += '-'; for (; index < spec.size() && spec[index] >= '0' && spec[index] <= '9'; ++index) result += spec[index]; return result + 's'; } inline std::string NanText(const std::string& spec, bool negative, bool upper) { std::string sign; if (negative) sign = "-"; else if (spec.find('+') != std::string::npos) sign = "+"; else if (spec.find(' ') != std::string::npos) sign = " "; return sign + (upper ? "NAN" : "nan"); } inline std::string FormatPointer(const std::string& spec, const void* pointer) { std::size_t index = 1; bool left = false; bool zero = false; for (; index < spec.size() && std::strchr("-+ #0", spec[index]); ++index) { if (spec[index] == '-') left = true; if (spec[index] == '0') zero = true; } const auto readNumber = [&] { std::size_t number = 0; for (; index < spec.size() && spec[index] >= '0' && spec[index] <= '9'; ++index) { number = number * 10 + static_cast(spec[index] - '0'); if (number > static_cast(INT_MAX)) throw std::overflow_error("Formatted output exceeds INT_MAX"); } return number; }; const std::size_t width = readNumber(); bool hasPrecision = false; std::size_t precision = 0; if (index < spec.size() && spec[index] == '.') { ++index; hasPrecision = true; precision = readNumber(); } const auto value = static_cast(reinterpret_cast(pointer)); std::string digits; for (unsigned long long rest = value; rest != 0; rest >>= 4) digits.insert(digits.begin(), "0123456789abcdef"[rest & 15]); if (digits.empty() && !(hasPrecision && precision == 0)) digits = "0"; if (digits.size() < precision) digits.insert(0, precision - digits.size(), '0'); const std::size_t padding = width > digits.size() ? width - digits.size() : 0; if (left) return digits + std::string(padding, ' '); if (zero && !hasPrecision) return std::string(padding, '0') + digits; return std::string(padding, ' ') + digits; } inline constexpr char KnownConversions[] = "diouxXeEfFgGaAcsSpn"; constexpr size_t maximumArgumentPosition = 65535; inline size_t ArgumentPosition(const char*& format) { const char* end = format; while (*end >= '0' && *end <= '9') ++end; if (end == format || *end != '$') return 0; size_t position = 0; for (const char* digit = format; digit != end && position <= maximumArgumentPosition; ++digit) position = position * 10 + static_cast(*digit - '0'); if (position == 0) return 0; if (position > maximumArgumentPosition) throw std::invalid_argument("Format argument position out of range"); format = end + 1; return position; } inline std::vector NumberedArgumentClasses(const char* format, bool wide = false) { if (!std::strchr(format, '$')) return {}; std::vector classes; bool numbered = false; size_t next = 1; const auto take = [&](size_t position, ArgumentClass type) { if (classes.size() < position) classes.resize(position); classes[position - 1] = type; }; while ((format = std::strchr(format, '%')) != nullptr) { ++format; if (*format == '%') { ++format; continue; } if (const size_t position = ArgumentPosition(format)) { next = position; numbered = true; } while (*format && std::strchr("-+ #0123456789.*", *format)) { if (*format++ != '*') continue; const size_t position = ArgumentPosition(format); if (position) numbered = true; take(position ? position : next++, ArgumentClass::Integer); } bool longDouble = false; while (*format && std::strchr("hljztL", *format)) longDouble = *format++ == 'L'; const char conversion = *format; if (!conversion) break; ++format; if (std::strchr("aAeEfFgG", conversion)) take(next++, longDouble ? ArgumentClass::LongDouble : ArgumentClass::Double); else if (std::strchr(wide ? "diouxXcspn" : "diouxXcsSpn", conversion)) take(next++, ArgumentClass::Integer); } if (!numbered) classes.clear(); return classes; } inline int FormatWindows(char* buffer, size_t size, const char* format, const void* source, std::string* complete = nullptr, bool secure = false) { if (!format || !source) throw std::invalid_argument("Null formatting argument"); const char* const formatStart = format; FormatArguments args(source); args.Number(NumberedArgumentClasses(format)); FormatOutput output(buffer, size, complete); const auto star = [&] { const size_t position = ArgumentPosition(format); return position ? args.At(position) : args.Next(); }; while (*format) { const char* literal = format; while (*format && *format != '%') ++format; output.Append(literal, static_cast(format - literal)); if (!*format) break; ++format; if (*format == '%') { output.Append(format++, 1); continue; } if (const size_t position = ArgumentPosition(format)) args.Seek(position); std::string spec = "%"; for (; *format && std::strchr("-+ #0", *format); ++format) spec += *format; if (*format == '*') { ++format; const int width = star(); if (width < 0) spec += '-'; spec += std::to_string(width < 0 ? -static_cast(width) : width); } else { while (*format >= '0' && *format <= '9') spec += *format++; } size_t precisionLimit = std::numeric_limits::max(); if (*format == '.') { ++format; if (*format == '*') { ++format; const int precision = star(); if (precision >= 0) { spec += "." + std::to_string(precision); precisionLimit = static_cast(precision); } } else { spec += '.'; precisionLimit = 0; while (*format >= '0' && *format <= '9') { const auto digit = static_cast(*format - '0'); precisionLimit = precisionLimit <= (std::numeric_limits::max() - digit) / 10 ? precisionLimit * 10 + digit : std::numeric_limits::max(); spec += *format++; } } } std::string length; if (*format && std::strchr("hljztL", *format)) { length += *format++; if ((length == "h" && *format == 'h') || (length == "l" && *format == 'l')) length += *format++; } const char conversion = *format; if (!conversion) throw std::invalid_argument("Incomplete format conversion"); ++format; if (!std::strchr(KnownConversions, conversion)) { const char text[] = {'%', conversion, '\0'}; output.Value(PaddedStringSpec(spec), conversion == '%' ? "%" : text); continue; } length = ApplicableLength(length, conversion); const bool integerLength = length.empty() || length == "h" || length == "hh" || length == "l" || length == "ll" || length == "j" || length == "z" || length == "t"; if (conversion == 'd' || conversion == 'i') { if (!integerLength) throw std::invalid_argument("Invalid integer length"); long long value; if (length.empty() || length == "h" || length == "hh") { value = args.Next(); if (length == "h") value = static_cast(value); if (length == "hh") value = static_cast(value); } else value = args.Next(); output.Value(spec + "ll" + conversion, value); } else if (std::strchr("ouxX", conversion)) { if (!integerLength) throw std::invalid_argument("Invalid integer length"); for (size_t flag = 1; flag < spec.size() && std::strchr("-+ #0", spec[flag]);) { if (spec[flag] == ' ' || spec[flag] == '+') spec.erase(flag, 1); else ++flag; } unsigned long long value; if (length.empty() || length == "h" || length == "hh") { value = args.Next(); if (length == "h") value = static_cast(value); if (length == "hh") value = static_cast(value); } else value = args.Next(); if (conversion == 'o' && value == 0 && precisionLimit == 0 && spec.find('#') != std::string::npos) spec = spec.substr(0, spec.find('.')) + ".1"; output.Value(spec + "ll" + conversion, value); } else if (std::strchr("aAeEfFgG", conversion)) { if (length == "L") { const long double value = args.Next(); if (std::isnan(value)) { output.Value(PaddedStringSpec(spec), NanText(spec, std::signbit(value), conversion < 'a').c_str()); } else if (std::isfinite(value)) { const auto remaining = static_cast(INT_MAX - output.Count()); const std::string text = conversion == 'a' || conversion == 'A' ? HexFloat(spec, conversion, value, remaining) : DecimalFloat(spec, conversion, value, remaining); output.Append(text.data(), text.size()); } else { output.Value(spec + "L" + conversion, value); } } else { if (!length.empty() && length != "l") throw std::invalid_argument("Invalid floating length"); const double value = args.Next(); if (std::isnan(value)) { output.Value(PaddedStringSpec(spec), NanText(spec, std::signbit(value), conversion < 'a').c_str()); } else if ((conversion == 'a' || conversion == 'A') && std::isfinite(value)) { const std::string text = HexFloat(spec, conversion, value, static_cast(INT_MAX - output.Count())); output.Append(text.data(), text.size()); } else if (std::isfinite(value)) { const std::string text = DecimalFloat(spec, conversion, value, static_cast(INT_MAX - output.Count())); output.Append(text.data(), text.size()); } else { output.Value(spec + conversion, value); } } } else if (conversion == 'c' && (length.empty() || length == "h")) { output.Value(spec + conversion, args.Next()); } else if (conversion == 'c' && length == "l") { std::string utf8; AppendUtf8(utf8, static_cast(args.Next())); if (utf8[0] != 0) output.Value(spec.substr(0, spec.find('.')) + 's', utf8.c_str()); } else if (conversion == 's' && (length.empty() || length == "h")) { const char* value = args.Next(); if (!value && secure) throw FormatConstraintViolation("printf_s: bad %s argument"); if (!value) { value = "(null)"; spec = spec.substr(0, spec.find('.')); } output.Value(spec + 's', value); } else if ((conversion == 's' && length == "l") || (conversion == 'S' && length.empty())) { const char16_t* value = args.Next(); if (!value && secure) throw FormatConstraintViolation("printf_s: bad %ls argument"); if (!value) value = u"(null)"; std::string utf8; while (utf8.size() < precisionLimit && *value) { const char32_t code = *value++; const auto previousSize = utf8.size(); AppendUtf8(utf8, code); if (utf8.size() > precisionLimit) { utf8.resize(previousSize); break; } } output.Value(spec + 's', utf8.c_str()); } else if (conversion == 'p' && (length.empty() || length == "l")) { const std::string text = FormatPointer(spec, args.Next()); output.Append(text.data(), text.size()); } else if (conversion == 'n' && secure) { throw FormatConstraintViolation("printf_s: %n disallowed"); } else if (conversion == 'n' && integerLength && spec == "%") { void* pointer = args.Next(); if (!pointer) throw std::invalid_argument("Null format count pointer"); const int count = output.Count(); if (length == "hh") *static_cast(pointer) = static_cast(count); else if (length == "h") *static_cast(pointer) = static_cast(count); else if (length.empty()) *static_cast(pointer) = count; else *static_cast(pointer) = count; } else { throw std::invalid_argument("Unsupported format conversion '" + spec + length + conversion + "' in \"" + std::string(formatStart).substr(0, 160) + "\""); } } return output.Count(); } inline int PrintWindows(const char* format, const void* args) { auto* output = GuestStandardOutputForWriting_nid_no_patch(); if (!output) return -1; std::string buffer; const int size = FormatWindows(nullptr, 0, format, args, &buffer); if (std::fwrite(buffer.data(), 1, static_cast(size), output) != static_cast(size)) throw std::runtime_error("Formatted output write failed"); return size; } } #endif