예제 #1
0
def write_object(d, output, context):
    amf.utils.logger().debug("amf3.write_object(%s)", repr(d))
    amf0.write_byte(OBJECT, output) # Type Code
    key = context.get_object_reference_index(d)
    ref_str = ''
    if key == -1:
        context.add_object_reference(d)
        ref_str = '1' # inline object code
        # Getting the class definition of the given object.
        class_def = amf.utils.get_class_def(d)
        class_def_key = context.get_class_def_reference_index(class_def)
        if class_def_key == -1:
            context.add_class_def_reference(class_def)
            ref_str = '001' + ref_str # '00' means Non-dynamic object and '1' means inline class-def
            ref = class_def['num_of_members']
            ref <<= 4
            ref |= int(ref_str, 2)
            _write_integer(ref, output, context)
            _write_string(class_def['type'], output, context) # class name
            for name in class_def['member_names']:
                _write_string(name, output, context) # member names
        else:
            ref_str = '0' + ref_str # referenced class-def code
            ref_str = amf.utils.to_binary(class_def_key) + ref_str
            ref = int(ref_str, 2)
            amf0.write_byte(ref, output)
        for member_name in class_def['member_names']:
            value = getattr(d, member_name, None)
            _write_data(value, output, context)
    else:
        key <<= 1
        _write_integer(key, output, context)
예제 #2
0
def write_number(d, output, context):
    if isinstance(d, Decimal): d = float(d)
    if isinstance(d, (int, long)) and d >= -268435456 and d <= 268435455: # check valid range for 29bits
        amf0.write_byte(INTEGER, output) # Type Code
        _write_integer(d, output, context)
    else:
        amf0.write_byte(NUMBER, output) # Type Code
        amf0.write_double(d, output)
예제 #3
0
def _write_headers(headers, output):
    header_count = len(headers)
    amf0.write_int(header_count, output)
    for header in headers:
        context = amf.AMFMessageBodyContext()
        amf0.write_utf(header['name'], output)
        amf0.write_byte(header.get('mustUnderstand', False), output)
        content = StringIO()
        write_data(header['content'], content, context)
        amf0.write_long(len(content.getvalue()), output) # Length in bytes of header
        output.write(content.getvalue())
        content.close()
예제 #4
0
def write_date(d, output, context):
    amf.utils.logger().debug("amf3.write_date(%s)", repr(d))
    amf0.write_byte(DATE, output) # Type Code
    key = context.get_object_reference_index(d)
    if key == -1:
        ms = amf.utils.get_timestamp_from_date(d)
        _write_integer(0x01, output, context) # inline ref
        amf0.write_double(ms, output)
        context.add_object_reference(d)
    else:
        key <<= 1
        _write_integer(key, output, context)
예제 #5
0
def write_data(d, output=None, context=None):
    ret = False
    if not output:
        output = StringIO()
        ret = True
    if not context:
        context=amf.AMFMessageBodyContext()
    amf0.write_byte(amf0.AMF3_DATA_TYPE, output) # Type Code
    _write_data(d, output, context)
    if ret:
        try:
            return output.getvalue()
        finally:
            output.close()
예제 #6
0
def write_array(a, output, context):
    amf.utils.logger().debug("amf3.write_array(%s)", repr(a))
    amf0.write_byte(ARRAY, output) # Type Code
    key = context.get_object_reference_index(a)
    if key == -1:
        context.add_object_reference(a)
        count = len(a)
        ref = count << 1 | 0x01
        _write_integer(ref, output, context)
        write_null(None, output, context)
        for elem in a:
            _write_data(elem, output, context)
    else:
        key <<= 1
        _write_integer(key, output, context)
예제 #7
0
def write_dynamic_object(d, output, context):
    amf.utils.logger().debug("amf3.write_dynamice_object(%s)", repr(d))
    amf0.write_byte(OBJECT, output) # Type Code
    key = context.get_object_reference_index(d)
    if key == -1:
        context.add_object_reference(d)
        amf0.write_byte(0x0B, output) # '1011' in binary which means dynamic, inline class definition and inline object
        _write_string('', output, context) # anonymous object
        for key, value in d.iteritems():
            if not key == '_explicitType':
                _write_string(key, output, context)
                _write_data(value, output, context)
        _write_string('', output, context)
    else:
        key <<= 1
        _write_integer(key, output, context)
예제 #8
0
def write_byte_array(ba, output, context):
    amf.utils.logger().debug("amf3.byte_array()")
    data = ba.data
    amf0.write_byte(BYTEARRAY, output) # Type Code
    _write_string(data, output, context)
예제 #9
0
def write_xml(document, output, context):
    xmlstr = re.sub(r'\>(\n|\r|\r\n| |\t)*\<', '><', document.toxml().strip())
    amf.utils.logger().debug("amf3.write_xml(%s)", repr(xmlstr))
    amf0.write_byte(XML, output) # Type Code
    _write_string(xmlstr, output, context)
예제 #10
0
def write_string(s, output, context):
    amf0.write_byte(STRING, output) # Type Code
    _write_string(s, output, context)
예제 #11
0
def _write_integer(d, output, context=None):
    amf.utils.logger().debug("amf3._write_integer(%s)", repr(d))
    d &= 0x1fffffff
    if d < 0x80:
        amf0.write_byte(d, output)
    elif d < 0x4000:
        amf0.write_byte(d >> 7 & 0x7F | 0x80, output)
        amf0.write_byte(d & 0x7F, output)
    elif d < 0x200000:
        amf0.write_byte(d >> 14 & 0x7F | 0x80, output)
        amf0.write_byte(d >> 7 & 0x7F | 0x80, output)
        amf0.write_byte(d & 0x7F, output)
    else:
        amf0.write_byte(d >> 22 & 0x7F | 0x80, output)
        amf0.write_byte(d >> 15 & 0x7F | 0x80, output)
        amf0.write_byte(d >> 8 & 0x7F | 0x80, output)
        amf0.write_byte(d & 0xFF, output)
예제 #12
0
def write_boolean(b, output, context=None):
    if b:
        amf0.write_byte(BOOLEAN_TRUE, output) # Type Code
    else:
        amf0.write_byte(BOOLEAN_FALSE, output) # Type Code
예제 #13
0
def write_null(n, output, context=None):
    amf.utils.logger().debug("amf3.write_null()")
    amf0.write_byte(NULL, output) # Type Code