Exemple #1
0
static tb_object_ref_t tb_object_bin_reader_func_data(tb_object_bin_reader_t* reader, tb_size_t type, tb_uint64_t size)
{
    // check
    tb_assert_and_check_return_val(reader && reader->stream && reader->list, tb_null);

    // empty?
    if (!size) return tb_object_data_init_from_data(tb_null, 0);

    // make data
    tb_char_t* data = tb_malloc0_cstr((tb_size_t)size);
    tb_assert_and_check_return_val(data, tb_null);

    // read data
    if (!tb_stream_bread(reader->stream, (tb_byte_t*)data, (tb_size_t)size)) 
    {
        tb_free(data);
        return tb_null;
    }

    // decode data
    {
        tb_byte_t*  pb = (tb_byte_t*)data;
        tb_byte_t*  pe = (tb_byte_t*)data + size;
        tb_byte_t   xb = (tb_byte_t)(((size >> 8) & 0xff) | (size & 0xff));
        for (; pb < pe; pb++, xb++) *pb ^= xb;
    }

    // make the data object
    tb_object_ref_t object = tb_object_data_init_from_data(data, (tb_size_t)size); 

    // exit data
    tb_free(data);

    // ok?
    return object;
}
Exemple #2
0
static tb_bool_t tb_oc_xml_writer_func_data(tb_oc_xml_writer_t* writer, tb_object_ref_t object, tb_size_t level)
{
    // check
    tb_assert_and_check_return_val(writer && writer->stream, tb_false);

    // no empty?
    if (tb_oc_data_size(object))
    {
        // writ beg
        if (!tb_oc_writer_tab(writer->stream, writer->deflate, level)) return tb_false;
        if (tb_stream_printf(writer->stream, "<data>") < 0) return tb_false;
        if (!tb_oc_writer_newline(writer->stream, writer->deflate)) return tb_false;

        // decode base64 data
        tb_byte_t const*    ib = (tb_byte_t const*)tb_oc_data_getp(object);
        tb_size_t           in = tb_oc_data_size(object); 
        tb_size_t           on = in << 1;
        tb_char_t*          ob = tb_malloc0_cstr(on);
        tb_assert_and_check_return_val(ob && on, tb_false);
        on = tb_base64_encode(ib, in, ob, on);
        tb_trace_d("base64: %u => %u", in, on);

        // writ data
        tb_char_t const*    p = ob;
        tb_char_t const*    e = ob + on;
        tb_size_t           n = 0;
        for (; p < e && *p; p++, n++)
        {
            if (!(n & 63))
            {
                if (n) if (!tb_oc_writer_newline(writer->stream, writer->deflate)) break;
                if (!tb_oc_writer_tab(writer->stream, writer->deflate, level)) break;
            }
            if (tb_stream_printf(writer->stream, "%c", *p) < 0) break;
        }

        // free the data
        tb_free(ob);
               
        // check
        tb_check_return_val(p == e, tb_false);

        // writ newline
        if (!tb_oc_writer_newline(writer->stream, writer->deflate)) return tb_false;
     
        // writ end
        if (!tb_oc_writer_tab(writer->stream, writer->deflate, level)) return tb_false;
        if (tb_stream_printf(writer->stream, "</data>") < 0) return tb_false;
        if (!tb_oc_writer_newline(writer->stream, writer->deflate)) return tb_false;
    }
    else 
    {
        // writ
        if (!tb_oc_writer_tab(writer->stream, writer->deflate, level)) return tb_false;
        if (tb_stream_printf(writer->stream, "<data/>") < 0) return tb_false;
        if (!tb_oc_writer_newline(writer->stream, writer->deflate)) return tb_false;
    }

    // ok
    return tb_true;
}