]> git.r.bdr.sh - rbdr/mobius/blob - hotline/flattened_file_object.go
Implement some special case file descriptions
[rbdr/mobius] / hotline / flattened_file_object.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "os"
6 )
7
8 type flattenedFileObject struct {
9 FlatFileHeader FlatFileHeader
10 FlatFileInformationForkHeader FlatFileInformationForkHeader
11 FlatFileInformationFork FlatFileInformationFork
12 FlatFileDataForkHeader FlatFileDataForkHeader
13 FileData []byte
14 }
15
16 // FlatFileHeader is the first section of a "Flattened File Object". All fields have static values.
17 type FlatFileHeader struct {
18 Format [4]byte // Always "FILP"
19 Version [2]byte // Always 1
20 RSVD [16]byte // Always empty zeros
21 ForkCount [2]byte // Number of forks
22 }
23
24 // NewFlatFileHeader returns a FlatFileHeader struct
25 func NewFlatFileHeader() FlatFileHeader {
26 return FlatFileHeader{
27 Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // FILP
28 Version: [2]byte{0, 1},
29 RSVD: [16]byte{},
30 ForkCount: [2]byte{0, 2},
31 }
32 }
33
34 // FlatFileInformationForkHeader is the second section of a "Flattened File Object"
35 type FlatFileInformationForkHeader struct {
36 ForkType [4]byte // Always "INFO"
37 CompressionType [4]byte // Always 0; Compression was never implemented in the Hotline protocol
38 RSVD [4]byte // Always zeros
39 DataSize [4]byte // Size of the flat file information fork
40 }
41
42 type FlatFileInformationFork struct {
43 Platform []byte // Operating System used. ("AMAC" or "MWIN")
44 TypeSignature []byte // File type signature
45 CreatorSignature []byte // File creator signature
46 Flags []byte
47 PlatformFlags []byte
48 RSVD []byte
49 CreateDate []byte
50 ModifyDate []byte
51 NameScript []byte // TODO: what is this?
52 NameSize []byte // Length of file name (Maximum 128 characters)
53 Name []byte // File name
54 CommentSize []byte // Length of file comment
55 Comment []byte // File comment
56 }
57
58 func NewFlatFileInformationFork(fileName string, modifyTime []byte, typeSignature string, creatorSignature string) FlatFileInformationFork {
59 return FlatFileInformationFork{
60 Platform: []byte("AMAC"), // TODO: Remove hardcode to support "AWIN" Platform (maybe?)
61 TypeSignature: []byte(typeSignature), // TODO: Don't infer types from filename
62 CreatorSignature: []byte(creatorSignature), // TODO: Don't infer types from filename
63 Flags: []byte{0, 0, 0, 0}, // TODO: What is this?
64 PlatformFlags: []byte{0, 0, 1, 0}, // TODO: What is this?
65 RSVD: make([]byte, 32), // Unimplemented in Hotline Protocol
66 CreateDate: modifyTime, // some filesystems don't support createTime
67 ModifyDate: modifyTime,
68 NameScript: make([]byte, 2), // TODO: What is this?
69 Name: []byte(fileName),
70 CommentSize: []byte{0, 0},
71 Comment: []byte{}, // TODO: implement (maybe?)
72 }
73 }
74
75 func (ffif *FlatFileInformationFork) friendlyType() []byte {
76
77 if name, ok := friendlyCreatorNames[string(ffif.TypeSignature)]; ok {
78 return []byte(name)
79 }
80 return ffif.CreatorSignature
81 }
82
83 // DataSize calculates the size of the flat file information fork, which is
84 // 72 bytes for the fixed length fields plus the length of the Name + Comment
85 func (ffif *FlatFileInformationFork) DataSize() []byte {
86 size := make([]byte, 4)
87
88 // TODO: Can I do math directly on two byte slices?
89 dataSize := len(ffif.Name) + len(ffif.Comment) + 74
90
91 binary.BigEndian.PutUint32(size, uint32(dataSize))
92
93 return size
94 }
95
96 func (ffo *flattenedFileObject) TransferSize() []byte {
97 payloadSize := len(ffo.BinaryMarshal())
98 dataSize := binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize[:])
99
100 transferSize := make([]byte, 4)
101 binary.BigEndian.PutUint32(transferSize, dataSize+uint32(payloadSize))
102
103 return transferSize
104 }
105
106 func (ffif *FlatFileInformationFork) ReadNameSize() []byte {
107 size := make([]byte, 2)
108 binary.BigEndian.PutUint16(size, uint16(len(ffif.Name)))
109
110 return size
111 }
112
113 type FlatFileDataForkHeader struct {
114 ForkType [4]byte
115 CompressionType [4]byte
116 RSVD [4]byte
117 DataSize [4]byte
118 }
119
120 func (ffif *FlatFileInformationFork) UnmarshalBinary(b []byte) error {
121 nameSize := b[70:72]
122 bs := binary.BigEndian.Uint16(nameSize)
123 nameEnd := 72 + bs
124
125 ffif.Platform = b[0:4]
126 ffif.TypeSignature = b[4:8]
127 ffif.CreatorSignature = b[8:12]
128 ffif.Flags = b[12:16]
129 ffif.PlatformFlags = b[16:20]
130 ffif.RSVD = b[20:52]
131 ffif.CreateDate = b[52:60]
132 ffif.ModifyDate = b[60:68]
133 ffif.NameScript = b[68:70]
134 ffif.NameSize = b[70:72]
135 ffif.Name = b[72:nameEnd]
136
137 if len(b) > int(nameEnd) {
138 ffif.CommentSize = b[nameEnd : nameEnd+2]
139 commentLen := binary.BigEndian.Uint16(ffif.CommentSize)
140
141 commentStartPos := int(nameEnd) + 2
142 commentEndPos := int(nameEnd) + 2 + int(commentLen)
143
144 ffif.Comment = b[commentStartPos:commentEndPos]
145 }
146
147 return nil
148 }
149
150 func (f *flattenedFileObject) BinaryMarshal() []byte {
151 var out []byte
152 out = append(out, f.FlatFileHeader.Format[:]...)
153 out = append(out, f.FlatFileHeader.Version[:]...)
154 out = append(out, f.FlatFileHeader.RSVD[:]...)
155 out = append(out, f.FlatFileHeader.ForkCount[:]...)
156
157 out = append(out, []byte("INFO")...)
158 out = append(out, []byte{0, 0, 0, 0}...)
159 out = append(out, make([]byte, 4)...)
160 out = append(out, f.FlatFileInformationFork.DataSize()...)
161
162 out = append(out, f.FlatFileInformationFork.Platform...)
163 out = append(out, f.FlatFileInformationFork.TypeSignature...)
164 out = append(out, f.FlatFileInformationFork.CreatorSignature...)
165 out = append(out, f.FlatFileInformationFork.Flags...)
166 out = append(out, f.FlatFileInformationFork.PlatformFlags...)
167 out = append(out, f.FlatFileInformationFork.RSVD...)
168 out = append(out, f.FlatFileInformationFork.CreateDate...)
169 out = append(out, f.FlatFileInformationFork.ModifyDate...)
170 out = append(out, f.FlatFileInformationFork.NameScript...)
171 out = append(out, f.FlatFileInformationFork.ReadNameSize()...)
172 out = append(out, f.FlatFileInformationFork.Name...)
173 out = append(out, f.FlatFileInformationFork.CommentSize...)
174 out = append(out, f.FlatFileInformationFork.Comment...)
175
176 out = append(out, f.FlatFileDataForkHeader.ForkType[:]...)
177 out = append(out, f.FlatFileDataForkHeader.CompressionType[:]...)
178 out = append(out, f.FlatFileDataForkHeader.RSVD[:]...)
179 out = append(out, f.FlatFileDataForkHeader.DataSize[:]...)
180
181 return out
182 }
183
184 func NewFlattenedFileObject(fileRoot string, filePath, fileName []byte, dataOffset int64) (*flattenedFileObject, error) {
185 fullFilePath, err := readPath(fileRoot, filePath, fileName)
186 if err != nil {
187 return nil, err
188 }
189 file, err := effectiveFile(fullFilePath)
190 if err != nil {
191 return nil, err
192 }
193
194 defer func(file *os.File) { _ = file.Close() }(file)
195
196 fileInfo, err := file.Stat()
197 if err != nil {
198 return nil, err
199 }
200
201 dataSize := make([]byte, 4)
202 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-dataOffset))
203
204 mTime := toHotlineTime(fileInfo.ModTime())
205
206 ft, _ := fileTypeFromInfo(fileInfo)
207
208 return &flattenedFileObject{
209 FlatFileHeader: NewFlatFileHeader(),
210 FlatFileInformationFork: NewFlatFileInformationFork(string(fileName), mTime, ft.TypeCode, ft.CreatorCode),
211 FlatFileDataForkHeader: FlatFileDataForkHeader{
212 ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
213 CompressionType: [4]byte{},
214 RSVD: [4]byte{},
215 DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]},
216 },
217 }, nil
218 }