...
1 package network
2
3 import (
4 "bufio"
5 "bytes"
6 "encoding/binary"
7 )
8
9 func ScanFile(data []byte, atEOF bool) (advance int, token []byte, err error) {
10 if len(data) <= 9 {
11 return 0, nil, nil
12 }
13 if data[0] == 0 {
14 return 0, []byte{}, bufio.ErrFinalToken
15 }
16 term := bytes.Index(data[5:], []byte{0x0}) + 6
17 if term < 0 {
18 return 0, nil, nil
19 }
20 token = make([]byte, term+16)
21 copy(token, data)
22 return term + 16, token, nil
23 }
24
25 func fileTokenToFile(data []byte) (File, error) {
26 checksum := [16]byte{}
27 copy(checksum[:], data[len(data)-16:])
28 return File{
29 uint8(data[0]),
30 binary.LittleEndian.Uint32(data[1:5]),
31 string(data[5 : len(data)-16]),
32 checksum,
33 }, nil
34 }
35
View as plain text