innodb-utils 5.0.0

InnoDB file analysis toolkit
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Tablespace compression detection and decompression.
//!
//! Detects the compression algorithm from FSP flags and provides zlib and LZ4
//! decompression helpers for compressed page data.
//!
//! Supports both MySQL (bits 11-12) and MariaDB flag layouts:
//! - MariaDB full_crc32: compression algo in bits 5-7
//! - MariaDB original: PAGE_COMPRESSION flag at bit 16
//! - MariaDB page-level: algorithm ID embedded per-page at offset 26

use flate2::read::ZlibDecoder;
use std::io::Read;

use crate::innodb::vendor::VendorInfo;

/// Compression algorithm detected or used for a page.
///
/// # Examples
///
/// ```
/// use idb::innodb::compression::CompressionAlgorithm;
///
/// let algo = CompressionAlgorithm::Zlib;
/// assert_eq!(format!("{algo}"), "Zlib");
///
/// let algo = CompressionAlgorithm::None;
/// assert_eq!(format!("{algo}"), "None");
///
/// let algo = CompressionAlgorithm::Lz4;
/// assert_eq!(format!("{algo}"), "LZ4");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionAlgorithm {
    None,
    Zlib,
    Lz4,
    /// MariaDB LZO compression (detection only — not decompressed).
    Lzo,
    /// MariaDB LZMA compression (detection only — not decompressed).
    Lzma,
    /// MariaDB bzip2 compression (detection only — not decompressed).
    Bzip2,
    /// MariaDB Snappy compression (detection only — not decompressed).
    Snappy,
    /// MySQL 8.0.14+ ZSTD compression (decompressed via ruzstd).
    Zstd,
}

/// Detect the compression algorithm from FSP space flags.
///
/// When `vendor_info` is provided:
/// - MariaDB full_crc32: reads compression algo from bits 5-7
/// - MariaDB original: checks bit 16 for PAGE_COMPRESSION (algo is per-page)
/// - MySQL/Percona: reads bits 11-12
///
/// Without vendor info, defaults to MySQL bit layout.
///
/// # Examples
///
/// ```
/// use idb::innodb::compression::{detect_compression, CompressionAlgorithm};
/// use idb::innodb::vendor::{VendorInfo, MariaDbFormat};
///
/// // No compression flags → None
/// assert_eq!(detect_compression(0, None), CompressionAlgorithm::None);
///
/// // MySQL: bit 11 set → Zlib
/// assert_eq!(detect_compression(1 << 11, None), CompressionAlgorithm::Zlib);
///
/// // MySQL: bits 11-12 = 2 → LZ4
/// assert_eq!(detect_compression(2 << 11, None), CompressionAlgorithm::Lz4);
///
/// // MariaDB full_crc32: bits 5-7 = 1 → Zlib
/// let maria = VendorInfo::mariadb(MariaDbFormat::FullCrc32);
/// let flags = 0x10 | (1 << 5); // bit 4 (marker) + algo=1
/// assert_eq!(detect_compression(flags, Some(&maria)), CompressionAlgorithm::Zlib);
/// ```
pub fn detect_compression(
    fsp_flags: u32,
    vendor_info: Option<&VendorInfo>,
) -> CompressionAlgorithm {
    use crate::innodb::constants::*;

    if let Some(vi) = vendor_info {
        if vi.is_full_crc32() {
            // MariaDB full_crc32: compression algo in bits 5-7
            let algo = (fsp_flags & MARIADB_FSP_FLAGS_FCRC32_COMPRESSED_ALGO_MASK) >> 5;
            return mariadb_algo_from_id(algo as u8);
        }
        if vi.vendor == crate::innodb::vendor::InnoDbVendor::MariaDB {
            // MariaDB original: bit 16 indicates page compression is enabled
            // but the algorithm is stored per-page, not in FSP flags
            if fsp_flags & MARIADB_FSP_FLAGS_PAGE_COMPRESSION != 0 {
                // Algorithm is per-page; return Zlib as a default indicator
                // that page compression is enabled. Actual algo is in each page.
                return CompressionAlgorithm::Zlib;
            }
            return CompressionAlgorithm::None;
        }
    }

    // MySQL/Percona: bits 11-12
    let comp_bits = (fsp_flags >> 11) & 0x03;
    match comp_bits {
        1 => CompressionAlgorithm::Zlib,
        2 => CompressionAlgorithm::Lz4,
        3 => CompressionAlgorithm::Zstd,
        _ => CompressionAlgorithm::None,
    }
}

/// Detect the compression algorithm from a MariaDB page-compressed page.
///
/// For page types 34354 (PAGE_COMPRESSED) and 37401 (PAGE_COMPRESSED_ENCRYPTED),
/// the algorithm ID is stored as a u8 at byte offset 26 (FIL_PAGE_FILE_FLUSH_LSN).
///
/// # Examples
///
/// ```
/// use idb::innodb::compression::{detect_mariadb_page_compression, CompressionAlgorithm};
///
/// // Build a minimal page with algorithm ID at byte 26
/// let mut page = vec![0u8; 38];
///
/// // Algorithm ID 2 = LZ4
/// page[26] = 2;
/// assert_eq!(detect_mariadb_page_compression(&page), Some(CompressionAlgorithm::Lz4));
///
/// // Algorithm ID 1 = Zlib
/// page[26] = 1;
/// assert_eq!(detect_mariadb_page_compression(&page), Some(CompressionAlgorithm::Zlib));
///
/// // Too-short buffer returns None
/// let short = vec![0u8; 10];
/// assert_eq!(detect_mariadb_page_compression(&short), None);
/// ```
pub fn detect_mariadb_page_compression(page_data: &[u8]) -> Option<CompressionAlgorithm> {
    if page_data.len() < 27 {
        return None;
    }
    let algo_id = page_data[26];
    Some(mariadb_algo_from_id(algo_id))
}

/// Convert a MariaDB compression algorithm ID to enum.
///
/// IDs from MariaDB `fil_space_t::comp_algo`:
/// 0 = none, 1 = zlib, 2 = lz4, 3 = lzo, 4 = lzma, 5 = bzip2, 6 = snappy
fn mariadb_algo_from_id(id: u8) -> CompressionAlgorithm {
    match id {
        1 => CompressionAlgorithm::Zlib,
        2 => CompressionAlgorithm::Lz4,
        3 => CompressionAlgorithm::Lzo,
        4 => CompressionAlgorithm::Lzma,
        5 => CompressionAlgorithm::Bzip2,
        6 => CompressionAlgorithm::Snappy,
        _ => CompressionAlgorithm::None,
    }
}

/// Decompress zlib-compressed page data.
///
/// Returns the decompressed data, or None if decompression fails.
///
/// # Examples
///
/// ```
/// use idb::innodb::compression::decompress_zlib;
///
/// // Compress some data with flate2, then decompress with decompress_zlib
/// use flate2::write::ZlibEncoder;
/// use flate2::Compression;
/// use std::io::Write;
///
/// let original = b"Hello, InnoDB!";
/// let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
/// encoder.write_all(original).unwrap();
/// let compressed = encoder.finish().unwrap();
///
/// let result = decompress_zlib(&compressed).unwrap();
/// assert_eq!(result, original);
///
/// // Invalid data returns None
/// assert!(decompress_zlib(&[0xFF, 0xFE]).is_none());
/// ```
pub fn decompress_zlib(compressed: &[u8]) -> Option<Vec<u8>> {
    let mut decoder = ZlibDecoder::new(compressed);
    let mut decompressed = Vec::new();
    decoder.read_to_end(&mut decompressed).ok()?;
    Some(decompressed)
}

/// Decompress LZ4-compressed page data.
///
/// `uncompressed_len` is the expected output size (typically the page size).
/// Returns the decompressed data, or None if decompression fails.
///
/// # Examples
///
/// ```
/// use idb::innodb::compression::decompress_lz4;
///
/// let original = b"Hello, LZ4 compression!";
/// let compressed = lz4_flex::compress(original);
///
/// let result = decompress_lz4(&compressed, original.len()).unwrap();
/// assert_eq!(result, original);
/// ```
pub fn decompress_lz4(compressed: &[u8], uncompressed_len: usize) -> Option<Vec<u8>> {
    lz4_flex::decompress(compressed, uncompressed_len).ok()
}

/// Decompress ZSTD-compressed page data.
///
/// Returns the decompressed data, or None if decompression fails.
///
/// # Examples
///
/// ```
/// use idb::innodb::compression::decompress_zstd;
///
/// // Invalid data returns None
/// assert!(decompress_zstd(&[0xFF, 0xFE]).is_none());
/// ```
pub fn decompress_zstd(compressed: &[u8]) -> Option<Vec<u8>> {
    let mut decoder = ruzstd::decoding::StreamingDecoder::new(compressed).ok()?;
    let mut decompressed = Vec::new();
    std::io::Read::read_to_end(&mut decoder, &mut decompressed).ok()?;
    Some(decompressed)
}

/// Check if a page appears to be a hole-punched page.
///
/// Hole-punched pages have their data zeroed out after the compressed content.
/// The FIL header is preserved, and the actual data is followed by trailing zeros.
///
/// # Examples
///
/// ```
/// use idb::innodb::compression::is_hole_punched;
///
/// let page_size = 16384u32;
///
/// // All-zero page is considered hole-punched
/// let zeros = vec![0u8; page_size as usize];
/// assert!(is_hole_punched(&zeros, page_size));
///
/// // Data in the first part but zeros in the last quarter → hole-punched
/// let mut page = vec![0u8; page_size as usize];
/// page[0] = 0xFF;
/// page[100] = 0xAB;
/// assert!(is_hole_punched(&page, page_size));
///
/// // Non-zero byte in the last quarter → not hole-punched
/// page[page_size as usize - 10] = 0x01;
/// assert!(!is_hole_punched(&page, page_size));
/// ```
pub fn is_hole_punched(page_data: &[u8], page_size: u32) -> bool {
    if page_data.len() < page_size as usize {
        return false;
    }

    // A hole-punched page has trailing zeros. Check the last quarter of the page.
    let check_start = (page_size as usize * 3) / 4;
    page_data[check_start..page_size as usize]
        .iter()
        .all(|&b| b == 0)
}

impl std::fmt::Display for CompressionAlgorithm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CompressionAlgorithm::None => write!(f, "None"),
            CompressionAlgorithm::Zlib => write!(f, "Zlib"),
            CompressionAlgorithm::Lz4 => write!(f, "LZ4"),
            CompressionAlgorithm::Lzo => write!(f, "LZO"),
            CompressionAlgorithm::Lzma => write!(f, "LZMA"),
            CompressionAlgorithm::Bzip2 => write!(f, "bzip2"),
            CompressionAlgorithm::Snappy => write!(f, "Snappy"),
            CompressionAlgorithm::Zstd => write!(f, "ZSTD"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::innodb::vendor::MariaDbFormat;

    #[test]
    fn test_detect_compression_mysql() {
        assert_eq!(detect_compression(0, None), CompressionAlgorithm::None);
        assert_eq!(
            detect_compression(1 << 11, None),
            CompressionAlgorithm::Zlib
        );
        assert_eq!(detect_compression(2 << 11, None), CompressionAlgorithm::Lz4);
        assert_eq!(
            detect_compression(3 << 11, None),
            CompressionAlgorithm::Zstd
        );
        // Other bits set shouldn't affect compression detection
        assert_eq!(
            detect_compression(0xFF | (1 << 11), None),
            CompressionAlgorithm::Zlib
        );
    }

    #[test]
    fn test_detect_compression_mariadb_full_crc32() {
        let vendor = VendorInfo::mariadb(MariaDbFormat::FullCrc32);
        // bits 5-7 = 1 (zlib)
        let flags = 0x10 | (1 << 5);
        assert_eq!(
            detect_compression(flags, Some(&vendor)),
            CompressionAlgorithm::Zlib
        );
        // bits 5-7 = 2 (lz4)
        let flags = 0x10 | (2 << 5);
        assert_eq!(
            detect_compression(flags, Some(&vendor)),
            CompressionAlgorithm::Lz4
        );
        // bits 5-7 = 3 (lzo)
        let flags = 0x10 | (3 << 5);
        assert_eq!(
            detect_compression(flags, Some(&vendor)),
            CompressionAlgorithm::Lzo
        );
    }

    #[test]
    fn test_detect_mariadb_page_compression() {
        let mut page = vec![0u8; 38];
        page[26] = 2; // LZ4
        assert_eq!(
            detect_mariadb_page_compression(&page),
            Some(CompressionAlgorithm::Lz4)
        );
        page[26] = 6; // Snappy
        assert_eq!(
            detect_mariadb_page_compression(&page),
            Some(CompressionAlgorithm::Snappy)
        );
    }

    #[test]
    fn test_decompress_zlib() {
        use flate2::write::ZlibEncoder;
        use flate2::Compression;
        use std::io::Write;

        let original = b"Hello, InnoDB compression test data!";
        let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(original).unwrap();
        let compressed = encoder.finish().unwrap();

        let result = decompress_zlib(&compressed).unwrap();
        assert_eq!(result, original);
    }

    #[test]
    fn test_decompress_lz4() {
        let original = b"Hello, LZ4 compression test data for InnoDB!";
        let compressed = lz4_flex::compress_prepend_size(original);
        // lz4_flex::compress_prepend_size adds 4-byte length prefix,
        // but decompress expects just the compressed data with known length
        let result = lz4_flex::decompress(&compressed[4..], original.len());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), original);
    }

    #[test]
    fn test_detect_compression_mysql_zstd() {
        // MySQL 8.0.14+: bits 11-12 = 3 → ZSTD
        assert_eq!(
            detect_compression(3 << 11, None),
            CompressionAlgorithm::Zstd
        );
    }

    #[test]
    fn test_decompress_zstd() {
        // Use ruzstd to compress, then decompress
        let original = b"Hello, ZSTD compression test data for InnoDB!";
        let compressed = ruzstd::encoding::compress_to_vec(
            &original[..],
            ruzstd::encoding::CompressionLevel::Fastest,
        );
        let result = decompress_zstd(&compressed).unwrap();
        assert_eq!(result, original);
    }

    #[test]
    fn test_zstd_display() {
        assert_eq!(format!("{}", CompressionAlgorithm::Zstd), "ZSTD");
    }

    #[test]
    fn test_decompress_zstd_invalid() {
        assert!(decompress_zstd(&[0xFF, 0xFE]).is_none());
    }

    #[test]
    fn test_is_hole_punched() {
        let page_size = 16384u32;
        let mut page = vec![0u8; page_size as usize];
        // All zeros = hole punched
        assert!(is_hole_punched(&page, page_size));

        // Some data in the first part, zeros in the last quarter
        page[0] = 0xFF;
        page[100] = 0xAB;
        assert!(is_hole_punched(&page, page_size));

        // Non-zero byte in the last quarter = not hole punched
        page[page_size as usize - 10] = 0x01;
        assert!(!is_hole_punched(&page, page_size));
    }
}