]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_name_with_info.go
Add .goreleaser.yaml
[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
JH
11 fileNameWithInfoHeader
12 name []byte // File name
43ecc0f4
JH
13}
14
72dd37f1
JH
15// fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo
16type fileNameWithInfoHeader struct {
7cd900d6 17 Type [4]byte // File type code
72dd37f1
JH
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
43ecc0f4
JH
23}
24
72dd37f1
JH
25func (f *fileNameWithInfoHeader) nameLen() int {
26 return int(binary.BigEndian.Uint16(f.NameSize[:]))
43ecc0f4 27}
72dd37f1 28
b129b7cb
JH
29// Read implements io.Reader for FileNameWithInfo
30func (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
72dd37f1
JH
42}
43
b129b7cb
JH
44func (f *FileNameWithInfo) Write(p []byte) (int, error) {
45 err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.fileNameWithInfoHeader)
72dd37f1 46 if err != nil {
b129b7cb 47 return 0, err
72dd37f1
JH
48 }
49 headerLen := binary.Size(f.fileNameWithInfoHeader)
b129b7cb 50 f.name = p[headerLen : headerLen+f.nameLen()]
72dd37f1 51
b129b7cb 52 return len(p), nil
72dd37f1 53}