-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathheader.go
More file actions
84 lines (69 loc) · 1.61 KB
/
header.go
File metadata and controls
84 lines (69 loc) · 1.61 KB
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
package pkg
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/splunk/stef/go/pkg/internal"
)
type FixedHeader struct {
Compression Compression
}
type VarHeader struct {
SchemaWireBytes []byte
UserData map[string]string
}
func (h *VarHeader) Serialize(dst *bytes.Buffer) error {
if err := internal.WriteUvarint(uint64(len(h.SchemaWireBytes)), dst); err != nil {
return err
}
if _, err := dst.Write(h.SchemaWireBytes); err != nil {
return err
}
if err := internal.WriteUvarint(uint64(len(h.UserData)), dst); err != nil {
return err
}
for k, v := range h.UserData {
if err := internal.WriteString(k, dst); err != nil {
return err
}
if err := internal.WriteString(v, dst); err != nil {
return err
}
}
return nil
}
const maxSchemaWireBytes = 1024 * 1024
const maxUserDataValues = 1024
func (h *VarHeader) Deserialize(src *bytes.Buffer) error {
slen, err := binary.ReadUvarint(src)
if err != nil {
return err
}
if slen > maxSchemaWireBytes {
return fmt.Errorf("schema too large: %d > %d", slen, maxSchemaWireBytes)
}
h.SchemaWireBytes = make([]byte, slen)
if _, err = src.Read(h.SchemaWireBytes); err != nil {
return err
}
count, err := binary.ReadUvarint(src)
if err != nil {
return err
}
if count > maxUserDataValues {
return fmt.Errorf("too many user data values: %d > %d", count, maxUserDataValues)
}
h.UserData = make(map[string]string, count)
for i := 0; i < int(count); i++ {
k, err := internal.ReadString(src)
if err != nil {
return err
}
v, err := internal.ReadString(src)
if err != nil {
return err
}
h.UserData[k] = v
}
return nil
}