summaryrefslogtreecommitdiff
path: root/src/libms/commonlib.c
blob: edbd7b8e53cffccd69924bcc83608e8e03a34a6a (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
#include "libms/commonlib.h"

#include "libms/libms.h"

void LMSi_AnalyzeMessageHeader(struct MsbInfo *info) {
    info->version = info->header->version;
    info->sectionCount = info->header->sectionCount;
    if (info->sectionCount == 0) {
        info->sectionInfos = NULL;
    } else {
        info->sectionInfos = (struct MsbBlock *)LMSi_Malloc(sizeof(struct MsbBlock) * info->sectionCount);
    }
    info->fileLength = info->header->fileLength;
}

// Complete fakematch here but hey, it works
inline static struct MsbBlockHeader *GetBlockHeader(struct MsbInfo *info, int offset) {
    return (struct MsbBlockHeader *)((unsigned int)info->header + offset);
}

inline static struct MsbBlockHeader *GetBlockHeader2(struct MsbInfo *info, int offset) {
    return (struct MsbBlockHeader *)(offset + (unsigned int)info->header);
}

void LMSi_AnalyzeMessageBlocks(struct MsbInfo *info) {
    int i;
    int offset;
    struct MsbBlock *section;

    offset = ROUND_UP(sizeof(struct MsbHeader), 0x20); // file header
    for (i = 0; i < info->sectionCount; i++) {
        section = &info->sectionInfos[i];
        section->ptr = &GetBlockHeader(info, offset)->numEntries;
        section->name[0] = GetBlockHeader(info, offset)->name[0];
        section->name[1] = GetBlockHeader2(info, offset)->name[1];
        section->name[2] = GetBlockHeader2(info, offset)->name[2];
        section->name[3] = GetBlockHeader2(info, offset)->name[3];
        section->sectionLength = GetBlockHeader2(info, offset)->sectionLength;
        section->field_0x0C = GetBlockHeader2(info, offset)->field_0x08;
        offset = offset + 0x10;                   // section header
        offset = offset + section->sectionLength; // section
        offset = ROUND_UP(offset, 0x10);          // align
    }
}

void LMSi_AnalyzeMessageBinary(struct MsbInfo *info, const char *type) {
    LMSi_AnalyzeMessageHeader(info);
    LMSi_AnalyzeMessageBlocks(info);
}

int LMSi_SearchBlockByName(struct MsbInfo *info, const char *name) {
    for (unsigned short i = 0; i < info->sectionCount; i++) {
        if (LMSi_MemCmp(info->sectionInfos[i].name, name, 4)) {
            return i;
        }
    }

    return -1;
}

int LMSi_GetHashTableIndexFromLabel(const char *label, int tableSize) {
    unsigned int hash = 0;
    int c;
    while (1) {
        c = *label;
        if (c == '\0') {
            break;
        }
        hash = hash * 0x492 + c;
        label++;
    }
    return hash % tableSize;
}