Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1930)

Delta Between Two Patch Sets: openpgp/packet/compressed.go

Issue 12685044: code review 12685044: openpgp: Implement compressed data packets & add suppor...
Left Patch Set: diff -r 1747226a2f43 https://code.google.com/p/go.crypto Created 10 years, 7 months ago
Right Patch Set: diff -r 1747226a2f43 https://code.google.com/p/go.crypto Created 10 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | openpgp/packet/config.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package packet 5 package packet
6 6
7 import ( 7 import (
8 "code.google.com/p/go.crypto/openpgp/errors" 8 "code.google.com/p/go.crypto/openpgp/errors"
9 "compress/bzip2" 9 "compress/bzip2"
10 "compress/flate" 10 "compress/flate"
(...skipping 11 matching lines...) Expand all
22 const ( 22 const (
23 NoCompression = flate.NoCompression 23 NoCompression = flate.NoCompression
24 BestSpeed = flate.BestSpeed 24 BestSpeed = flate.BestSpeed
25 BestCompression = flate.BestCompression 25 BestCompression = flate.BestCompression
26 DefaultCompression = flate.DefaultCompression 26 DefaultCompression = flate.DefaultCompression
27 ) 27 )
28 28
29 // CompressionConfig contains compressor configuration settings. 29 // CompressionConfig contains compressor configuration settings.
30 type CompressionConfig struct { 30 type CompressionConfig struct {
31 // Level is the compression level to use. It must be set to 31 // Level is the compression level to use. It must be set to
32 » // between -1 and 9. See the constants above for convenient 32 » // between -1 and 9, with -1 causing the compressor to use the
33 » // common settings for Level. 33 » // default compression level, 0 causing the compressor to use
34 » // no compression and 1 to 9 representing increasing (better,
35 » // slower) compression levels. If Level is less than -1 or
36 » // more then 9, a non-nil error will be returned during
37 » // encryption. See the constants above for convenient common
38 » // settings for Level.
34 Level int 39 Level int
35 } 40 }
36 41
37 func (c *Compressed) parse(r io.Reader) error { 42 func (c *Compressed) parse(r io.Reader) error {
38 var buf [1]byte 43 var buf [1]byte
39 _, err := readFull(r, buf[:]) 44 _, err := readFull(r, buf[:])
40 if err != nil { 45 if err != nil {
41 return err 46 return err
42 } 47 }
43 48
44 switch buf[0] { 49 switch buf[0] {
45 case 1: 50 case 1:
46 c.Body = flate.NewReader(r) 51 c.Body = flate.NewReader(r)
47 case 2: 52 case 2:
48 c.Body, err = zlib.NewReader(r) 53 c.Body, err = zlib.NewReader(r)
49 case 3: 54 case 3:
50 c.Body = bzip2.NewReader(r) 55 c.Body = bzip2.NewReader(r)
51 default: 56 default:
52 err = errors.UnsupportedError("unknown compression algorithm: " + strconv.Itoa(int(buf[0]))) 57 err = errors.UnsupportedError("unknown compression algorithm: " + strconv.Itoa(int(buf[0])))
53 } 58 }
54 59
55 return err 60 return err
56 } 61 }
57 62
58 // compressedWriterCloser represents the serialized compression stream 63 // compressedWriterCloser represents the serialized compression stream
59 // header and the compressor. Its Close() method ensures that both the 64 // header and the compressor. Its Close() method ensures that both the
60 // compressor and serialized stream header are closed. It's Write() 65 // compressor and serialized stream header are closed. It's Write()
agl1 2013/08/15 16:04:24 Another It's -> Its here.
61 // method writes to the compressor. 66 // method writes to the compressor.
62 type compressedWriteCloser struct { 67 type compressedWriteCloser struct {
63 sh io.Closer // Stream Header 68 sh io.Closer // Stream Header
64 c io.WriteCloser // Compressor 69 c io.WriteCloser // Compressor
65 } 70 }
66 71
67 func (cwc compressedWriteCloser) Write(p []byte) (int, error) { 72 func (cwc compressedWriteCloser) Write(p []byte) (int, error) {
68 return cwc.c.Write(p) 73 return cwc.c.Write(p)
69 } 74 }
70 75
71 func (cwc compressedWriteCloser) Close() (err error) { 76 func (cwc compressedWriteCloser) Close() (err error) {
72 err = cwc.c.Close() 77 err = cwc.c.Close()
73 if err != nil { 78 if err != nil {
74 return err 79 return err
75 } 80 }
76 81
77 return cwc.sh.Close() 82 return cwc.sh.Close()
78 } 83 }
79 84
80 // SerializeCompressed serializes a compressed data packet to w and 85 // SerializeCompressed serializes a compressed data packet to w and
81 // returns a WriteCloser to which the literal data packets themselves 86 // returns a WriteCloser to which the literal data packets themselves
82 // can be written and which MUST be closed on completion. level must 87 // can be written and which MUST be closed on completion. If cc is
83 // be between -1 and 9, with -1 causing the compressor to use the 88 // nil, sensible defaults will be used to configure the compression
84 // default compression level, 0 causing the compressor to use no 89 // algorithm.
85 // compression and 1 to 9 representing increasing (better, slower)
86 // compression levels.
87 func SerializeCompressed(w io.WriteCloser, algo CompressionAlgo, cc *Compression Config) (literaldata io.WriteCloser, err error) { 90 func SerializeCompressed(w io.WriteCloser, algo CompressionAlgo, cc *Compression Config) (literaldata io.WriteCloser, err error) {
88 compressed, err := serializeStreamHeader(w, packetTypeCompressed) 91 compressed, err := serializeStreamHeader(w, packetTypeCompressed)
89 if err != nil { 92 if err != nil {
90 return 93 return
91 } 94 }
92 95
93 _, err = compressed.Write([]byte{uint8(algo)}) 96 _, err = compressed.Write([]byte{uint8(algo)})
94 if err != nil { 97 if err != nil {
95 return 98 return
96 } 99 }
(...skipping 14 matching lines...) Expand all
111 err = errors.UnsupportedError("Unsupported compression algorithm : " + s) 114 err = errors.UnsupportedError("Unsupported compression algorithm : " + s)
112 } 115 }
113 if err != nil { 116 if err != nil {
114 return 117 return
115 } 118 }
116 119
117 literaldata = compressedWriteCloser{compressed, compressor} 120 literaldata = compressedWriteCloser{compressed, compressor}
118 121
119 return 122 return
120 } 123 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b