]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_name_with_info.go
patch: v0.8.1
[rbdr/mobius] / hotline / file_name_with_info.go
CommitLineData
43ecc0f4
JH
1package hotline
2
3import (
72dd37f1 4 "bytes"
43ecc0f4 5 "encoding/binary"
43ecc0f4
JH
6)
7
43ecc0f4 8type FileNameWithInfo struct {
72dd37f1
JH
9 fileNameWithInfoHeader
10 name []byte // File name
43ecc0f4
JH
11}
12
72dd37f1
JH
13// fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo
14type fileNameWithInfoHeader struct {
7cd900d6 15 Type [4]byte // File type code
72dd37f1
JH
16 Creator [4]byte // File creator code
17 FileSize [4]byte // File Size in bytes
18 RSVD [4]byte
19 NameScript [2]byte // ??
20 NameSize [2]byte // Length of name field
43ecc0f4
JH
21}
22
72dd37f1
JH
23func (f *fileNameWithInfoHeader) nameLen() int {
24 return int(binary.BigEndian.Uint16(f.NameSize[:]))
43ecc0f4 25}
72dd37f1
JH
26
27func (f *FileNameWithInfo) MarshalBinary() (data []byte, err error) {
28 var buf bytes.Buffer
29 err = binary.Write(&buf, binary.LittleEndian, f.fileNameWithInfoHeader)
30 if err != nil {
31 return data, err
32 }
33
34 _, err = buf.Write(f.name)
35 if err != nil {
36 return data, err
37 }
38
39 return buf.Bytes(), err
40}
41
42func (f *FileNameWithInfo) UnmarshalBinary(data []byte) error {
43 err := binary.Read(bytes.NewReader(data), binary.BigEndian, &f.fileNameWithInfoHeader)
44 if err != nil {
45 return err
46 }
47 headerLen := binary.Size(f.fileNameWithInfoHeader)
48 f.name = data[headerLen : headerLen+f.nameLen()]
49
50 return err
51}