2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-04-11 14:41:50 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package eestream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-06-19 16:59:09 +01:00
|
|
|
"context"
|
2018-04-11 14:41:50 +01:00
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2018-04-17 14:39:14 +01:00
|
|
|
"io/ioutil"
|
2018-04-11 14:41:50 +01:00
|
|
|
|
2018-10-25 09:24:39 +01:00
|
|
|
"storj.io/storj/internal/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.
|
2018-09-14 15:10:43 +01:00
|
|
|
func Pad(data ranger.Ranger, 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.
|
2018-09-14 15:10:43 +01:00
|
|
|
func Unpad(data ranger.Ranger, padding int) (ranger.Ranger, 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.
|
2019-06-04 12:36:27 +01:00
|
|
|
func UnpadSlow(ctx context.Context, data ranger.Ranger) (_ ranger.Ranger, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-06-19 16:59:09 +01:00
|
|
|
r, err := data.Range(ctx, data.Size()-uint32Size, uint32Size)
|
2018-06-18 17:46:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2018-04-11 14:41:50 +01:00
|
|
|
var p [uint32Size]byte
|
2018-06-18 17:46:49 +01:00
|
|
|
_, 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.
|
2018-04-17 14:39:14 +01:00
|
|
|
func PadReader(data io.ReadCloser, blockSize int) io.ReadCloser {
|
2018-04-11 14:41:50 +01:00
|
|
|
cr := newCountingReader(data)
|
2018-04-17 14:39:14 +01:00
|
|
|
return readcloser.MultiReadCloser(cr,
|
2018-06-18 17:46:49 +01:00
|
|
|
readcloser.LazyReadCloser(func() (io.ReadCloser, error) {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(makePadding(cr.N, blockSize))), nil
|
2018-04-17 14:39:14 +01:00
|
|
|
}))
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type countingReader struct {
|
2018-04-17 14:39:14 +01:00
|
|
|
R io.ReadCloser
|
2018-04-11 14:41:50 +01:00
|
|
|
N int64
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:39:14 +01:00
|
|
|
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
|
|
|
|
}
|
2018-04-17 14:39:14 +01:00
|
|
|
|
|
|
|
func (cr *countingReader) Close() error {
|
|
|
|
return cr.R.Close()
|
|
|
|
}
|