From e6d0c9c9b0e8329dfa6aeef9ffb0dacc06e4f4a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Tue, 6 Oct 2026 07:48:23 +0200 Subject: [PATCH] feat(libSceHttp): implement sceHttpUriSweepPath Co-Authored-By: Claude --- core/libs/prx/libSceHttp/src/Uri.cpp | 42 ++++++++++++++++++++++++++++ core/libs/tests/GuestHttp.cpp | 23 +++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/core/libs/prx/libSceHttp/src/Uri.cpp b/core/libs/prx/libSceHttp/src/Uri.cpp index 4dc4dd3b3..b9a300ea4 100644 --- a/core/libs/prx/libSceHttp/src/Uri.cpp +++ b/core/libs/prx/libSceHttp/src/Uri.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -254,4 +255,45 @@ int APS5_VABI sceHttpUriParse(SceHttpUriElement* out, const char* src_url, void* return 0; } +int APS5_VABI sceHttpUriSweepPath(char* dst, const char* src, size_t srcSize) { + if (srcSize == 0) return 0; + if (!dst || !src) return ERROR_INVALID_VALUE; + + const size_t length = srcSize - 1; + if (src[0] != '/') { + std::memcpy(dst, src, length); + dst[length] = '\0'; + return 0; + } + + dst[0] = '/'; + dst[1] = '\0'; + size_t end = 0; + size_t pos = 1; + while (pos < length) { + if (src[pos] == '.' && src[pos + 1] == '/') { + pos += 2; + continue; + } + if (src[pos] == '.' && src[pos + 1] == '.' && src[pos + 2] == '/') { + if (end != 0) { + dst[end] = '\0'; + end = static_cast(std::strrchr(dst, '/') - dst); + dst[end + 1] = '\0'; + } + pos += 3; + continue; + } + size_t count = length - pos; + if (const char* slash = std::strchr(src + pos, '/')) { + count = std::min(static_cast(slash + 1 - (src + pos)), count); + } + std::memcpy(dst + end + 1, src + pos, count); + dst[end + 1 + count] = '\0'; + end += count; + pos += count; + } + return 0; +} + } diff --git a/core/libs/tests/GuestHttp.cpp b/core/libs/tests/GuestHttp.cpp index ae625c507..75d86a904 100644 --- a/core/libs/tests/GuestHttp.cpp +++ b/core/libs/tests/GuestHttp.cpp @@ -11,6 +11,7 @@ int APS5_VABI sceHttpSetInflateGZIPEnabled(int, int); int APS5_VABI sceHttpUriBuild(char*, std::size_t*, std::size_t, const SceHttpUriElement*, std::uint32_t); int APS5_VABI sceHttpUriEscape(char*, std::size_t*, std::size_t, const char*); int APS5_VABI sceHttpUriUnescape(char*, std::size_t*, std::size_t, const char*); +int APS5_VABI sceHttpUriSweepPath(char*, const char*, std::size_t); int APS5_VABI sceHttpCreateEpoll(int, HttpEpollHandle*); int APS5_VABI sceHttpDestroyEpoll(int, HttpEpollHandle); int APS5_VABI sceHttpReadData(int, void*, std::size_t); @@ -194,4 +195,26 @@ int main() { Require(parse(response, 15) == parseInvalidResponse); Require(parse(response, 16) == parseInvalidResponse); Require(phrase == nullptr && phraseLength == 0); + auto sweep = [](const char* src, const char* expected) { + char swept[128]; + std::memset(swept, 'x', sizeof(swept)); + return sceHttpUriSweepPath(swept, src, std::strlen(src) + 1) == 0 && Equal(swept, expected); + }; + Require(sceHttpUriSweepPath(nullptr, nullptr, 0) == 0); + Require(sceHttpUriSweepPath(nullptr, "/foo", 5) == invalidValue); + char sweptPath[16]; + Require(sceHttpUriSweepPath(sweptPath, nullptr, 5) == invalidValue); + Require(sweep("foo/../bar", "foo/../bar")); + Require(sweep("/foo/../bar", "/bar")); + Require(sweep("/foo/./bar", "/foo/bar")); + Require(sweep("/foo/.", "/foo/.")); + Require(sweep("/foo/..", "/foo/..")); + Require(sweep("/foo/bar/../foo/././../../../test/index.html", "/test/index.html")); + Require(sweep("/", "/")); + Require(sweep("", "")); + Require(sweep("/a/b/c/../../d/", "/a/d/")); + Require(sweep("/../a", "/a")); + Require(sweep("/a/..b/.c", "/a/..b/.c")); + Require(sceHttpUriSweepPath(sweptPath, "/a/b/../c", 6) == 0 && Equal(sweptPath, "/a/b/")); + Require(sceHttpUriSweepPath(sweptPath, "/ab/c", 3) == 0 && Equal(sweptPath, "/a")); }