7 opts = optparse.OptionParser ()
8 opts.add_option ('-c', '--colour', '--color',
9 action='store_true', default=False, dest='colour',
10 help='Print colourful output (loads Python module "colored", page with "less -R")')
11 (options,args) = opts.parse_args ()
14 print 'Usage: ' + attr ('bold') + sys.argv [0] + '[--colour] file.der' + attr (0)
15 print 'Output: ' + nicemeaning ('MEANING') + ': ' + nicetag ('TAG') + ' ' + nicecontlen ('CONTLEN', 3) + ' ' + nicetagofs ('TAGOFS') + ' ' + nicenesting ('NESTING') + ', ' + niceclass ('CLASS') + ', ' + niceprimconstr ('PRIMCONSTR')
19 from colored import fg, bg, attr
47 7: 'Object-Descriptor',
59 19: 'PrintableString',
64 24: 'GeneralizedTime',
68 28: 'UniversalString',
69 29: 'CHARACTER STRING',
76 return bg ('yellow') + fg ('red') + s + attr (0)
79 return attr ('bold') + s + attr (0)
83 ds = 'tag 0x%02x' % ds
84 return fg ('green') + ds + attr (0)
86 def nicecontlen (ds, depth):
89 return fg ('magenta') + '#' * depth + ds + attr (0)
94 return fg ('cyan') + '@' + ds + attr (0)
97 if type (ad) == type ([]):
99 return fg ('light_blue') + '^' + str (ad) + attr (0)
104 return attr ('dim') + ds + attr (0)
106 def niceprimconstr (ds):
107 if type (ds) == bool:
111 return attr ('dim') + ds + attr (0)
113 der = open (args [0], 'r').read (65537)
118 return ofs >= len (der)
123 print 'ATTEMPTED READ BEYOND EOF (RETURNING 0x00)'
127 return ord (der [ofs-1])
133 while nesting != [] and ofs >= nesting [-1]:
134 if ofs > nesting [-1]:
135 print 'READ OFFSET %d EXCEEDS ENCAPSULATION %d (RETURNING)' % (ofs, nesting [-1])
139 tag_class = (tag & 0xc0) >> 6
140 tag_pc = (tag & 0x20) != 0
144 if lenlen & 0x80 == 0:
148 lenlen = lenlen - 0x80 + 1
153 leng = leng + read1 ()
157 meaning = universal2str [tag_num]
159 meaning = '[APPLICATION ' + str (tag_num) + ']'
161 meaning = '[' + str (tag_num) + ']'
163 meaning = '[PRIVATE ' + str (tag_num) + ']'
165 print ('%s%s: %s %s %s %s, %s, %s' % (
167 nicemeaning (meaning), nicetag (tag),
168 nicecontlen (leng, lenlen),
169 nicetagofs (ofs - lenlen - 1),
170 nicenesting (nesting),
171 niceclass (tag_class),
172 niceprimconstr (tag_pc) ) )
174 if tag_pc == 0 and leng > 0:
175 print ' ' * ( len (nesting) + 1 ),
184 cstr = cstr + chr (ch)
188 ival = -1 if ch >= 128 else 0
190 ival = (ival << 8) | ch
192 ostr = str (ch / 40) + '.' + str (ch % 40)
195 oval = (oval << 7) | (ch & 0x7f)
197 ostr = ostr + '.' + str (oval)
201 cstr = attr ('bold') + ostr + attr (0)
203 cstr = attr ('bold') + str (ival) + attr (0)
205 cstr = '"' + attr ('bold') + cstr + attr (0) + '"'
209 # print 'Now at', ofs, 'adding', leng, 'pushing', ofs + leng
210 nesting.append (ofs + leng)
213 if ofs != nesting [-1]:
214 print niceerror ('NESTING NOT ENDED CORRECTLY, OFFSET IS %d INSTEAD OF %d (CONTINUING)' % (ofs, nesting [-1]))