コード例 #1
0
def create_dissector(filename, platform, folders=None, includes=None):
    """Parse 'filename' to create a Wireshark protocol dissector.

    'filename' is the C header/code file to parse.
    'platform' is the platform we should simulate.
    'folders' is a set of all folders to -Include.
    'includes' is a set of filenames to #include.
    Returns the error if parsing failed, None if succeeded.
    """
    try:
        text = cpp.parse_file(filename, platform, folders, includes)
        ast = cparser.parse(text, filename)
        cparser.find_structs(ast, platform)
    except OSError:
        raise
    except Exception as err:
        # Clean up a bit
        try:
            del cparser.StructVisitor._last_proto.dissectors[platform]
            del cparser.StructVisitor._last_visitor
            del cparser.StructVisitor._last_diss
            del cparser.StructVisitor._last_proto
        except Exception:
            pass

        if Options.verbose:
            print('Failed "%s":%s which raised %s' %
                  (filename, platform.name, repr(err)))
        if Options.debug:
            sys.excepthook(*sys.exc_info())
        return err

    if Options.verbose:
        print("Parsed header file '%s':%s successfully." %
              (filename, platform.name))
コード例 #2
0
ファイル: test_cparser.py プロジェクト: Rad0x6f/kpro9
def parse_error():
    """Test that two structs with the same name raises an error."""
    code = 'struct a {int c;}; \nstruct b { int d; \nstruct a {int d;}; };'
    ast = cparser.parse(code, 'test')
    with contexts.raises(cparser.ParseError) as error:
        cparser.find_structs(ast)
    assert str(error).startswith('Two structs with same name a')
コード例 #3
0
ファイル: csjark.py プロジェクト: eventh/kpro9
def create_dissector(filename, platform, folders=None, includes=None):
    """Parse 'filename' to create a Wireshark protocol dissector.

    'filename' is the C header/code file to parse.
    'platform' is the platform we should simulate.
    'folders' is a set of all folders to -Include.
    'includes' is a set of filenames to #include.
    Returns the error if parsing failed, None if succeeded.
    """
    try:
        text = cpp.parse_file(filename, platform, folders, includes)
        ast = cparser.parse(text, filename)
        cparser.find_structs(ast, platform)
    except OSError:
        raise
    except Exception as err:
        # Clean up a bit
        try:
            del cparser.StructVisitor._last_proto.dissectors[platform]
            del cparser.StructVisitor._last_visitor
            del cparser.StructVisitor._last_diss
            del cparser.StructVisitor._last_proto
        except Exception:
            pass

        if Options.verbose:
            print('Failed "%s":%s which raised %s' % (filename, platform.name, repr(err)))
        if Options.debug:
            sys.excepthook(*sys.exc_info())
        return err

    if Options.verbose:
        print("Parsed header file '%s':%s successfully." % (filename, platform.name))
コード例 #4
0
def parse_error():
    """Test that two structs with the same name raises an error."""
    code = 'struct a {int c;}; \nstruct b { int d; \nstruct a {int d;}; };'
    ast = cparser.parse(code, 'test')
    with contexts.raises(cparser.ParseError) as error:
        cparser.find_structs(ast)
    assert str(error).startswith('Two structs with same name a')
コード例 #5
0
ファイル: requirements.py プロジェクト: Rad0x6f/kpro9
def req_1f():
    """Test requirement FR1-F: Detect same name structs."""
    cparser.StructVisitor.all_protocols = {}
    code = 'struct a {int c;};\nstruct b { int d;\nstruct a {int d;}; };'
    ast = cparser.parse(code, 'test')
    with contexts.raises(cparser.ParseError) as error:
        cparser.find_structs(ast)
    assert str(error).startswith('Two structs with same name a')
    cparser.StructVisitor.all_protocols = {}
コード例 #6
0
def req_1f():
    """Test requirement FR1-F: Detect same name structs."""
    cparser.StructVisitor.all_protocols = {}
    code = 'struct a {int c;};\nstruct b { int d;\nstruct a {int d;}; };'
    ast = cparser.parse(code, 'test')
    with contexts.raises(cparser.ParseError) as error:
        cparser.find_structs(ast)
    assert str(error).startswith('Two structs with same name a')
    cparser.StructVisitor.all_protocols = {}
コード例 #7
0
ファイル: requirements.py プロジェクト: Rad0x6f/kpro9
def req_1b():
    """Test requirement FR1-B: Support enums."""
    cparser.StructVisitor.all_protocols = {}
    ast = cparser.parse('enum c {a, b=3}; struct req { enum c test; };')
    assert isinstance(_child(ast.children()[1], 4), c_ast.Enum)
    enum = list(cparser.find_structs(ast)[0].dissectors.values())[0].children[0]
    assert enum
    assert enum.name == 'test'
    assert enum._valuestring_values == '{[0]="a", [3]="b"}'
    assert enum.type == 'uint32' and enum.size == 4
コード例 #8
0
def req_1b():
    """Test requirement FR1-B: Support enums."""
    cparser.StructVisitor.all_protocols = {}
    ast = cparser.parse('enum c {a, b=3}; struct req { enum c test; };')
    assert isinstance(_child(ast.children()[1], 4), c_ast.Enum)
    enum = list(
        cparser.find_structs(ast)[0].dissectors.values())[0].children[0]
    assert enum
    assert enum.name == 'test'
    assert enum._valuestring_values == '{[0]="a", [3]="b"}'
    assert enum.type == 'uint32' and enum.size == 4
コード例 #9
0
ファイル: requirements.py プロジェクト: Rad0x6f/kpro9
def req_1e():
    """Test requirement FR1-E: Support arrays."""
    cparser.StructVisitor.all_protocols = {}
    ast = cparser.parse('struct req1e {int a[8][7]; char b[9]; float c[5];};')
    a, b, c = [_child(i, 1) for i in _child(ast, 2).children()]
    assert isinstance(b, c_ast.ArrayDecl) and isinstance(c, c_ast.ArrayDecl)
    assert isinstance(_child(a, 1), c_ast.ArrayDecl)
    assert _child(a, 2).declname == 'a'
    assert _child(a, 3).names[0] == 'int'
    a, b, c = list(cparser.find_structs(ast)[0].dissectors.values())[0].children
    assert isinstance(b, dissector.Field)
    assert a.type == 'bytes' and b.type == 'string' and c.type == 'bytes'
    assert a.size == 4*56 and b.size == 9 and c.size == 20
コード例 #10
0
def req_1e():
    """Test requirement FR1-E: Support arrays."""
    cparser.StructVisitor.all_protocols = {}
    ast = cparser.parse('struct req1e {int a[8][7]; char b[9]; float c[5];};')
    a, b, c = [_child(i, 1) for i in _child(ast, 2).children()]
    assert isinstance(b, c_ast.ArrayDecl) and isinstance(c, c_ast.ArrayDecl)
    assert isinstance(_child(a, 1), c_ast.ArrayDecl)
    assert _child(a, 2).declname == 'a'
    assert _child(a, 3).names[0] == 'int'
    a, b, c = list(
        cparser.find_structs(ast)[0].dissectors.values())[0].children
    assert isinstance(b, dissector.Field)
    assert a.type == 'bytes' and b.type == 'string' and c.type == 'bytes'
    assert a.size == 4 * 56 and b.size == 9 and c.size == 20
コード例 #11
0
ファイル: test_cparser.py プロジェクト: Rad0x6f/kpro9
def create_structs():
    """Creates an AST and finds all structs within it."""
    code = '''
    struct inner {
        int a1;
        int a2;
        int a3;
    };
    enum color { RED, GREEN=3, YELLOW, RED=10 };
    typedef enum weekday { MON, TUE, WED, FRI=5 } weekday_t;
    struct find {
        int a; float b; char c;
        enum color enumtest;
        char str[30]; float d[3];
        struct inner inner_struct;
        unsigned short oprs[+2][9-7][((5 * 3) + (-5)) / 5];
        weekday_t day;
        long double bytes;
    };
    '''
    ast = cparser.parse(code, 'test')
    structs = {i.name: i for i in cparser.find_structs(ast)}
    yield list(structs['find'].dissectors.values())[0].children
    cparser.StructVisitor.all_protocols = {}
コード例 #12
0
def create_structs():
    """Creates an AST and finds all structs within it."""
    code = '''
    struct inner {
        int a1;
        int a2;
        int a3;
    };
    enum color { RED, GREEN=3, YELLOW, RED=10 };
    typedef enum weekday { MON, TUE, WED, FRI=5 } weekday_t;
    struct find {
        int a; float b; char c;
        enum color enumtest;
        char str[30]; float d[3];
        struct inner inner_struct;
        unsigned short oprs[+2][9-7][((5 * 3) + (-5)) / 5];
        weekday_t day;
        long double bytes;
    };
    '''
    ast = cparser.parse(code, 'test')
    structs = {i.name: i for i in cparser.find_structs(ast)}
    yield list(structs['find'].dissectors.values())[0].children
    cparser.StructVisitor.all_protocols = {}