atlas_program/
entrypoint_deprecated.rs1#![allow(clippy::arithmetic_side_effects)]
11
12extern crate alloc;
13use {
14    crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey},
15    alloc::vec::Vec,
16    std::{
17        mem::size_of,
18        result::Result as ResultGeneric,
19        slice::{from_raw_parts, from_raw_parts_mut},
20    },
21};
22
23pub type ProgramResult = ResultGeneric<(), ProgramError>;
24
25pub type ProcessInstruction =
31    fn(program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult;
32
33pub const SUCCESS: u64 = 0;
35
36#[macro_export]
43macro_rules! entrypoint_deprecated {
44    ($process_instruction:ident) => {
45        #[no_mangle]
47        pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
48            let (program_id, accounts, instruction_data) =
49                unsafe { $crate::entrypoint_deprecated::deserialize(input) };
50            match $process_instruction(&program_id, &accounts, &instruction_data) {
51                Ok(()) => $crate::entrypoint_deprecated::SUCCESS,
52                Err(error) => error.into(),
53            }
54        }
55    };
56}
57
58#[allow(clippy::type_complexity)]
62pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a>>, &'a [u8]) {
63    let mut offset: usize = 0;
64
65    #[allow(clippy::cast_ptr_alignment)]
68    let num_accounts = *(input.add(offset) as *const u64) as usize;
69    offset += size_of::<u64>();
70
71    let mut accounts = Vec::with_capacity(num_accounts);
74    for _ in 0..num_accounts {
75        let dup_info = *(input.add(offset) as *const u8);
76        offset += size_of::<u8>();
77        if dup_info == u8::MAX {
78            #[allow(clippy::cast_ptr_alignment)]
79            let is_signer = *(input.add(offset) as *const u8) != 0;
80            offset += size_of::<u8>();
81
82            #[allow(clippy::cast_ptr_alignment)]
83            let is_writable = *(input.add(offset) as *const u8) != 0;
84            offset += size_of::<u8>();
85
86            let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
87            offset += size_of::<Pubkey>();
88
89            #[allow(clippy::cast_ptr_alignment)]
90            let lamports = &mut *(input.add(offset) as *mut u64);
91            offset += size_of::<u64>();
92
93            #[allow(clippy::cast_ptr_alignment)]
94            let data_len = *(input.add(offset) as *const u64) as usize;
95            offset += size_of::<u64>();
96
97            let data = from_raw_parts_mut(input.add(offset), data_len);
98            offset += data_len;
99
100            let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
101            offset += size_of::<Pubkey>();
102
103            #[allow(clippy::cast_ptr_alignment)]
104            let executable = *(input.add(offset) as *const u8) != 0;
105            offset += size_of::<u8>();
106
107            offset += size_of::<u64>();
109
110            accounts.push(AccountInfo::new(
111                key,
112                is_signer,
113                is_writable,
114                lamports,
115                data,
116                owner,
117                executable,
118            ));
119        } else {
120            accounts.push(accounts[dup_info as usize].clone());
122        }
123    }
124
125    #[allow(clippy::cast_ptr_alignment)]
128    let instruction_data_len = *(input.add(offset) as *const u64) as usize;
129    offset += size_of::<u64>();
130
131    let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
132    offset += instruction_data_len;
133
134    let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
137
138    (program_id, accounts, instruction_data)
139}