2015-10-26 15:55:44 -05:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package binres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unicode/utf16"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SortedFlag uint32 = 1 << 0
|
|
|
|
UTF8Flag = 1 << 8
|
|
|
|
)
|
|
|
|
|
|
|
|
// PoolRef is the i'th string in a pool.
|
|
|
|
type PoolRef uint32
|
|
|
|
|
|
|
|
// 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
|
|
|
|
type Pool struct {
|
|
|
|
chunkHeader
|
|
|
|
|
|
|
|
strings []string
|
|
|
|
styles []*Span
|
|
|
|
flags uint32 // SortedFlag, UTF8Flag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *Pool) IsSorted() bool { return pl.flags&SortedFlag == SortedFlag }
|
|
|
|
func (pl *Pool) IsUTF8() bool { return pl.flags&UTF8Flag == UTF8Flag }
|
|
|
|
|
|
|
|
func (pl *Pool) UnmarshalBinary(bin []byte) error {
|
|
|
|
if err := (&pl.chunkHeader).UnmarshalBinary(bin); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pl.typ != ResStringPool {
|
|
|
|
return fmt.Errorf("have type %s, want %s", pl.typ, ResStringPool)
|
|
|
|
}
|
|
|
|
|
2015-12-22 14:37:17 -06:00
|
|
|
pl.strings = make([]string, btou32(bin[8:]))
|
|
|
|
pl.styles = make([]*Span, btou32(bin[12:]))
|
2015-10-26 15:55:44 -05:00
|
|
|
pl.flags = btou32(bin[16:])
|
|
|
|
|
|
|
|
offstrings := btou32(bin[20:])
|
|
|
|
offstyle := btou32(bin[24:])
|
2015-12-22 14:37:17 -06:00
|
|
|
hdrlen := 28
|
2015-10-26 15:55:44 -05:00
|
|
|
|
|
|
|
if pl.IsUTF8() {
|
|
|
|
for i := range pl.strings {
|
2015-12-22 14:37:17 -06:00
|
|
|
offset := int(offstrings + btou32(bin[hdrlen+i*4:]))
|
|
|
|
|
|
|
|
// if leading bit set for nchars and nbytes,
|
|
|
|
// treat first byte as 7-bit high word and next as low word.
|
|
|
|
nchars := int(bin[offset])
|
|
|
|
offset++
|
|
|
|
if nchars&(1<<7) != 0 {
|
|
|
|
n0 := nchars ^ (1 << 7) // high word
|
|
|
|
n1 := int(bin[offset]) // low word
|
|
|
|
nchars = n0*(1<<8) + n1
|
|
|
|
offset++
|
2015-10-26 15:55:44 -05:00
|
|
|
}
|
|
|
|
|
2015-12-22 14:37:17 -06:00
|
|
|
// TODO(d) At least one case in android.jar (api-10) resource table has only
|
|
|
|
// highbit set, making 7-bit highword zero. The reason for this is currently
|
|
|
|
// unknown but would make tests that unmarshal-marshal to match bytes impossible.
|
2015-10-26 15:55:44 -05:00
|
|
|
// The values noted were high-word: 0 (after highbit unset), low-word: 141
|
2015-12-22 14:37:17 -06:00
|
|
|
// The size may be treated as an int8 triggering the use of two bytes to store size
|
|
|
|
// even though the implementation uses uint8.
|
|
|
|
nbytes := int(bin[offset])
|
|
|
|
offset++
|
|
|
|
if nbytes&(1<<7) != 0 {
|
|
|
|
n0 := nbytes ^ (1 << 7) // high word
|
|
|
|
n1 := int(bin[offset]) // low word
|
|
|
|
nbytes = n0*(1<<8) + n1
|
|
|
|
offset++
|
2015-10-26 15:55:44 -05:00
|
|
|
}
|
|
|
|
|
2015-12-22 14:37:17 -06:00
|
|
|
data := bin[offset : offset+nbytes]
|
|
|
|
if x := uint8(bin[offset+nbytes]); x != 0 {
|
|
|
|
return fmt.Errorf("expected zero terminator, got 0x%02X for nchars=%v nbytes=%v data=%q", x, nchars, nbytes, data)
|
2015-10-26 15:55:44 -05:00
|
|
|
}
|
|
|
|
pl.strings[i] = string(data)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := range pl.strings {
|
2015-12-22 14:37:17 -06:00
|
|
|
offset := int(offstrings + btou32(bin[hdrlen+i*4:])) // read index of string
|
|
|
|
|
|
|
|
// if leading bit set for nchars, treat first byte as 7-bit high word and next as low word.
|
|
|
|
nchars := int(btou16(bin[offset:]))
|
|
|
|
offset += 2
|
|
|
|
if nchars&(1<<15) != 0 { // TODO(d) this is untested
|
|
|
|
n0 := nchars ^ (1 << 15) // high word
|
|
|
|
n1 := int(btou16(bin[offset:])) // low word
|
|
|
|
nchars = n0*(1<<16) + n1
|
|
|
|
offset += 2
|
2015-10-26 15:55:44 -05:00
|
|
|
}
|
|
|
|
|
2015-12-22 14:37:17 -06:00
|
|
|
data := make([]uint16, nchars)
|
2015-10-26 15:55:44 -05:00
|
|
|
for i := range data {
|
2015-12-22 14:37:17 -06:00
|
|
|
data[i] = btou16(bin[offset+2*i:])
|
2015-10-26 15:55:44 -05:00
|
|
|
}
|
|
|
|
|
2015-12-22 14:37:17 -06:00
|
|
|
if x := btou16(bin[offset+nchars*2:]); x != 0 {
|
|
|
|
return fmt.Errorf("expected zero terminator, got 0x%04X for nchars=%v data=%q", x, nchars, data)
|
2015-10-26 15:55:44 -05:00
|
|
|
}
|
|
|
|
pl.strings[i] = string(utf16.Decode(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 14:37:17 -06:00
|
|
|
// TODO decode styles
|
2015-10-26 15:55:44 -05:00
|
|
|
_ = offstyle
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *Pool) MarshalBinary() ([]byte, error) {
|
|
|
|
if pl.IsUTF8() {
|
|
|
|
return nil, fmt.Errorf("encode utf8 not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hdrlen = 28
|
|
|
|
// indices of string indices
|
|
|
|
iis = make([]uint32, len(pl.strings))
|
|
|
|
iislen = len(iis) * 4
|
|
|
|
// utf16 encoded strings concatenated together
|
|
|
|
strs []uint16
|
|
|
|
)
|
|
|
|
for i, x := range pl.strings {
|
|
|
|
if len(x)>>16 > 0 {
|
|
|
|
panic(fmt.Errorf("string lengths over 1<<15 not yet supported, got len %d", len(x)))
|
|
|
|
}
|
|
|
|
p := utf16.Encode([]rune(x))
|
|
|
|
if len(p) == 0 {
|
|
|
|
strs = append(strs, 0x0000, 0x0000)
|
|
|
|
} else {
|
|
|
|
strs = append(strs, uint16(len(p))) // string length (implicitly includes zero terminator to follow)
|
|
|
|
strs = append(strs, p...)
|
|
|
|
strs = append(strs, 0) // zero terminated
|
|
|
|
}
|
|
|
|
// indices start at zero
|
|
|
|
if i+1 != len(iis) {
|
|
|
|
iis[i+1] = uint32(len(strs) * 2) // utf16 byte index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check strings is 4-byte aligned, pad with zeros if not.
|
|
|
|
for x := (len(strs) * 2) % 4; x != 0; x -= 2 {
|
|
|
|
strs = append(strs, 0x0000)
|
|
|
|
}
|
|
|
|
|
|
|
|
strslen := len(strs) * 2
|
|
|
|
|
|
|
|
hdr := chunkHeader{
|
|
|
|
typ: ResStringPool,
|
|
|
|
headerByteSize: 28,
|
|
|
|
byteSize: uint32(28 + iislen + strslen),
|
|
|
|
}
|
|
|
|
|
|
|
|
bin := make([]byte, hdr.byteSize)
|
|
|
|
|
|
|
|
hdrbin, err := hdr.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(bin, hdrbin)
|
|
|
|
|
|
|
|
putu32(bin[8:], uint32(len(pl.strings)))
|
|
|
|
putu32(bin[12:], uint32(len(pl.styles)))
|
|
|
|
putu32(bin[16:], pl.flags)
|
|
|
|
putu32(bin[20:], uint32(hdrlen+iislen))
|
|
|
|
putu32(bin[24:], 0) // index of styles start, is 0 when styles length is 0
|
|
|
|
|
|
|
|
buf := bin[28:]
|
|
|
|
for _, x := range iis {
|
|
|
|
putu32(buf, x)
|
|
|
|
buf = buf[4:]
|
|
|
|
}
|
|
|
|
for _, x := range strs {
|
|
|
|
putu16(buf, x)
|
|
|
|
buf = buf[2:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) != 0 {
|
|
|
|
panic(fmt.Errorf("failed to fill allocated buffer, %v bytes left over", len(buf)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Span struct {
|
|
|
|
name PoolRef
|
|
|
|
|
|
|
|
firstChar, lastChar uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (spn *Span) UnmarshalBinary(bin []byte) error {
|
|
|
|
const end = 0xFFFFFFFF
|
|
|
|
spn.name = PoolRef(btou32(bin))
|
|
|
|
if spn.name == end {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
spn.firstChar = btou32(bin[4:])
|
|
|
|
spn.lastChar = btou32(bin[8:])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
type Map struct {
|
|
|
|
chunkHeader
|
|
|
|
rs []uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Map) UnmarshalBinary(bin []byte) error {
|
|
|
|
(&m.chunkHeader).UnmarshalBinary(bin)
|
|
|
|
buf := bin[m.headerByteSize:m.byteSize]
|
|
|
|
m.rs = make([]uint32, len(buf)/4)
|
|
|
|
for i := range m.rs {
|
|
|
|
m.rs[i] = btou32(buf[i*4:])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Map) MarshalBinary() ([]byte, error) {
|
|
|
|
bin := make([]byte, 8+len(m.rs)*4)
|
|
|
|
b, err := m.chunkHeader.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(bin, b)
|
|
|
|
for i, r := range m.rs {
|
|
|
|
putu32(bin[8+i*4:], r)
|
|
|
|
}
|
|
|
|
return bin, nil
|
|
|
|
}
|