00001
00034 #include "linden_common.h"
00035
00036 #include "llsdutil.h"
00037
00038 #if LL_WINDOWS
00039 # define WIN32_LEAN_AND_MEAN
00040 # include <winsock2.h>
00041 #elif LL_LINUX || LL_SOLARIS
00042 # include <netinet/in.h>
00043 #elif LL_DARWIN
00044 # include <arpa/inet.h>
00045 #endif
00046
00047 #include "llsdserialize.h"
00048
00049
00050 LLSD ll_sd_from_vector3(const LLVector3& vec)
00051 {
00052 LLSD rv;
00053 rv.append((F64)vec.mV[VX]);
00054 rv.append((F64)vec.mV[VY]);
00055 rv.append((F64)vec.mV[VZ]);
00056 return rv;
00057 }
00058
00059 LLVector3 ll_vector3_from_sd(const LLSD& sd, S32 start_index)
00060 {
00061 LLVector3 rv;
00062 rv.mV[VX] = (F32)sd[start_index].asReal();
00063 rv.mV[VY] = (F32)sd[++start_index].asReal();
00064 rv.mV[VZ] = (F32)sd[++start_index].asReal();
00065 return rv;
00066 }
00067
00068
00069 LLSD ll_sd_from_vector4(const LLVector4& vec)
00070 {
00071 LLSD rv;
00072 rv.append((F64)vec.mV[VX]);
00073 rv.append((F64)vec.mV[VY]);
00074 rv.append((F64)vec.mV[VZ]);
00075 rv.append((F64)vec.mV[VW]);
00076 return rv;
00077 }
00078
00079 LLVector4 ll_vector4_from_sd(const LLSD& sd, S32 start_index)
00080 {
00081 LLVector4 rv;
00082 rv.mV[VX] = (F32)sd[start_index].asReal();
00083 rv.mV[VY] = (F32)sd[++start_index].asReal();
00084 rv.mV[VZ] = (F32)sd[++start_index].asReal();
00085 rv.mV[VW] = (F32)sd[++start_index].asReal();
00086 return rv;
00087 }
00088
00089
00090 LLSD ll_sd_from_vector3d(const LLVector3d& vec)
00091 {
00092 LLSD rv;
00093 rv.append(vec.mdV[VX]);
00094 rv.append(vec.mdV[VY]);
00095 rv.append(vec.mdV[VZ]);
00096 return rv;
00097 }
00098
00099 LLVector3d ll_vector3d_from_sd(const LLSD& sd, S32 start_index)
00100 {
00101 LLVector3d rv;
00102 rv.mdV[VX] = sd[start_index].asReal();
00103 rv.mdV[VY] = sd[++start_index].asReal();
00104 rv.mdV[VZ] = sd[++start_index].asReal();
00105 return rv;
00106 }
00107
00108
00109 LLSD ll_sd_from_vector2(const LLVector2& vec)
00110 {
00111 LLSD rv;
00112 rv.append((F64)vec.mV[VX]);
00113 rv.append((F64)vec.mV[VY]);
00114 return rv;
00115 }
00116
00117 LLVector2 ll_vector2_from_sd(const LLSD& sd)
00118 {
00119 LLVector2 rv;
00120 rv.mV[VX] = (F32)sd[0].asReal();
00121 rv.mV[VY] = (F32)sd[1].asReal();
00122 return rv;
00123 }
00124
00125
00126 LLSD ll_sd_from_quaternion(const LLQuaternion& quat)
00127 {
00128 LLSD rv;
00129 rv.append((F64)quat.mQ[VX]);
00130 rv.append((F64)quat.mQ[VY]);
00131 rv.append((F64)quat.mQ[VZ]);
00132 rv.append((F64)quat.mQ[VW]);
00133 return rv;
00134 }
00135
00136 LLQuaternion ll_quaternion_from_sd(const LLSD& sd)
00137 {
00138 LLQuaternion quat;
00139 quat.mQ[VX] = (F32)sd[0].asReal();
00140 quat.mQ[VY] = (F32)sd[1].asReal();
00141 quat.mQ[VZ] = (F32)sd[2].asReal();
00142 quat.mQ[VW] = (F32)sd[3].asReal();
00143 return quat;
00144 }
00145
00146
00147 LLSD ll_sd_from_color4(const LLColor4& c)
00148 {
00149 LLSD rv;
00150 rv.append(c.mV[0]);
00151 rv.append(c.mV[1]);
00152 rv.append(c.mV[2]);
00153 rv.append(c.mV[3]);
00154 return rv;
00155 }
00156
00157 LLColor4 ll_color4_from_sd(const LLSD& sd)
00158 {
00159 LLColor4 c;
00160 c.mV[0] = (F32)sd[0].asReal();
00161 c.mV[1] = (F32)sd[1].asReal();
00162 c.mV[2] = (F32)sd[2].asReal();
00163 c.mV[3] = (F32)sd[3].asReal();
00164 return c;
00165 }
00166
00167
00168 LLSD ll_sd_from_U32(const U32 val)
00169 {
00170 std::vector<U8> v;
00171 U32 net_order = htonl(val);
00172
00173 v.resize(4);
00174 memcpy(&(v[0]), &net_order, 4);
00175
00176 return LLSD(v);
00177 }
00178
00179 U32 ll_U32_from_sd(const LLSD& sd)
00180 {
00181 U32 ret;
00182 std::vector<U8> v = sd.asBinary();
00183 if (v.size() < 4)
00184 {
00185 return 0;
00186 }
00187 memcpy(&ret, &(v[0]), 4);
00188 ret = ntohl(ret);
00189 return ret;
00190 }
00191
00192
00193 LLSD ll_sd_from_U64(const U64 val)
00194 {
00195 std::vector<U8> v;
00196 U32 high, low;
00197
00198 high = (U32)(val >> 32);
00199 low = (U32)val;
00200 high = htonl(high);
00201 low = htonl(low);
00202
00203 v.resize(8);
00204 memcpy(&(v[0]), &high, 4);
00205 memcpy(&(v[4]), &low, 4);
00206
00207 return LLSD(v);
00208 }
00209
00210 U64 ll_U64_from_sd(const LLSD& sd)
00211 {
00212 U32 high, low;
00213 std::vector<U8> v = sd.asBinary();
00214
00215 if (v.size() < 8)
00216 {
00217 return 0;
00218 }
00219
00220 memcpy(&high, &(v[0]), 4);
00221 memcpy(&low, &(v[4]), 4);
00222 high = ntohl(high);
00223 low = ntohl(low);
00224
00225 return ((U64)high) << 32 | low;
00226 }
00227
00228
00229 LLSD ll_sd_from_ipaddr(const U32 val)
00230 {
00231 std::vector<U8> v;
00232
00233 v.resize(4);
00234 memcpy(&(v[0]), &val, 4);
00235
00236 return LLSD(v);
00237 }
00238
00239 U32 ll_ipaddr_from_sd(const LLSD& sd)
00240 {
00241 U32 ret;
00242 std::vector<U8> v = sd.asBinary();
00243 if (v.size() < 4)
00244 {
00245 return 0;
00246 }
00247 memcpy(&ret, &(v[0]), 4);
00248 return ret;
00249 }
00250
00251
00252 LLSD ll_string_from_binary(const LLSD& sd)
00253 {
00254 std::vector<U8> value = sd.asBinary();
00255 std::string str;
00256 str.resize(value.size());
00257 memcpy(&str[0], &value[0], value.size());
00258 return str;
00259 }
00260
00261
00262 LLSD ll_binary_from_string(const LLSD& sd)
00263 {
00264 std::vector<U8> binary_value;
00265
00266 LLString string_value = sd.asString();
00267 const char* string_p = string_value.c_str();
00268 while (*string_p)
00269 {
00270 binary_value.push_back(*string_p);
00271 string_p++;
00272 }
00273
00274 binary_value.push_back('\0');
00275
00276 return binary_value;
00277 }
00278
00279 char* ll_print_sd(const LLSD& sd)
00280 {
00281 const U32 bufferSize = 10 * 1024;
00282 static char buffer[bufferSize];
00283 std::ostringstream stream;
00284
00285 stream << LLSDOStreamer<LLSDXMLFormatter>(sd);
00286 stream << std::ends;
00287 strncpy(buffer, stream.str().c_str(), bufferSize);
00288 buffer[bufferSize - 1] = '\0';
00289 return buffer;
00290 }
00291
00292 char* ll_pretty_print_sd(const LLSD& sd)
00293 {
00294 const U32 bufferSize = 10 * 1024;
00295 static char buffer[bufferSize];
00296 std::ostringstream stream;
00297
00298 stream << LLSDOStreamer<LLSDXMLFormatter>(sd, LLSDFormatter::OPTIONS_PRETTY);
00299 stream << std::ends;
00300 strncpy(buffer, stream.str().c_str(), bufferSize);
00301 buffer[bufferSize - 1] = '\0';
00302 return buffer;
00303 }