libafl/inputs/
bytes.rs

1//! The `BytesInput` is the "normal" input, a map of bytes, that can be sent directly to the client
2//! (As opposed to other, more abstract, inputs, like an Grammar-Based AST Input)
3
4use alloc::{
5    borrow::ToOwned,
6    rc::Rc,
7    vec::{self, Vec},
8};
9use core::cell::RefCell;
10
11use libafl_bolts::{HasLen, ownedref::OwnedSlice};
12
13use super::ValueInput;
14use crate::inputs::{HasMutatorBytes, HasTargetBytes, ResizableMutator};
15
16/// A bytes input is the basic input
17pub type BytesInput = ValueInput<Vec<u8>>;
18
19/// Rc Ref-cell from Input
20impl From<BytesInput> for Rc<RefCell<BytesInput>> {
21    fn from(input: BytesInput) -> Self {
22        Rc::new(RefCell::new(input))
23    }
24}
25
26impl HasMutatorBytes for BytesInput {
27    fn mutator_bytes(&self) -> &[u8] {
28        self.as_ref()
29    }
30
31    fn mutator_bytes_mut(&mut self) -> &mut [u8] {
32        self.as_mut()
33    }
34}
35
36impl ResizableMutator<u8> for BytesInput {
37    fn resize(&mut self, new_len: usize, value: u8) {
38        self.as_mut().resize(new_len, value);
39    }
40
41    fn extend<'a, I: IntoIterator<Item = &'a u8>>(&mut self, iter: I) {
42        <Vec<u8> as Extend<I::Item>>::extend(self.as_mut(), iter);
43    }
44
45    fn splice<R, I>(&mut self, range: R, replace_with: I) -> vec::Splice<'_, I::IntoIter>
46    where
47        R: core::ops::RangeBounds<usize>,
48        I: IntoIterator<Item = u8>,
49    {
50        self.as_mut().splice(range, replace_with)
51    }
52
53    fn drain<R>(&mut self, range: R) -> vec::Drain<'_, u8>
54    where
55        R: core::ops::RangeBounds<usize>,
56    {
57        self.as_mut().drain(range)
58    }
59}
60
61impl HasTargetBytes for BytesInput {
62    #[inline]
63    fn target_bytes(&self) -> OwnedSlice<u8> {
64        OwnedSlice::from(self.as_ref())
65    }
66}
67
68impl HasLen for BytesInput {
69    fn len(&self) -> usize {
70        self.as_ref().len()
71    }
72}
73
74impl From<&[u8]> for BytesInput {
75    fn from(bytes: &[u8]) -> Self {
76        Self::new(bytes.to_owned())
77    }
78}
79
80impl From<BytesInput> for Vec<u8> {
81    fn from(value: BytesInput) -> Vec<u8> {
82        value.into_inner()
83    }
84}