Debian Codegraph @

openpgp/packet/compressed.go

raw
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
5 package packet
6
7 import (
8 "compress/bzip2"
9 "compress/flate"
10 "compress/zlib"
11 "io"
12 "strconv"
13
14 "github.com/ProtonMail/go-crypto/openpgp/errors"
15 )
16
17 // Compressed represents a compressed OpenPGP packet. The decompressed contents
18 // will contain more OpenPGP packets. See RFC 4880, section 5.6.
19 type Compressed struct {
20 Body io.Reader
21 }
22
23 const (
24 NoCompression = flate.NoCompression
25 BestSpeed = flate.BestSpeed
26 BestCompression = flate.BestCompression
27 DefaultCompression = flate.DefaultCompression
28 )
29
30 // CompressionConfig contains compressor configuration settings.
31 type CompressionConfig struct {
32 // Level is the compression level to use. It must be set to
33 // between -1 and 9, with -1 causing the compressor to use the
34 // default compression level, 0 causing the compressor to use
35 // no compression and 1 to 9 representing increasing (better,
36 // slower) compression levels. If Level is less than -1 or
37 // more then 9, a non-nil error will be returned during
38 // encryption. See the constants above for convenient common
39 // settings for Level.
40 Level int
41 }
42
43 // decompressionReader ensures that the whole compression packet is read.
44 type decompressionReader struct {
45 compressed io.Reader
46 decompressed io.ReadCloser
47 readAll bool
48 }
49
50 func newDecompressionReader(r io.Reader, decompressor io.ReadCloser) *decompressionReader {
51 return &decompressionReader{
52 compressed: r,
53 decompressed: decompressor,
54 }
55 }
56
57 func (dr *decompressionReader) Read(data []byte) (n int, err error) {
58 if dr.readAll {
59 return 0, io.EOF
60 }
61 n, err = dr.decompressed.Read(data)
62 if err == io.EOF {
63 dr.readAll = true
64 // Close the decompressor.
65 if errDec := dr.decompressed.Close(); errDec != nil {
66 return n, errDec
67 }
68 // Consume all remaining data from the compressed packet.
69 consumeAll(dr.compressed)
70 }
71 return n, err
72 }
73
74 func (c *Compressed) parse(r io.Reader) error {
75 var buf [1]byte
76 _, err := readFull(r, buf[:])
77 if err != nil {
78 return err
79 }
80
81 switch buf[0] {
82 case 0:
83 c.Body = r
84 case 1:
85 c.Body = newDecompressionReader(r, flate.NewReader(r))
86 case 2:
87 decompressor, err := zlib.NewReader(r)
88 if err != nil {
89 return err
90 }
91 c.Body = newDecompressionReader(r, decompressor)
92 case 3:
93 c.Body = newDecompressionReader(r, io.NopCloser(bzip2.NewReader(r)))
94 default:
95 err = errors.UnsupportedError("unknown compression algorithm: " + strconv.Itoa(int(buf[0])))
96 }
97
98 return err
99 }
100
101 // LimitedBodyReader wraps the provided body reader with a limiter that restricts
102 // the number of bytes read to the specified limit.
103 // If limit is nil, the reader is unbounded.
104 func (c *Compressed) LimitedBodyReader(limit *int64) io.Reader {
105 if limit == nil {
106 return c.Body
107 }
108 return &LimitReader{R: c.Body, N: *limit}
109 }
110
111 // compressedWriterCloser represents the serialized compression stream
112 // header and the compressor. Its Close() method ensures that both the
113 // compressor and serialized stream header are closed. Its Write()
114 // method writes to the compressor.
115 type compressedWriteCloser struct {
116 sh io.Closer // Stream Header
117 c io.WriteCloser // Compressor
118 }
119
120 func (cwc compressedWriteCloser) Write(p []byte) (int, error) {
121 return cwc.c.Write(p)
122 }
123
124 func (cwc compressedWriteCloser) Close() (err error) {
125 err = cwc.c.Close()
126 if err != nil {
127 return err
128 }
129
130 return cwc.sh.Close()
131 }
132
133 // SerializeCompressed serializes a compressed data packet to w and
134 // returns a WriteCloser to which the literal data packets themselves
135 // can be written and which MUST be closed on completion. If cc is
136 // nil, sensible defaults will be used to configure the compression
137 // algorithm.
138 func SerializeCompressed(w io.WriteCloser, algo CompressionAlgo, cc *CompressionConfig) (literaldata io.WriteCloser, err error) {
139 compressed, err := serializeStreamHeader(w, packetTypeCompressed)
140 if err != nil {
141 return
142 }
143
144 _, err = compressed.Write([]byte{uint8(algo)})
145 if err != nil {
146 return
147 }
148
149 level := DefaultCompression
150 if cc != nil {
151 level = cc.Level
152 }
153
154 var compressor io.WriteCloser
155 switch algo {
156 case CompressionZIP:
157 compressor, err = flate.NewWriter(compressed, level)
158 case CompressionZLIB:
159 compressor, err = zlib.NewWriterLevel(compressed, level)
160 default:
161 s := strconv.Itoa(int(algo))
162 err = errors.UnsupportedError("Unsupported compression algorithm: " + s)
163 }
164 if err != nil {
165 return
166 }
167
168 literaldata = compressedWriteCloser{compressed, compressor}
169
170 return
171 }
172
173 // LimitReader is an io.Reader that fails with MessageToLarge if read bytes exceed N.
174 type LimitReader struct {
175 R io.Reader // underlying reader
176 N int64 // max bytes allowed
177 }
178
179 func (l *LimitReader) Read(p []byte) (int, error) {
180 if l.N <= 0 {
181 return 0, errors.ErrMessageTooLarge
182 }
183
184 n, err := l.R.Read(p)
185 l.N -= int64(n)
186
187 if err == nil && l.N <= 0 {
188 err = errors.ErrMessageTooLarge
189 }
190
191 return n, err
192 }