summaryrefslogtreecommitdiff
path: root/libs/revolution/src/homebuttonLib/nw4hbm/db/db_mapFile.cpp
blob: adc72b7c4a9e23a1850bfc4972b79d7e2ecbbd98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <revolution/dvd.h>
#include <revolution/os.h>
#include "assert.h"
#include "mapFile.h"

#include "global.h"

typedef u8 GetCharFunc(u8 const* buf);

namespace nw4hbm {
    namespace db {
        static u8 GetCharOnMem_(const u8* buf);
        static u8 GetCharOnDvd_(u8 const* buf);

        static u8* SearchNextLine_(u8* buf, s32 lines);
        static u8* SearchNextSection_(u8* buf);
        static u8* SearchParam_(u8* lineTop, u32 argNum, u8 splitter);

        static u32 XStrToU32_(u8 const* str);
        static u32 CopySymbol_(u8 const* buf, u8* str, u32 strLenMax, u8 splitter);

        static bool QuerySymbolToMapFile_(u8* buf, OSModuleInfo const* moduleInfo, u32 address,
                                          u8* strBuf, u32 strBufSize);
        static bool QuerySymbolToSingleMapFile_(MapFile* pMapFile, u32 address, u8* strBuf,
                                                u32 strBufSize) NO_INLINE;
    }  // namespace db
}  // namespace nw4hbm

namespace nw4hbm {
    namespace db {
        static u8 sMapBuf[0x200];
        static s32 sMapBufOffset = -1;
        static DVDFileInfo sFileInfo;
        static u32 sFileLength;
        static MapFile* sMapFileList;
        static GetCharFunc* GetCharPtr_;
    }  // namespace db
}  // namespace nw4hbm

namespace nw4hbm {
    namespace db {
        bool MapFile_Exists(void) {
            return sMapFileList ? true : false;
        }

        static u8 GetCharOnMem_(u8 const* buf) {
            return *buf;
        }

        static s32 GetSize(s32 offset, u32 length) {
            if (offset + ARRAY_SIZE(sMapBuf) >= length) {
                return OSRoundUp32B(length - offset);
            }

            return ARRAY_SIZE(sMapBuf);
        }

        static u8 GetCharOnDvd_(u8 const* buf) {
            s32 address = (u32)buf & ~0x80000000;
            s32 offset = address - sMapBufOffset;

            if (address >= sFileLength) {
                return 0;
            }

            if (sMapBufOffset < 0 || offset < 0 || ARRAY_SIZE(sMapBuf) <= offset) {
                s32 len;
                s32 size;

                sMapBufOffset = OSRoundDown32B(address);
                offset = address - sMapBufOffset;
                size = GetSize(sMapBufOffset, sFileLength);

                BOOL enabled = OSEnableInterrupts();

                len = DVDReadAsyncPrio(&sFileInfo, sMapBuf, size, sMapBufOffset, NULL, 2);

                while (DVDGetCommandBlockStatus(&sFileInfo.cb)) {}

                OSRestoreInterrupts(enabled);

                if (len <= 0) {
                    return 0;
                }
            }

            return sMapBuf[offset];
        }

        void dummyString() {
            u8* buffer;
            NW4HBM_ASSERT_CHECK_NULL(0, buffer);

            u8* mapDataBuf;
            NW4HBM_ASSERT_CHECK_NULL(0, mapDataBuf);

            MapFile* pMapFile;
            NW4HBM_ASSERT_CHECK_NULL(0, pMapFile);

            NW4HBM_ASSERT(0, sMapFileList->moduleInfo != NULL);

            u8* filePath;
            NW4HBM_ASSERT_CHECK_NULL(0, filePath);

            NW4HBM_ASSERT(0, pMapFile->fileEntry >= 0);
        }

        static u8* SearchNextLine_(u8* buf, s32 lines) {
            u8 c;

            NW4HBM_ASSERT_CHECK_NULL(361, GetCharPtr_);

            if (buf == NULL) {
                return NULL;
            }

            for (; (c = (*GetCharPtr_)(buf)) != '\0'; buf++) {
                if (c == '\n') {
                    if (--lines <= 0) {
                        return buf + 1;
                    }
                }
            }

            return NULL;
        }

        static u8* SearchNextSection_(u8* buf) {
            NW4HBM_ASSERT_CHECK_NULL(397, GetCharPtr_);

            do {
                buf = SearchNextLine_(buf, 1);

                if (!buf) {
                    return NULL;
                }
            } while ((*GetCharPtr_)(buf) != '.');

            return buf;
        }

        static u8* SearchParam_(u8* lineTop, u32 argNum, u8 splitter) {
            int inArg = 0;
            u8* buf = lineTop;

            NW4HBM_ASSERT_CHECK_NULL(432, GetCharPtr_);

            if (buf == NULL) {
                return NULL;
            }

            while (true) {
                u8 c = (*GetCharPtr_)(buf);

                if (c == '\0' || c == '\n') {
                    return 0;
                }

                if (inArg) {
                    if (c == splitter) {
                        inArg = 0;
                    }
                } else if (c != splitter) {
                    if (!argNum--) {
                        return buf;
                    }

                    inArg = 1;
                }

                buf++;
            }

            return 0;
        }

        static u32 XStrToU32_(u8 const* str) {
            u32 val = 0;

            NW4HBM_ASSERT_CHECK_NULL(486, str);
            NW4HBM_ASSERT_CHECK_NULL(487, GetCharPtr_);

            while (true) {
                u32 num;
                u8 c;

                c = (*GetCharPtr_)(str);

                if ('0' <= c && c <= '9') {
                    num = static_cast<u32>(c - '0');
                } else if ('a' <= c && c <= 'z') {
                    num = static_cast<u32>(c - ('a' - 10));  // ?
                } else if ('A' <= c && c <= 'Z') {
                    num = static_cast<u32>(c - ('A' - 10));  // What's the - 10 for
                } else {
                    return val;
                }

                if (val >= 0x10000000) {
                    return 0;
                }

                val = num + (val << 4);
                str++;
            }

            return 0;
        }

        static u32 CopySymbol_(const u8* buf, u8* str, u32 strLenMax, u8 splitter) {
            u32 cnt = 0;

            NW4HBM_ASSERT_CHECK_NULL(544, buf);
            NW4HBM_ASSERT_CHECK_NULL(545, str);
            NW4HBM_ASSERT_CHECK_NULL(546, GetCharPtr_);

            while (true) {
                u8 c = (*GetCharPtr_)(buf++);

                if (c == splitter || c == '\0' || c == '\n') {
                    *str = '\0';
                    return cnt;
                }

                *str = c;
                str++;
                cnt++;

                if (cnt >= strLenMax - 1) {
                    *str = '\0';
                    return cnt;
                }
            }

            return 0;
        }

        static bool QuerySymbolToMapFile_(u8* buf, OSModuleInfo const* moduleInfo, u32 address,
                                          u8* strBuf, u32 strBufSize) {
            OSSectionInfo* sectionInfo = NULL;
            u32 sectionCnt;

            NW4HBM_ASSERT_CHECK_NULL(602, strBuf);
            NW4HBM_ASSERT(603, strBufSize > 0);

            if (moduleInfo) {
                sectionInfo = reinterpret_cast<OSSectionInfo*>(moduleInfo->sectionInfoOffset);
                sectionCnt = moduleInfo->numSections;
            }

            do {
                u32 offset = 0;

                buf = SearchNextSection_(buf);
                buf = SearchNextLine_(buf, 3);

                if (sectionInfo) {
                    offset = sectionInfo->offset;

                    if (address < offset) {
                        goto get_next_section_info;
                    }

                    if (address >= offset + sectionInfo->size) {
                        goto get_next_section_info;
                    }
                }

                while (true) {
                    u8* param;
                    u32 startAddr;
                    u32 size;

                    buf = SearchNextLine_(buf, 1);
                    if (!buf) {
                        return false;
                    }

                    param = SearchParam_(buf, 1, ' ');
                    if (!param) {
                        break;
                    }

                    size = XStrToU32_(param);
                    param = SearchParam_(buf, 2, ' ');
                    if (!param) {
                        break;
                    }

                    startAddr = XStrToU32_(param);
                    if (!startAddr) {
                        continue;
                    }

                    startAddr = startAddr + offset;
                    if (address < startAddr || startAddr + size <= address) {
                        continue;
                    }

                    param = SearchParam_(buf, 5, ' ');
                    if (!param) {
                        *strBuf = '\0';
                        return true;
                    }

                    if ((*GetCharPtr_)(param) == '.') {
                        continue;
                    }

                    CopySymbol_(param, strBuf, strBufSize, ' ');
                    return true;
                }

            get_next_section_info:
                if (sectionInfo) {
                    if (!--sectionCnt) {
                        return false;
                    }

                    sectionInfo++;
                }
            } while (true);

            return false;
        }

        static bool QuerySymbolToSingleMapFile_(MapFile* pMapFile, u32 address, u8* strBuf,
                                                u32 strBufSize) {
            NW4HBM_ASSERT_CHECK_NULL(723, pMapFile);
            NW4HBM_ASSERT_CHECK_NULL(724, strBuf);

            if (pMapFile->mapBuf) {
                GetCharPtr_ = &GetCharOnMem_;
                return QuerySymbolToMapFile_(pMapFile->mapBuf, pMapFile->moduleInfo, address,
                                             strBuf, strBufSize);
            }

            if (pMapFile->fileEntry >= 0) {
                bool ret;

                if (DVDFastOpen(pMapFile->fileEntry, &sFileInfo)) {
                    sFileLength = sFileInfo.length;
                    GetCharPtr_ = &GetCharOnDvd_;
                    ret = QuerySymbolToMapFile_((u8*)(0x80000000), pMapFile->moduleInfo, address, strBuf, strBufSize);

                    DVDClose(&sFileInfo);
                    return ret;
                }
            }

            *strBuf = '\0';
            return false;
        }

        bool MapFile_QuerySymbol(u32 address, u8* strBuf, u32 strBufSize) {
            MapFile* pMap;
            for (pMap = sMapFileList; pMap; pMap = pMap->next) {
                if (QuerySymbolToSingleMapFile_(pMap, address, strBuf, strBufSize)) {
                    return true;
                }
            }

            return false;
        }

    }  // namespace db
}  // namespace nw4hbm