]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_resume_data.go
Merge pull request #49 from jhalter/fix_1.2.3_client_no_agreement_behavior
[rbdr/mobius] / hotline / file_resume_data.go
CommitLineData
16a4ad70
JH
1package hotline
2
3import (
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
9type 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
17type 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
24func 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
33func 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
43func (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
56func (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}