Beispiel #1
0
func NewChunkReader(r io.Reader, c btncrypt.Cipher) (*ChunkReader, error) {
	cr := &ChunkReader{r: r, c: c}

	if err := cr.header.ReadFrom(r, c); err != nil {
		return nil, fmt.Errorf("Failed to read header: %v", err)
	}

	var err error
	cr.bdr, err = btncrypt.NewReader(cr.r, cr.c, cr.Length())
	if err != nil {
		return nil, err
	}

	return cr, nil
}
Beispiel #2
0
func (h *ChunkHeader) ReadFrom(r io.Reader, c btncrypt.Cipher) error {
	magic := make([]byte, SignatureLength+1)
	if _, err := r.Read(magic); err != nil {
		if err == io.EOF {
			return err
		}
		return fmt.Errorf("Failed to read signature magic / format bytes: %v", err)
	}
	if magic[0] != ChunkSignatureMagic1 ||
		magic[1] != ChunkSignatureMagic2 {
		return errors.New("signature magic mismatch")
	}
	if magic[2] != CurrentFormat {
		return fmt.Errorf("Expected format version %x but got %x", CurrentFormat, magic[2])
	}

	framelen := ChunkHeaderLength - c.FrameOverhead() - SignatureLength - 1
	bdr, err := btncrypt.NewReader(r, c, framelen)
	if err != nil {
		return err
	}

	encoded := make([]byte, framelen)
	if _, err := io.ReadFull(bdr, encoded); err != nil {
		return fmt.Errorf("Failed to read header frame: %v", err)
	}
	if !bdr.HasReadAll() {
		panic("Incomplete read in prologue frame !?!?")
	}

	dec := gob.NewDecoder(bytes.NewBuffer(encoded))
	if err := dec.Decode(h); err != nil {
		return err
	}

	return nil
}
Beispiel #3
0
func (ch *ChunkIO) readContentFrame(i int) (*decryptedContentFrame, error) {
	// the frame carries a part of the content at offset
	offset := i * ContentFramePayloadLength

	// payload length of the encrypted frame
	framePayloadLen := ContentFramePayloadLength
	isLastFrame := false
	distToLast := ch.PayloadLen() - offset
	if distToLast <= ContentFramePayloadLength {
		framePayloadLen = distToLast
		isLastFrame = true
	}

	// the offset of the start of the frame in blob
	blobOffset := ch.encryptedFrameOffset(i)

	rd := &blobstore.OffsetReader{ch.bh, int64(blobOffset)}
	bdr, err := btncrypt.NewReader(rd, ch.c, framePayloadLen)
	if err != nil {
		return nil, fmt.Errorf("Failed to create BtnDecryptReader: %v", err)
	}

	p := make([]byte, framePayloadLen, ContentFramePayloadLength)
	if _, err := io.ReadFull(bdr, p); err != nil {
		return nil, fmt.Errorf("Failed to decrypt frame idx: %d, err: %v", i, err)
	}
	if !bdr.HasReadAll() {
		panic("Incomplete frame read")
	}

	log.Printf("ChunkIO: Read content frame idx: %d", i)
	return &decryptedContentFrame{
		P: p, Offset: offset,
		IsLastFrame: isLastFrame,
	}, nil
}