udf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package udf
  2. import (
  3. "io"
  4. )
  5. const SECTOR_SIZE = 2048
  6. type Udf struct {
  7. r io.ReaderAt
  8. isInited bool
  9. pvd *PrimaryVolumeDescriptor
  10. pd *PartitionDescriptor
  11. lvd *LogicalVolumeDescriptor
  12. fsd *FileSetDescriptor
  13. root_fe *FileEntry
  14. }
  15. func (udf *Udf) PartitionStart() uint64 {
  16. if udf.pd == nil {
  17. panic(udf)
  18. } else {
  19. return uint64(udf.pd.PartitionStartingLocation)
  20. }
  21. }
  22. func (udf *Udf) GetReader() io.ReaderAt {
  23. return udf.r
  24. }
  25. func (udf *Udf) ReadSectors(sectorNumber uint64, sectorsCount uint64) []byte {
  26. buf := make([]byte, SECTOR_SIZE*sectorsCount)
  27. readed, err := udf.r.ReadAt(buf[:], int64(SECTOR_SIZE*sectorNumber))
  28. if err != nil {
  29. panic(err)
  30. }
  31. if readed != int(SECTOR_SIZE*sectorsCount) {
  32. panic(readed)
  33. }
  34. return buf[:]
  35. }
  36. func (udf *Udf) ReadSector(sectorNumber uint64) []byte {
  37. return udf.ReadSectors(sectorNumber, 1)
  38. }
  39. func (udf *Udf) init() {
  40. if udf.isInited {
  41. return
  42. }
  43. anchorDesc := NewAnchorVolumeDescriptorPointer(udf.ReadSector(256))
  44. if anchorDesc.Descriptor.TagIdentifier != DESCRIPTOR_ANCHOR_VOLUME_POINTER {
  45. panic(anchorDesc.Descriptor.TagIdentifier)
  46. }
  47. for sector := uint64(anchorDesc.MainVolumeDescriptorSeq.Location); ; sector++ {
  48. desc := NewDescriptor(udf.ReadSector(sector))
  49. if desc.TagIdentifier == DESCRIPTOR_TERMINATING {
  50. break
  51. }
  52. switch desc.TagIdentifier {
  53. case DESCRIPTOR_PRIMARY_VOLUME:
  54. udf.pvd = desc.PrimaryVolumeDescriptor()
  55. case DESCRIPTOR_PARTITION:
  56. udf.pd = desc.PartitionDescriptor()
  57. case DESCRIPTOR_LOGICAL_VOLUME:
  58. udf.lvd = desc.LogicalVolumeDescriptor()
  59. }
  60. }
  61. partitionStart := udf.PartitionStart()
  62. udf.fsd = NewFileSetDescriptor(udf.ReadSector(partitionStart + udf.lvd.LogicalVolumeContentsUse.Location))
  63. udf.root_fe = NewFileEntry(udf.ReadSector(partitionStart + udf.fsd.RootDirectoryICB.Location))
  64. udf.isInited = true
  65. }
  66. func (udf *Udf) ReadDir(fe *FileEntry) []File {
  67. udf.init()
  68. if fe == nil {
  69. fe = udf.root_fe
  70. }
  71. ps := udf.PartitionStart()
  72. adPos := fe.AllocationDescriptors[0]
  73. fdLen := uint64(adPos.Length)
  74. fdBuf := udf.ReadSectors(ps+uint64(adPos.Location), (fdLen+SECTOR_SIZE-1)/SECTOR_SIZE)
  75. fdOff := uint64(0)
  76. result := make([]File, 0)
  77. for uint32(fdOff) < adPos.Length {
  78. fid := NewFileIdentifierDescriptor(fdBuf[fdOff:])
  79. if fid.FileIdentifier != "" {
  80. result = append(result, File{
  81. Udf: udf,
  82. Fid: fid,
  83. })
  84. }
  85. fdOff += fid.Len()
  86. }
  87. return result
  88. }
  89. func NewUdfFromReader(r io.ReaderAt) *Udf {
  90. udf := &Udf{
  91. r: r,
  92. isInited: false,
  93. }
  94. return udf
  95. }