Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
miasm2.core.interval.interval Class Reference
+ Inheritance diagram for miasm2.core.interval.interval:
+ Collaboration diagram for miasm2.core.interval.interval:

Public Member Functions

def __init__
 
def __iter__
 
def cannon
 
def __repr__
 
def __contains__
 
def __eq__
 
def __add__
 
def __sub__
 
def __and__
 
def hull
 
def empty
 
def show
 

Static Public Member Functions

def cannon_list
 

Public Attributes

 is_cannon
 
 intervals
 

Detailed Description

Stands for intervals with integer bounds

Offers common methods to work with interval

Definition at line 33 of file interval.py.

Constructor & Destructor Documentation

def miasm2.core.interval.interval.__init__ (   self,
  bounds = None 
)
Instance an interval object
@bounds: (optional) list of (int, int) and/or interval instance

Definition at line 38 of file interval.py.

38 
39  def __init__(self, bounds=None):
40  """Instance an interval object
41  @bounds: (optional) list of (int, int) and/or interval instance
42  """
43  if bounds is None:
44  bounds = []
45  elif isinstance(bounds, interval):
46  bounds = bounds.intervals
47  self.is_cannon = False
48  self.intervals = bounds
49  self.cannon()

Member Function Documentation

def miasm2.core.interval.interval.__add__ (   self,
  i 
)

Definition at line 123 of file interval.py.

124  def __add__(self, i):
125  if isinstance(i, interval):
126  i = i.intervals
127  i = interval(self.intervals + i)
128  return i

+ Here is the caller graph for this function:

def miasm2.core.interval.interval.__and__ (   self,
  v 
)

Definition at line 179 of file interval.py.

180  def __and__(self, v):
181  out = []
182  for x in self.intervals:
183  if x[0] > x[1]:
184  continue
185  for y in v.intervals:
186  rez = cmp_interval(x, y)
187 
188  if rez == INT_DISJOIN:
189  continue
190  elif rez == INT_EQ:
191  out.append(x)
192  continue
193  elif rez == INT_A_IN_B:
194  out.append(x)
195  continue
196  elif rez == INT_B_IN_A:
197  out.append(y)
198  continue
199  elif rez == INT_JOIN_AB:
200  continue
201  elif rez == INT_JOIN_BA:
202  continue
203  elif rez == INT_JOIN:
204  if x[0] < y[0]:
205  out.append((y[0], x[1]))
206  else:
207  out.append((x[0], y[1]))
208  continue
209  else:
210  raise ValueError('unknown state', rez)
211  return interval(out)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

def miasm2.core.interval.interval.__contains__ (   self,
  other 
)

Definition at line 103 of file interval.py.

104  def __contains__(self, other):
105  if isinstance(other, interval):
106  for intervalB in other.intervals:
107  is_in = False
108  for intervalA in self.intervals:
109  if cmp_interval(intervalA, intervalB) in [INT_EQ, INT_B_IN_A]:
110  is_in = True
111  break
112  if not is_in:
113  return False
114  return True
115  else:
116  for intervalA in self.intervals:
117  if intervalA[0] <= other <= intervalA[1]:
118  return True
119  return False

+ Here is the call graph for this function:

def miasm2.core.interval.interval.__eq__ (   self,
  i 
)

Definition at line 120 of file interval.py.

121  def __eq__(self, i):
122  return self.intervals == i.intervals

+ Here is the caller graph for this function:

def miasm2.core.interval.interval.__iter__ (   self)
Iterate on intervals

Definition at line 50 of file interval.py.

50 
51  def __iter__(self):
52  """Iterate on intervals"""
53  for inter in self.intervals:
54  yield inter
def miasm2.core.interval.interval.__repr__ (   self)

Definition at line 95 of file interval.py.

95 
96  def __repr__(self):
97  if self.intervals:
98  o = " U ".join(["[0x%X 0x%X]" % (x[0], x[1])
99  for x in self.intervals])
100  else:
101  o = "[]"
102  return o
def miasm2.core.interval.interval.__sub__ (   self,
  v 
)

Definition at line 129 of file interval.py.

130  def __sub__(self, v):
131  to_test = self.intervals[:]
132  i = -1
133  to_del = v.intervals[:]
134  while i < len(to_test) - 1:
135  i += 1
136  x = to_test[i]
137  if x[0] > x[1]:
138  del to_test[i]
139  i -= 1
140  continue
141 
142  while to_del and to_del[0][1] < x[0]:
143  del to_del[0]
144 
145  for y in to_del:
146  if y[0] > x[1]:
147  break
148  rez = cmp_interval(x, y)
149  if rez == INT_DISJOIN:
150  continue
151  elif rez == INT_EQ:
152  del to_test[i]
153  i -= 1
154  break
155  elif rez == INT_A_IN_B:
156  del to_test[i]
157  i -= 1
158  break
159  elif rez == INT_B_IN_A:
160  del to_test[i]
161  i1 = (x[0], y[0] - 1)
162  i2 = (y[1] + 1, x[1])
163  to_test[i:i] = [i1, i2]
164  i -= 1
165  break
166  elif rez in [INT_JOIN_AB, INT_JOIN_BA]:
167  continue
168  elif rez == INT_JOIN:
169  del to_test[i]
170  if x[0] < y[0]:
171  to_test[i:i] = [(x[0], y[0] - 1)]
172  else:
173  to_test[i:i] = [(y[1] + 1, x[1])]
174  i -= 1
175  break
176  else:
177  raise ValueError('unknown state', rez)
178  return interval(to_test)

+ Here is the call graph for this function:

def miasm2.core.interval.interval.cannon (   self)

Definition at line 88 of file interval.py.

88 
89  def cannon(self):
90  "Apply .cannon_list() on self contained intervals"
91  if self.is_cannon is True:
92  return
93  self.intervals = interval.cannon_list(self.intervals)
94  self.is_cannon = True
def miasm2.core.interval.interval.cannon_list (   tmp)
static
Return a cannonizes list of intervals
@tmp: list of (int, int)

Definition at line 56 of file interval.py.

56 
57  def cannon_list(tmp):
58  """
59  Return a cannonizes list of intervals
60  @tmp: list of (int, int)
61  """
62  tmp = sorted([x for x in tmp if x[0] <= x[1]])
63  out = []
64  if not tmp:
65  return out
66  out.append(tmp.pop())
67  while tmp:
68  x = tmp.pop()
69  rez = cmp_interval(out[-1], x)
70 
71  if rez == INT_EQ:
72  continue
73  elif rez == INT_DISJOIN:
74  out.append(x)
75  elif rez == INT_B_IN_A:
76  continue
77  elif rez in [INT_JOIN, INT_JOIN_AB, INT_JOIN_BA, INT_A_IN_B]:
78  u, v = x
79  while out and cmp_interval(out[-1], (u, v)) in [
80  INT_JOIN, INT_JOIN_AB, INT_JOIN_BA, INT_A_IN_B]:
81  u = min(u, out[-1][0])
82  v = max(v, out[-1][1])
83  out.pop()
84  out.append((u, v))
85  else:
86  raise ValueError('unknown state', rez)
87  return out[::-1]

+ Here is the call graph for this function:

def miasm2.core.interval.interval.empty (   self)
Return True iff the interval is empty

Definition at line 220 of file interval.py.

221  def empty(self):
222  """Return True iff the interval is empty"""
223  return not self.intervals
def miasm2.core.interval.interval.hull (   self)

Definition at line 212 of file interval.py.

213  def hull(self):
214  "Return the first and the last bounds of intervals"
215  if not self.intervals:
216  return None, None
217  return self.intervals[0][0], self.intervals[-1][1]
218 

+ Here is the caller graph for this function:

def miasm2.core.interval.interval.show (   self,
  img_x = 1350,
  img_y = 20,
  dry_run = False 
)
show image representing the interval

Definition at line 224 of file interval.py.

225  def show(self, img_x=1350, img_y=20, dry_run=False):
226  """
227  show image representing the interval
228  """
229  try:
230  import Image
231  import ImageDraw
232  except ImportError:
233  print 'cannot import python PIL imaging'
234  return
235 
236  img = Image.new('RGB', (img_x, img_y), (100, 100, 100))
237  draw = ImageDraw.Draw(img)
238  i_min, i_max = self.hull()
239 
240  print hex(i_min), hex(i_max)
241 
242  addr2x = lambda addr: (addr - i_min) * img_x / (i_max - i_min)
243  for a, b in self.intervals:
244  draw.rectangle((addr2x(a), 0, addr2x(b), img_y), (200, 0, 0))
245 
246  if dry_run is False:
247  img.show()

+ Here is the call graph for this function:

Member Data Documentation

miasm2.core.interval.interval.intervals

Definition at line 47 of file interval.py.

miasm2.core.interval.interval.is_cannon

Definition at line 46 of file interval.py.


The documentation for this class was generated from the following file: