Skip to content

Commit a4c8342

Browse files
authored
Merge pull request shogo82148#10 from shogo82148/fix-issue9
Fix issue shogo82148#9
2 parents 8a63a59 + 9b7ce3f commit a4c8342

9 files changed

Lines changed: 303 additions & 147 deletions

File tree

apk/apk.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@ import (
1010
"io/ioutil"
1111
"os"
1212
"strconv"
13-
"strings"
14-
15-
_ "image/jpeg"
16-
_ "image/png"
1713

1814
"github.com/pkg/errors"
1915
"github.com/shogo82148/androidbinary"
2016
)
2117

22-
var DefaultResTableConfig = &androidbinary.ResTableConfig{}
23-
2418
type Apk struct {
2519
f *os.File
2620
zipreader *zip.Reader
@@ -80,7 +74,7 @@ func (k *Apk) Close() error {
8074
// Icon return icon image
8175
func (k *Apk) Icon(resConfig *androidbinary.ResTableConfig) (image.Image, error) {
8276
iconPath := k.getResource(k.manifest.App.Icon, resConfig)
83-
if strings.HasPrefix(iconPath, "@0x") {
77+
if androidbinary.IsResId(iconPath) {
8478
return nil, errors.New("unable to convert icon-id to icon path")
8579
}
8680
imgData, err := k.readZipFile(iconPath)
@@ -93,7 +87,7 @@ func (k *Apk) Icon(resConfig *androidbinary.ResTableConfig) (image.Image, error)
9387

9488
func (k *Apk) Label(resConfig *androidbinary.ResTableConfig) (s string, err error) {
9589
s = k.getResource(k.manifest.App.Label, resConfig)
96-
if strings.HasPrefix(s, "@0x") {
90+
if androidbinary.IsResId(s) {
9791
err = errors.New("unable to convert label-id to string")
9892
}
9993
return
@@ -126,7 +120,7 @@ func (k *Apk) parseManifest() error {
126120
}
127121
xmlfile, err := androidbinary.NewXMLFile(bytes.NewReader(xmlData))
128122
if err != nil {
129-
return errors.Wrap(err, "parse-axml")
123+
return errors.Wrap(err, "parse-xml")
130124
}
131125
reader := xmlfile.Reader()
132126
data, err := ioutil.ReadAll(reader)
@@ -146,15 +140,11 @@ func (k *Apk) parseResources() (err error) {
146140
}
147141

148142
func (k *Apk) getResource(id string, resConfig *androidbinary.ResTableConfig) string {
149-
if resConfig == nil {
150-
resConfig = DefaultResTableConfig
151-
}
152-
var resId uint32
153-
_, err := fmt.Sscanf(id, "@0x%x", &resId)
143+
resID, err := androidbinary.ParseResId(id)
154144
if err != nil {
155145
return id
156146
}
157-
val, err := k.table.GetResource(androidbinary.ResId(resId), resConfig)
147+
val, err := k.table.GetResource(resID, resConfig)
158148
if err != nil {
159149
return id
160150
}

apk/apk_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package apk
22

33
import (
4+
_ "image/jpeg"
5+
_ "image/png"
46
"testing"
57

68
"github.com/stretchr/testify/assert"

common.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/binary"
66
"io"
7-
"os"
87
"unicode/utf16"
98
)
109

@@ -51,10 +50,14 @@ type ResStringPoolHeader struct {
5150
StylesStart uint32
5251
}
5352

53+
type ResStringPoolSpan struct {
54+
FirstChar, LastChar uint32
55+
}
56+
5457
type ResStringPool struct {
5558
Header ResStringPoolHeader
5659
Strings []string
57-
Styles []string
60+
Styles []ResStringPoolSpan
5861
}
5962

6063
const NilResStringPoolRef = ResStringPoolRef(0xFFFFFFFF)
@@ -113,7 +116,9 @@ func readStringPool(sr *io.SectionReader) (*ResStringPool, error) {
113116
for i, start := range stringStarts {
114117
var str string
115118
var err error
116-
sr.Seek(int64(sp.Header.StringStart+start), os.SEEK_SET)
119+
if _, err := sr.Seek(int64(sp.Header.StringStart+start), seekStart); err != nil {
120+
return nil, err
121+
}
117122
if (sp.Header.Flags & UTF8_FLAG) == 0 {
118123
str, err = readUTF16(sr)
119124
} else {
@@ -125,20 +130,14 @@ func readStringPool(sr *io.SectionReader) (*ResStringPool, error) {
125130
sp.Strings[i] = str
126131
}
127132

128-
sp.Styles = make([]string, sp.Header.StyleCount)
133+
sp.Styles = make([]ResStringPoolSpan, sp.Header.StyleCount)
129134
for i, start := range styleStarts {
130-
var str string
131-
var err error
132-
sr.Seek(int64(sp.Header.StylesStart+start), os.SEEK_SET)
133-
if (sp.Header.Flags & UTF8_FLAG) == 0 {
134-
str, err = readUTF16(sr)
135-
} else {
136-
str, err = readUTF8(sr)
135+
if _, err := sr.Seek(int64(sp.Header.StylesStart+start), seekStart); err != nil {
136+
return nil, err
137137
}
138-
if err != nil {
138+
if err := binary.Read(sr, binary.LittleEndian, &sp.Styles[i]); err != nil {
139139
return nil, err
140140
}
141-
sp.Styles[i] = str
142141
}
143142

144143
return sp, nil
@@ -148,7 +147,7 @@ func readUTF16(sr *io.SectionReader) (string, error) {
148147
// read lenth of string
149148
size, err := readUTF16length(sr)
150149
if err != nil {
151-
return "", nil
150+
return "", err
152151
}
153152

154153
// read string value

common_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
var readStringPoolTests = []struct {
1111
input []uint8
1212
strings []string
13-
styles []string
13+
styles []ResStringPoolSpan
1414
}{
1515
{
1616
[]uint8{
@@ -29,18 +29,20 @@ var readStringPoolTests = []struct {
2929

3030
// StyleIndexes
3131
0x00, 0x00, 0x00, 0x00,
32-
0x04, 0x00, 0x00, 0x00,
32+
0x08, 0x00, 0x00, 0x00,
3333

3434
// Strings
3535
0x01, 0x00, 0x61, 0x00,
3636
0x01, 0x00, 0x42, 0x30,
3737

3838
// Styles
39-
0x01, 0x00, 0x63, 0x00,
40-
0x01, 0x00, 0x43, 0x30,
39+
0x01, 0x00, 0x00, 0x00,
40+
0x02, 0x00, 0x00, 0x00,
41+
0x03, 0x00, 0x00, 0x00,
42+
0x04, 0x00, 0x00, 0x00,
4143
},
42-
[]string{"a", "\3042"},
43-
[]string{"b", "\3043"},
44+
[]string{"a", "\u3042"},
45+
[]ResStringPoolSpan{{1, 2}, {3, 4}},
4446
},
4547
}
4648

@@ -52,10 +54,10 @@ func TestReadStringPool(t *testing.T) {
5254
if err != nil {
5355
t.Errorf("got %v want no error", err)
5456
}
55-
if reflect.DeepEqual(actual.Strings, tt.strings) {
57+
if !reflect.DeepEqual(actual.Strings, tt.strings) {
5658
t.Errorf("got %v want %v", actual.Strings, tt.strings)
5759
}
58-
if reflect.DeepEqual(actual.Styles, tt.styles) {
60+
if !reflect.DeepEqual(actual.Styles, tt.styles) {
5961
t.Errorf("got %v want %v", actual.Styles, tt.styles)
6062
}
6163
}

const_fallback.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build !go17
2+
3+
package androidbinary
4+
5+
import "os"
6+
7+
const seekStart = os.SEEK_SET

const_go17.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build go17
2+
3+
package androidbinary
4+
5+
import "io"
6+
7+
const seekStart = io.SeekStart

0 commit comments

Comments
 (0)