summaryrefslogtreecommitdiff
path: root/src/nw4r/snd/snd_SeqFile.cpp
blob: 6dd78c3d28b819b0b900fe53ab3ff0c5478407c4 (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
#include "nw4r/snd/snd_SeqFile.h"

/* Original source:
 * kiwi515/ogws
 * src/nw4r/snd/snd_SeqFile.cpp
 */

/*******************************************************************************
 * headers
 */

#include <cstring>

#include "common.h"

#include "nw4r/snd/snd_Util.h"

#include "nw4r/ut/ut_binaryFileFormat.h"
#include "nw4r/ut/ut_algorithm.h" // ut::AddOffsetToPtr

#include "nw4r/NW4RAssert.hpp"

/*******************************************************************************
 * functions
 */

namespace nw4r { namespace snd { namespace detail {

bool SeqFileReader::IsValidFileHeader(void const *seqData)
{
	ut::BinaryFileHeader const *fileHeader =
		static_cast<ut::BinaryFileHeader const *>(seqData);

	NW4RAssertMessage_Line(
		43, fileHeader->signature == SeqFile::SIGNATURE_FILE,
		"invalid file signature. seq data is not available.");

	if (fileHeader->signature != SeqFile::SIGNATURE_FILE)
		return false;

	u16 version = Util::ReadBigEndian(fileHeader->version);

	NW4RAssertMessage_Line(
		51, version >= NW4R_FILE_VERSION(1, 0),
		"seq file is not supported version.\n"
		"  please reconvert file using new version tools.\n");

	version = Util::ReadBigEndian(fileHeader->version); // ? again?
	if (version < NW4R_FILE_VERSION(1, 0))
		return false;

	NW4RAssertMessage_Line(
		59, version <= SUPPORTED_FILE_VERSION,
		"seq file is not supported version.\n"
		"  please reconvert file using new version tools.\n");

	if (version > SUPPORTED_FILE_VERSION)
		return false;

	return true;
}

SeqFileReader::SeqFileReader(void const *seqData) :
	mHeader		(nullptr),
	mDataBlock	(nullptr)
{
	NW4RAssertPointerNonnull_Line(78, seqData);

	if (!IsValidFileHeader(seqData))
		return;

	mHeader = static_cast<SeqFile::Header const *>(seqData);
	mDataBlock = static_cast<SeqFile::DataBlock const *>(ut::AddOffsetToPtr(
		mHeader, Util::ReadBigEndian(mHeader->dataBlockOffset)));

	NW4RAssert_Line(87, mDataBlock->blockHeader.kind
	                        == SeqFile::SIGNATURE_DATA_BLOCK);
}

void const *SeqFileReader::GetBaseAddress() const
{
	NW4RAssertPointerNonnull_Line(101, mHeader);

	return ut::AddOffsetToPtr(mDataBlock,
	                          Util::ReadBigEndian(mDataBlock->baseOffset));
}

bool SeqFileReader::ReadOffsetByLabel(char const *labelName,
                                      u32 *offsetPtr) const
{
	NW4RAssertPointerNonnull_Line(117, offsetPtr);

	// NOTE: reinterpret_cast necessary instead of static_cast for regalloc(???)
	SeqFile::LabelBlock const *labelBlock =
		reinterpret_cast<SeqFile::LabelBlock const *>(ut::AddOffsetToPtr(
			mHeader, Util::ReadBigEndian(mHeader->labelBlockOffset)));

	if (!labelBlock)
		return false;

	u32 labelNameLen = std::strlen(labelName);

	for (int index = 0;
	     index < Util::ReadBigEndian(labelBlock->labelInfoTable.count); ++index)
	{
		u32 ofs = labelBlock->labelInfoTable.item[index];

		// NOTE: reinterpret_cast necessary here too
		SeqFile::LabelInfo const *labelInfo =
			reinterpret_cast<SeqFile::LabelInfo const *>(
				ut::AddOffsetToPtr(labelBlock, Util::ReadBigEndian(ofs) + 8));

		NW4RAssertPointerNonnull_Line(133, labelInfo);

		if (labelNameLen == Util::ReadBigEndian(labelInfo->nameLen)
		    && std::strncmp(labelName, labelInfo->name, labelNameLen) == 0)
		{
			*offsetPtr = Util::ReadBigEndian(labelInfo->offset);
			return true;
		}
	}

	return false;
}

}}} // namespace nw4r::snd::detail