feat(libSceHttp): implement sceHttpUriSweepPath

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Adrià Franch
2026-10-06 07:48:23 +02:00
parent 5895ea721a
commit e6d0c9c9b0
2 changed files with 65 additions and 0 deletions
+42
View File
@@ -1,3 +1,4 @@
#include <algorithm>
#include <cctype>
#include <charconv>
#include <cstdint>
@@ -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<size_t>(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<size_t>(slash + 1 - (src + pos)), count);
}
std::memcpy(dst + end + 1, src + pos, count);
dst[end + 1 + count] = '\0';
end += count;
pos += count;
}
return 0;
}
}
+23
View File
@@ -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"));
}