1 /*
2  *	HT Editor
3  *	htxbehead.cc
4  *
5  *	Copyright (C) 2003 Stefan Esser
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License version 2 as
9  *	published by the Free Software Foundation.
10  *
11  *	This program is distributed in the hope that it will be useful,
12  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *	GNU General Public License for more details.
15  *
16  *	You should have received a copy of the GNU General Public License
17  *	along with this program; if not, write to the Free Software
18  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include "formats.h"
22 #include "htapp.h"
23 #include "atom.h"
24 #include "htcoff.h"
25 #include "htctrl.h"
26 #include "endianess.h"
27 #include "hthex.h"
28 #include "htiobox.h"
29 #include "htnewexe.h"
30 #include "htxbe.h"
31 #include "htxbehead.h"
32 #include "httag.h"
33 #include "strtools.h"
34 #include "snprintf.h"
35 
36 #include "xbestruct.h"
37 
38 #include <string.h>
39 
40 static ht_mask_ptable xbemagic[] = {
41 	{"magic",						STATICTAG_EDIT_CHAR("00000000") STATICTAG_EDIT_CHAR("00000001") STATICTAG_EDIT_CHAR("00000002") STATICTAG_EDIT_CHAR("00000003")},
42 	{0, 0}
43 };
44 
45 static ht_tag_flags_s xbe_init_flags[] =
46 {
47 	{-1, "XBE - initialisation flags"},
48 	{0,  "[00] Mount Utility Drive"},
49 	{1,  "[01] Format Utility Drive"},
50 	{2,  "[02] Limit to 64MB"},
51 	{3,  "[03] Dont setup harddisk"},
52 	{0, 0}
53 };
54 
55 static ht_mask_ptable xbeimageheader[] = {
56 	{"base address",				STATICTAG_EDIT_DWORD_LE("00000104")},
57 	{"size of headers",			STATICTAG_EDIT_DWORD_LE("00000108")},
58 	{"size of image",			STATICTAG_EDIT_DWORD_LE("0000010c")},
59 	{"size of imageheader",				STATICTAG_EDIT_DWORD_LE("00000110")},
60 	{"timestamp",				STATICTAG_EDIT_DWORD_LE("00000114")},
61 	{"certificate address",				STATICTAG_EDIT_DWORD_LE("00000118")},
62 	{"number of sections",				STATICTAG_EDIT_DWORD_LE("0000011c")},
63 	{"section header address",					STATICTAG_EDIT_DWORD_LE("00000120")},
64 	{"initialisation flags",			STATICTAG_EDIT_DWORD_LE("00000124") " " STATICTAG_FLAGS("00000124", ATOM_XBE_INIT_FLAGS_STR)},
65 	{"entry point",					STATICTAG_EDIT_DWORD_LE("00000128")},
66 	{"TLS address",					STATICTAG_EDIT_DWORD_LE("0000012c")},
67 	{"PE stack commit",					STATICTAG_EDIT_DWORD_LE("00000130")},
68 	{"PE heap reserve",					STATICTAG_EDIT_DWORD_LE("00000134")},
69 	{"PE heap commit",					STATICTAG_EDIT_DWORD_LE("00000138")},
70 	{"PE base address",					STATICTAG_EDIT_DWORD_LE("0000013c")},
71 	{"PE size of image",					STATICTAG_EDIT_DWORD_LE("00000140")},
72 	{"PE checksum",					STATICTAG_EDIT_DWORD_LE("00000144")},
73 	{"PE timestamp",					STATICTAG_EDIT_DWORD_LE("00000148")},
74 	{"debug pathname address",					STATICTAG_EDIT_DWORD_LE("0000014c")},
75 	{"debug filename address",					STATICTAG_EDIT_DWORD_LE("00000150")},
76 	{"debug unicode filename address",					STATICTAG_EDIT_DWORD_LE("00000154")},
77 	{"kernel image thunk address",					STATICTAG_EDIT_DWORD_LE("00000158")},
78 	{"non-kernel import directoy address",					STATICTAG_EDIT_DWORD_LE("0000015c")},
79 	{"number of library versions",					STATICTAG_EDIT_DWORD_LE("00000160")},
80 	{"library versions address",					STATICTAG_EDIT_DWORD_LE("00000164")},
81 	{"kernel library version",					STATICTAG_EDIT_DWORD_LE("00000168")},
82 	{"xapi library version",					STATICTAG_EDIT_DWORD_LE("0000016c")},
83 	{"logo bitmap address",					STATICTAG_EDIT_DWORD_LE("00000170")},
84 	{"logo bitmap size",					STATICTAG_EDIT_DWORD_LE("00000174")},
85 	{0, 0}
86 };
87 
88 static ht_tag_flags_s xbe_media_flags[] =
89 {
90 	{-1, "XBE - allowed media flags"},
91 	{0,  "[00] Harddisk"},
92 	{1,  "[01] DVD X2"},
93 	{2,  "[02] DVD CD"},
94 	{3,  "[03] CD"},
95 	{4,  "[04] DVD-5 RO"},
96 	{5,  "[05] DVD-9 RO"},
97 	{6,  "[06] DVD-5 RW"},
98 	{7,  "[07] DVD-9 RW"},
99 	{8,  "[08] Dongle"},
100 	{9,  "[09] Mediaboard"},
101 	{30,  "[30] Nonsecure Harddisk"},
102 	{31,  "[31] Nonsecure Mode"},
103 	{0, 0}
104 };
105 
106 static ht_tag_flags_s xbe_region_codes[] =
107 {
108 	{ -1, "XBE - game region codes"},
109 	{ 0,  "[00] USA"},
110 	{ 1,  "[01] Japan"},
111 	{ 2,  "[02] Rest of World"},
112 	{ 31,  "[31] Manufacturing" },
113 	{0, 0}
114 };
115 
116 
117 static ht_mask_ptable xbecertificate[] = {
118 	{"certificate size",				STATICTAG_EDIT_DWORD_LE("00000000")},
119 	{"timestamp",				STATICTAG_EDIT_DWORD_LE("00000004")},
120 	{"title id",				STATICTAG_EDIT_DWORD_LE("00000008")},
121 	{"alternate title ids",				STATICTAG_EDIT_DWORD_LE("0000005c") "  " STATICTAG_EDIT_DWORD_LE("00000060") "  " STATICTAG_EDIT_DWORD_LE("00000064")},
122 	{"",				STATICTAG_EDIT_DWORD_LE("00000068") "  " STATICTAG_EDIT_DWORD_LE("0000006c") "  " STATICTAG_EDIT_DWORD_LE("00000070")},
123 	{"",				STATICTAG_EDIT_DWORD_LE("00000074") "  " STATICTAG_EDIT_DWORD_LE("00000078") "  " STATICTAG_EDIT_DWORD_LE("0000007c")},
124 	{"",				STATICTAG_EDIT_DWORD_LE("00000080") "  " STATICTAG_EDIT_DWORD_LE("00000084") "  " STATICTAG_EDIT_DWORD_LE("00000088")},
125 	{"",				STATICTAG_EDIT_DWORD_LE("0000008c") "  " STATICTAG_EDIT_DWORD_LE("00000090") "  " STATICTAG_EDIT_DWORD_LE("00000094")},
126 	{"",				STATICTAG_EDIT_DWORD_LE("00000098")},
127 	{"allowed media",			STATICTAG_EDIT_DWORD_LE("0000009c") " (" STATICTAG_FLAGS("0000009c", ATOM_XBE_MEDIA_FLAGS_STR) ")"},
128 	{"game region",				STATICTAG_EDIT_DWORD_LE("000000a0") " (" STATICTAG_FLAGS("000000a0", ATOM_XBE_REGION_STR) ")"},
129 	{"game ratings",				STATICTAG_EDIT_DWORD_LE("000000a4")},
130 	{"disk number",				STATICTAG_EDIT_DWORD_LE("000000a8")},
131 	{"version",				STATICTAG_EDIT_DWORD_LE("000000ac")},
132 	{"lan key",			STATICTAG_EDIT_BYTE("000000b0") STATICTAG_EDIT_BYTE("000000b1") STATICTAG_EDIT_BYTE("000000b2") STATICTAG_EDIT_BYTE("000000b3") STATICTAG_EDIT_BYTE("000000b4") STATICTAG_EDIT_BYTE("000000b5") STATICTAG_EDIT_BYTE("000000b6") STATICTAG_EDIT_BYTE("000000b7") STATICTAG_EDIT_BYTE("000000b8") STATICTAG_EDIT_BYTE("000000b9") STATICTAG_EDIT_BYTE("000000ba") STATICTAG_EDIT_BYTE("000000bb") STATICTAG_EDIT_BYTE("000000bc") STATICTAG_EDIT_BYTE("000000bd") STATICTAG_EDIT_BYTE("000000be") STATICTAG_EDIT_BYTE("000000bf")},
133 	{"signature key",			STATICTAG_EDIT_BYTE("000000c0") STATICTAG_EDIT_BYTE("000000c1") STATICTAG_EDIT_BYTE("000000c2") STATICTAG_EDIT_BYTE("000000c3") STATICTAG_EDIT_BYTE("000000c4") STATICTAG_EDIT_BYTE("000000c5") STATICTAG_EDIT_BYTE("000000c6") STATICTAG_EDIT_BYTE("000000c7") STATICTAG_EDIT_BYTE("000000c8") STATICTAG_EDIT_BYTE("000000c9") STATICTAG_EDIT_BYTE("000000ca") STATICTAG_EDIT_BYTE("000000cb") STATICTAG_EDIT_BYTE("000000cc") STATICTAG_EDIT_BYTE("000000cd") STATICTAG_EDIT_BYTE("000000ce") STATICTAG_EDIT_BYTE("000000cf")},
134 	{"alternate signature keys",			STATICTAG_EDIT_BYTE("000000d0") STATICTAG_EDIT_BYTE("000000d1") STATICTAG_EDIT_BYTE("000000d2") STATICTAG_EDIT_BYTE("000000d3") STATICTAG_EDIT_BYTE("000000d4") STATICTAG_EDIT_BYTE("000000d5") STATICTAG_EDIT_BYTE("000000d6") STATICTAG_EDIT_BYTE("000000d7") STATICTAG_EDIT_BYTE("000000d8") STATICTAG_EDIT_BYTE("000000d9") STATICTAG_EDIT_BYTE("000000da") STATICTAG_EDIT_BYTE("000000db") STATICTAG_EDIT_BYTE("000000dc") STATICTAG_EDIT_BYTE("000000dd") STATICTAG_EDIT_BYTE("000000de") STATICTAG_EDIT_BYTE("000000df")},
135 	{"",			STATICTAG_EDIT_BYTE("000000e0") STATICTAG_EDIT_BYTE("000000e1") STATICTAG_EDIT_BYTE("000000e2") STATICTAG_EDIT_BYTE("000000e3") STATICTAG_EDIT_BYTE("000000e4") STATICTAG_EDIT_BYTE("000000e5") STATICTAG_EDIT_BYTE("000000e6") STATICTAG_EDIT_BYTE("000000e7") STATICTAG_EDIT_BYTE("000000e8") STATICTAG_EDIT_BYTE("000000e9") STATICTAG_EDIT_BYTE("000000ea") STATICTAG_EDIT_BYTE("000000eb") STATICTAG_EDIT_BYTE("000000ec") STATICTAG_EDIT_BYTE("000000ed") STATICTAG_EDIT_BYTE("000000ee") STATICTAG_EDIT_BYTE("000000ef")},
136 	{"",			STATICTAG_EDIT_BYTE("000000f0") STATICTAG_EDIT_BYTE("000000f1") STATICTAG_EDIT_BYTE("000000f2") STATICTAG_EDIT_BYTE("000000f3") STATICTAG_EDIT_BYTE("000000f4") STATICTAG_EDIT_BYTE("000000f5") STATICTAG_EDIT_BYTE("000000f6") STATICTAG_EDIT_BYTE("000000f7") STATICTAG_EDIT_BYTE("000000f8") STATICTAG_EDIT_BYTE("000000f9") STATICTAG_EDIT_BYTE("000000fa") STATICTAG_EDIT_BYTE("000000fb") STATICTAG_EDIT_BYTE("000000fc") STATICTAG_EDIT_BYTE("000000fd") STATICTAG_EDIT_BYTE("000000fe") STATICTAG_EDIT_BYTE("000000ff")},
137 	{"",			STATICTAG_EDIT_BYTE("00000100") STATICTAG_EDIT_BYTE("00000101") STATICTAG_EDIT_BYTE("00000102") STATICTAG_EDIT_BYTE("00000103") STATICTAG_EDIT_BYTE("00000104") STATICTAG_EDIT_BYTE("00000105") STATICTAG_EDIT_BYTE("00000106") STATICTAG_EDIT_BYTE("00000107") STATICTAG_EDIT_BYTE("00000108") STATICTAG_EDIT_BYTE("00000109") STATICTAG_EDIT_BYTE("0000010a") STATICTAG_EDIT_BYTE("0000010b") STATICTAG_EDIT_BYTE("0000010c") STATICTAG_EDIT_BYTE("0000010d") STATICTAG_EDIT_BYTE("0000010e") STATICTAG_EDIT_BYTE("0000010f")},
138 	{"",			STATICTAG_EDIT_BYTE("00000110") STATICTAG_EDIT_BYTE("00000111") STATICTAG_EDIT_BYTE("00000112") STATICTAG_EDIT_BYTE("00000113") STATICTAG_EDIT_BYTE("00000114") STATICTAG_EDIT_BYTE("00000115") STATICTAG_EDIT_BYTE("00000116") STATICTAG_EDIT_BYTE("00000117") STATICTAG_EDIT_BYTE("00000118") STATICTAG_EDIT_BYTE("00000119") STATICTAG_EDIT_BYTE("0000011a") STATICTAG_EDIT_BYTE("0000011b") STATICTAG_EDIT_BYTE("0000011c") STATICTAG_EDIT_BYTE("0000011d") STATICTAG_EDIT_BYTE("0000011e") STATICTAG_EDIT_BYTE("0000011f")},
139 	{"",			STATICTAG_EDIT_BYTE("00000120") STATICTAG_EDIT_BYTE("00000121") STATICTAG_EDIT_BYTE("00000122") STATICTAG_EDIT_BYTE("00000123") STATICTAG_EDIT_BYTE("00000124") STATICTAG_EDIT_BYTE("00000125") STATICTAG_EDIT_BYTE("00000126") STATICTAG_EDIT_BYTE("00000127") STATICTAG_EDIT_BYTE("00000128") STATICTAG_EDIT_BYTE("00000129") STATICTAG_EDIT_BYTE("0000012a") STATICTAG_EDIT_BYTE("0000012b") STATICTAG_EDIT_BYTE("0000012c") STATICTAG_EDIT_BYTE("0000012d") STATICTAG_EDIT_BYTE("0000012e") STATICTAG_EDIT_BYTE("0000012f")},
140 	{"",			STATICTAG_EDIT_BYTE("00000130") STATICTAG_EDIT_BYTE("00000131") STATICTAG_EDIT_BYTE("00000132") STATICTAG_EDIT_BYTE("00000133") STATICTAG_EDIT_BYTE("00000134") STATICTAG_EDIT_BYTE("00000135") STATICTAG_EDIT_BYTE("00000136") STATICTAG_EDIT_BYTE("00000137") STATICTAG_EDIT_BYTE("00000138") STATICTAG_EDIT_BYTE("00000139") STATICTAG_EDIT_BYTE("0000013a") STATICTAG_EDIT_BYTE("0000013b") STATICTAG_EDIT_BYTE("0000013c") STATICTAG_EDIT_BYTE("0000013d") STATICTAG_EDIT_BYTE("0000013e") STATICTAG_EDIT_BYTE("0000013f")},
141 	{"",			STATICTAG_EDIT_BYTE("00000140") STATICTAG_EDIT_BYTE("00000141") STATICTAG_EDIT_BYTE("00000142") STATICTAG_EDIT_BYTE("00000143") STATICTAG_EDIT_BYTE("00000144") STATICTAG_EDIT_BYTE("00000145") STATICTAG_EDIT_BYTE("00000146") STATICTAG_EDIT_BYTE("00000147") STATICTAG_EDIT_BYTE("00000148") STATICTAG_EDIT_BYTE("00000149") STATICTAG_EDIT_BYTE("0000014a") STATICTAG_EDIT_BYTE("0000014b") STATICTAG_EDIT_BYTE("0000014c") STATICTAG_EDIT_BYTE("0000014d") STATICTAG_EDIT_BYTE("0000014e") STATICTAG_EDIT_BYTE("0000014f")},
142 	{"",			STATICTAG_EDIT_BYTE("00000150") STATICTAG_EDIT_BYTE("00000151") STATICTAG_EDIT_BYTE("00000152") STATICTAG_EDIT_BYTE("00000153") STATICTAG_EDIT_BYTE("00000154") STATICTAG_EDIT_BYTE("00000155") STATICTAG_EDIT_BYTE("00000156") STATICTAG_EDIT_BYTE("00000157") STATICTAG_EDIT_BYTE("00000158") STATICTAG_EDIT_BYTE("00000159") STATICTAG_EDIT_BYTE("0000015a") STATICTAG_EDIT_BYTE("0000015b") STATICTAG_EDIT_BYTE("0000015c") STATICTAG_EDIT_BYTE("0000015d") STATICTAG_EDIT_BYTE("0000015e") STATICTAG_EDIT_BYTE("0000015f")},
143 	{"",			STATICTAG_EDIT_BYTE("00000160") STATICTAG_EDIT_BYTE("00000161") STATICTAG_EDIT_BYTE("00000162") STATICTAG_EDIT_BYTE("00000163") STATICTAG_EDIT_BYTE("00000164") STATICTAG_EDIT_BYTE("00000165") STATICTAG_EDIT_BYTE("00000166") STATICTAG_EDIT_BYTE("00000167") STATICTAG_EDIT_BYTE("00000168") STATICTAG_EDIT_BYTE("00000169") STATICTAG_EDIT_BYTE("0000016a") STATICTAG_EDIT_BYTE("0000016b") STATICTAG_EDIT_BYTE("0000016c") STATICTAG_EDIT_BYTE("0000016d") STATICTAG_EDIT_BYTE("0000016e") STATICTAG_EDIT_BYTE("0000016f")},
144 	{"",			STATICTAG_EDIT_BYTE("00000170") STATICTAG_EDIT_BYTE("00000171") STATICTAG_EDIT_BYTE("00000172") STATICTAG_EDIT_BYTE("00000173") STATICTAG_EDIT_BYTE("00000174") STATICTAG_EDIT_BYTE("00000175") STATICTAG_EDIT_BYTE("00000176") STATICTAG_EDIT_BYTE("00000177") STATICTAG_EDIT_BYTE("00000178") STATICTAG_EDIT_BYTE("00000179") STATICTAG_EDIT_BYTE("0000017a") STATICTAG_EDIT_BYTE("0000017b") STATICTAG_EDIT_BYTE("0000017c") STATICTAG_EDIT_BYTE("0000017d") STATICTAG_EDIT_BYTE("0000017e") STATICTAG_EDIT_BYTE("0000017f")},
145 	{"",			STATICTAG_EDIT_BYTE("00000180") STATICTAG_EDIT_BYTE("00000181") STATICTAG_EDIT_BYTE("00000182") STATICTAG_EDIT_BYTE("00000183") STATICTAG_EDIT_BYTE("00000184") STATICTAG_EDIT_BYTE("00000185") STATICTAG_EDIT_BYTE("00000186") STATICTAG_EDIT_BYTE("00000187") STATICTAG_EDIT_BYTE("00000188") STATICTAG_EDIT_BYTE("00000189") STATICTAG_EDIT_BYTE("0000018a") STATICTAG_EDIT_BYTE("0000018b") STATICTAG_EDIT_BYTE("0000018c") STATICTAG_EDIT_BYTE("0000018d") STATICTAG_EDIT_BYTE("0000018e") STATICTAG_EDIT_BYTE("0000018f")},
146 	{"",			STATICTAG_EDIT_BYTE("00000190") STATICTAG_EDIT_BYTE("00000191") STATICTAG_EDIT_BYTE("00000192") STATICTAG_EDIT_BYTE("00000193") STATICTAG_EDIT_BYTE("00000194") STATICTAG_EDIT_BYTE("00000195") STATICTAG_EDIT_BYTE("00000196") STATICTAG_EDIT_BYTE("00000197") STATICTAG_EDIT_BYTE("00000198") STATICTAG_EDIT_BYTE("00000199") STATICTAG_EDIT_BYTE("0000019a") STATICTAG_EDIT_BYTE("0000019b") STATICTAG_EDIT_BYTE("0000019c") STATICTAG_EDIT_BYTE("0000019d") STATICTAG_EDIT_BYTE("0000019e") STATICTAG_EDIT_BYTE("0000019")},
147 	{"",			STATICTAG_EDIT_BYTE("000001a0") STATICTAG_EDIT_BYTE("000001a1") STATICTAG_EDIT_BYTE("000001a2") STATICTAG_EDIT_BYTE("000001a3") STATICTAG_EDIT_BYTE("000001a4") STATICTAG_EDIT_BYTE("000001a5") STATICTAG_EDIT_BYTE("000001a6") STATICTAG_EDIT_BYTE("000001a7") STATICTAG_EDIT_BYTE("000001a8") STATICTAG_EDIT_BYTE("000001a9") STATICTAG_EDIT_BYTE("000001aa") STATICTAG_EDIT_BYTE("000001ab") STATICTAG_EDIT_BYTE("000001ac") STATICTAG_EDIT_BYTE("000001ad") STATICTAG_EDIT_BYTE("000001ae") STATICTAG_EDIT_BYTE("000001af")},
148 	{"",			STATICTAG_EDIT_BYTE("000001b0") STATICTAG_EDIT_BYTE("000001b1") STATICTAG_EDIT_BYTE("000001b2") STATICTAG_EDIT_BYTE("000001b3") STATICTAG_EDIT_BYTE("000001b4") STATICTAG_EDIT_BYTE("000001b5") STATICTAG_EDIT_BYTE("000001b6") STATICTAG_EDIT_BYTE("000001b7") STATICTAG_EDIT_BYTE("000001b8") STATICTAG_EDIT_BYTE("000001b9") STATICTAG_EDIT_BYTE("000001ba") STATICTAG_EDIT_BYTE("000001bb") STATICTAG_EDIT_BYTE("000001bc") STATICTAG_EDIT_BYTE("000001bd") STATICTAG_EDIT_BYTE("000001be") STATICTAG_EDIT_BYTE("000001bf")},
149 	{"",			STATICTAG_EDIT_BYTE("000001c0") STATICTAG_EDIT_BYTE("000001c1") STATICTAG_EDIT_BYTE("000001c2") STATICTAG_EDIT_BYTE("000001c3") STATICTAG_EDIT_BYTE("000001c4") STATICTAG_EDIT_BYTE("000001c5") STATICTAG_EDIT_BYTE("000001c6") STATICTAG_EDIT_BYTE("000001c7") STATICTAG_EDIT_BYTE("000001c8") STATICTAG_EDIT_BYTE("000001c9") STATICTAG_EDIT_BYTE("000001ca") STATICTAG_EDIT_BYTE("000001cb") STATICTAG_EDIT_BYTE("000001cc") STATICTAG_EDIT_BYTE("000001cd") STATICTAG_EDIT_BYTE("000001ce") STATICTAG_EDIT_BYTE("000001cf")},
150 
151 	{0, 0}
152 };
153 
154 static ht_tag_flags_s xbe_section_flags[] =
155 {
156 	{-1, "XBE - section flags"},
157 	{0,  "[00] Writeable"},
158 	{1,  "[01] Preload"},
159 	{2,  "[02] Executable"},
160 	{3,  "[03] Inserted File"},
161 	{4,  "[04] Head Page Read Only"},
162 	{5,  "[05] Tail Page Read Only"},
163 	{0, 0}
164 };
165 
166 static ht_mask_ptable xbesectionheader[] = {
167 	{"section flags",				STATICTAG_EDIT_DWORD_LE("00000000") " " STATICTAG_FLAGS("00000000", ATOM_XBE_SECTION_FLAGS_STR)},
168 	{"virtual address",			STATICTAG_EDIT_DWORD_LE("00000004")},
169 	{"virtual size",			STATICTAG_EDIT_DWORD_LE("00000008")},
170 	{"raw address",			STATICTAG_EDIT_DWORD_LE("0000000c")},
171 	{"raw size",			STATICTAG_EDIT_DWORD_LE("00000010")},
172 	{"section name address",			STATICTAG_EDIT_DWORD_LE("00000014")},
173 	{"section name reference counter",			STATICTAG_EDIT_DWORD_LE("00000018")},
174 	{"head shared page reference counter address",		STATICTAG_EDIT_DWORD_LE("0000001c")},
175 	{"tail shared page reference counter address",			STATICTAG_EDIT_DWORD_LE("00000020")},
176 	{0, 0}
177 };
178 
179 static ht_tag_flags_s xbe_library_flags[] =
180 {
181 	{-1, "XBE - library flags"},
182 	{15,  "[15] Debug Build"},
183 	{0, 0}
184 };
185 
186 
187 static ht_mask_ptable xbelibraryversion[] = {
188 	{"library name",			STATICTAG_EDIT_CHAR("00000000") STATICTAG_EDIT_CHAR("00000001") STATICTAG_EDIT_CHAR("00000002") STATICTAG_EDIT_CHAR("00000003") STATICTAG_EDIT_CHAR("00000004")STATICTAG_EDIT_CHAR("00000005")STATICTAG_EDIT_CHAR("00000006")STATICTAG_EDIT_CHAR("00000007")},
189 	{"major version",			STATICTAG_EDIT_WORD_LE("00000008")},
190 	{"minor version",			STATICTAG_EDIT_WORD_LE("0000000a")},
191 	{"build version",			STATICTAG_EDIT_WORD_LE("0000000c")},
192 	{"library flags",			STATICTAG_EDIT_WORD_LE("0000000e") " " STATICTAG_FLAGS("0000000a", ATOM_XBE_LIBRARY_FLAGS_STR)},
193 	{0, 0}
194 };
195 
htxbeheader_init(Bounds * b,File * file,ht_format_group * group)196 static ht_view *htxbeheader_init(Bounds *b, File *file, ht_format_group *group)
197 {
198 	ht_xbe_shared_data *xbe_shared=(ht_xbe_shared_data *)group->get_shared_data();
199 
200 	ht_xbe_header_viewer *v = new ht_xbe_header_viewer();
201 	v->init(b, DESC_XBE_HEADER, VC_EDIT | VC_SEARCH, file, group);
202 
203 	registerAtom(ATOM_XBE_INIT_FLAGS, xbe_init_flags);
204 	registerAtom(ATOM_XBE_SECTION_FLAGS, xbe_section_flags);
205 	registerAtom(ATOM_XBE_MEDIA_FLAGS, xbe_media_flags);
206 	registerAtom(ATOM_XBE_REGION, xbe_region_codes);
207 	registerAtom(ATOM_XBE_LIBRARY_FLAGS, xbe_library_flags);
208 
209 	ht_mask_sub *s;
210 	ht_collapsable_sub *cs;
211 
212 	s = new ht_mask_sub();
213 	s->init(file, 0);
214 	char info[128];
215 	ht_snprintf(info, sizeof info, "* XBE header");
216 	s->add_mask(info);
217 	v->insertsub(s);
218 
219 	/* FIXME: */
220 	bool xbe_bigendian = false;
221 
222 	s = new ht_mask_sub();
223 	s->init(file, 1);
224 	s->add_staticmask_ptable(xbemagic, 0x0, xbe_bigendian);
225 
226 	/* image header */
227 	s->add_staticmask_ptable(xbeimageheader, 0x0, xbe_bigendian);
228 	cs = new ht_collapsable_sub();
229 	cs->init(file, s, 1, "image header", 1);
230 	v->insertsub(cs);
231 
232 	/* image header */
233 	s = new ht_mask_sub();
234 	s->init(file, 2);
235 	s->add_staticmask_ptable(xbecertificate, xbe_shared->header.certificate_address-xbe_shared->header.base_address, xbe_bigendian);
236 	cs = new ht_collapsable_sub();
237 	cs->init(file, s, 1, "certificate", 1);
238 	v->insertsub(cs);
239 
240 	/* library versions */
241 
242 	for (uint i=0; i < xbe_shared->header.number_of_library_versions; i++) {
243 		s = new ht_mask_sub();
244 		s->init(file, 50+i);
245 
246 		s->add_staticmask_ptable(xbelibraryversion, xbe_shared->header.library_versions_address-xbe_shared->header.base_address+i*sizeof *xbe_shared->libraries, xbe_bigendian);
247 
248 		char t[256];
249 		ht_snprintf(t, sizeof t, "library %d: %-9s %d.%d.%d", i, &xbe_shared->libraries[i].library_name, xbe_shared->libraries[i].major_version, xbe_shared->libraries[i].minor_version, xbe_shared->libraries[i].build_version);
250 
251 		cs=new ht_collapsable_sub();
252 		cs->init(file, s, 1, t, 1);
253 
254 		v->insertsub(cs);
255 	}
256 
257 	/* section headers */
258 
259 	for (uint i=0; i<xbe_shared->sections.number_of_sections; i++) {
260 		const char *name;
261 //		uint ofs;
262 
263 		s=new ht_mask_sub();
264 		s->init(file, 100+i);
265 
266 		s->add_staticmask_ptable(xbesectionheader, xbe_shared->header.section_header_address-xbe_shared->header.base_address+i*sizeof *xbe_shared->sections.sections, xbe_bigendian);
267 
268 		if (xbe_shared->sections.sections[i].section_name_address) {
269 
270 			name = (char *)xbe_shared->sections.sections[i].section_name_address;
271 
272 		} else {
273 			name = "<empty>";
274 		}
275 
276 		char t[256];
277 		ht_snprintf(t, sizeof t, "section header %d: %s - rva %08x vsize %08x", i, name, xbe_shared->sections.sections[i].virtual_address, xbe_shared->sections.sections[i].virtual_size);
278 
279 		cs=new ht_collapsable_sub();
280 		cs->init(file, s, 1, t, 1);
281 
282 		v->insertsub(cs);
283 	}
284 	return v;
285 }
286 
287 format_viewer_if htxbeheader_if = {
288 	htxbeheader_init,
289 	0
290 };
291 
292 /*
293  *	CLASS ht_pe_header_viewer
294  */
295 
init(Bounds * b,const char * desc,int caps,File * file,ht_format_group * group)296 void ht_xbe_header_viewer::init(Bounds *b, const char *desc, int caps, File *file, ht_format_group *group)
297 {
298 	ht_uformat_viewer::init(b, desc, caps, file, group);
299 	VIEW_DEBUG_NAME("ht_xbe_header_viewer");
300 }
301 
302 /*
303 static ht_format_viewer *find_hex_viewer(ht_group *group)
304 {
305 	// FIXME: God forgive us...
306 	ht_group *vr_group=group;
307 	while (strcmp(vr_group->desc, VIEWERGROUP_NAME)) vr_group=vr_group->group;
308 	ht_view *c=vr_group->getfirstchild();
309 	while (c) {
310 		if (c->desc && (strcmp(c->desc, DESC_HEX)==0)) {
311 			return (ht_format_viewer*)c;
312 		}
313 		c=c->next;
314 	}
315 	return NULL;
316 }
317 */
318 
ref_sel(LINE_ID * id)319 bool ht_xbe_header_viewer::ref_sel(LINE_ID *id)
320 {
321 	return true;
322 }
323