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,328 +389,308 @@ 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)
{
// format specifier? %[flags][width][.precision][length]
if (*format != '%') {
// no
out(*format, buffer, idx++, maxlen);
format++;
continue;
}
else {
// yes, evaluate it
format++;
} }
// evaluate flags while (*format) {
flags = 0U; // format specifier? %[flags][width][.precision][length]
do { if (*format != '%') {
switch (*format) { // no
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; out(*format, buffer, idx++, maxlen);
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; format++;
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; continue;
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; } else {
case '#': flags |= FLAGS_HASH; format++; n = 1U; break; // yes, evaluate it
default : n = 0U; break; format++;
}
} while (n);
// evaluate width field
width = 0U;
if (_is_digit(*format)) {
width = _atoi(&format);
}
else if (*format == '*') {
const int w = vaArgInteger(va_list); // const int w = va.next<int>(cpu, mem);
if (w < 0) {
flags |= FLAGS_LEFT; // reverse padding
width = (unsigned int)-w;
}
else {
width = (unsigned int)w;
}
format++;
}
// evaluate precision field
precision = 0U;
if (*format == '.') {
flags |= FLAGS_PRECISION;
format++;
if (_is_digit(*format)) {
precision = _atoi(&format);
}
else if (*format == '*') {
precision = vaArgInteger(va_list); // precision = (unsigned int)va.next<int>(cpu, mem);
format++;
}
}
// evaluate length field
switch (*format) {
case 'l' :
flags |= FLAGS_LONG;
format++;
if (*format == 'l') {
flags |= FLAGS_LONG_LONG;
format++;
} }
break;
case 'h' : // evaluate flags
flags |= FLAGS_SHORT; flags = 0U;
format++; do {
if (*format == 'h') { switch (*format) {
flags |= FLAGS_CHAR; case '0':
format++; flags |= FLAGS_ZEROPAD;
format++;
n = 1U;
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;
}
} while (n);
// evaluate width field
width = 0U;
if (_is_digit(*format)) {
width = _atoi(&format);
} else if (*format == '*') {
const int w = vaArgInteger(va_list); // const int w = va.next<int>(cpu, mem);
if (w < 0) {
flags |= FLAGS_LEFT; // reverse padding
width = (unsigned int)-w;
} else {
width = (unsigned int)w;
}
format++;
} }
break;
// evaluate precision field
precision = 0U;
if (*format == '.') {
flags |= FLAGS_PRECISION;
format++;
if (_is_digit(*format)) {
precision = _atoi(&format);
} else if (*format == '*') {
precision = vaArgInteger(va_list); // precision = (unsigned int)va.next<int>(cpu, mem);
format++;
}
}
// evaluate length field
switch (*format) {
case 'l':
flags |= FLAGS_LONG;
format++;
if (*format == 'l') {
flags |= FLAGS_LONG_LONG;
format++;
}
break;
case 'h':
flags |= FLAGS_SHORT;
format++;
if (*format == 'h') {
flags |= FLAGS_CHAR;
format++;
}
break;
#if defined(PRINTF_SUPPORT_PTRDIFF_T) #if defined(PRINTF_SUPPORT_PTRDIFF_T)
case 't' : case 't':
flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++; format++;
break; break;
#endif #endif
case 'j' : case 'j':
flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++; format++;
break; break;
case 'z' : case 'z':
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
switch (*format) {
case 'd' :
case 'i' :
case 'u' :
case 'x' :
case 'X' :
case 'o' :
case 'b' : {
// set the base
unsigned int base;
if (*format == 'x' || *format == 'X') {
base = 16U;
}
else if (*format == 'o') {
base = 8U;
}
else if (*format == 'b') {
base = 2U;
flags &= ~FLAGS_HASH; // no hash for bin format
}
else {
base = 10U;
flags &= ~FLAGS_HASH; // no hash for dec format
}
// uppercase
if (*format == 'X') {
flags |= FLAGS_UPPERCASE;
} }
// no plus or space flag for u, x, X, o, b // evaluate specifier
if ((*format != 'i') && (*format != 'd')) { switch (*format) {
flags &= ~(FLAGS_PLUS | FLAGS_SPACE); case 'd':
} case 'i':
case 'u':
case 'x':
case 'X':
case 'o':
case 'b': {
// set the base
unsigned int base;
if (*format == 'x' || *format == 'X') {
base = 16U;
} else if (*format == 'o') {
base = 8U;
} else if (*format == 'b') {
base = 2U;
flags &= ~FLAGS_HASH; // no hash for bin format
} else {
base = 10U;
flags &= ~FLAGS_HASH; // no hash for dec format
}
// uppercase
if (*format == 'X') {
flags |= FLAGS_UPPERCASE;
}
// convert the integer // no plus or space flag for u, x, X, o, b
if ((*format == 'i') || (*format == 'd')) { if ((*format != 'i') && (*format != 'd')) {
// signed flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
if (flags & FLAGS_LONG_LONG) { }
// convert the integer
if ((*format == 'i') || (*format == 'd')) {
// signed
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,
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); flags);
} } else {
else { // const int value = (flags & FLAGS_CHAR) ? (char)va.next<int>(cpu, mem) : (flags & FLAGS_SHORT) ? (short
//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); // 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 {
} // const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va.next<unsigned int>(cpu, mem) : (flags & FLAGS_SHORT) ?
else { // (unsigned short int)va.next<unsigned int>(cpu, mem) : va.next<unsigned int>(cpu, mem);
//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) ? static_cast<u08>(vaArgInteger(va_list))
// (unsigned short int)va.next<unsigned int>(cpu, mem) : va.next<unsigned int>(cpu, mem); : (flags & FLAGS_SHORT) ? static_cast<u16>(vaArgInteger(va_list))
const unsigned int value = (flags & FLAGS_CHAR) ? static_cast<u08>(vaArgInteger(va_list)) : static_cast<u32>(vaArgInteger(va_list));
: (flags & FLAGS_SHORT) ? static_cast<u16>(vaArgInteger(va_list)) idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
: static_cast<u32>(vaArgInteger(va_list)); }
idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags); }
} format++;
} break;
format++; }
break;
}
#if defined(PRINTF_SUPPORT_FLOAT) #if defined(PRINTF_SUPPORT_FLOAT)
case 'f' : case 'f':
case 'F' : case 'F':
//idx = _ftoa(out, buffer, idx, maxlen, va.next<double>(cpu, mem), precision, width, flags); // idx = _ftoa(out, buffer, idx, maxlen, va.next<double>(cpu, mem), precision, width, flags);
idx = _ftoa(out, buffer, idx, maxlen, vaArgDouble(va_list), precision, width, flags); idx = _ftoa(out, buffer, idx, maxlen, vaArgDouble(va_list), precision, width, flags);
format++; format++;
break; break;
#endif // PRINTF_SUPPORT_FLOAT #endif // PRINTF_SUPPORT_FLOAT
case 'c' : { case 'c': {
unsigned int l = 1U; unsigned int l = 1U;
// pre padding // pre padding
if (!(flags & FLAGS_LEFT)) { if (!(flags & FLAGS_LEFT)) {
while (l++ < width) { while (l++ < width) {
out(' ', buffer, idx++, maxlen); out(' ', buffer, idx++, maxlen);
} }
} }
// char output // char output
//out((char)va.next<int>(cpu, mem), buffer, idx++, maxlen); // out((char)va.next<int>(cpu, mem), buffer, idx++, maxlen);
out(static_cast<char>(vaArgInteger(va_list)), buffer, idx++, maxlen); out(static_cast<char>(vaArgInteger(va_list)), buffer, idx++, maxlen);
// post padding // post padding
if (flags & FLAGS_LEFT) { if (flags & FLAGS_LEFT) {
while (l++ < width) { while (l++ < width) {
out(' ', buffer, idx++, maxlen); out(' ', buffer, idx++, maxlen);
} }
} }
format++; format++;
break; break;
} }
case 's' : { case 's': {
const char* p = vaArgPtr<const char>(va_list); // const char *p = va.next<Ptr<char>>(cpu, mem).get(mem); const char* p = vaArgPtr<const char>(va_list); // const char *p = va.next<Ptr<char>>(cpu, mem).get(mem);
p = p != nullptr ? p : "(null)"; p = p != nullptr ? p : "(null)";
unsigned int l = _strlen(p); unsigned int l = _strlen(p);
// pre padding // pre padding
if (flags & FLAGS_PRECISION) { if (flags & FLAGS_PRECISION) {
l = (l < precision ? l : precision); l = (l < precision ? l : precision);
} }
if (!(flags & FLAGS_LEFT)) { if (!(flags & FLAGS_LEFT)) {
while (l++ < width) { while (l++ < width) {
out(' ', buffer, idx++, maxlen); out(' ', buffer, idx++, maxlen);
} }
} }
// string output // string output
while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
out(*(p++), buffer, idx++, maxlen); out(*(p++), buffer, idx++, maxlen);
} }
// post padding // post padding
if (flags & FLAGS_LEFT) { if (flags & FLAGS_LEFT) {
while (l++ < width) { while (l++ < width) {
out(' ', buffer, idx++, maxlen); out(' ', buffer, idx++, maxlen);
} }
} }
format++; format++;
break; break;
} }
case 'p' : { case 'p': {
width = sizeof(void*) * 2U; width = sizeof(void*) * 2U;
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
#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
format++; format++;
break; break;
} }
case '%' : case '%':
out('%', buffer, idx++, maxlen); out('%', buffer, idx++, maxlen);
format++; format++;
break; break;
default : default:
out(*format, buffer, idx++, maxlen); out(*format, buffer, idx++, maxlen);
format++; format++;
break; break;
}
} }
}
// termination // termination
out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen); out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
// return written chars without terminating \0 // return written chars without terminating \0
return (int)idx; return (int)idx;
} }
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