示例#1
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    '
示例#2
0
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:
        click.secho('invalid arguments', fg='red', err=True)
        return
    db = ctx.obj['db']
    Q = ctx.obj.get('select', Query())
    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))
示例#3
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 {
示例#4
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,),{})
示例#5
0
class Select_Prototype(Resource):
    def get(self):
        return {
            "verbose": False,
            "tag": "",
            "key": "",
            "match": "",
            "format": "C",
            "proto": "",
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("verbose", type=bool)
        parser.add_argument("tag")
        parser.add_argument("key")
        parser.add_argument("match")
        parser.add_argument("proto")
        parser.add_argument("format")
        args = parser.parse_args()
        db = g_ctx.obj["db"]
        if args["tag"]:
            db.set_tag(args["tag"])
        if verbose := args["verbose"]:
            keys = ("src", "tag")
        if args["key"] and args["match"]:
            Q = where(args["key"]).matches(args["match"])
        else:
            Q = Query().noop()
        proto = args["proto"].split(";")
        fmt = args.get("format", "C")
        reqs = {}
        try:
            for p in proto:
                pos, t = p.split(":")
                pos = int(pos)
                reqs[pos] = c_type(t).show()
        except Exception:
            return abort(400, reason="bad prototype request")
        L = []
        for l in db.search(Q & (where("cls") == "cFunc")):
            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
            d = {"id": l["id"], "val": x.show(db, form=fmt)}
            if verbose:
                for k in keys:
                    d[k] = l[k]
            L.append(d)
        return L
示例#6
0
class Select_Prototype(Resource):
    def get(self):
        return {
            'verbose': False,
            'tag': '',
            'key': '',
            'match': '',
            'format': 'C',
            'proto': ''
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('verbose', type=bool)
        parser.add_argument('tag')
        parser.add_argument('key')
        parser.add_argument('match')
        parser.add_argument('proto')
        parser.add_argument('format')
        args = parser.parse_args()
        db = g_ctx.obj['db']
        if args['tag']:
            db.set_tag(args['tag'])
        if verbose := args['verbose']:
            keys = ('src', 'tag')
        if args['key'] and args['match']:
            Q = where(args['key']).matches(args['match'])
        else:
            Q = Query()
        proto = args['proto'].split(';')
        fmt = args.get('format', 'C')
        reqs = {}
        try:
            for p in proto:
                pos, t = p.split(':')
                pos = int(pos)
                reqs[pos] = c_type(t).show()
        except Exception:
            return abort(400, reason="bad prototype request")
        L = []
        for l in db.search(Q & (where('cls') == 'cFunc')):
            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
            d = {'id': l['id'], 'val': x.show(db, form=fmt)}
            if verbose:
                for k in keys:
                    d[k] = l[k]
            L.append(d)
        return L
示例#7
0
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)
示例#8
0
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:
            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:
            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
示例#9
0
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())
    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:
                    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'))
示例#10
0
class Show(Resource):
    def get(self):
        return {
            "verbose": False,
            "tag": "",
            "recursive": False,
            "fmt": "C",
            "identifier": "",
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("verbose", type=bool)
        parser.add_argument("recursive", type=bool)
        parser.add_argument("tag")
        parser.add_argument("fmt")
        parser.add_argument("identifier")
        args = parser.parse_args()
        db = g_ctx.obj["db"]
        if args["tag"]:
            db.set_tag(args["tag"])
        if verbose := args["verbose"]:
            keys = ("cls", "tag", "src", "use")
        fmt = args["fmt"] or "C"
        recursive = args["recursive"]
        identifier = args["identifier"]
        Q = where("id") == identifier
        L = []
        for l in db.search(Q):
            x = ccore.from_db(l)
            d = {"id": l["id"], "val": x.show(db, recursive, form=fmt)}
            if verbose:
                for k in keys:
                    d[k] = l[k]
            L.append(d)
        return L
示例#11
0
class Show(Resource):
    def get(self):
        return {
            'verbose': False,
            'tag': '',
            'recursive': False,
            'fmt': 'C',
            'identifier': ''
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('verbose', type=bool)
        parser.add_argument('recursive', type=bool)
        parser.add_argument('tag')
        parser.add_argument('fmt')
        parser.add_argument('identifier')
        args = parser.parse_args()
        db = g_ctx.obj['db']
        if args['tag']:
            db.set_tag(args['tag'])
        if verbose := args['verbose']:
            keys = ('cls', 'tag', 'src', 'use')
        fmt = args['fmt'] or 'C'
        recursive = args['recursive']
        identifier = args['identifier']
        Q = where('id') == identifier
        L = []
        for l in db.search(Q):
            x = ccore.from_db(l)
            d = {'id': l['id'], 'val': x.show(db, recursive, form=fmt)}
            if verbose:
                for k in keys:
                    d[k] = l[k]
            L.append(d)
        return L
示例#12
0
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)
示例#13
0
class Select_Struct(Resource):
    def get(self):
        return {
            'verbose': False,
            'tag': '',
            'key': '',
            'match': '',
            'def': False,
            'format': 'C',
            'conds': ''
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('verbose', type=bool)
        parser.add_argument('tag')
        parser.add_argument('key')
        parser.add_argument('match')
        parser.add_argument('def', type=bool)
        parser.add_argument('format')
        parser.add_argument('conds')
        args = parser.parse_args()
        db = g_ctx.obj['db']
        if args['tag']:
            db.set_tag(args['tag'])
        if verbose := args['verbose']:
            keys = ('src', 'tag')
        if args['key'] and args['match']:
            Q = where(args['key']).matches(args['match'])
        else:
            Q = Query()
        fmt = args['format'] or 'C'
        conds = args['conds'].split(';')
        pdef = args['def']
        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:
            abort(400, reason="invalid constraints")
        L = []
        for l in db.search(Q & (
            (where('cls') == 'cStruct') | (where('cls') == 'cClass'))):
            x = ccore.from_db(l)
            out = ''
            ctcls = c_type
            try:
                if x._is_class:
                    x = x.as_cStruct(db)
                t = x.build(db)
            except:
                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: out = x.identifier
                    else: out = x.show(db, form=fmt)
            if out:
                d = {'val': out}
                if verbose:
                    for k in keys:
                        d[k] = l[k]
                L.append(d)
        return L
示例#14
0
class Select_Struct(Resource):
    def get(self):
        return {
            "verbose": False,
            "tag": "",
            "key": "",
            "match": "",
            "def": False,
            "format": "C",
            "conds": "",
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("verbose", type=bool)
        parser.add_argument("tag")
        parser.add_argument("key")
        parser.add_argument("match")
        parser.add_argument("def", type=bool)
        parser.add_argument("format")
        parser.add_argument("conds")
        args = parser.parse_args()
        db = g_ctx.obj["db"]
        if args["tag"]:
            db.set_tag(args["tag"])
        if verbose := args["verbose"]:
            keys = ("src", "tag")
        if args["key"] and args["match"]:
            Q = where(args["key"]).matches(args["match"])
        else:
            Q = Query().noop()
        fmt = args["format"] or "C"
        conds = args["conds"].split(";")
        pdef = args["def"]
        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:
            abort(400, reason="invalid constraints")
        L = []
        for l in db.search(Q & (
            (where("cls") == "cStruct") | (where("cls") == "cClass"))):
            x = ccore.from_db(l)
            out = ""
            ctcls = c_type
            try:
                if x._is_class:
                    x = x.as_cStruct(db)
                t = x.build(db)
            except Exception:
                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:
                        out = x.identifier
                    else:
                        out = x.show(db, form=fmt)
            if out:
                d = {"val": out}
                if verbose:
                    for k in keys:
                        d[k] = l[k]
                L.append(d)
        return L
示例#15
0
class Select_Constant(Resource):
    def get(self):
        return {
            "verbose": False,
            "tag": "",
            "key": "",
            "match": "",
            "mask": False,
            "prefix": "",
            "val": "",
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("verbose", type=bool)
        parser.add_argument("tag")
        parser.add_argument("key")
        parser.add_argument("match")
        parser.add_argument("mask", type=bool)
        parser.add_argument("prefix")
        parser.add_argument("val")
        args = parser.parse_args()
        db = g_ctx.obj["db"]
        if args["tag"]:
            db.set_tag(args["tag"])
        if verbose := args["verbose"]:
            keys = ("src", "tag")
        if args["key"] and args["match"]:
            Q = where(args["key"]).matches(args["match"])
        else:
            Q = Query().noop()
        try:
            value = int(args["val"], 0)
        except (ValueError, TypeError):
            abort(400, reason="invalid value")
        mask = args["mask"]
        pfx = args["prefix"] or ""
        Q &= (where("cls") == "cMacro") | (where("cls") == "cEnum")
        L = []
        for l in db.search(Q):
            x = ccore.from_db(l)
            out = ""
            if x._is_macro:
                if pfx not in x.identifier:
                    continue
                try:
                    v = int(x, 0)
                except Exception:
                    continue
                else:
                    if v == value:
                        out = x.identifier
                    elif mask and (pfx in x.identifier):
                        if v < value and v & value:
                            out = x.identifier + " | "
            else:
                for k, v in x.items():
                    if v == value and (pfx in k):
                        out = k
                        break
                    elif mask and (pfx in k):
                        if v < value and v & value:
                            out = k + " | "
            if out:
                d = {"val": out}
                if verbose:
                    for k in keys:
                        d[k] = l[k]
                L.append(d)
        return L
示例#16
0
class Select_Constant(Resource):
    def get(self):
        return {
            'verbose': False,
            'tag': '',
            'key': '',
            'match': '',
            'mask': False,
            'prefix': '',
            'val': ''
        }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('verbose', type=bool)
        parser.add_argument('tag')
        parser.add_argument('key')
        parser.add_argument('match')
        parser.add_argument('mask', type=bool)
        parser.add_argument('prefix')
        parser.add_argument('val')
        args = parser.parse_args()
        db = g_ctx.obj['db']
        if args['tag']:
            db.set_tag(args['tag'])
        if verbose := args['verbose']:
            keys = ('src', 'tag')
        if args['key'] and args['match']:
            Q = where(args['key']).matches(args['match'])
        else:
            Q = Query()
        try:
            value = int(args['val'], 0)
        except (ValueError, TypeError):
            abort(400, reason="invalid value")
        mask = args['mask']
        pfx = args['prefix'] or ''
        Q &= (where('cls') == 'cMacro') | (where('cls') == 'cEnum')
        L = []
        for l in db.search(Q):
            x = ccore.from_db(l)
            out = ''
            if x._is_macro:
                if (pfx not in x.identifier):
                    continue
                try:
                    v = int(x, 0)
                except Exception:
                    continue
                else:
                    if v == value:
                        out = x.identifier
                    elif mask and (pfx in x.identifier):
                        if v < value and v & value:
                            out = x.identifier + ' | '
            else:
                for k, v in x.items():
                    if v == value and (pfx in k):
                        out = k
                        break
                    elif mask and (pfx in k):
                        if v < value and v & value:
                            out = k + ' | '
            if out:
                d = {'val': out}
                if verbose:
                    for k in keys:
                        d[k] = l[k]
                L.append(d)
        return L
示例#17
0
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:
        click.secho('invalid arguments', fg='red', err=True)
        return
    db = ctx.obj['db']
    Q = ctx.obj.get('select', Query())
    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:
                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))