// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package riff implements the Resource Interchange File Format, used by media // formats such as AVI, WAVE and WEBP. // // A RIFF stream contains a sequence of chunks. Each chunk consists of an 8-byte // header (containing a 4-byte chunk type and a 4-byte chunk length), the chunk // data (presented as an io.Reader), and some padding bytes. // // A detailed description of the format is at // http://www.tactilemedia.com/info/MCI_Control_Info.html
package riff // import "golang.org/x/image/riff" import ( ) var ( errMissingPaddingByte = errors.New("riff: missing padding byte") errMissingRIFFChunkHeader = errors.New("riff: missing RIFF chunk header") errListSubchunkTooLong = errors.New("riff: list subchunk too long") errShortChunkData = errors.New("riff: short chunk data") errShortChunkHeader = errors.New("riff: short chunk header") errStaleReader = errors.New("riff: stale reader") ) // u32 decodes the first four bytes of b as a little-endian integer. func u32( []byte) uint32 { return uint32([0]) | uint32([1])<<8 | uint32([2])<<16 | uint32([3])<<24 } const chunkHeaderSize = 8 // FourCC is a four character code. type FourCC [4]byte // LIST is the "LIST" FourCC. var LIST = FourCC{'L', 'I', 'S', 'T'} // NewReader returns the RIFF stream's form type, such as "AVI " or "WAVE", and // its chunks as a *Reader. func ( io.Reader) ( FourCC, *Reader, error) { var [chunkHeaderSize]byte if , := io.ReadFull(, [:]); != nil { if == io.EOF || == io.ErrUnexpectedEOF { = errMissingRIFFChunkHeader } return FourCC{}, nil, } if [0] != 'R' || [1] != 'I' || [2] != 'F' || [3] != 'F' { return FourCC{}, nil, errMissingRIFFChunkHeader } return NewListReader(u32([4:]), ) } // NewListReader returns a LIST chunk's list type, such as "movi" or "wavl", // and its chunks as a *Reader. func ( uint32, io.Reader) ( FourCC, *Reader, error) { if < 4 { return FourCC{}, nil, errShortChunkData } := &Reader{r: } if , := io.ReadFull(, .buf[:4]); != nil { if == io.EOF || == io.ErrUnexpectedEOF { = errShortChunkData } return FourCC{}, nil, } .totalLen = - 4 return FourCC{.buf[0], .buf[1], .buf[2], .buf[3]}, , nil } // Reader reads chunks from an underlying io.Reader. type Reader struct { r io.Reader err error totalLen uint32 chunkLen uint32 chunkReader *chunkReader buf [chunkHeaderSize]byte padded bool } // Next returns the next chunk's ID, length and data. It returns io.EOF if there // are no more chunks. The io.Reader returned becomes stale after the next Next // call, and should no longer be used. // // It is valid to call Next even if all of the previous chunk's data has not // been read. func ( *Reader) () ( FourCC, uint32, io.Reader, error) { if .err != nil { return FourCC{}, 0, nil, .err } // Drain the rest of the previous chunk. if .chunkLen != 0 { := .chunkLen var int64 , .err = io.Copy(ioutil.Discard, .chunkReader) if .err == nil && uint32() != { .err = errShortChunkData } if .err != nil { return FourCC{}, 0, nil, .err } } .chunkReader = nil if .padded { if .totalLen == 0 { .err = errListSubchunkTooLong return FourCC{}, 0, nil, .err } .totalLen-- _, .err = io.ReadFull(.r, .buf[:1]) if .err != nil { if .err == io.EOF { .err = errMissingPaddingByte } return FourCC{}, 0, nil, .err } } // We are done if we have no more data. if .totalLen == 0 { .err = io.EOF return FourCC{}, 0, nil, .err } // Read the next chunk header. if .totalLen < chunkHeaderSize { .err = errShortChunkHeader return FourCC{}, 0, nil, .err } .totalLen -= chunkHeaderSize if _, .err = io.ReadFull(.r, .buf[:chunkHeaderSize]); .err != nil { if .err == io.EOF || .err == io.ErrUnexpectedEOF { .err = errShortChunkHeader } return FourCC{}, 0, nil, .err } = FourCC{.buf[0], .buf[1], .buf[2], .buf[3]} .chunkLen = u32(.buf[4:]) if .chunkLen > .totalLen { .err = errListSubchunkTooLong return FourCC{}, 0, nil, .err } .padded = .chunkLen&1 == 1 .chunkReader = &chunkReader{} return , .chunkLen, .chunkReader, nil } type chunkReader struct { z *Reader } func ( *chunkReader) ( []byte) (int, error) { if != .z.chunkReader { return 0, errStaleReader } := .z if .err != nil { if .err == io.EOF { return 0, errStaleReader } return 0, .err } := int(.chunkLen) if == 0 { return 0, io.EOF } if < 0 { // Converting uint32 to int overflowed. = math.MaxInt32 } if > len() { = len() } , := .r.Read([:]) .totalLen -= uint32() .chunkLen -= uint32() if != io.EOF { .err = } return , }