Just An Application

September 27, 2011

Android Internals: Binary XML – Part Eight: The CDATA Chunk

1.0 Example

There are no CDATA chunks in the main.xml layout example and by default it is unlikely that the Binary XML version of any layout will contain any unless you go out of your way to do something like this.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        This is some character data
    
        <![CDATA[This is some more character data]]>
        <![CDATA[<tag>As is this</tag>]]>
    </LinearLayout>

which will get you this

    0000000 03 00 08 00 cc 01 00 00 01 00 1c 00 04 01 00 00
    0000010 07 00 00 00 00 00 00 00 00 01 00 00 38 00 00 00
    0000020 00 00 00 00 00 00 00 00 0f 00 00 00 1f 00 00 00
    0000030 29 00 00 00 56 00 00 00 59 00 00 00 68 00 00 00
    0000040 0c 0c 6c 61 79 6f 75 74 5f 77 69 64 74 68 00 0d
    0000050 0d 6c 61 79 6f 75 74 5f 68 65 69 67 68 74 00 07
    0000060 07 61 6e 64 72 6f 69 64 00 2a 2a 68 74 74 70 3a
    0000070 2f 2f 73 63 68 65 6d 61 73 2e 61 6e 64 72 6f 69
    0000080 64 2e 63 6f 6d 2f 61 70 6b 2f 72 65 73 2f 61 6e
    0000090 64 72 6f 69 64 00 00 00 00 0c 0c 4c 69 6e 65 61
    00000a0 72 4c 61 79 6f 75 74 00 61 61 20 54 68 69 73 20
    00000b0 69 73 20 73 6f 6d 65 20 63 68 61 72 61 63 74 65
    00000c0 72 20 64 61 74 61 0a 20 20 20 20 0a 20 20 20 20
    00000d0 54 68 69 73 20 69 73 20 73 6f 6d 65 20 6d 6f 72
    00000e0 65 20 63 68 61 72 61 63 74 65 72 20 64 61 74 61
    00000f0 0a 20 20 20 20 3c 74 61 67 3e 41 73 20 69 73 20
    0000100 74 68 69 73 3c 2f 74 61 67 3e 0a 00 80 01 08 00
    0000110 10 00 00 00 f4 00 01 01 f5 00 01 01 00 01 10 00
    0000120 18 00 00 00 02 00 00 00 ff ff ff ff 02 00 00 00
    0000130 03 00 00 00 02 01 10 00 4c 00 00 00 02 00 00 00
    0000140 ff ff ff ff ff ff ff ff 05 00 00 00 14 00 14 00
    0000150 02 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00
    0000160 ff ff ff ff 08 00 00 10 ff ff ff ff 03 00 00 00
    0000170 01 00 00 00 ff ff ff ff 08 00 00 10 ff ff ff ff
    0000180 04 01 10 00 1c 00 00 00 05 00 00 00 ff ff ff ff
    0000190 06 00 00 00 08 00 00 00 00 00 00 00 03 01 10 00
    00001a0 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    00001b0 05 00 00 00 01 01 10 00 18 00 00 00 0b 00 00 00
    00001c0 ff ff ff ff 02 00 00 00 03 00 00 00    

The bytes in blue are the chunk header and those in green the chunk body.

2.0 Chunk Header

The chunk header is an instance of struct ResXMLTree_node.

The header.type field is 0x0104 (RES_XML_CDATA_TYPE).

3.0 Chunk Body

The chunk body is an instance of this C++ struct (see frameworks/base/include/ResourceTypes.h lines 520-527)

    struct ResXMLTree_cdataExt
    {
        // The raw CDATA character data.
        struct ResStringPool_ref data;
    
        // The typed value of the character data if this is a CDATA node.
        struct Res_value typedData;
    };

3.1 data

The data field specifies the raw character data as in index into the StringPool chunk of the containing XML chunk.

3.2 typedData

The typedData field specifies the value of the character data contained in this chunk.

It is not clear in what circumstances this field is going to contain an actual value and how that value is derived from the character data and there are no clues in the source code.

4.0 The Example Annotated

This is an annotated version of full Binary XML file shown above

    00000000 03 00       // type [XML]
    00000002 08 00       // header size
    00000004 cc 01 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000008 01 00       // type [STRING_POOL]
    0000000a 1c 00       // header size
    0000000c 04 01 00 00 // chunk size
    --------------------

    00000010 07 00 00 00 // stringCount
    00000014 00 00 00 00 // styleCount
    00000018 00 01 00 00 // flags
    0000001c 38 00 00 00 // stringsStart (address 00000040)
    00000020 00 00 00 00 // stylesStart  (address 00000008)
    ++++++++++++++++++++

    00000024 00 00 00 00 // string[0]
    00000028 0f 00 00 00 // string[1]
    0000002c 1f 00 00 00 // string[2]
    00000030 29 00 00 00 // string[3]
    00000034 56 00 00 00 // string[4]
    00000038 59 00 00 00 // string[5]
    0000003c 68 00 00 00 // string[6]

    00000040 0c 0c 6c 61 // [0] "layout_width"
    00000044 79 6f 75 74
    00000048 5f 77 69 64
    0000004c 74 68 00 0d // [1] "layout_height"
    00000050 0d 6c 61 79
    00000054 6f 75 74 5f
    00000058 68 65 69 67
    0000005c 68 74 00 07 // [2] "android"
    00000060 07 61 6e 64
    00000064 72 6f 69 64
    00000068 00 2a 2a 68 // [3] "http://schemas.android.com/apk/res/android"
    0000006c 74 74 70 3a
    00000070 2f 2f 73 63
    00000074 68 65 6d 61
    00000078 73 2e 61 6e
    0000007c 64 72 6f 69
    00000080 64 2e 63 6f
    00000084 6d 2f 61 70
    00000088 6b 2f 72 65
    0000008c 73 2f 61 6e
    00000090 64 72 6f 69
    00000094 64 00 00 00 // [4] ""
    00000098 00 0c 0c 4c // [5] "LinearLayout"
    0000009c 69 6e 65 61
    000000a0 72 4c 61 79
    000000a4 6f 75 74 00
    000000a8 61 61 20 54 // [6] " This is some character data
    
        This is some more character data
        <tag>As is this</tag>
    "
    000000ac 68 69 73 20
    000000b0 69 73 20 73
    000000b4 6f 6d 65 20
    000000b8 63 68 61 72
    000000bc 61 63 74 65
    000000c0 72 20 64 61
    000000c4 74 61 0a 20
    000000c8 20 20 20 0a
    000000cc 20 20 20 20
    000000d0 54 68 69 73
    000000d4 20 69 73 20
    000000d8 73 6f 6d 65
    000000dc 20 6d 6f 72
    000000e0 65 20 63 68
    000000e4 61 72 61 63
    000000e8 74 65 72 20
    000000ec 64 61 74 61
    000000f0 0a 20 20 20
    000000f4 20 3c 74 61
    000000f8 67 3e 41 73
    000000fc 20 69 73 20
    00000100 74 68 69 73
    00000104 3c 2f 74 61
    00000108 67 3e 0a 00
    ==================== [End of STRING_POOL]

    0000010c 80 01       // type [XML_RESOURCE_MAP]
    0000010e 08 00       // header size
    00000110 10 00 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000114 f4 00 01 01 // [0]
    00000118 f5 00 01 01 // [1]
    ==================== [End of XML_RESOURCE_MAP]

    0000011c 00 01       // type [XML_START_NAMESPACE]
    0000011e 10 00       // header size
    00000120 18 00 00 00 // chunk size
    --------------------

    00000124 02 00 00 00 // lineNumber
    00000128 ff ff ff ff // comment
    ++++++++++++++++++++

    0000012c 02 00 00 00 // prefix
    00000130 03 00 00 00 // uri
    ==================== [End of XML_START_NAMESPACE]

    00000134 02 01       // type [XML_START_ELEMENT]
    00000136 10 00       // header size
    00000138 4c 00 00 00 // chunk size
    --------------------

    0000013c 02 00 00 00 // lineNumber
    00000140 ff ff ff ff // comment
    ++++++++++++++++++++

    00000144 ff ff ff ff // ns
    00000148 05 00 00 00 // name [LinearLayout]
    0000014c 14 00       // attributeStart
    0000014e 14 00       // attributeSize
    00000150 02 00       // attributeCount
    00000152 00 00       // idIndex
    00000154 00 00       // classIndex
    00000156 00 00       // styleIndex
    00000158 03 00 00 00 // attribute[0] ns
    0000015c 00 00 00 00 // attribute[0] name [layout_width]
    00000160 ff ff ff ff // attribute[0] rawValue
    00000164 08 00       // size
    00000166 00          // 0
    00000167 10          // dataType
    00000168 ff ff ff ff // data
    0000016c 03 00 00 00 // attribute[1] ns
    00000170 01 00 00 00 // attribute[1] name [layout_height]
    00000174 ff ff ff ff // attribute[1] rawValue
    00000178 08 00       // size
    0000017a 00          // 0
    0000017b 10          // dataType
    0000017c ff ff ff ff // data
    ==================== [End of XML_START_ELEMENT]

    00000180 04 01       // type [XML_CDATA]
    00000182 10 00       // header size
    00000184 1c 00 00 00 // chunk size
    --------------------

    00000188 05 00 00 00 // lineNumber
    0000018c ff ff ff ff // comment
    ++++++++++++++++++++

    00000190 06 00 00 00 // data
    00000194 08 00       // size
    00000196 00          // 0
    00000197 00          // dataType
    00000198 00 00 00 00 // data
    ==================== [End of XML_CDATA]

    0000019c 03 01       // type [XML_END_ELEMENT]
    0000019e 10 00       // header size
    000001a0 18 00 00 00 // chunk size
    --------------------

    000001a4 0b 00 00 00 // lineNumber
    000001a8 ff ff ff ff // comment
    ++++++++++++++++++++

    000001ac ff ff ff ff // ns
    000001b0 05 00 00 00 // name [LinearLayout]
    ==================== [End of XML_END_ELEMENT]

    000001b4 01 01       // type [XML_END_NAMESPACE]
    000001b6 10 00       // header size
    000001b8 18 00 00 00 // chunk size
    --------------------

    000001bc 0b 00 00 00 // lineNumber
    000001c0 ff ff ff ff // comment
    ++++++++++++++++++++

    000001c4 02 00 00 00 // prefix
    000001c8 03 00 00 00 // uri
    ==================== [End of XML_END_NAMESPACE]

    ==================== [End of XML]

Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

September 26, 2011

Android Internals: Binary XML – Part Seven: The XML End Element Chunk

1.0 The Example

The example contains two EndElement chunks representing the end tags of the TextView and LinearLayout elements

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk headers and those in green the chunk bodies.

2.0 Chunk Header

The chunk header is an instance of struct ResXMLTree_node.

The header.type field is 0x0103 (RES_XML_END_ELEMENT_TYPE).

3.0 Chunk Body

The chunk body is an instance of the following C++ struct (see frameworks/base/include/ResourceTypes.h lines 546-554)

    struct ResXMLTree_endElementExt
    {
        // String of the full namespace of this element.
        struct ResStringPool_ref ns;
    
        // String name of this node if it is an ELEMENT; the raw
        // character data if this is a CDATA node.
        struct ResStringPool_ref name;
    };

3.1 ns

The ns specifies the namespace name (the Namespace URI) of this element as an index into the StringPool of the containing XML chunk.

If the element has no namespace name the value of this field is -1 (0xFFFFFFFF).

3.2 name

The name field specifies the local name of the element as an index into the StringPool of the containing XML chunk.

4.0 The Example Annotated

This is an annotated version of the EndElement chunk which represents the end tag of the LinearLayout element in the example.

    ...

    00000294 03 01       // type [XML_END_ELEMENT]
    00000296 10 00       // header size
    00000298 18 00 00 00 // chunk size
    --------------------

    0000029c 0c 00 00 00 // lineNumber
    000002a0 ff ff ff ff // comment
    ++++++++++++++++++++

    000002a4 ff ff ff ff // ns
    000002a8 07 00 00 00 // name [LinearLayout]
    ==================== [End of XML_END_ELEMENT]
	
    ...

Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

September 25, 2011

Android Internals: Binary XML – Part Six: The XML Start Element Chunk

1.0 The Example

The example contains two StartElement chunks representing the start tags of the LinearLayout and TextView elements

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk headers and those in green the chunk bodies.

2.0 Chunk Header

The chunk header is an instance of struct ResXMLTree_node.

The header.type field is 0x0102 (RES_XML_START_ELEMENT_TYPE)

3.0 Chunk Body

The chunk body comprises an instance of the C++ struct ResXMLTree_attrExt followed by zero or more Attributes.

3.1 struct ResXMLTree_attrExt

The struct ResXMLTree_attrExt is defined as follows (see frameworks/base/include/ResourceTypes.h lines 561-589)

    struct ResXMLTree_attrExt
    {
        // String of the full namespace of this element.
        struct ResStringPool_ref ns;
    
        // String name of this node if it is an ELEMENT; the raw
        // character data if this is a CDATA node.
        struct ResStringPool_ref name;
    
        // Byte offset from the start of this structure where the attributes start.
        uint16_t attributeStart;
    
        // Size of the ResXMLTree_attribute structures that follow.
        uint16_t attributeSize;
    
        // Number of attributes associated with an ELEMENT.  These are
        // available as an array of ResXMLTree_attribute structures
        // immediately following this node.
        uint16_t attributeCount;
    
        // Index (1-based) of the "id" attribute. 0 if none.
        uint16_t idIndex;
    
        // Index (1-based) of the "class" attribute. 0 if none.
        uint16_t classIndex;
    
        // Index (1-based) of the "style" attribute. 0 if none.
        uint16_t styleIndex;
    };

3.1.1 ns

The ns specifies the namespace name (the Namespace URI) of this element as an index into the StringPool of the containing XML chunk.

If the element has no namespace name the value of this field is -1.

3.1.2 name

The name field specifies the local name of this element as an index into the StringPool of the containing XML chunk.

3.1.3 attributeStart

The attributeStart field specifies the start of the attribute data within the chunk body as an offset from the start of the chunk body.

Since this structure is at the start of the chunk body and is of fixed-size the value of this field is fixed at 20 (0×0014).

3.1.4 attributeSize

The attributeSize field specifies the size in bytes of the structure used to represent an Attribute.

This value is fixed and coincidentally is the same as that of the attributeStart field preceding it, that is, 20 (0×0014).

3.1.5 attributeCount

The attributeCount field specifies the number of Attributes that follow.

3.1.6 idIndex

The idIndex field specifies the Attribute if any which represents the id attribute in the original XML.

3.1.7 classIndex

The classIndex field specifies the Attribute if any which represents the class attribute in the original XML.

3.1.8 styleIndex

The styleIndex field specifies the Attribute if any which represents the style attribute in the original XML.

3.2 Attributes

An Attribute is represented by an instance of this C++ struct (see frameworks/base/include/ResourceTypes.h lines 591-604)

    struct ResXMLTree_attribute
    {
        // Namespace of this attribute.
        struct ResStringPool_ref ns;
    
        // Name of this attribute.
        struct ResStringPool_ref name;

        // The original raw string value of this attribute.
        struct ResStringPool_ref rawValue;
    
        // Processesd typed value of this attribute.
        struct Res_value typedValue;
    };

3.2.1 ns

The ns field specifies namespace name (the Namespace URI) of the attribute as an index into the StringPool of the containing XML chunk.

3.2.2 name

The name field specifies the local name of this Attribute as an index into the StringPool of the containing XML chunk.

Note that the value of this field also functions as an index into the Resource Id array of the XMLResourceMap chunk if it is present.

3.2.3 rawValue

The rawValue field specifies the value of this Attribute as it appeared in the original XML as an index into the StringPool of the containing XML chunk.

If the original value is not available it has the value -1.

3.2.4 typedValue

The typedValue is an instance of struct Res_value which specifies the value of the Attribute.

4.0 The Example Annotated

This is an annotated version of the StartElement chunk which represents the start tag of the LinearLayout element in the example.

    ...

    000001bc 02 01       // type [XML_START_ELEMENT]
    000001be 10 00       // header size
    000001c0 60 00 00 00 // chunk size
    --------------------

    000001c4 02 00 00 00 // lineNumber
    000001c8 ff ff ff ff // comment
    ++++++++++++++++++++

    000001cc ff ff ff ff // ns
    000001d0 07 00 00 00 // name [LinearLayout]
    000001d4 14 00       // attributeStart
    000001d6 14 00       // attributeSize
    000001d8 03 00       // attributeCount
    000001da 00 00       // idIndex
    000001dc 00 00       // classIndex
    000001de 00 00       // styleIndex
    000001e0 05 00 00 00 // attribute[0] ns
    000001e4 00 00 00 00 // attribute[0] name [orientation]
    000001e8 ff ff ff ff // attribute[0] rawValue
    000001ec 08 00       // size
    000001ee 00          // 0
    000001ef 10          // dataType
    000001f0 01 00 00 00 // data
    000001f4 05 00 00 00 // attribute[1] ns
    000001f8 01 00 00 00 // attribute[1] name [layout_width]
    000001fc ff ff ff ff // attribute[1] rawValue
    00000200 08 00       // size
    00000202 00          // 0
    00000203 10          // dataType
    00000204 ff ff ff ff // data
    00000208 05 00 00 00 // attribute[2] ns
    0000020c 02 00 00 00 // attribute[2] name [layout_height]
    00000210 ff ff ff ff // attribute[2] rawValue
    00000214 08 00       // size
    00000216 00          // 0
    00000217 10          // dataType
    00000218 ff ff ff ff // data
    ==================== [End of XML_START_ELEMENT]
	
    ...

Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

September 24, 2011

Android Internals: Binary XML – Part Five: The XML Start And End Namespace Chunks

1.0 The Example

The example contains a single pair of Start/End Namespace chunks

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk headers and those in green the chunk bodies.

The Start Namespace chunk starts at 00001a4 and the End Namespace chunk at 00002ac.

2.0 Start And End Namespace Chunks

Start and end namespace chunks represent the scope of a namespace declaration which occurs in the start tag of an element.

If there are multiple declarations in the same start tag then the start namespace chunks for the namespaces appear in the order of the declarations and the end namespace chunks in reverse order.

The chunks which represent the start and end of namespace scopes have the same structure.

3.0 Chunk Header

The chunk header is an instance of the struct ResXMLTree_node.

The header.type field is

The header.headerSize is always 16 (0x0010).

The header.size is always 24 (0x0018).

4.0 Chunk Body

The chunk body is an instance of the following C++ struct (see frameworks/base/include/ResourceTypes.h lines 533-540)

    struct ResXMLTree_namespaceExt
    {
        // The prefix of the namespace.
        struct ResStringPool_ref prefix;
    
        // The URI of the namespace.
        struct ResStringPool_ref uri;
    };

4.1 prefix

The prefix field specifies the XML Namespace prefix as an index into the StringPool of the containing XML chunk.

4.1 uri

The uri field specifies the XML Namespace URI as an index into the StringPool of the containing XML chunk.

5.0 The Example Annotated

5.1 The Start Namespace Chunk

This is an annotated version of the Start Namespace chunk from the example.

    ...

    000001a4 00 01       // type [XML_START_NAMESPACE]
    000001a6 10 00       // header size
    000001a8 18 00 00 00 // chunk size
    --------------------

    000001ac 02 00 00 00 // lineNumber
    000001b0 ff ff ff ff // comment
    ++++++++++++++++++++

    000001b4 04 00 00 00 // prefix
    000001b8 05 00 00 00 // uri
    ==================== [End of XML_START_NAMESPACE]
	
    ...

5.2 The End Namespace Chunk

This is an annotated version of the End Namespace chunk from the example.

    ...

    000002ac 01 01       // type [XML_END_NAMESPACE]
    000002ae 10 00       // header size
    000002b0 18 00 00 00 // chunk size
    --------------------

    000002b4 0c 00 00 00 // lineNumber
    000002b8 ff ff ff ff // comment
    ++++++++++++++++++++

    000002bc 04 00 00 00 // prefix
    000002c0 05 00 00 00 // uri
    ==================== [End of XML_END_NAMESPACE]
	
    ...

Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

September 23, 2011

Android Internals: Binary XML – Part Four: The XML Resource Map Chunk

1.0 The Example

The example contains a single XMLResourceMap chunk.

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk header and those in green the chunk body.

2.0 Chunk Header

The chunk header is an instance of struct ResChunk_header.

The header.type field is 0x0180 (RES_XML_RESOURCE_MAP_TYPE).

3.0 Chunk Body

The chunk body is an array of Resource Ids.

The element at index i is the Resource Id of the Attribute named by the string at index i in the StringPool chunk of the containing XML chunk.

4.0 The Example Annotated

This is an annotated version of the XMLResourceMap chunk from the example.

    ...
    
    0000018c 80 01       // type [XML_RESOURCE_MAP]
    0000018e 08 00       // header size
    00000190 18 00 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000194 c4 00 01 01 // [0]
    00000198 f4 00 01 01 // [1]
    0000019c f5 00 01 01 // [2]
    000001a0 4f 01 01 01 // [3]
    ==================== [End of XML_RESOURCE_MAP]
	
    ...

The specified Resource Ids are

  • 0x010100c4

  • 0x010100f4

  • 0x010100f5

  • 0x0101014f

The first four strings in the StringPool are

  • orientation

  • layout_width

  • layout_height

  • text

Checking the documentation shows that the Resource Ids of the attributes

  • R.attr.orientation

  • R.attr.layout_width

  • R.attr.layout_height

  • R.attr.text

in the android package are indeed those specified in the chunk body.


Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

September 22, 2011

Android Internals: Binary XML – Part Three: XML Node

The headers of the chunks representing the start and end of namespace scopes, the start and end of elements, and CDATA are all instances of the following
C++ struct (see frameworks/base/include/ResourceTypes.h lines 505-514)

    struct ResXMLTree_node
    {
        struct ResChunk_header header;

        // Line number in original source file at which this element appeared.
        uint32_t lineNumber;

        // Optional XML comment that was associated with this element; -1 if none.
        struct ResStringPool_ref comment;
    };

Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

Android Internals: Binary XML – Part Two: The XML Chunk

1.0 The Example

The example contains a single XML chunk which represents the original XML document.

    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

The bytes in blue are the chunk header and those in green, that is everything else, the chunk body.

2.0 Chunk Header

The chunk header is an instance of the following C++ struct (see frameworks/base/include/ResourceTypes.h lines 496-499)

    struct ResXMLTree_header
    {
        struct ResChunk_header header;
    };

which is simply a struct ResChunk_header.

The header.type is always 0×0003 (RES_XML_TYPE).

3.0 Chunk Body

The chunk body comprises

  • a StringPool chunk

  • an optional XMLResourceMap chunk

  • the chunks representing the elements of the original XML document

The XML document is typically represented as follows

  • a StartNamespace chunk

  • a StartElement chunk representing the start tag of the root element

  • zero or more chunks representing the body of the root element

  • an EndElement chunk representing the end tag of the root element

  • an EndNamespace chunk

4.0 The Example Annotated

This is an annotated version of the chunk header and the following StringPool chunk from the example.


    00000000 03 00       // type [XML]
    00000002 08 00       // header size
    00000004 c4 02 00 00 // chunk size
    --------------------

    ++++++++++++++++++++

    00000008 01 00       // type [STRING_POOL]
    0000000a 1c 00       // header size
    0000000c 84 01 00 00 // chunk size
    --------------------

    00000010 0a 00 00 00 // stringCount
    00000014 00 00 00 00 // styleCount
    00000018 00 00 00 00 // flags
    0000001c 44 00 00 00 // stringsStart (address 0000004c)
    00000020 00 00 00 00 // stylesStart  (address 00000008)
    ++++++++++++++++++++

    00000024 00 00 00 00 // string[0]
    00000028 1a 00 00 00 // string[1]
    0000002c 36 00 00 00 // string[2]
    00000030 54 00 00 00 // string[3]
    00000034 60 00 00 00 // string[4]
    00000038 72 00 00 00 // string[5]
    0000003c ca 00 00 00 // string[6]
    00000040 ce 00 00 00 // string[7]
    00000044 ea 00 00 00 // string[8]
    00000048 fe 00 00 00 // string[9]

    0000004c 0b 00 6f 00 // [0] "orientation"
    00000050 72 00 69 00
    00000054 65 00 6e 00
    00000058 74 00 61 00
    0000005c 74 00 69 00
    00000060 6f 00 6e 00
    00000064 00 00 0c 00 // [1] "layout_width"
    00000068 6c 00 61 00
    0000006c 79 00 6f 00
    00000070 75 00 74 00
    00000074 5f 00 77 00
    00000078 69 00 64 00
    0000007c 74 00 68 00
    00000080 00 00 0d 00 // [2] "layout_height"
    00000084 6c 00 61 00
    00000088 79 00 6f 00
    0000008c 75 00 74 00
    00000090 5f 00 68 00
    00000094 65 00 69 00
    00000098 67 00 68 00
    0000009c 74 00 00 00
    000000a0 04 00 74 00 // [3] "text"
    000000a4 65 00 78 00
    000000a8 74 00 00 00
    000000ac 07 00 61 00 // [4] "android"
    000000b0 6e 00 64 00
    000000b4 72 00 6f 00
    000000b8 69 00 64 00
    000000bc 00 00 2a 00 // [5] "http://schemas.android.com/apk/res/android"
    000000c0 68 00 74 00
    000000c4 74 00 70 00
    000000c8 3a 00 2f 00
    000000cc 2f 00 73 00
    000000d0 63 00 68 00
    000000d4 65 00 6d 00
    000000d8 61 00 73 00
    000000dc 2e 00 61 00
    000000e0 6e 00 64 00
    000000e4 72 00 6f 00
    000000e8 69 00 64 00
    000000ec 2e 00 63 00
    000000f0 6f 00 6d 00
    000000f4 2f 00 61 00
    000000f8 70 00 6b 00
    000000fc 2f 00 72 00
    00000100 65 00 73 00
    00000104 2f 00 61 00
    00000108 6e 00 64 00
    0000010c 72 00 6f 00
    00000110 69 00 64 00
    00000114 00 00 00 00 // [6] ""
    00000118 00 00 0c 00 // [7] "LinearLayout"
    0000011c 4c 00 69 00
    00000120 6e 00 65 00
    00000124 61 00 72 00
    00000128 4c 00 61 00
    0000012c 79 00 6f 00
    00000130 75 00 74 00
    00000134 00 00 08 00 // [8] "TextView"
    00000138 54 00 65 00
    0000013c 78 00 74 00
    00000140 56 00 69 00
    00000144 65 00 77 00
    00000148 00 00 1e 00 // [9] "Hello World, PendragonActivity"
    0000014c 48 00 65 00
    00000150 6c 00 6c 00
    00000154 6f 00 20 00
    00000158 57 00 6f 00
    0000015c 72 00 6c 00
    00000160 64 00 2c 00
    00000164 20 00 50 00
    00000168 65 00 6e 00
    0000016c 64 00 72 00
    00000170 61 00 67 00
    00000174 6f 00 6e 00
    00000178 41 00 63 00
    0000017c 74 00 69 00
    00000180 76 00 69 00
    00000184 74 00 79 00
    00000188 00 00 00 00
    ==================== [End of STRING_POOL]

    ...

Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

Android Internals: Binary XML – Part One Example

The examples which follow are taken from the binary XML version of the main.xml layout file that was automatically generated as part of the same project the Resource table example was taken from.

1.0 main.xml

The contents of the generated main.xml layout file.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>

1.1 Binary main.xml

The result of running hexdump on the binary version main.xml layout file extracted from the application .apk file.


    0000000 03 00 08 00 c4 02 00 00 01 00 1c 00 84 01 00 00
    0000010 0a 00 00 00 00 00 00 00 00 00 00 00 44 00 00 00
    0000020 00 00 00 00 00 00 00 00 1a 00 00 00 36 00 00 00
    0000030 54 00 00 00 60 00 00 00 72 00 00 00 ca 00 00 00
    0000040 ce 00 00 00 ea 00 00 00 fe 00 00 00 0b 00 6f 00
    0000050 72 00 69 00 65 00 6e 00 74 00 61 00 74 00 69 00
    0000060 6f 00 6e 00 00 00 0c 00 6c 00 61 00 79 00 6f 00
    0000070 75 00 74 00 5f 00 77 00 69 00 64 00 74 00 68 00
    0000080 00 00 0d 00 6c 00 61 00 79 00 6f 00 75 00 74 00
    0000090 5f 00 68 00 65 00 69 00 67 00 68 00 74 00 00 00
    00000a0 04 00 74 00 65 00 78 00 74 00 00 00 07 00 61 00
    00000b0 6e 00 64 00 72 00 6f 00 69 00 64 00 00 00 2a 00
    00000c0 68 00 74 00 74 00 70 00 3a 00 2f 00 2f 00 73 00
    00000d0 63 00 68 00 65 00 6d 00 61 00 73 00 2e 00 61 00
    00000e0 6e 00 64 00 72 00 6f 00 69 00 64 00 2e 00 63 00
    00000f0 6f 00 6d 00 2f 00 61 00 70 00 6b 00 2f 00 72 00
    0000100 65 00 73 00 2f 00 61 00 6e 00 64 00 72 00 6f 00
    0000110 69 00 64 00 00 00 00 00 00 00 0c 00 4c 00 69 00
    0000120 6e 00 65 00 61 00 72 00 4c 00 61 00 79 00 6f 00
    0000130 75 00 74 00 00 00 08 00 54 00 65 00 78 00 74 00
    0000140 56 00 69 00 65 00 77 00 00 00 1e 00 48 00 65 00
    0000150 6c 00 6c 00 6f 00 20 00 57 00 6f 00 72 00 6c 00
    0000160 64 00 2c 00 20 00 50 00 65 00 6e 00 64 00 72 00
    0000170 61 00 67 00 6f 00 6e 00 41 00 63 00 74 00 69 00
    0000180 76 00 69 00 74 00 79 00 00 00 00 00 80 01 08 00
    0000190 18 00 00 00 c4 00 01 01 f4 00 01 01 f5 00 01 01
    00001a0 4f 01 01 01 00 01 10 00 18 00 00 00 02 00 00 00
    00001b0 ff ff ff ff 04 00 00 00 05 00 00 00 02 01 10 00
    00001c0 60 00 00 00 02 00 00 00 ff ff ff ff ff ff ff ff
    00001d0 07 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    00001e0 05 00 00 00 00 00 00 00 ff ff ff ff 08 00 00 10
    00001f0 01 00 00 00 05 00 00 00 01 00 00 00 ff ff ff ff
    0000200 08 00 00 10 ff ff ff ff 05 00 00 00 02 00 00 00
    0000210 ff ff ff ff 08 00 00 10 ff ff ff ff 02 01 10 00
    0000220 60 00 00 00 07 00 00 00 ff ff ff ff ff ff ff ff
    0000230 08 00 00 00 14 00 14 00 03 00 00 00 00 00 00 00
    0000240 05 00 00 00 01 00 00 00 ff ff ff ff 08 00 00 10
    0000250 ff ff ff ff 05 00 00 00 02 00 00 00 ff ff ff ff
    0000260 08 00 00 10 fe ff ff ff 05 00 00 00 03 00 00 00
    0000270 09 00 00 00 08 00 00 03 09 00 00 00 03 01 10 00
    0000280 18 00 00 00 0b 00 00 00 ff ff ff ff ff ff ff ff
    0000290 08 00 00 00 03 01 10 00 18 00 00 00 0c 00 00 00
    00002a0 ff ff ff ff ff ff ff ff 07 00 00 00 01 01 10 00
    00002b0 18 00 00 00 0c 00 00 00 ff ff ff ff 04 00 00 00
    00002c0 05 00 00 00                                    
    00002c4

Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

September 13, 2011

Android Internals: Resources – Part One: Resources And Chunks

1.0 Resources And Chunks

Every Android application defines a set of Resources.

These Resources are stored in a Resource Table, or in some cases, for example, layouts, as Android specific Binary XML, which is referenced from the Resource Table.

An application’s Resource Table is stored persistently in the application’s .apk file in the resources.arsc file. Binary XML data is also stored in files in the .apk.

The Resource Table and associated Binary XML data is represented both at runtime and when stored in files by Chunks.

As a consequence an application’s Resources can be loaded by simply mapping the files that contain them into memory.

2.0 Chunks

A Chunk is just a piece of memory split into two parts, a header and a body. The exact structure of the header and the body of a given Chunk is determined by its type.

2.1 Chunk Types

The possible Chunk types are defined by the following C++ enum (see frameworks/base/include/ResourceTypes.h lines 179-201)

    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
    };

2.2 Chunk Headers

All Chunk headers irrespective of the Chunk type have an instance of the C++ struct ResChunk_header (see frameworks/base/include/ResourceTypes.h lines 160-177)
as their first field

    struct ResChunk_header
    {
        // Type identifier for this chunk.  The meaning of this value depends
        // on the containing chunk.
        uint16_t type;

        // Size of the chunk header (in bytes).  Adding this value to
        // the address of the chunk allows you to find its associated data
        // (if any).
        uint16_t headerSize;

        // Total size of this chunk (in bytes).  This is the chunkSize plus
        // the size of any data associated with the chunk.  Adding this value
        // to the chunk allows you to completely skip its contents (including
        // any child chunks).  If this value is the same as chunkSize, there is
        // no data associated with the chunk.
        uint32_t size;
    };

This means that given the address, A, of any Chunk it is always possible to determine

  • its type

  • where the body of the Chunk starts (A + headerSize)

  • where the next Chunk, if any, starts (A + size)

without knowing anything further about the structure of the given Chunk.

2.3 Byte Order

By default the data in Chunks is in little-endian byte order both at runtime and when stored in files.


Copyright (c) 2011 By Simon Lewis. All Rights Reserved.

The WordPress Classic Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: