binres

package
v0.0.0-...-2824937 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2020 License: MIT, BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package binres implements encoding and decoding of android binary resources.

Binary resource structs support unmarshalling the binary output of aapt. Implementations of marshalling for each struct must produce the exact input sent to unmarshalling. This allows tests to validate each struct representation of the binary format as follows:

  • unmarshal the output of aapt
  • marshal the struct representation
  • perform byte-to-byte comparison with aapt output per chunk header and body

This process should strive to make structs idiomatic to make parsing xml text into structs trivial.

Once the struct representation is validated, tests for parsing xml text into structs can become self-referential as the following holds true:

  • the unmarshalled input of aapt output is the only valid target
  • the unmarshalled input of xml text may be compared to the unmarshalled input of aapt output to identify errors, e.g. text-trims, wrong flags, etc

This provides validation, byte-for-byte, for producing binary xml resources.

It should be made clear that unmarshalling binary resources is currently only in scope for proving that the BinaryMarshaler works correctly. Any other use is currently out of scope.

A simple view of binary xml document structure:

XML
  Pool
  Map
  Namespace
  [...node]

Additional resources: https://android.googlesource.com/platform/frameworks/base/+/master/include/androidfw/ResourceTypes.h https://justanapplication.wordpress.com/2011/09/13/ (a series of articles, increment date)

Index

Constants

View Source
const (
	SortedFlag uint32 = 1 << 0
	UTF8Flag          = 1 << 8
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	NS         PoolRef
	Name       PoolRef
	RawValue   PoolRef // The original raw string value of this attribute.
	TypedValue Data    // Processesd typed value of this attribute.
}

func (*Attribute) MarshalBinary

func (attr *Attribute) MarshalBinary() ([]byte, error)

func (*Attribute) UnmarshalBinary

func (attr *Attribute) UnmarshalBinary(bin []byte) error

type CharData

type CharData struct {
	NodeHeader
	RawData   PoolRef // raw character data
	TypedData Data    // typed value of character data
}

CharData represents a CDATA node and includes ref to node's text value.

func (*CharData) MarshalBinary

func (cdt *CharData) MarshalBinary() ([]byte, error)

func (*CharData) UnmarshalBinary

func (cdt *CharData) UnmarshalBinary(bin []byte) error

type Data

type Data struct {
	ByteSize uint16
	Res0     uint8 // always 0, useful for debugging bad read offsets
	Type     DataType
	Value    uint32
}

func (*Data) MarshalBinary

func (d *Data) MarshalBinary() ([]byte, error)

func (*Data) UnmarshalBinary

func (d *Data) UnmarshalBinary(bin []byte) error

type DataType

type DataType uint8
const (
	DataNull             DataType = 0x00 // either 0 or 1 for resource undefined or empty
	DataReference        DataType = 0x01 // ResTable_ref, a reference to another resource table entry
	DataAttribute        DataType = 0x02 // attribute resource identifier
	DataString           DataType = 0x03 // index into the containing resource table's global value string pool
	DataFloat            DataType = 0x04 // single-precision floating point number
	DataDimension        DataType = 0x05 // complex number encoding a dimension value, such as "100in"
	DataFraction         DataType = 0x06 // complex number encoding a fraction of a container
	DataDynamicReference DataType = 0x07 // dynamic ResTable_ref, which needs to be resolved before it can be used like a TYPE_REFERENCE.
	DataIntDec           DataType = 0x10 // raw integer value of the form n..n
	DataIntHex           DataType = 0x11 // raw integer value of the form 0xn..n
	DataIntBool          DataType = 0x12 // either 0 or 1, for input "false" or "true"
	DataIntColorARGB8    DataType = 0x1c // raw integer value of the form #aarrggbb
	DataIntColorRGB8     DataType = 0x1d // raw integer value of the form #rrggbb
	DataIntColorARGB4    DataType = 0x1e // raw integer value of the form #argb
	DataIntColorRGB4     DataType = 0x1f // raw integer value of the form #rgb
)

explicitly defined for clarity and resolvability with apt source

func (DataType) String

func (i DataType) String() string

type Element

type Element struct {
	NodeHeader
	NS             PoolRef
	Name           PoolRef // name of node if element, otherwise chardata if CDATA
	AttributeStart uint16  // byte offset where attrs start
	AttributeSize  uint16  // byte size of attrs
	AttributeCount uint16  // length of attrs
	IdIndex        uint16  // Index (1-based) of the "id" attribute. 0 if none.
	ClassIndex     uint16  // Index (1-based) of the "class" attribute. 0 if none.
	StyleIndex     uint16  // Index (1-based) of the "style" attribute. 0 if none.

	Children []*Element
	// contains filtered or unexported fields
}

func (*Element) MarshalBinary

func (el *Element) MarshalBinary() ([]byte, error)

func (*Element) UnmarshalBinary

func (el *Element) UnmarshalBinary(bin []byte) error

type ElementEnd

type ElementEnd struct {
	NodeHeader
	NS   PoolRef
	Name PoolRef // name of node if binElement, raw chardata if binCharData
}

ElementEnd marks the end of an element node, either Element or CharData.

func (*ElementEnd) MarshalBinary

func (el *ElementEnd) MarshalBinary() ([]byte, error)

func (*ElementEnd) UnmarshalBinary

func (el *ElementEnd) UnmarshalBinary(bin []byte) error

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map contains a uint32 slice mapping strings in the string pool back to resource identifiers. The i'th element of the slice is also the same i'th element of the string pool.

func (*Map) MarshalBinary

func (m *Map) MarshalBinary() ([]byte, error)

func (*Map) UnmarshalBinary

func (m *Map) UnmarshalBinary(bin []byte) error

type Namespace

type Namespace struct {
	NodeHeader
	// contains filtered or unexported fields
}

func (*Namespace) MarshalBinary

func (ns *Namespace) MarshalBinary() ([]byte, error)

func (*Namespace) UnmarshalBinary

func (ns *Namespace) UnmarshalBinary(bin []byte) error

type NodeHeader

type NodeHeader struct {
	LineNumber uint32  // line number in source file this element appears
	Comment    PoolRef // optional xml comment associated with element, MaxUint32 if none
	// contains filtered or unexported fields
}

NodeHeader is header all xml node types have, providing additional information regarding an xml node over binChunkHeader.

func (*NodeHeader) MarshalBinary

func (hdr *NodeHeader) MarshalBinary() ([]byte, error)

func (*NodeHeader) UnmarshalBinary

func (hdr *NodeHeader) UnmarshalBinary(bin []byte) error

type Package

type Package struct {
	// contains filtered or unexported fields
}

func (Package) MarshalBinary

func (hdr Package) MarshalBinary() ([]byte, error)

func (*Package) UnmarshalBinary

func (pkg *Package) UnmarshalBinary(bin []byte) error

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool has the following structure marshalled:

	binChunkHeader
 StringCount  uint32 // Number of strings in this pool
 StyleCount   uint32 // Number of style spans in pool
 Flags        uint32 // SortedFlag, UTF8Flag
 StringsStart uint32 // Index of string data from header
 StylesStart  uint32 // Index of style data from header

 StringIndices []uint32 // starting at zero

 // UTF16 entries are concatenations of the following:
 // [2]byte uint16 string length, exclusive
 // [2]byte [optional] low word if high bit of length was set
 // [n]byte data
 // [2]byte 0x0000 terminator
 Strings []uint16

func (*Pool) IsSorted

func (pl *Pool) IsSorted() bool

func (*Pool) IsUTF8

func (pl *Pool) IsUTF8() bool

func (*Pool) MarshalBinary

func (pl *Pool) MarshalBinary() ([]byte, error)

func (*Pool) UnmarshalBinary

func (pl *Pool) UnmarshalBinary(bin []byte) error

type PoolRef

type PoolRef uint32

PoolRef is the i'th string in a pool.

type ResType

type ResType uint16
const (
	ResNull       ResType = 0x0000
	ResStringPool ResType = 0x0001
	ResTable      ResType = 0x0002
	ResXML        ResType = 0x0003

	ResXMLStartNamespace ResType = 0x0100
	ResXMLEndNamespace   ResType = 0x0101
	ResXMLStartElement   ResType = 0x0102
	ResXMLEndElement     ResType = 0x0103
	ResXMLCharData       ResType = 0x0104

	ResXMLResourceMap ResType = 0x0180

	ResTablePackage  ResType = 0x0200
	ResTableType     ResType = 0x0201
	ResTableTypeSpec ResType = 0x0202
)

explicitly defined for clarity and resolvability with apt source

func (ResType) IsSupported

func (t ResType) IsSupported() bool

func (ResType) String

func (i ResType) String() string

type Span

type Span struct {
	// contains filtered or unexported fields
}

func (*Span) UnmarshalBinary

func (spn *Span) UnmarshalBinary(bin []byte) error

type Table

type Table struct {
	TableHeader
	// contains filtered or unexported fields
}

TODO next up: package chunk https://justanapplication.wordpress.com/2011/09/16/

func (*Table) UnmarshalBinary

func (tbl *Table) UnmarshalBinary(bin []byte) error

type TableHeader

type TableHeader struct {
	// contains filtered or unexported fields
}

func (TableHeader) MarshalBinary

func (hdr TableHeader) MarshalBinary() ([]byte, error)

func (*TableHeader) UnmarshalBinary

func (hdr *TableHeader) UnmarshalBinary(bin []byte) error

type XML

type XML struct {
	Pool *Pool
	Map  *Map

	Namespace *Namespace
	Children  []*Element
	// contains filtered or unexported fields
}

func (*XML) MarshalBinary

func (bx *XML) MarshalBinary() ([]byte, error)

func (*XML) UnmarshalBinary

func (bx *XML) UnmarshalBinary(bin []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL