From 6d61d0a7b2528d63b5c08ede71be4166d8fe550c Mon Sep 17 00:00:00 2001 From: gibbed Date: Tue, 2 Jun 2015 10:41:55 -0500 Subject: [PATCH] Added _snprintf and swprintf. --- src/xenia/kernel/xboxkrnl_strings.cc | 75 ++++++++++++++++++++++++++-- src/xenia/kernel/xboxkrnl_table.inc | 2 +- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/xenia/kernel/xboxkrnl_strings.cc b/src/xenia/kernel/xboxkrnl_strings.cc index 585073686..bc19568ca 100644 --- a/src/xenia/kernel/xboxkrnl_strings.cc +++ b/src/xenia/kernel/xboxkrnl_strings.cc @@ -648,12 +648,12 @@ int32_t format_core(PPCContext* ppc_context, FormatData& data, ArgList& args, class StackArgList : public ArgList { public: - StackArgList(PPCContext* ppc_context) : ppc_context(ppc_context), index_(2) {} + StackArgList(PPCContext* ppc_context, int32_t index) : ppc_context(ppc_context), index_(index) {} uint32_t get32() { return (uint32_t)get64(); } uint64_t get64() { - auto value = SHIM_GET_ARG_64(2 + index_); + auto value = SHIM_GET_ARG_64(index_); ++index_; return value; } @@ -795,7 +795,7 @@ SHIM_CALL DbgPrint_shim(PPCContext* ppc_context, KernelState* kernel_state) { } auto format = (const uint8_t*)SHIM_MEM_ADDR(format_ptr); - StackArgList args(ppc_context); + StackArgList args(ppc_context, 1); StringFormatData data(format); int32_t count = format_core(ppc_context, data, args, false); @@ -809,6 +809,43 @@ SHIM_CALL DbgPrint_shim(PPCContext* ppc_context, KernelState* kernel_state) { SHIM_SET_RETURN_32(X_STATUS_SUCCESS); } +// https://msdn.microsoft.com/en-us/library/2ts7cx93.aspx +SHIM_CALL _snprintf_shim(PPCContext* ppc_context, KernelState* kernel_state) { + uint32_t buffer_ptr = SHIM_GET_ARG_32(0); + int32_t buffer_count = SHIM_GET_ARG_32(1); + uint32_t format_ptr = SHIM_GET_ARG_32(2); + + XELOGD("_snprintf(%08X, %i, %08X, ...)", buffer_ptr, buffer_count, + format_ptr); + + if (buffer_ptr == 0 || buffer_count <= 0 || format_ptr == 0) { + SHIM_SET_RETURN_32(-1); + return; + } + + auto buffer = (uint8_t*)SHIM_MEM_ADDR(buffer_ptr); + auto format = (const uint8_t*)SHIM_MEM_ADDR(format_ptr); + + StackArgList args(ppc_context, 3); + StringFormatData data(format); + + int32_t count = format_core(ppc_context, data, args, false); + if (count < 0) { + if (buffer_count > 0) { + buffer[0] = '\0'; // write a null, just to be safe + } + } else if (count <= buffer_count) { + std::memcpy(buffer, data.str().c_str(), count); + if (count < buffer_count) { + buffer[count] = '\0'; + } + } else { + std::memcpy(buffer, data.str().c_str(), buffer_count); + count = -1; // for return value + } + SHIM_SET_RETURN_32(count); +} + // https://msdn.microsoft.com/en-us/library/ybk95axf.aspx SHIM_CALL sprintf_shim(PPCContext* ppc_context, KernelState* kernel_state) { uint32_t buffer_ptr = SHIM_GET_ARG_32(0); @@ -824,7 +861,7 @@ SHIM_CALL sprintf_shim(PPCContext* ppc_context, KernelState* kernel_state) { auto buffer = (uint8_t*)SHIM_MEM_ADDR(buffer_ptr); auto format = (const uint8_t*)SHIM_MEM_ADDR(format_ptr); - StackArgList args(ppc_context); + StackArgList args(ppc_context, 2); StringFormatData data(format); int32_t count = format_core(ppc_context, data, args, false); @@ -837,6 +874,34 @@ SHIM_CALL sprintf_shim(PPCContext* ppc_context, KernelState* kernel_state) { SHIM_SET_RETURN_32(count); } +// https://msdn.microsoft.com/en-us/library/ybk95axf.aspx +SHIM_CALL swprintf_shim(PPCContext* ppc_context, KernelState* kernel_state) { + uint32_t buffer_ptr = SHIM_GET_ARG_32(0); + uint32_t format_ptr = SHIM_GET_ARG_32(1); + + XELOGD("swprintf(%08X, %08X, ...)", buffer_ptr, format_ptr); + + if (buffer_ptr == 0 || format_ptr == 0) { + SHIM_SET_RETURN_32(-1); + return; + } + + auto buffer = (uint16_t*)SHIM_MEM_ADDR(buffer_ptr); + auto format = (const uint16_t*)SHIM_MEM_ADDR(format_ptr); + + StackArgList args(ppc_context, 2); + WideStringFormatData data(format); + + int32_t count = format_core(ppc_context, data, args, false); + if (count <= 0) { + buffer[0] = '\0'; + } else { + xe::copy_and_swap(buffer, (uint16_t*)data.wstr().c_str(), count); + buffer[count] = '\0'; + } + SHIM_SET_RETURN_32(count); +} + // https://msdn.microsoft.com/en-us/library/1kt27hek.aspx SHIM_CALL _vsnprintf_shim(PPCContext* ppc_context, KernelState* kernel_state) { uint32_t buffer_ptr = SHIM_GET_ARG_32(0); @@ -961,7 +1026,9 @@ SHIM_CALL vswprintf_shim(PPCContext* ppc_context, KernelState* kernel_state) { void xe::kernel::xboxkrnl::RegisterStringExports( xe::cpu::ExportResolver* export_resolver, KernelState* state) { SHIM_SET_MAPPING("xboxkrnl.exe", DbgPrint, state); + SHIM_SET_MAPPING("xboxkrnl.exe", _snprintf, state); SHIM_SET_MAPPING("xboxkrnl.exe", sprintf, state); + SHIM_SET_MAPPING("xboxkrnl.exe", swprintf, state); SHIM_SET_MAPPING("xboxkrnl.exe", _vsnprintf, state); SHIM_SET_MAPPING("xboxkrnl.exe", vsprintf, state); SHIM_SET_MAPPING("xboxkrnl.exe", _vscwprintf, state); diff --git a/src/xenia/kernel/xboxkrnl_table.inc b/src/xenia/kernel/xboxkrnl_table.inc index b9b848568..903a14301 100644 --- a/src/xenia/kernel/xboxkrnl_table.inc +++ b/src/xenia/kernel/xboxkrnl_table.inc @@ -328,7 +328,7 @@ XE_EXPORT(xboxkrnl, 0x0000013A, _snprintf, XE_EXPORT(xboxkrnl, 0x0000013B, sprintf, kFunction), XE_EXPORT(xboxkrnl, 0x0000013C, _scwprintf, kFunction), XE_EXPORT(xboxkrnl, 0x0000013D, _snwprintf, kFunction), -XE_EXPORT(xboxkrnl, 0x0000013E, _swprintf, kFunction), +XE_EXPORT(xboxkrnl, 0x0000013E, swprintf, kFunction), XE_EXPORT(xboxkrnl, 0x0000013F, RtlTimeFieldsToTime, kFunction), XE_EXPORT(xboxkrnl, 0x00000140, RtlTimeToTimeFields, kFunction), XE_EXPORT(xboxkrnl, 0x00000141, RtlTryEnterCriticalSection, kFunction),