summaryrefslogtreecommitdiff
path: root/vendor/github.com/zeebo/blake3/digest.go
blob: 58365d5abd7edcbc4cf2e344e0b08f0163e40241 (plain)
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
package blake3

import (
	"fmt"
	"io"
	"unsafe"

	"github.com/zeebo/blake3/internal/alg"
	"github.com/zeebo/blake3/internal/consts"
	"github.com/zeebo/blake3/internal/utils"
)

// Digest captures the state of a Hasher allowing reading and seeking through
// the output stream.
type Digest struct {
	counter uint64
	chain   [8]uint32
	block   [16]uint32
	blen    uint32
	flags   uint32
	buf     [16]uint32
	bufn    int
}

// Read reads data frm the hasher into out. It always fills the entire buffer and
// never errors. The stream will wrap around when reading past 2^64 bytes.
func (d *Digest) Read(p []byte) (n int, err error) {
	n = len(p)

	if d.bufn > 0 {
		n := d.slowCopy(p)
		p = p[n:]
		d.bufn -= n
	}

	for len(p) >= 64 {
		d.fillBuf()

		if consts.IsLittleEndian {
			*(*[64]byte)(unsafe.Pointer(&p[0])) = *(*[64]byte)(unsafe.Pointer(&d.buf[0]))
		} else {
			utils.WordsToBytes(&d.buf, p)
		}

		p = p[64:]
		d.bufn = 0
	}

	if len(p) == 0 {
		return n, nil
	}

	d.fillBuf()
	d.bufn -= d.slowCopy(p)

	return n, nil
}

// Seek sets the position to the provided location. Only SeekStart and
// SeekCurrent are allowed.
func (d *Digest) Seek(offset int64, whence int) (int64, error) {
	switch whence {
	case io.SeekStart:
	case io.SeekEnd:
		return 0, fmt.Errorf("seek from end not supported")
	case io.SeekCurrent:
		offset += int64(consts.BlockLen*d.counter) - int64(d.bufn)
	default:
		return 0, fmt.Errorf("invalid whence: %d", whence)
	}
	if offset < 0 {
		return 0, fmt.Errorf("seek before start")
	}
	d.setPosition(uint64(offset))
	return offset, nil
}

func (d *Digest) setPosition(pos uint64) {
	d.counter = pos / consts.BlockLen
	d.fillBuf()
	d.bufn -= int(pos % consts.BlockLen)
}

func (d *Digest) slowCopy(p []byte) (n int) {
	off := uint(consts.BlockLen-d.bufn) % consts.BlockLen
	if consts.IsLittleEndian {
		n = copy(p, (*[consts.BlockLen]byte)(unsafe.Pointer(&d.buf[0]))[off:])
	} else {
		var tmp [consts.BlockLen]byte
		utils.WordsToBytes(&d.buf, tmp[:])
		n = copy(p, tmp[off:])
	}
	return n
}

func (d *Digest) fillBuf() {
	alg.Compress(&d.chain, &d.block, d.counter, d.blen, d.flags, &d.buf)
	d.counter++
	d.bufn = consts.BlockLen
}