]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "encoding/binary" | |
6 | ) | |
7 | ||
8 | type FileNameWithInfo struct { | |
9 | fileNameWithInfoHeader | |
10 | name []byte // File name | |
11 | } | |
12 | ||
13 | // fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo | |
14 | type fileNameWithInfoHeader struct { | |
15 | Type [4]byte // file type code | |
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 | |
21 | } | |
22 | ||
23 | func (f *fileNameWithInfoHeader) nameLen() int { | |
24 | return int(binary.BigEndian.Uint16(f.NameSize[:])) | |
25 | } | |
26 | ||
27 | func (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 | ||
42 | func (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 | } | |
52 |