sfbinpack 0.6.5

Library to read Stockfish Binpacks
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use std::io::{self};
use std::io::{Read, Seek};
use thiserror::Error;

use crate::common::{
    binpack_error::BinpackError, compressed_training_file_reader::CompressedTrainingDataFileReader,
    entry::PackedTrainingDataEntry, entry::TrainingDataEntry,
};

use super::move_score_list_reader::PackedMoveScoreListReader;

const SUGGESTED_CHUNK_SIZE: usize = 8192;

#[derive(Debug, Error)]
pub enum CompressedReaderError {
    #[error("IO error: {0}")]
    Io(#[from] io::Error),
    #[error("Invalid data format: {0}")]
    InvalidFormat(String),
    #[error("End of file reached")]
    EndOfFile,
    #[error("Binpack error: {0}")]
    BinpackError(#[from] BinpackError),
}

type Result<T> = std::result::Result<T, CompressedReaderError>;

/// Read the next raw binpack chunk payload into `buffer`.
///
/// Returns `Ok(false)` when the stream is already at EOF. Otherwise this reads
/// the next chunk header and payload, resizes `buffer` to the chunk size, and
/// overwrites it with the chunk bytes before returning `Ok(true)`.
///
/// This helper does not keep any reader state beyond the current stream
/// position, so it can be called repeatedly on the same file handle as long as
/// the handle remains positioned at the start of the next chunk.
pub fn read_chunk_into<T: Read + Seek>(file: &mut T, buffer: &mut Vec<u8>) -> Result<bool> {
    let mut reader = CompressedTrainingDataFileReader::new(file)?;

    if !reader.has_next_chunk() {
        return Ok(false);
    }

    reader.read_next_chunk_into(buffer)?;

    Ok(true)
}

pub fn parse_chunk(chunk: &[u8]) -> Vec<TrainingDataEntry> {
    let mut reader = ChunkReader::default();
    let mut entries = Vec::new();

    while reader.has_next(chunk) {
        entries.push(reader.next(chunk));
    }

    entries
}

/// Reads Stockfish binpacks and returns a TrainingDataEntry
/// for each encoded entry.
#[derive(Debug)]
pub struct CompressedTrainingDataEntryReader<T: Read + Seek> {
    chunk: Vec<u8>,
    chunk_reader: ChunkReader,
    input_file: Option<CompressedTrainingDataFileReader<T>>,
    is_end: bool,
}

#[derive(Debug, Default)]
pub struct ChunkReader {
    movelist_reader: Option<PackedMoveScoreListReader>,
    offset: usize,
    is_end: bool,
}

/*
Search for EBNF: ..., to find the implementation.

File         = Block*
Block        = ChunkHeader Chain*
ChunkHeader  = Magic ChunkSize
Magic        = "BINP"
ChunkSize    = UINT32LE               (* 4 bytes, little endian *)

Chain        = Stem Count MoveText
Stem         = Position Move Score PlyResult Rule50
Count        = UINT16BE               (* 2 bytes, big endian *)
MoveText     = MoveScore*

(* Stem components - total 32 bytes *)
Position     = CompressedPosition     (* 24 bytes *)
Move         = CompressedMove         (* 2 bytes *)
Score        = INT16BE                (* 2 bytes, big endian, signed *)
PlyResult    = UINT8                  (* 2 byte, big endian unsigned *)
Rule50       = UINT16BE               (* 2 bytes, big endian *)

(* MoveText components *)
MoveScore    = EncodedMove EncodedScore

(* Encoded components *)
EncodedMove  = VARLEN_UINT            (* Variable length encoding *)
EncodedScore = VARLEN_INT             (* Variable length encoding *)
*/

// EBNF: File
impl<T: Read + Seek> CompressedTrainingDataEntryReader<T> {
    /// Create a new CompressedTrainingDataEntryReader,
    /// reading from the file at the given path.
    /// # Examples
    ///
    /// ```
    /// use std::fs::File;
    /// use sfbinpack::CompressedTrainingDataEntryReader;
    ///
    /// let file = File::options().read(true).write(false).create(false).open("test/ep1.binpack").unwrap();
    /// let mut reader = CompressedTrainingDataEntryReader::new(file).unwrap();
    ///
    /// while reader.has_next() {
    ///     let entry = reader.next();
    /// }
    /// ```
    pub fn new(file: T) -> Result<Self> {
        let chunk = Vec::with_capacity(SUGGESTED_CHUNK_SIZE);

        let mut reader = Self {
            chunk,
            chunk_reader: ChunkReader::default(),
            input_file: Some(CompressedTrainingDataFileReader::new(file)?),
            is_end: false,
        };

        if !reader.load_next_chunk()? {
            reader.is_end = true;
            return Err(CompressedReaderError::EndOfFile);
        }

        Ok(reader)
    }

    pub fn into_inner(&mut self) -> io::Result<T> {
        self.input_file.take().unwrap().into_inner()
    }

    /// Get how much of the file has been read so far
    pub fn read_bytes(&self) -> u64 {
        self.input_file.as_ref().unwrap().read_bytes()
    }

    /// Read the next raw binpack chunk payload into `buffer`.
    ///
    /// Returns `Ok(false)` when no more chunks are available. Otherwise this
    /// reads the next chunk header and payload, resizes `buffer` to the chunk
    /// size, and overwrites it with the chunk bytes before returning `Ok(true)`.
    pub fn read_next_chunk_into(&mut self, buffer: &mut Vec<u8>) -> Result<bool> {
        if !self.input_file.as_mut().unwrap().has_next_chunk() {
            return Ok(false);
        }

        self.input_file
            .as_mut()
            .unwrap()
            .read_next_chunk_into(buffer)?;

        Ok(true)
    }

    /// Parse all entries from a single chunk payload.
    pub fn parse_chunk(chunk: &[u8]) -> Vec<TrainingDataEntry> {
        parse_chunk(chunk)
    }

    /// Check if there are more TrainingDataEntry to read
    pub fn has_next(&self) -> bool {
        !self.is_end
    }

    /// Check if the next entry is a continuation of the last returned entry from next()
    pub fn is_next_entry_continuation(&self) -> bool {
        if let Some(ref reader) = self.chunk_reader.movelist_reader {
            return reader.has_next();
        }

        false
    }

    /// Get the next TrainingDataEntry
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> TrainingDataEntry {
        let entry = self.chunk_reader.next(&self.chunk);

        if !self.chunk_reader.has_next(&self.chunk) {
            self.fetch_next_chunk_if_needed();
        }

        entry
    }

    // EBNF: BLOCK
    fn fetch_next_chunk_if_needed(&mut self) {
        if self.chunk_reader.has_next(&self.chunk) {
            return;
        }

        if self.load_next_chunk().unwrap() {
            return;
        }

        self.is_end = true;
    }

    fn load_next_chunk(&mut self) -> Result<bool> {
        if !self.input_file.as_mut().unwrap().has_next_chunk() {
            return Ok(false);
        }

        self.input_file
            .as_mut()
            .unwrap()
            .read_next_chunk_into(&mut self.chunk)?;

        self.chunk_reader = ChunkReader::default();

        Ok(true)
    }
}

impl ChunkReader {
    /// Check whether another entry can be read from this chunk.
    pub fn has_next(&self, chunk: &[u8]) -> bool {
        if self
            .movelist_reader
            .as_ref()
            .is_some_and(|reader| reader.has_next())
        {
            return true;
        }

        !self.is_end && self.offset + PackedTrainingDataEntry::byte_size() + 2 <= chunk.len()
    }

    /// Read the next entry from this chunk.
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self, chunk: &[u8]) -> TrainingDataEntry {
        if let Some(ref mut reader) = self.movelist_reader {
            let entry = reader.next_entry(&chunk[self.offset..]);

            if !reader.has_next() {
                self.offset += reader.num_read_bytes();
                self.movelist_reader = None;
                self.finish_if_at_end(chunk);
            }

            return entry;
        }

        // We don't have a movelist reader, so we first need to extract the "stem" information

        // EBNF: Stem
        let entry = self.read_entry(chunk);

        // EBNF: Count
        let num_plies = self.read_plies(chunk);

        if num_plies > 0 {
            // EBNF: MoveText
            self.movelist_reader = Some(PackedMoveScoreListReader::new(entry, num_plies));
        } else {
            self.finish_if_at_end(chunk);
        }

        entry
    }

    fn read_entry(&mut self, chunk: &[u8]) -> TrainingDataEntry {
        let size = PackedTrainingDataEntry::byte_size();

        debug_assert!(self.offset + size <= chunk.len());

        let packed = PackedTrainingDataEntry::from_slice(&chunk[self.offset..self.offset + size]);

        self.offset += size;

        packed.unpack_entry()
    }

    fn read_plies(&mut self, chunk: &[u8]) -> u16 {
        let ply = ((chunk[self.offset] as u16) << 8) | (chunk[self.offset + 1] as u16);
        self.offset += 2;
        ply
    }

    fn finish_if_at_end(&mut self, chunk: &[u8]) {
        if self.offset + PackedTrainingDataEntry::byte_size() + 2 > chunk.len() {
            self.is_end = true;
        }
    }
}

impl CompressedTrainingDataEntryReader<io::Cursor<Vec<u8>>> {
    /// Create a reader from an owned byte buffer.
    ///
    /// This is convenient for wasm environments where binpack data is often
    /// already available in memory.
    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
        Self::new(io::Cursor::new(bytes))
    }
}

impl<'a> CompressedTrainingDataEntryReader<io::Cursor<&'a [u8]>> {
    /// Create a reader from a borrowed byte slice.
    pub fn from_slice(bytes: &'a [u8]) -> Result<Self> {
        Self::new(io::Cursor::new(bytes))
    }
}

#[cfg(test)]
mod tests {
    use std::{fs::OpenOptions, io::Cursor};

    use crate::chess::{
        coords::Square,
        piece::Piece,
        position::Position,
        r#move::{Move, MoveType},
    };

    use super::*;

    #[test]
    fn test_reader_simple() {
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(false)
            .append(false)
            .open("./test/ep1.binpack")
            .unwrap();
        let mut reader = CompressedTrainingDataEntryReader::new(file).unwrap();

        let mut entries: Vec<TrainingDataEntry> = Vec::new();

        while reader.has_next() {
            let entry = reader.next();

            entries.push(entry);
        }

        let expected = vec![
            TrainingDataEntry {
                pos: Position::from_fen("1q5b/1r5k/4p2p/1b2P1pN/3p4/6PP/1nP3B1/1Q2B1K1 w - - 0 35")
                    .unwrap(),
                mv: Move::new(
                    Square::new(10),
                    Square::new(26),
                    MoveType::Normal,
                    Piece::none(),
                ),
                score: -201,
                ply: 68,
                result: 0,
            },
            TrainingDataEntry {
                pos: Position::from_fen("1q5b/1r5k/4p2p/1b2P1pN/2Pp4/6PP/1n4B1/1Q2B1K1 b - - 0 35")
                    .unwrap(),
                mv: Move::new(
                    Square::new(27),
                    Square::new(19),
                    MoveType::Normal,
                    Piece::none(),
                ),
                score: 254,
                ply: 69,
                result: 0,
            },
            TrainingDataEntry {
                pos: Position::from_fen(
                    "1q5b/1r5k/4p2p/1b2P1pN/2P5/3p2PP/1n4B1/1Q2B1K1 w - - 0 36",
                )
                .unwrap(),
                mv: Move::new(
                    Square::new(14),
                    Square::new(49),
                    MoveType::Normal,
                    Piece::none(),
                ),
                score: -220,
                ply: 70,
                result: 0,
            },
        ];

        assert_eq!(entries, expected);
    }

    #[test]
    fn test_reader_big_score_diff() {
        let cursor: Cursor<Vec<u8>> = Cursor::new(Vec::from([
            66, 73, 78, 80, 37, 0, 0, 0, 130, 130, 144, 210, 8, 192, 70, 82, 72, 58, 64, 0, 81, 16,
            18, 113, 155, 5, 0, 0, 0, 0, 0, 0, 10, 104, 249, 253, 0, 68, 0, 0, 0, 1, 29, 83, 79,
        ]));

        let mut reader = CompressedTrainingDataEntryReader::new(cursor).unwrap();

        let mut entries: Vec<TrainingDataEntry> = Vec::new();
        while reader.has_next() {
            let entry = reader.next();

            entries.push(entry);
        }

        let expected = vec![
            TrainingDataEntry {
                pos: Position::from_fen("1q5b/1r5k/4p2p/1b2P1pN/3p4/6PP/1nP3B1/1Q2B1K1 w - - 0 35")
                    .unwrap(),
                mv: Move::new(
                    Square::new(10),
                    Square::new(26),
                    MoveType::Normal,
                    Piece::none(),
                ),
                score: -31999,
                ply: 68,
                result: 0,
            },
            TrainingDataEntry {
                pos: Position::from_fen("1q5b/1r5k/4p2p/1b2P1pN/2Pp4/6PP/1n4B1/1Q2B1K1 b - - 0 35")
                    .unwrap(),
                mv: Move::new(
                    Square::new(27),
                    Square::new(19),
                    MoveType::Normal,
                    Piece::none(),
                ),
                score: -1500,
                ply: 69,
                result: 0,
            },
        ];

        assert_eq!(entries, expected);
    }

    #[test]
    fn test_reader_from_bytes() {
        let file = std::fs::read("./test/ep1.binpack").unwrap();
        let mut reader = CompressedTrainingDataEntryReader::from_bytes(file).unwrap();

        let mut num_entries = 0;
        while reader.has_next() {
            let _ = reader.next();
            num_entries += 1;
        }

        assert_eq!(num_entries, 3);
    }

    #[test]
    fn test_chunk_read_and_parse() {
        let first_chunk: Vec<u8> = vec![
            98, 121, 192, 21, 24, 76, 241, 100, 100, 106, 0, 4, 8, 48, 2, 17, 17, 145, 19, 117,
            247, 0, 0, 0, 61, 232, 0, 253, 0, 39, 0, 2, 0, 0,
        ];
        let second_chunk: Vec<u8> = vec![
            98, 121, 192, 21, 24, 76, 241, 100, 100, 106, 0, 4, 8, 48, 2, 17, 17, 145, 19, 117,
            247, 0, 0, 0, 61, 232, 0, 253, 0, 39, 0, 2, 0, 0,
        ];

        let mut file = Vec::new();
        file.extend_from_slice(b"BINP");
        file.extend_from_slice(&(first_chunk.len() as u32).to_le_bytes());
        file.extend_from_slice(&first_chunk);
        file.extend_from_slice(b"BINP");
        file.extend_from_slice(&(second_chunk.len() as u32).to_le_bytes());
        file.extend_from_slice(&second_chunk);

        let mut reader = CompressedTrainingDataEntryReader::from_bytes(file).unwrap();
        let mut chunk = Vec::new();

        assert!(reader.read_next_chunk_into(&mut chunk).unwrap());
        assert_eq!(chunk, second_chunk);

        let entries = parse_chunk(&chunk);

        assert_eq!(entries.len(), 1);
        assert!(!reader.read_next_chunk_into(&mut chunk).unwrap());
    }

    // test case for https://github.com/Disservin/binpack-rust/issues/17
    #[test]
    #[should_panic(expected = "index out of bounds: the len is 0 but the index is 0")]
    fn test_reader_no_moves() {
        // Safe API UB: CompressedTrainingDataEntryReader constructs a BitReader
        // from a raw pointer without tracking length. A crafted chunk with
        // num_plies > 0 but no movetext bytes triggers OOB reads.

        // Valid packed entry bytes from crate tests (32 bytes).
        let entry_bytes: [u8; 32] = [
            98, 121, 192, 21, 24, 76, 241, 100, 100, 106, 0, 4, 8, 48, 2, 17, 17, 145, 19, 117,
            247, 0, 0, 0, 61, 232, 0, 253, 0, 39, 0, 2,
        ];

        // num_plies = 1, but movetext is empty (chunk size == 32 + 2).
        let mut chunk = Vec::new();
        chunk.extend_from_slice(&entry_bytes);
        chunk.extend_from_slice(&1u16.to_be_bytes());

        // File header: "BINP" + chunk_size (LE).
        let mut file = Vec::new();
        file.extend_from_slice(b"BINP");
        file.extend_from_slice(&(chunk.len() as u32).to_le_bytes());
        file.extend_from_slice(&chunk);

        let cursor = Cursor::new(file);
        let mut reader = CompressedTrainingDataEntryReader::new(cursor).unwrap();

        // First next() returns the stem entry and sets movelist_reader.
        let _ = reader.next();
        // Second next() consumes movetext via BitReader and triggers OOB.
        let _ = reader.next();
    }
}