aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_resume_data.go
blob: 7cbfb175a76b34e4d6b1aada06a4d4e6df05da6c (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package hotline

import (
	"bytes"
	"encoding/binary"
	"fmt"
)

// FileResumeData is sent when a client or server would like to resume a transfer from an offset
type FileResumeData struct {
	Format       [4]byte  // "RFLT"
	Version      [2]byte  // Always 1
	RSVD         [34]byte // Present in the Hotline protocol docs, but unused.  Left here for documentation purposes.
	ForkCount    [2]byte  // Length of ForkInfoList.  Either 2 or 3 depending on whether file has a resource fork
	ForkInfoList []ForkInfoList
}

// ForkType represents a 4-byte fork type identifier
type ForkType [4]byte

// String returns a string representation of the fork type
func (ft ForkType) String() string {
	return fmt.Sprintf("%c%c%c%c", ft[0], ft[1], ft[2], ft[3])
}

type ForkInfoList struct {
	Fork     ForkType // "DATA", "INFO", or "MACR"
	DataSize [4]byte  // offset from which to resume the transfer of data
	RSVDA    [4]byte  // Present in the Hotline protocol docs, but unused.  Left here for documentation purposes.
	RSVDB    [4]byte  // Present in the Hotline protocol docs, but unused.  Left here for documentation purposes.
}

var (
	ForkTypeDATA = ForkType{0x44, 0x41, 0x54, 0x41} // DATA: Data fork
	ForkTypeINFO = ForkType{0x49, 0x4E, 0x46, 0x4F} // INFO: Information fork
	ForkTypeMACR = ForkType{0x4d, 0x41, 0x43, 0x52} // MACR: Mac resource fork
)

func NewForkInfoList(b []byte) *ForkInfoList {
	return &ForkInfoList{
		Fork:     ForkTypeDATA,
		DataSize: [4]byte{b[0], b[1], b[2], b[3]},
		RSVDA:    [4]byte{},
		RSVDB:    [4]byte{},
	}
}

var (
	FormatFILP = [4]byte{0x46, 0x49, 0x4c, 0x50} // Flattened file format: "FILP"
	FormatRFLT = [4]byte{0x52, 0x46, 0x4C, 0x54} // File resume format: "RFLT" (?)

	PlatformAMAC = [4]byte{0x41, 0x4D, 0x41, 0x43} // Mac platform: "AMAC"
	PlatformMWIN = [4]byte{0x4D, 0x57, 0x49, 0x4E} // Windows platform: "MWIN"
)

func NewFileResumeData(list []ForkInfoList) *FileResumeData {
	return &FileResumeData{
		Format:       FormatRFLT,
		Version:      [2]byte{0, 1},
		RSVD:         [34]byte{},
		ForkCount:    [2]byte{0, uint8(len(list))},
		ForkInfoList: list,
	}
}

func (frd *FileResumeData) BinaryMarshal() ([]byte, error) {
	var buf bytes.Buffer
	_ = binary.Write(&buf, binary.LittleEndian, frd.Format)
	_ = binary.Write(&buf, binary.LittleEndian, frd.Version)
	_ = binary.Write(&buf, binary.LittleEndian, frd.RSVD)
	_ = binary.Write(&buf, binary.LittleEndian, frd.ForkCount)
	for _, fil := range frd.ForkInfoList {
		_ = binary.Write(&buf, binary.LittleEndian, fil)
	}

	return buf.Bytes(), nil
}

// resumeDataHeaderLen is the fixed-size header (Format, Version, RSVD, ForkCount)
// that precedes the variable-length fork info list.
const resumeDataHeaderLen = 42

// forkInfoLen is the size of a single ForkInfoList entry.
const forkInfoLen = 16

func (frd *FileResumeData) UnmarshalBinary(b []byte) error {
	if len(b) < resumeDataHeaderLen {
		return fmt.Errorf("file resume data too short: %d bytes, need at least %d", len(b), resumeDataHeaderLen)
	}

	frd.Format = [4]byte{b[0], b[1], b[2], b[3]}
	frd.Version = [2]byte{b[4], b[5]}
	frd.ForkCount = [2]byte{b[40], b[41]}

	forkCount := int(frd.ForkCount[1])
	if need := resumeDataHeaderLen + forkCount*forkInfoLen; len(b) < need {
		return fmt.Errorf("file resume data truncated: %d bytes, need %d for %d forks", len(b), need, forkCount)
	}

	for i := 0; i < forkCount; i++ {
		var fil ForkInfoList
		start := resumeDataHeaderLen + i*forkInfoLen
		end := start + forkInfoLen

		r := bytes.NewReader(b[start:end])
		if err := binary.Read(r, binary.BigEndian, &fil); err != nil {
			return err
		}

		frd.ForkInfoList = append(frd.ForkInfoList, fil)
	}

	return nil
}