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
//! The global (to a given thread) context. Used to set global settings which are used internally by several functions.
//! # Examples
//! ```
//! use pwn::{context, I386};
//! use pwn::Bits::ThirtyTwo;
//! context::set_arch(I386);
//! assert_eq!(context::get_arch(), I386);
//! assert_eq!(context::get_bits(), ThirtyTwo)
//! ```
//! # Warning
//! `context` is local to each thread. Context values may need to be re-set when using multi-threaded code.
#![allow(dead_code)]

use crate::context::Bits::{SixtyFour, ThirtyTwo};
use crate::context::Endianness::Little;

use std::cell::RefCell;

/// The word endianness of a given [`Arch`]
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(missing_docs)]
pub enum Endianness {
    Little,
    Big,
}

/// The word size of a given [`Arch`]
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
#[allow(missing_docs)]
pub enum Bits {
    Eight = 8,
    Sixteen = 16,
    ThirtyTwo = 32,
    SixtyFour = 64,
}

/// An architecture, identified by its endianness and word size
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(missing_docs)]
pub struct Arch {
    pub endian: Endianness,
    pub bits: Bits,
}

/// The 64-bit version of x86
pub const AMD64: Arch = Arch {
    endian: Little,
    bits: SixtyFour,
};

/// The 32-bit version of x86
pub const I386: Arch = Arch {
    endian: Little,
    bits: ThirtyTwo,
};

/// The current context, used by most functions for runtime
/// behaviour modification
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(missing_docs)]
pub struct Context {
    arch: Arch,
}

impl Default for Context {
    fn default() -> Self {
        Self { arch: I386 }
    }
}

thread_local! {
    /** The default `Context`.
    * Arch: [`I386`],
    * Log Level: [`Info`]
     **/
    static CONTEXT: RefCell<Context> = Default::default();
}

// Setters
/// Set the context's architecture
pub fn set_arch(a: Arch) {
    CONTEXT.with(|c| c.borrow_mut().arch = a)
}
/// Set the context's endianess
pub fn set_endianess(e: Endianness) {
    CONTEXT.with(|c| c.borrow_mut().arch.endian = e)
}
/// Set the context's word size
pub fn set_bits(b: Bits) {
    CONTEXT.with(|c| c.borrow_mut().arch.bits = b)
}
// Getters
/// Get the context's architecture
pub fn get_arch() -> Arch {
    CONTEXT.with(|c| c.borrow().arch)
}
/// Get the context's endianess
pub fn get_endianess() -> Endianness {
    CONTEXT.with(|c| c.borrow().arch.endian)
}
/// Get the context's word size
pub fn get_bits() -> Bits {
    CONTEXT.with(|c| c.borrow().arch.bits)
}