disktest_lib/
generator.rs

1// -*- coding: utf-8 -*-
2//
3// disktest - Storage tester
4//
5// Copyright 2020-2024 Michael Büsch <m@bues.ch>
6//
7// Licensed under the Apache License version 2.0
8// or the MIT license, at your option.
9// SPDX-License-Identifier: Apache-2.0 OR MIT
10//
11
12mod chacha;
13mod crc;
14
15use crate::util::prettybytes;
16use anyhow as ah;
17
18pub use crate::generator::chacha::GeneratorChaCha12;
19pub use crate::generator::chacha::GeneratorChaCha20;
20pub use crate::generator::chacha::GeneratorChaCha8;
21pub use crate::generator::crc::GeneratorCrc;
22
23pub trait NextRandom {
24    /// Get the size of the next() output with count = 1, in bytes.
25    fn get_base_size(&self) -> usize;
26
27    /// Generate the next chunks.
28    /// buf: Buffer to hold all chunks.
29    /// count: The number of chunks to return.
30    /// Returns all chunks concatenated in a Vec.
31    fn next(&mut self, buf: &mut [u8], count: usize);
32
33    /// Seek the algorithm to the specified offset.
34    fn seek(&mut self, byte_offset: u64) -> ah::Result<()> {
35        if byte_offset == 0 {
36            Ok(())
37        } else {
38            Err(ah::format_err!(
39                "The selected random number generator \
40                 does not support seeking to byte offset {}.",
41                prettybytes(byte_offset, true, true, true)
42            ))
43        }
44    }
45}
46
47// vim: ts=4 sw=4 expandtab