mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #620 from DotDebian/feat/libc-snwprintf-s
feat(libc): implement snwprintf_s
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
@@ -88,7 +89,7 @@ void AppendPadded(std::u16string& out, const std::u16string& text, bool left, in
|
||||
if (left) out.append(pad, u' ');
|
||||
}
|
||||
|
||||
std::u16string FormatWide(const char16_t* format, VaList* source) {
|
||||
std::u16string FormatWide(const char16_t* format, VaList* source, bool rejectNullStrings = false) {
|
||||
if (format == nullptr || source == nullptr) throw std::invalid_argument("Null formatting argument");
|
||||
LibcDetail::FormatArguments args(source);
|
||||
std::u16string out;
|
||||
@@ -171,10 +172,12 @@ std::u16string FormatWide(const char16_t* format, VaList* source) {
|
||||
const std::size_t limit = precision < 0 ? SIZE_MAX : static_cast<std::size_t>(precision);
|
||||
if (conversion == u'S' || length == "l") {
|
||||
const char16_t* value = args.Next<const char16_t*>();
|
||||
if (value == nullptr && rejectNullStrings) throw std::invalid_argument("Null string argument");
|
||||
if (value == nullptr) value = u"(null)";
|
||||
for (std::size_t index = 0; index < limit && value[index] != 0; ++index) text.push_back(value[index]);
|
||||
} else {
|
||||
const char* value = args.Next<const char*>();
|
||||
if (value == nullptr && rejectNullStrings) throw std::invalid_argument("Null string argument");
|
||||
AppendUtf16(text, value != nullptr ? value : "(null)", limit);
|
||||
}
|
||||
AppendPadded(out, text, left, width);
|
||||
@@ -204,6 +207,27 @@ int APS5_VABI vswprintf_nid_postfix(char16_t* buffer, std::size_t size, const ch
|
||||
}
|
||||
}
|
||||
|
||||
int APS5_VABI snwprintf_s_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, ...) {
|
||||
constexpr std::size_t RsizeMax = SIZE_MAX >> 1;
|
||||
int result = -1;
|
||||
if (buffer != nullptr && format != nullptr && size != 0 && size <= RsizeMax) {
|
||||
APS5_VA_BEGIN(format);
|
||||
try {
|
||||
const std::u16string text = FormatWide(format, reinterpret_cast<VaList*>(args), true);
|
||||
if (text.size() <= static_cast<std::size_t>(INT_MAX)) {
|
||||
const std::size_t copied = text.size() < size - 1 ? text.size() : size - 1;
|
||||
std::memcpy(buffer, text.data(), copied * sizeof(char16_t));
|
||||
buffer[copied] = 0;
|
||||
result = static_cast<int>(text.size());
|
||||
}
|
||||
} catch (const std::invalid_argument&) {
|
||||
}
|
||||
APS5_VA_END();
|
||||
}
|
||||
if (result < 0 && buffer != nullptr && size != 0 && size < RsizeMax) buffer[0] = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
int APS5_VABI wprintf_nid_postfix(const char16_t* format, ...) {
|
||||
APS5_VA_BEGIN(format);
|
||||
int result = -1;
|
||||
|
||||
@@ -138,9 +138,4 @@ void APS5_VABI _Unlocksyslock_nid_postfix() {
|
||||
g_sysLock.unlock();
|
||||
}
|
||||
|
||||
int APS5_VABI snwprintf_s() {
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
extern "C" int APS5_VABI vswprintf_nid_postfix(char16_t*, std::size_t, const char16_t*, VaList*);
|
||||
extern "C" int APS5_VABI snwprintf_s_nid_postfix(char16_t*, std::size_t, const char16_t*, ...);
|
||||
|
||||
static int APS5_VABI Format(char16_t* buffer, std::size_t size, const char16_t* format, ...) {
|
||||
#ifdef _WIN32
|
||||
@@ -34,7 +36,60 @@ static void Check(const char16_t* format, const char* input, const char16_t* exp
|
||||
}
|
||||
}
|
||||
|
||||
static void Require(bool condition, const char* message) {
|
||||
if (!condition) {
|
||||
std::fprintf(stderr, "snwprintf_s: %s\n", message);
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
static void CheckBounded() {
|
||||
constexpr std::size_t rsizeMax = SIZE_MAX >> 1;
|
||||
const char16_t* const nullWide = nullptr;
|
||||
const char* const nullNarrow = nullptr;
|
||||
char16_t buffer[16];
|
||||
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 16, u"%d-%ls-%s", 42, u"ab", "\xc3\xa9") == 7, "complete output length");
|
||||
Require(buffer == std::u16string(u"42-ab-\u00e9"), "complete output");
|
||||
|
||||
buffer[5] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 5, u"%d-%ls", 42, u"abcd") == 7, "truncated output length");
|
||||
Require(buffer == std::u16string(u"42-a") && buffer[5] == u'x', "truncated output");
|
||||
|
||||
buffer[0] = u'x';
|
||||
buffer[1] = u'y';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 1, u"%x", 0xabcu) == 3, "one unit buffer length");
|
||||
Require(buffer[0] == 0 && buffer[1] == u'y', "one unit buffer");
|
||||
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 16, u"") == 0 && buffer[0] == 0, "empty format");
|
||||
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 16, nullptr) < 0 && buffer[0] == 0, "null format");
|
||||
Require(snwprintf_s_nid_postfix(nullptr, 16, u"a") < 0, "null buffer");
|
||||
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 0, u"a") < 0 && buffer[0] == u'x', "zero size");
|
||||
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, rsizeMax + 1, u"a") < 0 && buffer[0] == u'x', "size above RSIZE_MAX");
|
||||
|
||||
int written = -7;
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 16, u"ab%n", &written) < 0 && buffer[0] == 0 && written == -7, "%n");
|
||||
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 16, u"a%ls", nullWide) < 0 && buffer[0] == 0, "null %ls argument");
|
||||
|
||||
buffer[0] = u'x';
|
||||
Require(snwprintf_s_nid_postfix(buffer, 16, u"a%s", nullNarrow) < 0 && buffer[0] == 0, "null %s argument");
|
||||
|
||||
Require(Format(buffer, 16, u"%s", nullNarrow) == 6 && buffer == std::u16string(u"(null)"), "vswprintf null %s argument");
|
||||
}
|
||||
|
||||
int main() {
|
||||
CheckBounded();
|
||||
Check(u"%.2s", "\xc3\xa9\xc3\xa8", u"\u00e9\u00e8");
|
||||
Check(u"%.1s", "\xc3\xa9\xc3\xa8", u"\u00e9");
|
||||
Check(u"%.0s", "\xc3\xa9", u"");
|
||||
|
||||
@@ -70,7 +70,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- [sceSystemServiceDisableMediaPlay](../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - unknown signature
|
||||
- [sceSystemServiceReenableMediaPlay](../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - unknown signature
|
||||
- [sceVoiceSetMuteFlag](../../core/libs/prx/libSceVoice/Export.cpp) (libSceVoice) - unknown signature
|
||||
- [snwprintf_s](../../core/libs/prx/libc/src/RuntimeSupport.cpp) (libc) - unknown signature
|
||||
- [snwprintf_s](../../core/libs/prx/libc/src/FormattingWide.cpp) (libc) - follows C11 K.3.9.1.3 with `RSIZE_MAX` assumed to be `SIZE_MAX >> 1`. A runtime-constraint violation only returns a negative value: `set_constraint_handler_s` is not exported and the console's default handler is unknown. An invalid multibyte `%s` argument is copied byte by byte instead of being reported as an encoding error, as in `vswprintf`
|
||||
- [libSceUlt queues](../../core/libs/prx/libSceUlt/Export.cpp) - the added `sceUltQueuePop`, `sceUltQueueTryPush`, `sceUltQueueDestroy` and `sceUltQueueDataResourcePoolDestroy` signatures are inferred from the existing push/pop and object APIs. Queue destruction is assumed to reject blocked callers with `ULT_ERROR_BUSY` and otherwise discard queued data; pool exhaustion returns `ULT_ERROR_AGAIN`. These rules, alignment checks and shared slot limits have not been confirmed on PS5 hardware or a title. Non-null queue and data-pool option parameters throw because their layouts are unknown.
|
||||
|
||||
### Functional
|
||||
|
||||
Reference in New Issue
Block a user