compressed.go 884 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package packet
  5. import (
  6. "code.google.com/p/go.crypto/openpgp/errors"
  7. "compress/bzip2"
  8. "compress/flate"
  9. "compress/zlib"
  10. "io"
  11. "strconv"
  12. )
  13. // Compressed represents a compressed OpenPGP packet. The decompressed contents
  14. // will contain more OpenPGP packets. See RFC 4880, section 5.6.
  15. type Compressed struct {
  16. Body io.Reader
  17. }
  18. func (c *Compressed) parse(r io.Reader) error {
  19. var buf [1]byte
  20. _, err := readFull(r, buf[:])
  21. if err != nil {
  22. return err
  23. }
  24. switch buf[0] {
  25. case 1:
  26. c.Body = flate.NewReader(r)
  27. case 2:
  28. c.Body, err = zlib.NewReader(r)
  29. case 3:
  30. c.Body = bzip2.NewReader(r)
  31. default:
  32. err = errors.UnsupportedError("unknown compression algorithm: " + strconv.Itoa(int(buf[0])))
  33. }
  34. return err
  35. }