-
Notifications
You must be signed in to change notification settings - Fork 1
/
wim.go
199 lines (185 loc) · 4.86 KB
/
wim.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"unicode/utf16"
"unicode/utf8"
)
// WIM was generated 2023-02-04 19:27:11 by https://xml-to-go.github.io/ in Ukraine.
type WIM struct {
Path string
XMLName xml.Name `xml:"WIM"`
Text string `xml:",chardata"`
TOTALBYTES struct {
Text string `xml:",chardata"`
} `xml:"TOTALBYTES"`
IMAGE []struct {
Summary string
Text string `xml:",chardata"`
INDEX string `xml:"INDEX,attr"`
DIRCOUNT struct {
Text string `xml:",chardata"`
} `xml:"DIRCOUNT"`
FILECOUNT struct {
Text string `xml:",chardata"`
} `xml:"FILECOUNT"`
TOTALBYTES struct {
Text string `xml:",chardata"`
} `xml:"TOTALBYTES"`
HARDLINKBYTES struct {
Text string `xml:",chardata"`
} `xml:"HARDLINKBYTES"`
CREATIONTIME struct {
Text string `xml:",chardata"`
HIGHPART struct {
Text string `xml:",chardata"`
} `xml:"HIGHPART"`
LOWPART struct {
Text string `xml:",chardata"`
} `xml:"LOWPART"`
} `xml:"CREATIONTIME"`
LASTMODIFICATIONTIME struct {
Text string `xml:",chardata"`
HIGHPART struct {
Text string `xml:",chardata"`
} `xml:"HIGHPART"`
LOWPART struct {
Text string `xml:",chardata"`
} `xml:"LOWPART"`
} `xml:"LASTMODIFICATIONTIME"`
WIMBOOT struct {
Text string `xml:",chardata"`
} `xml:"WIMBOOT"`
WINDOWS struct {
Text string `xml:",chardata"`
ARCH struct {
Text string `xml:",chardata"`
} `xml:"ARCH"`
PRODUCTNAME struct {
Text string `xml:",chardata"`
} `xml:"PRODUCTNAME"`
EDITIONID struct {
Text string `xml:",chardata"`
} `xml:"EDITIONID"`
INSTALLATIONTYPE struct {
Text string `xml:",chardata"`
} `xml:"INSTALLATIONTYPE"`
SERVICINGDATA struct {
Text string `xml:",chardata"`
GDRDUREVISION struct {
Text string `xml:",chardata"`
} `xml:"GDRDUREVISION"`
PKEYCONFIGVERSION struct {
Text string `xml:",chardata"`
} `xml:"PKEYCONFIGVERSION"`
} `xml:"SERVICINGDATA"`
HAL struct {
Text string `xml:",chardata"`
} `xml:"HAL"`
PRODUCTTYPE struct {
Text string `xml:",chardata"`
} `xml:"PRODUCTTYPE"`
PRODUCTSUITE struct {
Text string `xml:",chardata"`
} `xml:"PRODUCTSUITE"`
LANGUAGES struct {
Text string `xml:",chardata"`
LANGUAGE struct {
Text string `xml:",chardata"`
} `xml:"LANGUAGE"`
DEFAULT struct {
Text string `xml:",chardata"`
} `xml:"DEFAULT"`
} `xml:"LANGUAGES"`
VERSION struct {
Text string `xml:",chardata"`
MAJOR struct {
Text string `xml:",chardata"`
} `xml:"MAJOR"`
MINOR struct {
Text string `xml:",chardata"`
} `xml:"MINOR"`
BUILD struct {
Text string `xml:",chardata"`
} `xml:"BUILD"`
SPBUILD struct {
Text string `xml:",chardata"`
} `xml:"SPBUILD"`
SPLEVEL struct {
Text string `xml:",chardata"`
} `xml:"SPLEVEL"`
} `xml:"VERSION"`
SYSTEMROOT struct {
Text string `xml:",chardata"`
} `xml:"SYSTEMROOT"`
} `xml:"WINDOWS"`
NAME struct {
Text string `xml:",chardata"`
} `xml:"NAME"`
DESCRIPTION struct {
Text string `xml:",chardata"`
} `xml:"DESCRIPTION"`
FLAGS struct {
Text string `xml:",chardata"`
} `xml:"FLAGS"`
DISPLAYNAME struct {
Text string `xml:",chardata"`
} `xml:"DISPLAYNAME"`
DISPLAYDESCRIPTION struct {
Text string `xml:",chardata"`
} `xml:"DISPLAYDESCRIPTION"`
} `xml:"IMAGE"`
}
func DecodeUTF16(b []byte) (string, error) {
if len(b)%2 != 0 {
return "", fmt.Errorf("Must have even length byte slice")
}
u16s := make([]uint16, 1)
ret := &bytes.Buffer{}
b8buf := make([]byte, 4)
lb := len(b)
for i := 0; i < lb; i += 2 {
u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8)
r := utf16.Decode(u16s)
n := utf8.EncodeRune(b8buf, r[0])
ret.Write(b8buf[:n])
}
return ret.String(), nil
}
func WIMInfo(wim string) (*WIM, error) {
_, err := Run("wiminfo", "--extract-xml="+wim+"-info.xml", wim)
if err != nil {
return nil, err
}
defer os.Remove(wim + "-info.xml")
wimxmlb, err := ioutil.ReadFile(wim + "-info.xml")
if err != nil {
return nil, err
}
wimxml, err := DecodeUTF16(wimxmlb)
if err != nil {
return nil, err
}
wiminfo := &WIM{Path: wim}
err = xml.Unmarshal([]byte(wimxml), wiminfo)
if err != nil {
return nil, err
}
for i := 0; i < len(wiminfo.IMAGE); i++ {
img := wiminfo.IMAGE[i]
edition := img.NAME.Text
if img.DESCRIPTION.Text != "" && img.DESCRIPTION.Text != img.NAME.Text {
edition += " - " + img.DESCRIPTION.Text
}
edition += " (NT" + img.WINDOWS.VERSION.MAJOR.Text + "." + img.WINDOWS.VERSION.MINOR.Text + " Build " + img.WINDOWS.VERSION.BUILD.Text
if img.WINDOWS.VERSION.SPLEVEL.Text != "" && img.WINDOWS.VERSION.SPLEVEL.Text != "0" {
edition += " Service Pack " + img.WINDOWS.VERSION.SPLEVEL.Text + " v" + img.WINDOWS.VERSION.SPBUILD.Text
}
edition += ")"
wiminfo.IMAGE[i].Summary = edition
}
return wiminfo, nil
}