storj/pkg/eestream/pad.go

91 lines
2.5 KiB
Go
Raw Normal View History

2018-04-11 14:41:50 +01:00
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package eestream
import (
"bytes"
"context"
2018-04-11 14:41:50 +01:00
"encoding/binary"
"io"
"io/ioutil"
2018-04-11 14:41:50 +01:00
"storj.io/storj/internal/pkg/readcloser"
2018-04-11 14:41:50 +01:00
"storj.io/storj/pkg/ranger"
)
const (
uint32Size = 4
)
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.RangeCloser, blockSize int) (
2018-04-11 14:41:50 +01:00
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.RangeCloser, padding int) (ranger.RangeCloser, error) {
2018-04-11 14:41:50 +01:00
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.RangeCloser) (ranger.RangeCloser, error) {
r, err := data.Range(ctx, data.Size()-uint32Size, uint32Size)
if err != nil {
return nil, Error.Wrap(err)
}
2018-04-11 14:41:50 +01:00
var p [uint32Size]byte
_, err = io.ReadFull(r, p[:])
2018-04-11 14:41:50 +01:00
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 {
2018-04-11 14:41:50 +01:00
cr := newCountingReader(data)
return readcloser.MultiReadCloser(cr,
readcloser.LazyReadCloser(func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(makePadding(cr.N, blockSize))), nil
}))
2018-04-11 14:41:50 +01:00
}
type countingReader struct {
R io.ReadCloser
2018-04-11 14:41:50 +01:00
N int64
}
func newCountingReader(r io.ReadCloser) *countingReader {
2018-04-11 14:41:50 +01:00
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()
}