@@ -4,12 +4,13 @@ use std::io::{Read, Seek};
44use byteorder:: { BigEndian , ByteOrder , LittleEndian , ReadBytesExt } ;
55
66use crate :: constants:: * ;
7+ use crate :: error:: AppError ;
78use crate :: header:: * ;
89use crate :: load_commands:: * ;
910use crate :: mach_o:: MachO ;
1011use crate :: memory_utils:: { advance_to_next_load_command, get_file_offset} ;
1112
12- pub fn parse < R : Read + Seek > ( file : & mut R ) -> io :: Result < MachO > {
13+ pub fn parse < R : Read + Seek > ( file : & mut R ) -> Result < MachO , AppError > {
1314 let magic = file. read_u32 :: < BigEndian > ( ) ?;
1415 check_magic_number ( magic) ?;
1516
@@ -33,22 +34,22 @@ pub fn parse<R: Read + Seek>(file: &mut R) -> io::Result<MachO> {
3334 } )
3435}
3536
36- fn check_magic_number ( magic : u32 ) -> io :: Result < ( ) > {
37+ fn check_magic_number ( magic : u32 ) -> Result < ( ) , AppError > {
3738 match magic {
3839 MH_MAGIC | MH_MAGIC_64 | MH_CIGAM | MH_CIGAM_64 => Ok ( ( ) ) ,
39- _ => Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "Invalid Mach-O magic number" ) )
40+ _ => Err ( AppError :: from ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "Invalid Mach-O magic number" ) ) )
4041 }
4142}
4243
43- fn parse_header < R : Read + Seek , E : ByteOrder > ( file : & mut R , magic : u32 ) -> io :: Result < MachHeader > {
44+ fn parse_header < R : Read + Seek , E : ByteOrder > ( file : & mut R , magic : u32 ) -> Result < MachHeader , AppError > {
4445 match magic {
4546 MH_MAGIC | MH_CIGAM => MachHeader32 :: from_file :: < R , E > ( file, magic) ,
4647 MH_MAGIC_64 | MH_CIGAM_64 => MachHeader64 :: from_file :: < R , E > ( file, magic) ,
4748 _ => unreachable ! ( ) ,
4849 }
4950}
5051
51- fn parse_load_commands < R : Read + Seek , E : ByteOrder > ( file : & mut R , header : & MachHeader ) -> io :: Result < ( Vec < LoadCommand > , Vec < Vec < Section > > , Vec < LcStr > ) > {
52+ fn parse_load_commands < R : Read + Seek , E : ByteOrder > ( file : & mut R , header : & MachHeader ) -> Result < ( Vec < LoadCommand > , Vec < Vec < Section > > , Vec < LcStr > ) , AppError > {
5253 let mut load_commands: Vec < LoadCommand > = Vec :: new ( ) ;
5354 let mut sections: Vec < Vec < Section > > = Vec :: new ( ) ;
5455 let mut load_commands_strings: Vec < LcStr > = Vec :: new ( ) ;
@@ -70,9 +71,8 @@ fn parse_load_commands<R: Read + Seek, E: ByteOrder>(file: &mut R, header: &Mach
7071 Ok ( ( load_commands, sections, load_commands_strings) )
7172}
7273
73- fn parse_command < R : Read , E : ByteOrder > ( file : & mut R , load_command_prefix : & LoadCommandPrefix ) -> io :: Result < LoadCommand > {
74+ fn parse_command < R : Read , E : ByteOrder > ( file : & mut R , load_command_prefix : & LoadCommandPrefix ) ->Result < LoadCommand , AppError > {
7475 match load_command_prefix. cmd {
75- LC_SEGMENT => SegmentCommand32 :: from_file :: < R , E > ( file, load_command_prefix) ,
7676 LC_SYMTAB => SymtabCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
7777 LC_SYMSEG => SymsegCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
7878 LC_THREAD | LC_UNIXTHREAD => ThreadCommand :: from_file :: < E > ( load_command_prefix) ,
@@ -88,6 +88,7 @@ fn parse_command<R: Read, E: ByteOrder>(file: &mut R, load_command_prefix: &Load
8888 LC_SUB_LIBRARY => SubLibraryCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
8989 LC_TWOLEVEL_HINTS => TwoLevelHintsCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
9090 LC_PREBIND_CKSUM => PrebindCksumCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
91+ LC_SEGMENT => SegmentCommand32 :: from_file :: < R , E > ( file, load_command_prefix) ,
9192 LC_SEGMENT_64 => SegmentCommand64 :: from_file :: < R , E > ( file, load_command_prefix) ,
9293 LC_ROUTINES_64 => RoutinesCommand64 :: from_file :: < R , E > ( file, load_command_prefix) ,
9394 LC_UUID => UuidCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
@@ -102,23 +103,23 @@ fn parse_command<R: Read, E: ByteOrder>(file: &mut R, load_command_prefix: &Load
102103 LC_LINKER_OPTION => LinkerOptionCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
103104 LC_NOTE => NoteCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
104105 LC_BUILD_VERSION => BuildVersionCommand :: from_file :: < R , E > ( file, load_command_prefix) ,
105- _ => Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "unknown load command type!" ) )
106+ _ => Err ( AppError :: from ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "unknown load command type!" ) ) )
106107 }
107108}
108109
109- fn parse_sections_for_segment < R : Read + Seek , E : ByteOrder > ( file : & mut R , load_command : & LoadCommand ) -> io :: Result < Vec < Section > > {
110+ fn parse_sections_for_segment < R : Read + Seek , E : ByteOrder > ( file : & mut R , load_command : & LoadCommand ) -> Result < Vec < Section > , AppError > {
110111 let mut load_command_sections = Vec :: new ( ) ;
111112 match load_command {
112113 LoadCommand :: SegmentCommand ( command) => {
113114 match command {
114115 SegmentCommand :: SEG32 ( command) => {
115- for i in 0 ..command. nsects {
116+ for _ in 0 ..command. nsects {
116117 let section = Section32 :: from_file :: < R , E > ( file) ?;
117118 load_command_sections. push ( section) ;
118119 }
119120 }
120121 SegmentCommand :: SEG64 ( command) => {
121- for i in 0 ..command. nsects {
122+ for _ in 0 ..command. nsects {
122123 let section = Section64 :: from_file :: < R , E > ( file) ?;
123124 load_command_sections. push ( section) ;
124125 }
@@ -130,7 +131,7 @@ fn parse_sections_for_segment<R: Read + Seek, E: ByteOrder>(file: &mut R, load_c
130131 Ok ( load_command_sections)
131132}
132133
133- fn parse_load_command_string < R : Read + Seek , E : ByteOrder > ( file : & mut R , load_command : & LoadCommand , lc_offset : u64 , cmdsize : u32 ) -> io :: Result < LcStr > {
134+ fn parse_load_command_string < R : Read + Seek , E : ByteOrder > ( file : & mut R , load_command : & LoadCommand , lc_offset : u64 , cmdsize : u32 ) -> Result < LcStr , AppError > {
134135 let mut load_command_string = Vec :: new ( ) ;
135136 match load_command {
136137 LoadCommand :: DylibCommand ( _) |
@@ -153,6 +154,6 @@ fn parse_load_command_string<R: Read + Seek, E: ByteOrder>(file: &mut R, load_co
153154 Ok ( load_command_string)
154155}
155156
156- fn get_load_command_remaining_size ( lc_offset : u64 , lc_size : u64 , file_offset : u64 ) -> io :: Result < u64 > {
157+ fn get_load_command_remaining_size ( lc_offset : u64 , lc_size : u64 , file_offset : u64 ) -> Result < u64 , AppError > {
157158 Ok ( ( lc_offset + lc_size) - file_offset)
158159}
0 commit comments