Debian Codegraph @

rpython/rlib/rstruct/formatiterator.py

raw symbols SCIP
1 from rpython.rlib import jit
2 from rpython.rlib.rarithmetic import ovfcheck
3 from rpython.rlib.rstruct.error import StructError
4 from rpython.rlib.rstruct.nativefmttable import native_is_bigendian, native_fmttable
5 from rpython.rlib.rstruct.standardfmttable import standard_fmttable
6 from rpython.rlib.unroll import unrolling_iterable
7
8
9 class FormatIterator(object):
10 """
11 An iterator-like object that follows format strings step by step.
12 It provides input to the packer/unpacker and accumulates their output.
13 The subclasses are specialized for either packing, unpacking, or
14 just computing the size.
15 """
16 _mixin_ = True
17 _operate_is_specialized_ = False
18
19 @jit.look_inside_iff(lambda self, fmt: jit.isconstant(fmt))
20 def interpret(self, fmt):
21 # decode the byte order, size and alignment based on the 1st char
22 table = unroll_native_fmtdescs
23 self.bigendian = native_is_bigendian
24 index = 0
25 if len(fmt) > 0:
26 c = fmt[0]
27 index = 1
28 if c == '@':
29 pass
30 elif c == '=':
31 table = unroll_standard_fmtdescs
32 elif c == '<':
33 table = unroll_standard_fmtdescs
34 self.bigendian = False
35 elif c == '>' or c == '!':
36 table = unroll_standard_fmtdescs
37 self.bigendian = True
38 else:
39 index = 0
40
41 # interpret the format string,
42 # calling self.operate() for each format unit
43 while index < len(fmt):
44 c = fmt[index]
45 index += 1
46 if c.isspace():
47 continue
48 if c.isdigit():
49 repetitions = ord(c) - ord('0')
50 while True:
51 if index == len(fmt):
52 raise StructError("incomplete struct format")
53 c = fmt[index]
54 index += 1
55 if not c.isdigit():
56 break
57 try:
58 repetitions = ovfcheck(repetitions * 10)
59 repetitions = ovfcheck(repetitions + (ord(c) -
60 ord('0')))
61 except OverflowError:
62 raise StructError("overflow in item count")
63 assert repetitions >= 0
64 else:
65 repetitions = 1
66
67 for fmtdesc in table:
68 if c == fmtdesc.fmtchar:
69 if self._operate_is_specialized_:
70 if fmtdesc.alignment > 1:
71 self.align(fmtdesc.mask)
72 self.operate(fmtdesc, repetitions)
73 break
74 else:
75 if c == '\0':
76 raise StructError("embedded null character")
77 raise StructError("bad char in struct format")
78 if not self._operate_is_specialized_:
79 if fmtdesc.alignment > 1:
80 self.align(fmtdesc.mask)
81 self.operate(fmtdesc, repetitions)
82 self.finished()
83
84 def finished(self):
85 pass
86
87
88 class CalcSizeFormatIterator(FormatIterator):
89 totalsize = 0
90
91 def operate(self, fmtdesc, repetitions):
92 try:
93 size = ovfcheck(fmtdesc.size * repetitions)
94 self.totalsize = ovfcheck(self.totalsize + size)
95 except OverflowError:
96 raise StructError("total struct size too long")
97
98 def align(self, mask):
99 pad = (-self.totalsize) & mask
100 try:
101 self.totalsize = ovfcheck(self.totalsize + pad)
102 except OverflowError:
103 raise StructError("total struct size too long")
104
105
106 class FmtDesc(object):
107 def __init__(self, fmtchar, attrs):
108 self.fmtchar = fmtchar
109 self.alignment = 1 # by default
110 self.needcount = False # by default
111 self.__dict__.update(attrs)
112 self.mask = self.alignment - 1
113 assert self.alignment & self.mask == 0, (
114 "this module assumes that all alignments are powers of two")
115 def _freeze_(self):
116 return True
117
118 def table2desclist(table):
119 items = table.items()
120 items.sort()
121 lst = [FmtDesc(key, attrs) for key, attrs in items]
122 return unrolling_iterable(lst)
123
124
125 unroll_standard_fmtdescs = table2desclist(standard_fmttable)
126 unroll_native_fmtdescs = table2desclist(native_fmttable)