summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorGravatar bunnei2014-11-12 22:56:27 -0500
committerGravatar bunnei2014-11-12 22:56:27 -0500
commit3cfdabb2e3594f8b380b53db2af7de41958408c6 (patch)
tree082f1d6a5530f35f5d702bf77704e13489f6f978 /src/core
parentMerge pull request #188 from bunnei/apt-fixes (diff)
parentUse std::u16string for conversion between UTF-8 and UTF-16, FS:USER functions (diff)
downloadyuzu-3cfdabb2e3594f8b380b53db2af7de41958408c6.tar.gz
yuzu-3cfdabb2e3594f8b380b53db2af7de41958408c6.tar.xz
yuzu-3cfdabb2e3594f8b380b53db2af7de41958408c6.zip
Merge pull request #183 from archshift/lowpath
Add support for UTF-16 strings for LowPaths in FS:USER
Diffstat (limited to 'src/core')
-rw-r--r--src/core/file_sys/archive.h99
-rw-r--r--src/core/hle/service/fs_user.cpp164
2 files changed, 180 insertions, 83 deletions
diff --git a/src/core/file_sys/archive.h b/src/core/file_sys/archive.h
index aeabf09ac..38145eed8 100644
--- a/src/core/file_sys/archive.h
+++ b/src/core/file_sys/archive.h
@@ -7,11 +7,13 @@
7#include <memory> 7#include <memory>
8 8
9#include "common/common_types.h" 9#include "common/common_types.h"
10#include "common/string_util.h"
10#include "common/bit_field.h" 11#include "common/bit_field.h"
11 12
12#include "core/file_sys/file.h" 13#include "core/file_sys/file.h"
13#include "core/file_sys/directory.h" 14#include "core/file_sys/directory.h"
14 15
16#include "core/mem_map.h"
15#include "core/hle/kernel/kernel.h" 17#include "core/hle/kernel/kernel.h"
16 18
17//////////////////////////////////////////////////////////////////////////////////////////////////// 19////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -19,6 +21,15 @@
19 21
20namespace FileSys { 22namespace FileSys {
21 23
24// Path string type
25enum LowPathType : u32 {
26 Invalid = 0,
27 Empty = 1,
28 Binary = 2,
29 Char = 3,
30 Wchar = 4
31};
32
22union Mode { 33union Mode {
23 u32 hex; 34 u32 hex;
24 BitField<0, 1, u32> read_flag; 35 BitField<0, 1, u32> read_flag;
@@ -26,6 +37,94 @@ union Mode {
26 BitField<2, 1, u32> create_flag; 37 BitField<2, 1, u32> create_flag;
27}; 38};
28 39
40class Path {
41public:
42
43 Path():
44 type(Invalid)
45 {
46 }
47
48 Path(LowPathType type, u32 size, u32 pointer):
49 type(type)
50 {
51 switch (type) {
52 case Binary:
53 {
54 u8* data = Memory::GetPointer(pointer);
55 binary = std::vector<u8>(data, data + size);
56 break;
57 }
58 case Char:
59 {
60 const char* data = reinterpret_cast<const char*>(Memory::GetPointer(pointer));
61 string = std::string(data, size - 1); // Data is always null-terminated.
62 break;
63 }
64 case Wchar:
65 {
66 const char16_t* data = reinterpret_cast<const char16_t*>(Memory::GetPointer(pointer));
67 u16str = std::u16string(data, size/2 - 1); // Data is always null-terminated.
68 break;
69 }
70 }
71 }
72
73 LowPathType GetType() const {
74 return type;
75 }
76
77 const std::string AsString() const {
78 switch (GetType()) {
79 case Char:
80 return string;
81 case Wchar:
82 return Common::UTF16ToUTF8(u16str);
83 case Empty:
84 return {};
85 default:
86 ERROR_LOG(KERNEL, "LowPathType cannot be converted to string!");
87 return {};
88 }
89 }
90
91 const std::u16string AsU16Str() const {
92 switch (GetType()) {
93 case Char:
94 return Common::UTF8ToUTF16(string);
95 case Wchar:
96 return u16str;
97 case Empty:
98 return {};
99 default:
100 ERROR_LOG(KERNEL, "LowPathType cannot be converted to u16string!");
101 return {};
102 }
103 }
104
105 const std::vector<u8> AsBinary() const {
106 switch (GetType()) {
107 case Binary:
108 return binary;
109 case Char:
110 return std::vector<u8>(string.begin(), string.end());
111 case Wchar:
112 return std::vector<u8>(u16str.begin(), u16str.end());
113 case Empty:
114 return {};
115 default:
116 ERROR_LOG(KERNEL, "LowPathType cannot be converted to binary!");
117 return {};
118 }
119 }
120
121private:
122 LowPathType type;
123 std::vector<u8> binary;
124 std::string string;
125 std::u16string u16str;
126};
127
29class Archive : NonCopyable { 128class Archive : NonCopyable {
30public: 129public:
31 /// Supported archive types 130 /// Supported archive types
diff --git a/src/core/hle/service/fs_user.cpp b/src/core/hle/service/fs_user.cpp
index 48d806e2f..9dc83291d 100644
--- a/src/core/hle/service/fs_user.cpp
+++ b/src/core/hle/service/fs_user.cpp
@@ -5,6 +5,7 @@
5#include "common/common.h" 5#include "common/common.h"
6 6
7#include "fs_user.h" 7#include "fs_user.h"
8#include "common/string_util.h"
8#include "core/settings.h" 9#include "core/settings.h"
9#include "core/hle/kernel/archive.h" 10#include "core/hle/kernel/archive.h"
10 11
@@ -13,20 +14,6 @@
13 14
14namespace FS_User { 15namespace FS_User {
15 16
16// Command to access archive file
17enum class LowPathType : u32 {
18 Invalid = 0,
19 Empty = 1,
20 Binary = 2,
21 Char = 3,
22 Wchar = 4
23};
24
25std::string GetStringFromCmdBuff(const u32 pointer, const u32 size) {
26 auto data = reinterpret_cast<const char*>(Memory::GetPointer(pointer));
27 return std::string(data, size - 1);
28}
29
30// We currently return 0 for success and -1 for failure in cmd_buff[1]. -1 was chosen because it 17// We currently return 0 for success and -1 for failure in cmd_buff[1]. -1 was chosen because it
31// puts all the sections of the http://3dbrew.org/wiki/Error_codes to something non-zero, to make 18// puts all the sections of the http://3dbrew.org/wiki/Error_codes to something non-zero, to make
32// sure we don't mislead the application into thinking something worked. 19// sure we don't mislead the application into thinking something worked.
@@ -44,32 +31,36 @@ void Initialize(Service::Interface* self) {
44void OpenFile(Service::Interface* self) { 31void OpenFile(Service::Interface* self) {
45 u32* cmd_buff = Service::GetCommandBuffer(); 32 u32* cmd_buff = Service::GetCommandBuffer();
46 33
47 u32 transaction = cmd_buff[1];
48 // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to 34 // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to
49 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case. 35 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
50 Handle archive_handle = static_cast<Handle>(cmd_buff[3]); 36 Handle archive_handle = static_cast<Handle>(cmd_buff[3]);
51 LowPathType type = static_cast<LowPathType>(cmd_buff[4]); 37 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
52 u32 size = cmd_buff[5]; 38 u32 filename_size = cmd_buff[5];
53 FileSys::Mode mode; mode.hex = cmd_buff[6]; 39 FileSys::Mode mode; mode.hex = cmd_buff[6];
54 u32 attributes = cmd_buff[7]; // TODO(Link Mauve): do something with those attributes. 40 u32 attributes = cmd_buff[7]; // TODO(Link Mauve): do something with those attributes.
55 u32 pointer = cmd_buff[9]; 41 u32 filename_ptr = cmd_buff[9];
56 42
57 if (type != LowPathType::Char) { 43 FileSys::Path file_path(filename_type, filename_size, filename_ptr);
58 ERROR_LOG(KERNEL, "file LowPath type other than char is currently unsupported"); 44 std::string file_string;
59 cmd_buff[1] = -1; 45 switch (file_path.GetType()) {
46 case FileSys::Char:
47 case FileSys::Wchar:
48 file_string = file_path.AsString();
49 break;
50 default:
51 WARN_LOG(KERNEL, "file LowPath type is currently unsupported; returning archive handle instead");
60 return; 52 return;
61 } 53 }
62 54
63 std::string file_name = GetStringFromCmdBuff(pointer, size); 55 DEBUG_LOG(KERNEL, "type=%d size=%d mode=%d attrs=%d data=%s",
64 56 filename_type, filename_size, mode, attributes, file_string.c_str());
65 DEBUG_LOG(KERNEL, "type=%d size=%d mode=%d attrs=%d data=%s", type, size, mode, attributes, file_name.c_str());
66 57
67 Handle handle = Kernel::OpenFileFromArchive(archive_handle, file_name, mode); 58 Handle handle = Kernel::OpenFileFromArchive(archive_handle, file_string, mode);
68 if (handle) { 59 if (handle) {
69 cmd_buff[1] = 0; 60 cmd_buff[1] = 0;
70 cmd_buff[3] = handle; 61 cmd_buff[3] = handle;
71 } else { 62 } else {
72 ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_name.c_str()); 63 ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_string.c_str());
73 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily. 64 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
74 cmd_buff[1] = -1; 65 cmd_buff[1] = -1;
75 } 66 }
@@ -80,31 +71,25 @@ void OpenFile(Service::Interface* self) {
80void OpenFileDirectly(Service::Interface* self) { 71void OpenFileDirectly(Service::Interface* self) {
81 u32* cmd_buff = Service::GetCommandBuffer(); 72 u32* cmd_buff = Service::GetCommandBuffer();
82 73
83 u32 transaction = cmd_buff[1]; 74 auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[2]);
84 auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[2]); 75 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
85 LowPathType archive_type = static_cast<LowPathType>(cmd_buff[3]); 76 u32 archivename_size = cmd_buff[4];
86 u32 archive_size = cmd_buff[4]; 77 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[5]);
87 LowPathType file_type = static_cast<LowPathType>(cmd_buff[5]); 78 u32 filename_size = cmd_buff[6];
88 u32 size = cmd_buff[6];
89 FileSys::Mode mode; mode.hex = cmd_buff[7]; 79 FileSys::Mode mode; mode.hex = cmd_buff[7];
90 u32 attributes = cmd_buff[8]; // TODO(Link Mauve): do something with those attributes. 80 u32 attributes = cmd_buff[8]; // TODO(Link Mauve): do something with those attributes.
91 u32 archive_pointer = cmd_buff[10]; 81 u32 archivename_ptr = cmd_buff[10];
92 u32 pointer = cmd_buff[12]; 82 u32 filename_ptr = cmd_buff[12];
93 83
94 if (archive_type != LowPathType::Empty) { 84 DEBUG_LOG(KERNEL, "archive_type=%d archive_size=%d file_type=%d file_size=%d file_mode=%d file_attrs=%d",
85 archivename_type, archivename_size, filename_type, filename_size, mode, attributes);
86
87 if (archivename_type != FileSys::Empty) {
95 ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported"); 88 ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported");
96 cmd_buff[1] = -1; 89 cmd_buff[1] = -1;
97 return; 90 return;
98 } 91 }
99 92
100 std::string archive_name = GetStringFromCmdBuff(archive_pointer, archive_size);
101 std::string file_name = GetStringFromCmdBuff(pointer, size);
102
103 DEBUG_LOG(KERNEL, "archive_type=%d archive_size=%d archive_data=%s "
104 "file_type=%d file_size=%d file_mode=%d file_attrs=%d file_data=%s",
105 archive_type, archive_size, archive_name.c_str(),
106 file_type, size, mode, attributes, file_name.c_str());
107
108 // TODO(Link Mauve): check if we should even get a handle for the archive, and don't leak it. 93 // TODO(Link Mauve): check if we should even get a handle for the archive, and don't leak it.
109 Handle archive_handle = Kernel::OpenArchive(archive_id); 94 Handle archive_handle = Kernel::OpenArchive(archive_id);
110 if (archive_handle) { 95 if (archive_handle) {
@@ -112,23 +97,30 @@ void OpenFileDirectly(Service::Interface* self) {
112 // cmd_buff[2] isn't used according to 3dmoo's implementation. 97 // cmd_buff[2] isn't used according to 3dmoo's implementation.
113 cmd_buff[3] = archive_handle; 98 cmd_buff[3] = archive_handle;
114 } else { 99 } else {
115 ERROR_LOG(KERNEL, "failed to get a handle for archive %s", archive_name.c_str()); 100 ERROR_LOG(KERNEL, "failed to get a handle for archive");
116 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily. 101 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
117 cmd_buff[1] = -1; 102 cmd_buff[1] = -1;
118 return; 103 return;
119 } 104 }
120 105
121 if (file_type != LowPathType::Char) { 106 FileSys::Path file_path(filename_type, filename_size, filename_ptr);
122 WARN_LOG(KERNEL, "file LowPath type other than char is currently unsupported; returning archive handle instead"); 107 std::string file_string;
108 switch (file_path.GetType()) {
109 case FileSys::Char:
110 case FileSys::Wchar:
111 file_string = file_path.AsString();
112 break;
113 default:
114 WARN_LOG(KERNEL, "file LowPath type is currently unsupported; returning archive handle instead");
123 return; 115 return;
124 } 116 }
125 117
126 Handle handle = Kernel::OpenFileFromArchive(archive_handle, file_name, mode); 118 Handle handle = Kernel::OpenFileFromArchive(archive_handle, file_string, mode);
127 if (handle) { 119 if (handle) {
128 cmd_buff[1] = 0; 120 cmd_buff[1] = 0;
129 cmd_buff[3] = handle; 121 cmd_buff[3] = handle;
130 } else { 122 } else {
131 ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_name.c_str()); 123 ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_string.c_str());
132 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily. 124 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
133 cmd_buff[1] = -1; 125 cmd_buff[1] = -1;
134 } 126 }
@@ -153,21 +145,25 @@ void CreateDirectory(Service::Interface* self) {
153 // TODO: cmd_buff[2], aka archive handle lower word, isn't used according to 145 // TODO: cmd_buff[2], aka archive handle lower word, isn't used according to
154 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case. 146 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
155 Handle archive_handle = static_cast<Handle>(cmd_buff[3]); 147 Handle archive_handle = static_cast<Handle>(cmd_buff[3]);
156 LowPathType type = static_cast<LowPathType>(cmd_buff[4]); 148 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
157 u32 name_size = cmd_buff[5]; 149 u32 dirname_size = cmd_buff[5];
158 u32 name_offset = cmd_buff[8]; 150 u32 dirname_ptr = cmd_buff[8];
159 151
160 if (type != LowPathType::Char) { 152 FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
161 ERROR_LOG(KERNEL, "directory LowPath type other than char is currently unsupported"); 153 std::string dir_string;
154 switch (dir_path.GetType()) {
155 case FileSys::Char:
156 case FileSys::Wchar:
157 dir_string = dir_path.AsString();
158 break;
159 default:
162 cmd_buff[1] = -1; 160 cmd_buff[1] = -1;
163 return; 161 return;
164 } 162 }
165 163
166 std::string dir_name = GetStringFromCmdBuff(name_offset, name_size); 164 DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_string.c_str());
167 165
168 DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", type, name_size, dir_name.c_str()); 166 cmd_buff[1] = Kernel::CreateDirectoryFromArchive(archive_handle, dir_string);
169
170 cmd_buff[1] = Kernel::CreateDirectoryFromArchive(archive_handle, dir_name);
171 167
172 DEBUG_LOG(KERNEL, "called"); 168 DEBUG_LOG(KERNEL, "called");
173} 169}
@@ -178,26 +174,30 @@ void OpenDirectory(Service::Interface* self) {
178 // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to 174 // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to
179 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case. 175 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
180 Handle archive_handle = static_cast<Handle>(cmd_buff[2]); 176 Handle archive_handle = static_cast<Handle>(cmd_buff[2]);
181 LowPathType type = static_cast<LowPathType>(cmd_buff[3]); 177 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
182 u32 size = cmd_buff[4]; 178 u32 dirname_size = cmd_buff[4];
183 u32 pointer = cmd_buff[6]; 179 u32 dirname_ptr = cmd_buff[6];
184 180
185 if (type != LowPathType::Char) { 181 FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
186 ERROR_LOG(KERNEL, "directory LowPath type other than char is currently unsupported"); 182 std::string dir_string;
183 switch (dir_path.GetType()) {
184 case FileSys::Char:
185 case FileSys::Wchar:
186 dir_string = dir_path.AsString();
187 break;
188 default:
187 cmd_buff[1] = -1; 189 cmd_buff[1] = -1;
188 return; 190 return;
189 } 191 }
190 192
191 std::string dir_name = GetStringFromCmdBuff(pointer, size); 193 DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_string.c_str());
192 194
193 DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", type, size, dir_name.c_str()); 195 Handle handle = Kernel::OpenDirectoryFromArchive(archive_handle, dir_string);
194
195 Handle handle = Kernel::OpenDirectoryFromArchive(archive_handle, dir_name);
196 if (handle) { 196 if (handle) {
197 cmd_buff[1] = 0; 197 cmd_buff[1] = 0;
198 cmd_buff[3] = handle; 198 cmd_buff[3] = handle;
199 } else { 199 } else {
200 ERROR_LOG(KERNEL, "failed to get a handle for directory %s", dir_name.c_str()); 200 ERROR_LOG(KERNEL, "failed to get a handle for directory %s", dir_string.c_str());
201 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily. 201 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
202 cmd_buff[1] = -1; 202 cmd_buff[1] = -1;
203 } 203 }
@@ -208,28 +208,26 @@ void OpenDirectory(Service::Interface* self) {
208void OpenArchive(Service::Interface* self) { 208void OpenArchive(Service::Interface* self) {
209 u32* cmd_buff = Service::GetCommandBuffer(); 209 u32* cmd_buff = Service::GetCommandBuffer();
210 210
211 auto arch_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[1]); 211 auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[1]);
212 LowPathType type = static_cast<LowPathType>(cmd_buff[2]); 212 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]);
213 u32 size = cmd_buff[3]; 213 u32 archivename_size = cmd_buff[3];
214 u32 pointer = cmd_buff[5]; 214 u32 archivename_ptr = cmd_buff[5];
215
216 DEBUG_LOG(KERNEL, "type=%d size=%d", archivename_type, archivename_size);
215 217
216 if (type != LowPathType::Empty) { 218 if (archivename_type != FileSys::Empty) {
217 ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported"); 219 ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported");
218 cmd_buff[1] = -1; 220 cmd_buff[1] = -1;
219 return; 221 return;
220 } 222 }
221 223
222 std::string archive_name = GetStringFromCmdBuff(pointer, size); 224 Handle handle = Kernel::OpenArchive(archive_id);
223
224 DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", type, size, archive_name.c_str());
225
226 Handle handle = Kernel::OpenArchive(arch_id);
227 if (handle) { 225 if (handle) {
228 cmd_buff[1] = 0; 226 cmd_buff[1] = 0;
229 // cmd_buff[2] isn't used according to 3dmoo's implementation. 227 // cmd_buff[2] isn't used according to 3dmoo's implementation.
230 cmd_buff[3] = handle; 228 cmd_buff[3] = handle;
231 } else { 229 } else {
232 ERROR_LOG(KERNEL, "failed to get a handle for archive %s", archive_name.c_str()); 230 ERROR_LOG(KERNEL, "failed to get a handle for archive");
233 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily. 231 // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
234 cmd_buff[1] = -1; 232 cmd_buff[1] = -1;
235 } 233 }