-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_stream.go
More file actions
54 lines (49 loc) · 1.29 KB
/
block_stream.go
File metadata and controls
54 lines (49 loc) · 1.29 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
package bitsafe
import (
"fmt"
"io"
"github.com/boxtheta/bitsafe/util"
)
// reads from a io.Reader and outputs block_size blocks, padding the last one
// if necessary.
type BlockStream struct {
// Maximum block size is 256 bytes
block_size uint8
pad_func util.PaddingFunc
reader io.Reader
use_rand_pad bool
eos_pad bool
}
func NewBlockStream(block_size uint8,
reader io.Reader,
pad_fn util.PaddingFunc,
use_rand_pad bool,
eos_pa bool,
) BlockStream {
return BlockStream{
block_size: block_size,
pad_func: pad_fn,
reader: reader,
use_rand_pad: use_rand_pad,
eos_pad: false,
}
}
// Provides an io.Reader compliant reader that always returns a full block_size
// of data where a block is any n < 256, if the returned read size is less than
// block_size then the difference between the returned read and block_size will
// be filled with padding bytes.
func (s *BlockStream) Read(b []byte) (int, error) {
if len(b) != int(s.block_size) {
return 0, fmt.Errorf("expected b to be %d bytes wide, got %d", s.block_size, len(b))
}
block := make([]byte, s.block_size)
rs, err := s.reader.Read(block)
if err != nil {
return 0, err
}
copy(b, block)
if rs < int(s.block_size) {
s.pad_func(b, s.block_size, uint8(rs), s.use_rand_pad)
}
return rs, nil
}