@@ -120,6 +120,16 @@ def do_add_parser(self, parser_adder):
120120 help = 'Target CPU is big endian' )
121121 dump_parser .set_defaults (subcmd = 'dump' , big_endian = False )
122122
123+ extract_parser = subparsers .add_parser ('extract' ,
124+ help = 'Extract the binary descriptor blob to a file' )
125+ extract_parser .add_argument ('file' , type = str , help = 'Executable file' )
126+ extract_parser .add_argument ('out_file' , type = str , help = 'Bindesc binary dump file' )
127+ extract_parser .add_argument ('--file-type' , type = str , choices = self .EXTENSIONS ,
128+ help = 'Input file type' )
129+ extract_parser .add_argument ('-b' , '--big-endian' , action = 'store_true' ,
130+ help = 'Target CPU is big endian' )
131+ extract_parser .set_defaults (subcmd = 'extract' , big_endian = False )
132+
123133 search_parser = subparsers .add_parser ('search' , help = 'Search for a specific descriptor' )
124134 search_parser .add_argument ('descriptor' , type = str , help = 'Descriptor name' )
125135 search_parser .add_argument ('file' , type = str , help = 'Executable file' )
@@ -204,6 +214,29 @@ def get_offset(self, args):
204214 self .die ('Could not find binary descriptor magic' )
205215 self .inf (f'{ index } { hex (index )} ' )
206216
217+ def extract (self , args ):
218+ image = self .get_image_data (args .file )
219+
220+ magic = struct .pack ('>Q' if self .is_big_endian else 'Q' , self .MAGIC )
221+ index = image .find (magic )
222+ if index == - 1 :
223+ self .die ('Could not find binary descriptor magic' )
224+
225+ index += len (magic ) # index points to first descriptor
226+ block_start = index
227+ current_tag = self .bytes_to_short (image [index :index + 2 ])
228+ while current_tag != self .DESCRIPTORS_END :
229+ index += 2 # index points to length
230+ length = self .bytes_to_short (image [index :index + 2 ])
231+ # go to next tag
232+ index = self .align (index + 2 + length , 4 )
233+ current_tag = self .bytes_to_short (image [index :index + 2 ])
234+ block_len = index - block_start
235+
236+ with open (args .out_file , 'wb' ) as out_file :
237+ out_file .write (image [block_start :index ])
238+ self .inf (f'{ block_start } +{ block_len } { hex (block_start )} +{ hex (block_len )} ' )
239+
207240 def do_run (self , args , _ ):
208241 if MISSING_REQUIREMENTS :
209242 raise RuntimeError ('one or more Python dependencies were missing; '
0 commit comments