| 1 |
package fat |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"math" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
"unicode/utf16" |
| 12 |
|
| 13 |
"github.com/mitchellh/go-fs" |
| 14 |
) |
| 15 |
|
| 16 |
type DirectoryAttr uint8 |
| 17 |
|
| 18 |
const ( |
| 19 |
AttrReadOnly DirectoryAttr = 0x01 |
| 20 |
AttrHidden = 0x02 |
| 21 |
AttrSystem = 0x04 |
| 22 |
AttrVolumeId = 0x08 |
| 23 |
AttrDirectory = 0x10 |
| 24 |
AttrArchive = 0x20 |
| 25 |
AttrLongName = AttrReadOnly | AttrHidden | AttrSystem | AttrVolumeId |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
const DirectoryEntrySize = 32 |
| 30 |
|
| 31 |
|
| 32 |
const LastLongEntryMask = 0x40 |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
type DirectoryCluster struct { |
| 37 |
entries []*DirectoryClusterEntry |
| 38 |
fat16Root bool |
| 39 |
startCluster uint32 |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
type DirectoryClusterEntry struct { |
| 45 |
name string |
| 46 |
ext string |
| 47 |
attr DirectoryAttr |
| 48 |
createTime time.Time |
| 49 |
accessTime time.Time |
| 50 |
writeTime time.Time |
| 51 |
cluster uint32 |
| 52 |
fileSize uint32 |
| 53 |
deleted bool |
| 54 |
|
| 55 |
longOrd uint8 |
| 56 |
longName string |
| 57 |
longChecksum uint8 |
| 58 |
} |
| 59 |
|
| 60 |
func DecodeDirectoryCluster(startCluster uint32, device fs.BlockDevice, fat *FAT) (*DirectoryCluster, error) { |
| 61 |
bs := fat.bs |
| 62 |
chain := fat.Chain(startCluster) |
| 63 |
data := make([]byte, uint32(len(chain))*bs.BytesPerCluster()) |
| 64 |
for i, clusterNumber := range chain { |
| 65 |
dataOffset := uint32(i) * bs.BytesPerCluster() |
| 66 |
devOffset := int64(bs.ClusterOffset(int(clusterNumber))) |
| 67 |
chainData := data[dataOffset : dataOffset+bs.BytesPerCluster()] |
| 68 |
|
| 69 |
if _, err := device.ReadAt(chainData, devOffset); err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
result, err := decodeDirectoryCluster(data, bs) |
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
|
| 79 |
result.startCluster = startCluster |
| 80 |
return result, nil |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
func DecodeFAT16RootDirectoryCluster(device fs.BlockDevice, bs *BootSectorCommon) (*DirectoryCluster, error) { |
| 86 |
data := make([]byte, DirectoryEntrySize*bs.RootEntryCount) |
| 87 |
if _, err := device.ReadAt(data, int64(bs.RootDirOffset())); err != nil { |
| 88 |
return nil, err |
| 89 |
} |
| 90 |
|
| 91 |
result, err := decodeDirectoryCluster(data, bs) |
| 92 |
if err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
|
| 96 |
result.fat16Root = true |
| 97 |
return result, nil |
| 98 |
} |
| 99 |
|
| 100 |
func decodeDirectoryCluster(data []byte, bs *BootSectorCommon) (*DirectoryCluster, error) { |
| 101 |
entries := make([]*DirectoryClusterEntry, 0, bs.RootEntryCount) |
| 102 |
for i := uint16(0); i < uint16(len(data)/DirectoryEntrySize); i++ { |
| 103 |
offset := i * DirectoryEntrySize |
| 104 |
entryData := data[offset : offset+DirectoryEntrySize] |
| 105 |
if entryData[0] == 0 { |
| 106 |
break |
| 107 |
} |
| 108 |
|
| 109 |
entry, err := DecodeDirectoryClusterEntry(entryData) |
| 110 |
if err != nil { |
| 111 |
return nil, err |
| 112 |
} |
| 113 |
|
| 114 |
entries = append(entries, entry) |
| 115 |
} |
| 116 |
|
| 117 |
result := &DirectoryCluster{ |
| 118 |
entries: entries, |
| 119 |
} |
| 120 |
|
| 121 |
return result, nil |
| 122 |
} |
| 123 |
|
| 124 |
func NewDirectoryCluster(start uint32, parent uint32, t time.Time) *DirectoryCluster { |
| 125 |
cluster := new(DirectoryCluster) |
| 126 |
cluster.startCluster = start |
| 127 |
|
| 128 |
|
| 129 |
cluster.entries = []*DirectoryClusterEntry{ |
| 130 |
{ |
| 131 |
accessTime: t, |
| 132 |
attr: AttrDirectory, |
| 133 |
cluster: start, |
| 134 |
createTime: t, |
| 135 |
name: ".", |
| 136 |
writeTime: t, |
| 137 |
}, |
| 138 |
{ |
| 139 |
accessTime: t, |
| 140 |
attr: AttrDirectory, |
| 141 |
cluster: parent, |
| 142 |
createTime: t, |
| 143 |
name: "..", |
| 144 |
writeTime: t, |
| 145 |
}, |
| 146 |
} |
| 147 |
|
| 148 |
return cluster |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
func NewFat16RootDirectoryCluster(bs *BootSectorCommon, label string) (*DirectoryCluster, error) { |
| 154 |
if bs.RootEntryCount == 0 { |
| 155 |
return nil, errors.New("root entry count is 0 in boot sector") |
| 156 |
} |
| 157 |
|
| 158 |
result := &DirectoryCluster{ |
| 159 |
entries: make([]*DirectoryClusterEntry, 1, bs.RootEntryCount), |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
result.entries[0] = &DirectoryClusterEntry{ |
| 164 |
attr: AttrVolumeId, |
| 165 |
name: label, |
| 166 |
cluster: 0, |
| 167 |
} |
| 168 |
|
| 169 |
return result, nil |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
func (d *DirectoryCluster) Bytes() []byte { |
| 174 |
result := make([]byte, cap(d.entries)*DirectoryEntrySize) |
| 175 |
|
| 176 |
for i, entry := range d.entries { |
| 177 |
offset := i * DirectoryEntrySize |
| 178 |
entryBytes := entry.Bytes() |
| 179 |
copy(result[offset:offset+DirectoryEntrySize], entryBytes) |
| 180 |
} |
| 181 |
|
| 182 |
return result |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
func (d *DirectoryCluster) WriteToDevice(device fs.BlockDevice, fat *FAT) error { |
| 187 |
if d.fat16Root { |
| 188 |
|
| 189 |
offset := int64(fat.bs.RootDirOffset()) |
| 190 |
if _, err := device.WriteAt(d.Bytes(), offset); err != nil { |
| 191 |
return err |
| 192 |
} |
| 193 |
} else { |
| 194 |
chain := &ClusterChain{ |
| 195 |
device: device, |
| 196 |
fat: fat, |
| 197 |
startCluster: d.startCluster, |
| 198 |
} |
| 199 |
|
| 200 |
if _, err := chain.Write(d.Bytes()); err != nil { |
| 201 |
return err |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return nil |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
func (d *DirectoryClusterEntry) Bytes() []byte { |
| 210 |
var result [DirectoryEntrySize]byte |
| 211 |
|
| 212 |
if d.longName != "" { |
| 213 |
runes := bytes.Runes([]byte(d.longName)) |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if len(runes) < 13 { |
| 218 |
runes = append(runes, 0) |
| 219 |
for len(runes) < 13 { |
| 220 |
runes = append(runes, 0xFFFF) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
result[0] = d.longOrd |
| 226 |
|
| 227 |
|
| 228 |
for i := 0; i < int(math.Min(float64(len(runes)), 5)); i++ { |
| 229 |
offset := 1 + (i * 2) |
| 230 |
data := result[offset : offset+2] |
| 231 |
binary.LittleEndian.PutUint16(data, uint16(runes[i])) |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
result[11] = byte(AttrLongName) |
| 236 |
|
| 237 |
|
| 238 |
result[12] = 0 |
| 239 |
|
| 240 |
|
| 241 |
result[13] = d.longChecksum |
| 242 |
|
| 243 |
|
| 244 |
for i := 0; i < 6; i++ { |
| 245 |
offset := 14 + (i * 2) |
| 246 |
data := result[offset : offset+2] |
| 247 |
binary.LittleEndian.PutUint16(data, uint16(runes[i+5])) |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
result[26] = 0 |
| 252 |
result[27] = 0 |
| 253 |
|
| 254 |
|
| 255 |
for i := 0; i < 2; i++ { |
| 256 |
offset := 28 + (i * 2) |
| 257 |
data := result[offset : offset+2] |
| 258 |
binary.LittleEndian.PutUint16(data, uint16(runes[i+11])) |
| 259 |
} |
| 260 |
} else { |
| 261 |
|
| 262 |
var simpleName string |
| 263 |
if d.name == "." || d.name == ".." { |
| 264 |
simpleName = d.name |
| 265 |
} else { |
| 266 |
simpleName = fmt.Sprintf("%s.%s", d.name, d.ext) |
| 267 |
} |
| 268 |
copy(result[0:11], shortNameEntryValue(simpleName)) |
| 269 |
|
| 270 |
|
| 271 |
result[11] = byte(d.attr) |
| 272 |
|
| 273 |
|
| 274 |
crtDate, crtTime, crtTenths := encodeDOSTime(d.createTime) |
| 275 |
result[13] = crtTenths |
| 276 |
binary.LittleEndian.PutUint16(result[14:16], crtTime) |
| 277 |
binary.LittleEndian.PutUint16(result[16:18], crtDate) |
| 278 |
|
| 279 |
|
| 280 |
accDate, _, _ := encodeDOSTime(d.accessTime) |
| 281 |
binary.LittleEndian.PutUint16(result[18:20], accDate) |
| 282 |
|
| 283 |
|
| 284 |
binary.LittleEndian.PutUint16(result[20:22], uint16(d.cluster>>16)) |
| 285 |
|
| 286 |
|
| 287 |
wrtDate, wrtTime, _ := encodeDOSTime(d.writeTime) |
| 288 |
binary.LittleEndian.PutUint16(result[22:24], wrtTime) |
| 289 |
binary.LittleEndian.PutUint16(result[24:26], wrtDate) |
| 290 |
|
| 291 |
|
| 292 |
binary.LittleEndian.PutUint16(result[26:28], uint16(d.cluster&0xFFFF)) |
| 293 |
|
| 294 |
|
| 295 |
binary.LittleEndian.PutUint32(result[28:32], d.fileSize) |
| 296 |
} |
| 297 |
|
| 298 |
return result[:] |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
func (d *DirectoryClusterEntry) IsLong() bool { |
| 303 |
return (d.attr & AttrLongName) == AttrLongName |
| 304 |
} |
| 305 |
|
| 306 |
func (d *DirectoryClusterEntry) IsVolumeId() bool { |
| 307 |
return (d.attr & AttrVolumeId) == AttrVolumeId |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
func DecodeDirectoryClusterEntry(data []byte) (*DirectoryClusterEntry, error) { |
| 313 |
var result DirectoryClusterEntry |
| 314 |
|
| 315 |
|
| 316 |
result.attr = DirectoryAttr(data[11]) |
| 317 |
if (result.attr & AttrLongName) == AttrLongName { |
| 318 |
result.longOrd = data[0] |
| 319 |
|
| 320 |
chars := make([]uint16, 13) |
| 321 |
for i := 0; i < 5; i++ { |
| 322 |
offset := 1 + (i * 2) |
| 323 |
chars[i] = binary.LittleEndian.Uint16(data[offset : offset+2]) |
| 324 |
} |
| 325 |
|
| 326 |
for i := 0; i < 6; i++ { |
| 327 |
offset := 14 + (i * 2) |
| 328 |
chars[i+5] = binary.LittleEndian.Uint16(data[offset : offset+2]) |
| 329 |
} |
| 330 |
|
| 331 |
for i := 0; i < 2; i++ { |
| 332 |
offset := 28 + (i * 2) |
| 333 |
chars[i+11] = binary.LittleEndian.Uint16(data[offset : offset+2]) |
| 334 |
} |
| 335 |
|
| 336 |
result.longName = string(utf16.Decode(chars)) |
| 337 |
result.longChecksum = data[13] |
| 338 |
} else { |
| 339 |
result.deleted = data[0] == 0xE5 |
| 340 |
|
| 341 |
|
| 342 |
if data[0] == 0x05 { |
| 343 |
data[0] = 0xE5 |
| 344 |
} |
| 345 |
|
| 346 |
result.name = strings.TrimRight(string(data[0:8]), " ") |
| 347 |
result.ext = strings.TrimRight(string(data[8:11]), " ") |
| 348 |
|
| 349 |
|
| 350 |
createTimeTenths := data[13] |
| 351 |
createTimeWord := binary.LittleEndian.Uint16(data[14:16]) |
| 352 |
createDateWord := binary.LittleEndian.Uint16(data[16:18]) |
| 353 |
result.createTime = decodeDOSTime(createDateWord, createTimeWord, createTimeTenths) |
| 354 |
|
| 355 |
|
| 356 |
accessDateWord := binary.LittleEndian.Uint16(data[18:20]) |
| 357 |
result.accessTime = decodeDOSTime(accessDateWord, 0, 0) |
| 358 |
|
| 359 |
|
| 360 |
writeTimeWord := binary.LittleEndian.Uint16(data[22:24]) |
| 361 |
writeDateWord := binary.LittleEndian.Uint16(data[24:26]) |
| 362 |
result.writeTime = decodeDOSTime(writeDateWord, writeTimeWord, 0) |
| 363 |
|
| 364 |
|
| 365 |
result.cluster = uint32(binary.LittleEndian.Uint16(data[20:22])) |
| 366 |
result.cluster <<= 4 |
| 367 |
result.cluster |= uint32(binary.LittleEndian.Uint16(data[26:28])) |
| 368 |
|
| 369 |
|
| 370 |
result.fileSize = binary.LittleEndian.Uint32(data[28:32]) |
| 371 |
} |
| 372 |
|
| 373 |
return &result, nil |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
func NewLongDirectoryClusterEntry(name string, shortName string) ([]*DirectoryClusterEntry, error) { |
| 380 |
|
| 381 |
checksum := checksumShortName(shortNameEntryValue(shortName)) |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
numLongEntries := len(name) / 13 |
| 386 |
if len(name)%13 != 0 { |
| 387 |
numLongEntries++ |
| 388 |
} |
| 389 |
|
| 390 |
entries := make([]*DirectoryClusterEntry, numLongEntries) |
| 391 |
for i := 0; i < numLongEntries; i++ { |
| 392 |
entries[i] = new(DirectoryClusterEntry) |
| 393 |
entry := entries[i] |
| 394 |
entry.attr = AttrLongName |
| 395 |
entry.longOrd = uint8(numLongEntries - i) |
| 396 |
|
| 397 |
if i == 0 { |
| 398 |
entry.longOrd |= LastLongEntryMask |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
j := (numLongEntries - i - 1) * 13 |
| 403 |
k := j + 13 |
| 404 |
if k > len(name) { |
| 405 |
k = len(name) |
| 406 |
} |
| 407 |
|
| 408 |
entry.longChecksum = checksum |
| 409 |
entry.longName = name[j:k] |
| 410 |
} |
| 411 |
|
| 412 |
return entries, nil |
| 413 |
} |
| 414 |
|
| 415 |
func decodeDOSTime(date, dosTime uint16, tenths uint8) time.Time { |
| 416 |
return time.Date( |
| 417 |
1980+int(date>>9), |
| 418 |
time.Month((date>>5)&0x0F), |
| 419 |
int(date&0x1F), |
| 420 |
int(dosTime>>11), |
| 421 |
int((dosTime>>5)&0x3F), |
| 422 |
int((dosTime&0x1F)*2), |
| 423 |
int(tenths)*10*int(time.Millisecond), |
| 424 |
time.Local) |
| 425 |
} |
| 426 |
|
| 427 |
func encodeDOSTime(t time.Time) (uint16, uint16, uint8) { |
| 428 |
var date uint16 = uint16((t.Year() - 1980) << 9) |
| 429 |
date |= uint16(t.Month()) << 5 |
| 430 |
date += uint16(t.Day() & 0xFF) |
| 431 |
|
| 432 |
var time uint16 = uint16(t.Hour() << 11) |
| 433 |
time |= uint16(t.Minute() << 5) |
| 434 |
time += uint16(t.Second() / 2) |
| 435 |
|
| 436 |
var tenths uint8 |
| 437 |
|
| 438 |
|
| 439 |
return date, time, tenths |
| 440 |
} |