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
// /!\ This file is not used in the project /!\
// First Idea to implément the compression system onto the packet system
// After some internal debate I decided to not implement encryption and compression onto the packet system
// use super::{CompressionState, Packet, PacketError, PacketResult};
// use crate::{
// protocol::types::{VarInt, WriteToBytes},
// ProtocolWrite,
// };
// use bytes::BytesMut;
// use tracing::{debug, error};
// use std::io::{Read, Write};
// /// Trait pour la compression/décompression des paquets
// pub trait PacketCompression {
// /// Compresse les données du paquet si nécessaire
// fn compress(&self) -> PacketResult<BytesMut>;
// /// Décompresse les données du paquet
// fn decompress(&self) -> PacketResult<BytesMut>;
// /// Détermine si les données doivent être compressées
// fn should_compress(&self) -> bool;
// }
// impl PacketCompression for Packet {
// fn compress(&self) -> PacketResult<BytesMut> {
// match self.compression {
// CompressionState::Disabled => {
// debug!("Compression disabled, returning raw data");
// Ok(self.data.clone())
// }
// CompressionState::Enabled { threshold } => {
// // Prepare full packet data
// let mut uncompressed_packet = Vec::new();
// VarInt(self.id)
// .write_to(&mut uncompressed_packet)
// .map_err(PacketError::Io)?;
// uncompressed_packet.extend_from_slice(&self.data);
// let uncompressed_size = uncompressed_packet.len();
// debug!(
// "Preparing compression: size={}, threshold={}, packet_id=0x{:x}",
// uncompressed_size, threshold, self.id
// );
// if uncompressed_size > MAX_UNCOMPRESSED_SIZE {
// return Err(PacketError::compression("Packet too large"));
// }
// let mut output = BytesMut::new();
// if uncompressed_size < threshold as usize {
// // Send uncompressed with Data Length = 0
// debug!("Below threshold, sending uncompressed");
// VarInt(0).write_to_bytes(&mut output)?;
// output.extend_from_slice(&uncompressed_packet);
// } else {
// debug!("Above threshold, compressing data");
// let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
// encoder.write_all(&uncompressed_packet)?;
// let compressed = encoder.finish()?;
// VarInt(uncompressed_size as i32).write_to_bytes(&mut output)?;
// // Write compressed payload
// output.extend_from_slice(&compressed);
// debug!(
// "Compressed: original={}, compressed={}, ratio={:.2}",
// uncompressed_size,
// compressed.len(),
// compressed.len() as f32 / uncompressed_size as f32
// );
// }
// Ok(output)
// }
// }
// }
// fn decompress(&self) -> PacketResult<BytesMut> {
// match self.compression {
// CompressionState::Disabled => Ok(self.data.clone()),
// CompressionState::Enabled { threshold } => {
// // Read Data Length
// let mut uncompressed_packet = Vec::new();
// VarInt(self.id)
// .write_to(&mut uncompressed_packet)
// .map_err(PacketError::Io)?;
// uncompressed_packet.extend_from_slice(&self.data);
// let uncompressed_size = uncompressed_packet.len();
// debug!(
// "Decompressing packet: declared length: {}, threshold: {}",
// uncompressed_size, threshold
// );
// if uncompressed_size == 0 || uncompressed_size < threshold as usize {
// // Uncompressed packet - return raw data after Data Length
// let data = &self.data;
// let mut output = BytesMut::with_capacity(data.len());
// output.extend_from_slice(data);
// debug!("Uncompressed packet: size={}", data.len());
// return Ok(output);
// }
// // Verify size requirements for compressed packets
// if uncompressed_size as usize > MAX_UNCOMPRESSED_SIZE {
// return Err(PacketError::compression("Invalid declared length"));
// }
// if uncompressed_size < threshold as usize {
// return Err(PacketError::compression(
// "Compressed packet smaller than threshold",
// ));
// }
// // Decompress
// let compressed = &self.data;
// debug!("Compressed data size: {}", compressed.len());
// let mut decoder = ZlibDecoder::new(std::io::Cursor::new(&compressed[..]));
// let mut decompressed = Vec::with_capacity(uncompressed_size);
// if let Err(e) = decoder.read_to_end(&mut decompressed) {
// error!("Decompression error: {}", e);
// return Err(PacketError::compression(format!(
// "Decompression failed: {}",
// e
// )));
// }
// // Verify decompressed length
// if decompressed.len() != uncompressed_size {
// error!(
// "Length mismatch: expected={}, got={}",
// uncompressed_size,
// decompressed.len()
// );
// return Err(PacketError::compression("Length mismatch"));
// }
// let mut output = BytesMut::with_capacity(decompressed.len());
// output.extend_from_slice(&decompressed);
// Ok(output)
// }
// }
// }
// fn should_compress(&self) -> bool {
// match self.compression {
// CompressionState::Disabled => false,
// CompressionState::Enabled { threshold } => {
// threshold >= 0 && self.data.len() >= threshold as usize
// }
// }
// }
// }
// #[cfg(test)]
// mod tests {
// use crate::ProtocolRead;
// use super::*;
// #[test]
// fn test_compression_disabled() {
// let mut packet = Packet::new(0x00);
// packet.data.extend_from_slice(b"test data");
// let compressed = packet.compress().unwrap();
// assert_eq!(&compressed[..], b"test data");
// let decompressed = packet.decompress().unwrap();
// assert_eq!(&decompressed[..], b"test data");
// }
// #[test]
// fn test_compression_above_threshold() {
// let mut packet = Packet::new(0x00);
// packet.enable_compression(256);
// // Créer des données qui dépassent le seuil
// let data = vec![1u8; 1000];
// packet.data = BytesMut::from(&data[..]);
// let compressed = packet.compress().unwrap();
// let mut compressed_packet = packet.clone();
// compressed_packet.data = compressed;
// let decompressed = compressed_packet.decompress().unwrap();
// // Vérifier que l'ID du paquet et les données sont préservés
// let mut cursor = std::io::Cursor::new(&decompressed[..]);
// let (VarInt(id), _) = VarInt::read_from(&mut cursor).unwrap();
// assert_eq!(id, packet.id);
// let remaining_data = &decompressed[cursor.position() as usize..];
// assert_eq!(remaining_data, &data[..]);
// }
// #[test]
// fn test_compression_below_threshold() {
// let mut packet = Packet::new(0x00);
// packet.enable_compression(256);
// let data = vec![1u8; 100]; // En dessous du seuil
// packet.data = BytesMut::from(&data[..]);
// let compressed = packet.compress().unwrap();
// assert_eq!(compressed[0], 0); // VarInt(0) indiquant pas de compression
// let mut compressed_packet = packet.clone();
// compressed_packet.data = compressed;
// let decompressed = compressed_packet.decompress().unwrap();
// assert_eq!(&decompressed[..], &packet.data[..]);
// }
// #[test]
// #[ignore]
// // TODO: fix test
// fn test_invalid_compressed_length() {
// let mut packet_to_compress = Packet::new(0x00);
// packet_to_compress.enable_compression(0);
// let mut data = Vec::new();
// // 8388609 = 0x800001 = 0b100000000000000000001
// // Proper 3-byte VarInt encoding:
// data.push(0x81); // 0b10000001: LSB 1 + continuation
// data.push(0x86); // 0b10000000: middle byte 0 + continuation
// data.push(0x40); // 0b01000000: MSB 1, no continuation
// packet_to_compress.data = BytesMut::from(&data[..]);
// let compressed_packet = packet_to_compress.compress();
// let data = compressed_packet.unwrap();
// let mut packet_to_decompress = Packet::new(0x00);
// packet_to_decompress.enable_compression(0);
// packet_to_decompress.data = BytesMut::from(&data[..]);
// let result = packet_to_decompress.decompress();
// println!("Result: {:?}", result);
// assert!(result.is_err());
// }
// #[test]
// fn test_max_uncompressed_size() {
// let mut packet = Packet::new(0x00);
// packet.enable_compression(0);
// // Test avec une taille exactement égale à la limite
// let mut temp = Vec::new();
// VarInt(MAX_UNCOMPRESSED_SIZE as i32)
// .write_to(&mut temp)
// .expect_err("Should fail");
// // Vérifier que l'erreur est bien due à la taille du VarInt
// packet.data = BytesMut::from(&temp[..]);
// assert!(packet.decompress().is_err());
// }
// }