forked from mogaika/udf
/
udf.go
173 lines (150 loc) · 4.86 KB
/
udf.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
package udf
import (
"errors"
"io"
"unsafe"
)
// Udf is a wrapper around an .iso file that allows reading its ISO-13346 "UDF" data
type Udf struct {
r io.ReaderAt
isInited bool
pvd *PrimaryVolumeDescriptor
pd map[uint16]*PartitionDescriptor
lvd *LogicalVolumeDescriptor
fsd *FileSetDescriptor
root_fe FileEntryInterface
SECTOR_SIZE uint64
}
// NewUdfFromReader returns an Udf reader reading from a given file
func NewUdfFromReader(r io.ReaderAt) (*Udf, error) {
udf := &Udf{
r: r,
isInited: false,
pd: make(map[uint16]*PartitionDescriptor),
}
err := udf.init()
return udf, err
}
func (udf *Udf) init() (err error) {
if udf.isInited {
return
}
var anchorDesc *AnchorVolumeDescriptorPointer
for udf.SECTOR_SIZE = 512; udf.SECTOR_SIZE <= 32768; udf.SECTOR_SIZE <<= 1 {
anchorDesc = NewAnchorVolumeDescriptorPointer(udf.ReadSector(256))
if anchorDesc.Descriptor.TagIdentifier == DESCRIPTOR_ANCHOR_VOLUME_POINTER &&
anchorDesc.Descriptor.TagChecksum == anchorDesc.Descriptor.Checksum() {
break
}
}
//fmt.Printf("udf.SECTOR_SIZE = %d\n", udf.SECTOR_SIZE)
if anchorDesc.Descriptor.TagIdentifier != DESCRIPTOR_ANCHOR_VOLUME_POINTER ||
anchorDesc.Descriptor.TagChecksum != anchorDesc.Descriptor.Checksum() {
err = errors.New("couldn't find sector size")
return
}
for sector := uint64(anchorDesc.MainVolumeDescriptorSeq.Location); ; sector++ {
desc := NewDescriptor(udf.ReadSector(sector))
if desc.TagIdentifier == DESCRIPTOR_TERMINATING {
break
}
switch desc.TagIdentifier {
case DESCRIPTOR_PRIMARY_VOLUME:
udf.pvd = desc.PrimaryVolumeDescriptor()
case DESCRIPTOR_PARTITION:
pd := desc.PartitionDescriptor()
udf.pd[pd.PartitionNumber] = pd
case DESCRIPTOR_LOGICAL_VOLUME:
udf.lvd = desc.LogicalVolumeDescriptor()
}
}
// DEBUGGING ONLY
// udf.pvd.Show()
// for i, pd := range udf.pd {
// pd.Show(i)
// }
// udf.lvd.Show()
// DEBUGGING ONLY - end
for i, pMap := range udf.lvd.PartitionMaps {
if pMap.PartitionMapType != 2 {
// Check to error early if there is no match with a partition number
if _, ok := udf.pd[pMap.PartitionNumber]; !ok {
return errors.New("could not find partition number")
}
udf.lvd.PartitionMaps[i].PartitionStart = udf.pd[pMap.PartitionNumber].PartitionStartingLocation
continue
}
metaFile := NewFileEntry(0, udf.ReadSector(uint64(udf.pd[pMap.PartitionNumber].PartitionStartingLocation)))
if metaFile != nil && len(metaFile.GetAllocationDescriptors()) > 0 {
udf.lvd.PartitionMaps[i].PartitionStart = uint32(metaFile.GetAllocationDescriptors()[0].GetLocation()) + udf.pd[pMap.PartitionNumber].PartitionStartingLocation
}
}
partitionStart := udf.LogicalPartitionStart(udf.lvd.LogicalVolumeContentsUse.GetPartition())
udf.fsd = NewFileSetDescriptor(udf.ReadSector(partitionStart + uint64(udf.lvd.LogicalVolumeContentsUse.Location.LogicalBlockNumber)))
rootICB := udf.fsd.RootDirectoryICB
udf.root_fe = NewFileEntry(udf.lvd.LogicalVolumeContentsUse.GetPartition(), udf.ReadSector(udf.LogicalPartitionStart(rootICB.GetPartition())+rootICB.GetLocation()))
udf.isInited = true
return
}
func (udf *Udf) ReadSector(sectorNumber uint64) []byte {
return udf.ReadSectors(sectorNumber, 1)
}
func (udf *Udf) ReadSectors(sectorNumber uint64, sectorsCount uint64) []byte {
buf := make([]byte, udf.SECTOR_SIZE*sectorsCount)
read, err := udf.r.ReadAt(buf[:], int64(udf.SECTOR_SIZE*sectorNumber))
if err != nil {
panic(err)
}
/*if readed != int(udf.SECTOR_SIZE*sectorsCount) {
panic(readed)
}*/
return buf[:read]
}
func (udf *Udf) ReadDir(fe FileEntryInterface) []File {
udf.init()
if fe == nil {
fe = udf.root_fe
}
ps := udf.LogicalPartitionStart(fe.GetPartition())
adPos := fe.GetAllocationDescriptors()[0]
fdLen := uint64(adPos.GetLength())
fdBuf := udf.ReadSectors(ps+adPos.GetLocation(), (fdLen+udf.SECTOR_SIZE-1)/udf.SECTOR_SIZE)
fdOff := uint64(0)
result := make([]File, 0)
var dummy *FileIdentifierDescriptor
fidSize := int(unsafe.Sizeof(*dummy))
for uint32(fdOff) < adPos.GetLength() {
// Some Windows ISOs have some padding data that we can ignore?
if len(fdBuf[fdOff:]) < fidSize {
//fmt.Printf("WARNING: skipping incomplete data\n")
break
}
fid := NewFileIdentifierDescriptor(fdBuf[fdOff:])
if fid.FileIdentifier != "" {
result = append(result, File{
Udf: udf,
Fid: fid,
})
}
fdOff += fid.Len()
}
return result
}
// XXX - unused
func (udf *Udf) PhysicalPartitionStart(partition uint16) (physical uint64) {
if udf.pd == nil {
panic(udf)
} else {
return uint64(udf.pd[partition].PartitionStartingLocation)
}
}
func (udf *Udf) LogicalPartitionStart(partition uint16) (logical uint64) {
if udf.lvd == nil || len(udf.lvd.PartitionMaps) < 1 {
panic(udf)
} else {
return uint64(udf.lvd.PartitionMaps[partition].PartitionStart)
}
}
func (udf *Udf) GetReader() io.ReaderAt {
return udf.r
}