llsdmessagereader.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * @file llsdmessagereader.cpp
  3. * @brief LLSDMessageReader class implementation.
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (c) 2007-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llsdmessagereader.h"
  34. #include "llmath.h"
  35. #include "llmessagebuilder.h"
  36. #include "llquaternion.h"
  37. #include "llsdmessagebuilder.h"
  38. #include "llsdutil.h"
  39. #include "llsdutil_math.h"
  40. #include "llvector2.h"
  41. #include "llvector3.h"
  42. #include "llvector3d.h"
  43. #include "llvector4.h"
  44. #include "llcolor4.h"
  45. LLSDMessageReader::LLSDMessageReader()
  46. : mMessageName(NULL)
  47. {
  48. }
  49. LLSD getLLSD(const LLSD& input, const char* block, const char* var,
  50. S32 blocknum)
  51. {
  52. // babbage: log error to llerrs if variable not found to mimic
  53. // LLTemplateMessageReader::getData behaviour
  54. if (!block)
  55. {
  56. llerrs << "NULL block name" << llendl;
  57. return LLSD();
  58. }
  59. if (!var)
  60. {
  61. llerrs << "NULL var name" << llendl;
  62. return LLSD();
  63. }
  64. if (!input[block].isArray())
  65. {
  66. // NOTE: babbage: need to return default for missing blocks to allow
  67. // backwards/forwards compatibility - handlers must cope with default
  68. // values.
  69. llwarns << "block " << block << " not found" << llendl;
  70. return LLSD();
  71. }
  72. LLSD result = input[block][blocknum][var];
  73. if (result.isUndefined())
  74. {
  75. // NOTE: babbage: need to return default for missing vars to allow
  76. // backwards/forwards compatibility - handlers must cope with default
  77. // values.
  78. llwarns << "var " << var << " not found" << llendl;
  79. }
  80. return result;
  81. }
  82. //virtual
  83. void LLSDMessageReader::getBinaryData(const char* block, const char* var,
  84. void* datap, S32 size, S32 blocknum,
  85. S32 max_size)
  86. {
  87. LLSD::Binary data = getLLSD(mMessage, block, var, blocknum);
  88. S32 data_size = (S32)data.size();
  89. if (size && data_size != size)
  90. {
  91. return;
  92. }
  93. if (max_size < data_size)
  94. {
  95. data_size = max_size;
  96. }
  97. // Calls to memcpy will fail if data_size is not positive.
  98. // Phoenix 2009-02-27
  99. if (data_size <= 0)
  100. {
  101. return;
  102. }
  103. memcpy(datap, &(data[0]), data_size);
  104. }
  105. //virtual
  106. void LLSDMessageReader::getBool(const char* block, const char* var, bool& data,
  107. S32 blocknum)
  108. {
  109. data = getLLSD(mMessage, block, var, blocknum);
  110. }
  111. //virtual
  112. void LLSDMessageReader::getS8(const char* block, const char* var, S8& data,
  113. S32 blocknum)
  114. {
  115. data = getLLSD(mMessage, block, var, blocknum).asInteger();
  116. }
  117. //virtual
  118. void LLSDMessageReader::getU8(const char* block, const char* var, U8& data,
  119. S32 blocknum)
  120. {
  121. data = getLLSD(mMessage, block, var, blocknum).asInteger();
  122. }
  123. //virtual
  124. void LLSDMessageReader::getS16(const char* block, const char* var, S16& data,
  125. S32 blocknum)
  126. {
  127. data = getLLSD(mMessage, block, var, blocknum).asInteger();
  128. }
  129. //virtual
  130. void LLSDMessageReader::getU16(const char* block, const char* var, U16& data,
  131. S32 blocknum)
  132. {
  133. data = getLLSD(mMessage, block, var, blocknum).asInteger();
  134. }
  135. //virtual
  136. void LLSDMessageReader::getS32(const char* block, const char* var, S32& data,
  137. S32 blocknum)
  138. {
  139. data = getLLSD(mMessage, block, var, blocknum);
  140. }
  141. //virtual
  142. void LLSDMessageReader::getF32(const char* block, const char* var, F32& data,
  143. S32 blocknum)
  144. {
  145. data = (F32)getLLSD(mMessage, block, var, blocknum).asReal();
  146. }
  147. //virtual
  148. void LLSDMessageReader::getU32(const char* block, const char* var, U32& data,
  149. S32 blocknum)
  150. {
  151. data = ll_U32_from_sd(getLLSD(mMessage, block, var, blocknum));
  152. }
  153. //virtual
  154. void LLSDMessageReader::getU64(const char* block, const char* var, U64& data,
  155. S32 blocknum)
  156. {
  157. data = ll_U64_from_sd(getLLSD(mMessage, block, var, blocknum));
  158. }
  159. //virtual
  160. void LLSDMessageReader::getF64(const char* block, const char* var, F64& data,
  161. S32 blocknum)
  162. {
  163. data = getLLSD(mMessage, block, var, blocknum);
  164. }
  165. //virtual
  166. void LLSDMessageReader::getVector3(const char* block, const char* var,
  167. LLVector3& vec, S32 blocknum)
  168. {
  169. vec = ll_vector3_from_sd(getLLSD(mMessage, block, var, blocknum));
  170. }
  171. //virtual
  172. void LLSDMessageReader::getVector4(const char* block, const char* var,
  173. LLVector4& vec, S32 blocknum)
  174. {
  175. vec = ll_vector4_from_sd(getLLSD(mMessage, block, var, blocknum));
  176. }
  177. //virtual
  178. void LLSDMessageReader::getVector3d(const char* block, const char* var,
  179. LLVector3d& vec, S32 blocknum)
  180. {
  181. vec = ll_vector3d_from_sd(getLLSD(mMessage, block, var, blocknum));
  182. }
  183. //virtual
  184. void LLSDMessageReader::getQuat(const char* block, const char* var,
  185. LLQuaternion& q, S32 blocknum)
  186. {
  187. q = ll_quaternion_from_sd(getLLSD(mMessage, block, var, blocknum));
  188. }
  189. //virtual
  190. void LLSDMessageReader::getUUID(const char* block, const char* var,
  191. LLUUID& uuid, S32 blocknum)
  192. {
  193. uuid = getLLSD(mMessage, block, var, blocknum);
  194. }
  195. //virtual
  196. void LLSDMessageReader::getIPAddr(const char* block, const char* var, U32& ip,
  197. S32 blocknum)
  198. {
  199. ip = ll_ipaddr_from_sd(getLLSD(mMessage, block, var, blocknum));
  200. }
  201. //virtual
  202. void LLSDMessageReader::getIPPort(const char* block, const char* var,
  203. U16& port, S32 blocknum)
  204. {
  205. port = getLLSD(mMessage, block, var, blocknum).asInteger();
  206. }
  207. //virtual
  208. void LLSDMessageReader::getString(const char* block, const char* var,
  209. S32 buffer_size, char* buffer, S32 blocknum)
  210. {
  211. if (buffer_size <= 0)
  212. {
  213. llwarns << "buffer_size <= 0" << llendl;
  214. return;
  215. }
  216. std::string data = getLLSD(mMessage, block, var, blocknum);
  217. S32 data_size = data.size();
  218. if (data_size >= buffer_size)
  219. {
  220. data_size = buffer_size - 1;
  221. }
  222. memcpy(buffer, data.data(), data_size);
  223. buffer[data_size] = '\0';
  224. }
  225. //virtual
  226. void LLSDMessageReader::getString(const char* block, const char* var,
  227. std::string& outstr, S32 blocknum)
  228. {
  229. outstr = getLLSD(mMessage, block, var, blocknum).asString();
  230. }
  231. //virtual
  232. S32 LLSDMessageReader::getNumberOfBlocks(const char* blockname)
  233. {
  234. return mMessage[blockname].size();
  235. }
  236. S32 getElementSize(const LLSD& llsd)
  237. {
  238. LLSD::Type type = llsd.type();
  239. switch (type)
  240. {
  241. case LLSD::TypeBoolean:
  242. return sizeof(bool);
  243. case LLSD::TypeInteger:
  244. return sizeof(S32);
  245. case LLSD::TypeReal:
  246. return sizeof(F64);
  247. case LLSD::TypeString:
  248. return llsd.asString().size();
  249. case LLSD::TypeUUID:
  250. return sizeof(LLUUID);
  251. case LLSD::TypeDate:
  252. return sizeof(LLDate);
  253. case LLSD::TypeURI:
  254. return sizeof(LLURI);
  255. case LLSD::TypeBinary:
  256. {
  257. LLSD::Binary data = llsd;
  258. return data.size() * sizeof(U8);
  259. }
  260. case LLSD::TypeMap:
  261. case LLSD::TypeArray:
  262. case LLSD::TypeUndefined:
  263. default:
  264. break;
  265. }
  266. return 0;
  267. }
  268. // Mainly used to find size of binary block of data
  269. //virtual
  270. S32 LLSDMessageReader::getSize(const char* blockname, const char* varname)
  271. {
  272. return getElementSize(mMessage[blockname][0][varname]);
  273. }
  274. //virtual
  275. S32 LLSDMessageReader::getSize(const char* blockname, S32 blocknum,
  276. const char* varname)
  277. {
  278. return getElementSize(mMessage[blockname][blocknum][varname]);
  279. }
  280. //virtual
  281. void LLSDMessageReader::clearMessage()
  282. {
  283. mMessage = LLSD();
  284. }
  285. //virtual
  286. const char* LLSDMessageReader::getMessageName() const
  287. {
  288. return mMessageName;
  289. }
  290. // virtual
  291. S32 LLSDMessageReader::getMessageSize() const
  292. {
  293. return 0;
  294. }
  295. //virtual
  296. void LLSDMessageReader::copyToBuilder(LLMessageBuilder& builder) const
  297. {
  298. builder.copyFromLLSD(mMessage);
  299. }
  300. void LLSDMessageReader::setMessage(const char* name, const LLSD& message)
  301. {
  302. mMessageName = name;
  303. // TODO: Validate
  304. mMessage = message;
  305. }