]> git.r.bdr.sh - rbdr/mobius/blob - hotline/file_name_with_info.go
Fix broken io.Reader implementations
[rbdr/mobius] / hotline / file_name_with_info.go
1 package hotline
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "io"
7 "slices"
8 )
9
10 type FileNameWithInfo struct {
11 fileNameWithInfoHeader
12 Name []byte // File Name
13 }
14
15 // fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo
16 type fileNameWithInfoHeader struct {
17 Type [4]byte // File type code
18 Creator [4]byte // File creator code
19 FileSize [4]byte // File Size in bytes
20 RSVD [4]byte
21 NameScript [2]byte // ??
22 NameSize [2]byte // Length of Name field
23 }
24
25 func (f *fileNameWithInfoHeader) nameLen() int {
26 return int(binary.BigEndian.Uint16(f.NameSize[:]))
27 }
28
29 // Read implements io.Reader for FileNameWithInfo
30 func (f *FileNameWithInfo) Read(b []byte) (int, error) {
31 return copy(b,
32 slices.Concat(
33 f.Type[:],
34 f.Creator[:],
35 f.FileSize[:],
36 f.RSVD[:],
37 f.NameScript[:],
38 f.NameSize[:],
39 f.Name,
40 ),
41 ), io.EOF
42 }
43
44 func (f *FileNameWithInfo) Write(p []byte) (int, error) {
45 err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.fileNameWithInfoHeader)
46 if err != nil {
47 return 0, err
48 }
49 headerLen := binary.Size(f.fileNameWithInfoHeader)
50 f.Name = p[headerLen : headerLen+f.nameLen()]
51
52 return len(p), nil
53 }