Skip to content

Commit 4d3e4bb

Browse files
committed
mostly right
revamped log styles lol
1 parent 04dd940 commit 4d3e4bb

File tree

7 files changed

+115
-40
lines changed

7 files changed

+115
-40
lines changed

tests/code/filesystem_dirents/code/calculator.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "calculator.h"
22

3+
#include "log.h"
4+
35
/**
46
* Do not update those routines!
57
* Those should simulate how PS4 handles maths
@@ -52,7 +54,8 @@ s64 nearest_dirent(const char* buffer, s64 size, s64 offset) {
5254
for (s64 out_offset = offset_adj; out_offset <= offset_adj + max_advance; out_offset += 8) {
5355
const OrbisInternals::FolderDirent* tested_dirent = reinterpret_cast<const OrbisInternals::FolderDirent*>(buffer + out_offset);
5456
status = validate_pfs_getdirentries_dirent(tested_dirent);
55-
// LogError("Testing", out_offset, status);
57+
LogWarning(GetSt(Style::BOLD), "Testing", out_offset - offset, GetSt(Style::RESET), "=", GetSt(Style::FG_MAGENTA), status);
58+
5659
if (status < 0) continue;
5760

5861
// LogError("Found a match forward at", out_offset);
@@ -81,19 +84,31 @@ void calculate_pfs_getdirentries(OrbisInternals::DirentCombinationGetdirentries*
8184
s64 apparent_end = offset + count;
8285
s64 apparent_end_down = ALDN(apparent_end, 512);
8386
s64 file_offset_down = ALDN(offset, 512);
87+
s64 file_offset_up = ALDN(offset, 512);
8488
s64 directory_size = ALUP(size, 0x10000);
8589

8690
// within the same sector or worse
8791
if (apparent_end_down <= file_offset_down) {
88-
spec->expected_result = s64(einval_int);
89-
spec->expected_errno = EINVAL;
92+
spec->expected_basep = 0;
93+
spec->expected_result = s64(einval_int);
94+
spec->expected_end_position = offset;
95+
spec->expected_errno = EINVAL;
96+
return;
97+
}
98+
99+
if ((apparent_end_down == file_offset_up) && (count < 512)) {
100+
spec->expected_basep = offset;
101+
spec->expected_result = 0;
102+
spec->expected_end_position = offset;
103+
spec->expected_errno = 0;
104+
// LogError("qweqweqwe");
90105
return;
91106
}
92107

93108
if (offset >= size) {
94-
spec->expected_end_position = directory_size;
95109
spec->expected_basep = offset;
96110
spec->expected_result = 0;
111+
spec->expected_end_position = directory_size;
97112
// LogError("qweqweqwe");
98113
return;
99114
}
@@ -119,18 +134,23 @@ void calculate_pfs_getdirentries(OrbisInternals::DirentCombinationGetdirentries*
119134
s64 bytes_written = 0;
120135
s64 buffer_position = offset;
121136

122-
while (buffer_position < apparent_end) {
123-
const OrbisInternals::FolderDirent* pfs_dirent = reinterpret_cast<const OrbisInternals::FolderDirent*>(buffer + buffer_position + dirent_offset);
137+
while (bytes_written < count) {
138+
if (ALDN(buffer_position + dirent_offset, 8) >= apparent_end_down) {
139+
LogError("QQWWERRR");
140+
break;
141+
}
124142

125-
if (pfs_dirent->d_reclen == 0) break;
126-
if ((bytes_written + dirent_offset + pfs_dirent->d_reclen) > count) break;
127-
// without dirent offset i think
128-
// it sometimes underreads data at this line
129-
//
130-
if ((buffer_position + dirent_offset + pfs_dirent->d_reclen) >= apparent_end_down) {
131-
LogError("XVFED");
143+
const OrbisInternals::FolderDirent* pfs_dirent = reinterpret_cast<const OrbisInternals::FolderDirent*>(buffer + buffer_position + dirent_offset);
144+
if ((bytes_written + dirent_offset + pfs_dirent->d_reclen) >= count) {
145+
LogError("AAAQWEEEEE");
132146
break;
133147
}
148+
if (pfs_dirent->d_reclen == 0) break;
149+
150+
// if ((buffer_position + dirent_offset + pfs_dirent->d_reclen) >= apparent_end_down) {
151+
// LogError("XVFED");
152+
// break;
153+
// }
134154

135155
// align to 65536 when dir size is bigger?
136156
// if (Common::AlignUp(buffer_position, count) !=

tests/code/filesystem_dirents/code/fs_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ TEST(DirentTests, PFSGetdirentries) {
111111
fd = sceKernelOpen(input_pfs, O_DIRECTORY, 0777);
112112
add_fd(fd);
113113
s64 basep {};
114-
114+
115115
for (const auto& spec: pfs_dirent_variants) {
116116
spec_size = spec.first;
117117
spec_offset = spec.second;

tests/code/filesystem_dirents/code/fs_test_tools.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,48 @@ s64 validate_normal_dirent(const oi::FolderDirent* dirent) {
7373
}
7474

7575
/**
76-
* I suspect that pfs dirents are u32 but need to be mask
77-
* maybe Orbis uses normal types, but on disk each one is u32 instead of u32,u16,u8,u8
78-
* so we can clear lower bytes and check for anything above!
76+
* reclen is basically a checksum
77+
* i've never seen any other than -11, but i've attached others for verbosity
7978
*/
8079

8180
// pfs getdirentries returns normal direntries
8281
s64 validate_pfs_read_dirent(const oi::PfsDirent* dirent) {
83-
if ((dirent->d_reclen & 0x07) != 0) return -11;
84-
if (ALUP(16 + dirent->d_namlen + 1, 8) != dirent->d_reclen) return -12;
85-
if (dirent->d_reclen < 24 || dirent->d_reclen > 272) return -13;
86-
if (dirent->d_type > 15) return -14;
87-
if (dirent->d_namlen == 0) return -15;
88-
if ((strnlen(dirent->d_name, 255) + 1) != dirent->d_namlen) return -16;
82+
if (dirent->d_fileno == 0) return -10;
83+
if (ALUP(16 + dirent->d_namlen + 1, 8) != dirent->d_reclen) return -11;
84+
// these don't fail so often
85+
if (dirent->d_namlen == 0) return -12;
86+
if (dirent->d_type == 0) return -13;
87+
if (dirent->d_reclen == 0) return -14;
88+
if ((dirent->d_reclen & 0x07) != 0) return -15;
89+
if (dirent->d_reclen < 24 || dirent->d_reclen > 272) return -16;
90+
if (dirent->d_type > 15) return -17;
91+
if (strnlen(dirent->d_name, 255) != dirent->d_namlen) return -18;
8992
return 1;
9093
}
9194

9295
// pfs getdirentries returns normal direntries
9396
s64 validate_pfs_getdirentries_dirent(const oi::FolderDirent* dirent) {
94-
if ((dirent->d_reclen & 0x07) != 0) return -11;
95-
if (ALUP(16 + dirent->d_namlen + 1, 8) != dirent->d_reclen) return -12;
96-
if (dirent->d_reclen < 24 || dirent->d_reclen > 272) return -13;
97-
if (dirent->d_type > 15) return -14;
98-
if (dirent->d_namlen == 0) return -15;
99-
if (strnlen(dirent->d_name, 255) != dirent->d_namlen) return -16;
97+
if (dirent->d_fileno == 0) return -10;
98+
if (ALUP(16 + dirent->d_namlen + 1, 8) != dirent->d_reclen) return -11;
99+
// these don't fail so often
100+
if (dirent->d_namlen == 0) return -12;
101+
if (dirent->d_type == 0) return -13;
102+
if (dirent->d_reclen == 0) return -14;
103+
if ((dirent->d_reclen & 0x07) != 0) return -15;
104+
if (dirent->d_reclen < 24 || dirent->d_reclen > 272) return -16;
105+
if (dirent->d_type > 15) return -17;
106+
if (strnlen(dirent->d_name, 255) != dirent->d_namlen) return -18;
100107
return 1;
101108
}
102109

110+
/**
111+
* I suspect that pfs dirents are u32 but need to be mask
112+
* maybe Orbis uses normal types, but on disk each one is u32 instead of u32,u16,u8,u8
113+
* so we can clear lower bytes and check for anything above!
114+
*/
115+
103116
// pfs getdirentries returns normal direntries
104117
s64 validate_pfs_getdirentries_experimental(const oi::PfsDirent* dirent) {
105-
if (dirent->d_fileno == u32(-1)) return -11;
106118
if (dirent->d_type & ~s32(0xFF)) return -12;
107119
if (dirent->d_namlen & ~s32(0xFF)) return -13;
108120
if (dirent->d_reclen & ~s32(0xFFFF)) return -14;

tests/code/filesystem_dirents/code/log.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
#include <iomanip>
44
#include <sstream>
55

6+
namespace Style {
7+
static const unsigned style_size = __enum_end + 1;
8+
9+
static const char* styles[style_size] = {
10+
"\033[0m", "\033[1m", // res, bold
11+
"\033[22m", // notbold
12+
"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m", "\033[37m", // FG
13+
"", // end
14+
};
15+
16+
} // namespace Style
17+
18+
const char* GetSt(Style::Style style) {
19+
using namespace Style;
20+
if (style < style_size) return styles[style];
21+
return styles[style_size - 1];
22+
}
23+
624
std::ostream& center(std::ostream& os, const std::string& s, int width) {
725
int len = (int)s.size();
826
if (width <= len) return os << s.substr(0, width);

tests/code/filesystem_dirents/code/log.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99

1010
#define STR(x) std::to_string(x)
1111

12+
namespace Style {
13+
typedef enum Style: unsigned {
14+
RESET = 0,
15+
BOLD,
16+
NOTBOLD,
17+
FG_BLACK,
18+
FG_RED,
19+
FG_GREEN,
20+
FG_YELLOW,
21+
FG_BLUE,
22+
FG_MAGENTA,
23+
FG_CYAN,
24+
FG_WHITE,
25+
__enum_end,
26+
} Style;
27+
28+
} // namespace Style
29+
30+
const char* GetSt(Style::Style code);
31+
1232
std::ostream& center(std::ostream& os, const std::string& s, int width);
1333
std::string center(const std::string& s, int width);
1434
std::ostream& right(std::ostream& os, const std::string& s, int width);
@@ -31,9 +51,14 @@ std::string to_hex(T value) {
3151
std::string to_hex_string(const void* data, long long length, std::string sep = " ");
3252

3353
template <typename... Args>
34-
void LogCustom(const char* fn, const char* msg, Args&&... args) {
35-
std::cout << "[" << center(fn, 20) << "] " << msg;
54+
void LogCustom(const char* fn, bool show_line, const char* msg, Args&&... args) {
55+
std::cout << GetSt(Style::RESET) << "[" << center(fn, 20) << "] " << msg;
3656
((std::cout << " " << args), ...);
57+
std::cout << GetSt(Style::RESET);
58+
if (show_line) {
59+
std::cout << " ( " << __FILE__ << ":" << __LINE__ << " )";
60+
}
61+
3762
std::cout << std::endl;
3863
}
3964

@@ -44,27 +69,27 @@ void ResetErrorCounter(void);
4469

4570
#define Log(...) \
4671
{ \
47-
LogCustom(__FUNCTION__, "[INFO]", ##__VA_ARGS__); \
72+
LogCustom(__FUNCTION__, false, "[INFO]", ##__VA_ARGS__); \
4873
}
4974

5075
#define LogTest(...) \
5176
{ \
52-
LogCustom(__FUNCTION__, "\033[34;1m[TEST]\033[0m", ##__VA_ARGS__); \
77+
LogCustom(__FUNCTION__, false, GetSt(Style::BOLD), GetSt(Style::FG_BLUE), "[TEST]", GetSt(Style::RESET), ##__VA_ARGS__); \
5378
}
5479

5580
#define LogError(...) \
5681
{ \
57-
LogCustom(__FUNCTION__, "\033[31;1m[FAIL]\033[0m", ##__VA_ARGS__, "( " __FILE__ ":", __LINE__, ")"); \
82+
LogCustom(__FUNCTION__, true, GetSt(Style::BOLD), GetSt(Style::FG_RED), "[FAIL]", GetSt(Style::RESET), ##__VA_ARGS__); \
5883
}
5984

6085
#define LogWarning(...) \
6186
{ \
62-
LogCustom(__FUNCTION__, "\033[33;1m[WARN]\033[0m", ##__VA_ARGS__, "( " __FILE__ ":", __LINE__, ")"); \
87+
LogCustom(__FUNCTION__, true, GetSt(Style::BOLD), GetSt(Style::FG_YELLOW), "[WARN]", GetSt(Style::RESET), ##__VA_ARGS__); \
6388
}
6489

6590
#define LogSuccess(...) \
6691
{ \
67-
LogCustom(__FUNCTION__, "\033[32;1m[SUCC]\033[0m", ##__VA_ARGS__); \
92+
LogCustom(__FUNCTION__, false, GetSt(Style::BOLD), GetSt(Style::FG_GREEN), "[SUCC]", GetSt(Style::RESET), ##__VA_ARGS__); \
6893
}
6994

7095
#define TEST_CASE(cond, success_str, fail_str, ...) \

tests/code/filesystem_dirents/code/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ IMPORT_TEST_GROUP(DirentTests);
88

99
int main(int ac, char** av) {
1010
// No buffering
11-
// setvbuf(stdout, NULL, _IONBF, 0);
11+
setvbuf(stdout, NULL, _IONBF, 0);
1212

1313
// Log tests start
1414
Log();

tests/code/filesystem_dirents/code/read_variants/pfs_getdirentries.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ std::vector<std::pair<s64, s64>> pfs_dirent_variants = {
3737

3838
// apparent end = offset + length before checking dirents
3939

40-
{0, 0},
41-
{128, 128},
40+
{0, 0}, // 0 -2147352554 0 41414141414141414141414141414141
41+
{128, 128}, // 0 -2147352554 128 41414141414141414141414141414141
4242
{512, 0}, // 0 496 496 07000000180004012e00000000000000
4343
{512, -1}, // 0 496 496 07000000180004012e00000000000000
4444
{512, -1}, // 0 496 496 07000000180004012e00000000000000

0 commit comments

Comments
 (0)