-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.go
More file actions
299 lines (259 loc) · 8.32 KB
/
Copy pathstring.go
File metadata and controls
299 lines (259 loc) · 8.32 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package parcel
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"unicode/utf16"
)
const (
// maxStringChars is the maximum number of UTF-16 code units (or bytes for
// UTF-8 strings) allowed when reading strings from a parcel. The binder
// transaction limit is 1MB, so a string cannot validly contain more than
// 512K UTF-16 chars (1MB / 2 bytes per char). This guards against OOM
// from malicious or corrupted parcels.
maxStringChars = 512 * 1024
)
// WriteString16 writes a string in UTF-16LE wire format.
// Writes int32 char count (number of UTF-16 code units),
// then UTF-16LE encoded data with a null terminator, padded to 4 bytes.
// An empty string writes length 0 followed by a UTF-16 null terminator.
// Use WriteNullString16 to write a null sentinel (-1 length).
func (p *Parcel) WriteString16(
s string,
) {
runes := []rune(s)
encoded := utf16.Encode(runes)
charCount := len(encoded)
// Silently drop strings exceeding int32 max UTF-16 code units.
// In practice, Go strings cannot reach this size on any real system.
if charCount > math.MaxInt32 {
return
}
p.WriteInt32(int32(charCount))
// Write UTF-16LE encoded data plus null terminator.
dataBytes := (charCount + 1) * 2
buf := p.grow(dataBytes)
for i, u := range encoded {
binary.LittleEndian.PutUint16(buf[i*2:], u)
}
// Null terminator.
binary.LittleEndian.PutUint16(buf[charCount*2:], 0)
}
// WriteNullString16 writes a null string sentinel (-1 length) in UTF-16LE wire format.
func (p *Parcel) WriteNullString16() {
p.WriteInt32(-1)
}
// WriteNullableString16 writes a nullable string in UTF-16LE wire format.
// If s is nil, writes a null sentinel (-1). Otherwise writes the string.
func (p *Parcel) WriteNullableString16(s *string) {
if s == nil {
p.WriteNullString16()
return
}
p.WriteString16(*s)
}
// ReadString16 reads a string in UTF-16LE wire format.
// Reads int32 char count. If -1, returns empty string.
// Then reads (charCount+1)*2 bytes (including null terminator), padded to 4 bytes.
func (p *Parcel) ReadString16() (string, error) {
charCount, err := p.ReadInt32()
if err != nil {
return "", err
}
if charCount < 0 {
// Null string on the wire (charCount == -1). Returns empty string
// because Go strings cannot represent null. Use ReadNullableString16
// to distinguish null from empty.
return "", nil
}
if int(charCount) > maxStringChars {
return "", fmt.Errorf("parcel: ReadString16 charCount %d exceeds limit %d", charCount, maxStringChars)
}
// Read (charCount+1)*2 bytes for data plus null terminator.
dataBytes := (int(charCount) + 1) * 2
b, err := p.read(dataBytes)
if err != nil {
return "", err
}
units := make([]uint16, charCount)
for i := range units {
units[i] = binary.LittleEndian.Uint16(b[i*2:])
}
return string(utf16.Decode(units)), nil
}
// ReadNullableString16 reads a string in UTF-16LE wire format,
// distinguishing null from empty. Returns nil for null strings
// (charCount == -1) and a pointer to the string value otherwise.
func (p *Parcel) ReadNullableString16() (*string, error) {
charCount, err := p.ReadInt32()
if err != nil {
return nil, err
}
if charCount < 0 {
return nil, nil
}
if int(charCount) > maxStringChars {
return nil, fmt.Errorf("parcel: ReadNullableString16 charCount %d exceeds limit %d", charCount, maxStringChars)
}
// Read (charCount+1)*2 bytes for data plus null terminator.
dataBytes := (int(charCount) + 1) * 2
b, err := p.read(dataBytes)
if err != nil {
return nil, err
}
units := make([]uint16, charCount)
for i := range units {
units[i] = binary.LittleEndian.Uint16(b[i*2:])
}
s := string(utf16.Decode(units))
return &s, nil
}
// WriteString writes a string in UTF-8 wire format (for @utf8InCpp).
// Writes int32 byte length, then UTF-8 bytes with a null terminator,
// padded to 4 bytes. An empty string writes length 0 followed by a null byte.
// Use WriteNullString to write a null sentinel (-1 length).
func (p *Parcel) WriteString(
s string,
) {
byteLen := len(s)
// Silently drop strings exceeding int32 max bytes.
// In practice, Go strings cannot reach this size on any real system.
if byteLen > math.MaxInt32 {
return
}
p.WriteInt32(int32(byteLen))
// Write UTF-8 bytes plus null terminator.
buf := p.grow(byteLen + 1)
copy(buf[:byteLen], s)
buf[byteLen] = 0
}
// WriteNullString writes a null string sentinel (-1 length) in UTF-8 wire format.
func (p *Parcel) WriteNullString() {
p.WriteInt32(-1)
}
// WriteNullableString writes a nullable string in UTF-8 wire format.
// If s is nil, writes a null sentinel (-1). Otherwise writes the string.
func (p *Parcel) WriteNullableString(s *string) {
if s == nil {
p.WriteNullString()
return
}
p.WriteString(*s)
}
// ReadString reads a string in UTF-8 wire format (for @utf8InCpp).
// Reads int32 byte length. If -1, returns empty string.
// Then reads byteLen+1 bytes (including null terminator), padded to 4 bytes.
func (p *Parcel) ReadString() (string, error) {
byteLen, err := p.ReadInt32()
if err != nil {
return "", err
}
if byteLen < 0 {
// Null string on the wire (byteLen < 0). Returns empty string
// because Go strings cannot represent null. Use ReadNullableString
// to distinguish null from empty.
return "", nil
}
// UTF-8 uses at most 4 bytes per character.
if int(byteLen) > maxStringChars*4 {
return "", fmt.Errorf("parcel: ReadString byteLen %d exceeds limit %d", byteLen, maxStringChars*4)
}
// Read byteLen+1 bytes for data plus null terminator.
b, err := p.read(int(byteLen) + 1)
if err != nil {
return "", err
}
return string(b[:byteLen]), nil
}
// ReadNullableString reads a string in UTF-8 wire format,
// distinguishing null from empty. Returns nil for null strings
// (byteLen < 0) and a pointer to the string value otherwise.
func (p *Parcel) ReadNullableString() (*string, error) {
byteLen, err := p.ReadInt32()
if err != nil {
return nil, err
}
if byteLen < 0 {
return nil, nil
}
// UTF-8 uses at most 4 bytes per character.
if int(byteLen) > maxStringChars*4 {
return nil, fmt.Errorf("parcel: ReadNullableString byteLen %d exceeds limit %d", byteLen, maxStringChars*4)
}
// Read byteLen+1 bytes for data plus null terminator.
b, err := p.read(int(byteLen) + 1)
if err != nil {
return nil, err
}
s := string(b[:byteLen])
return &s, nil
}
// WriteStringList writes a list of strings in the format used by Java's
// Parcel.writeStringList: int32 count (or -1 for nil), then count strings
// each written via writeString (UTF-16LE wire format, i.e. WriteString16).
func (p *Parcel) WriteStringList(
items []string,
) {
if items == nil {
p.WriteInt32(-1)
return
}
p.WriteInt32(int32(len(items)))
for _, s := range items {
p.WriteString16(s)
}
}
// ReadStringList reads a list of strings written by Java's
// Parcel.writeStringList (or createStringArrayList).
// Returns nil for a null list (count == -1).
func (p *Parcel) ReadStringList() ([]string, error) {
count, err := p.ReadInt32()
if err != nil {
return nil, err
}
if count < 0 {
return nil, nil
}
if int(count) > maxStringChars {
return nil, fmt.Errorf("parcel: ReadStringList count %d exceeds limit %d", count, maxStringChars)
}
items := make([]string, count)
for i := range items {
s, err := p.ReadString16()
if err != nil {
return nil, fmt.Errorf("parcel: ReadStringList[%d]: %w", i, err)
}
items[i] = s
}
return items, nil
}
// WriteCString writes a null-terminated C string (no length prefix).
// The string data plus its null terminator are padded to 4-byte alignment.
// This matches C++ Parcel::writeCString.
func (p *Parcel) WriteCString(s string) {
n := len(s) + 1 // include null terminator
buf := p.grow(n)
copy(buf[:len(s)], s)
buf[len(s)] = 0
}
// ReadCString reads a null-terminated C string (no length prefix).
// Scans forward from the current position for a null byte, then advances
// the position past the null byte, padded to 4-byte alignment.
// This matches C++ Parcel::readCString.
func (p *Parcel) ReadCString() (string, error) {
if p.pos >= p.Len() {
return "", fmt.Errorf("parcel: ReadCString at end of parcel")
}
avail := p.Data()[p.pos:p.Len()]
idx := bytes.IndexByte(avail, 0)
if idx < 0 {
return "", fmt.Errorf("parcel: ReadCString no null terminator found")
}
s := string(avail[:idx])
// Advance position past the null terminator, padded to 4 bytes.
raw := idx + 1
aligned := (raw + 3) &^ 3
p.SetPosition(p.pos + aligned)
return s, nil
}