-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
102 lines (95 loc) · 2.3 KB
/
header.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
package abdecrypt
import (
"bufio"
"encoding/hex"
"fmt"
"io"
"strconv"
"strings"
)
const (
Magic string = "ANDROID BACKUP"
)
// AndroidBackupHeader represents Android backup header.
type AndroidBackupHeader struct {
Version int
Compressed bool
UserSalt []byte
ChecksumSalt []byte
Rounds int
UserIV []byte
MasterKeyBlob []byte
}
// ReadHeader reads Android backup header from reader r.
// Note that ReadHeader returns a reader with remaining data
// to be used to get backup payload. Original reader will be
// advanced by ReadHeader.
func ReadHeader(r io.Reader) (*AndroidBackupHeader, io.Reader, error) {
ab := &AndroidBackupHeader{}
br := bufio.NewReader(r)
for i := 1; i <= 9; i++ {
line, err := br.ReadString('\n')
if err != nil {
return nil, nil, err
}
line = strings.TrimRight(line, "\n")
switch i {
case 1:
if line != Magic {
return nil, nil, fmt.Errorf("wrong magic: %s", line)
}
case 2:
version, err := strconv.Atoi(line)
if err != nil {
return nil, nil, fmt.Errorf("invalid version: %s", line)
}
ab.Version = version
case 3:
switch line {
case "0":
ab.Compressed = false
case "1":
ab.Compressed = true
default:
return nil, nil, fmt.Errorf("invalid compression info: %s", line)
}
case 4:
switch line {
case "AES-256":
default:
return nil, nil, fmt.Errorf("unsupported encryption algorithm: %s", line)
}
case 5:
userSalt, err := hex.DecodeString(line)
if err != nil {
return nil, nil, fmt.Errorf("error parsing user salt: %v", err)
}
ab.UserSalt = userSalt
case 6:
checksumSalt, err := hex.DecodeString(line)
if err != nil {
return nil, nil, fmt.Errorf("error parsing checksum salt: %v", err)
}
ab.ChecksumSalt = checksumSalt
case 7:
rounds, err := strconv.Atoi(line)
if err != nil {
return nil, nil, fmt.Errorf("error parsing rounds: %v", err)
}
ab.Rounds = rounds
case 8:
userIV, err := hex.DecodeString(line)
if err != nil {
return nil, nil, fmt.Errorf("error parsing user IV: %v", err)
}
ab.UserIV = userIV
case 9:
masterKeyBlob, err := hex.DecodeString(line)
if err != nil {
return nil, nil, fmt.Errorf("error parsing master key blob: %v", err)
}
ab.MasterKeyBlob = masterKeyBlob
}
}
return ab, br, nil
}