/
compressed_data.js
209 lines (178 loc) · 6.32 KB
/
compressed_data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3.0 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import { Deflate } from '@openpgp/pako/lib/deflate';
import { Inflate } from '@openpgp/pako/lib/inflate';
import { Z_SYNC_FLUSH, Z_FINISH } from '@openpgp/pako/lib/zlib/constants';
import { decode as BunzipDecode } from '@openpgp/seek-bzip';
import * as stream from '@openpgp/web-stream-tools';
import enums from '../enums';
import util from '../util';
import defaultConfig from '../config';
import LiteralDataPacket from './literal_data';
import OnePassSignaturePacket from './one_pass_signature';
import SignaturePacket from './signature';
import PacketList from './packetlist';
// A Compressed Data packet can contain the following packet types
const allowedPackets = /*#__PURE__*/ util.constructAllowedPackets([
LiteralDataPacket,
OnePassSignaturePacket,
SignaturePacket
]);
/**
* Implementation of the Compressed Data Packet (Tag 8)
*
* {@link https://tools.ietf.org/html/rfc4880#section-5.6|RFC4880 5.6}:
* The Compressed Data packet contains compressed data. Typically,
* this packet is found as the contents of an encrypted packet, or following
* a Signature or One-Pass Signature packet, and contains a literal data packet.
*/
class CompressedDataPacket {
static get tag() {
return enums.packet.compressedData;
}
/**
* @param {Object} [config] - Full configuration, defaults to openpgp.config
*/
constructor(config = defaultConfig) {
/**
* List of packets
* @type {PacketList}
*/
this.packets = null;
/**
* Compression algorithm
* @type {compression}
*/
this.algorithm = enums.read(enums.compression, config.preferredCompressionAlgorithm);
/**
* Compressed packet data
* @type {Uint8Array | ReadableStream<Uint8Array>}
*/
this.compressed = null;
/**
* zip/zlib compression level, between 1 and 9
*/
this.deflateLevel = config.deflateLevel;
}
/**
* Parsing function for the packet.
* @param {Uint8Array | ReadableStream<Uint8Array>} bytes - Payload of a tag 8 packet
*/
async read(bytes) {
await stream.parse(bytes, async reader => {
// One octet that gives the algorithm used to compress the packet.
this.algorithm = enums.read(enums.compression, await reader.readByte());
// Compressed data, which makes up the remainder of the packet.
this.compressed = reader.remainder();
await this.decompress();
});
}
/**
* Return the compressed packet.
* @returns {Uint8Array | ReadableStream<Uint8Array>} Binary compressed packet.
*/
write() {
if (this.compressed === null) {
this.compress();
}
return util.concat([new Uint8Array([enums.write(enums.compression, this.algorithm)]), this.compressed]);
}
/**
* Decompression method for decompressing the compressed data
* read by read_packet
*/
async decompress() {
if (!decompress_fns[this.algorithm]) {
throw new Error(this.algorithm + ' decompression not supported');
}
this.packets = new PacketList();
await this.packets.read(decompress_fns[this.algorithm](this.compressed), allowedPackets);
}
/**
* Compress the packet data (member decompressedData)
*/
compress() {
if (!compress_fns[this.algorithm]) {
throw new Error(this.algorithm + ' compression not supported');
}
this.compressed = compress_fns[this.algorithm](this.packets.write(), this.deflateLevel);
}
}
export default CompressedDataPacket;
//////////////////////////
// //
// Helper functions //
// //
//////////////////////////
const nodeZlib = util.getNodeZlib();
function uncompressed(data) {
return data;
}
function node_zlib(func, create, options = {}) {
return function (data) {
if (!util.isStream(data) || stream.isArrayStream(data)) {
return stream.fromAsync(() => stream.readToEnd(data).then(data => {
return new Promise((resolve, reject) => {
func(data, options, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
}));
}
return stream.nodeToWeb(stream.webToNode(data).pipe(create(options)));
};
}
function pako_zlib(constructor, options = {}) {
return function(data) {
const obj = new constructor(options);
return stream.transform(data, value => {
if (value.length) {
obj.push(value, Z_SYNC_FLUSH);
return obj.result;
}
}, () => {
if (constructor === Deflate) {
obj.push([], Z_FINISH);
return obj.result;
}
});
};
}
function bzip2(func) {
return function(data) {
return stream.fromAsync(async () => func(await stream.readToEnd(data)));
};
}
const compress_fns = nodeZlib ? {
zip: /*#__PURE__*/ (compressed, level) => node_zlib(nodeZlib.deflateRaw, nodeZlib.createDeflateRaw, { level })(compressed),
zlib: /*#__PURE__*/ (compressed, level) => node_zlib(nodeZlib.deflate, nodeZlib.createDeflate, { level })(compressed)
} : {
zip: /*#__PURE__*/ (compressed, level) => pako_zlib(Deflate, { raw: true, level })(compressed),
zlib: /*#__PURE__*/ (compressed, level) => pako_zlib(Deflate, { level })(compressed)
};
const decompress_fns = nodeZlib ? {
uncompressed: uncompressed,
zip: /*#__PURE__*/ node_zlib(nodeZlib.inflateRaw, nodeZlib.createInflateRaw),
zlib: /*#__PURE__*/ node_zlib(nodeZlib.inflate, nodeZlib.createInflate),
bzip2: /*#__PURE__*/ bzip2(BunzipDecode)
} : {
uncompressed: uncompressed,
zip: /*#__PURE__*/ pako_zlib(Inflate, { raw: true }),
zlib: /*#__PURE__*/ pako_zlib(Inflate),
bzip2: /*#__PURE__*/ bzip2(BunzipDecode)
};