Exemplo n.º 1
0
def test_format_ctypes(configfile, c_header):
    c = conf.Config(configfile)
    conf.config = c
    defs = list(parse(c_header, tag="test"))
    x = ccore.from_db(defs[0])
    assert x._is_macro
    assert x.show(form="ctypes") == "MYCONST = 16"
    x = ccore.from_db(defs[5])
    assert x._is_typedef
    assert (x.show(form="ctypes") ==
            "foo = POINTER(CFUNCTYPE(c_int, c_int, c_byte, c_uint, c_void_p))")
    x = ccore.from_db(defs[8])
    assert x._is_typedef
    assert (x.show(
        form="ctypes"
    ) == "foo2 = POINTER(CFUNCTYPE(POINTER(c_void_p*3), c_int, c_void_p))*2")
    x = ccore.from_db(defs[10])
    assert x._is_struct
    assert (x.show(form="ctypes") ==
            """struct__mystruct = type('struct__mystruct',(Structure,),{})

struct__mystruct._fields_ = [("I", myinteger),
                             ("tab", c_int*12),
                             ("p", c_ubyte*16),
                             ("s", POINTER(c_short)),
                             ("next", POINTER(struct__mystruct)),
                             ("func", foo),
                             ("bar", struct__bar*2)]""")
Exemplo n.º 2
0
def test_format_C(configfile, c_header):
    c = conf.Config(configfile)
    conf.config = c
    defs = list(parse(c_header, tag="test"))
    x = ccore.from_db(defs[0])
    assert x._is_macro
    assert x.show(form="C") == "#define MYCONST  0x10;"
    x = ccore.from_db(defs[5])
    assert x._is_typedef
    assert x.show(
        form="C") == "typedef int (*foo)(int, char, unsigned int, void *);"
    x = ccore.from_db(defs[8])
    assert x._is_typedef
    assert x.show(form="C") == "typedef void *(*(*foo2[2])(int, void **))[3];"
    x = ccore.from_db(defs[10])
    assert x._is_struct
    assert (x.show(form="C") == """struct _mystruct {
  myinteger I;
  int tab[12];
  unsigned char p[16];
  short *s;
  struct _mystruct *next;
  foo func;
  struct _bar bar[2];
};""")
Exemplo n.º 3
0
Arquivo: main.py Projeto: bdcht/ccrawl
def prototype(ctx, proto):
    """Get prototype definitions from the remote database
    (or the local database if no remote is found) matching
    constraints on name of its return type or specific
    arguments.
    """
    reqs = {}
    try:
        for p in proto:
            pos, t = p.split(':')
            pos = int(pos)
            reqs[pos] = c_type(t).show()
    except Exception:
        click.secho('invalid arguments', fg='red', err=True)
        return
    db = ctx.obj['db']
    Q = ctx.obj.get('select', Query().noop())
    L = db.search(Q, cls='cFunc')
    R = []
    with click.progressbar(L) as pL:
        for l in L:
            x = ccore.from_db(l)
            P = [c_type(t).show() for t in x.argtypes()]
            P.insert(0, c_type(x.restype()).show())
            if max(reqs) >= len(P): continue
            if not all(((t == P[i]) for (i, t) in reqs.items())):
                continue
            R.append(x.show(db, form='C'))
    if R:
        click.echo('\n'.join(R))
Exemplo n.º 4
0
def test_format_C(configfile,c_header):
    c = conf.Config(configfile)
    conf.config = c
    defs = list(parse(c_header,tag='test'))
    x = ccore.from_db(defs[0])
    assert x._is_macro
    assert x.show(form='C') == '#define MYCONST  0x10;'
    x = ccore.from_db(defs[5])
    assert x._is_typedef
    assert x.show(form='C') == 'typedef int (*foo)(int, char, unsigned int, void *);'
    x = ccore.from_db(defs[8])
    assert x._is_typedef
    assert x.show(form='C') == 'typedef void *(*(*foo2[2])(int, void **))[3];'
    x = ccore.from_db(defs[10])
    assert x._is_struct
    assert x.show(form='C') == """struct _mystruct {
Exemplo n.º 5
0
def test_format_amoco(configfile,c_header):
    c = conf.Config(configfile)
    conf.config = c
    defs = list(parse(c_header,tag='test'))
    x = ccore.from_db(defs[0])
    assert x._is_macro
    assert x.show(form='amoco') == 'MYCONST = 0x10'
    x = ccore.from_db(defs[5])
    assert x._is_typedef
    assert x.show(form='amoco') == "TypeDefine('foo','P')"
    x = ccore.from_db(defs[8])
    assert x._is_typedef
    assert x.show(form='amoco') == "TypeDefine('foo2','P * 2')"
    x = ccore.from_db(defs[10])
    assert x._is_struct
    assert x.show(form='amoco') == '@StructDefine("""\nmyinteger : I ;comment for field I\ni * 12 : tab ;modern comment for tab\ns * 16 : p ;\nP : s ;\nP : next ;\nfoo : func ;\nstruct__bar * 2 : bar ;\n""")\nclass struct__mystruct(StructFormatter):\n    def __init__(self,data="",offset=0):\n        if data: self.unpack(data,offset)\n    '
Exemplo n.º 6
0
def test_format_ctypes(configfile,c_header):
    c = conf.Config(configfile)
    conf.config = c
    defs = list(parse(c_header,tag='test'))
    x = ccore.from_db(defs[0])
    assert x._is_macro
    assert x.show(form='ctypes') == 'MYCONST = 16'
    x = ccore.from_db(defs[5])
    assert x._is_typedef
    assert x.show(form='ctypes') == 'foo = POINTER(CFUNCTYPE(c_int, c_int, c_byte, c_uint, c_void_p))'
    x = ccore.from_db(defs[8])
    assert x._is_typedef
    assert x.show(form='ctypes') == 'foo2 = POINTER(CFUNCTYPE(POINTER(c_void_p*3), c_int, c_void_p))*2'
    x = ccore.from_db(defs[10])
    assert x._is_struct
    assert x.show(form='ctypes') ==  """struct__mystruct = type('struct__mystruct',(Structure,),{})
Exemplo n.º 7
0
def test_parser_cxx(configfile,cxx_myclass):
    c = conf.Config(configfile)
    c.Terminal.quiet = True
    c.Terminal.timer = False
    c.Collect.strict = False
    c.Collect.cxx    = True
    conf.config = c
    defs = list(parse_string(cxx_myclass))
    assert len(defs)==1
    x = ccore.from_db(defs[0])
    assert x._is_class
    assert len(x)==11
    assert x[4] == (('', 'void (int)'),
                    ('_ZN7MyClassC1Ei', 'MyClass'),
                    ('PUBLIC', None))
Exemplo n.º 8
0
def test_parser_c(configfile,c_header):
    c = conf.Config(configfile)
    c.Terminal.quiet = True
    c.Terminal.timer = False
    c.Collect.strict = False
    c.Collect.cxx    = False
    conf.config = c
    defs = list(parse(c_header,tag='test'))
    assert defs[0]['cls'] == 'cMacro'
    assert defs[0]['id']  == 'MYCONST'
    assert defs[0]['tag'] == 'test'
    assert defs[0]['val'] == ' 0x10'
    x = ccore.from_db(defs[5])
    assert x._is_typedef
    assert str(x)=='int (*)(int, char, unsigned int, void *)'
Exemplo n.º 9
0
def test_parser_c(configfile, c_header):
    c = conf.Config(configfile)
    c.Terminal.quiet = True
    c.Terminal.timer = False
    c.Collect.strict = False
    c.Collect.cxx = False
    conf.config = c
    defs = list(parse(c_header, tag="test"))
    assert defs[0]["cls"] == "cMacro"
    assert defs[0]["id"] == "MYCONST"
    assert defs[0]["tag"] == "test"
    assert defs[0]["val"] == " 0x10"
    x = ccore.from_db(defs[5])
    assert x._is_typedef
    assert str(x) == "int (*)(int, char, unsigned int, void *)"
Exemplo n.º 10
0
Arquivo: main.py Projeto: bdcht/ccrawl
def show(ctx, form, recursive, identifier):
    """Print a definition
    from the remote database (or the local database if no remote is found) in
    C/C++ (default) format or other supported format (ctypes, amoco, raw).
    If the recursive option is used, the printed definitions include all
    other types required by the topmost definition.
    """
    db = ctx.obj['db']
    if recursive is True:
        recursive = set()
    Q = where('id') == identifier
    if db.contains(Q):
        for l in db.search(Q):
            x = ccore.from_db(l)
            click.echo(x.show(db, recursive, form=form))
    else:
        click.secho("identifier '%s' not found" % identifier,
                    fg='red',
                    err=True)
Exemplo n.º 11
0
Arquivo: main.py Projeto: bdcht/ccrawl
def store(ctx, update):
    """Update the remote database with definitions from the current local database.
    If the update option flag is set, the dependency graph of local definitions
    is computed before pushing definitions to the remote database.
    """
    db = ctx.obj['db']
    rdb = db.rdb
    #force all operations to occur on local database:
    db.rdb = None
    Done = []
    for l in db.search(db.tag):
        x = ccore.from_db(l)
        if not conf.QUIET:
            click.echo("unfolding '%s'..." % x.identifier, nl=False)
        try:
            l['use'] = list(x.unfold(db).subtypes.keys())
        except Exception:
            if not conf.QUIET:
                click.secho('failed.', fg='red')
        else:
            if not conf.QUIET:
                click.secho('ok.', fg='green')
            if update is True:
                db.ldb.update(l)
        Done.append(l)
    if rdb:
        if not conf.QUIET:
            click.echo('remote db insert multiple ...', nl=False)
        try:
            rdb.insert_multiple(Done)
        except Exception:
            if not conf.QUIET:
                click.secho('failed.', fg='red')
        else:
            if not conf.QUIET:
                click.secho('done.', fg='green')
            if not update:
                db.ldb.remove(doc_ids=[l.doc_id for l in Done])
    #restore remote database operations:
    db.rdb = rdb
Exemplo n.º 12
0
    def update_structs(self, proxydb, req=None):
        from ccrawl.core import ccore
        from ccrawl.ext import amoco

        col = self.db.get_collection("nodes")
        req = req or {}
        req.update({"cls": "cStruct"})
        s_32 = self.db["structs_ptr32"]
        s_64 = self.db["structs_ptr64"]
        for s in col.find(req):
            click.echo("updating {}".format(s["id"]))
            i = s["_id"]
            x = ccore.from_db(s)
            try:
                ax = amoco.build(x, proxydb)()
                off32 = ax.offsets(psize=4)
                tot32 = ax.size(psize=4)
                off64 = ax.offsets(psize=8)
                tot64 = ax.size(psize=8)
            except Exception:
                continue
            s_32.update_one(
                {"_id": i},
                {"$set": {
                    "_id": i,
                    "size": tot32,
                    "offsets": off32
                }},
                upsert=True,
            )
            s_64.update_one(
                {"_id": i},
                {"$set": {
                    "_id": i,
                    "size": tot64,
                    "offsets": off64
                }},
                upsert=True,
            )
Exemplo n.º 13
0
Arquivo: main.py Projeto: bdcht/ccrawl
def constant(ctx, mask, symbol, val):
    """Get constant definitions (macros or enums)
    from the remote database (or the local database if no remote is found) matching
    constraints on value (possibly representing a mask of several symbols) and
    symbol prefix.
    """
    value = int(val, 0)
    db = ctx.obj['db']
    Q = ctx.obj.get('select', Query().noop())
    Q &= ((where('cls') == 'cMacro') | (where('cls') == 'cEnum'))
    L = db.search(Q)
    R = []
    with click.progressbar(L) as pL:
        for l in pL:
            x = ccore.from_db(l)
            if x._is_macro:
                if not (symbol in x.identifier):
                    continue
                try:
                    v = int(x, 0)
                except Exception:
                    continue
                else:
                    if v == value:
                        R.append(x.identifier + '\n')
                    elif mask and (symbol in x.identifier):
                        if v < value and v & value:
                            R.append(x.identifier + ' | ')
            else:
                for k, v in x.items():
                    if v == value and (symbol in k):
                        R.append(k + '\n')
                        break
                    elif mask and (symbol in k):
                        if v < value and v & value:
                            R.append(k + ' | ')
    if R:
        s = ''.join(R)
        click.echo(s.strip(' |\n'))
Exemplo n.º 14
0
Arquivo: main.py Projeto: bdcht/ccrawl
def info(ctx, identifier):
    """Get database internal informations about a definition.
    """
    db = ctx.obj['db']
    Q = where('id') == identifier
    if db.contains(Q):
        for l in db.search(Q):
            x = ccore.from_db(l)
            click.echo("identifier: {}".format(identifier))
            click.secho("class     : {}".format(l['cls']), fg='cyan')
            click.echo("source    : {}".format(l['src']))
            click.secho("tag       : {}".format(l['tag']), fg='magenta')
            if x._is_struct or x._is_union or x._is_class:
                try:
                    t = x.build(db)
                except (TypeError, KeyError) as e:
                    what = e.args[0]
                    click.secho("can't build %s:\nmissing type: '%s'" %
                                (x.identifier, what),
                                fg='red',
                                err=True)
                    click.echo('', err=True)
                    continue
                F = []
                for i, f in enumerate(t._fields_):
                    field = getattr(t, f[0])
                    F.append((field.offset, field.size))
                xsize = F[-1][0] + F[-1][1]
                click.secho("size      : {}".format(xsize), fg='yellow')
                click.secho("offsets   : {}".format([(f[0], f[1]) for f in F]),
                            fg='yellow')

    else:
        click.secho("identifier '%s' not found" % identifier,
                    fg='red',
                    err=True)
Exemplo n.º 15
0
Arquivo: main.py Projeto: bdcht/ccrawl
def struct(ctx, pdef, conds):
    """Get structured definitions (struct, union or class)
    from the remote database (or the local database if no remote is found) matching
    constraints on total size or specific type name or size at given offset within
    the structure.
    """
    reqs = {}
    try:
        for p in conds:
            off, t = p.split(':')
            if off == '*':
                sz = int(t)
                reqs[off] = sz
            else:
                off = int(off)
                if t[0] == '+':
                    reqs[off] = int(t)
                elif t[0] == '?':
                    reqs[off] = t
                else:
                    reqs[off] = c_type(t)
    except Exception:
        click.secho('invalid arguments', fg='red', err=True)
        return
    db = ctx.obj['db']
    Q = ctx.obj.get('select', Query().noop())
    L = db.search(Q
                  & ((where('cls') == 'cStruct') | (where('cls') == 'cClass')))
    R = []
    fails = []
    with click.progressbar(L) as pL:
        for l in pL:
            x = ccore.from_db(l)
            ctcls = c_type
            try:
                if x._is_class:
                    x = x.as_cStruct(db)
                t = x.build(db)
            except Exception:
                fails.append("can't build %s" % x.identifier)
                continue
            F = []
            for i, f in enumerate(t._fields_):
                field = getattr(t, f[0])
                F.append((field.offset, field.size, ctcls(x[i][0])))
            if F:
                xsize = F[-1][0] + F[-1][1]
                if '*' in reqs and reqs['*'] != xsize: continue
                F = dict(((f[0], f[1:3]) for f in F))
                ok = []
                for o, s in reqs.items():
                    if o == '*': continue
                    cond = (o in F)
                    ok.append(cond)
                    if not cond: break
                    if s == '?': continue
                    if s == '*': cond = (F[o][1].is_ptr)
                    elif isinstance(s, c_type):
                        cond = (F[o][1].show() == s.show())
                    else:
                        cond = (F[o][0] == s)
                    ok.append(cond)
                    if not cond: break
                if all(ok):
                    if not pdef: res = x.identifier
                    else: res = x.show(db, form='C')
                    R.append(res)
    if conf.VERBOSE:
        click.secho(u'\n'.join(fails), fg='red', err=True)
    if R:
        click.echo('\n'.join(R))