forked from Radfordhound/HedgeLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBINA.cs
275 lines (234 loc) · 8.27 KB
/
BINA.cs
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
using HedgeLib.Headers;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace HedgeLib.IO
{
public static class BINA
{
public static Encoding Encoding => Encoding.GetEncoding("shift-jis");
public enum OffsetTypes
{
SixBit = 0x40,
FourteenBit = 0x80,
ThirtyBit = 0xC0
}
}
public class BINAReader : ExtendedBinaryReader
{
// Constructors
public BINAReader(Stream input, uint offset = 0) :
base(input, BINA.Encoding)
{
Offset = offset;
}
public BINAReader(Stream input, Encoding encoding,
uint offset = 0) : base(input, encoding)
{
Offset = offset;
}
// Methods
public BINAHeader ReadHeader()
{
var sig = ReadSignature(4);
JumpBehind(4);
if (sig == BINAHeader.Signature)
return new BINAv2Header(this);
else if (sig == PACxHeader.PACxSignature)
return new PACxHeader(this);
return new BINAv1Header(this);
}
public List<uint> ReadFooter(uint finalTableLength)
{
uint lastOffsetPos = Offset;
uint footerEnd = (uint)BaseStream.Position + finalTableLength;
var offsets = new List<uint>();
while (BaseStream.Position < BaseStream.Length &&
BaseStream.Position < footerEnd)
{
byte b = ReadByte();
byte type = (byte)(b & 0xC0); // 0xC0 = 1100 0000. We're getting the first 2 bits.
byte d = (byte)(b & 0x3F); // 0x3F = 0011 1111. We're getting the last 6 bits.
if (type == (byte)BINA.OffsetTypes.SixBit)
{
d <<= 2;
offsets.Add(d + lastOffsetPos);
}
else if (type == (byte)BINA.OffsetTypes.FourteenBit)
{
byte b2 = ReadByte();
ushort d2 = (ushort)(((d << 8) | b2) << 2);
offsets.Add(d2 + lastOffsetPos);
}
else if (type == (byte)BINA.OffsetTypes.ThirtyBit)
{
var bytes = ReadBytes(3);
uint d2 = (uint)(((d << 24) | (bytes[0] << 16) |
(bytes[1] << 8) | bytes[2]) << 2);
offsets.Add(d2 + lastOffsetPos);
}
else break;
lastOffsetPos = offsets[offsets.Count - 1];
}
return offsets;
}
}
public class BINAWriter : ExtendedBinaryWriter
{
// Variables/Constants
protected List<StringTableEntry> strings = new List<StringTableEntry>();
// Constructors
public BINAWriter(Stream output, uint offset = 0,
bool isBigEndian = false) : base(output, BINA.Encoding, isBigEndian)
{
Offset = offset;
}
public BINAWriter(Stream output, BINAHeader header) :
base(output, BINA.Encoding, header.IsBigEndian)
{
header.PrepareWrite(this);
}
public BINAWriter(Stream output, Encoding encoding,
BINAHeader header) : base(output, encoding, header.IsBigEndian)
{
header.PrepareWrite(this);
}
// Methods
public void FinishWrite(BINAHeader header)
{
WriteStringTable(header);
WriteFooter(header);
BaseStream.Position = 0;
header.FinishWrite(this);
}
public void WriteStringTable(BINAHeader header)
{
uint stringTablePos = WriteStringTable();
// Update header values
if (header is BINAv2Header h2)
{
h2.StringTableOffset = (stringTablePos - Offset);
h2.StringTableLength = (uint)BaseStream.Position - stringTablePos;
}
else if (header is PACxHeader h3)
{
h3.StringTableLength = (uint)BaseStream.Position - stringTablePos;
}
}
public uint WriteStringTable()
{
FixPadding();
uint stringTablePos = (uint)BaseStream.Position;
foreach (var tableEntry in strings)
{
// Fill-in all the offsets that point to this string in the file
foreach (string offsetName in tableEntry.OffsetNames)
{
FillInOffset(offsetName,
(uint)BaseStream.Position, false);
}
// Write the string
WriteNullTerminatedString(tableEntry.Data);
}
FixPadding();
return stringTablePos;
}
public uint WriteFooter(BINAHeader header)
{
uint footerStartPos = WriteFooter();
if (header is PACxHeader)
FixPadding(8);
// Update header values and write footer magic
header.FinalTableLength = (uint)BaseStream.Position - footerStartPos;
if (header is BINAv1Header h1)
{
h1.FinalTableOffset = (footerStartPos - Offset);
if (h1.IsFooterMagicPresent)
h1.WriteFooterMagic(this);
}
else if (header is BINAv2Header h2)
{
h2.DataLength = (uint)BaseStream.Position - 0x10;
}
header.FileSize = (uint)BaseStream.Position;
return footerStartPos;
}
public uint WriteFooter()
{
bool isBigEndian = IsBigEndian;
uint footerStartPos = (uint)BaseStream.Position;
uint lastOffsetPos = Offset;
IsBigEndian = true;
// Write Offset Table
foreach (var offset in offsets)
{
uint d = (offset.Value - lastOffsetPos) >> 2;
if (d <= 0x3F)
{
Write((byte)(((byte)BINA.OffsetTypes.SixBit) | d));
}
else if (d <= 0x3FFF)
{
Write((ushort)((((byte)BINA.OffsetTypes.FourteenBit) << 8) | d));
}
else
{
Write((uint)((((byte)BINA.OffsetTypes.ThirtyBit) << 24) | d));
}
lastOffsetPos = offset.Value;
}
FixPadding(4);
IsBigEndian = isBigEndian;
return footerStartPos;
}
public void AddString(string offsetName, string str, uint offsetLength = 4)
{
if (string.IsNullOrEmpty(offsetName)) return;
if (string.IsNullOrEmpty(str))
{
WriteNulls(offsetLength);
return;
}
var tableEntry = new StringTableEntry(str);
bool newEntry = true;
// Make sure there aren't any existing entries of this string
foreach (var strEntry in strings)
{
if (strEntry.Data == str)
{
tableEntry = strEntry;
newEntry = false;
break;
}
}
// Add an offset to the string we're going to write into the string table later
AddOffset(offsetName, offsetLength);
tableEntry.OffsetNames.Add(offsetName);
if (newEntry)
strings.Add(tableEntry);
}
public override void FillInOffset(string name,
bool absolute = true, bool removeOffset = false)
{
base.FillInOffset(name, absolute, removeOffset);
}
public override void FillInOffset(string name, uint value,
bool absolute = true, bool removeOffset = false)
{
base.FillInOffset(name, value, absolute, removeOffset);
}
// Other
protected class StringTableEntry
{
// Variables/Constants
public List<string> OffsetNames = new List<string>();
public string Data;
// Constructors
public StringTableEntry() { }
public StringTableEntry(string data)
{
Data = data;
}
}
}
}