-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (134 loc) · 4.78 KB
/
Copy pathProgram.cs
File metadata and controls
167 lines (134 loc) · 4.78 KB
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
using NLog;
using NLog.Layouts;
using Tdf;
internal class Program
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static void Main(string[] args)
{
if (args.Length < 1)
{
Logger.Info("Usage: ./Taggi <EBOOT.elf>");
return;
}
uint baseAddr = 0x00010000;
string filePath = args[0];
if (!File.Exists(filePath))
{
Logger.Info("File not found: " + filePath);
return;
}
var layout = new SimpleLayout("${message:withexception=true}");
LogManager.Setup().LoadConfiguration(builder =>
{
builder.ForLogger()
.WriteToConsole(layout)
.WriteToFile("out.txt",layout);
});
byte[] fileBytes = File.ReadAllBytes(filePath);
Logger.Info($"Loaded {fileBytes.Length} bytes.");
Logger.Info(new string('=', 80));
Logger.Info("");
List<(int offset, Element element)> validElements = new();
for (int i = 0; i <= fileBytes.Length - 6; i++)
{
byte[] candidateBytes = new byte[6];
Array.Copy(fileBytes, i, candidateBytes, 0, 6);
Element element = new Element(candidateBytes);
if (element.IsSane())
{
if (IsValidElementStruct(fileBytes, i, element))
{
validElements.Add((i, element));
}
}
}
foreach (var (offset, element) in validElements)
{
uint address = baseAddr + (uint)offset;
Logger.Info($"[0x{address:X8}] Tag: {element.Tag,-4} | Type: {element.DataType + " (0x" + BitConverter.ToString(new byte[1] { element.TypeByte }) + ")",-25} | Next: {element.BytesToNextElement}");
if (element.BytesToNextElement == 0)
{
Logger.Info(new string('=', 80));
Logger.Info("");
}
}
Logger.Info(new string('=', 80));
Logger.Info($"Found {validElements.Count} valid elements.");
Logger.Info($"Program ended.");
}
private static bool IsValidElementStruct(byte[] fileBytes, int currentOffset, Element currentElement)
{
if (currentElement.BytesToNextElement == 0)
return true;
int nextElementOffset = currentOffset + currentElement.BytesToNextElement;
if (nextElementOffset + 6 > fileBytes.Length)
return false;
byte[] nextBytes = new byte[6];
Array.Copy(fileBytes, nextElementOffset, nextBytes, 0, 6);
Element nextElement = new Element(nextBytes);
return nextElement.IsSane();
}
public class Element
{
public readonly byte[] TagBytes = new byte[3];
public readonly byte SeparatorByte;
public readonly byte TypeByte;
public readonly byte BytesToNextElement;
public readonly string Tag;
public readonly DataType DataType;
public Element(byte[] bytes)
{
if (bytes.Length != 6)
throw new ArgumentException("Expecting 6 bytes.");
TagBytes[0] = bytes[0];
TagBytes[1] = bytes[1];
TagBytes[2] = bytes[2];
SeparatorByte = bytes[3];
TypeByte = bytes[4];
BytesToNextElement = bytes[5];
DataType = (DataType)TypeByte;
Tag = new TdfMember(TagBytes).ToString();
}
public bool IsSane()
{
if (Tag.Length <= 1 || Tag.Length > 4) //Tag possibly could be 1 letter, but its quite rare, and it produces a lot of false results here.
return false;
foreach (char c in Tag) //Check if characters are from the alphabet A-Z
{
if (c < 'A' || c > 'Z')
return false;
}
if (SeparatorByte != 0x00 && SeparatorByte != 0x01) //Usually its 0x00, rare cases 0x01.
return false;
// if (!Enum.IsDefined(typeof(DataType), TypeByte)) We haven't figured all type bytes so we will not filter with this yet.
// return false;
return true;
}
public bool HasNext() => BytesToNextElement != 0;
}
public enum DataType : byte
{
SORTED_DICTIONARY = 0x00,
LIST = 0x01,
FLOAT = 0x02,
BOOLEAN = 0x0F,
STRING = 0x04,
MAYBE_BLAZE_OBJECT = 0x06,
STRUCT = 0x03,
STRUCT2 = 0x0A,
BYTEARRAY = 0x08,
UNION = 0x09,
BYTE2 = 0x10,
BYTE = 0x11,
USHORT_SHORT = 0x13,
INT_UINT = 0x14,
UINT_INT = 0x15,
MAYBE_LONG = 0x16,
ULONG = 0x17,
LONG = 0x18,
BLAZE_OBJECT_ID = 0x0C,
ENUM2 = 0x0E,
ENUM = 0x07,
}
}