]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_name_with_info.go
Refactoring, cleanup, test backfilling
[rbdr/mobius] / hotline / file_name_with_info.go
CommitLineData
43ecc0f4
JH
1package hotline
2
3import (
72dd37f1 4 "bytes"
43ecc0f4 5 "encoding/binary"
b129b7cb
JH
6 "io"
7 "slices"
43ecc0f4
JH
8)
9
43ecc0f4 10type FileNameWithInfo struct {
72dd37f1 11 fileNameWithInfoHeader
95159e55 12 Name []byte // File Name
1a37f014
JH
13
14 readOffset int // Internal offset to track read progress
43ecc0f4
JH
15}
16
72dd37f1
JH
17// fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo
18type fileNameWithInfoHeader struct {
7cd900d6 19 Type [4]byte // File type code
72dd37f1
JH
20 Creator [4]byte // File creator code
21 FileSize [4]byte // File Size in bytes
22 RSVD [4]byte
23 NameScript [2]byte // ??
95159e55 24 NameSize [2]byte // Length of Name field
43ecc0f4
JH
25}
26
72dd37f1
JH
27func (f *fileNameWithInfoHeader) nameLen() int {
28 return int(binary.BigEndian.Uint16(f.NameSize[:]))
43ecc0f4 29}
72dd37f1 30
b129b7cb 31// Read implements io.Reader for FileNameWithInfo
1a37f014
JH
32func (f *FileNameWithInfo) Read(p []byte) (int, error) {
33 buf := slices.Concat(
34 f.Type[:],
35 f.Creator[:],
36 f.FileSize[:],
37 f.RSVD[:],
38 f.NameScript[:],
39 f.NameSize[:],
40 f.Name,
41 )
42
43 if f.readOffset >= len(buf) {
44 return 0, io.EOF // All bytes have been read
45 }
46
47 n := copy(p, buf[f.readOffset:])
48 f.readOffset += n
49
50 return n, nil
72dd37f1
JH
51}
52
b129b7cb
JH
53func (f *FileNameWithInfo) Write(p []byte) (int, error) {
54 err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.fileNameWithInfoHeader)
72dd37f1 55 if err != nil {
b129b7cb 56 return 0, err
72dd37f1
JH
57 }
58 headerLen := binary.Size(f.fileNameWithInfoHeader)
95159e55 59 f.Name = p[headerLen : headerLen+f.nameLen()]
72dd37f1 60
b129b7cb 61 return len(p), nil
72dd37f1 62}