saip-tool: add option to extact profile elements to file

In some cases it may be helpful to extract a single profile element
from the sequence to a dedicated file.

Change-Id: I77a80bfaf8970660a84fa61f7e08f404ffc4c2da
This commit is contained in:
Philipp Maier
2025-03-25 12:13:39 +01:00
parent 02a7a2139f
commit 2b3b2c2a3b

View File

@@ -48,6 +48,10 @@ parser_dump.add_argument('--dump-decoded', action='store_true', help='Dump decod
parser_check = subparsers.add_parser('check', help='Run constraint checkers on PE-Sequence') parser_check = subparsers.add_parser('check', help='Run constraint checkers on PE-Sequence')
parser_rpe = subparsers.add_parser('extract-pe', help='Extract specified PE to (DER encoded) file')
parser_rpe.add_argument('--pe-file', required=True, help='PE file name')
parser_rpe.add_argument('--identification', type=int, help='Extract PE matching specified identification')
parser_rpe = subparsers.add_parser('remove-pe', help='Remove specified PEs from PE-Sequence') parser_rpe = subparsers.add_parser('remove-pe', help='Remove specified PEs from PE-Sequence')
parser_rpe.add_argument('--output-file', required=True, help='Output file name') parser_rpe.add_argument('--output-file', required=True, help='Output file name')
parser_rpe.add_argument('--identification', type=int, action='append', help='Remove PEs matching specified identification') parser_rpe.add_argument('--identification', type=int, action='append', help='Remove PEs matching specified identification')
@@ -126,6 +130,14 @@ def do_check(pes: ProfileElementSequence, opts):
checker.check(pes) checker.check(pes)
print("All good!") print("All good!")
def do_extract_pe(pes: ProfileElementSequence, opts):
new_pe_list = []
for pe in pes.pe_list:
if pe.identification == opts.identification:
print("Extracting PE %s (id=%u) to file %s..." % (pe, pe.identification, opts.pe_file))
with open(opts.pe_file, 'wb') as f:
f.write(pe.to_der())
def do_remove_pe(pes: ProfileElementSequence, opts): def do_remove_pe(pes: ProfileElementSequence, opts):
new_pe_list = [] new_pe_list = []
for pe in pes.pe_list: for pe in pes.pe_list:
@@ -248,6 +260,8 @@ if __name__ == '__main__':
do_dump(pes, opts) do_dump(pes, opts)
elif opts.command == 'check': elif opts.command == 'check':
do_check(pes, opts) do_check(pes, opts)
elif opts.command == 'extract-pe':
do_extract_pe(pes, opts)
elif opts.command == 'remove-pe': elif opts.command == 'remove-pe':
do_remove_pe(pes, opts) do_remove_pe(pes, opts)
elif opts.command == 'remove-naa': elif opts.command == 'remove-naa':