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-09-29 12:04:07 -07:00
|
|
|
|
|
|
|
//!+
|
|
|
|
/* This file is gopl.io/ch13/bzip/bzip2.c, */
|
|
|
|
/* a simple wrapper for libbzip2 suitable for cgo. */
|
|
|
|
#include <bzlib.h>
|
|
|
|
|
|
|
|
int bz2compress(bz_stream *s, int action,
|
|
|
|
char *in, unsigned *inlen, char *out, unsigned *outlen) {
|
|
|
|
s->next_in = in;
|
|
|
|
s->avail_in = *inlen;
|
|
|
|
s->next_out = out;
|
|
|
|
s->avail_out = *outlen;
|
|
|
|
int r = BZ2_bzCompress(s, action);
|
|
|
|
*inlen -= s->avail_in;
|
|
|
|
*outlen -= s->avail_out;
|
2015-11-30 19:37:52 -08:00
|
|
|
|
|
|
|
/* "C code may store a Go pointer in C memory subject to rule 2:
|
|
|
|
* it must stop storing the pointer before it returns to Go." */
|
|
|
|
s->next_in = NULL;
|
|
|
|
s->next_out = NULL;
|
|
|
|
|
2015-09-29 12:04:07 -07:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
//!-
|