]>
Commit | Line | Data |
---|---|---|
43ecc0f4 JH |
1 | package hotline |
2 | ||
3 | import ( | |
72dd37f1 | 4 | "bytes" |
43ecc0f4 | 5 | "encoding/binary" |
43ecc0f4 JH |
6 | ) |
7 | ||
43ecc0f4 | 8 | type 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 |
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 | |
43ecc0f4 JH |
21 | } |
22 | ||
72dd37f1 JH |
23 | func (f *fileNameWithInfoHeader) nameLen() int { |
24 | return int(binary.BigEndian.Uint16(f.NameSize[:])) | |
43ecc0f4 | 25 | } |
72dd37f1 JH |
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 |