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))
def select(ctx, ands, ors): """Get document(s) from the remote database (or the local database if no remote is found) matching multiple constraints. """ Q = Query().noop() try: for x in ands: k, v = x.split('=') Q &= (where(k).search(v)) for x in ors: k, v = x.split('=') Q |= (where(k).search(v)) except Exception: click.secho('invalid options (ignored)', fg='yellow', err=True) ctx.obj['select'] = Q if ctx.invoked_subcommand is None: db = ctx.obj['db'] L = db.search(Q) for l in L: click.echo('found ', nl=False) click.secho('%s ' % l['cls'], nl=False, fg='cyan') click.echo('identifer ', nl=False) click.secho('"%s"' % l['id'], fg='magenta') else: if conf.DEBUG: click.echo('FIND_COMMAND: %s' % ctx.invoked_subcommand)
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
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
def test_from_db_2(db_doc2): x = ccore.from_db(data=db_doc2[0]) assert x._is_struct assert x.identifier == "struct X" assert x.subtypes is None Q = Query().noop() class DB(object): def get(self, id): if isinstance(id, type(Q)): id = id._hash[-1] if id == "yyyy": return db_doc2[1] x.unfold(DB()) assert "yyyy" in x.subtypes y = x.subtypes["yyyy"] assert y._is_typedef
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'))
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
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
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
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
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))