androidbinary

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2021 License: MIT Imports: 10 Imported by: 0

README

androidbinary

Build Status GoDoc

Android binary file parser

High Level API

Parse APK files
package main

import (
	"github.com/shogo82148/androidbinary/apk"
)

func main() {
	pkg, _ := apk.OpenFile("your-android-app.apk")
	defer pkg.Close()

	icon, _ := pkg.Icon(nil) // returns the icon of APK as image.Image
	pkgName := pkg.PackageName() // returns the package name

	resConfigEN := &androidbinary.ResTableConfig{
		Language: [2]uint8{uint8('e'), uint8('n')},
	}
	appLabel, _ = pkg.Label(resConfigEN) // get app label for en translation
}

Low Level API

Parse XML binary
package main

import (
	"encoding/xml"

	"github.com/shogo82148/androidbinary"
	"github.com/shogo82148/androidbinary/apk"
)

func main() {
	f, _ := os.Open("AndroidManifest.xml")
	xml, _ := androidbinary.NewXMLFile(f)
	reader := xml.Reader()

	// read XML from reader
	var manifest apk.Manifest
	data, _ := ioutil.ReadAll(reader)
	xml.Unmarshal(data, &manifest)
}
Parse Resource files
package main

import (
	"fmt"
	"github.com/shogo82148/androidbinary"
)

func main() {
	f, _ := os.Open("resources.arsc")
	rsc, _ := androidbinary.NewTableFile(f)
	resource, _ := rsc.GetResource(androidbinary.ResID(0xCAFEBABE), nil)
	fmt.Println(resource)
}

License

This software is released under the MIT License, see LICENSE.

Documentation

Index

Examples

Constants

View Source
const NilResStringPoolRef = ResStringPoolRef(0xFFFFFFFF)

NilResStringPoolRef is nil reference for string pool.

Variables

This section is empty.

Functions

func IsResID

func IsResID(s string) bool

IsResID returns whether s is ResId.

Types

type Bool

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

Bool is a boolean value in XML file. It may be an immediate value or a reference.

func (Bool) Bool

func (v Bool) Bool() (bool, error)

Bool returns the boolean value. It resolves the reference if needed.

func (Bool) MarshalXMLAttr

func (v Bool) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

MarshalXMLAttr implements xml.MarshalerAttr.

func (Bool) MustBool

func (v Bool) MustBool() bool

MustBool is same as Bool, but it panics if it fails to parse the value.

func (*Bool) SetBool

func (v *Bool) SetBool(value bool)

SetBool sets a boolean value.

func (*Bool) SetResID

func (v *Bool) SetResID(resID ResID)

SetResID sets a boolean value with the resource id.

func (*Bool) UnmarshalXMLAttr

func (v *Bool) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr implements xml.UnmarshalerAttr.

func (Bool) WithResTableConfig

func (v Bool) WithResTableConfig(config *ResTableConfig) Bool

WithResTableConfig ties ResTableConfig to the Bool.

func (Bool) WithTableFile

func (v Bool) WithTableFile(table *TableFile) Bool

WithTableFile ties TableFile to the Bool.

type ChunkType

type ChunkType uint16

ChunkType is a type of a resource chunk.

const (
	ResNullChunkType       ChunkType = 0x0000
	ResStringPoolChunkType ChunkType = 0x0001
	ResTableChunkType      ChunkType = 0x0002
	ResXMLChunkType        ChunkType = 0x0003

	// Chunk types in RES_XML_TYPE
	ResXMLFirstChunkType     ChunkType = 0x0100
	ResXMLStartNamespaceType ChunkType = 0x0100
	ResXMLEndNamespaceType   ChunkType = 0x0101
	ResXMLStartElementType   ChunkType = 0x0102
	ResXMLEndElementType     ChunkType = 0x0103
	ResXMLCDataType          ChunkType = 0x0104
	ResXMLLastChunkType      ChunkType = 0x017f

	// This contains a uint32_t array mapping strings in the string
	// pool back to resource identifiers.  It is optional.
	ResXMLResourceMapType ChunkType = 0x0180

	// Chunk types in RES_TABLE_TYPE
	ResTablePackageType  ChunkType = 0x0200
	ResTableTypeType     ChunkType = 0x0201
	ResTableTypeSpecType ChunkType = 0x0202
)

Chunk types.

type DataType

type DataType uint8

DataType is a type of the data value.

const (
	TypeNull          DataType = 0x00
	TypeReference     DataType = 0x01
	TypeAttribute     DataType = 0x02
	TypeString        DataType = 0x03
	TypeFloat         DataType = 0x04
	TypeDemention     DataType = 0x05
	TypeFraction      DataType = 0x06
	TypeFirstInt      DataType = 0x10
	TypeIntDec        DataType = 0x10
	TypeIntHex        DataType = 0x11
	TypeIntBoolean    DataType = 0x12
	TypeFirstColorInt DataType = 0x1c
	TypeIntColorARGB8 DataType = 0x1c
	TypeIntColorRGB8  DataType = 0x1d
	TypeIntColorARGB4 DataType = 0x1e
	TypeIntColorRGB4  DataType = 0x1f
	TypeLastColorInt  DataType = 0x1f
	TypeLastInt       DataType = 0x1f
)

The constants for DataType

type Flags

type Flags uint32

Flags are flags for string pool header.

const (
	SortedFlag Flags = 1 << 0
	UTF8Flag   Flags = 1 << 8
)

the values of Flags.

type InputFlags

type InputFlags uint8

InputFlags are input flags.

const (
	MaskKeysHidden InputFlags = 0x03
	KeysHiddenAny  InputFlags = 0x00
	KeysHiddenNo   InputFlags = 0x01
	KeysHiddenYes  InputFlags = 0x02
	KeysHiddenSoft InputFlags = 0x03

	MaskNavHidden InputFlags = 0x0c
	NavHiddenAny  InputFlags = 0x00
	NavHiddenNo   InputFlags = 0x04
	NavHiddenYes  InputFlags = 0x08
)

input flags

type Int32

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

Int32 is an integer value in XML file. It may be an immediate value or a reference.

func (Int32) Int32

func (v Int32) Int32() (int32, error)

Int32 returns the integer value. It resolves the reference if needed.

func (Int32) MarshalXMLAttr

func (v Int32) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

MarshalXMLAttr implements xml.MarshalerAttr.

func (Int32) MustInt32

func (v Int32) MustInt32() int32

MustInt32 is same as Int32, but it panics if it fails to parse the value.

func (*Int32) SetInt32

func (v *Int32) SetInt32(value int32)

SetInt32 sets an integer value.

func (*Int32) SetResID

func (v *Int32) SetResID(resID ResID)

SetResID sets a boolean value with the resource id.

func (*Int32) UnmarshalXMLAttr

func (v *Int32) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr implements xml.UnmarshalerAttr.

func (Int32) WithResTableConfig

func (v Int32) WithResTableConfig(config *ResTableConfig) Bool

WithResTableConfig ties ResTableConfig to the Bool.

func (Int32) WithTableFile

func (v Int32) WithTableFile(table *TableFile) Int32

WithTableFile ties TableFile to the Bool.

type ResChunkHeader

type ResChunkHeader struct {
	Type       ChunkType
	HeaderSize uint16
	Size       uint32
}

ResChunkHeader is a header of a resource chunk.

type ResID

type ResID uint32

ResID is ID for resources.

func ParseResID

func ParseResID(s string) (ResID, error)

ParseResID parses ResId.

func (ResID) Entry

func (id ResID) Entry() int

Entry returns the entry index of id.

func (ResID) Package

func (id ResID) Package() uint32

Package returns the package index of id.

func (ResID) String

func (id ResID) String() string

func (ResID) Type

func (id ResID) Type() int

Type returns the type index of id.

type ResStringPool

type ResStringPool struct {
	Header  ResStringPoolHeader
	Strings []string
	Styles  []ResStringPoolSpan
}

ResStringPool is a string pool resource.

func (*ResStringPool) GetString

func (pool *ResStringPool) GetString(ref ResStringPoolRef) string

GetString returns a string referenced by ref.

type ResStringPoolHeader

type ResStringPoolHeader struct {
	Header      ResChunkHeader
	StringCount uint32
	StyleCount  uint32
	Flags       Flags
	StringStart uint32
	StylesStart uint32
}

ResStringPoolHeader is a chunk header of string pool.

type ResStringPoolRef

type ResStringPoolRef uint32

ResStringPoolRef is a type representing a reference to a string.

type ResStringPoolSpan

type ResStringPoolSpan struct {
	FirstChar, LastChar uint32
}

ResStringPoolSpan is a span of style information associated with a string in the pool.

type ResTableConfig

type ResTableConfig struct {
	Size uint32
	// imsi
	Mcc uint16
	Mnc uint16

	// locale
	Language [2]uint8
	Country  [2]uint8

	// screen type
	Orientation uint8
	Touchscreen uint8
	Density     uint16

	// inout
	Keyboard   uint8
	Navigation uint8
	InputFlags InputFlags
	InputPad0  uint8

	// screen size
	ScreenWidth  uint16
	ScreenHeight uint16

	// version
	SDKVersion   uint16
	MinorVersion uint16

	// screen config
	ScreenLayout          ScreenLayout
	UIMode                UIMode
	SmallestScreenWidthDp uint16

	// screen size dp
	ScreenWidthDp  uint16
	ScreenHeightDp uint16
}

ResTableConfig is a configuration of a table.

func (*ResTableConfig) IsBetterThan

func (c *ResTableConfig) IsBetterThan(o *ResTableConfig, r *ResTableConfig) bool

IsBetterThan returns true if c is better than o for the r configuration.

func (*ResTableConfig) IsLocaleBetterThan

func (c *ResTableConfig) IsLocaleBetterThan(o *ResTableConfig, r *ResTableConfig) bool

IsLocaleBetterThan returns true if c is a better locale match than o for the r configuration.

func (*ResTableConfig) IsLocaleMoreSpecificThan

func (c *ResTableConfig) IsLocaleMoreSpecificThan(o *ResTableConfig) int

IsLocaleMoreSpecificThan a positive integer if this config is more specific than o, a negative integer if |o| is more specific and 0 if they're equally specific.

func (*ResTableConfig) IsMoreSpecificThan

func (c *ResTableConfig) IsMoreSpecificThan(o *ResTableConfig) bool

IsMoreSpecificThan returns true if c is more specific than o.

func (*ResTableConfig) Locale

func (c *ResTableConfig) Locale() string

Locale returns the locale of the configuration.

func (*ResTableConfig) Match

func (c *ResTableConfig) Match(settings *ResTableConfig) bool

Match returns true if c can be considered a match for the parameters in settings.

type ResTableEntry

type ResTableEntry struct {
	Size  uint16
	Flags uint16
	Key   ResStringPoolRef
}

ResTableEntry is the beginning of information about an entry in the resource table.

type ResTableHeader

type ResTableHeader struct {
	Header       ResChunkHeader
	PackageCount uint32
}

ResTableHeader is a header of TableFile.

type ResTablePackage

type ResTablePackage struct {
	Header         ResChunkHeader
	ID             uint32
	Name           [128]uint16
	TypeStrings    uint32
	LastPublicType uint32
	KeyStrings     uint32
	LastPublicKey  uint32
}

ResTablePackage is a header of table packages.

type ResTableType

type ResTableType struct {
	Header       ResChunkHeader
	ID           uint8
	Res0         uint8
	Res1         uint16
	EntryCount   uint32
	EntriesStart uint32
	Config       ResTableConfig
}

ResTableType is a type of a table.

type ResTableTypeSpec

type ResTableTypeSpec struct {
	Header     ResChunkHeader
	ID         uint8
	Res0       uint8
	Res1       uint16
	EntryCount uint32
}

ResTableTypeSpec is specification of the resources defined by a particular type.

type ResValue

type ResValue struct {
	Size     uint16
	Res0     uint8
	DataType DataType
	Data     uint32
}

ResValue is a representation of a value in a resource

type ResXMLTreeAttrExt

type ResXMLTreeAttrExt struct {
	NS             ResStringPoolRef
	Name           ResStringPoolRef
	AttributeStart uint16
	AttributeSize  uint16
	AttributeCount uint16
	IDIndex        uint16
	ClassIndex     uint16
	StyleIndex     uint16
}

ResXMLTreeAttrExt is extended XML tree node for start tags -- includes attribute.

type ResXMLTreeAttribute

type ResXMLTreeAttribute struct {
	NS         ResStringPoolRef
	Name       ResStringPoolRef
	RawValue   ResStringPoolRef
	TypedValue ResValue
}

ResXMLTreeAttribute is an attribute of start tags.

type ResXMLTreeEndElementExt

type ResXMLTreeEndElementExt struct {
	NS   ResStringPoolRef
	Name ResStringPoolRef
}

ResXMLTreeEndElementExt is extended XML tree node for element start/end nodes.

type ResXMLTreeNamespaceExt

type ResXMLTreeNamespaceExt struct {
	Prefix ResStringPoolRef
	URI    ResStringPoolRef
}

ResXMLTreeNamespaceExt is extended XML tree node for namespace start/end nodes.

type ResXMLTreeNode

type ResXMLTreeNode struct {
	Header     ResChunkHeader
	LineNumber uint32
	Comment    ResStringPoolRef
}

ResXMLTreeNode is basic XML tree node.

type ScreenLayout

type ScreenLayout uint8

ScreenLayout describes screen layout.

const (
	MaskScreenSize   ScreenLayout = 0x0f
	ScreenSizeAny    ScreenLayout = 0x01
	ScreenSizeSmall  ScreenLayout = 0x02
	ScreenSizeNormal ScreenLayout = 0x03
	ScreenSizeLarge  ScreenLayout = 0x04
	ScreenSizeXLarge ScreenLayout = 0x05

	MaskScreenLong  ScreenLayout = 0x30
	ShiftScreenLong              = 4
	ScreenLongAny   ScreenLayout = 0x00
	ScreenLongNo    ScreenLayout = 0x10
	ScreenLongYes   ScreenLayout = 0x20

	MaskLayoutDir  ScreenLayout = 0xC0
	ShiftLayoutDir              = 6
	LayoutDirAny   ScreenLayout = 0x00
	LayoutDirLTR   ScreenLayout = 0x40
	LayoutDirRTL   ScreenLayout = 0x80
)

ScreenLayout bits

type String

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

String is a boolean value in XML file. It may be an immediate value or a reference.

func (String) MarshalXMLAttr

func (v String) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

MarshalXMLAttr implements xml.MarshalerAttr.

func (String) MustString

func (v String) MustString() string

MustString is same as String, but it panics if it fails to parse the value.

func (*String) SetResID

func (v *String) SetResID(resID ResID)

SetResID sets a boolean value with the resource id.

func (*String) SetString

func (v *String) SetString(value string)

SetString sets a string value.

func (String) String

func (v String) String() (string, error)

String returns the string value. It resolves the reference if needed.

func (*String) UnmarshalXMLAttr

func (v *String) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr implements xml.UnmarshalerAttr.

func (String) WithResTableConfig

func (v String) WithResTableConfig(config *ResTableConfig) String

WithResTableConfig ties ResTableConfig to the Bool.

func (String) WithTableFile

func (v String) WithTableFile(table *TableFile) String

WithTableFile ties TableFile to the Bool.

type TableEntry

type TableEntry struct {
	Key   *ResTableEntry
	Value *ResValue
	Flags uint32
}

TableEntry is a entry in a resource table.

type TableFile

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

TableFile is a resource table file.

func NewTableFile

func NewTableFile(r io.ReaderAt) (*TableFile, error)

NewTableFile returns new TableFile.

Example
package main

import (
	"fmt"
	"os"

	"github.com/danbai225/androidbinary"
)

func main() {
	f, err := os.Open("testdata/resources.arsc")
	if err != nil {
		panic(err)
	}
	tableFile, err := androidbinary.NewTableFile(f)
	if err != nil {
		panic(err)
	}

	val, err := tableFile.GetResource(0x7f040000, &androidbinary.ResTableConfig{})
	if err != nil {
		panic(err)
	}
	fmt.Println(val)
}
Output:

FireworksMeasure

func (*TableFile) GetResource

func (f *TableFile) GetResource(id ResID, config *ResTableConfig) (interface{}, error)

GetResource returns a resource referenced by id.

func (*TableFile) GetString

func (f *TableFile) GetString(ref ResStringPoolRef) string

GetString returns a string referenced by ref.

type TablePackage

type TablePackage struct {
	Header      ResTablePackage
	TypeStrings *ResStringPool
	KeyStrings  *ResStringPool
	TableTypes  []*TableType
}

TablePackage is a table package.

type TableType

type TableType struct {
	Header  *ResTableType
	Entries []TableEntry
}

TableType is a collection of resource entries for a particular resource data type.

type UIMode

type UIMode uint8

UIMode describes UI mode.

const (
	MaskUIModeType   UIMode = 0x0f
	UIModeTypeAny    UIMode = 0x01
	UIModeTypeNormal UIMode = 0x02
	UIModeTypeDesk   UIMode = 0x03
	UIModeTypeCar    UIMode = 0x04

	MaskUIModeNight  UIMode = 0x30
	ShiftUIModeNight        = 4
	UIModeNightAny   UIMode = 0x00
	UIModeNightNo    UIMode = 0x10
	UIModeNightYes   UIMode = 0x20
)

UIMode bits

type XMLFile

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

XMLFile is an XML file expressed in binary format.

func NewXMLFile

func NewXMLFile(r io.ReaderAt) (*XMLFile, error)

NewXMLFile returns a new XMLFile.

Example
package main

import (
	"encoding/xml"
	"os"

	"github.com/danbai225/androidbinary"
	"github.com/danbai225/androidbinary/apk"
)

func main() {
	f, _ := os.Open("testdata/AndroidManifest.xml")
	xmlFile, err := androidbinary.NewXMLFile(f)
	if err != nil {
		panic(err)
	}

	var v apk.Manifest
	dec := xml.NewDecoder(xmlFile.Reader())
	dec.Decode(&v)

	enc := xml.NewEncoder(os.Stdout)
	enc.Indent("", "\t")
	enc.Encode(v)

}
Output:

	<Manifest package="net.sorablue.shogo.FWMeasure" xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="テスト版">
	<application android:allowTaskReparenting="false" android:allowBackup="false" android:backupAgent="" android:debuggable="false" android:description="" android:enabled="false" android:hasCode="false" android:hardwareAccelerated="false" android:icon="@0x7F020000" android:killAfterRestore="false" android:largeHeap="false" android:label="@0x7F040000" android:logo="" android:manageSpaceActivity="" android:name="" android:permission="" android:persistent="false" android:process="" android:restoreAnyVersion="false" android:requiredAccountType="" android:restrictedAccountType="" android:supportsRtl="false" android:taskAffinity="" android:testOnly="false" android:theme="" android:uiOptions="" android:vmSafeMode="false">
		<activity android:theme="" android:name="FWMeasureActivity" android:label="" android:screenOrientation="0">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"></action>
				<category android:name="android.intent.category.LAUNCHER"></category>
			</intent-filter>
		</activity>
		<activity android:theme="" android:name="MapActivity" android:label="" android:screenOrientation="0"></activity>
		<activity android:theme="" android:name="SettingActivity" android:label="" android:screenOrientation=""></activity>
		<activity android:theme="" android:name="PlaceSettingActivity" android:label="" android:screenOrientation=""></activity>
	</application>
	<instrumentation android:name="" android:targetPackage="" android:handleProfiling="false" android:functionalTest="false"></instrumentation>
	<uses-sdk android:minSdkVersion="0" android:targetSdkVersion="0" android:maxSdkVersion="0"></uses-sdk>
	<uses-permission android:name="android.permission.CAMERA" android:maxSdkVersion="0"></uses-permission>
	<uses-permission android:name="android.permission.WAKE_LOCK" android:maxSdkVersion="0"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="0"></uses-permission>
	<uses-permission android:name="android.permission.INTERNET" android:maxSdkVersion="0"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" android:maxSdkVersion="0"></uses-permission>
	<uses-permission android:name="android.permission.RECORD_AUDIO" android:maxSdkVersion="0"></uses-permission>
</Manifest>

func (*XMLFile) Decode

func (f *XMLFile) Decode(v interface{}, table *TableFile, config *ResTableConfig) error

Decode decodes XML file and stores the result in the value pointed to by v. To resolve the resource references, Decode also stores default TableFile and ResTableConfig in the value pointed to by v.

func (*XMLFile) GetString

func (f *XMLFile) GetString(ref ResStringPoolRef) string

GetString returns a string referenced by ref.

func (*XMLFile) Reader

func (f *XMLFile) Reader() *bytes.Reader

Reader returns a reader of XML file expressed in text format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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