susp.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package iso9660
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. /* The following types of core SUSP records are being handled in some way:
  7. * - [x] CE (SUSP 5.1: continuation)
  8. * - [ ] PD (SUSP 5.2: padding)
  9. * - [x] SP (SUSP 5.3: offset)
  10. * - [ ] ST (SUSP 5.4)
  11. * - [x] ER (SUSP 5.5: extension record)
  12. * - [ ] ES (SUSP 5.6)
  13. */
  14. // SUSP-112 4.1
  15. type SystemUseEntry []byte
  16. func (e SystemUseEntry) Length() int {
  17. return int(e[2])
  18. }
  19. func (e SystemUseEntry) Data() []byte {
  20. return e[4:]
  21. }
  22. func (e SystemUseEntry) Type() string {
  23. return string(e[:2])
  24. }
  25. type ExtensionRecord struct {
  26. Version int
  27. Identifier string
  28. Descriptor string
  29. Source string
  30. }
  31. // See SUSP-112 5.5
  32. func ExtensionRecordDecode(e SystemUseEntry) (*ExtensionRecord, error) {
  33. if e.Type() != "ER" {
  34. return nil, fmt.Errorf("wrong type of record, expected ER")
  35. }
  36. if e.Length() < 8 {
  37. return nil, io.ErrUnexpectedEOF
  38. }
  39. identifierLen := int(e[4])
  40. if e.Length() < 8+identifierLen {
  41. return nil, io.ErrUnexpectedEOF
  42. }
  43. descriptorLen := int(e[5])
  44. if e.Length() < 8+identifierLen+descriptorLen {
  45. return nil, io.ErrUnexpectedEOF
  46. }
  47. sourceLen := int(e[6])
  48. if e.Length() < 8+identifierLen+descriptorLen+sourceLen {
  49. return nil, io.ErrUnexpectedEOF
  50. }
  51. return &ExtensionRecord{
  52. Version: int(e[7]),
  53. Identifier: string(e[8 : 8+identifierLen]),
  54. Descriptor: string(e[8+identifierLen : 8+identifierLen+descriptorLen]),
  55. Source: string(e[8+identifierLen+descriptorLen : 8+identifierLen+descriptorLen+sourceLen]),
  56. }, nil
  57. }
  58. // See SUSP-112 5.3
  59. func SPRecordDecode(e SystemUseEntry) (*SPRecord, error) {
  60. if e.Type() != "SP" {
  61. return nil, fmt.Errorf("wrong type of record, expected SP")
  62. }
  63. if e.Length() < 7 {
  64. return nil, io.ErrUnexpectedEOF
  65. }
  66. if beByte := e[4]; beByte != 0xBE {
  67. return nil, fmt.Errorf("invalid control byte, %x != 0xBE", beByte)
  68. }
  69. if efByte := e[5]; efByte != 0xEF {
  70. return nil, fmt.Errorf("invalid control byte, %x != 0xEF", efByte)
  71. }
  72. return &SPRecord{
  73. BytesSkipped: e[6],
  74. }, nil
  75. }
  76. type SPRecord struct {
  77. BytesSkipped uint8
  78. }
  79. type SystemUseEntrySlice []SystemUseEntry
  80. func (s SystemUseEntrySlice) GetExtensionRecords() ([]*ExtensionRecord, error) {
  81. results := make([]*ExtensionRecord, 0)
  82. for _, entry := range s {
  83. if entry.Type() == "ER" {
  84. er, err := ExtensionRecordDecode(entry)
  85. if err != nil {
  86. return nil, err
  87. }
  88. results = append(results, er)
  89. }
  90. }
  91. return results, nil
  92. }
  93. // SUSP-112 5.1
  94. type ContinuationEntry struct {
  95. blockLocation uint32
  96. offset uint32
  97. lengthOfArea uint32
  98. }
  99. func umarshalContinuationEntry(e SystemUseEntry) (*ContinuationEntry, error) {
  100. if e.Length() != 28 {
  101. return nil, fmt.Errorf("invalid ContinuationArea record with length %d instead of 28", e.Length())
  102. }
  103. location, err := UnmarshalUint32LSBMSB(e.Data()[0:8])
  104. if err != nil {
  105. return nil, fmt.Errorf("block location: %w", err)
  106. }
  107. offset, err := UnmarshalUint32LSBMSB(e.Data()[8:16])
  108. if err != nil {
  109. return nil, fmt.Errorf("offset: %w", err)
  110. }
  111. length, err := UnmarshalUint32LSBMSB(e.Data()[16:24])
  112. if err != nil {
  113. return nil, fmt.Errorf("length: %w", err)
  114. }
  115. return &ContinuationEntry{
  116. blockLocation: location,
  117. offset: offset,
  118. lengthOfArea: length,
  119. }, nil
  120. }
  121. const (
  122. SUEType_ContinuationArea = "CE"
  123. SUEType_PaddingField = "PD"
  124. SUEType_SharingProtocolIndicator = "SP"
  125. SUEType_SharingProtocolTerminator = "ST"
  126. SUEType_ExtensionsReference = "ER"
  127. SUEType_ExtensionSelector = "ES"
  128. )
  129. func splitSystemUseEntries(data []byte, ra io.ReaderAt) ([]SystemUseEntry, error) {
  130. output := make([]SystemUseEntry, 0)
  131. for len(data) > 0 {
  132. if len(data) < 4 {
  133. // SUSP-112 4
  134. // If the remaining allocated space /.../ is less than four bytes long /.../ shall be ignored.
  135. break
  136. }
  137. entryLen := int(data[2])
  138. if len(data) < entryLen {
  139. return nil, fmt.Errorf("splitting System Use entries: %w, expected %d bytes but have only %d", io.ErrUnexpectedEOF, entryLen, len(data))
  140. }
  141. entry := SystemUseEntry(data[:entryLen])
  142. if entry.Type() == SUEType_ContinuationArea {
  143. ce, err := umarshalContinuationEntry(entry)
  144. if err != nil {
  145. return output, fmt.Errorf("unmarshaling ContinuationEntry: %w", err)
  146. }
  147. continuation := make([]byte, ce.lengthOfArea)
  148. finalOffset := (ce.blockLocation * sectorSize) + ce.offset
  149. if _, err := ra.ReadAt(continuation, int64(finalOffset)); err != nil {
  150. return output, fmt.Errorf("reading Continuation Area: %w", err)
  151. }
  152. continuedEntries, err := splitSystemUseEntries(continuation, ra)
  153. if err != nil {
  154. return output, fmt.Errorf("splitting Continuation Area: %w", err)
  155. }
  156. output = append(output, continuedEntries...)
  157. } else {
  158. output = append(output, entry)
  159. }
  160. data = data[entryLen:]
  161. }
  162. return output, nil
  163. }
  164. type SUSPMetadata struct {
  165. Offset uint8
  166. HasRockRidge bool
  167. }
  168. func (sm *SUSPMetadata) Clone() *SUSPMetadata {
  169. if sm == nil {
  170. return nil
  171. }
  172. return &SUSPMetadata{
  173. Offset: sm.Offset,
  174. HasRockRidge: sm.HasRockRidge,
  175. }
  176. }