2018-10-10 03:23:35 +02:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-23 22:41:19 +02:00
|
|
|
"math"
|
|
|
|
"strings"
|
2018-10-10 03:23:35 +02:00
|
|
|
|
2019-01-09 19:05:41 +01:00
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
2018-10-10 03:23:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Stream []Blob
|
|
|
|
|
2018-10-23 22:41:19 +02:00
|
|
|
// -1 to leave room for padding, since there must be at least one byte of pkcs7 padding
|
|
|
|
const maxBlobDataSize = MaxBlobSize - 1
|
2018-10-10 03:23:35 +02:00
|
|
|
|
2018-10-23 22:41:19 +02:00
|
|
|
// New creates a new Stream from a byte slice
|
|
|
|
func New(data []byte) (Stream, error) {
|
2018-10-10 03:23:35 +02:00
|
|
|
key := randIV()
|
2018-10-23 22:41:19 +02:00
|
|
|
ivs := make([][]byte, numContentBlobs(data)+1) // +1 for terminating 0-length blob
|
2018-10-10 03:23:35 +02:00
|
|
|
for i := range ivs {
|
|
|
|
ivs[i] = randIV()
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:19 +02:00
|
|
|
return makeStream(data, key, ivs, "", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconstruct creates a stream from the given data using predetermined IVs and key from the SD blob
|
|
|
|
// NOTE: this will assume that all blobs except the last one are at max length. in theory this is not
|
|
|
|
// required, but in practice this is always true. if this is false, streams may not match exactly
|
|
|
|
func Reconstruct(data []byte, sdBlob SDBlob) (Stream, error) {
|
|
|
|
ivs := make([][]byte, len(sdBlob.BlobInfos))
|
|
|
|
for i := range ivs {
|
|
|
|
ivs[i] = sdBlob.BlobInfos[i].IV
|
|
|
|
}
|
|
|
|
|
|
|
|
return makeStream(data, sdBlob.Key, ivs, sdBlob.StreamName, sdBlob.SuggestedFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeStream(data, key []byte, ivs [][]byte, streamName, suggestedFilename string) (Stream, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
numBlobs := numContentBlobs(data)
|
|
|
|
if len(ivs) != numBlobs+1 { // +1 for terminating 0-length blob
|
|
|
|
return nil, errors.Err("incorrect number of IVs provided")
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:23:35 +02:00
|
|
|
s := make(Stream, numBlobs+1) // +1 for sd blob
|
|
|
|
for i := 0; i < numBlobs; i++ {
|
2018-10-23 22:41:19 +02:00
|
|
|
start := i * maxBlobDataSize
|
2018-10-10 03:23:35 +02:00
|
|
|
end := start + maxBlobDataSize
|
|
|
|
if end > len(data) {
|
|
|
|
end = len(data)
|
|
|
|
}
|
|
|
|
s[i+1], err = NewBlob(data[start:end], key, ivs[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:19 +02:00
|
|
|
sd := newSdBlob(s[1:], key, ivs, streamName, suggestedFilename)
|
|
|
|
jsonSD, err := sd.ToBlob()
|
2018-10-10 03:23:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:19 +02:00
|
|
|
// COMPATIBILITY HACK to make json output match python's json. this can be
|
|
|
|
// removed when we implement canonical JSON encoding
|
|
|
|
jsonSD = []byte(strings.Replace(string(jsonSD), ",", ", ", -1))
|
|
|
|
jsonSD = []byte(strings.Replace(string(jsonSD), ":", ": ", -1))
|
|
|
|
|
|
|
|
s[0] = jsonSD
|
2018-10-10 03:23:35 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Stream) Data() ([]byte, error) {
|
|
|
|
if len(s) < 2 {
|
2018-10-23 22:41:19 +02:00
|
|
|
return nil, errors.Err("stream must be at least 2 blobs long") // sd blob and content blob
|
2018-10-10 03:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sdBlob := &SDBlob{}
|
|
|
|
err := sdBlob.FromBlob(s[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sdBlob.IsValid() {
|
|
|
|
return nil, errors.Err("sd blob is not valid")
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:19 +02:00
|
|
|
if sdBlob.BlobInfos[len(sdBlob.BlobInfos)-1].Length != 0 {
|
|
|
|
return nil, errors.Err("sd blob is missing the terminating 0-length blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s[1:]) != len(sdBlob.BlobInfos)-1 { // -1 for terminating 0-length blob
|
|
|
|
return nil, errors.Err("number of blobs in stream does not match number of blobs in sd info")
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:23:35 +02:00
|
|
|
var file []byte
|
2018-10-23 22:41:19 +02:00
|
|
|
for i, blobInfo := range sdBlob.BlobInfos {
|
|
|
|
if blobInfo.Length == 0 {
|
|
|
|
if i != len(sdBlob.BlobInfos)-1 {
|
|
|
|
return nil, errors.Err("got 0-length blob before end of stream")
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if blobInfo.BlobNum != i {
|
|
|
|
return nil, errors.Err("blobs are out of order in sd blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := s[i+1]
|
|
|
|
|
|
|
|
if !bytes.Equal(blob.Hash(), blobInfo.BlobHash) {
|
2018-10-10 03:23:35 +02:00
|
|
|
return nil, errors.Err("blob hash doesn't match hash in blobInfo")
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:41:19 +02:00
|
|
|
data, err := blob.Plaintext(sdBlob.Key, blobInfo.IV)
|
2018-10-10 03:23:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
file = append(file, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
2018-10-23 22:41:19 +02:00
|
|
|
|
|
|
|
//numContentBlobs returns the number of content blobs required to store the data
|
|
|
|
func numContentBlobs(data []byte) int {
|
|
|
|
return int(math.Ceil(float64(len(data)) / float64(maxBlobDataSize)))
|
|
|
|
}
|