/
splitfile.go
61 lines (49 loc) · 1.52 KB
/
splitfile.go
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
package splitfile
import (
"encoding/hex"
"errors"
)
// ChecksumAlgo represents a checksum algorithm.
type ChecksumAlgo string
const (
ChecksumAlgoNone ChecksumAlgo = "None"
ChecksumAlgoCRC32 ChecksumAlgo = "CRC32"
)
var (
ErrUnknownChecksumAlgo = errors.New("unknown checksum algorithm")
)
// Splitfile is the structure of a of a .split file.
type Splitfile struct {
// Algorithm used for all checksums in the file.
ChecksumAlgo ChecksumAlgo `json:"checksum_algo"`
// The whole main file the chunks were derived from. The checksum is of the
// whole file.
MainFile *File `json:"main_file"`
// The chunks that were derived from the main file. Number of chunks can be
// derived from the number of elements, and their order represents their
// actual order of concatenation to recreate the main file.
Chunks []*File `json:"chunks"`
}
// File represents a file, either a main file or an specific chunk.
type File struct {
// Original filename at time of chunk creation.
Filename string `json:"filename"`
// Checksum of the file.
Checksum Checksum `json:"checksum"`
// Size of the file.
Size int64 `json:"size"`
}
// Checksum represents a hash checksum.
//
// Implmentens TextMarshaler and TextUnmarshaler.
type Checksum []byte
func (chk Checksum) MarshalText() (text []byte, err error) {
text = make([]byte, hex.EncodedLen(len(chk)))
hex.Encode(text, chk)
return
}
func (chk *Checksum) UnmarshalText(text []byte) error {
*chk = make(Checksum, hex.DecodedLen(len(text)))
_, err := hex.Decode(*chk, text)
return err
}