Just An Application

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 (0x0014).

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 (0x0014).

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.

Blog at WordPress.com.

%d bloggers like this: