forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resume.go
50 lines (40 loc) · 1.01 KB
/
resume.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
package sha256
import (
"bytes"
"encoding/gob"
)
// Len returns the number of bytes which have been written to the digest.
func (d *digest) Len() uint64 {
return d.len
}
// State returns a snapshot of the state of the digest.
func (d *digest) State() ([]byte, error) {
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
// We encode this way so that we do not have
// to export these fields of the digest struct.
vals := []interface{}{
d.h, d.x, d.nx, d.len, d.is224,
}
for _, val := range vals {
if err := encoder.Encode(val); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
// Restore resets the digest to the given state.
func (d *digest) Restore(state []byte) error {
decoder := gob.NewDecoder(bytes.NewReader(state))
// We decode this way so that we do not have
// to export these fields of the digest struct.
vals := []interface{}{
&d.h, &d.x, &d.nx, &d.len, &d.is224,
}
for _, val := range vals {
if err := decoder.Decode(val); err != nil {
return err
}
}
return nil
}