pwn_helper/
bytes.rs

1pub use pwn_helper_macros::bytes;
2pub use pwn_helper_macros::pack;
3
4pub fn concat_bytes(l: &[u8], r: &[u8]) -> Vec<u8> {
5    let mut l = l.to_vec();
6    for x in r {
7        l.push(*x)
8    }
9    l
10}
11
12pub fn concat_vec(mut l: Vec<u8>, r: &[u8]) -> Vec<u8> {
13    for x in r {
14        l.push(*x)
15    }
16    l
17}
18
19/// Pads the end of l to length `len` with `c`
20pub fn ljust(l: &[u8], c: u8, len: usize) -> Vec<u8> {
21    let cur_len = l.len();
22    let mut l = l.to_vec();
23
24    if cur_len < len {
25        for _ in 0..len - cur_len {
26            l.push(c)
27        }
28    }
29
30    l
31}
32
33/// Pads the beginning of l to length `len` with `c`
34pub fn rjust(l: &[u8], c: u8, len: usize) -> Vec<u8> {
35    let cur_len = l.len();
36
37    if cur_len < len {
38        let mut out = Vec::with_capacity(len);
39
40        for _ in 0..len - cur_len {
41            out.push(c);
42        }
43
44        for x in l {
45            out.push(*x);
46        }
47
48        out
49    } else {
50        l.to_vec()
51    }
52}
53
54/// Copies the array `l` into the output `len` times
55pub fn multiply_bytes(l: &[u8], len: usize) -> Vec<u8> {
56    let mut out = Vec::with_capacity(len * l.len());
57
58    for _ in 0..len {
59        for x in l {
60            out.push(*x)
61        }
62    }
63
64    out
65}
66
67#[cfg(test)]
68mod tests {
69    use pwn_helper_macros::{bytes, pack};
70
71    use crate::bytes::{ljust, rjust};
72
73    #[test]
74    fn test_ljust() {
75        let justified = ljust(b"Test", 0, 0x20);
76
77        assert_eq!(&justified, b"Test\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
78    }
79
80    #[test]
81    fn test_rjust() {
82        let justified = rjust(b"Test", 0, 0x20);
83
84        assert_eq!(&justified, b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Test");
85    }
86
87    #[test]
88    #[allow(clippy::eq_op)]
89    fn test_mul_bytestr() {
90        assert_eq!(bytes!(b"OwO" * 2), b"OwOOwO");
91    }
92
93    #[test]
94    #[allow(clippy::eq_op)]
95    fn test_add_byte() {
96        assert_eq!(bytes!(b'O' + b'U'), b"OU");
97    }
98
99    #[test]
100    #[allow(clippy::eq_op)]
101    fn test_add_bytestr() {
102        assert_eq!(bytes!(b"OwO" + b"UwU"), b"OwOUwU");
103    }
104
105    #[test]
106    #[allow(clippy::eq_op)]
107    fn test_add_bytestr_byte() {
108        assert_eq!(bytes!(b"OwOUw" + b'U'), b"OwOUwU");
109    }
110
111    #[test]
112    #[allow(clippy::eq_op)]
113    fn test_add_byte_bytestr() {
114        assert_eq!(bytes!(b'O' + b"wOUwU"), b"OwOUwU");
115    }
116
117    #[test]
118    #[allow(clippy::eq_op)]
119    fn test_pack_32() {
120        assert_eq!(pack!(0xcafebabe, 32), b"\xbe\xba\xfe\xca");
121    }
122
123    #[test]
124    #[allow(clippy::eq_op)]
125    fn test_pack_64() {
126        assert_eq!(pack!(0xcafebabe, 64), b"\xbe\xba\xfe\xca\x00\x00\x00\x00");
127    }
128}