-
Notifications
You must be signed in to change notification settings - Fork 17
/
AmlImg.go
130 lines (105 loc) · 2.61 KB
/
AmlImg.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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/hzyitc/AmlImg/AmlImg"
)
func unpack(filePath, extractPath string) error {
img, err := AmlImg.NewReader(filePath, true)
if err != nil {
return errors.New("NewReader error: " + err.Error())
}
defer img.Close()
cmdfile, err := os.Create(extractPath + "/commands.txt")
if err != nil {
return errors.New("Create error: " + err.Error())
}
defer cmdfile.Close()
for i := 0; i < int(img.Header.ItemCount); i++ {
item := img.Items[i]
filename := fmt.Sprintf("%d.%s.%s", item.Id, item.Name, item.Type)
if item.ImgType == AmlImg.ImgType_Sparse {
filename += ".sparse"
}
println("Extracting ", extractPath+"/"+filename)
imtType := "unknown"
if item.ImgType == AmlImg.ImgType_Normal {
imtType = "normal"
} else if item.ImgType == AmlImg.ImgType_Sparse {
imtType = "sparse"
}
fmt.Fprintf(cmdfile, "%s:%s:%s:%s\n", item.Type, item.Name, imtType, filename)
file, err := os.Create(extractPath + "/" + filename)
if err != nil {
return errors.New("Create error:" + err.Error())
}
err = img.Seek(uint32(i), 0)
if err != nil {
file.Close()
return errors.New("Seek error:" + err.Error())
}
_, err = io.Copy(file, img)
if err != nil {
file.Close()
return errors.New("Copy error:" + err.Error())
}
file.Close()
}
return nil
}
func pack(filePath, dirPath string) error {
img, err := AmlImg.NewWriter()
if err != nil {
return errors.New("NewWriter error: " + err.Error())
}
cmdfile, err := os.Open(dirPath + "/commands.txt")
if err != nil {
return errors.New("Open error: " + err.Error())
}
defer cmdfile.Close()
scanner := bufio.NewScanner(cmdfile)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
txt := scanner.Text()
if txt == "" {
continue
} else if strings.HasPrefix(txt, "#") {
continue
}
c := strings.SplitN(txt, ":", 4)
Type := c[0]
Name := c[1]
filename := c[3]
imgType := AmlImg.ImgType_Normal
switch c[2] {
case "normal":
imgType = AmlImg.ImgType_Normal
case "sparse":
imgType = AmlImg.ImgType_Sparse
default:
return errors.New("unknown imgType: " + c[2])
}
img.Add(Type, Name, imgType, func(w io.Writer) error {
println("Packing ", filename)
file, err := os.Open(dirPath + "/" + filename)
if err != nil {
return errors.New("Open error: " + err.Error())
}
_, err = io.Copy(w, file)
return err
})
}
err = scanner.Err()
if err != nil {
return errors.New("scanner error: " + err.Error())
}
err = img.Write(filePath, 2)
if err != nil {
return errors.New("Write error: " + err.Error())
}
return nil
}