2015-09-29 12:04:07 -07:00
|
|
|
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
2015-10-28 11:20:59 -07:00
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
|
|
|
|
// See page 362.
|
2015-11-30 19:37:52 -08:00
|
|
|
// The version of this file that appears in the book does not comply
|
|
|
|
// with the proposed rules for passing pointers between Go and C.
|
|
|
|
// (https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md)
|
|
|
|
// The rules forbid a C function like bz2compress from storing 'in' and
|
|
|
|
// 'out' (pointers to variables allocated by Go) into the Go variable 's',
|
|
|
|
// even temporarily.
|
|
|
|
//
|
|
|
|
// To comply with the rules, the bz_stream variable must be allocated
|
|
|
|
// by C code. We have introduced two C functions, bz2alloc and
|
|
|
|
// bz2free, to allocate and free instances of the bz_stream type.
|
|
|
|
// Also, we have changed bz2compress so that before it returns, it
|
|
|
|
// clears the fields of the bz_stream that contain point to Go
|
|
|
|
// variables.
|
|
|
|
|
2015-09-29 12:04:07 -07:00
|
|
|
//!+
|
|
|
|
|
|
|
|
// Package bzip provides a writer that uses bzip2 compression (bzip.org).
|
|
|
|
package bzip
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -I/usr/include
|
|
|
|
#cgo LDFLAGS: -L/usr/lib -lbz2
|
|
|
|
#include <bzlib.h>
|
2015-11-30 19:37:52 -08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
bz_stream* bz2alloc() { return calloc(1, sizeof(bz_stream)); }
|
2015-09-29 12:04:07 -07:00
|
|
|
int bz2compress(bz_stream *s, int action,
|
|
|
|
char *in, unsigned *inlen, char *out, unsigned *outlen);
|
2015-11-30 19:37:52 -08:00
|
|
|
void bz2free(bz_stream* s) { return free(s); }
|
2015-09-29 12:04:07 -07:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type writer struct {
|
|
|
|
w io.Writer // underlying output stream
|
|
|
|
stream *C.bz_stream
|
|
|
|
outbuf [64 * 1024]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWriter returns a writer for bzip2-compressed streams.
|
|
|
|
func NewWriter(out io.Writer) io.WriteCloser {
|
|
|
|
const (
|
|
|
|
blockSize = 9
|
|
|
|
verbosity = 0
|
|
|
|
workFactor = 30
|
|
|
|
)
|
2015-11-30 19:37:52 -08:00
|
|
|
w := &writer{w: out, stream: C.bz2alloc()}
|
2015-09-29 12:04:07 -07:00
|
|
|
C.BZ2_bzCompressInit(w.stream, blockSize, verbosity, workFactor)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
//!-
|
|
|
|
|
|
|
|
//!+write
|
|
|
|
func (w *writer) Write(data []byte) (int, error) {
|
|
|
|
if w.stream == nil {
|
|
|
|
panic("closed")
|
|
|
|
}
|
|
|
|
var total int // uncompressed bytes written
|
|
|
|
|
|
|
|
for len(data) > 0 {
|
|
|
|
inlen, outlen := C.uint(len(data)), C.uint(cap(w.outbuf))
|
|
|
|
C.bz2compress(w.stream, C.BZ_RUN,
|
|
|
|
(*C.char)(unsafe.Pointer(&data[0])), &inlen,
|
|
|
|
(*C.char)(unsafe.Pointer(&w.outbuf)), &outlen)
|
|
|
|
total += int(inlen)
|
|
|
|
data = data[inlen:]
|
|
|
|
if _, err := w.w.Write(w.outbuf[:outlen]); err != nil {
|
|
|
|
return total, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//!-write
|
|
|
|
|
|
|
|
//!+close
|
|
|
|
// Close flushes the compressed data and closes the stream.
|
|
|
|
// It does not close the underlying io.Writer.
|
|
|
|
func (w *writer) Close() error {
|
|
|
|
if w.stream == nil {
|
|
|
|
panic("closed")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
C.BZ2_bzCompressEnd(w.stream)
|
2015-11-30 19:37:52 -08:00
|
|
|
C.bz2free(w.stream)
|
2015-09-29 12:04:07 -07:00
|
|
|
w.stream = nil
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
inlen, outlen := C.uint(0), C.uint(cap(w.outbuf))
|
|
|
|
r := C.bz2compress(w.stream, C.BZ_FINISH, nil, &inlen,
|
|
|
|
(*C.char)(unsafe.Pointer(&w.outbuf)), &outlen)
|
|
|
|
if _, err := w.w.Write(w.outbuf[:outlen]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r == C.BZ_STREAM_END {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//!-close
|