|
1 | 1 | #[cfg(feature = "parse")]
|
2 | 2 | use {
|
3 |
| - flate2::bufread::ZlibDecoder, goblin::elf::Elf, goblin::error::Result, serde_json::Value, |
4 |
| - std::io::Read, |
| 3 | + flate2::bufread::ZlibDecoder, goblin::elf::Elf, serde_json::Value, std::fmt, std::io::Read, |
| 4 | + std::str::FromStr, |
5 | 5 | };
|
6 | 6 |
|
7 |
| -#[cfg(feature = "parse")] |
8 |
| -pub fn parse_idl_from_program_binary(buffer: &[u8]) -> Result<Value> { |
| 7 | +#[derive(Clone, Debug)] |
| 8 | +pub enum IdlType { |
| 9 | + Anchor, |
| 10 | + Kinobi, |
| 11 | +} |
| 12 | + |
| 13 | +impl fmt::Display for IdlType { |
| 14 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 15 | + match self { |
| 16 | + IdlType::Anchor => write!(f, "Anchor"), |
| 17 | + IdlType::Kinobi => write!(f, "Kinobi"), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl FromStr for IdlType { |
| 23 | + type Err = &'static str; |
| 24 | + |
| 25 | + fn from_str(s: &str) -> Result<Self, &'static str> { |
| 26 | + match s.to_lowercase().as_str() { |
| 27 | + "anchor" => Ok(IdlType::Anchor), |
| 28 | + "kinobi" => Ok(IdlType::Kinobi), |
| 29 | + _ => Err("Invalid IDL type"), |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn get_section_name(idl_type: IdlType) -> String { |
| 35 | + match idl_type { |
| 36 | + IdlType::Anchor => ".solana.idl".to_string(), |
| 37 | + IdlType::Kinobi => ".kinobi.idl".to_string(), |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +pub fn parse_idl_from_program_binary( |
| 42 | + buffer: &[u8], |
| 43 | + idl_type: IdlType, |
| 44 | +) -> goblin::error::Result<Value> { |
9 | 45 | let elf = Elf::parse(buffer)?;
|
10 | 46 |
|
| 47 | + let section_name = get_section_name(idl_type); |
| 48 | + |
11 | 49 | // Iterate over section headers and print information
|
12 | 50 | for sh in &elf.section_headers {
|
13 | 51 | let name = elf.shdr_strtab.get_at(sh.sh_name).unwrap_or("<invalid>");
|
14 |
| - if name == ".solana.idl" { |
| 52 | + if name == section_name { |
15 | 53 | // Get offset of .solana.idl section data
|
16 | 54 | let offset = sh.sh_offset as usize;
|
17 | 55 |
|
|
0 commit comments