small adjustments to printf

This commit is contained in:
georgemoralis 2023-10-06 14:46:12 +03:00
parent 551455e56e
commit e31365aea3
1 changed files with 273 additions and 292 deletions

View File

@ -50,13 +50,14 @@
#pragma once #pragma once
#include <stdio.h>
#include <cstdarg> #include <cstdarg>
#include <cstdbool> #include <cstdbool>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include "va_ctx.h" #include "va_ctx.h"
#include <stdio.h>
namespace Emulator::HLE::Libraries::LibC { namespace Emulator::HLE::Libraries::LibC {
// ntoa conversion buffer size, this must be big enough to hold // ntoa conversion buffer size, this must be big enough to hold
@ -388,28 +389,25 @@ static inline size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t ma
} }
#endif // PRINTF_SUPPORT_FLOAT #endif // PRINTF_SUPPORT_FLOAT
// internal vsnprintf // internal vsnprintf
static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, VaList* va_list) static inline int _vsnprintf(out_fct_type out, char* buffer, const char* format, VaList* va_list) {
{
unsigned int flags, width, precision, n; unsigned int flags, width, precision, n;
size_t idx = 0U; size_t idx = 0U;
auto maxlen = static_cast<size_t>(-1);
if (!buffer) { if (!buffer) {
// use null output function // use null output function
out = _out_null; out = _out_null;
} }
while (*format) while (*format) {
{
// format specifier? %[flags][width][.precision][length] // format specifier? %[flags][width][.precision][length]
if (*format != '%') { if (*format != '%') {
// no // no
out(*format, buffer, idx++, maxlen); out(*format, buffer, idx++, maxlen);
format++; format++;
continue; continue;
} } else {
else {
// yes, evaluate it // yes, evaluate it
format++; format++;
} }
@ -418,11 +416,31 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
flags = 0U; flags = 0U;
do { do {
switch (*format) { switch (*format) {
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; case '0':
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; flags |= FLAGS_ZEROPAD;
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; format++;
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; n = 1U;
case '#': flags |= FLAGS_HASH; format++; n = 1U; break; break;
case '-':
flags |= FLAGS_LEFT;
format++;
n = 1U;
break;
case '+':
flags |= FLAGS_PLUS;
format++;
n = 1U;
break;
case ' ':
flags |= FLAGS_SPACE;
format++;
n = 1U;
break;
case '#':
flags |= FLAGS_HASH;
format++;
n = 1U;
break;
default: n = 0U; break; default: n = 0U; break;
} }
} while (n); } while (n);
@ -431,15 +449,13 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
width = 0U; width = 0U;
if (_is_digit(*format)) { if (_is_digit(*format)) {
width = _atoi(&format); width = _atoi(&format);
} } else if (*format == '*') {
else if (*format == '*') {
const int w = vaArgInteger(va_list); // const int w = va.next<int>(cpu, mem); const int w = vaArgInteger(va_list); // const int w = va.next<int>(cpu, mem);
if (w < 0) { if (w < 0) {
flags |= FLAGS_LEFT; // reverse padding flags |= FLAGS_LEFT; // reverse padding
width = (unsigned int)-w; width = (unsigned int)-w;
} } else {
else {
width = (unsigned int)w; width = (unsigned int)w;
} }
format++; format++;
@ -452,8 +468,7 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
format++; format++;
if (_is_digit(*format)) { if (_is_digit(*format)) {
precision = _atoi(&format); precision = _atoi(&format);
} } else if (*format == '*') {
else if (*format == '*') {
precision = vaArgInteger(va_list); // precision = (unsigned int)va.next<int>(cpu, mem); precision = vaArgInteger(va_list); // precision = (unsigned int)va.next<int>(cpu, mem);
format++; format++;
} }
@ -491,8 +506,7 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++; format++;
break; break;
default : default: break;
break;
} }
// evaluate specifier // evaluate specifier
@ -508,15 +522,12 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
unsigned int base; unsigned int base;
if (*format == 'x' || *format == 'X') { if (*format == 'x' || *format == 'X') {
base = 16U; base = 16U;
} } else if (*format == 'o') {
else if (*format == 'o') {
base = 8U; base = 8U;
} } else if (*format == 'b') {
else if (*format == 'b') {
base = 2U; base = 2U;
flags &= ~FLAGS_HASH; // no hash for bin format flags &= ~FLAGS_HASH; // no hash for bin format
} } else {
else {
base = 10U; base = 10U;
flags &= ~FLAGS_HASH; // no hash for dec format flags &= ~FLAGS_HASH; // no hash for dec format
} }
@ -536,34 +547,35 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
auto value = vaArgLongLong(va_list); // const long long value = va.next<long long>(cpu, mem); auto value = vaArgLongLong(va_list); // const long long value = va.next<long long>(cpu, mem);
idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base,
precision, width, flags);
#endif #endif
} } else if (flags & FLAGS_LONG) {
else if (flags & FLAGS_LONG) {
auto value = vaArgLong(va_list); // const long value = va.next<long>(cpu, mem); auto value = vaArgLong(va_list); // const long value = va.next<long>(cpu, mem);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width,
} flags);
else { } else {
//const int value = (flags & FLAGS_CHAR) ? (char)va.next<int>(cpu, mem) : (flags & FLAGS_SHORT) ? (short int)va.next<int>(cpu, mem): va.next<int>(cpu, mem); // const int value = (flags & FLAGS_CHAR) ? (char)va.next<int>(cpu, mem) : (flags & FLAGS_SHORT) ? (short
// int)va.next<int>(cpu, mem): va.next<int>(cpu, mem);
const int value = (flags & FLAGS_CHAR) ? static_cast<char>(vaArgInteger(va_list)) const int value = (flags & FLAGS_CHAR) ? static_cast<char>(vaArgInteger(va_list))
: (flags & FLAGS_SHORT) ? static_cast<int16_t>(vaArgInteger(va_list)) : (flags & FLAGS_SHORT) ? static_cast<int16_t>(vaArgInteger(va_list))
: vaArgInteger(va_list); : vaArgInteger(va_list);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width,
flags);
} }
} } else {
else {
// unsigned // unsigned
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
//idx = _ntoa_long_long(out, buffer, idx, maxlen, va.next<unsigned long long>(cpu, mem), false, base, precision, width, flags); // idx = _ntoa_long_long(out, buffer, idx, maxlen, va.next<unsigned long long>(cpu, mem), false, base, precision, width,
idx = _ntoa_long_long(out, buffer, idx, maxlen, static_cast<u64>(vaArgLongLong(va_list)), false, base, precision, width, flags); // flags);
idx =
_ntoa_long_long(out, buffer, idx, maxlen, static_cast<u64>(vaArgLongLong(va_list)), false, base, precision, width, flags);
#endif #endif
} } else if (flags & FLAGS_LONG) {
else if (flags & FLAGS_LONG) {
// idx = _ntoa_long(out, buffer, idx, maxlen, va.next<unsigned long>(cpu, mem), false, base, precision, width, flags); // idx = _ntoa_long(out, buffer, idx, maxlen, va.next<unsigned long>(cpu, mem), false, base, precision, width, flags);
idx = _ntoa_long(out, buffer, idx, maxlen, static_cast<u32>(vaArgLong(va_list)), false, base, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, static_cast<u32>(vaArgLong(va_list)), false, base, precision, width, flags);
} } else {
else {
// const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va.next<unsigned int>(cpu, mem) : (flags & FLAGS_SHORT) ? // const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va.next<unsigned int>(cpu, mem) : (flags & FLAGS_SHORT) ?
// (unsigned short int)va.next<unsigned int>(cpu, mem) : va.next<unsigned int>(cpu, mem); // (unsigned short int)va.next<unsigned int>(cpu, mem) : va.next<unsigned int>(cpu, mem);
const unsigned int value = (flags & FLAGS_CHAR) ? static_cast<u08>(vaArgInteger(va_list)) const unsigned int value = (flags & FLAGS_CHAR) ? static_cast<u08>(vaArgInteger(va_list))
@ -637,15 +649,16 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
const bool is_ll = sizeof(uintptr_t) == sizeof(long long); const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) { if (is_ll) {
//idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va.next<Ptr<void>>(cpu, mem).address(), false, 16U, precision, width, flags); // idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va.next<Ptr<void>>(cpu, mem).address(), false, 16U, precision,
idx = _ntoa_long_long(out, buffer, idx, maxlen, reinterpret_cast<uintptr_t>(vaArgPtr<void>(va_list)), false, 16U, precision, width, flags); // width, flags);
} idx = _ntoa_long_long(out, buffer, idx, maxlen, reinterpret_cast<uintptr_t>(vaArgPtr<void>(va_list)), false, 16U, precision,
else { width, flags);
} else {
#endif #endif
//idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va.next<Ptr<void>>(cpu, mem).address()), false, 16U, precision, width, flags); // idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va.next<Ptr<void>>(cpu, mem).address()), false, 16U,
idx = _ntoa_long(out, buffer, idx, maxlen, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(vaArgPtr<void>(va_list))), false, 16U, // precision, width, flags);
precision, width, idx = _ntoa_long(out, buffer, idx, maxlen, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(vaArgPtr<void>(va_list))), false,
flags); 16U, precision, width, flags);
#if defined(PRINTF_SUPPORT_LONG_LONG) #if defined(PRINTF_SUPPORT_LONG_LONG)
} }
#endif #endif
@ -675,41 +688,9 @@ static inline int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen
static int printf_ctx(VaCtx* ctx) { static int printf_ctx(VaCtx* ctx) {
const char* format = vaArgPtr<const char>(&ctx->va_list); const char* format = vaArgPtr<const char>(&ctx->va_list);
char buffer[256]; char buffer[256];
int result= _vsnprintf(_out_buffer, buffer, (size_t)-1, format, &ctx->va_list); int result = _vsnprintf(_out_buffer, buffer, format, &ctx->va_list);
puts(buffer); printf("%s", buffer);
return result; return result;
} }
#if 0
///////////////////////////////////////////////////////
/**
* Tiny sprintf implementation
* Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD!
* \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output!
* \param format A string that specifies the format of the output
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
*/
inline int sprintf(char* buffer, const char* format, CPUState &cpu, MemState &mem, module::vargs &args)
{
const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, cpu, mem, args);
return ret;
}
/**
* Tiny snprintf/vsnprintf implementation
* \param buffer A pointer to the buffer where to store the formatted string
* \param count The maximum number of characters to store in the buffer, including a terminating null character
* \param format A string that specifies the format of the output
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
* If the formatted string is truncated the buffer size (count) is returned
*/
inline int snprintf(char* buffer, size_t count, const char* format, CPUState &cpu, MemState &mem, module::vargs &args)
{
const int ret = _vsnprintf(_out_buffer, buffer, count, format, cpu, mem, args);
return ret;
}
#endif
} // namespace Emulator::HLE::Libraries::LibC } // namespace Emulator::HLE::Libraries::LibC