Skip to content

Commit

Permalink
Add alloc feature and use it in encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z committed Mar 9, 2024
1 parent e890e18 commit 8eee457
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ serde = { version = "1.0", features = ["derive"], optional = true }

[features]
default = ["std", "full-decoder", "formatter"]
std = []
alloc = []
std = ["alloc"]
full-decoder = []
formatter = ["std", "full-decoder"]
encoder = ["full-decoder"]
encoder = ["alloc", "full-decoder"]
serialization = ["serde", "bitflags/serde"]
nolibc = []

Expand Down Expand Up @@ -56,11 +57,11 @@ required-features = ["full-decoder"]

[[example]]
name = "encode_manually"
required-features = ["encoder"]
required-features = ["encoder", "formatter"]

[[example]]
name = "encode_macro"
required-features = ["encoder"]
required-features = ["encoder", "formatter"]

[[example]]
name = "decoded_to_encoder_req"
Expand Down
6 changes: 5 additions & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{ffi, *};
use alloc::{vec, vec::Vec};
use core::{
mem::{self, ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut},
Expand Down Expand Up @@ -102,6 +103,7 @@ macro_rules! zeroed {
///
/// ```
/// # use zydis::*;
/// # #[cfg(feature = "formatter")] {
/// let decoder = Decoder::new64();
/// let add = b"\x83\x05\x45\x23\x01\x00\x11";
///
Expand All @@ -119,6 +121,7 @@ macro_rules! zeroed {
/// // Decode & format it again for demonstration purposes.
/// let redec: FullInstruction = decoder.decode_first(&reencoded).unwrap().unwrap();
/// assert_eq!(redec.to_string(), "sub dword ptr [rip+0x12345], 0x22");
/// # }
/// ```
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -295,6 +298,7 @@ impl EncoderRequest {
}

/// Encodes the instruction into a new buffer.
#[cfg(feature = "alloc")]
pub fn encode(&self) -> Result<Vec<u8>> {
let mut out = vec![0; MAX_INSTRUCTION_LENGTH];
let length = self.encode_into(&mut out[..])?;
Expand Down Expand Up @@ -712,7 +716,7 @@ macro_rules! insn32 {
}}
}

#[cfg(test)]
#[cfg(all(test, feature = "formatter"))]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use core::{
fmt,
mem::{self, MaybeUninit},
ffi::{c_void, CStr},
ptr,
};

use crate::decoder::{Instruction, OperandArrayVec};
use core::ffi::{c_void, CStr};

use super::{
enums::*,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
//! - [Decode, change and re-encode instructions][`EncoderRequest`]
//! - [Encode new instructions from scratch][`EncoderRequest`]
#[cfg(feature = "alloc")]
extern crate alloc;

#[macro_use]
mod status;
mod decoder;
Expand Down

0 comments on commit 8eee457

Please sign in to comment.