]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_resume_data.go
Extensive refactor, quality of life enhancements
[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
a2ef262a
JH
15
16 readOffset int
16a4ad70
JH
17}
18
19type ForkInfoList struct {
20 Fork [4]byte // "DATA" or "MACR"
21 DataSize [4]byte // offset from which to resume the transfer of data
22 RSVDA [4]byte // Unused
23 RSVDB [4]byte // Unused
24}
25
26func NewForkInfoList(b []byte) *ForkInfoList {
27 return &ForkInfoList{
28 Fork: [4]byte{0x44, 0x41, 0x54, 0x41},
29 DataSize: [4]byte{b[0], b[1], b[2], b[3]},
30 RSVDA: [4]byte{},
31 RSVDB: [4]byte{},
32 }
33}
34
35func NewFileResumeData(list []ForkInfoList) *FileResumeData {
36 return &FileResumeData{
37 Format: [4]byte{0x52, 0x46, 0x4C, 0x54}, // RFLT
38 Version: [2]byte{0, 1},
39 RSVD: [34]byte{},
40 ForkCount: [2]byte{0, uint8(len(list))},
41 ForkInfoList: list,
42 }
43}
44
a2ef262a
JH
45//
46//func (frd *FileResumeData) Read(p []byte) (int, error) {
47// buf := slices.Concat(
48// frd.Format[:],
49// frd.Version[:],
50// frd.RSVD[:],
51// frd.ForkCount[:],
52// )
53// for _, fil := range frd.ForkInfoList {
54// buf = append(buf, fil...)
55// _ = binary.Write(&buf, binary.LittleEndian, fil)
56// }
57//
58// var buf bytes.Buffer
59// _ = binary.Write(&buf, binary.LittleEndian, frd.Format)
60// _ = binary.Write(&buf, binary.LittleEndian, frd.Version)
61// _ = binary.Write(&buf, binary.LittleEndian, frd.RSVD)
62// _ = binary.Write(&buf, binary.LittleEndian, frd.ForkCount)
63// for _, fil := range frd.ForkInfoList {
64// _ = binary.Write(&buf, binary.LittleEndian, fil)
65// }
66//
67// return buf.Bytes(), nil
68//}
69
16a4ad70
JH
70func (frd *FileResumeData) BinaryMarshal() ([]byte, error) {
71 var buf bytes.Buffer
72 _ = binary.Write(&buf, binary.LittleEndian, frd.Format)
73 _ = binary.Write(&buf, binary.LittleEndian, frd.Version)
74 _ = binary.Write(&buf, binary.LittleEndian, frd.RSVD)
75 _ = binary.Write(&buf, binary.LittleEndian, frd.ForkCount)
76 for _, fil := range frd.ForkInfoList {
77 _ = binary.Write(&buf, binary.LittleEndian, fil)
78 }
79
80 return buf.Bytes(), nil
81}
82
83func (frd *FileResumeData) UnmarshalBinary(b []byte) error {
84 frd.Format = [4]byte{b[0], b[1], b[2], b[3]}
85 frd.Version = [2]byte{b[4], b[5]}
86 frd.ForkCount = [2]byte{b[40], b[41]}
87
88 for i := 0; i < int(frd.ForkCount[1]); i++ {
89 var fil ForkInfoList
90 start := 42 + i*16
91 end := start + 16
92
93 r := bytes.NewReader(b[start:end])
94 if err := binary.Read(r, binary.BigEndian, &fil); err != nil {
95 return err
96 }
97
98 frd.ForkInfoList = append(frd.ForkInfoList, fil)
99 }
100
101 return nil
102}