AndroidManifest.xml文件格式

AndroidManifest.xml文件格式

结构详解

xml文件头

1
2
3
4
5
typedef struct {
ushort type <format=hex>;
ushort head_size;
int size;
} axml_header;

4字节:魔数(类型标识+头部结构大小)
4字节:文件大小

字符串区块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
typedef struct {
axml_header header;
int string_count;
int style_count;
int flag;
int string_offset;
int style_offset;
if (string_count > 0) {
axml_string_offset string_offsets[string_count] <comment="String item">;
}

if (style_count > 0) {
uint style_offsets[style_count];
off = style_offset - string_offset;
} else {
off = header.size - string_offset;
}

FSkip(off);
} axml_strings;

字符串区块头(和xml头结构一致)
2字节:类型标识
2字节:头部大小
4字节:字符串区块大小

4字节:字符串数目
4字节:样式数目
4字节:缺省位置
4字节:字符串池的偏移量(字符串池起始距离字符串区块头起始位置的偏移量)
4字节:样式池的偏移量

string_count * 4字节:string_count个字符串偏移信息块
style_count * 4字节: style_count个样式偏移信息块

字符串偏移信息块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
typedef struct {
uint offset;

local int64 pos = FTell();
local int off = sizeof(file.header) + file.strings.string_offset + offset;
FSeek(g_offset + off);
// Printf("offset=%x\n", off);

local int isUtf8 = parentof(this).flag & (1<<8);
if (isUtf8)
axml_string8 astr;
else
axml_string16 astr;

FSeek(pos);
} axml_string_offset <read=AxmlStringRead, optimize=false>;

每个块只有4字节,记录对应字符串起始距离字符串池起始的偏移量
索引偏移信息块对应的字符串:
字符串位置=xml文件头大小+字符串池偏移量+对应字符串偏移信息(就是本块内的数据)
检查对应字符串是否是utf-8编码,解析结构不同

1
2
3
4
5
6
typedef struct {
axml_header header;

local int count = (header.size-8) / 4;
uint ids[count] <format=hex>;
} axml_resources;

资源区块头(和xml头结构一致)
2字节:类型标识
2字节:头部大小
4字节:资源区块大小

资源数量=(区块大小-8字节头部大小)/4(资源一项为4字节)
一个资源列表,资源为ID值,这些ID对应android中实际的资源。
一个ID值由PackageID、TypeID、EntryID组成。

CHUNK

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
typedef struct {
uint type <format=hex, read=AxmlTypeString, comment="Chunk Type">;
int chunk_size;
int linenumber;
int comment;

if (type == CHUNK_STARTTAG) {
int uri <read=AxmlStringById>;
int name <read=AxmlStringById>;
ushort attr_start;
ushort attr_size;
ushort attr_count;
ushort idIndex;
ushort classIndex;
ushort styleIndex;
if (attr_count > 0) {
axml_attr attrs[attr_count];
}
} else if (type == CHUNK_ENDTAG) {
int url;
int name <read=AxmlStringById>;
} else if (type == CHUNK_STARTNS) {
int prefix <read=AxmlStringById>;
int uri <read=AxmlStringById>;
} else if (type == CHUNK_ENDNS) {
int prefix <read=AxmlStringById>;
int uri <read=AxmlStringById>;
} else if (type == CHUNK_TEXT) {
int text;
int unknow;
}
} axml_node <read=AxmlNodeRead>;

Chunk通用结构:
4字节chunk类型
4字节chunk大小
4字节行号
4字节未定义区

接下来会根据不同chunk类型,相对应不同结构

NAMESPACE START

命名空间开端
4字节Prefix,命名空间前缀(在字符串池中的索引值)
4字节Uri,命名空间的URI(在字符串池中的索引值)

TAG START

1
2
3
4
5
6
7
8
int uri <read=AxmlStringById>;
int name <read=AxmlStringById>;
ushort attr_start;
ushort attr_size;
ushort attr_count;
ushort idIndex;
uint attr_class;
axml_attr attrs[attr_count];

标签开端
4字节Uri,标签对应的命名空间的URI(在字符串池中的索引值)
4字节Name,标签名称(在字符串池中的索引值)
2字节attr_start,标识为标签开端
2字节attr_size,标识为单条属性大小
2字节attr_count,该标签属性数目
2字节idIndex,不明
4字节attr_class,标签类属性
attr_count*attr_count字节,属性内容列表

1
2
3
4
5
6
7
typedef struct {
int url <read=AxmlStringById>;
int name <read=AxmlStringById>;
int str <read=AxmlStringById>;

axml_value value;
} axml_attr <read=AxmlAttrRead>;

4字节Uri,属性对应的命名空间的URI(在字符串池中的索引值)
4字节Name,属性名称(在字符串池中的索引值)
4字节str,属性值(在字符串池中的索引值)
8字节value包含data结构大小、data类型、data(在字符串池中的索引值)

完整template

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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

LittleEndian();

local int g_offset = 0;

enum {
RES_NULL_TYPE = 0x0000,
RES_STRING_POOL_TYPE = 0x0001,
RES_TABLE_TYPE = 0x0002,
RES_XML_TYPE = 0x0003,

// Chunk types in RES_XML_TYPE
RES_XML_FIRST_CHUNK_TYPE = 0x0100,
RES_XML_START_NAMESPACE_TYPE= 0x0100,
RES_XML_END_NAMESPACE_TYPE = 0x0101,
RES_XML_START_ELEMENT_TYPE = 0x0102,
RES_XML_END_ELEMENT_TYPE = 0x0103,
RES_XML_CDATA_TYPE = 0x0104,
RES_XML_LAST_CHUNK_TYPE = 0x017f,
// This contains a uint32_t array mapping strings in the string
// pool back to resource identifiers. It is optional.
RES_XML_RESOURCE_MAP_TYPE = 0x0180,

// Chunk types in RES_TABLE_TYPE
RES_TABLE_PACKAGE_TYPE = 0x0200,
RES_TABLE_TYPE_TYPE = 0x0201,
RES_TABLE_TYPE_SPEC_TYPE = 0x0202
};

typedef enum <uint> {
CHUNK_HEAD = 0x00080003,
CHUNK_STRING = 0x001c0001,
CHUNK_RESOURCE = 0x00080180,
CHUNK_STARTNS = 0x00100100,
CHUNK_ENDNS = 0x00100101,
CHUNK_STARTTAG = 0x00100102,
CHUNK_ENDTAG = 0x00100103,
CHUNK_TEXT = 0x00100104
} axml_type;

// DimemsionTable[8] = {"px", "dip", "sp", "pt", "in", "mm", "", ""};
typedef enum <uint> {
px = 0,
dip,
sp,
pt,
in,
mm
} axml_dimension;

local float RadixTable[8] = {0.00390625f, 3.051758E-005f, 1.192093E-007f, 4.656613E-010f};

/* attributes' types */
typedef enum<uint> {

ATTR_NULL = 0,
ATTR_REFERENCE = 1,
ATTR_ATTRIBUTE = 2,
ATTR_STRING = 3,
ATTR_FLOAT = 4,
ATTR_DIMENSION = 5,
ATTR_FRACTION = 6,

ATTR_FIRSTINT = 16,

ATTR_DEC = 16,
ATTR_HEX = 17,
ATTR_BOOLEAN = 18,

ATTR_FIRSTCOLOR = 28,
ATTR_ARGB8 = 28,
ATTR_RGB8 = 29,
ATTR_ARGB4 = 30,
ATTR_RGB4 = 31,
ATTR_LASTCOLOR = 31,

ATTR_LASTINT = 31,
} axml_attr_type;

typedef struct {
ushort type <format=hex>;
ushort head_size;
int size;
} axml_header;

typedef struct {
uchar len;
uchar enc_len;
if (len > 0) {
char data[len];
}
} axml_string8;

typedef struct {
ushort len;
if (len > 0) {
wchar_t data[len];
}
} axml_string16;

typedef struct {
uint offset;

local int64 pos = FTell();
local int off = sizeof(file.header) + file.strings.string_offset + offset;
FSeek(g_offset + off);
// Printf("offset=%x\n", off);

local int isUtf8 = parentof(this).flag & (1<<8);
if (isUtf8)
axml_string8 astr;
else
axml_string16 astr;

FSeek(pos);
} axml_string_offset <read=AxmlStringRead, optimize=false>;

string AxmlStringRead(axml_string_offset &o) {
if (o.astr.len > 0)
return o.astr.data;
else
return "NULL String";
}

typedef struct {
axml_header header;
int string_count;
int style_count;
int flag;
int string_offset;
int style_offset;
if (string_count > 0) {
axml_string_offset string_offsets[string_count] <comment="String item">;
}

if (style_count > 0) {
uint style_offsets[style_count];
off = style_offset - string_offset;
} else {
off = header.size - string_offset;
}

FSkip(off);
} axml_strings;

typedef struct {
axml_header header;

local int count = (header.size-8) / 4;
uint ids[count] <format=hex>;
} axml_resources;

typedef struct {
ushort size;
uchar res0;
uchar dateType <read=AxmlAttrTypeRead>;
uint data;
} axml_value;

typedef struct {
int url <read=AxmlStringById>;
int name <read=AxmlStringById>;
int str <read=AxmlStringById>;

axml_value value;
} axml_attr <read=AxmlAttrRead>;

string AxmlAttrTypeRead(uchar type) {
if (type == ATTR_NULL)
return EnumToString( ATTR_NULL );
else if (type == ATTR_REFERENCE)
return EnumToString( ATTR_REFERENCE );
else if (type == ATTR_ATTRIBUTE)
return EnumToString( ATTR_ATTRIBUTE );
else if (type == ATTR_STRING)
return EnumToString( ATTR_STRING );
else if (type == ATTR_FLOAT)
return EnumToString( ATTR_FLOAT );
else if (type == ATTR_DIMENSION)
return EnumToString( ATTR_DIMENSION );
else if (type == ATTR_FRACTION)
return EnumToString( ATTR_FRACTION );
else if (type == ATTR_DEC)
return EnumToString( ATTR_DEC );
else if (type == ATTR_HEX)
return EnumToString( ATTR_HEX );
else if (type == ATTR_BOOLEAN)
return EnumToString( ATTR_BOOLEAN );
else if (type == ATTR_ARGB8)
return EnumToString( ATTR_ARGB8 );
else if (type == ATTR_RGB8)
return EnumToString( ATTR_RGB8 );
else if (type == ATTR_ARGB4)
return EnumToString( ATTR_ARGB4 );
else if (type == ATTR_RGB4)
return EnumToString( ATTR_RGB4 );
else if (type >= ATTR_FIRSTCOLOR && type <= ATTR_LASTCOLOR4)
return "ATTR_COLOR";
else if (type >= ATTR_FIRSTINT && type <= ATTR_LASTINT)
return "ATTR_INT";
else
return "ATTR_UNKNOW";
}

string AxmlAttrRead(axml_attr &attr) {
local uchar type = attr.value.dateType;
string name = AxmlStringById(attr.name);
string buf = "Unknow";

if (type == ATTR_STRING) {
local string val = AxmlStringById(attr.str);
SPrintf(buf, "%s=%s", name, val);
} else if (type == ATTR_NULL) {
SPrintf(buf, "%s=NULL", name);
} else if (type == ATTR_REFERENCE) {
if (attr.value.data>>24 == 1) {
SPrintf(buf, "%s=@android:%08X", name, attr.value.data);
} else {
SPrintf(buf, "%s=@%08X", name, attr.value.data);
}
} else if (type == ATTR_ATTRIBUTE) {
if (attr.value.data>>24 == 1) {
SPrintf(buf, "%s=?android:%08X", name, attr.value.data);
} else {
SPrintf(buf, "%s=?%08X", name, attr.value.data);
}
} else if (type == ATTR_FLOAT) {
SPrintf(buf, "%s=%g", name, (float)attr.value.data);
} else if (type == ATTR_DIMENSION) {
SPrintf(buf, "%s=%f%s", name,
(float)(attr.value.data & 0xffffff00) * RadixTable[(attr.data >> 4) & 0x03],
DimemsionTable[attr.data & 0x0f] );
} else if (type == ATTR_FRACTION) {
SPrintf(buf, "%s=%f%s", name,
(float)(attr.value.data & 0xffffff00) * RadixTable[(attr.value.data >> 4) & 0x03],
FractionTable[attr.value.data & 0x0f] );
} else if (type == ATTR_HEX) {
SPrintf(buf, "%s=0x%08x", name, attr.value.data);
} else if (type == ATTR_BOOLEAN) {
if (attr.value.data == 0) {
SPrintf(buf, "%s=false", name);
} else {
SPrintf(buf, "%s=true", name);
}
} else if (type >= ATTR_FIRSTCOLOR && type <= ATTR_LASTCOLOR) {
SPrintf(buf, "%s=#%08x", name, attr.value.data);
} else if (type >= ATTR_FIRSTINT && type <= ATTR_LASTINT) {
SPrintf(buf, "%s=%d", name, attr.value.data);
} else {
SPrintf(buf, "%s=UNKNOW", name);
}

return buf;
}

typedef struct {
uint type <format=hex, read=AxmlTypeString, comment="Chunk Type">;
int chunk_size;
int linenumber;
int comment;

if (type == CHUNK_STARTTAG) {
int uri <read=AxmlStringById>;
int name <read=AxmlStringById>;
ushort attr_start;
ushort attr_size;
ushort attr_count;
ushort idIndex;
ushort classIndex;
ushort styleIndex;
if (attr_count > 0) {
axml_attr attrs[attr_count];
}
} else if (type == CHUNK_ENDTAG) {
int url;
int name <read=AxmlStringById>;
} else if (type == CHUNK_STARTNS) {
int prefix <read=AxmlStringById>;
int uri <read=AxmlStringById>;
} else if (type == CHUNK_ENDNS) {
int prefix <read=AxmlStringById>;
int uri <read=AxmlStringById>;
} else if (type == CHUNK_TEXT) {
int text;
int unknow;
}
} axml_node <read=AxmlNodeRead>;

string AxmlNodeRead(axml_node &node) {
string buf = AxmlTypeString(node.type);
if (node.type == CHUNK_STARTNS) {
SPrintf(buf, "<xmlns:%s>", AxmlStringById(node.prefix));
} else if (node.type == CHUNK_ENDNS) {
SPrintf(buf, "</xmlns:%s>", AxmlStringById(node.prefix));
} else if (node.type == CHUNK_STARTTAG) {
SPrintf(buf, "<%s>", AxmlStringById(node.name));
} else if (node.type == CHUNK_ENDTAG) {
SPrintf(buf, "</%s>", AxmlStringById(node.name));
}
return buf;
}

string AxmlTypeString(uint &type) {
if (type == CHUNK_HEAD)
return "TYPE_HEAD";
else if (type == CHUNK_STRING)
return "TYPE_STRINGS";
else if (type == CHUNK_RESOURCE)
return "TYPE_RESOURCES";
else if (type == CHUNK_STARTNS)
return "TYPE_NS_START";
else if (type == CHUNK_ENDNS)
return "TYPE_NS_END";
else if (type == CHUNK_STARTTAG)
return "TYPE_TAG_START";
else if (type == CHUNK_ENDTAG)
return "TYPE_TAG_END";
else if (type == CHUNK_TEXT)
return "TYPE_TEXT";
else
return "TYPE_UNKNOW";
}

struct FILE {
local int off;
local int total;

axml_header header;
axml_strings strings;
axml_resources resources;

total = 0;
do {
total += 1;
// Printf("total=%d\n", total);

axml_node node;
} while (!FEof());

} file;

string AxmlStringById(int &index) {
string ret = "";
if (index >= 0 && index < file.strings.string_count) {
return file.strings.string_offsets[index].astr.data;
} else {
SPrintf(ret, "NULL Out off strings[%d] range %d", file.strings.string_count, index);
return ret;
}
}