You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compressed.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "compress/bzip2"
  7. "compress/flate"
  8. "compress/zlib"
  9. "golang.org/x/crypto/openpgp/errors"
  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. const (
  19. NoCompression = flate.NoCompression
  20. BestSpeed = flate.BestSpeed
  21. BestCompression = flate.BestCompression
  22. DefaultCompression = flate.DefaultCompression
  23. )
  24. // CompressionConfig contains compressor configuration settings.
  25. type CompressionConfig struct {
  26. // Level is the compression level to use. It must be set to
  27. // between -1 and 9, with -1 causing the compressor to use the
  28. // default compression level, 0 causing the compressor to use
  29. // no compression and 1 to 9 representing increasing (better,
  30. // slower) compression levels. If Level is less than -1 or
  31. // more then 9, a non-nil error will be returned during
  32. // encryption. See the constants above for convenient common
  33. // settings for Level.
  34. Level int
  35. }
  36. func (c *Compressed) parse(r io.Reader) error {
  37. var buf [1]byte
  38. _, err := readFull(r, buf[:])
  39. if err != nil {
  40. return err
  41. }
  42. switch buf[0] {
  43. case 1:
  44. c.Body = flate.NewReader(r)
  45. case 2:
  46. c.Body, err = zlib.NewReader(r)
  47. case 3:
  48. c.Body = bzip2.NewReader(r)
  49. default:
  50. err = errors.UnsupportedError("unknown compression algorithm: " + strconv.Itoa(int(buf[0])))
  51. }
  52. return err
  53. }
  54. // compressedWriterCloser represents the serialized compression stream
  55. // header and the compressor. Its Close() method ensures that both the
  56. // compressor and serialized stream header are closed. Its Write()
  57. // method writes to the compressor.
  58. type compressedWriteCloser struct {
  59. sh io.Closer // Stream Header
  60. c io.WriteCloser // Compressor
  61. }
  62. func (cwc compressedWriteCloser) Write(p []byte) (int, error) {
  63. return cwc.c.Write(p)
  64. }
  65. func (cwc compressedWriteCloser) Close() (err error) {
  66. err = cwc.c.Close()
  67. if err != nil {
  68. return err
  69. }
  70. return cwc.sh.Close()
  71. }
  72. // SerializeCompressed serializes a compressed data packet to w and
  73. // returns a WriteCloser to which the literal data packets themselves
  74. // can be written and which MUST be closed on completion. If cc is
  75. // nil, sensible defaults will be used to configure the compression
  76. // algorithm.
  77. func SerializeCompressed(w io.WriteCloser, algo CompressionAlgo, cc *CompressionConfig) (literaldata io.WriteCloser, err error) {
  78. compressed, err := serializeStreamHeader(w, packetTypeCompressed)
  79. if err != nil {
  80. return
  81. }
  82. _, err = compressed.Write([]byte{uint8(algo)})
  83. if err != nil {
  84. return
  85. }
  86. level := DefaultCompression
  87. if cc != nil {
  88. level = cc.Level
  89. }
  90. var compressor io.WriteCloser
  91. switch algo {
  92. case CompressionZIP:
  93. compressor, err = flate.NewWriter(compressed, level)
  94. case CompressionZLIB:
  95. compressor, err = zlib.NewWriterLevel(compressed, level)
  96. default:
  97. s := strconv.Itoa(int(algo))
  98. err = errors.UnsupportedError("Unsupported compression algorithm: " + s)
  99. }
  100. if err != nil {
  101. return
  102. }
  103. literaldata = compressedWriteCloser{compressed, compressor}
  104. return
  105. }