forked from fornwall/apksigner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
182 lines (171 loc) · 4.02 KB
/
manifest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2014-2019 apksigner Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"bytes"
"io"
"sort"
"strings"
"golang.org/x/exp/errors/fmt"
)
// References about .jar/.apk manifest files:
// - https://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html
type Manifest map[string]Attributes
type Attributes []string
func (as Attributes) Without(key string) Attributes {
key = key + ": "
for i, v := range as {
if strings.HasPrefix(v, key) {
return append(as[:i:i], as[i+1:]...)
}
}
return as
}
func ParseManifest(r io.Reader) (Manifest, error) {
const namePrefix = "Name: "
m := Manifest{}
k, v := "", Attributes{}
// TODO: handle advanced base64-encoded attributes correctly
scan := bufio.NewScanner(
io.MultiReader(r, strings.NewReader("\r\n\r\n")))
for scan.Scan() {
line := scan.Text()
switch {
case line == "":
// new section
if len(v) > 0 {
m[k] = v
k, v = "", Attributes{}
}
case strings.HasPrefix(line, namePrefix):
k = line[len(namePrefix):]
case strings.HasPrefix(line, " "):
if len(v) == 0 {
k += line[1:]
} else {
// TODO: optimize (?)
v[len(v)-1] += line[1:]
}
default:
v = append(v, line)
}
}
if scan.Err() != nil {
return nil, fmt.Errorf("META-INF/MANIFEST.MF: %w", scan.Err())
}
return m, nil
}
func (m Manifest) WriteTo(w io.Writer) (n int64, err error) {
w = &wrap72{Writer: w}
write := func(s string) {
if err == nil {
wn, werr := w.Write([]byte(s))
n, err = n+int64(wn), werr
}
}
for _, attr := range m[""] {
write(attr + "\r\n")
}
if err != nil {
return
}
// Sort the manifest filenames
names := []string{}
for name := range m {
names = append(names, name)
}
sort.Strings(names)
if names[0] == "" {
names = names[1:]
}
// Print the sorted per-file sections of the manifest
for _, name := range names {
write("\r\n")
wn, werr := m.WriteEntry(w, name)
n, err = n+wn, werr
if err != nil {
return
}
}
// HACK: for now, adding extra newline for easier comparison with one real
// .apk file; try to remove it in future if OK
write("\r\n")
return
}
func (m Manifest) WriteEntry(w io.Writer, name string) (n int64, err error) {
w = &wrap72{Writer: w}
write := func(s string) {
if err == nil {
wn, werr := w.Write([]byte(s))
n, err = n+int64(wn), werr
}
}
// FIXME: verify that name has no '\n', etc.
write("Name: " + name + "\r\n")
for _, attr := range m[name] {
// TODO: handle advanced base64-encoded attributes correctly
write(attr + "\r\n")
}
return
}
// wrap72 writes to Writer, splitting any lines exceeding 72 bytes (including
// the terminating "\r\n"). Continuation of a split line is marked with a
// single space " " prefix.
type wrap72 struct {
io.Writer
n int
}
func (w *wrap72) Write(buf []byte) (n int, err error) {
const max = 70
for len(buf) > 0 {
i := bytes.IndexAny(buf, "\r\n")
if i == 0 {
// Newline characters (CR/LF) are safe to write
for i < len(buf) && (buf[i] == '\r' || buf[i] == '\n') {
i++
}
wn, werr := w.Writer.Write(buf[:i])
n += wn
if werr != nil {
return n, werr
}
w.n = 0
buf = buf[i:]
continue
}
if i == -1 {
i = len(buf)
}
if w.n == max {
_, werr := w.Writer.Write([]byte("\r\n "))
if werr != nil {
return n, werr
}
w.n = 1
}
// If line exceeds max length (not counting final CR/LF), split it
if w.n+i > 70 {
i = 70 - w.n
}
wn, werr := w.Writer.Write(buf[:i])
n += wn
if werr != nil {
return n, werr
}
w.n += i
buf = buf[i:]
}
return
}