10 type flattenedFileObject struct {
11 FlatFileHeader FlatFileHeader
12 FlatFileInformationForkHeader FlatFileForkHeader
13 FlatFileInformationFork FlatFileInformationFork
14 FlatFileDataForkHeader FlatFileForkHeader
15 FlatFileResForkHeader FlatFileForkHeader
17 readOffset int // Internal offset to track read progress
20 // FlatFileHeader is the first section of a "Flattened File Object". All fields have static values.
21 type FlatFileHeader struct {
22 Format [4]byte // Always "FILP"
23 Version [2]byte // Always 1
24 RSVD [16]byte // Always empty zeros
25 ForkCount [2]byte // Number of forks, either 2 or 3 if there is a resource fork
28 type FlatFileInformationFork struct {
29 Platform [4]byte // Operating System used. ("AMAC" or "MWIN")
30 TypeSignature [4]byte // File type signature
31 CreatorSignature [4]byte // File creator signature
38 NameSize [2]byte // Length of file name (Maximum 128 characters)
39 Name []byte // File name
40 CommentSize [2]byte // Length of the comment
41 Comment []byte // File comment
43 readOffset int // Internal offset to track read progress
46 func NewFlatFileInformationFork(fileName string, modifyTime [8]byte, typeSignature string, creatorSignature string) FlatFileInformationFork {
47 return FlatFileInformationFork{
48 Platform: [4]byte{0x41, 0x4D, 0x41, 0x43}, // "AMAC" TODO: Remove hardcode to support "AWIN" Platform (maybe?)
49 TypeSignature: [4]byte([]byte(typeSignature)), // TODO: Don't infer types from filename
50 CreatorSignature: [4]byte([]byte(creatorSignature)), // TODO: Don't infer types from filename
51 PlatformFlags: [4]byte{0, 0, 1, 0}, // TODO: What is this?
52 CreateDate: modifyTime, // some filesystems don't support createTime
53 ModifyDate: modifyTime,
54 Name: []byte(fileName),
55 Comment: []byte{}, // TODO: implement (maybe?)
59 func (ffif *FlatFileInformationFork) friendlyType() []byte {
60 if name, ok := friendlyCreatorNames[string(ffif.TypeSignature[:])]; ok {
63 return ffif.TypeSignature[:]
66 func (ffif *FlatFileInformationFork) friendlyCreator() []byte {
67 if name, ok := friendlyCreatorNames[string(ffif.CreatorSignature[:])]; ok {
70 return ffif.CreatorSignature[:]
73 func (ffif *FlatFileInformationFork) setComment(comment []byte) error {
74 commentSize := make([]byte, 2)
75 ffif.Comment = comment
76 binary.BigEndian.PutUint16(commentSize, uint16(len(comment)))
77 ffif.CommentSize = [2]byte(commentSize)
78 // TODO: return err if comment is too long
82 // DataSize calculates the size of the flat file information fork, which is
83 // 72 bytes for the fixed length fields plus the length of the Name + Comment
84 func (ffif *FlatFileInformationFork) DataSize() []byte {
85 size := make([]byte, 4)
87 dataSize := len(ffif.Name) + len(ffif.Comment) + 74 // 74 = len of fixed size headers
89 binary.BigEndian.PutUint32(size, uint32(dataSize))
94 func (ffif *FlatFileInformationFork) Size() [4]byte {
97 dataSize := len(ffif.Name) + len(ffif.Comment) + 74 // 74 = len of fixed size headers
99 binary.BigEndian.PutUint32(size[:], uint32(dataSize))
104 func (ffo *flattenedFileObject) TransferSize(offset int64) []byte {
107 // get length of the flattenedFileObject, including the info fork
108 b, _ := io.ReadAll(&ffoCopy)
109 payloadSize := len(b)
111 // length of data fork
112 dataSize := binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize[:])
114 // length of resource fork
115 resForkSize := binary.BigEndian.Uint32(ffo.FlatFileResForkHeader.DataSize[:])
117 size := make([]byte, 4)
118 binary.BigEndian.PutUint32(size, dataSize+resForkSize+uint32(payloadSize)-uint32(offset))
123 func (ffif *FlatFileInformationFork) ReadNameSize() []byte {
124 size := make([]byte, 2)
125 binary.BigEndian.PutUint16(size, uint16(len(ffif.Name)))
130 type FlatFileForkHeader struct {
131 ForkType [4]byte // Either INFO, DATA or MACR
132 CompressionType [4]byte
137 func (ffif *FlatFileInformationFork) Read(p []byte) (int, error) {
138 buf := slices.Concat(
140 ffif.TypeSignature[:],
141 ffif.CreatorSignature[:],
143 ffif.PlatformFlags[:],
154 if ffif.readOffset >= len(buf) {
155 return 0, io.EOF // All bytes have been read
158 n := copy(p, buf[ffif.readOffset:])
164 // Write implements the io.Writer interface for FlatFileInformationFork
165 func (ffif *FlatFileInformationFork) Write(p []byte) (int, error) {
167 bs := binary.BigEndian.Uint16(nameSize)
170 ffif.Platform = [4]byte(p[0:4])
171 ffif.TypeSignature = [4]byte(p[4:8])
172 ffif.CreatorSignature = [4]byte(p[8:12])
173 ffif.Flags = [4]byte(p[12:16])
174 ffif.PlatformFlags = [4]byte(p[16:20])
175 ffif.RSVD = [32]byte(p[20:52])
176 ffif.CreateDate = [8]byte(p[52:60])
177 ffif.ModifyDate = [8]byte(p[60:68])
178 ffif.NameScript = [2]byte(p[68:70])
179 ffif.NameSize = [2]byte(p[70:72])
180 ffif.Name = p[72:total]
182 if len(p) > int(total) {
183 ffif.CommentSize = [2]byte(p[total : total+2])
184 commentLen := binary.BigEndian.Uint16(ffif.CommentSize[:])
185 commentStartPos := int(total) + 2
186 commentEndPos := int(total) + 2 + int(commentLen)
188 ffif.Comment = p[commentStartPos:commentEndPos]
190 //total = uint16(commentEndPos)
196 func (ffif *FlatFileInformationFork) UnmarshalBinary(b []byte) error {
198 bs := binary.BigEndian.Uint16(nameSize)
201 ffif.Platform = [4]byte(b[0:4])
202 ffif.TypeSignature = [4]byte(b[4:8])
203 ffif.CreatorSignature = [4]byte(b[8:12])
204 ffif.Flags = [4]byte(b[12:16])
205 ffif.PlatformFlags = [4]byte(b[16:20])
206 ffif.RSVD = [32]byte(b[20:52])
207 ffif.CreateDate = [8]byte(b[52:60])
208 ffif.ModifyDate = [8]byte(b[60:68])
209 ffif.NameScript = [2]byte(b[68:70])
210 ffif.NameSize = [2]byte(b[70:72])
211 ffif.Name = b[72:nameEnd]
213 if len(b) > int(nameEnd) {
214 ffif.CommentSize = [2]byte(b[nameEnd : nameEnd+2])
215 commentLen := binary.BigEndian.Uint16(ffif.CommentSize[:])
217 commentStartPos := int(nameEnd) + 2
218 commentEndPos := int(nameEnd) + 2 + int(commentLen)
220 ffif.Comment = b[commentStartPos:commentEndPos]
226 // Read implements the io.Reader interface for flattenedFileObject
227 func (ffo *flattenedFileObject) Read(p []byte) (int, error) {
228 buf := slices.Concat(
229 ffo.FlatFileHeader.Format[:],
230 ffo.FlatFileHeader.Version[:],
231 ffo.FlatFileHeader.RSVD[:],
232 ffo.FlatFileHeader.ForkCount[:],
236 ffo.FlatFileInformationFork.DataSize(),
237 ffo.FlatFileInformationFork.Platform[:],
238 ffo.FlatFileInformationFork.TypeSignature[:],
239 ffo.FlatFileInformationFork.CreatorSignature[:],
240 ffo.FlatFileInformationFork.Flags[:],
241 ffo.FlatFileInformationFork.PlatformFlags[:],
242 ffo.FlatFileInformationFork.RSVD[:],
243 ffo.FlatFileInformationFork.CreateDate[:],
244 ffo.FlatFileInformationFork.ModifyDate[:],
245 ffo.FlatFileInformationFork.NameScript[:],
246 ffo.FlatFileInformationFork.ReadNameSize(),
247 ffo.FlatFileInformationFork.Name,
248 ffo.FlatFileInformationFork.CommentSize[:],
249 ffo.FlatFileInformationFork.Comment,
250 ffo.FlatFileDataForkHeader.ForkType[:],
251 ffo.FlatFileDataForkHeader.CompressionType[:],
252 ffo.FlatFileDataForkHeader.RSVD[:],
253 ffo.FlatFileDataForkHeader.DataSize[:],
256 if ffo.readOffset >= len(buf) {
257 return 0, io.EOF // All bytes have been read
260 n := copy(p, buf[ffo.readOffset:])
266 func (ffo *flattenedFileObject) ReadFrom(r io.Reader) (int64, error) {
269 if err := binary.Read(r, binary.BigEndian, &ffo.FlatFileHeader); err != nil {
273 if err := binary.Read(r, binary.BigEndian, &ffo.FlatFileInformationForkHeader); err != nil {
277 dataLen := binary.BigEndian.Uint32(ffo.FlatFileInformationForkHeader.DataSize[:])
278 ffifBuf := make([]byte, dataLen)
279 if _, err := io.ReadFull(r, ffifBuf); err != nil {
283 _, err := io.Copy(&ffo.FlatFileInformationFork, bytes.NewReader(ffifBuf))
288 if err := binary.Read(r, binary.BigEndian, &ffo.FlatFileDataForkHeader); err != nil {
295 func (ffo *flattenedFileObject) dataSize() int64 {
296 return int64(binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize[:]))
299 func (ffo *flattenedFileObject) rsrcSize() int64 {
300 return int64(binary.BigEndian.Uint32(ffo.FlatFileResForkHeader.DataSize[:]))