Example #1
0
def init_parser():
    """Set up the parser with the grammar to be recognized.
    """
    # CORE
    digit   = h.ch_range(0x30, 0x39)
    alpha   = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
    space   = h.in_(" \t\n\r\f\v")

    # AUX.
    plus    = h.ch('+')
    slash   = h.ch('/')
    equals  = h.ch('=')

    bsfdig      = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_("AEIMQUYcgkosw048")
    bsfdig_2bit = h.in_("AQgw")
    base64_3    = h.repeat_n(bsfdig, 4)
    base64_2    = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1    = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64      = h.action(h.sequence(h.many(base64_3),
                                      h.optional(h.choice(base64_2,
                                                          base64_1))),
                           act_base64)

    # TODO This is not quite the same as the C example, with uses act_ignore.
    #      But I can't get hammer to filter any value returned by act_ignore.
    ws          = h.ignore(h.many(space))
    document    = h.action(h.sequence(ws, base64, ws, h.end_p()),
                           act_document)

    # BUG sometimes inputs that should just don't parse.
    # It *seemed* to happen mostly with things like "bbbbaaaaBA==".
    # Using less actions seemed to make it less likely.

    return document
Example #2
0
def init_parser():
    """Set up the parser with the grammar to be recognized.
    """
    # CORE
    digit = h.ch_range(0x30, 0x39)
    alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
    space = h.in_(b" \t\n\r\f\v")

    # AUX.
    plus = h.ch(b'+')
    slash = h.ch(b'/')
    equals = h.ch(b'=')

    bsfdig = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_(b"AEIMQUYcgkosw048")
    bsfdig_2bit = h.in_(b"AQgw")
    base64_3 = h.repeat_n(bsfdig, 4)
    base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64 = h.action(
        h.sequence(h.many(base64_3), h.optional(h.choice(base64_2, base64_1))),
        act_base64)

    # TODO This is not quite the same as the C example, with uses act_ignore.
    #      But I can't get hammer to filter any value returned by act_ignore.
    ws = h.ignore(h.many(space))
    document = h.action(h.sequence(ws, base64, ws, h.end_p()), act_document)

    # BUG sometimes inputs that should just don't parse.
    # It *seemed* to happen mostly with things like "bbbbaaaaBA==".
    # Using less actions seemed to make it less likely.

    return document
Example #3
0
def relay_operate_configuration():
    header = h.token('\xa5\xce')
    length = h.uint8()
    breaker_bits = h.uint8()
    remote_bits = h.uint16()
    remote_pulse = h.uint8()
    reserved = h.uint8()

    parser = h.sequence(
        header, length, breaker_bits, remote_bits, remote_pulse, reserved,
        h.repeat_n(h.uint8(), 2),
        h.repeat_n(h.sequence(h.uint8(), h.uint8(), h.uint8()), 32), h.uint8())
    return parser
Example #4
0
def fast_meter_message():
    header = h.token('\xa5\xd1')
    any_char = h.ch_range('\x00', '\xff')

    parser = h.sequence(header, h.uint8(), h.ch('\x00'),
                        h.repeat_n(h.uint32(), 7), h.uint64(),
                        h.repeat_n(any_char, 167), any_char)
    return parser
Example #5
0
def relay_definition_message():
    header = h.token('\xa5\xc0')
    any_char = h.ch_range('\x00', '\xff')

    parser = h.sequence(header, h.uint8(), h.uint8(), h.uint8(), h.uint8(),
                        h.many1(any_char))

    return parser
Example #6
0
def init_parser():
    return h.sequence(
        h.many1(
            h.choice(relay_definition_message(),
                     fast_meter_configuration_block(), fast_meter_message(),
                     demand_fast_meter_configuration_block(),
                     peak_fast_meter_configuration_block(),
                     fast_message_block(), relay_operate_configuration())),
        h.end_p())
Example #7
0
def init_parser():
    """Return a parser with the grammar to be recognized.
    """
    # CORE

    # This is a direct translation of the  C example. In C the literal 0x30
    # is interchangable with the char literal '0' (note the single quotes).
    # This is not the case in Python.
    
    # TODO In the interests of being more Pythonic settle on either string
    #      literals, or integers
    digit   = h.ch_range(0x30, 0x39)
    alpha   = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
    space   = h.in_(" \t\n\r\f\v")

    # AUX.
    plus    = h.ch('+')
    slash   = h.ch('/')
    equals  = h.action(h.ch('='), act_equals)

    bsfdig      = h.action(h.choice(alpha, digit, plus, slash), act_bsfdig)
    bsfdig_4bit = h.action(h.in_("AEIMQUYcgkosw048"), act_bsfdig_4bit)
    bsfdig_2bit = h.action(h.in_("AQgw"), act_bsfdig_2bit)
    base64_3    = h.action(h.repeat_n(bsfdig, 4), act_base64_3)
    base64_2    = h.action(h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals),
                           act_base64_2)
    base64_1    = h.action(h.sequence(bsfdig, bsfdig_2bit, equals, equals),
                           act_base64_1)
    base64      = h.action(h.sequence(h.many(base64_3),
                                      h.optional(h.choice(base64_2,
                                                          base64_1))),
                           act_base64)

    # TODO This is not quite the same as the C example, with uses act_ignore.
    #      But I can't get hammer to filter any value returned by act_ignore.
    ws          = h.ignore(h.many(space))
    document    = h.action(h.sequence(ws, base64, ws, h.end_p()),
                           act_document)

    # BUG sometimes inputs that should just don't parse.
    # It *seemed* to happen mostly with things like "bbbbaaaaBA==".
    # Using less actions seemed to make it less likely.

    return document
Example #8
0
def init_parser():
    return h.sequence(
        h.many1(
            h.choice(
                #sampleEvents_parser,
                #pollingEngine_parser,
                #envelope_parser,
                #measurementDevice_parser,
                #sensors_parser,
                schemaVersion_parser())))
Example #9
0
def connection_start_ok_parser():
    parser = h.sequence(
        h.ch('\x01'),  # type
        h.uint16(),  # Channel
        h.uint32(),  # Length
        h.token("\x00\x0a"),  # Class
        h.token("\x00\x0b"),  # Method
        h.end_p()
    )
    return parser
Example #10
0
def basic_deliver_parser():
    parser = h.sequence(
        h.ch('\x01'),  # type
        h.uint16(),  # Channel
        h.uint32(),  # Length
        h.token("\x00\x3c"),  # Class
        h.token("\x00\x3c"),  # Method
        h.end_p()
    )
    return parser
Example #11
0
def init_parser():
    # CORE
    digit = h.ch_range(0x30, 0x39)
    alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))

    # AUX.
    plus = h.ch('+')
    slash = h.ch('/')
    equals = h.ch('=')

    bsfdig = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_('AEIMQUYcgkosw048')
    bsfdig_2bit = h.in_('AQgw')
    base64_3 = h.repeat_n(bsfdig, 4)
    base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64 = h.sequence(h.many(base64_3),
                        h.optional(h.choice(base64_2, base64_1)))

    return h.sequence(h.whitespace(base64), h.whitespace(h.end_p()))
Example #12
0
def init_parser():
    # CORE
    digit = h.ch_range(0x30, 0x39)
    alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))

    # AUX.
    plus = h.ch(b'+')
    slash = h.ch(b'/')
    equals = h.ch(b'=')

    bsfdig = h.choice(alpha, digit, plus, slash)
    bsfdig_4bit = h.in_(b'AEIMQUYcgkosw048')
    bsfdig_2bit = h.in_(b'AQgw')
    base64_3 = h.repeat_n(bsfdig, 4)
    base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
    base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)
    base64 = h.sequence(h.many(base64_3),
                        h.optional(h.choice(base64_2, base64_1)))

    return h.sequence(h.whitespace(base64), h.whitespace(h.end_p()))
Example #13
0
def length_block():


    parser = h.sequence(
    h.ch('\x01'),
    h.uint16(),
    h.and_(h._h_length_value(h.uint32(), h.ch_range('\x00', '\xff'))),
    h.uint32(),
    h.many(h.ch_range('\x00', '\xff')),
    h.end_p())
    return parser
Example #14
0
def connection_close_ok_parser():
    parser = h.sequence(
        h.ch('\x01'),  # type
        h.uint16(),  # Channel
        h.uint32(),  # Length
        h.token("\x00\x0a"),  # Class
        h.token("\x00\x33"),  # Method

        # No arguments
        h.end_p()
    )
    return parser
Example #15
0
def connection_open_vhost_ok_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x00\x0a"), # Class
        h.token("\x00\x29"), # Method

        # Arguments
        h.ch('\x00'),# Known-Host # most likely going to change this according to the message
        h.end_p()
    )
    return parser
Example #16
0
def basic_consume_ok_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x00\x3c"), # Class
        h.token("\x00\x15"), # Method

        # Arguments
        # consumer-tag
        h.end_p()
    )
    return parser
Example #17
0
def channel_open_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x00\x14"), # Class
        h.token("\x00\x0a"), # Method

        # Arguments
        h.ch('\x00'), # Out-of-bounds
        h.end_p()
    )
    return parser
Example #18
0
def channel_open_ok_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x16\x28"), # Class
        h.token("\x00\x0b"), # Method

        # Arguments
        h.uint32(), # Channel-Id
        h.end_p()
    )
    return parser
Example #19
0
def fast_message_block():
    header = h.token('\xa5\x46')
    length = h.uint8()
    routing_address = h.repeat_n(h.uint8(), 5)
    status = h.uint8()
    function_code = h.uint8()
    sequence_byte = h.uint8()
    response_number = h.uint8()
    any_value = h.many1(h.ch_range('\x00', '\xff'))
    crc = h.uint16()

    parser = h.sequence(header, length, routing_address, status, function_code,
                        sequence_byte, response_number, any_value)

    return parser
Example #20
0
def connection_tune_ok_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x00\x0a"), # Class
        h.token("\x00\x1f"), # Method

        # Arguments
        h.uint16(), # Channel max
        h.uint32(), # Frame max
        h.uint16(), # Heartbeat
        h.end_p()
    )
    return parser
Example #21
0
def connection_close_parser():
    parser = h.sequence(
        h.ch('\x01'),  # type
        h.uint16(),  # Channel
        h.uint32(),  # Length
        h.token("\x00\x0a"),  # Class
        h.token("\x00\x32"),  # Method

        # Arguments
        h.uint16(), # Replay-code
        #Replay-text
        h.uint16(), # Class-Id
        h.uint16(), # Method-Id
        h.end_p()
    )
    return parser
Example #22
0
def queue_declare_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x00\x32"), # Class
        h.token("\x00\x0a"), # Method

        # Arguments
        h.uint16(), # Ticket
        # how do you parse that long message?#Queue
        h.many(h.ch_range('\x00', '\xff')),
        #When you parse this out... (, ,, , 0L, ('','','',''))
        h.end_p()
    )
    return parser
Example #23
0
def basic_consume_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x00\x3c"), # Class
        h.token("\x00\x14"), # Method

        # Arguments
        h.uint16(), # Ticket
        # how do you parse that long message? #Queue
        h.many(h.ch_range('\x00', '\xff')),
        # consumer-tag
        h.end_p()
    )
    return parser
Example #24
0
def queue_declare_ok_parser():
    parser = h.sequence(
        h.ch('\x01'), # type
        h.uint16(), # Channel
        h.uint32(), # Length
        h.token("\x00\x32"), # Class
        h.token("\x00\x0b"), # Method

        # Arguments
        #how do you parse that long message? #Queue
        h.many(h.ch_range('\x00', '\xff')),
        #h.uint32(), # message-count
        #h.uint32(), # consumer-count
        h.end_p()
    )
    return parser
Example #25
0
def peak_fast_meter_configuration_block():
    header = h.token("\xa5\xc3")
    length = h.uint8()
    scale_factors = h.uint8()
    analog = h.uint8()
    samples = h.uint8()
    digital = h.uint8()
    analog_offset = h.uint16()
    timestamp_offset = h.uint16()
    digital_offset = h.uint16()
    analog_signal = h.repeat_n(h.uint8(), 10)
    all_signals = h.repeat_n(analog_signal, 5)
    checksum = h.ch_range('\x00', '\xff')
    calculation_blocks = h.uint8()
    parser = h.sequence(header, length, h.uint8(), h.uint8(), scale_factors,
                        analog, samples, digital, calculation_blocks,
                        analog_offset, timestamp_offset, digital_offset,
                        all_signals, checksum)

    return parser
Example #26
0
def init_parser():
    return h.sequence(h.many1(h.choice(
        length_block(),
        connection_start_parser(),
        connection_start_ok_parser(),
        connection_tune_parser(),
        connection_tune_ok_parser(),
        connection_open_vhost_parser(),
        channel_open_parser(),
        channel_open_ok_parser(),
        queue_declare_parser(),
        queue_declare_ok_parser(),
        basic_consume_parser(),
        basic_consume_ok_parser(),
        basic_publish_parser(),
        basic_deliver_parser(),
        channel_close_parser(),
        channel_close_ok_parser(),
        connection_close_parser(),
        connection_close_ok_parser()
    )))
Example #27
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.epsilon_p())
Example #28
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.ignore(h.ch("b")), h.ch("c"))
Example #29
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.optional(h.choice(h.ch("b"), h.ch("c"))), h.ch("d"))
Example #30
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.whitespace(h.ch("b")))
Example #31
0
 def setUpClass(cls):
     cls.parser = h.action(h.sequence(h.choice(h.ch("a"), h.ch("A")),
                                      h.choice(h.ch("b"), h.ch("B"))),
                             lambda x: [y.upper() for y in x])
Example #32
0
 def setUpClass(cls):
     #raise unittest.SkipTest("Bind doesn't work right now")
     cls.parser = h.indirect()
     a = h.ch("a")
     cls.parser.bind(h.choice(h.sequence(a, cls.parser),
                              h.epsilon_p()))
Example #33
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch(b"a"), h.epsilon_p())
Example #34
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch(b"a"), h.ignore(h.ch(b"b")), h.ch(b"c"))
Example #35
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch(b"a"),
                             h.optional(h.choice(h.ch(b"b"), h.ch(b"c"))),
                             h.ch(b"d"))
Example #36
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("1"), h.and_(h.ch("2")))
Example #37
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch("a"), h.choice(h.sequence(h.ch("+"), h.not_(h.ch("+"))),
                                                 h.token("++")),
                             h.ch("b"))
Example #38
0
 def setUpClass(cls):
     cls.parser = h.sequence(
         h.ch(b"a"),
         h.choice(h.sequence(h.ch(b"+"), h.not_(h.ch(b"+"))),
                  h.token(b"++")), h.ch(b"b"))
Example #39
0
 def setUpClass(cls):
     cls.parser = h.sequence(h.ch(b"1"), h.and_(h.ch(b"2")))
Example #40
0
 def setUpClass(cls):
     #raise unittest.SkipTest(b"Bind doesn't work right now")
     cls.parser = h.indirect()
     a = h.ch(b"a")
     cls.parser.bind(h.choice(h.sequence(a, cls.parser), h.epsilon_p()))