Home | History | Annotate | Download | only in tpm2
      1 #!/usr/bin/python
      2 
      3 import sys
      4 import re
      5 
      6 header = '''// This file was extracted from the TCG Published
      7 // Trusted Platform Module Library
      8 // Part 4: Supporting Routines
      9 // Family "2.0"
     10 // Level 00 Revision 01.16
     11 // October 30, 2014
     12 
     13 '''
     14 
     15 head_spaces = re.compile('^\s*[0-9]+\s{0,4}')
     16 sec_num_in_comments = re.compile('^//\s+([A-D0-9]\.([0-9]\.?)+)')
     17 source_lines = open(sys.argv[1], 'r').read().splitlines()
     18 
     19 def postprocess_lines(buffer):
     20     # get rid of heading line numbers and spaces.
     21     postproc_buffer = []
     22     for big_line in buffer:
     23         big_line = head_spaces.sub('', big_line)
     24         for line in big_line.splitlines():
     25             m = sec_num_in_comments.match(line)
     26             if m:
     27                 line = line.replace(m.groups()[0], '')
     28             postproc_buffer.append(line)
     29 
     30     return header + '\n'.join(postproc_buffer) + '\n'
     31 
     32 text = []
     33 for line in source_lines:
     34     text.append(line)
     35     if line == '' and text[-2].startswith('') and text[-5] == '':
     37         text = text[:-5]
     38 
     39 func_file = None
     40 file_name = ''
     41 prev_num = 0
     42 line_buffer = []
     43 output_buffer = []
     44 for line in text:
     45     f = re.match('^\s*[A-D0-9]+\.([0-9]+|([0-9]+\.)+)\s+(\S+\.[ch])$', line)
     46     if not f:
     47         f = re.match('^\s*[A-D0-9]+\.([0-9]+|([0-9]+\.)+)\s+[^\(]+\((\S+\.[ch])\)$', line)
     48     if f:
     49         file_name = f.groups(0)[2]
     50         continue
     51 
     52     num = re.match('^\s{0,3}([0-9]+)\s', line + ' ')
     53     if num:
     54         line_num = int(num.groups(0)[0])
     55         if line_num == 1:
     56             # this is the first line of a file
     57             if func_file:
     58                 func_file.write(postprocess_lines(output_buffer))
     59                 func_file.close()
     60             if file_name:
     61                 func_file = open('%s' % file_name, 'w')
     62             output_buffer = [line,]
     63             prev_num = 1
     64             line_buffer = []
     65             continue
     66         if line_num == prev_num + 1:
     67             if line_buffer:
     68                 output_buffer.append('\n'.join(line_buffer))
     69                 line_buffer = []
     70             output_buffer.append(line)
     71             prev_num = line_num
     72         continue
     73     if not '//' in line:
     74         line = '//' + line
     75     line_buffer.append(line)
     76 
     77 if func_file:
     78     func_file.write(postprocess_lines(output_buffer))
     79     func_file.close()
     80