]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "encoding/binary" | |
6 | ) | |
7 | ||
8 | // FileResumeData is sent when a client or server would like to resume a transfer from an offset | |
9 | type FileResumeData struct { | |
10 | Format [4]byte // "RFLT" | |
11 | Version [2]byte // Always 1 | |
12 | RSVD [34]byte // Unused | |
13 | ForkCount [2]byte // Length of ForkInfoList. Either 2 or 3 depending on whether file has a resource fork | |
14 | ForkInfoList []ForkInfoList | |
15 | } | |
16 | ||
17 | type ForkInfoList struct { | |
18 | Fork [4]byte // "DATA" or "MACR" | |
19 | DataSize [4]byte // offset from which to resume the transfer of data | |
20 | RSVDA [4]byte // Unused | |
21 | RSVDB [4]byte // Unused | |
22 | } | |
23 | ||
24 | func NewForkInfoList(b []byte) *ForkInfoList { | |
25 | return &ForkInfoList{ | |
26 | Fork: [4]byte{0x44, 0x41, 0x54, 0x41}, | |
27 | DataSize: [4]byte{b[0], b[1], b[2], b[3]}, | |
28 | RSVDA: [4]byte{}, | |
29 | RSVDB: [4]byte{}, | |
30 | } | |
31 | } | |
32 | ||
33 | func NewFileResumeData(list []ForkInfoList) *FileResumeData { | |
34 | return &FileResumeData{ | |
35 | Format: [4]byte{0x52, 0x46, 0x4C, 0x54}, // RFLT | |
36 | Version: [2]byte{0, 1}, | |
37 | RSVD: [34]byte{}, | |
38 | ForkCount: [2]byte{0, uint8(len(list))}, | |
39 | ForkInfoList: list, | |
40 | } | |
41 | } | |
42 | ||
43 | func (frd *FileResumeData) BinaryMarshal() ([]byte, error) { | |
44 | var buf bytes.Buffer | |
45 | _ = binary.Write(&buf, binary.LittleEndian, frd.Format) | |
46 | _ = binary.Write(&buf, binary.LittleEndian, frd.Version) | |
47 | _ = binary.Write(&buf, binary.LittleEndian, frd.RSVD) | |
48 | _ = binary.Write(&buf, binary.LittleEndian, frd.ForkCount) | |
49 | for _, fil := range frd.ForkInfoList { | |
50 | _ = binary.Write(&buf, binary.LittleEndian, fil) | |
51 | } | |
52 | ||
53 | return buf.Bytes(), nil | |
54 | } | |
55 | ||
56 | func (frd *FileResumeData) UnmarshalBinary(b []byte) error { | |
57 | frd.Format = [4]byte{b[0], b[1], b[2], b[3]} | |
58 | frd.Version = [2]byte{b[4], b[5]} | |
59 | frd.ForkCount = [2]byte{b[40], b[41]} | |
60 | ||
61 | for i := 0; i < int(frd.ForkCount[1]); i++ { | |
62 | var fil ForkInfoList | |
63 | start := 42 + i*16 | |
64 | end := start + 16 | |
65 | ||
66 | r := bytes.NewReader(b[start:end]) | |
67 | if err := binary.Read(r, binary.BigEndian, &fil); err != nil { | |
68 | return err | |
69 | } | |
70 | ||
71 | frd.ForkInfoList = append(frd.ForkInfoList, fil) | |
72 | } | |
73 | ||
74 | return nil | |
75 | } |