Example #1
0
void sd_object::test<2>()
{
    const std::string decoded("random");
    //const std::string encoded("cmFuZG9t\n");
    const std::string streamed("b(6)\"random\"");
    typedef std::vector<U8> buf_t;
    buf_t buf;
    std::copy(
        decoded.begin(),
        decoded.end(),
        std::back_insert_iterator<buf_t>(buf));
    LLSD sd;
    sd = buf;
    std::stringstream str;
    S32 count = LLSDSerialize::toNotation(sd, str);
    ensure_equals("output count", count, 1);
    std::string actual(str.str());
    ensure_equals("formatted binary encoding", actual, streamed);
    sd.clear();
    LLSDSerialize::fromNotation(sd, str, str.str().size());
    std::vector<U8> after;
    after = sd.asBinary();
    ensure_equals("binary decoded size", after.size(), decoded.size());
    ensure("binary decoding", (0 == memcmp(
                                   &after[0],
                                   decoded.c_str(),
                                   decoded.size())));
}
Example #2
0
void sd_object::test<5>()
{
    for(S32 i = 0; i < 100; ++i)
    {
        // gen up a starting point
        typedef std::vector<U8> buf_t;
        buf_t source;
        srand(666 + i);		/* Flawfinder: ignore */
        S32 size = rand() % 1000 + 10;
        std::generate_n(
            std::back_insert_iterator<buf_t>(source),
            size,
            rand);
        std::stringstream str;
        str << "b(" << size << ")\"";
        str.write((const char*)&source[0], size);
        str << "\"";
        LLSD sd;
        S32 count = LLSDSerialize::fromNotation(sd, str, str.str().size());
        ensure_equals("binary parse", count, 1);
        buf_t actual = sd.asBinary();
        ensure_equals("binary size", actual.size(), (size_t)size);
        ensure("binary data", (0 == memcmp(&source[0], &actual[0], size)));
    }
}
Example #3
0
// Converts an LLSD binary to an LLSD string
LLSD ll_string_from_binary(const LLSD& sd)
{
	std::vector<U8> value = sd.asBinary();
	std::string str;
	str.resize(value.size());
	memcpy(&str[0], &value[0], value.size());
	return str;
}
Example #4
0
U32 ll_ipaddr_from_sd(const LLSD& sd)
{
	U32 ret;
	std::vector<U8> v = sd.asBinary();
	if (v.size() < 4)
	{
		return 0;
	}
	memcpy(&ret, &(v[0]), 4);		/* Flawfinder: ignore */
	return ret;
}
Example #5
0
U64 ll_U64_from_sd(const LLSD& sd)
{
	U32 high, low;
	std::vector<U8> v = sd.asBinary();

	if (v.size() < 8)
	{
		return 0;
	}

	memcpy(&high, &(v[0]), 4);		/* Flawfinder: ignore */
	memcpy(&low, &(v[4]), 4);		/* Flawfinder: ignore */
	high = ntohl(high);
	low = ntohl(low);

	return ((U64)high) << 32 | low;
}
Example #6
0
void LLFilterSD2XMLRPC::streamOut(std::ostream& ostr, const LLSD& sd)
{
    ostr << "<value>";
    switch(sd.type())
    {
    case LLSD::TypeMap:
    {
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(map) BEGIN" << llendl;
#endif
        ostr << "<struct>";
        if(ostr.fail())
        {
            llinfos << "STREAM FAILURE writing struct" << llendl;
        }
        LLSD::map_const_iterator it = sd.beginMap();
        LLSD::map_const_iterator end = sd.endMap();
        for(; it != end; ++it)
        {
            ostr << "<member><name>" << xml_escape_string((*it).first)
                 << "</name>";
            streamOut(ostr, (*it).second);
            if(ostr.fail())
            {
                llinfos << "STREAM FAILURE writing '" << (*it).first
                        << "' with sd type " << (*it).second.type() << llendl;
            }
            ostr << "</member>";
        }
        ostr << "</struct>";
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(map) END" << llendl;
#endif
        break;
    }
    case LLSD::TypeArray:
    {
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(array) BEGIN" << llendl;
#endif
        ostr << "<array><data>";
        LLSD::array_const_iterator it = sd.beginArray();
        LLSD::array_const_iterator end = sd.endArray();
        for(; it != end; ++it)
        {
            streamOut(ostr, *it);
            if(ostr.fail())
            {
                llinfos << "STREAM FAILURE writing array element sd type "
                        << (*it).type() << llendl;
            }
        }
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(array) END" << llendl;
#endif
        ostr << "</data></array>";
        break;
    }
    case LLSD::TypeUndefined:
    // treat undefined as a bool with a false value.
    case LLSD::TypeBoolean:
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(bool)" << llendl;
#endif
        ostr << "<boolean>" << (sd.asBoolean() ? "1" : "0") << "</boolean>";
        break;
    case LLSD::TypeInteger:
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(int)" << llendl;
#endif
        ostr << "<i4>" << sd.asInteger() << "</i4>";
        break;
    case LLSD::TypeReal:
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(real)" << llendl;
#endif
        ostr << "<double>" << sd.asReal() << "</double>";
        break;
    case LLSD::TypeString:
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(string)" << llendl;
#endif
        ostr << "<string>" << xml_escape_string(sd.asString()) << "</string>";
        break;
    case LLSD::TypeUUID:
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(uuid)" << llendl;
#endif
        // serialize it as a string
        ostr << "<string>" << sd.asString() << "</string>";
        break;
    case LLSD::TypeURI:
    {
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(uri)" << llendl;
#endif
        // serialize it as a string
        ostr << "<string>" << xml_escape_string(sd.asString()) << "</string>";
        break;
    }
    case LLSD::TypeBinary:
    {
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(binary)" << llendl;
#endif
        // this is pretty inefficient, but we'll deal with that
        // problem when it becomes one.
        ostr << "<base64>";
        LLSD::Binary buffer = sd.asBinary();
        if(!buffer.empty())
        {
            // *TODO: convert to LLBase64
            int b64_buffer_length = apr_base64_encode_len(buffer.size());
            char* b64_buffer = new char[b64_buffer_length];
            b64_buffer_length = apr_base64_encode_binary(
                                    b64_buffer,
                                    &buffer[0],
                                    buffer.size());
            ostr.write(b64_buffer, b64_buffer_length - 1);
            delete[] b64_buffer;
        }
        ostr << "</base64>";
        break;
    }
    case LLSD::TypeDate:
#if LL_SPEW_STREAM_OUT_DEBUGGING
        llinfos << "streamOut(date)" << llendl;
#endif
        // no need to escape this since it will be alpha-numeric.
        ostr << "<dateTime.iso8601>" << sd.asString() << "</dateTime.iso8601>";
        break;
    default:
        // unhandled type
        llwarns << "Unhandled structured data type: " << sd.type()
                << llendl;
        break;
    }
    ostr << "</value>";
}
S32 LLSDXMLFormatter::format_impl(const LLSD& data, std::ostream& ostr, U32 options, U32 level) const
{
	S32 format_count = 1;
	std::string pre;
	std::string post;

	if (options & LLSDFormatter::OPTIONS_PRETTY)
	{
		for (U32 i = 0; i < level; i++)
		{
			pre += "    ";
		}
		post = "\n";
	}

	switch(data.type())
	{
	case LLSD::TypeMap:
		if(0 == data.size())
		{
			ostr << pre << "<map />" << post;
		}
		else
		{
			ostr << pre << "<map>" << post;
			LLSD::map_const_iterator iter = data.beginMap();
			LLSD::map_const_iterator end = data.endMap();
			for(; iter != end; ++iter)
			{
				ostr << pre << "<key>" << escapeString((*iter).first) << "</key>" << post;
				format_count += format_impl((*iter).second, ostr, options, level + 1);
			}
			ostr << pre <<  "</map>" << post;
		}
		break;

	case LLSD::TypeArray:
		if(0 == data.size())
		{
			ostr << pre << "<array />" << post;
		}
		else
		{
			ostr << pre << "<array>" << post;
			LLSD::array_const_iterator iter = data.beginArray();
			LLSD::array_const_iterator end = data.endArray();
			for(; iter != end; ++iter)
			{
				format_count += format_impl(*iter, ostr, options, level + 1);
			}
			ostr << pre << "</array>" << post;
		}
		break;

	case LLSD::TypeUndefined:
		ostr << pre << "<undef />" << post;
		break;

	case LLSD::TypeBoolean:
		ostr << pre << "<boolean>";
		if(mBoolAlpha ||
#if( LL_WINDOWS || __GNUC__ > 2)
		   (ostr.flags() & std::ios::boolalpha)
#else
		   (ostr.flags() & 0x0100)
#endif
			)
		{
			ostr << (data.asBoolean() ? "true" : "false");
		}
		else
		{
			ostr << (data.asBoolean() ? 1 : 0);
		}
		ostr << "</boolean>" << post;
		break;

	case LLSD::TypeInteger:
		ostr << pre << "<integer>" << data.asInteger() << "</integer>" << post;
		break;

	case LLSD::TypeReal:
		ostr << pre << "<real>";
		if(mRealFormat.empty())
		{
			ostr << data.asReal();
		}
		else
		{
			formatReal(data.asReal(), ostr);
		}
		ostr << "</real>" << post;
		break;

	case LLSD::TypeUUID:
		if(data.asUUID().isNull()) ostr << pre << "<uuid />" << post;
		else ostr << pre << "<uuid>" << data.asUUID() << "</uuid>" << post;
		break;

	case LLSD::TypeString:
		if(data.asString().empty()) ostr << pre << "<string />" << post;
		else ostr << pre << "<string>" << escapeString(data.asString()) <<"</string>" << post;
		break;

	case LLSD::TypeDate:
		ostr << pre << "<date>" << data.asDate() << "</date>" << post;
		break;

	case LLSD::TypeURI:
		ostr << pre << "<uri>" << escapeString(data.asString()) << "</uri>" << post;
		break;

	case LLSD::TypeBinary:
	{
		LLSD::Binary buffer = data.asBinary();
		if(buffer.empty())
		{
			ostr << pre << "<binary />" << post;
		}
		else
		{
			// *FIX: memory inefficient.
			// *TODO: convert to use LLBase64
			ostr << pre << "<binary encoding=\"base64\">";
			int b64_buffer_length = apr_base64_encode_len(buffer.size());
			char* b64_buffer = new char[b64_buffer_length];
			b64_buffer_length = apr_base64_encode_binary(
				b64_buffer,
				&buffer[0],
				buffer.size());
			ostr.write(b64_buffer, b64_buffer_length - 1);
			delete[] b64_buffer;
			ostr << "</binary>" << post;
		}
		break;
	}
	default:
		// *NOTE: This should never happen.
		ostr << pre << "<undef />" << post;
		break;
	}
	return format_count;
}