aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_name_with_info.go
blob: d9add8cc6a4f16142c3c6063279a91af68a491f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package hotline

import (
	"bytes"
	"encoding/binary"
)

type FileNameWithInfo struct {
	fileNameWithInfoHeader
	name []byte // File name
}

// fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo
type fileNameWithInfoHeader struct {
	Type       [4]byte // file type code
	Creator    [4]byte // File creator code
	FileSize   [4]byte // File Size in bytes
	RSVD       [4]byte
	NameScript [2]byte // ??
	NameSize   [2]byte // Length of name field
}

func (f *fileNameWithInfoHeader) nameLen() int {
	return int(binary.BigEndian.Uint16(f.NameSize[:]))
}

func (f *FileNameWithInfo) MarshalBinary() (data []byte, err error) {
	var buf bytes.Buffer
	err = binary.Write(&buf, binary.LittleEndian, f.fileNameWithInfoHeader)
	if err != nil {
		return data, err
	}

	_, err = buf.Write(f.name)
	if err != nil {
		return data, err
	}

	return buf.Bytes(), err
}

func (f *FileNameWithInfo) UnmarshalBinary(data []byte) error {
	err := binary.Read(bytes.NewReader(data), binary.BigEndian, &f.fileNameWithInfoHeader)
	if err != nil {
		return err
	}
	headerLen := binary.Size(f.fileNameWithInfoHeader)
	f.name = data[headerLen : headerLen+f.nameLen()]

	return err
}