]> git.r.bdr.sh - rbdr/mobius/blame - hotline/flattened_file_object.go
Handle zero length comment and file paths for Nostalgia compatibility
[rbdr/mobius] / hotline / flattened_file_object.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "encoding/binary"
6988a057
JH
5 "os"
6)
7
8type 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.
17type FlatFileHeader struct {
72dd37f1
JH
18 Format [4]byte // Always "FILP"
19 Version [2]byte // Always 1
20 RSVD [16]byte // Always empty zeros
85767504 21 ForkCount [2]byte // Number of forks
6988a057
JH
22}
23
24// NewFlatFileHeader returns a FlatFileHeader struct
25func NewFlatFileHeader() FlatFileHeader {
26 return FlatFileHeader{
72dd37f1
JH
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},
6988a057
JH
31 }
32}
33
34// FlatFileInformationForkHeader is the second section of a "Flattened File Object"
35type FlatFileInformationForkHeader struct {
85767504
JH
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
6988a057
JH
40}
41
42type 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
29f329ae 58func NewFlatFileInformationFork(fileName string, modifyTime []byte) FlatFileInformationFork {
6988a057 59 return FlatFileInformationFork{
29f329ae
JH
60 Platform: []byte("AMAC"), // TODO: Remove hardcode to support "AWIN" Platform (maybe?)
61 TypeSignature: []byte(fileTypeFromFilename(fileName).TypeCode), // TODO: Don't infer types from filename
62 CreatorSignature: []byte(fileTypeFromFilename(fileName).CreatorCode), // 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?
6988a057 69 Name: []byte(fileName),
5218c782
JH
70 CommentSize: []byte{0, 0},
71 Comment: []byte{}, // TODO: implement (maybe?)
6988a057
JH
72 }
73}
74
bb7fe19f
JH
75// DataSize calculates the size of the flat file information fork, which is
76// 72 bytes for the fixed length fields plus the length of the Name + Comment
85767504 77func (ffif *FlatFileInformationFork) DataSize() []byte {
6988a057 78 size := make([]byte, 4)
bb7fe19f 79
aebc4d36 80 // TODO: Can I do math directly on two byte slices?
bb7fe19f 81 dataSize := len(ffif.Name) + len(ffif.Comment) + 74
6988a057
JH
82
83 binary.BigEndian.PutUint32(size, uint32(dataSize))
84
85 return size
86}
87
85767504 88func (ffo *flattenedFileObject) TransferSize() []byte {
c5d9af5a 89 payloadSize := len(ffo.BinaryMarshal())
85767504 90 dataSize := binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize[:])
6988a057
JH
91
92 transferSize := make([]byte, 4)
93 binary.BigEndian.PutUint32(transferSize, dataSize+uint32(payloadSize))
94
95 return transferSize
96}
97
85767504 98func (ffif *FlatFileInformationFork) ReadNameSize() []byte {
6988a057
JH
99 size := make([]byte, 2)
100 binary.BigEndian.PutUint16(size, uint16(len(ffif.Name)))
101
102 return size
103}
104
105type FlatFileDataForkHeader struct {
85767504
JH
106 ForkType [4]byte
107 CompressionType [4]byte
108 RSVD [4]byte
109 DataSize [4]byte
6988a057
JH
110}
111
85767504 112func (ffif *FlatFileInformationFork) UnmarshalBinary(b []byte) error {
85767504 113 nameSize := b[70:72]
6988a057 114 bs := binary.BigEndian.Uint16(nameSize)
85767504 115 nameEnd := 72 + bs
6988a057 116
85767504
JH
117 ffif.Platform = b[0:4]
118 ffif.TypeSignature = b[4:8]
119 ffif.CreatorSignature = b[8:12]
120 ffif.Flags = b[12:16]
121 ffif.PlatformFlags = b[16:20]
122 ffif.RSVD = b[20:52]
123 ffif.CreateDate = b[52:60]
124 ffif.ModifyDate = b[60:68]
125 ffif.NameScript = b[68:70]
126 ffif.NameSize = b[70:72]
127 ffif.Name = b[72:nameEnd]
050407a3
JH
128
129 if len(b) > int(nameEnd) {
130 ffif.CommentSize = b[nameEnd : nameEnd+2]
131 commentLen := binary.BigEndian.Uint16(ffif.CommentSize)
132
133 commentStartPos := int(nameEnd) + 2
134 commentEndPos := int(nameEnd) + 2 + int(commentLen)
135
136 ffif.Comment = b[commentStartPos:commentEndPos]
137 }
85767504
JH
138
139 return nil
6988a057
JH
140}
141
85767504 142func (f *flattenedFileObject) BinaryMarshal() []byte {
6988a057 143 var out []byte
72dd37f1
JH
144 out = append(out, f.FlatFileHeader.Format[:]...)
145 out = append(out, f.FlatFileHeader.Version[:]...)
146 out = append(out, f.FlatFileHeader.RSVD[:]...)
147 out = append(out, f.FlatFileHeader.ForkCount[:]...)
6988a057
JH
148
149 out = append(out, []byte("INFO")...)
150 out = append(out, []byte{0, 0, 0, 0}...)
151 out = append(out, make([]byte, 4)...)
152 out = append(out, f.FlatFileInformationFork.DataSize()...)
153
154 out = append(out, f.FlatFileInformationFork.Platform...)
155 out = append(out, f.FlatFileInformationFork.TypeSignature...)
156 out = append(out, f.FlatFileInformationFork.CreatorSignature...)
157 out = append(out, f.FlatFileInformationFork.Flags...)
158 out = append(out, f.FlatFileInformationFork.PlatformFlags...)
159 out = append(out, f.FlatFileInformationFork.RSVD...)
160 out = append(out, f.FlatFileInformationFork.CreateDate...)
161 out = append(out, f.FlatFileInformationFork.ModifyDate...)
162 out = append(out, f.FlatFileInformationFork.NameScript...)
163 out = append(out, f.FlatFileInformationFork.ReadNameSize()...)
164 out = append(out, f.FlatFileInformationFork.Name...)
bb7fe19f
JH
165 out = append(out, f.FlatFileInformationFork.CommentSize...)
166 out = append(out, f.FlatFileInformationFork.Comment...)
6988a057 167
85767504
JH
168 out = append(out, f.FlatFileDataForkHeader.ForkType[:]...)
169 out = append(out, f.FlatFileDataForkHeader.CompressionType[:]...)
170 out = append(out, f.FlatFileDataForkHeader.RSVD[:]...)
171 out = append(out, f.FlatFileDataForkHeader.DataSize[:]...)
6988a057
JH
172
173 return out
174}
175
16a4ad70 176func NewFlattenedFileObject(fileRoot string, filePath, fileName []byte, dataOffset int64) (*flattenedFileObject, error) {
92a7e455
JH
177 fullFilePath, err := readPath(fileRoot, filePath, fileName)
178 if err != nil {
179 return nil, err
180 }
16a4ad70 181 file, err := effectiveFile(fullFilePath)
6988a057 182 if err != nil {
72dd37f1 183 return nil, err
6988a057 184 }
16a4ad70 185
29f329ae 186 defer func(file *os.File) { _ = file.Close() }(file)
6988a057
JH
187
188 fileInfo, err := file.Stat()
189 if err != nil {
72dd37f1 190 return nil, err
6988a057
JH
191 }
192
193 dataSize := make([]byte, 4)
16a4ad70 194 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-dataOffset))
6988a057 195
29f329ae
JH
196 mTime := toHotlineTime(fileInfo.ModTime())
197
72dd37f1 198 return &flattenedFileObject{
6988a057 199 FlatFileHeader: NewFlatFileHeader(),
29f329ae 200 FlatFileInformationFork: NewFlatFileInformationFork(string(fileName), mTime),
6988a057 201 FlatFileDataForkHeader: FlatFileDataForkHeader{
85767504
JH
202 ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
203 CompressionType: [4]byte{},
204 RSVD: [4]byte{},
205 DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]},
6988a057
JH
206 },
207 }, nil
208}