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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Entry compression methods.
//!
//! This module provides functions for compressing individual entries,
//! including solid and non-solid compression modes, and BCJ2 filter handling.
use std::io::{Read, Seek, Write};
use crate::{ArchivePath, Error, Result};
use super::options::EntryMeta;
use super::{Bcj2FolderInfo, PendingEntry, SolidBufferEntry, Writer};
impl<W: Write + Seek> Writer<W> {
/// Compresses an entry in non-solid mode.
pub(crate) fn compress_entry_non_solid(
&mut self,
archive_path: ArchivePath,
source: &mut dyn Read,
meta: EntryMeta,
) -> Result<()> {
// Read all data and compute CRC
let mut data = Vec::new();
source.read_to_end(&mut data).map_err(Error::Io)?;
// Check if BCJ2 filter is active - route to dedicated method
if self.options.filter.is_bcj2() {
return self.compress_entry_bcj2(archive_path, &data, meta);
}
let crc = crc32fast::hash(&data);
let uncompressed_size = data.len() as u64;
// Add entry (always, even for empty files)
let entry = PendingEntry {
path: archive_path,
meta,
uncompressed_size,
};
self.entries.push(entry);
// Empty files don't get a folder/stream - they're marked as EmptyStream/EmptyFile
// in the header. Only non-empty files need compression and stream tracking.
if data.is_empty() {
return Ok(());
}
// Process data through filter -> compress -> encrypt pipeline
// 4 cases:
// 1. No filter, no encryption -> 1-coder folder
// 2. Filter only -> 2-coder folder (filter + codec)
// 3. Encryption only -> 2-coder folder (AES + codec)
// 4. Filter + encryption -> 3-coder folder (AES + codec + filter)
#[cfg(feature = "aes")]
let (output_data, filter_info, encryption_info) = if self.options.is_data_encrypted() {
let (encrypted, filter_info, enc_info) =
self.filter_compress_and_encrypt_data(&data)?;
(encrypted, filter_info, Some(enc_info))
} else {
let (compressed, filter_info) = self.filter_and_compress_data(&data)?;
(compressed, filter_info, None)
};
#[cfg(not(feature = "aes"))]
let (output_data, filter_info, encryption_info) = {
let (compressed, filter_info) = self.filter_and_compress_data(&data)?;
(compressed, filter_info, Option::<()>::None)
};
let packed_size = output_data.len() as u64;
// Write compressed (and possibly encrypted) data
self.sink.write_all(&output_data).map_err(Error::Io)?;
self.compressed_bytes += packed_size;
// Track stream info (only for non-empty files)
self.stream_info.pack_sizes.push(packed_size);
self.stream_info.unpack_sizes.push(uncompressed_size);
self.stream_info.crcs.push(crc);
// Track encryption info for header writing
#[cfg(feature = "aes")]
self.stream_info.encryption_info.push(encryption_info);
// Track filter info for header writing
self.stream_info.filter_info.push(filter_info);
// Track that this is not a BCJ2 folder
self.stream_info.bcj2_folder_info.push(None);
// Suppress unused variable warning when aes feature is disabled
#[cfg(not(feature = "aes"))]
let _ = encryption_info;
// Track that this is a non-solid folder (1 stream per folder)
self.stream_info.num_unpack_streams_per_folder.push(1);
Ok(())
}
/// Compresses an entry using BCJ2 4-stream filter.
///
/// BCJ2 splits x86 code into 4 streams for improved compression:
/// - Stream 0: Main code
/// - Stream 1: CALL destinations (big-endian)
/// - Stream 2: JMP destinations (big-endian)
/// - Stream 3: Range-coded selector bits
pub(crate) fn compress_entry_bcj2(
&mut self,
archive_path: ArchivePath,
data: &[u8],
meta: EntryMeta,
) -> Result<()> {
use crate::codec::bcj2::bcj2_encode;
let crc = crc32fast::hash(data);
let uncompressed_size = data.len() as u64;
// Add entry
let entry = PendingEntry {
path: archive_path,
meta,
uncompressed_size,
};
self.entries.push(entry);
// Empty files don't get a folder/stream
if data.is_empty() {
return Ok(());
}
// Encode with BCJ2 - produces 4 streams
let streams = bcj2_encode(data);
// Write all 4 streams sequentially to output
self.sink.write_all(&streams.main).map_err(Error::Io)?;
self.sink.write_all(&streams.call).map_err(Error::Io)?;
self.sink.write_all(&streams.jump).map_err(Error::Io)?;
self.sink.write_all(&streams.range).map_err(Error::Io)?;
let total_packed = streams.total_size() as u64;
self.compressed_bytes += total_packed;
// Track BCJ2 folder info
let bcj2_info = Bcj2FolderInfo {
pack_sizes: [
streams.main.len() as u64,
streams.call.len() as u64,
streams.jump.len() as u64,
streams.range.len() as u64,
],
};
// For BCJ2, we don't use pack_sizes (handled separately)
// Store unpack_size and CRC
self.stream_info.unpack_sizes.push(uncompressed_size);
self.stream_info.crcs.push(crc);
// Track filter info as None (BCJ2 handled separately)
self.stream_info.filter_info.push(None);
// Track BCJ2 folder info
self.stream_info.bcj2_folder_info.push(Some(bcj2_info));
// Track encryption info (BCJ2 + encryption not supported yet)
#[cfg(feature = "aes")]
self.stream_info.encryption_info.push(None);
// Track that this is a non-solid folder (1 stream per folder)
self.stream_info.num_unpack_streams_per_folder.push(1);
Ok(())
}
/// Buffers an entry for solid compression.
pub(crate) fn buffer_entry_solid(
&mut self,
archive_path: ArchivePath,
source: &mut dyn Read,
meta: EntryMeta,
) -> Result<()> {
// Read all data and compute CRC
let mut data = Vec::new();
source.read_to_end(&mut data).map_err(Error::Io)?;
let crc = crc32fast::hash(&data);
let data_size = data.len() as u64;
// Buffer the entry
self.solid_buffer_size += data_size;
self.solid_buffer.push(SolidBufferEntry {
path: archive_path,
data,
meta,
crc,
});
// Check if buffer should be flushed
let size_exceeded = self
.options
.solid
.block_size
.is_some_and(|limit| self.solid_buffer_size >= limit);
let count_exceeded = self
.options
.solid
.files_per_block
.is_some_and(|limit| self.solid_buffer.len() >= limit);
if size_exceeded || count_exceeded {
self.flush_solid_buffer()?;
}
Ok(())
}
/// Flushes the solid buffer, compressing all buffered entries as one block.
pub(crate) fn flush_solid_buffer(&mut self) -> Result<()> {
if self.solid_buffer.is_empty() {
return Ok(());
}
// Concatenate all entry data (only non-empty entries have data streams)
let total_uncompressed: u64 = self.solid_buffer.iter().map(|e| e.data.len() as u64).sum();
let mut combined = Vec::with_capacity(total_uncompressed as usize);
// Collect sizes and CRCs for substreams (only non-empty entries)
// Empty entries (size=0) are marked as EmptyStream and don't have data streams
let mut sizes = Vec::new();
let mut crcs = Vec::new();
let mut num_streams = 0u64;
for entry in &self.solid_buffer {
if !entry.data.is_empty() {
combined.extend_from_slice(&entry.data);
sizes.push(entry.data.len() as u64);
crcs.push(entry.crc);
num_streams += 1;
}
// Empty entries are handled via EmptyStream/EmptyFile in FilesInfo
}
// Process data through filter -> compress -> encrypt pipeline
#[cfg(feature = "aes")]
let (output_data, filter_info, encryption_info) = if self.options.is_data_encrypted() {
let (encrypted, filter_info, enc_info) =
self.filter_compress_and_encrypt_data(&combined)?;
(encrypted, filter_info, Some(enc_info))
} else {
let (compressed, filter_info) = self.filter_and_compress_data(&combined)?;
(compressed, filter_info, None)
};
#[cfg(not(feature = "aes"))]
let (output_data, filter_info, encryption_info) = {
let (compressed, filter_info) = self.filter_and_compress_data(&combined)?;
(compressed, filter_info, Option::<()>::None)
};
let packed_size = output_data.len() as u64;
// Write compressed (and possibly encrypted) data
self.sink.write_all(&output_data).map_err(Error::Io)?;
self.compressed_bytes += packed_size;
// Record ONE folder with streams for non-empty entries only
self.stream_info.pack_sizes.push(packed_size);
self.stream_info.unpack_sizes.push(total_uncompressed);
// Track encryption info for header writing
#[cfg(feature = "aes")]
self.stream_info.encryption_info.push(encryption_info);
// Track filter info for header writing
self.stream_info.filter_info.push(filter_info);
// Track that this is not a BCJ2 folder
self.stream_info.bcj2_folder_info.push(None);
// Suppress unused variable warning when aes feature is disabled
#[cfg(not(feature = "aes"))]
let _ = encryption_info;
// Record number of streams in this folder (only non-empty entries)
self.stream_info
.num_unpack_streams_per_folder
.push(num_streams);
// For solid blocks with multiple streams, folder CRC is not used (substream CRCs are).
// For solid blocks with exactly 1 stream, use folder CRC directly (no SubStreamsInfo needed).
if num_streams == 1 {
// Single non-empty file: use folder CRC, no substreams needed
self.stream_info
.crcs
.push(crcs.first().copied().unwrap_or(0));
// Don't add to substream_sizes/crcs - not needed for single stream
} else {
// Multiple non-empty files: use substream CRCs
self.stream_info.crcs.push(0);
self.stream_info.substream_sizes.extend_from_slice(&sizes);
self.stream_info.substream_crcs.extend_from_slice(&crcs);
}
// Create entries for all buffered files
for entry in self.solid_buffer.drain(..) {
let uncompressed_size = entry.data.len() as u64;
self.entries.push(PendingEntry {
path: entry.path,
meta: entry.meta,
uncompressed_size,
});
}
self.solid_buffer_size = 0;
Ok(())
}
}