-
Notifications
You must be signed in to change notification settings - Fork 402
/
pad.go
94 lines (81 loc) · 2.71 KB
/
pad.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package eestream
import (
"bytes"
"context"
"encoding/binary"
"io"
"io/ioutil"
"storj.io/storj/internal/readcloser"
"storj.io/storj/pkg/ranger"
)
const (
uint32Size = 4
)
// makePadding calculates how many bytes of padding are needed to fill
// an encryption block then creates a slice of zero bytes that size.
// The last byte of the padding slice contains the count of the total padding bytes added.
func makePadding(dataLen int64, blockSize int) []byte {
amount := dataLen + uint32Size
r := amount % int64(blockSize)
padding := uint32Size
if r > 0 {
padding += blockSize - int(r)
}
paddingBytes := bytes.Repeat([]byte{0}, padding)
binary.BigEndian.PutUint32(paddingBytes[padding-uint32Size:], uint32(padding))
return paddingBytes
}
// Pad takes a Ranger and returns another Ranger that is a multiple of
// blockSize in length. The return value padding is a convenience to report how
// much padding was added.
func Pad(data ranger.Ranger, blockSize int) (
rr ranger.Ranger, padding int) {
paddingBytes := makePadding(data.Size(), blockSize)
return ranger.Concat(data, ranger.ByteRanger(paddingBytes)), len(paddingBytes)
}
// Unpad takes a previously padded Ranger data source and returns an unpadded
// ranger, given the amount of padding. This is preferable to UnpadSlow if you
// can swing it.
func Unpad(data ranger.Ranger, padding int) (ranger.Ranger, error) {
return ranger.Subrange(data, 0, data.Size()-int64(padding))
}
// UnpadSlow is like Unpad, but does not require the amount of padding.
// UnpadSlow will have to do extra work to make up for this missing information.
func UnpadSlow(ctx context.Context, data ranger.Ranger) (_ ranger.Ranger, err error) {
defer mon.Task()(&ctx)(&err)
r, err := data.Range(ctx, data.Size()-uint32Size, uint32Size)
if err != nil {
return nil, Error.Wrap(err)
}
var p [uint32Size]byte
_, err = io.ReadFull(r, p[:])
if err != nil {
return nil, Error.Wrap(err)
}
return Unpad(data, int(binary.BigEndian.Uint32(p[:])))
}
// PadReader is like Pad but works on a basic Reader instead of a Ranger.
func PadReader(data io.ReadCloser, blockSize int) io.ReadCloser {
cr := newCountingReader(data)
return readcloser.MultiReadCloser(cr,
readcloser.LazyReadCloser(func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(makePadding(cr.N, blockSize))), nil
}))
}
type countingReader struct {
R io.ReadCloser
N int64
}
func newCountingReader(r io.ReadCloser) *countingReader {
return &countingReader{R: r}
}
func (cr *countingReader) Read(p []byte) (n int, err error) {
n, err = cr.R.Read(p)
cr.N += int64(n)
return n, err
}
func (cr *countingReader) Close() error {
return cr.R.Close()
}