Esempio n. 1
0
  def __init__(self, m32=False):
    self.types = []
    self.by_clang_type = Attributize()
    self.by_name = Attributize()
    self.little_endian = '><'
    self.m32 = m32

    for typ in types_list[self.m32]:
      names, clang_types, size, pack = typ.split(':')
      size = int(size)
      names = names.split(',')
      name = names[0]
      typ = 'unsigned'
      if name in csv_list('float,double'):
        typ = 'float'
      elif name == 'bool':
        typ = 'bool'
      elif name[0] == 's':
        typ = 'signed'

      clang_types_list = [TypeKind.__dict__[e] for e in clang_types.split(',')]
      x = Attributize(name=name,
                      alias=names,
                      clang_types_list=clang_types_list,
                      size=size,
                      pack=pack,
                      typ=typ)
      self.types.append(x)
      for name in names:
        self.by_name[name] = x

      for v in clang_types_list:
        self.by_clang_type[v] = x
Esempio n. 2
0
    def __init__(self):
        self.extractors = []
        self.consts = Attributize(key_norm=byte_norm)
        self.typs = Attributize(key_norm=byte_norm)
        self.functions = Attributize(key_norm=byte_norm)
        self.vars = Attributize(key_norm=byte_norm)
        self.content = ['#include <stddef.h>']

        self.has_setup_includes = False
        self.cats = Attributize()
Esempio n. 3
0
def plot_meshlist(ctx):
  meshes = read_meshlist(ctx.infile, barvinok_set_attrs)

  meshes.append(Attributize(points=np.array([[0, 0, 0]]), color='white', points_size=20))
  meshes.append(
      Attributize(lines=np.array([[[0, 0, 0], [20, 0, -30]]]), color='white', points_size=20))

  ctx = VispyCtx(display_status=True)
  ctx.plot_meshes(*meshes)
  ctx.run()
Esempio n. 4
0
    def find_variables(self):
        res = re.findall(self.var_block_pattern, self.content)

        assert len(res) == 1
        res = res[0]
        x = Attributize()  # base OrderedDict
        for entry in re.finditer(self.var_pattern, res):
            m = Attributize(entry.groupdict())
            assert m.var_name == m.var_name2
            x[m.var_name] = m.var_default
        return x
Esempio n. 5
0
  def __init__(self, modes):
    super().__init__()
    self.modes = {}
    self.setup(modes)

    root_mode_name = BusPirateModes.TEXT_MODE
    self.update_submodes(self.modes[root_mode_name])
    self.state = Attributize(aux=1, pullup=1, power=0, cs=0)
    self.conf = Attributize(output_3v3=0, prot_3wire=0, lsb=0, ckp_idle=0, cke_active=1, sample_time=0)
    self.serial = None
    self.flags = None
    self.default_mode = None
    self.active_mode = None
Esempio n. 6
0
    def compute_score_en(self, s):
        x = DictWithDefault(lambda: 0)
        nspace = 0
        nalpha = 0
        nbad = 0
        n = len(s)
        for c in s:
            if isspace(c) or c == '_':
                nspace += 1
            elif isalpha(c):
                nalpha += 1
                x[c | 32] += 1
            elif not isgoodchar(c):
                nbad += 1

        tb = sorted(x.values())
        tb.reverse()
        score = 0
        if nalpha == 0:
            score += 1000
        else:
            for c, v in self.freq_en.items():
                score += (v - x[c] / nalpha)**2
        #score += (nspace / n - self.freq_sorted[0])**2
        score += (300 * nbad / n)**2
        return Attributize(score=score, nbad=nbad)
Esempio n. 7
0
def read_meshlist(filename, set_attrs):
  meshlist = common_pb2.MeshList.FromString(open(filename, "rb").read())
  print(meshlist)

  meshes = []
  for mesh in meshlist.mesh:
    tags = mesh.name.split('-')
    points = []
    faces = []
    lines = []
    for face in mesh.face:
      pts = list([[v.x, v.y, v.z] for v in face.vertex])
      pos = len(points)
      n = len(pts)
      points.extend(pts)
      faces.append(list(range(pos, pos + n)))
      for i in range(len(pts)):
        lines.append((pts[i], pts[(i + 1) % n]))
    m = Attributize(
        mesh=MeshData(vertices=np.array(points), faces=np.array(faces)),
        lines=lines,
        points=points,
        color=mesh.col.desc,
        faces=faces)
    m.tags = tags
    if set_attrs(m):
      meshes.append(m)
  return meshes
Esempio n. 8
0
def add_mesh_data_to_view(mdata,
                          view,
                          want_faces=True,
                          want_points=True,
                          want_lines=True,
                          transform=transforms.MatrixTransform()):
  color = mdata.get('color', 'g')
  if isinstance(color, str) and color == 'random': color = np.random.random(3).tolist()
  else: color = Color(color).rgb.tolist()

  res = Attributize()
  res.lines = []
  res.objs = []
  if want_lines and 'lines' in mdata:
    for polyline in mdata.lines:
      if isinstance(polyline, Attributize):
        width = polyline.get('width', 1)
        method = 'gl'
        if width > 1: method = 'gl'
        l = Line(
            pos=np.array(np.array(polyline.polyline)),
            antialias=True,
            method=method,
            color=np.array(polyline.colors),
            width=width,)
      else:
        l = Line(pos=np.array(np.array(polyline)), antialias=False, method='gl', color=color)
      l.transform = transform
      view.add(l)
      res.lines.append(l)
      res.objs.append(l)

  if want_points and mdata.get('points', None) is not None:
    scatter = Markers()
    #mdata.points = np.array((
    #    (10.928140, -51.417831, -213.253723),
    #    (0.000000, -46.719570, -205.607208),
    #    (0.000000, -53.499737, -215.031876),
    #    (0.000000, -69.314308, -223.780746),
    #    (0.000000, -89.549263, -170.910568),))
    #mdata.points = np.array(((-12.138942,-55.812309,-217.007050),(10.928140,-51.417831,-213.253723),(-7.289741,-43.585541,-200.506531)))
    points_color = mdata.get('points_color', color)
    points_size = mdata.get('points_size', 10)
    points = np.array(mdata.points)

    #print('PLOTTING ', points, points_size)
    if len(points) == 0: return
    scatter.set_data(points, edge_color=None, face_color=points_color, size=points_size)
    scatter.transform = transform
    view.add(scatter)
    res.objs.append(scatter)
    res.points = scatter

  if want_faces and 'mesh' in mdata:
    mesh = Mesh(meshdata=mdata.mesh, color=color + [0.7])
    mesh.transform = transform
    view.add(mesh)
    res.mesh = mesh
    res.objs.append(mesh)
  return res
Esempio n. 9
0
def get_mesh_data_from_graph(graph_data, want_lines=True, want_points=True, **kwargs):

  pc = graph_data.points
  faces = vec_pos_to_list(graph_data.faces)

  for face_vertices in faces:
    assert len(face_vertices) == 3
  faces = np.array(faces)
  print(faces)
  polylines = []

  if want_points:
    pc = list([pc[i] for i in range(len(pc))])
    pc = np.array(pc)

  if want_lines:
    cc = graph_data.graph.make_bidirectional().split_cc()

    polylines = []
    for x in cc:
      polyline = []
      walk = x.get_cover_walk_dumb()
      for y in walk:
        assert y >= 0 and y < len(pc)
      polyline.extend([pc[y] for y in walk])
      polylines.append(polyline)

  return Attributize(mesh=MeshData(vertices=pc, faces=faces), lines=polylines, points=pc, **kwargs)
Esempio n. 10
0
def read_desc(desc_file):
  lines = open(desc_file, 'r').readlines()[1:]
  layers = defaultdict(lambda: [])
  by_id = {}

  for line in lines:
    content = line.strip().split()
    idx, typ, layer = int(content[0]), int(content[1]), int(content[2])
    res = Attributize(typ=typ, layer=layer, cell_name=content[3])
    pos = 4
    if typ == 1:
      tmp = list([int(x) for x in content[pos:]])
      tmp = np.reshape(tmp, (4, 2))
      res.box = Box().union_points(tmp)
    else:
      tmp = list([int(x) for x in content[pos:pos+2]])
      res.box = Box(tmp, tmp)
      res.text = content[-1]

    res.good = False
    tb = layers[layer]
    res.idx = idx
    assert idx == len(by_id), content
    by_id[idx] = res
    tb.append(res)
  return layers, by_id
Esempio n. 11
0
def argparsify(parser, a):

    args = inspect.getargspec(a.func)
    typs = a.args

    a2_default = args[3]
    if not a2_default:
        a2_default = []
    pos_default = len(args[0]) - len(a2_default)
    a1 = args[0][:pos_default]
    a1_typs = typs[:pos_default]

    a2 = args[0][pos_default:]
    a2_typs = typs[pos_default:]

    for name, typ in zip(a1, a1_typs):
        opt = Attributize()
        if isinstance(typ, list):
            assert len(typ) == 1
            opt.type = typ[0]
            opt.nargs = '*'
        else:
            opt.type = typ
        parser.add_argument('--%s' % name, required=True, **opt._elem)

    for name, typ, default in zip(a2, a2_typs, a2_default):
        parser.add_argument('--%s' % name, type=typ, default=default)

    def do_call(data):
        data = {x: vars(data)[x] for x in args[0]}
        res = a.func(**data)
        if res is not None:
            print(res)

    parser.set_defaults(func=do_call)
Esempio n. 12
0
    def filter_walk(self, walk, can):
        data = [0] * n
        data[self.static_elem - n] = self.static_elem

        prev = walk[0]
        for i in walk:
            data[i - n] = prev
            prev = i
        base_score = self.oracle.sign(data)

        depths = Attributize(other=lambda x: set([]))
        for i in range(n - 1):
            depths[i + 1].add(walk[i])
        vals = []
        cnt = 0
        for i in range(n):
            if len(can[i]) == 1:
                cnt += 1
            data[self.static_elem - n] = i
            cur = self.oracle.sign(data)
            assert cur % base_score == 0
            v = cur // base_score - 1
            depths[v].add(i)
            vals.append(v)
        if cnt > 0:
            print('COULd SPARE ', cnt)

        for i in range(n):
            can[i].intersection_update(depths[vals[i] - 1])
            if len(can[i]) == 0:
                raise "FAIL"
Esempio n. 13
0
    def load_adcpro(filename):
        x = []
        params = Attributize()
        reg = re.compile('([^\t]*)\t([^\t]+)')
        with open(filename, 'r') as f:
            for line in f.readlines():
                if line.startswith('\t'):
                    pass
                m = reg.match(line.rstrip())
                if not m:
                    continue
                name = m.group(1)
                data = m.group(2)
                if len(name) == 0:
                    x.append(int(data))
                else:
                    params[name] = data.replace(',', '.')
        low_in = float(params['Min Code'])
        high_in = float(params['Max Code'])
        low_out = float(params['Min Voltage'])
        high_out = float(params['Max Voltage'])

        coeff = (high_out - low_out) / (high_in - low_in)
        x = [(e - low_in) * coeff + low_out for e in x]
        return [list(x)], float(params['Sampling Frequency'])
Esempio n. 14
0
 def reqs(self, flags):
     self.flags = flags
     res = Attributize(default=False)
     for x in self.all_actions:
         for v in x.reqs:
             res[v] = True
     return res
Esempio n. 15
0
def compute_i2c_data(data_list):
    data = [Attributize(data=x, stats=StatsHelper(x)) for x in data_list]

    if data[0].stats.num_edges() < data[1].stats.num_edges():
        data[0], data[1] = data[1], data[0]
    scl, sda = data
    return extract_i2c_comm(scl.data, sda.data, ignore_stopbits=True)
Esempio n. 16
0
    def __init__(self, desc_filename=path_here('./data/syscall_desc.txt')):
        self.syscall_filename = {
            Arch.x86: path_here('./data/unistd_32.h'),
            Arch.x86_64: path_here('./data/unistd_64.h')
        }

        self.desc_filename = desc_filename
        self.re = re.compile("""\
(?P<return_val>\S+)\s+(?P<func_name>\w+)\
(\|(?P<alias_list>\w+))?\
(:(?P<syscall_name>\w+)\
(:(?P<socketcall_id>\w+))?)?\
\((?P<params>[^)]*)\)\s+\
(?P<arch_list>\S+)\
""")

        self.all_archs = [Arch.x86, Arch.x86_64]
        self.data = Attributize()
        for arch in self.all_archs:
            self.data[arch] = SyscallData(arch)

        self.setup_syscalls_num()
        self.analyze()
        #only support x64 atm
        self.data[Arch.x86_64].build()
        self.data[Arch.x86].build()
Esempio n. 17
0
    def proc_entry(self, e):
        m = re.match(self.re, e)
        if not m:
            return
        extract = to_list(
            'return_val func_name alias_list syscall_name socketcall_id params arch_list'
        )
        tb = m.groupdict()
        data = Attributize(tb)
        data.params_list = [] if len(
            data.params) == 0 else data.params.split(',')

        data.arch_list = data.arch_list.split(',')
        if data.arch_list[0] == 'all':
            data.arch_list = self.all_archs
        else:
            data.arch_list = [
                Arch(x) for x in data.arch_list if x in Arch.__members__
            ]
        inter = set(data.arch_list).intersection(self.all_archs)
        data.arch_list = inter
        if not data.syscall_name:
            data.syscall_name = data.func_name

        for arch in inter:
            self.data[arch].add_entry(data)
Esempio n. 18
0
    def __init__(self, arch, watched, typ, pos, regs):
        self.arch = arch
        self.pos = pos
        self.watched = watched
        self.start_data = Attributize()
        self.end_data = Attributize()
        self.diff_params = Attributize(
            elem={x.name: x.diff_params
                  for x in watched})
        self.regs = regs

        self.typ = typ
        self.reads = []
        self.writes = []
        self.header = ''

        self.window = 0x100
Esempio n. 19
0
 def repl_func(x):
     m = Attributize(x.groupdict())
     if m.var_name in self.variables:
         v = self.variables[m.var_name]
         if m.var_name.find('_file') != -1: v = f"'{v}'"
         return 'self.{0} = {0} = {1}'.format(m.var_name, v)
     else:
         return x.group(0)
Esempio n. 20
0
    def update_params(**kwargs):
        if 'params' in kwargs:
            return kwargs['params']

        params = Attributize(default_none=True)
        for k, v in kwargs.items():
            params[k] = v
        return params
Esempio n. 21
0
    def finish(self, builder):
        cur = Attributize(key_norm=lambda x: byte_norm(x))
        builder.cats[self.name] = cur
        for prefix in self.prefixes:
            cur[prefix] = Attributize(key_norm=byte_norm)

        for e in self.added:
            for prefix in self.prefixes:
                if e.name.startswith(prefix):
                    if isinstance(e, opa_types.OpaMacro):

                        if isinstance(e.val, list):
                            # expressino macro, skipping
                            break
                        cur[prefix][e.val] = e.name
                    break
            else:
                assert 0, 'Should not have been added here'
Esempio n. 22
0
 def FromBuf(buf, args={}):
   input = io.BytesIO(buf)
   output = io.StringIO()
   args = dict(args)
   for k, v in kDefaults.items():
     if k not in args: args[k] = v
   x = xxd(Attributize(args), input, output)
   x.xxd()
   return output.getvalue()
Esempio n. 23
0
def test2():
  tb = load_data()
  mp = Attributize(other=lambda x: [])
  for i in range(2, len(tb)):
    u = tb[i] ^ tb[i - 2]
    mp[u].append(i * 4)
  res = []
  for k, v in mp._elem.items():
    if len(v) > 5:
      print(hex2(k), len(v))
Esempio n. 24
0
    def __init__(self, name, regs, lst):
        self.name = name
        self.regs = regs

        if lst == 'all': lst = regs.arch.regs
        self.lst = lst
        self.diff_params = Attributize(default_none=True,
                                       watcher=self,
                                       name=self.name,
                                       num_size=8)
Esempio n. 25
0
 def to_attr(self):
     if self.is_primitive:
         return self.get()
     elif self.is_array:
         return [self.child(i).to_attr() for i in range(self.array_size)]
     else:
         res = Attributize()
         for x in self._children.values():
             res[x._fieldname] = x.to_attr()
     return res
Esempio n. 26
0
def triangles_to_xyz_ijk(triangles):
    triangles = list(triangles)
    res = Attributize()
    points = flatten(triangles, depth=1)
    res.x = list([e[0] for e in points])
    res.y = list([e[1] for e in points])
    res.z = list([e[2] for e in points])
    res.i = list(range(0, 3 * len(triangles), 3))
    res.j = list(range(1, 3 * len(triangles), 3))
    res.k = list(range(2, 3 * len(triangles), 3))
    return res
Esempio n. 27
0
 def setup_syscalls_num(self):
     cur_re = re.compile(
         '#define\s+__NR_(?P<syscall_name>\w+)\s+(?P<syscall_val>\d+)')
     for arch, filename in self.syscall_filename.items():
         with open(filename, 'r') as f:
             for line in f.readlines():
                 m = cur_re.match(line)
                 if not m: continue
                 data = Attributize(m.groupdict())
                 self.data[arch].add_syscall_num(data.syscall_name,
                                                 int(data.syscall_val))
Esempio n. 28
0
def get_chunk(data, pos):
    length = struct.unpack('>I', data[pos:pos + 4])[0]
    pos += 4
    code = data[pos:pos + 4]
    pos += 4

    d = data[pos:pos + length]
    pos += length
    pos += 4
    print('got header ', code, length)
    return Attributize(length=length, code=code, next_pos=pos)
Esempio n. 29
0
def stl_parser(**kwargs):
    data = read_file_or_buf(**kwargs)
    parser = BufferParser(data)
    header = parser.read(80)
    ntr = parser.read_u32()
    tb = []
    for i in range(ntr):
        norm = parser.read_nf32(3)
        vx = [parser.read_nf32(3) for j in range(3)]
        attr = parser.read_u16()
        tb.append(Attributize(norm=norm, vx=vx, attr=attr))
    return tb
Esempio n. 30
0
 def add_sym0(self, symtab_section):
   sym = construct_make_default(self.elf.structs.Elf_Sym)
   sym.st_name = self.add_section_name_pos(b'', name_section=symtab_section.linked)
   sym.st_value = 0
   sym.st_size = 0
   sym.st_info.bind = 'STB_LOCAL'
   sym.st_info.type = 'STT_NOTYPE'
   sym.st_other.visibility = 'STV_DEFAULT'
   sym.st_shndx = 0
   res = Attributize(raw=sym)
   symtab_section.new_syms.append(res)
   return res