descr.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package udf
  2. import (
  3. "time"
  4. )
  5. const (
  6. DESCRIPTOR_PRIMARY_VOLUME = 0x1
  7. DESCRIPTOR_ANCHOR_VOLUME_POINTER = 0x2
  8. DESCRIPTOR_VOLUME_POINTER = 0x3
  9. DESCRIPTOR_IMPLEMENTATION_USE_VOLUME = 0x4
  10. DESCRIPTOR_PARTITION = 0x5
  11. DESCRIPTOR_LOGICAL_VOLUME = 0x6
  12. DESCRIPTOR_UNALLOCATED = 0x7
  13. DESCRIPTOR_TERMINATING = 0x8
  14. DESCRIPTOR_FILE_SET = 0x100
  15. DESCRIPTOR_IDENTIFIER = 0x101
  16. DESCRIPTOR_ALLOCATION_EXTENT = 0x102
  17. DESCRIPTOR_INDIRECT_ENTRY = 0x103
  18. DESCRIPTOR_TERMINAL_ENTRY = 0x104
  19. DESCRIPTOR_FILE_ENTRY = 0x105
  20. )
  21. type Descriptor struct {
  22. TagIdentifier uint16
  23. DescriptorVersion uint16
  24. TagChecksum uint8
  25. TagSerialNumber uint16
  26. DescriptorCRC uint16
  27. DescriptorCRCLength uint16
  28. TagLocation uint32
  29. data []byte
  30. }
  31. func (d *Descriptor) Data() []byte {
  32. buf := make([]byte, len(d.data))
  33. copy(buf, d.data[16:])
  34. return buf
  35. }
  36. func (d *Descriptor) FromBytes(b []byte) *Descriptor {
  37. d.TagIdentifier = rl_u16(b[0:])
  38. d.DescriptorVersion = rl_u16(b[2:])
  39. d.TagChecksum = r_u8(b[3:])
  40. d.TagSerialNumber = rl_u16(b[6:])
  41. d.DescriptorCRC = rl_u16(b[8:])
  42. d.DescriptorCRCLength = rl_u16(b[10:])
  43. d.TagLocation = rl_u32(b[12:])
  44. d.data = b[:]
  45. return d
  46. }
  47. func NewDescriptor(b []byte) *Descriptor {
  48. return new(Descriptor).FromBytes(b)
  49. }
  50. type AnchorVolumeDescriptorPointer struct {
  51. Descriptor Descriptor
  52. MainVolumeDescriptorSeq Extent
  53. ReserveVolumeDescriptorSeq Extent
  54. }
  55. func (ad *AnchorVolumeDescriptorPointer) FromBytes(b []byte) *AnchorVolumeDescriptorPointer {
  56. ad.Descriptor.FromBytes(b)
  57. ad.MainVolumeDescriptorSeq = NewExtent(b[16:])
  58. ad.ReserveVolumeDescriptorSeq = NewExtent(b[24:])
  59. return ad
  60. }
  61. func NewAnchorVolumeDescriptorPointer(b []byte) *AnchorVolumeDescriptorPointer {
  62. return new(AnchorVolumeDescriptorPointer).FromBytes(b)
  63. }
  64. func (d *Descriptor) AnchorVolumeDescriptorPointer() *AnchorVolumeDescriptorPointer {
  65. return NewAnchorVolumeDescriptorPointer(d.data)
  66. }
  67. type PrimaryVolumeDescriptor struct {
  68. Descriptor Descriptor
  69. VolumeDescriptorSequenceNumber uint32
  70. PrimaryVolumeDescriptorNumber uint32
  71. VolumeIdentifier string
  72. VolumeSequenceNumber uint16
  73. MaximumVolumeSequenceNumber uint16
  74. InterchangeLevel uint16
  75. MaximumInterchangeLevel uint16
  76. CharacterSetList uint32
  77. MaximumCharacterSetList uint32
  78. VolumeSetIdentifier string
  79. VolumeAbstract Extent
  80. VolumeCopyrightNoticeExtent Extent
  81. ApplicationIdentifier EntityID
  82. RecordingDateTime time.Time
  83. ImplementationIdentifier EntityID
  84. ImplementationUse []byte
  85. PredecessorVolumeDescriptorSequenceLocation uint32
  86. Flags uint16
  87. }
  88. func (pvd *PrimaryVolumeDescriptor) FromBytes(b []byte) *PrimaryVolumeDescriptor {
  89. pvd.Descriptor.FromBytes(b)
  90. pvd.VolumeDescriptorSequenceNumber = rl_u32(b[16:])
  91. pvd.PrimaryVolumeDescriptorNumber = rl_u32(b[20:])
  92. pvd.VolumeIdentifier = r_dstring(b[24:], 32)
  93. pvd.VolumeSequenceNumber = rl_u16(b[56:])
  94. pvd.MaximumVolumeSequenceNumber = rl_u16(b[58:])
  95. pvd.InterchangeLevel = rl_u16(b[60:])
  96. pvd.MaximumInterchangeLevel = rl_u16(b[62:])
  97. pvd.CharacterSetList = rl_u32(b[64:])
  98. pvd.MaximumCharacterSetList = rl_u32(b[68:])
  99. pvd.VolumeSetIdentifier = r_dstring(b[72:], 128)
  100. pvd.VolumeAbstract = NewExtent(b[328:])
  101. pvd.VolumeCopyrightNoticeExtent = NewExtent(b[336:])
  102. pvd.ApplicationIdentifier = NewEntityID(b[344:])
  103. pvd.RecordingDateTime = r_timestamp(b[376:])
  104. pvd.ImplementationIdentifier = NewEntityID(b[388:])
  105. pvd.ImplementationUse = b[420:484]
  106. pvd.PredecessorVolumeDescriptorSequenceLocation = rl_u32(b[484:])
  107. pvd.Flags = rl_u16(b[488:])
  108. return pvd
  109. }
  110. func NewPrimaryVolumeDescriptor(b []byte) *PrimaryVolumeDescriptor {
  111. return new(PrimaryVolumeDescriptor).FromBytes(b)
  112. }
  113. func (d *Descriptor) PrimaryVolumeDescriptor() *PrimaryVolumeDescriptor {
  114. return NewPrimaryVolumeDescriptor(d.data)
  115. }
  116. type PartitionDescriptor struct {
  117. Descriptor Descriptor
  118. VolumeDescriptorSequenceNumber uint32
  119. PartitionFlags uint16
  120. PartitionNumber uint16
  121. PartitionContents EntityID
  122. PartitionContentsUse []byte
  123. AccessType uint32
  124. PartitionStartingLocation uint32
  125. PartitionLength uint32
  126. ImplementationIdentifier EntityID
  127. ImplementationUse []byte
  128. }
  129. func (pd *PartitionDescriptor) FromBytes(b []byte) *PartitionDescriptor {
  130. pd.Descriptor.FromBytes(b)
  131. pd.VolumeDescriptorSequenceNumber = rl_u32(b[16:])
  132. pd.PartitionFlags = rl_u16(b[20:])
  133. pd.PartitionNumber = rl_u16(b[22:])
  134. pd.PartitionContents = NewEntityID(b[24:])
  135. pd.PartitionContentsUse = b[56:184]
  136. pd.AccessType = rl_u32(b[184:])
  137. pd.PartitionStartingLocation = rl_u32(b[188:])
  138. pd.PartitionLength = rl_u32(b[192:])
  139. pd.ImplementationIdentifier = NewEntityID(b[196:])
  140. pd.ImplementationUse = b[228:356]
  141. return pd
  142. }
  143. func NewPartitionDescriptor(b []byte) *PartitionDescriptor {
  144. return new(PartitionDescriptor).FromBytes(b)
  145. }
  146. func (d *Descriptor) PartitionDescriptor() *PartitionDescriptor {
  147. return NewPartitionDescriptor(d.data)
  148. }
  149. type PartitionMap struct {
  150. PartitionMapType uint8
  151. PartitionMapLength uint8
  152. VolumeSequenceNumber uint16
  153. PartitionNumber uint16
  154. }
  155. func (pm *PartitionMap) FromBytes(b []byte) *PartitionMap {
  156. pm.PartitionMapType = rb_u8(b[0:])
  157. pm.PartitionMapLength = rb_u8(b[1:])
  158. pm.VolumeSequenceNumber = rb_u16(b[2:])
  159. pm.PartitionNumber = rb_u16(b[4:])
  160. return pm
  161. }
  162. type LogicalVolumeDescriptor struct {
  163. Descriptor Descriptor
  164. VolumeDescriptorSequenceNumber uint32
  165. LogicalVolumeIdentifier string
  166. LogicalBlockSize uint32
  167. DomainIdentifier EntityID
  168. LogicalVolumeContentsUse ExtentLong
  169. MapTableLength uint32
  170. NumberOfPartitionMaps uint32
  171. ImplementationIdentifier EntityID
  172. ImplementationUse []byte
  173. IntegritySequenceExtent Extent
  174. PartitionMaps []PartitionMap
  175. }
  176. func (lvd *LogicalVolumeDescriptor) FromBytes(b []byte) *LogicalVolumeDescriptor {
  177. lvd.Descriptor.FromBytes(b)
  178. lvd.VolumeDescriptorSequenceNumber = rl_u32(b[16:])
  179. lvd.LogicalVolumeIdentifier = r_dstring(b[84:], 128)
  180. lvd.LogicalBlockSize = rl_u32(b[212:])
  181. lvd.DomainIdentifier = NewEntityID(b[216:])
  182. lvd.LogicalVolumeContentsUse = NewExtentLong(b[248:])
  183. lvd.MapTableLength = rl_u32(b[264:])
  184. lvd.NumberOfPartitionMaps = rl_u32(b[268:])
  185. lvd.ImplementationIdentifier = NewEntityID(b[272:])
  186. lvd.ImplementationUse = b[304:432]
  187. lvd.IntegritySequenceExtent = NewExtent(b[432:])
  188. lvd.PartitionMaps = make([]PartitionMap, lvd.NumberOfPartitionMaps)
  189. for i := range lvd.PartitionMaps {
  190. lvd.PartitionMaps[i].FromBytes(b[440+i*6:])
  191. }
  192. return lvd
  193. }
  194. func NewLogicalVolumeDescriptor(b []byte) *LogicalVolumeDescriptor {
  195. return new(LogicalVolumeDescriptor).FromBytes(b)
  196. }
  197. func (d *Descriptor) LogicalVolumeDescriptor() *LogicalVolumeDescriptor {
  198. return NewLogicalVolumeDescriptor(d.data)
  199. }
  200. type FileSetDescriptor struct {
  201. Descriptor Descriptor
  202. RecordingDateTime time.Time
  203. InterchangeLevel uint16
  204. MaximumInterchangeLevel uint16
  205. CharacterSetList uint32
  206. MaximumCharacterSetList uint32
  207. FileSetNumber uint32
  208. FileSetDescriptorNumber uint32
  209. LogicalVolumeIdentifier string
  210. FileSetIdentifier string
  211. CopyrightFileIdentifier string
  212. AbstractFileIdentifier string
  213. RootDirectoryICB ExtentLong
  214. DomainIdentifier EntityID
  215. NexExtent ExtentLong
  216. }
  217. func (fsd *FileSetDescriptor) FromBytes(b []byte) *FileSetDescriptor {
  218. fsd.Descriptor.FromBytes(b)
  219. fsd.RecordingDateTime = r_timestamp(b[16:])
  220. fsd.InterchangeLevel = rl_u16(b[28:])
  221. fsd.MaximumInterchangeLevel = rl_u16(b[30:])
  222. fsd.CharacterSetList = rl_u32(b[32:])
  223. fsd.MaximumCharacterSetList = rl_u32(b[36:])
  224. fsd.FileSetNumber = rl_u32(b[40:])
  225. fsd.FileSetDescriptorNumber = rl_u32(b[44:])
  226. fsd.LogicalVolumeIdentifier = r_dstring(b[112:], 128)
  227. fsd.FileSetIdentifier = r_dstring(b[304:], 32)
  228. fsd.CopyrightFileIdentifier = r_dstring(b[336:], 32)
  229. fsd.AbstractFileIdentifier = r_dstring(b[368:], 32)
  230. fsd.RootDirectoryICB = NewExtentLong(b[400:])
  231. fsd.DomainIdentifier = NewEntityID(b[416:])
  232. fsd.NexExtent = NewExtentLong(b[448:])
  233. return fsd
  234. }
  235. func NewFileSetDescriptor(b []byte) *FileSetDescriptor {
  236. return new(FileSetDescriptor).FromBytes(b)
  237. }
  238. func (d *Descriptor) FileSetDescriptor() *FileSetDescriptor {
  239. return NewFileSetDescriptor(d.data)
  240. }
  241. type FileIdentifierDescriptor struct {
  242. Descriptor Descriptor
  243. FileVersionNumber uint16
  244. FileCharacteristics uint8
  245. LengthOfFileIdentifier uint8
  246. ICB ExtentLong
  247. LengthOfImplementationUse uint16
  248. ImplementationUse EntityID
  249. FileIdentifier string
  250. }
  251. func (fid *FileIdentifierDescriptor) Len() uint64 {
  252. l := 38 + uint64(fid.LengthOfImplementationUse) + uint64(fid.LengthOfFileIdentifier)
  253. return 4 * ((l + 3) / 4) // padding = 4
  254. }
  255. func (fid *FileIdentifierDescriptor) FromBytes(b []byte) *FileIdentifierDescriptor {
  256. fid.Descriptor.FromBytes(b)
  257. fid.FileVersionNumber = rl_u16(b[16:])
  258. fid.FileCharacteristics = r_u8(b[18:])
  259. fid.LengthOfFileIdentifier = r_u8(b[19:])
  260. fid.ICB = NewExtentLong(b[20:])
  261. fid.LengthOfImplementationUse = rl_u16(b[36:])
  262. fid.ImplementationUse = NewEntityID(b[38:])
  263. identStart := 38 + fid.LengthOfImplementationUse
  264. fid.FileIdentifier = r_dcharacters(b[identStart : fid.LengthOfFileIdentifier+uint8(identStart)])
  265. return fid
  266. }
  267. func NewFileIdentifierDescriptor(b []byte) *FileIdentifierDescriptor {
  268. return new(FileIdentifierDescriptor).FromBytes(b)
  269. }
  270. func (d *Descriptor) FileIdentifierDescriptor() *FileIdentifierDescriptor {
  271. return NewFileIdentifierDescriptor(d.data)
  272. }
  273. type FileEntry struct {
  274. Descriptor Descriptor
  275. ICBTag *ICBTag
  276. Uid uint32
  277. Gid uint32
  278. Permissions uint32
  279. FileLinkCount uint16
  280. RecordFormat uint8
  281. RecordDisplayAttributes uint8
  282. RecordLength uint32
  283. InformationLength uint64
  284. LogicalBlocksRecorded uint64
  285. AccessTime time.Time
  286. ModificationTime time.Time
  287. AttributeTime time.Time
  288. Checkpoint uint32
  289. ExtendedAttributeICB ExtentLong
  290. ImplementationIdentifier EntityID
  291. UniqueId uint64
  292. LengthOfExtendedAttributes uint32
  293. LengthOfAllocationDescriptors uint32
  294. ExtendedAttributes []byte
  295. AllocationDescriptors []Extent
  296. }
  297. func (fe *FileEntry) FromBytes(b []byte) *FileEntry {
  298. fe.Descriptor.FromBytes(b)
  299. fe.ICBTag = NewICBTag(b[16:])
  300. fe.Uid = rl_u32(b[36:])
  301. fe.Gid = rl_u32(b[40:])
  302. fe.Permissions = rl_u32(b[44:])
  303. fe.FileLinkCount = rl_u16(b[48:])
  304. fe.RecordFormat = r_u8(b[50:])
  305. fe.RecordDisplayAttributes = r_u8(b[51:])
  306. fe.RecordLength = rl_u32(b[52:])
  307. fe.InformationLength = rl_u64(b[56:])
  308. fe.LogicalBlocksRecorded = rl_u64(b[64:])
  309. fe.AccessTime = r_timestamp(b[72:])
  310. fe.ModificationTime = r_timestamp(b[84:])
  311. fe.AttributeTime = r_timestamp(b[96:])
  312. fe.Checkpoint = rl_u32(b[108:])
  313. fe.ExtendedAttributeICB = NewExtentLong(b[112:])
  314. fe.ImplementationIdentifier = NewEntityID(b[128:])
  315. fe.UniqueId = rl_u64(b[160:])
  316. fe.LengthOfExtendedAttributes = rl_u32(b[168:])
  317. fe.LengthOfAllocationDescriptors = rl_u32(b[172:])
  318. allocDescStart := 176 + fe.LengthOfExtendedAttributes
  319. fe.ExtendedAttributes = b[176:allocDescStart]
  320. fe.AllocationDescriptors = make([]Extent, fe.LengthOfAllocationDescriptors/8)
  321. for i := range fe.AllocationDescriptors {
  322. fe.AllocationDescriptors[i] = NewExtent(b[allocDescStart+uint32(i)*8:])
  323. }
  324. return fe
  325. }
  326. func NewFileEntry(b []byte) *FileEntry {
  327. return new(FileEntry).FromBytes(b)
  328. }
  329. func (d *Descriptor) FileEntry() *FileEntry {
  330. return NewFileEntry(d.data)
  331. }