func (this *SnappyCodec) Forward(src, dst []byte) (uint, uint, error) { if src == nil { return uint(0), uint(0), errors.New("Invalid null source buffer") } if dst == nil { return uint(0), uint(0), errors.New("Invalid null destination buffer") } if kanzi.SameByteSlices(src, dst, false) { return 0, 0, errors.New("Input and output buffers cannot be equal") } count := this.size if this.size == 0 { count = uint(len(src)) } if n := snappy.MaxEncodedLen(int(count)); len(dst) < n { return 0, 0, fmt.Errorf("Output buffer is too small - size: %d, required %d", len(dst), n) } res, err := snappy.Encode(dst, src[0:count]) if err != nil { return 0, 0, fmt.Errorf("Encoding error: %v", err) } return count, uint(len(res)), nil }
func NewWriter() *Writer { w := new(Writer) w.h = newHash() w.buf = make([]byte, config.BlockSize) w.cdata = make([]byte, headerSize+snappy.MaxEncodedLen(config.BlockSize)+PadSize) w.refs = make([]*Ref, 0) w.kind = dataBlockKind return w }
func NewReader(ref *Ref) (r *Reader, err error) { r = new(Reader) r.h = newHash() r.box = make([]byte, nonceSize+headerSize+snappy.MaxEncodedLen(config.BlockSize)+PadSize) r.refs = []*Ref{ref} if err := r.loadPointers(); err != nil { return nil, err } return }
func (w *Writer) writeBlock(buf *util.Buffer, compression opt.Compression) (bh blockHandle, err error) { // Compress the buffer if necessary. var b []byte if compression == opt.SnappyCompression { // Allocate scratch enough for compression and block trailer. if n := snappy.MaxEncodedLen(buf.Len()) + blockTrailerLen; len(w.compressionScratch) < n { w.compressionScratch = make([]byte, n) } var compressed []byte compressed, err = snappy.Encode(w.compressionScratch, buf.Bytes()) if err != nil { return } n := len(compressed) b = compressed[:n+blockTrailerLen] b[n] = blockTypeSnappyCompression } else { tmp := buf.Alloc(blockTrailerLen) tmp[0] = blockTypeNoCompression b = buf.Bytes() } // Calculate the checksum. n := len(b) - 4 checksum := util.NewCRC(b[:n]).Value() binary.LittleEndian.PutUint32(b[n:], checksum) // Write the buffer to the file. _, err = w.writer.Write(b) if err != nil { return } bh = blockHandle{w.offset, uint64(len(b) - blockTrailerLen)} w.offset += uint64(len(b)) return }
// Walks the given ref and its subrefs. func WalkRefs(ref *Ref, callback func(*Ref) error) error { r := new(Reader) r.h = newHash() r.box = make([]byte, nonceSize+headerSize+snappy.MaxEncodedLen(config.BlockSize)+PadSize) r.refs = []*Ref{ref} if err := r.loadBlock(); err != nil { return err } if err := callback(ref); err != nil { return err } var tmp [RefLen]byte for r.kind != dataBlockKind { newrefs := make([]*Ref, 0) for { _, err := r.Read(tmp[:]) if err != nil { if err == io.EOF { break } return err } newrefs = append(newrefs, RefFromBytes(tmp[:])) } for _, v := range newrefs { if err := callback(v); err != nil { return err } } r.refs = newrefs if err := r.loadBlock(); err != nil { return err } } return nil }
const Ext = ".sz" // MediaType is the MIME type used to represent snappy framed content. const MediaType = "application/x-snappy-framed" // ContentEncoding is the appropriate HTTP Content-Encoding header value for // requests containing a snappy framed entity body. const ContentEncoding = "x-snappy-framed" // MaxBlockSize is the maximum number of decoded bytes allowed to be // represented in a snappy framed block (sections 4.2 and 4.3). const MaxBlockSize = 65536 // maxEncodedBlockSize is the maximum number of encoded bytes in a framed // block. var maxEncodedBlockSize = uint32(snappy.MaxEncodedLen(MaxBlockSize)) const VerifyChecksum = true const SkipVerifyChecksum = false // Block types defined by the snappy framed format specification. const ( blockCompressed = 0x00 blockUncompressed = 0x01 blockPadding = 0xfe blockStreamIdentifier = 0xff ) // streamID is the stream identifier block that begins a valid snappy framed // stream. var streamID = []byte{0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59}
func (this SnappyCodec) MaxEncodedLen(srcLen int) int { return snappy.MaxEncodedLen(srcLen) }