This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
textunmarshaler.go
150 lines (123 loc) · 2.53 KB
/
textunmarshaler.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Package bloomfilter is face-meltingly fast, thread-safe,
// marshalable, unionable, probability- and
// optimal-size-calculating Bloom filter in go
//
// https://github.com/steakknife/bloomfilter
//
// Copyright © 2014, 2015, 2018 Barry Allard
//
// MIT license
//
package bloomfilter
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"fmt"
"io"
)
const (
keyFormat = "%016x"
bitsFormat = "%016x"
)
func nl() string {
return fmt.Sprintln()
}
func unmarshalTextHeader(r io.Reader) (k, n, m uint64, err error) {
format := "k" + nl() + "%d" + nl()
format += "n" + nl() + "%d" + nl()
format += "m" + nl() + "%d" + nl()
format += "keys" + nl()
_, err = fmt.Fscanf(r, format, k, n, m)
return k, n, m, err
}
func unmarshalTextKeys(r io.Reader, keys []uint64) (err error) {
for i := range keys {
_, err = fmt.Fscanf(r, keyFormat, keys[i])
if err != nil {
return err
}
}
return nil
}
func unmarshalTextBits(r io.Reader, bits []uint64) (err error) {
_, err = fmt.Fscanf(r, "bits")
if err != nil {
return err
}
for i := range bits {
_, err = fmt.Fscanf(r, bitsFormat, bits[i])
if err != nil {
return err
}
}
return nil
}
func unmarshalAndCheckTextHash(r io.Reader, f *Filter) (err error) {
_, err = fmt.Fscanf(r, "sha384")
if err != nil {
return err
}
actualHash := [sha512.Size384]byte{}
for i := range actualHash {
_, err = fmt.Fscanf(r, "%02x", actualHash[i])
if err != nil {
return err
}
}
_, expectedHash, err := f.marshal()
if err != nil {
return err
}
if !hmac.Equal(expectedHash[:], actualHash[:]) {
return errHash()
}
return nil
}
// UnmarshalText conforms to TextUnmarshaler
func UnmarshalText(text []byte) (f *Filter, err error) {
r := bytes.NewBuffer(text)
k, n, m, err := unmarshalTextHeader(r)
if err != nil {
return nil, err
}
keys, err := newKeysBlank(k)
if err != nil {
return nil, err
}
err = unmarshalTextKeys(r, keys)
if err != nil {
return nil, err
}
bits, err := newBits(m)
if err != nil {
return nil, err
}
err = unmarshalTextBits(r, bits)
if err != nil {
return nil, err
}
f, err = newWithKeysAndBits(m, keys, bits, n)
if err != nil {
return nil, err
}
err = unmarshalAndCheckTextHash(r, f)
if err != nil {
return nil, err
}
return f, nil
}
// UnmarshalText method overwrites f with data decoded from text
func (f *Filter) UnmarshalText(text []byte) error {
f.lock.Lock()
defer f.lock.Unlock()
f2, err := UnmarshalText(text)
if err != nil {
return err
}
f.m = f2.m
f.n = f2.n
copy(f.bits, f2.bits)
copy(f.keys, f2.keys)
return nil
}