libnedit
Lightweight C++ library for Nintendo DS(i) formats
Loading...
Searching...
No Matches
ntr_Include.hpp
1
2#pragma once
3#include <cstdint>
4#include <string>
5#include <vector>
6#include <cstdio>
7#include <cstring>
8#include <codecvt>
9#include <locale>
10#include <memory>
11#include <sstream>
12#include <sys/stat.h>
13#include <dirent.h>
14#include <functional>
15#include <climits>
16#include <cmath>
17#include <stack>
18#include <optional>
19#include <iomanip>
20
21#define ATTR_PACKED __attribute__((packed))
22
23namespace ntr {
24
25 using u8 = uint8_t;
26 using u16 = uint16_t;
27 using u32 = uint32_t;
28 using u64 = uint64_t;
29
30 using i8 = int8_t;
31 using i16 = int16_t;
32 using i32 = int32_t;
33 using i64 = int64_t;
34
35 class ScopeGuard {
36 public:
37 using Fn = std::function<void()>;
38
39 private:
40 bool run;
41 Fn exit_fn;
42
43 public:
44 ScopeGuard(Fn &&fn) : run(true), exit_fn(fn) {}
45
46 ~ScopeGuard() {
47 if(this->run) {
48 this->exit_fn();
49 }
50 }
51
52 inline void Cancel() {
53 this->run = false;
54 }
55 };
56
57}
58
59#define NTR_R_SUCCEED() return ::ntr::ResultSuccess
60
61#define NTR_R_FAIL(rc) { \
62 const auto _tmp_rc = (rc); \
63 return _tmp_rc; \
64}
65
66#define NTR_R_TRY(rc) { \
67 const auto _tmp_rc = (rc); \
68 if(_tmp_rc.IsFailure()) { \
69 return _tmp_rc; \
70 } \
71}
72
73namespace ntr {
74
75 struct Result {
76 using BaseType = u16;
77
78 BaseType value;
79
80 static constexpr BaseType SuccessValue = BaseType();
81
82 constexpr Result() : value() {}
83 constexpr Result(const BaseType val) : value(val) {}
84
85 inline constexpr bool IsSuccess() const {
86 return this->value == SuccessValue;
87 }
88
89 inline constexpr bool IsFailure() const {
90 return !this->IsSuccess();
91 }
92
93 inline std::string GetDescription() const;
94 };
95
96 constexpr Result ResultSuccess = 0;
97 constexpr Result ResultUnknownResult = 0x0002;
98
99 constexpr Result ResultUnsupportedPixelFormat = 0x0101;
100 constexpr Result ResultInvalidSizeForPixelFormat = 0x0102;
101 constexpr Result ResultColorPaletteAllocationFailure = 0x0103;
102 constexpr Result ResultInvalidPixelFormat = 0x0104;
103 constexpr Result ResultInvalidCharacterFormat = 0x0105;
104 constexpr Result ResultInvalidScreenData = 0x0106;
105 constexpr Result ResultScreenDataGenerationUnimplemented = 0x0107;
106 constexpr Result ResultUnexpectedBannerIconDimensions = 0x0108;
107
108 constexpr Result ResultInvalidFile = 0x0201;
109 constexpr Result ResultEndOfData = 0x0202;
110 constexpr Result ResultReadNotSupported = 0x0203;
111 constexpr Result ResultWriteNotSupported = 0x0204;
112 constexpr Result ResultUnexpectedReadSize = 0x0205;
113 constexpr Result ResultFileNotCompressed = 0x0206;
114 constexpr Result ResultInvalidEndSeekOffset = 0x0207;
115 constexpr Result ResultInvalidFileOpenMode = 0x0208;
116 constexpr Result ResultUnableToOpenStdioFile = 0x0209;
117 constexpr Result ResultInvalidSeekPosition = 0x020a;
118 constexpr Result ResultUnableToSeekStdioFile = 0x020b;
119 constexpr Result ResultUnableToReadStdioFile = 0x020c;
120 constexpr Result ResultUnableToWriteStdioFile = 0x020d;
121 constexpr Result ResultUnableToCloseStdioFile = 0x020e;
122 constexpr Result ResultUnableToCreateStdioDirectory = 0x020f;
123 constexpr Result ResultUnableToDeleteStdioFile = 0x0210;
124
125 constexpr Result ResultNitroFsDirectoryNotFound = 0x0301;
126 constexpr Result ResultNitroFsFileNotFound = 0x0302;
127
128 constexpr Result ResultBMGInvalidHeader = 0x0401;
129 constexpr Result ResultBMGInvalidInfoSection = 0x0402;
130 constexpr Result ResultBMGInvalidDataSection = 0x0403;
131 constexpr Result ResultBMGInvalidUnsupportedEncoding = 0x0404;
132 constexpr Result ResultBMGInvalidMessageAttributeSize = 0x0405;
133 constexpr Result ResultBMGInvalidEscapeSequence = 0x0406;
134 constexpr Result ResultBMGUnexpectedSectionCount = 0x0407;
135 constexpr Result ResultBMGInvalidMessageIdSection = 0x0408;
136
137 constexpr Result ResultNCGRInvalidHeader = 0x0501;
138 constexpr Result ResultNCGRInvalidCharacterDataBlock = 0x0502;
139 constexpr Result ResultNCGRInvalidCharacterPositionBlock = 0x0503;
140
141 constexpr Result ResultNCLRInvalidHeader = 0x0601;
142 constexpr Result ResultNCLRInvalidPaletteDataBlock = 0x0602;
143
144 constexpr Result ResultNSCRInvalidHeader = 0x0701;
145 constexpr Result ResultNSCRInvalidScreenDataBlock = 0x0702;
146 constexpr Result ResultNSCRWriteNotSupported = 0x0703;
147
148 constexpr Result ResultNARCInvalidHeader = 0x0801;
149 constexpr Result ResultNARCInvalidFileAllocationTableBlock = 0x0802;
150 constexpr Result ResultNARCInvalidFileNameTableBlock = 0x0803;
151 constexpr Result ResultNARCInvalidFileImageBlock = 0x0804;
152
153 constexpr Result ResultSDATInvalidHeader = 0x0901;
154 constexpr Result ResultSDATInvalidSymbolBlock = 0x0902;
155 constexpr Result ResultSDATInvalidInfoBlock = 0x0903;
156 constexpr Result ResultSDATInvalidFileAllocationTableBlock = 0x0904;
157 constexpr Result ResultSDATInvalidFileBlock = 0x0905;
158 constexpr Result ResultSDATInvalidSymbols = 0x0906;
159 constexpr Result ResultSDATEntryNotFound = 0x0907;
160 constexpr Result ResultSDATInvalidEntryFormat = 0x0908;
161
162 constexpr Result ResultSSEQInvalidHeader = 0x0a01;
163 constexpr Result ResultSSEQInvalidDataSection = 0x0a02;
164 constexpr Result ResultSSEQWriteNotSupported = 0x0a03;
165
166 constexpr Result ResultSBNKInvalidHeader = 0x0b01;
167 constexpr Result ResultSBNKInvalidDataSection = 0x0b02;
168 constexpr Result ResultSBNKWriteNotSupported = 0x0b03;
169
170 constexpr Result ResultSWARInvalidHeader = 0x0c01;
171 constexpr Result ResultSWARInvalidDataSection = 0x0c02;
172 constexpr Result ResultSWARWriteNotSupported = 0x0c03;
173
174 constexpr Result ResultSTRMInvalidHeader = 0x0d01;
175 constexpr Result ResultSTRMInvalidHeadSection = 0x0d02;
176 constexpr Result ResultSTRMInvalidDataSection = 0x0d03;
177 constexpr Result ResultSTRMWriteNotSupported = 0x0d04;
178
179 constexpr Result ResultROMInvalidUnitCode = 0x0e01;
180 constexpr Result ResultROMInvalidNintendoLogoCRC16 = 0x0e02;
181
182 constexpr Result ResultCompressionInvalidLzFormat = 0x0f01;
183 constexpr Result ResultCompressionTooBigCompressSize = 0x0f02;
184 constexpr Result ResultCompressionInvalidRepeatSize = 0x0f03;
185
186 constexpr Result ResultUtilityInvalidSections = 0x1001;
187
188 using ResultDescriptionEntry = std::pair<Result, const char*>;
189
190 constexpr ResultDescriptionEntry ResultDescriptionTable[] = {
191 { ResultSuccess, "Success" },
192 { ResultUnknownResult, "Unknown result value" },
193
194 { ResultUnsupportedPixelFormat, "Unsupported pixel format" },
195 { ResultInvalidSizeForPixelFormat, "Invalid size for given pixel format" },
196 { ResultColorPaletteAllocationFailure, "Unable to allocate color in palette" },
197 { ResultInvalidPixelFormat, "Invalid pixel format" },
198 { ResultInvalidCharacterFormat, "Invalid (graphics) character format" },
199 { ResultInvalidScreenData, "Invalid screen (NSCR) data" },
200 { ResultScreenDataGenerationUnimplemented, "Unimplemented feature: screen data saving/generation" },
201 { ResultUnexpectedBannerIconDimensions, "Unexpected banner icon dimensions" },
202
203 { ResultInvalidFile, "Invalid file" },
204 { ResultEndOfData, "End of data (EOF)" },
205 { ResultReadNotSupported, "Read not supported in file" },
206 { ResultWriteNotSupported, "Write not supported in file" },
207 { ResultUnexpectedReadSize, "Got unexpected read size" },
208 { ResultFileNotCompressed, "File is not compressed" },
209 { ResultInvalidEndSeekOffset, "Invalid offset for end seeking (must be 0)" },
210 { ResultInvalidFileOpenMode, "Invalid file open mode" },
211 { ResultUnableToOpenStdioFile, "Unable to open stdio file" },
212 { ResultInvalidSeekPosition, "Invalid seek position" },
213 { ResultUnableToSeekStdioFile, "Unable to seek stdio file" },
214 { ResultUnableToReadStdioFile, "Unable to read stdio file" },
215 { ResultUnableToWriteStdioFile, "Unable to write stdio file" },
216 { ResultUnableToCloseStdioFile, "Unable to close stdio file" },
217 { ResultUnableToCreateStdioDirectory, "Unable to create stdio directory" },
218 { ResultUnableToDeleteStdioFile, "Unable to delete stdio file" },
219
220 { ResultNitroFsDirectoryNotFound, "NitroFs directory not found" },
221 { ResultNitroFsFileNotFound, "NitroFs file not found" },
222
223 { ResultBMGInvalidHeader, "Invalid BMG header" },
224 { ResultBMGInvalidInfoSection, "Invalid BMG INF1 section" },
225 { ResultBMGInvalidDataSection, "Invalid BMG DAT1 section" },
226 { ResultBMGInvalidUnsupportedEncoding, "Invalid or unsupported BMG encoding" },
227 { ResultBMGInvalidMessageAttributeSize, "Invalid BMG message attribute size" },
228 { ResultBMGInvalidEscapeSequence, "Invalid BMG escape sequence" },
229 { ResultBMGUnexpectedSectionCount, "Unexpected BMG section count (< 2)" },
230 { ResultBMGInvalidMessageIdSection, "Invalid BMG MID1 section" },
231
232 { ResultNCGRInvalidHeader, "Invalid NCGR header" },
233 { ResultNCGRInvalidCharacterDataBlock, "Invalid NCGR character data block" },
234 { ResultNCGRInvalidCharacterPositionBlock, "Invalid NCGR character position block" },
235
236 { ResultNCLRInvalidHeader, "Invalid NCLR header" },
237 { ResultNCLRInvalidPaletteDataBlock, "Invalid NCGR palette data block" },
238
239 { ResultNSCRInvalidHeader, "Invalid NSCR header" },
240 { ResultNSCRInvalidScreenDataBlock, "Invalid NSCR screen data block" },
241 { ResultNSCRWriteNotSupported, "Unsupported feature: writing to NSCR" },
242
243 { ResultNARCInvalidHeader, "Invalid NARC header" },
244 { ResultNARCInvalidFileAllocationTableBlock, "Invalid NARC file allocation table block" },
245 { ResultNARCInvalidFileNameTableBlock, "Invalid NARC file name table block" },
246 { ResultNARCInvalidFileImageBlock, "Invalid NARC file image block" },
247
248 { ResultSDATInvalidHeader, "Invalid SDAT header" },
249 { ResultSDATInvalidSymbolBlock, "Invalid SDAT symbol block" },
250 { ResultSDATInvalidInfoBlock, "Invalid SDAT info block" },
251 { ResultSDATInvalidFileAllocationTableBlock, "Invalid SDAT file allocation table block" },
252 { ResultSDATInvalidFileBlock, "Invalid SDAT file block" },
253 { ResultSDATInvalidSymbols, "Invalid SDAT symbols" },
254 { ResultSDATEntryNotFound, "SDAT entry not found" },
255 { ResultSDATInvalidEntryFormat, "Invalid SDAT entry format" },
256
257 { ResultSSEQInvalidHeader, "Invalid SSEQ header" },
258 { ResultSSEQInvalidDataSection, "Invalid SSEQ data section" },
259 { ResultSSEQWriteNotSupported, "Unsupported feature: writing to SSEQ" },
260
261 { ResultSBNKInvalidHeader, "Invalid SBNK header" },
262 { ResultSBNKInvalidDataSection, "Invalid SBNK data section" },
263 { ResultSBNKWriteNotSupported, "Unsupported feature: writing to SBNK" },
264
265 { ResultSWARInvalidHeader, "Invalid SWAR header" },
266 { ResultSWARInvalidDataSection, "Invalid SWAR data section" },
267 { ResultSWARWriteNotSupported, "Unsupported feature: writing to SWAR" },
268
269 { ResultSTRMInvalidHeader, "Invalid STRM header" },
270 { ResultSTRMInvalidHeadSection, "Invalid STRM head section" },
271 { ResultSTRMInvalidDataSection, "Invalid STRM data section" },
272 { ResultSTRMWriteNotSupported, "Unsupported feature: writing to STRM" },
273
274 { ResultUtilityInvalidSections, "Invalid DWC utility sections" }
275 };
276
277 inline constexpr Result GetResultDescription(const Result rc, std::string &out_desc) {
278 for(u32 i = 0; i < std::size(ResultDescriptionTable); i++) {
279 if(ResultDescriptionTable[i].first.value == rc.value) {
280 out_desc = ResultDescriptionTable[i].second;
281 NTR_R_SUCCEED();
282 }
283 }
284
285 NTR_R_FAIL(ResultUnknownResult);
286 }
287
288 inline std::string Result::GetDescription() const {
289 std::string desc = "<unknown result: " + std::to_string(this->value) + ">";
290 GetResultDescription(*this, desc);
291 return desc;
292 }
293
294}
295
296inline constexpr size_t operator ""_KB(unsigned long long n) {
297 return n * 0x400;
298}
299
300inline constexpr size_t operator ""_MB(unsigned long long n) {
301 return operator ""_KB(n) * 0x400;
302}
303
304inline constexpr size_t operator ""_GB(unsigned long long n) {
305 return operator ""_MB(n) * 0x400;
306}
307
308#define NTR_ENUM_BIT_OPERATORS(enum_type, base_type) \
309inline constexpr enum_type operator|(enum_type lhs, enum_type rhs) { \
310 return static_cast<enum_type>(static_cast<base_type>(lhs) | static_cast<base_type>(rhs)); \
311} \
312inline constexpr enum_type operator&(enum_type lhs, enum_type rhs) { \
313 return static_cast<enum_type>(static_cast<base_type>(lhs) & static_cast<base_type>(rhs)); \
314}
315
316#define NTR_BITMASK(n) (1 << n)
Definition ntr_Include.hpp:35
Definition ntr_Include.hpp:75