...

Source file src/github.com/lestrrat-go/jwx/jwe/compress.go

Documentation: github.com/lestrrat-go/jwx/jwe

     1  package jwe
     2  
     3  import (
     4  	"bytes"
     5  	"compress/flate"
     6  	"io/ioutil"
     7  
     8  	"github.com/lestrrat-go/jwx/internal/pool"
     9  	"github.com/lestrrat-go/jwx/jwa"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func uncompress(plaintext []byte) ([]byte, error) {
    14  	return ioutil.ReadAll(flate.NewReader(bytes.NewReader(plaintext)))
    15  }
    16  
    17  func compress(plaintext []byte, alg jwa.CompressionAlgorithm) ([]byte, error) {
    18  	if alg == jwa.NoCompress {
    19  		return plaintext, nil
    20  	}
    21  
    22  	buf := pool.GetBytesBuffer()
    23  	defer pool.ReleaseBytesBuffer(buf)
    24  
    25  	w, _ := flate.NewWriter(buf, 1)
    26  	in := plaintext
    27  	for len(in) > 0 {
    28  		n, err := w.Write(in)
    29  		if err != nil {
    30  			return nil, errors.Wrap(err, `failed to write to compression writer`)
    31  		}
    32  		in = in[n:]
    33  	}
    34  	if err := w.Close(); err != nil {
    35  		return nil, errors.Wrap(err, "failed to close compression writer")
    36  	}
    37  
    38  	ret := make([]byte, buf.Len())
    39  	copy(ret, buf.Bytes())
    40  	return ret, nil
    41  }
    42  

View as plain text