compress_roundtrip.mx raw

   1  // gzip and zlib round trips, including the trailer check.
   2  //
   3  // Both had to be repaired for this to pass:
   4  //
   5  //   - gzip created its flate.Writer lazily inside Write, i.e. allocated it in
   6  //     that frame and stored the pointer through the receiver. Close then
   7  //     flushed a released object: the file was a 10-byte header and an 8-byte
   8  //     trailer with NO deflate payload (18 bytes), and the runtime printed its
   9  //     "pointer into a transient frame arena" note. The compressor is created in
  10  //     the constructor now.
  11  //   - both readers handed the raw source to inflate unless it was already a
  12  //     flate.Reader, and Moxie's inflate reads in bulk: it drained the trailer
  13  //     (CRC32+size for gzip, Adler-32 for zlib) and the trailer read that
  14  //     followed started from the wrong bytes. A *bytes.Buffer source therefore
  15  //     decoded the data correctly and then failed with "invalid checksum".
  16  //     Both readers always wrap the source in a bufio.Reader now, which keeps
  17  //     the trailer owed to the reader.
  18  //
  19  // usage: compress_roundtrip    exit 0 when every shape round trips
  20  package main
  21  
  22  import (
  23  	"bytes"
  24  	"compress/gzip"
  25  	"compress/zlib"
  26  	"io"
  27  	"os"
  28  )
  29  
  30  func gzipCheck(tag []byte, src []byte, want []byte) (ok bool) {
  31  	r, err := gzip.NewReader(bytes.NewReader(src))
  32  	if err != nil {
  33  		println("gzip", tag, "NewReader:", err.Error())
  34  		return false
  35  	}
  36  	got, rerr := io.ReadAll(r)
  37  	if rerr != nil {
  38  		println("gzip", tag, "read:", rerr.Error())
  39  		return false
  40  	}
  41  	if !bytes.Equal(got, want) {
  42  		println("gzip", tag, "payload mismatch")
  43  		return false
  44  	}
  45  	return true
  46  }
  47  
  48  func main() {
  49  	// Termination: a batch program must still be stoppable, so the entry
  50  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  51  	select {
  52  	case <-shutdown:
  53  		return
  54  	default:
  55  	}
  56  
  57  	base := []byte("hello compression, this is a round trip across a buffer boundary\n")
  58  	fail := int32(0)
  59  
  60  	// gzip: write, then read back from a fresh bytes.Reader, a second buffer,
  61  	// and the same buffer the writer used (the shape that exposed the trailer).
  62  	var b1 bytes.Buffer
  63  	w1 := gzip.NewWriter(&b1)
  64  	if _, err := w1.Write(base); err != nil {
  65  		println("gzip write:", err.Error())
  66  		os.Exit(1)
  67  	}
  68  	if err := w1.Close(); err != nil {
  69  		println("gzip close:", err.Error())
  70  		os.Exit(1)
  71  	}
  72  	println("gzip bytes", int32(b1.Len()).String())
  73  	if b1.Len() <= 18 {
  74  		println("gzip: no deflate payload")
  75  		fail++
  76  	}
  77  	if !gzipCheck([]byte("fresh"), b1.Bytes(), base) {
  78  		fail++
  79  	}
  80  	var b2 bytes.Buffer
  81  	b2.Write(b1.Bytes())
  82  	if !gzipCheck([]byte("buffer"), b2.Bytes(), base) {
  83  		fail++
  84  	}
  85  
  86  	// zlib: same trailer discipline, Adler-32 instead of CRC32+size.
  87  	var z1 bytes.Buffer
  88  	zw := zlib.NewWriter(&z1)
  89  	zw.Write(base)
  90  	if err := zw.Close(); err != nil {
  91  		println("zlib close:", err.Error())
  92  		os.Exit(1)
  93  	}
  94  	zr, zerr := zlib.NewReader(bytes.NewReader(z1.Bytes()))
  95  	if zerr != nil {
  96  		println("zlib NewReader:", zerr.Error())
  97  		fail++
  98  	} else {
  99  		zgot, zrerr := io.ReadAll(zr)
 100  		if zrerr != nil {
 101  			println("zlib read:", zrerr.Error())
 102  			fail++
 103  		} else if !bytes.Equal(zgot, base) {
 104  			println("zlib payload mismatch")
 105  			fail++
 106  		} else {
 107  			println("zlib bytes", int32(z1.Len()).String())
 108  		}
 109  	}
 110  
 111  	if fail != 0 {
 112  		println("compress_roundtrip:", fail.String(), "case(s) failed")
 113  		os.Exit(1)
 114  	}
 115  	println("compress_roundtrip ok")
 116  }
 117