/indra/llcommon/llsdserialize.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 2170 lines · 1783 code · 166 blank · 221 comment · 209 complexity · 7f8958a866a6dd9d98ef17dd5e8ad27b MD5 · raw file

  1. /**
  2. * @file llsdserialize.cpp
  3. * @author Phoenix
  4. * @date 2006-03-05
  5. * @brief Implementation of LLSD parsers and formatters
  6. *
  7. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "linden_common.h"
  29. #include "llsdserialize.h"
  30. #include "llpointer.h"
  31. #include "llstreamtools.h" // for fullread
  32. #include <iostream>
  33. #include "apr_base64.h"
  34. #ifdef LL_STANDALONE
  35. # include <zlib.h>
  36. #else
  37. # include "zlib/zlib.h" // for davep's dirty little zip functions
  38. #endif
  39. #if !LL_WINDOWS
  40. #include <netinet/in.h> // htonl & ntohl
  41. #endif
  42. #include "lldate.h"
  43. #include "llsd.h"
  44. #include "llstring.h"
  45. #include "lluri.h"
  46. // File constants
  47. static const int MAX_HDR_LEN = 20;
  48. static const char LEGACY_NON_HEADER[] = "<llsd>";
  49. const std::string LLSD_BINARY_HEADER("LLSD/Binary");
  50. const std::string LLSD_XML_HEADER("LLSD/XML");
  51. /**
  52. * LLSDSerialize
  53. */
  54. // static
  55. void LLSDSerialize::serialize(const LLSD& sd, std::ostream& str, ELLSD_Serialize type, U32 options)
  56. {
  57. LLPointer<LLSDFormatter> f = NULL;
  58. switch (type)
  59. {
  60. case LLSD_BINARY:
  61. str << "<? " << LLSD_BINARY_HEADER << " ?>\n";
  62. f = new LLSDBinaryFormatter;
  63. break;
  64. case LLSD_XML:
  65. str << "<? " << LLSD_XML_HEADER << " ?>\n";
  66. f = new LLSDXMLFormatter;
  67. break;
  68. default:
  69. llwarns << "serialize request for unknown ELLSD_Serialize" << llendl;
  70. }
  71. if (f.notNull())
  72. {
  73. f->format(sd, str, options);
  74. }
  75. }
  76. // static
  77. bool LLSDSerialize::deserialize(LLSD& sd, std::istream& str, S32 max_bytes)
  78. {
  79. LLPointer<LLSDParser> p = NULL;
  80. char hdr_buf[MAX_HDR_LEN + 1] = ""; /* Flawfinder: ignore */
  81. int i;
  82. int inbuf = 0;
  83. bool legacy_no_header = false;
  84. bool fail_if_not_legacy = false;
  85. std::string header;
  86. /*
  87. * Get the first line before anything.
  88. */
  89. str.get(hdr_buf, MAX_HDR_LEN, '\n');
  90. if (str.fail())
  91. {
  92. str.clear();
  93. fail_if_not_legacy = true;
  94. }
  95. if (!strncasecmp(LEGACY_NON_HEADER, hdr_buf, strlen(LEGACY_NON_HEADER))) /* Flawfinder: ignore */
  96. {
  97. legacy_no_header = true;
  98. inbuf = str.gcount();
  99. }
  100. else
  101. {
  102. if (fail_if_not_legacy)
  103. goto fail;
  104. /*
  105. * Remove the newline chars
  106. */
  107. for (i = 0; i < MAX_HDR_LEN; i++)
  108. {
  109. if (hdr_buf[i] == 0 || hdr_buf[i] == '\r' ||
  110. hdr_buf[i] == '\n')
  111. {
  112. hdr_buf[i] = 0;
  113. break;
  114. }
  115. }
  116. header = hdr_buf;
  117. std::string::size_type start = std::string::npos;
  118. std::string::size_type end = std::string::npos;
  119. start = header.find_first_not_of("<? ");
  120. if (start != std::string::npos)
  121. {
  122. end = header.find_first_of(" ?", start);
  123. }
  124. if ((start == std::string::npos) || (end == std::string::npos))
  125. goto fail;
  126. header = header.substr(start, end - start);
  127. ws(str);
  128. }
  129. /*
  130. * Create the parser as appropriate
  131. */
  132. if (legacy_no_header)
  133. { // Create a LLSD XML parser, and parse the first chunk read above
  134. LLSDXMLParser* x = new LLSDXMLParser();
  135. x->parsePart(hdr_buf, inbuf); // Parse the first part that was already read
  136. x->parseLines(str, sd); // Parse the rest of it
  137. delete x;
  138. return true;
  139. }
  140. if (header == LLSD_BINARY_HEADER)
  141. {
  142. p = new LLSDBinaryParser;
  143. }
  144. else if (header == LLSD_XML_HEADER)
  145. {
  146. p = new LLSDXMLParser;
  147. }
  148. else
  149. {
  150. llwarns << "deserialize request for unknown ELLSD_Serialize" << llendl;
  151. }
  152. if (p.notNull())
  153. {
  154. p->parse(str, sd, max_bytes);
  155. return true;
  156. }
  157. fail:
  158. llwarns << "deserialize LLSD parse failure" << llendl;
  159. return false;
  160. }
  161. /**
  162. * Endian handlers
  163. */
  164. #if LL_BIG_ENDIAN
  165. U64 ll_htonll(U64 hostlonglong) { return hostlonglong; }
  166. U64 ll_ntohll(U64 netlonglong) { return netlonglong; }
  167. F64 ll_htond(F64 hostlonglong) { return hostlonglong; }
  168. F64 ll_ntohd(F64 netlonglong) { return netlonglong; }
  169. #else
  170. // I read some comments one a indicating that doing an integer add
  171. // here would be faster than a bitwise or. For now, the or has
  172. // programmer clarity, since the intended outcome matches the
  173. // operation.
  174. U64 ll_htonll(U64 hostlonglong)
  175. {
  176. return ((U64)(htonl((U32)((hostlonglong >> 32) & 0xFFFFFFFF))) |
  177. ((U64)(htonl((U32)(hostlonglong & 0xFFFFFFFF))) << 32));
  178. }
  179. U64 ll_ntohll(U64 netlonglong)
  180. {
  181. return ((U64)(ntohl((U32)((netlonglong >> 32) & 0xFFFFFFFF))) |
  182. ((U64)(ntohl((U32)(netlonglong & 0xFFFFFFFF))) << 32));
  183. }
  184. union LLEndianSwapper
  185. {
  186. F64 d;
  187. U64 i;
  188. };
  189. F64 ll_htond(F64 hostdouble)
  190. {
  191. LLEndianSwapper tmp;
  192. tmp.d = hostdouble;
  193. tmp.i = ll_htonll(tmp.i);
  194. return tmp.d;
  195. }
  196. F64 ll_ntohd(F64 netdouble)
  197. {
  198. LLEndianSwapper tmp;
  199. tmp.d = netdouble;
  200. tmp.i = ll_ntohll(tmp.i);
  201. return tmp.d;
  202. }
  203. #endif
  204. /**
  205. * Local functions.
  206. */
  207. /**
  208. * @brief Figure out what kind of string it is (raw or delimited) and handoff.
  209. *
  210. * @param istr The stream to read from.
  211. * @param value [out] The string which was found.
  212. * @param max_bytes The maximum possible length of the string. Passing in
  213. * a negative value will skip this check.
  214. * @return Returns number of bytes read off of the stream. Returns
  215. * PARSE_FAILURE (-1) on failure.
  216. */
  217. int deserialize_string(std::istream& istr, std::string& value, S32 max_bytes);
  218. /**
  219. * @brief Parse a delimited string.
  220. *
  221. * @param istr The stream to read from, with the delimiter already popped.
  222. * @param value [out] The string which was found.
  223. * @param d The delimiter to use.
  224. * @return Returns number of bytes read off of the stream. Returns
  225. * PARSE_FAILURE (-1) on failure.
  226. */
  227. int deserialize_string_delim(std::istream& istr, std::string& value, char d);
  228. /**
  229. * @brief Read a raw string off the stream.
  230. *
  231. * @param istr The stream to read from, with the (len) parameter
  232. * leading the stream.
  233. * @param value [out] The string which was found.
  234. * @param d The delimiter to use.
  235. * @param max_bytes The maximum possible length of the string. Passing in
  236. * a negative value will skip this check.
  237. * @return Returns number of bytes read off of the stream. Returns
  238. * PARSE_FAILURE (-1) on failure.
  239. */
  240. int deserialize_string_raw(
  241. std::istream& istr,
  242. std::string& value,
  243. S32 max_bytes);
  244. /**
  245. * @brief helper method for dealing with the different notation boolean format.
  246. *
  247. * @param istr The stream to read from with the leading character stripped.
  248. * @param data [out] the result of the parse.
  249. * @param compare The string to compare the boolean against
  250. * @param vale The value to assign to data if the parse succeeds.
  251. * @return Returns number of bytes read off of the stream. Returns
  252. * PARSE_FAILURE (-1) on failure.
  253. */
  254. int deserialize_boolean(
  255. std::istream& istr,
  256. LLSD& data,
  257. const std::string& compare,
  258. bool value);
  259. /**
  260. * @brief Do notation escaping of a string to an ostream.
  261. *
  262. * @param value The string to escape and serialize
  263. * @param str The stream to serialize to.
  264. */
  265. void serialize_string(const std::string& value, std::ostream& str);
  266. /**
  267. * Local constants.
  268. */
  269. static const std::string NOTATION_TRUE_SERIAL("true");
  270. static const std::string NOTATION_FALSE_SERIAL("false");
  271. static const char BINARY_TRUE_SERIAL = '1';
  272. static const char BINARY_FALSE_SERIAL = '0';
  273. /**
  274. * LLSDParser
  275. */
  276. LLSDParser::LLSDParser()
  277. : mCheckLimits(true), mMaxBytesLeft(0), mParseLines(false)
  278. {
  279. }
  280. // virtual
  281. LLSDParser::~LLSDParser()
  282. { }
  283. S32 LLSDParser::parse(std::istream& istr, LLSD& data, S32 max_bytes)
  284. {
  285. mCheckLimits = (LLSDSerialize::SIZE_UNLIMITED == max_bytes) ? false : true;
  286. mMaxBytesLeft = max_bytes;
  287. return doParse(istr, data);
  288. }
  289. // Parse using routine to get() lines, faster than parse()
  290. S32 LLSDParser::parseLines(std::istream& istr, LLSD& data)
  291. {
  292. mCheckLimits = false;
  293. mParseLines = true;
  294. return doParse(istr, data);
  295. }
  296. int LLSDParser::get(std::istream& istr) const
  297. {
  298. if(mCheckLimits) --mMaxBytesLeft;
  299. return istr.get();
  300. }
  301. std::istream& LLSDParser::get(
  302. std::istream& istr,
  303. char* s,
  304. std::streamsize n,
  305. char delim) const
  306. {
  307. istr.get(s, n, delim);
  308. if(mCheckLimits) mMaxBytesLeft -= istr.gcount();
  309. return istr;
  310. }
  311. std::istream& LLSDParser::get(
  312. std::istream& istr,
  313. std::streambuf& sb,
  314. char delim) const
  315. {
  316. istr.get(sb, delim);
  317. if(mCheckLimits) mMaxBytesLeft -= istr.gcount();
  318. return istr;
  319. }
  320. std::istream& LLSDParser::ignore(std::istream& istr) const
  321. {
  322. istr.ignore();
  323. if(mCheckLimits) --mMaxBytesLeft;
  324. return istr;
  325. }
  326. std::istream& LLSDParser::putback(std::istream& istr, char c) const
  327. {
  328. istr.putback(c);
  329. if(mCheckLimits) ++mMaxBytesLeft;
  330. return istr;
  331. }
  332. std::istream& LLSDParser::read(
  333. std::istream& istr,
  334. char* s,
  335. std::streamsize n) const
  336. {
  337. istr.read(s, n);
  338. if(mCheckLimits) mMaxBytesLeft -= istr.gcount();
  339. return istr;
  340. }
  341. void LLSDParser::account(S32 bytes) const
  342. {
  343. if(mCheckLimits) mMaxBytesLeft -= bytes;
  344. }
  345. /**
  346. * LLSDNotationParser
  347. */
  348. LLSDNotationParser::LLSDNotationParser()
  349. {
  350. }
  351. // virtual
  352. LLSDNotationParser::~LLSDNotationParser()
  353. { }
  354. // virtual
  355. S32 LLSDNotationParser::doParse(std::istream& istr, LLSD& data) const
  356. {
  357. // map: { string:object, string:object }
  358. // array: [ object, object, object ]
  359. // undef: !
  360. // boolean: true | false | 1 | 0 | T | F | t | f | TRUE | FALSE
  361. // integer: i####
  362. // real: r####
  363. // uuid: u####
  364. // string: "g'day" | 'have a "nice" day' | s(size)"raw data"
  365. // uri: l"escaped"
  366. // date: d"YYYY-MM-DDTHH:MM:SS.FFZ"
  367. // binary: b##"ff3120ab1" | b(size)"raw data"
  368. char c;
  369. c = istr.peek();
  370. while(isspace(c))
  371. {
  372. // pop the whitespace.
  373. c = get(istr);
  374. c = istr.peek();
  375. continue;
  376. }
  377. if(!istr.good())
  378. {
  379. return 0;
  380. }
  381. S32 parse_count = 1;
  382. switch(c)
  383. {
  384. case '{':
  385. {
  386. S32 child_count = parseMap(istr, data);
  387. if((child_count == PARSE_FAILURE) || data.isUndefined())
  388. {
  389. parse_count = PARSE_FAILURE;
  390. }
  391. else
  392. {
  393. parse_count += child_count;
  394. }
  395. if(istr.fail())
  396. {
  397. llinfos << "STREAM FAILURE reading map." << llendl;
  398. parse_count = PARSE_FAILURE;
  399. }
  400. break;
  401. }
  402. case '[':
  403. {
  404. S32 child_count = parseArray(istr, data);
  405. if((child_count == PARSE_FAILURE) || data.isUndefined())
  406. {
  407. parse_count = PARSE_FAILURE;
  408. }
  409. else
  410. {
  411. parse_count += child_count;
  412. }
  413. if(istr.fail())
  414. {
  415. llinfos << "STREAM FAILURE reading array." << llendl;
  416. parse_count = PARSE_FAILURE;
  417. }
  418. break;
  419. }
  420. case '!':
  421. c = get(istr);
  422. data.clear();
  423. break;
  424. case '0':
  425. c = get(istr);
  426. data = false;
  427. break;
  428. case 'F':
  429. case 'f':
  430. ignore(istr);
  431. c = istr.peek();
  432. if(isalpha(c))
  433. {
  434. int cnt = deserialize_boolean(
  435. istr,
  436. data,
  437. NOTATION_FALSE_SERIAL,
  438. false);
  439. if(PARSE_FAILURE == cnt) parse_count = cnt;
  440. else account(cnt);
  441. }
  442. else
  443. {
  444. data = false;
  445. }
  446. if(istr.fail())
  447. {
  448. llinfos << "STREAM FAILURE reading boolean." << llendl;
  449. parse_count = PARSE_FAILURE;
  450. }
  451. break;
  452. case '1':
  453. c = get(istr);
  454. data = true;
  455. break;
  456. case 'T':
  457. case 't':
  458. ignore(istr);
  459. c = istr.peek();
  460. if(isalpha(c))
  461. {
  462. int cnt = deserialize_boolean(istr,data,NOTATION_TRUE_SERIAL,true);
  463. if(PARSE_FAILURE == cnt) parse_count = cnt;
  464. else account(cnt);
  465. }
  466. else
  467. {
  468. data = true;
  469. }
  470. if(istr.fail())
  471. {
  472. llinfos << "STREAM FAILURE reading boolean." << llendl;
  473. parse_count = PARSE_FAILURE;
  474. }
  475. break;
  476. case 'i':
  477. {
  478. c = get(istr);
  479. S32 integer = 0;
  480. istr >> integer;
  481. data = integer;
  482. if(istr.fail())
  483. {
  484. llinfos << "STREAM FAILURE reading integer." << llendl;
  485. parse_count = PARSE_FAILURE;
  486. }
  487. break;
  488. }
  489. case 'r':
  490. {
  491. c = get(istr);
  492. F64 real = 0.0;
  493. istr >> real;
  494. data = real;
  495. if(istr.fail())
  496. {
  497. llinfos << "STREAM FAILURE reading real." << llendl;
  498. parse_count = PARSE_FAILURE;
  499. }
  500. break;
  501. }
  502. case 'u':
  503. {
  504. c = get(istr);
  505. LLUUID id;
  506. istr >> id;
  507. data = id;
  508. if(istr.fail())
  509. {
  510. llinfos << "STREAM FAILURE reading uuid." << llendl;
  511. parse_count = PARSE_FAILURE;
  512. }
  513. break;
  514. }
  515. case '\"':
  516. case '\'':
  517. case 's':
  518. if(!parseString(istr, data))
  519. {
  520. parse_count = PARSE_FAILURE;
  521. }
  522. if(istr.fail())
  523. {
  524. llinfos << "STREAM FAILURE reading string." << llendl;
  525. parse_count = PARSE_FAILURE;
  526. }
  527. break;
  528. case 'l':
  529. {
  530. c = get(istr); // pop the 'l'
  531. c = get(istr); // pop the delimiter
  532. std::string str;
  533. int cnt = deserialize_string_delim(istr, str, c);
  534. if(PARSE_FAILURE == cnt)
  535. {
  536. parse_count = PARSE_FAILURE;
  537. }
  538. else
  539. {
  540. data = LLURI(str);
  541. account(cnt);
  542. }
  543. if(istr.fail())
  544. {
  545. llinfos << "STREAM FAILURE reading link." << llendl;
  546. parse_count = PARSE_FAILURE;
  547. }
  548. break;
  549. }
  550. case 'd':
  551. {
  552. c = get(istr); // pop the 'd'
  553. c = get(istr); // pop the delimiter
  554. std::string str;
  555. int cnt = deserialize_string_delim(istr, str, c);
  556. if(PARSE_FAILURE == cnt)
  557. {
  558. parse_count = PARSE_FAILURE;
  559. }
  560. else
  561. {
  562. data = LLDate(str);
  563. account(cnt);
  564. }
  565. if(istr.fail())
  566. {
  567. llinfos << "STREAM FAILURE reading date." << llendl;
  568. parse_count = PARSE_FAILURE;
  569. }
  570. break;
  571. }
  572. case 'b':
  573. if(!parseBinary(istr, data))
  574. {
  575. parse_count = PARSE_FAILURE;
  576. }
  577. if(istr.fail())
  578. {
  579. llinfos << "STREAM FAILURE reading data." << llendl;
  580. parse_count = PARSE_FAILURE;
  581. }
  582. break;
  583. default:
  584. parse_count = PARSE_FAILURE;
  585. llinfos << "Unrecognized character while parsing: int(" << (int)c
  586. << ")" << llendl;
  587. break;
  588. }
  589. if(PARSE_FAILURE == parse_count)
  590. {
  591. data.clear();
  592. }
  593. return parse_count;
  594. }
  595. S32 LLSDNotationParser::parseMap(std::istream& istr, LLSD& map) const
  596. {
  597. // map: { string:object, string:object }
  598. map = LLSD::emptyMap();
  599. S32 parse_count = 0;
  600. char c = get(istr);
  601. if(c == '{')
  602. {
  603. // eat commas, white
  604. bool found_name = false;
  605. std::string name;
  606. c = get(istr);
  607. while(c != '}' && istr.good())
  608. {
  609. if(!found_name)
  610. {
  611. if((c == '\"') || (c == '\'') || (c == 's'))
  612. {
  613. putback(istr, c);
  614. found_name = true;
  615. int count = deserialize_string(istr, name, mMaxBytesLeft);
  616. if(PARSE_FAILURE == count) return PARSE_FAILURE;
  617. account(count);
  618. }
  619. c = get(istr);
  620. }
  621. else
  622. {
  623. if(isspace(c) || (c == ':'))
  624. {
  625. c = get(istr);
  626. continue;
  627. }
  628. putback(istr, c);
  629. LLSD child;
  630. S32 count = doParse(istr, child);
  631. if(count > 0)
  632. {
  633. // There must be a value for every key, thus
  634. // child_count must be greater than 0.
  635. parse_count += count;
  636. map.insert(name, child);
  637. }
  638. else
  639. {
  640. return PARSE_FAILURE;
  641. }
  642. found_name = false;
  643. c = get(istr);
  644. }
  645. }
  646. if(c != '}')
  647. {
  648. map.clear();
  649. return PARSE_FAILURE;
  650. }
  651. }
  652. return parse_count;
  653. }
  654. S32 LLSDNotationParser::parseArray(std::istream& istr, LLSD& array) const
  655. {
  656. // array: [ object, object, object ]
  657. array = LLSD::emptyArray();
  658. S32 parse_count = 0;
  659. char c = get(istr);
  660. if(c == '[')
  661. {
  662. // eat commas, white
  663. c = get(istr);
  664. while((c != ']') && istr.good())
  665. {
  666. LLSD child;
  667. if(isspace(c) || (c == ','))
  668. {
  669. c = get(istr);
  670. continue;
  671. }
  672. putback(istr, c);
  673. S32 count = doParse(istr, child);
  674. if(PARSE_FAILURE == count)
  675. {
  676. return PARSE_FAILURE;
  677. }
  678. else
  679. {
  680. parse_count += count;
  681. array.append(child);
  682. }
  683. c = get(istr);
  684. }
  685. if(c != ']')
  686. {
  687. return PARSE_FAILURE;
  688. }
  689. }
  690. return parse_count;
  691. }
  692. bool LLSDNotationParser::parseString(std::istream& istr, LLSD& data) const
  693. {
  694. std::string value;
  695. int count = deserialize_string(istr, value, mMaxBytesLeft);
  696. if(PARSE_FAILURE == count) return false;
  697. account(count);
  698. data = value;
  699. return true;
  700. }
  701. bool LLSDNotationParser::parseBinary(std::istream& istr, LLSD& data) const
  702. {
  703. // binary: b##"ff3120ab1"
  704. // or: b(len)"..."
  705. // I want to manually control those values here to make sure the
  706. // parser doesn't break when someone changes a constant somewhere
  707. // else.
  708. const U32 BINARY_BUFFER_SIZE = 256;
  709. const U32 STREAM_GET_COUNT = 255;
  710. // need to read the base out.
  711. char buf[BINARY_BUFFER_SIZE]; /* Flawfinder: ignore */
  712. get(istr, buf, STREAM_GET_COUNT, '"');
  713. char c = get(istr);
  714. if(c != '"') return false;
  715. if(0 == strncmp("b(", buf, 2))
  716. {
  717. // We probably have a valid raw binary stream. determine
  718. // the size, and read it.
  719. S32 len = strtol(buf + 2, NULL, 0);
  720. if(mCheckLimits && (len > mMaxBytesLeft)) return false;
  721. std::vector<U8> value;
  722. if(len)
  723. {
  724. value.resize(len);
  725. account(fullread(istr, (char *)&value[0], len));
  726. }
  727. c = get(istr); // strip off the trailing double-quote
  728. data = value;
  729. }
  730. else if(0 == strncmp("b64", buf, 3))
  731. {
  732. // *FIX: A bit inefficient, but works for now. To make the
  733. // format better, I would need to add a hint into the
  734. // serialization format that indicated how long it was.
  735. std::stringstream coded_stream;
  736. get(istr, *(coded_stream.rdbuf()), '\"');
  737. c = get(istr);
  738. std::string encoded(coded_stream.str());
  739. S32 len = apr_base64_decode_len(encoded.c_str());
  740. std::vector<U8> value;
  741. if(len)
  742. {
  743. value.resize(len);
  744. len = apr_base64_decode_binary(&value[0], encoded.c_str());
  745. value.resize(len);
  746. }
  747. data = value;
  748. }
  749. else if(0 == strncmp("b16", buf, 3))
  750. {
  751. // yay, base 16. We pop the next character which is either a
  752. // double quote or base 16 data. If it's a double quote, we're
  753. // done parsing. If it's not, put the data back, and read the
  754. // stream until the next double quote.
  755. char* read; /*Flawfinder: ignore*/
  756. U8 byte;
  757. U8 byte_buffer[BINARY_BUFFER_SIZE];
  758. U8* write;
  759. std::vector<U8> value;
  760. c = get(istr);
  761. while(c != '"')
  762. {
  763. putback(istr, c);
  764. read = buf;
  765. write = byte_buffer;
  766. get(istr, buf, STREAM_GET_COUNT, '"');
  767. c = get(istr);
  768. while(*read != '\0') /*Flawfinder: ignore*/
  769. {
  770. byte = hex_as_nybble(*read++);
  771. byte = byte << 4;
  772. byte |= hex_as_nybble(*read++);
  773. *write++ = byte;
  774. }
  775. // copy the data out of the byte buffer
  776. value.insert(value.end(), byte_buffer, write);
  777. }
  778. data = value;
  779. }
  780. else
  781. {
  782. return false;
  783. }
  784. return true;
  785. }
  786. /**
  787. * LLSDBinaryParser
  788. */
  789. LLSDBinaryParser::LLSDBinaryParser()
  790. {
  791. }
  792. // virtual
  793. LLSDBinaryParser::~LLSDBinaryParser()
  794. {
  795. }
  796. // virtual
  797. S32 LLSDBinaryParser::doParse(std::istream& istr, LLSD& data) const
  798. {
  799. /**
  800. * Undefined: '!'<br>
  801. * Boolean: 't' for true 'f' for false<br>
  802. * Integer: 'i' + 4 bytes network byte order<br>
  803. * Real: 'r' + 8 bytes IEEE double<br>
  804. * UUID: 'u' + 16 byte unsigned integer<br>
  805. * String: 's' + 4 byte integer size + string<br>
  806. * strings also secretly support the notation format
  807. * Date: 'd' + 8 byte IEEE double for seconds since epoch<br>
  808. * URI: 'l' + 4 byte integer size + string uri<br>
  809. * Binary: 'b' + 4 byte integer size + binary data<br>
  810. * Array: '[' + 4 byte integer size + all values + ']'<br>
  811. * Map: '{' + 4 byte integer size every(key + value) + '}'<br>
  812. * map keys are serialized as s + 4 byte integer size + string or in the
  813. * notation format.
  814. */
  815. char c;
  816. c = get(istr);
  817. if(!istr.good())
  818. {
  819. return 0;
  820. }
  821. S32 parse_count = 1;
  822. switch(c)
  823. {
  824. case '{':
  825. {
  826. S32 child_count = parseMap(istr, data);
  827. if((child_count == PARSE_FAILURE) || data.isUndefined())
  828. {
  829. parse_count = PARSE_FAILURE;
  830. }
  831. else
  832. {
  833. parse_count += child_count;
  834. }
  835. if(istr.fail())
  836. {
  837. llinfos << "STREAM FAILURE reading binary map." << llendl;
  838. parse_count = PARSE_FAILURE;
  839. }
  840. break;
  841. }
  842. case '[':
  843. {
  844. S32 child_count = parseArray(istr, data);
  845. if((child_count == PARSE_FAILURE) || data.isUndefined())
  846. {
  847. parse_count = PARSE_FAILURE;
  848. }
  849. else
  850. {
  851. parse_count += child_count;
  852. }
  853. if(istr.fail())
  854. {
  855. llinfos << "STREAM FAILURE reading binary array." << llendl;
  856. parse_count = PARSE_FAILURE;
  857. }
  858. break;
  859. }
  860. case '!':
  861. data.clear();
  862. break;
  863. case '0':
  864. data = false;
  865. break;
  866. case '1':
  867. data = true;
  868. break;
  869. case 'i':
  870. {
  871. U32 value_nbo = 0;
  872. read(istr, (char*)&value_nbo, sizeof(U32)); /*Flawfinder: ignore*/
  873. data = (S32)ntohl(value_nbo);
  874. if(istr.fail())
  875. {
  876. llinfos << "STREAM FAILURE reading binary integer." << llendl;
  877. }
  878. break;
  879. }
  880. case 'r':
  881. {
  882. F64 real_nbo = 0.0;
  883. read(istr, (char*)&real_nbo, sizeof(F64)); /*Flawfinder: ignore*/
  884. data = ll_ntohd(real_nbo);
  885. if(istr.fail())
  886. {
  887. llinfos << "STREAM FAILURE reading binary real." << llendl;
  888. }
  889. break;
  890. }
  891. case 'u':
  892. {
  893. LLUUID id;
  894. read(istr, (char*)(&id.mData), UUID_BYTES); /*Flawfinder: ignore*/
  895. data = id;
  896. if(istr.fail())
  897. {
  898. llinfos << "STREAM FAILURE reading binary uuid." << llendl;
  899. }
  900. break;
  901. }
  902. case '\'':
  903. case '"':
  904. {
  905. std::string value;
  906. int cnt = deserialize_string_delim(istr, value, c);
  907. if(PARSE_FAILURE == cnt)
  908. {
  909. parse_count = PARSE_FAILURE;
  910. }
  911. else
  912. {
  913. data = value;
  914. account(cnt);
  915. }
  916. if(istr.fail())
  917. {
  918. llinfos << "STREAM FAILURE reading binary (notation-style) string."
  919. << llendl;
  920. parse_count = PARSE_FAILURE;
  921. }
  922. break;
  923. }
  924. case 's':
  925. {
  926. std::string value;
  927. if(parseString(istr, value))
  928. {
  929. data = value;
  930. }
  931. else
  932. {
  933. parse_count = PARSE_FAILURE;
  934. }
  935. if(istr.fail())
  936. {
  937. llinfos << "STREAM FAILURE reading binary string." << llendl;
  938. parse_count = PARSE_FAILURE;
  939. }
  940. break;
  941. }
  942. case 'l':
  943. {
  944. std::string value;
  945. if(parseString(istr, value))
  946. {
  947. data = LLURI(value);
  948. }
  949. else
  950. {
  951. parse_count = PARSE_FAILURE;
  952. }
  953. if(istr.fail())
  954. {
  955. llinfos << "STREAM FAILURE reading binary link." << llendl;
  956. parse_count = PARSE_FAILURE;
  957. }
  958. break;
  959. }
  960. case 'd':
  961. {
  962. F64 real = 0.0;
  963. read(istr, (char*)&real, sizeof(F64)); /*Flawfinder: ignore*/
  964. data = LLDate(real);
  965. if(istr.fail())
  966. {
  967. llinfos << "STREAM FAILURE reading binary date." << llendl;
  968. parse_count = PARSE_FAILURE;
  969. }
  970. break;
  971. }
  972. case 'b':
  973. {
  974. // We probably have a valid raw binary stream. determine
  975. // the size, and read it.
  976. U32 size_nbo = 0;
  977. read(istr, (char*)&size_nbo, sizeof(U32)); /*Flawfinder: ignore*/
  978. S32 size = (S32)ntohl(size_nbo);
  979. if(mCheckLimits && (size > mMaxBytesLeft))
  980. {
  981. parse_count = PARSE_FAILURE;
  982. }
  983. else
  984. {
  985. std::vector<U8> value;
  986. if(size > 0)
  987. {
  988. value.resize(size);
  989. account(fullread(istr, (char*)&value[0], size));
  990. }
  991. data = value;
  992. }
  993. if(istr.fail())
  994. {
  995. llinfos << "STREAM FAILURE reading binary." << llendl;
  996. parse_count = PARSE_FAILURE;
  997. }
  998. break;
  999. }
  1000. default:
  1001. parse_count = PARSE_FAILURE;
  1002. llinfos << "Unrecognized character while parsing: int(" << (int)c
  1003. << ")" << llendl;
  1004. break;
  1005. }
  1006. if(PARSE_FAILURE == parse_count)
  1007. {
  1008. data.clear();
  1009. }
  1010. return parse_count;
  1011. }
  1012. S32 LLSDBinaryParser::parseMap(std::istream& istr, LLSD& map) const
  1013. {
  1014. map = LLSD::emptyMap();
  1015. U32 value_nbo = 0;
  1016. read(istr, (char*)&value_nbo, sizeof(U32)); /*Flawfinder: ignore*/
  1017. S32 size = (S32)ntohl(value_nbo);
  1018. S32 parse_count = 0;
  1019. S32 count = 0;
  1020. char c = get(istr);
  1021. while(c != '}' && (count < size) && istr.good())
  1022. {
  1023. std::string name;
  1024. switch(c)
  1025. {
  1026. case 'k':
  1027. if(!parseString(istr, name))
  1028. {
  1029. return PARSE_FAILURE;
  1030. }
  1031. break;
  1032. case '\'':
  1033. case '"':
  1034. {
  1035. int cnt = deserialize_string_delim(istr, name, c);
  1036. if(PARSE_FAILURE == cnt) return PARSE_FAILURE;
  1037. account(cnt);
  1038. break;
  1039. }
  1040. }
  1041. LLSD child;
  1042. S32 child_count = doParse(istr, child);
  1043. if(child_count > 0)
  1044. {
  1045. // There must be a value for every key, thus child_count
  1046. // must be greater than 0.
  1047. parse_count += child_count;
  1048. map.insert(name, child);
  1049. }
  1050. else
  1051. {
  1052. return PARSE_FAILURE;
  1053. }
  1054. ++count;
  1055. c = get(istr);
  1056. }
  1057. if((c != '}') || (count < size))
  1058. {
  1059. // Make sure it is correctly terminated and we parsed as many
  1060. // as were said to be there.
  1061. return PARSE_FAILURE;
  1062. }
  1063. return parse_count;
  1064. }
  1065. S32 LLSDBinaryParser::parseArray(std::istream& istr, LLSD& array) const
  1066. {
  1067. array = LLSD::emptyArray();
  1068. U32 value_nbo = 0;
  1069. read(istr, (char*)&value_nbo, sizeof(U32)); /*Flawfinder: ignore*/
  1070. S32 size = (S32)ntohl(value_nbo);
  1071. // *FIX: This would be a good place to reserve some space in the
  1072. // array...
  1073. S32 parse_count = 0;
  1074. S32 count = 0;
  1075. char c = istr.peek();
  1076. while((c != ']') && (count < size) && istr.good())
  1077. {
  1078. LLSD child;
  1079. S32 child_count = doParse(istr, child);
  1080. if(PARSE_FAILURE == child_count)
  1081. {
  1082. return PARSE_FAILURE;
  1083. }
  1084. if(child_count)
  1085. {
  1086. parse_count += child_count;
  1087. array.append(child);
  1088. }
  1089. ++count;
  1090. c = istr.peek();
  1091. }
  1092. c = get(istr);
  1093. if((c != ']') || (count < size))
  1094. {
  1095. // Make sure it is correctly terminated and we parsed as many
  1096. // as were said to be there.
  1097. return PARSE_FAILURE;
  1098. }
  1099. return parse_count;
  1100. }
  1101. bool LLSDBinaryParser::parseString(
  1102. std::istream& istr,
  1103. std::string& value) const
  1104. {
  1105. // *FIX: This is memory inefficient.
  1106. U32 value_nbo = 0;
  1107. read(istr, (char*)&value_nbo, sizeof(U32)); /*Flawfinder: ignore*/
  1108. S32 size = (S32)ntohl(value_nbo);
  1109. if(mCheckLimits && (size > mMaxBytesLeft)) return false;
  1110. std::vector<char> buf;
  1111. if(size)
  1112. {
  1113. buf.resize(size);
  1114. account(fullread(istr, &buf[0], size));
  1115. value.assign(buf.begin(), buf.end());
  1116. }
  1117. return true;
  1118. }
  1119. /**
  1120. * LLSDFormatter
  1121. */
  1122. LLSDFormatter::LLSDFormatter() :
  1123. mBoolAlpha(false)
  1124. {
  1125. }
  1126. // virtual
  1127. LLSDFormatter::~LLSDFormatter()
  1128. { }
  1129. void LLSDFormatter::boolalpha(bool alpha)
  1130. {
  1131. mBoolAlpha = alpha;
  1132. }
  1133. void LLSDFormatter::realFormat(const std::string& format)
  1134. {
  1135. mRealFormat = format;
  1136. }
  1137. void LLSDFormatter::formatReal(LLSD::Real real, std::ostream& ostr) const
  1138. {
  1139. std::string buffer = llformat(mRealFormat.c_str(), real);
  1140. ostr << buffer;
  1141. }
  1142. /**
  1143. * LLSDNotationFormatter
  1144. */
  1145. LLSDNotationFormatter::LLSDNotationFormatter()
  1146. {
  1147. }
  1148. // virtual
  1149. LLSDNotationFormatter::~LLSDNotationFormatter()
  1150. { }
  1151. // static
  1152. std::string LLSDNotationFormatter::escapeString(const std::string& in)
  1153. {
  1154. std::ostringstream ostr;
  1155. serialize_string(in, ostr);
  1156. return ostr.str();
  1157. }
  1158. // virtual
  1159. S32 LLSDNotationFormatter::format(const LLSD& data, std::ostream& ostr, U32 options) const
  1160. {
  1161. S32 format_count = 1;
  1162. switch(data.type())
  1163. {
  1164. case LLSD::TypeMap:
  1165. {
  1166. ostr << "{";
  1167. bool need_comma = false;
  1168. LLSD::map_const_iterator iter = data.beginMap();
  1169. LLSD::map_const_iterator end = data.endMap();
  1170. for(; iter != end; ++iter)
  1171. {
  1172. if(need_comma) ostr << ",";
  1173. need_comma = true;
  1174. ostr << '\'';
  1175. serialize_string((*iter).first, ostr);
  1176. ostr << "':";
  1177. format_count += format((*iter).second, ostr);
  1178. }
  1179. ostr << "}";
  1180. break;
  1181. }
  1182. case LLSD::TypeArray:
  1183. {
  1184. ostr << "[";
  1185. bool need_comma = false;
  1186. LLSD::array_const_iterator iter = data.beginArray();
  1187. LLSD::array_const_iterator end = data.endArray();
  1188. for(; iter != end; ++iter)
  1189. {
  1190. if(need_comma) ostr << ",";
  1191. need_comma = true;
  1192. format_count += format(*iter, ostr);
  1193. }
  1194. ostr << "]";
  1195. break;
  1196. }
  1197. case LLSD::TypeUndefined:
  1198. ostr << "!";
  1199. break;
  1200. case LLSD::TypeBoolean:
  1201. if(mBoolAlpha ||
  1202. #if( LL_WINDOWS || __GNUC__ > 2)
  1203. (ostr.flags() & std::ios::boolalpha)
  1204. #else
  1205. (ostr.flags() & 0x0100)
  1206. #endif
  1207. )
  1208. {
  1209. ostr << (data.asBoolean()
  1210. ? NOTATION_TRUE_SERIAL : NOTATION_FALSE_SERIAL);
  1211. }
  1212. else
  1213. {
  1214. ostr << (data.asBoolean() ? 1 : 0);
  1215. }
  1216. break;
  1217. case LLSD::TypeInteger:
  1218. ostr << "i" << data.asInteger();
  1219. break;
  1220. case LLSD::TypeReal:
  1221. ostr << "r";
  1222. if(mRealFormat.empty())
  1223. {
  1224. ostr << data.asReal();
  1225. }
  1226. else
  1227. {
  1228. formatReal(data.asReal(), ostr);
  1229. }
  1230. break;
  1231. case LLSD::TypeUUID:
  1232. ostr << "u" << data.asUUID();
  1233. break;
  1234. case LLSD::TypeString:
  1235. ostr << '\'';
  1236. serialize_string(data.asString(), ostr);
  1237. ostr << '\'';
  1238. break;
  1239. case LLSD::TypeDate:
  1240. ostr << "d\"" << data.asDate() << "\"";
  1241. break;
  1242. case LLSD::TypeURI:
  1243. ostr << "l\"";
  1244. serialize_string(data.asString(), ostr);
  1245. ostr << "\"";
  1246. break;
  1247. case LLSD::TypeBinary:
  1248. {
  1249. // *FIX: memory inefficient.
  1250. std::vector<U8> buffer = data.asBinary();
  1251. ostr << "b(" << buffer.size() << ")\"";
  1252. if(buffer.size()) ostr.write((const char*)&buffer[0], buffer.size());
  1253. ostr << "\"";
  1254. break;
  1255. }
  1256. default:
  1257. // *NOTE: This should never happen.
  1258. ostr << "!";
  1259. break;
  1260. }
  1261. return format_count;
  1262. }
  1263. /**
  1264. * LLSDBinaryFormatter
  1265. */
  1266. LLSDBinaryFormatter::LLSDBinaryFormatter()
  1267. {
  1268. }
  1269. // virtual
  1270. LLSDBinaryFormatter::~LLSDBinaryFormatter()
  1271. { }
  1272. // virtual
  1273. S32 LLSDBinaryFormatter::format(const LLSD& data, std::ostream& ostr, U32 options) const
  1274. {
  1275. S32 format_count = 1;
  1276. switch(data.type())
  1277. {
  1278. case LLSD::TypeMap:
  1279. {
  1280. ostr.put('{');
  1281. U32 size_nbo = htonl(data.size());
  1282. ostr.write((const char*)(&size_nbo), sizeof(U32));
  1283. LLSD::map_const_iterator iter = data.beginMap();
  1284. LLSD::map_const_iterator end = data.endMap();
  1285. for(; iter != end; ++iter)
  1286. {
  1287. ostr.put('k');
  1288. formatString((*iter).first, ostr);
  1289. format_count += format((*iter).second, ostr);
  1290. }
  1291. ostr.put('}');
  1292. break;
  1293. }
  1294. case LLSD::TypeArray:
  1295. {
  1296. ostr.put('[');
  1297. U32 size_nbo = htonl(data.size());
  1298. ostr.write((const char*)(&size_nbo), sizeof(U32));
  1299. LLSD::array_const_iterator iter = data.beginArray();
  1300. LLSD::array_const_iterator end = data.endArray();
  1301. for(; iter != end; ++iter)
  1302. {
  1303. format_count += format(*iter, ostr);
  1304. }
  1305. ostr.put(']');
  1306. break;
  1307. }
  1308. case LLSD::TypeUndefined:
  1309. ostr.put('!');
  1310. break;
  1311. case LLSD::TypeBoolean:
  1312. if(data.asBoolean()) ostr.put(BINARY_TRUE_SERIAL);
  1313. else ostr.put(BINARY_FALSE_SERIAL);
  1314. break;
  1315. case LLSD::TypeInteger:
  1316. {
  1317. ostr.put('i');
  1318. U32 value_nbo = htonl(data.asInteger());
  1319. ostr.write((const char*)(&value_nbo), sizeof(U32));
  1320. break;
  1321. }
  1322. case LLSD::TypeReal:
  1323. {
  1324. ostr.put('r');
  1325. F64 value_nbo = ll_htond(data.asReal());
  1326. ostr.write((const char*)(&value_nbo), sizeof(F64));
  1327. break;
  1328. }
  1329. case LLSD::TypeUUID:
  1330. ostr.put('u');
  1331. ostr.write((const char*)(&(data.asUUID().mData)), UUID_BYTES);
  1332. break;
  1333. case LLSD::TypeString:
  1334. ostr.put('s');
  1335. formatString(data.asString(), ostr);
  1336. break;
  1337. case LLSD::TypeDate:
  1338. {
  1339. ostr.put('d');
  1340. F64 value = data.asReal();
  1341. ostr.write((const char*)(&value), sizeof(F64));
  1342. break;
  1343. }
  1344. case LLSD::TypeURI:
  1345. ostr.put('l');
  1346. formatString(data.asString(), ostr);
  1347. break;
  1348. case LLSD::TypeBinary:
  1349. {
  1350. // *FIX: memory inefficient.
  1351. ostr.put('b');
  1352. std::vector<U8> buffer = data.asBinary();
  1353. U32 size_nbo = htonl(buffer.size());
  1354. ostr.write((const char*)(&size_nbo), sizeof(U32));
  1355. if(buffer.size()) ostr.write((const char*)&buffer[0], buffer.size());
  1356. break;
  1357. }
  1358. default:
  1359. // *NOTE: This should never happen.
  1360. ostr.put('!');
  1361. break;
  1362. }
  1363. return format_count;
  1364. }
  1365. void LLSDBinaryFormatter::formatString(
  1366. const std::string& string,
  1367. std::ostream& ostr) const
  1368. {
  1369. U32 size_nbo = htonl(string.size());
  1370. ostr.write((const char*)(&size_nbo), sizeof(U32));
  1371. ostr.write(string.c_str(), string.size());
  1372. }
  1373. /**
  1374. * local functions
  1375. */
  1376. int deserialize_string(std::istream& istr, std::string& value, S32 max_bytes)
  1377. {
  1378. int c = istr.get();
  1379. if(istr.fail())
  1380. {
  1381. // No data in stream, bail out but mention the character we
  1382. // grabbed.
  1383. return LLSDParser::PARSE_FAILURE;
  1384. }
  1385. int rv = LLSDParser::PARSE_FAILURE;
  1386. switch(c)
  1387. {
  1388. case '\'':
  1389. case '"':
  1390. rv = deserialize_string_delim(istr, value, c);
  1391. break;
  1392. case 's':
  1393. // technically, less than max_bytes, but this is just meant to
  1394. // catch egregious protocol errors. parse errors will be
  1395. // caught in the case of incorrect counts.
  1396. rv = deserialize_string_raw(istr, value, max_bytes);
  1397. break;
  1398. default:
  1399. break;
  1400. }
  1401. if(LLSDParser::PARSE_FAILURE == rv) return rv;
  1402. return rv + 1; // account for the character grabbed at the top.
  1403. }
  1404. int deserialize_string_delim(
  1405. std::istream& istr,
  1406. std::string& value,
  1407. char delim)
  1408. {
  1409. std::ostringstream write_buffer;
  1410. bool found_escape = false;
  1411. bool found_hex = false;
  1412. bool found_digit = false;
  1413. U8 byte = 0;
  1414. int count = 0;
  1415. while (true)
  1416. {
  1417. int next_byte = istr.get();
  1418. ++count;
  1419. if(istr.fail())
  1420. {
  1421. // If our stream is empty, break out
  1422. value = write_buffer.str();
  1423. return LLSDParser::PARSE_FAILURE;
  1424. }
  1425. char next_char = (char)next_byte; // Now that we know it's not EOF
  1426. if(found_escape)
  1427. {
  1428. // next character(s) is a special sequence.
  1429. if(found_hex)
  1430. {
  1431. if(found_digit)
  1432. {
  1433. found_digit = false;
  1434. found_hex = false;
  1435. found_escape = false;
  1436. byte = byte << 4;
  1437. byte |= hex_as_nybble(next_char);
  1438. write_buffer << byte;
  1439. byte = 0;
  1440. }
  1441. else
  1442. {
  1443. // next character is the first nybble of
  1444. //
  1445. found_digit = true;
  1446. byte = hex_as_nybble(next_char);
  1447. }
  1448. }
  1449. else if(next_char == 'x')
  1450. {
  1451. found_hex = true;
  1452. }
  1453. else
  1454. {
  1455. switch(next_char)
  1456. {
  1457. case 'a':
  1458. write_buffer << '\a';
  1459. break;
  1460. case 'b':
  1461. write_buffer << '\b';
  1462. break;
  1463. case 'f':
  1464. write_buffer << '\f';
  1465. break;
  1466. case 'n':
  1467. write_buffer << '\n';
  1468. break;
  1469. case 'r':
  1470. write_buffer << '\r';
  1471. break;
  1472. case 't':
  1473. write_buffer << '\t';
  1474. break;
  1475. case 'v':
  1476. write_buffer << '\v';
  1477. break;
  1478. default:
  1479. write_buffer << next_char;
  1480. break;
  1481. }
  1482. found_escape = false;
  1483. }
  1484. }
  1485. else if(next_char == '\\')
  1486. {
  1487. found_escape = true;
  1488. }
  1489. else if(next_char == delim)
  1490. {
  1491. break;
  1492. }
  1493. else
  1494. {
  1495. write_buffer << next_char;
  1496. }
  1497. }
  1498. value = write_buffer.str();
  1499. return count;
  1500. }
  1501. int deserialize_string_raw(
  1502. std::istream& istr,
  1503. std::string& value,
  1504. S32 max_bytes)
  1505. {
  1506. int count = 0;
  1507. const S32 BUF_LEN = 20;
  1508. char buf[BUF_LEN]; /* Flawfinder: ignore */
  1509. istr.get(buf, BUF_LEN - 1, ')');
  1510. count += istr.gcount();
  1511. int c = istr.get();
  1512. c = istr.get();
  1513. count += 2;
  1514. if(((c == '"') || (c == '\'')) && (buf[0] == '('))
  1515. {
  1516. // We probably have a valid raw string. determine
  1517. // the size, and read it.
  1518. // *FIX: This is memory inefficient.
  1519. S32 len = strtol(buf + 1, NULL, 0);
  1520. if((max_bytes>0)&&(len>max_bytes)) return LLSDParser::PARSE_FAILURE;
  1521. std::vector<char> buf;
  1522. if(len)
  1523. {
  1524. buf.resize(len);
  1525. count += fullread(istr, (char *)&buf[0], len);
  1526. value.assign(buf.begin(), buf.end());
  1527. }
  1528. c = istr.get();
  1529. ++count;
  1530. if(!((c == '"') || (c == '\'')))
  1531. {
  1532. return LLSDParser::PARSE_FAILURE;
  1533. }
  1534. }
  1535. else
  1536. {
  1537. return LLSDParser::PARSE_FAILURE;
  1538. }
  1539. return count;
  1540. }
  1541. static const char* NOTATION_STRING_CHARACTERS[256] =
  1542. {
  1543. "\\x00", // 0
  1544. "\\x01", // 1
  1545. "\\x02", // 2
  1546. "\\x03", // 3
  1547. "\\x04", // 4
  1548. "\\x05", // 5
  1549. "\\x06", // 6
  1550. "\\a", // 7
  1551. "\\b", // 8
  1552. "\\t", // 9
  1553. "\\n", // 10
  1554. "\\v", // 11
  1555. "\\f", // 12
  1556. "\\r", // 13
  1557. "\\x0e", // 14
  1558. "\\x0f", // 15
  1559. "\\x10", // 16
  1560. "\\x11", // 17
  1561. "\\x12", // 18
  1562. "\\x13", // 19
  1563. "\\x14", // 20
  1564. "\\x15", // 21
  1565. "\\x16", // 22
  1566. "\\x17", // 23
  1567. "\\x18", // 24
  1568. "\\x19", // 25
  1569. "\\x1a", // 26
  1570. "\\x1b", // 27
  1571. "\\x1c", // 28
  1572. "\\x1d", // 29
  1573. "\\x1e", // 30
  1574. "\\x1f", // 31
  1575. " ", // 32
  1576. "!", // 33
  1577. "\"", // 34
  1578. "#", // 35
  1579. "$", // 36
  1580. "%", // 37
  1581. "&", // 38
  1582. "\\'", // 39
  1583. "(", // 40
  1584. ")", // 41
  1585. "*", // 42
  1586. "+", // 43
  1587. ",", // 44
  1588. "-", // 45
  1589. ".", // 46
  1590. "/", // 47
  1591. "0", // 48
  1592. "1", // 49
  1593. "2", // 50
  1594. "3", // 51
  1595. "4", // 52
  1596. "5", // 53
  1597. "6", // 54
  1598. "7", // 55
  1599. "8", // 56
  1600. "9", // 57
  1601. ":", // 58
  1602. ";", // 59
  1603. "<", // 60
  1604. "=", // 61
  1605. ">", // 62
  1606. "?", // 63
  1607. "@", // 64
  1608. "A", // 65
  1609. "B", // 66
  1610. "C", // 67
  1611. "D", // 68
  1612. "E", // 69
  1613. "F", // 70
  1614. "G", // 71
  1615. "H", // 72
  1616. "I", // 73
  1617. "J", // 74
  1618. "K", // 75
  1619. "L", // 76
  1620. "M", // 77
  1621. "N", // 78
  1622. "O", // 79
  1623. "P", // 80
  1624. "Q", // 81
  1625. "R", // 82
  1626. "S", // 83
  1627. "T", // 84
  1628. "U", // 85
  1629. "V", // 86
  1630. "W", // 87
  1631. "X", // 88
  1632. "Y", // 89
  1633. "Z", // 90
  1634. "[", // 91
  1635. "\\\\", // 92
  1636. "]", // 93
  1637. "^", // 94
  1638. "_", // 95
  1639. "`", // 96
  1640. "a", // 97
  1641. "b", // 98
  1642. "c", // 99
  1643. "d", // 100
  1644. "e", // 101
  1645. "f", // 102
  1646. "g", // 103
  1647. "h", // 104
  1648. "i", // 105
  1649. "j", // 106
  1650. "k", // 107
  1651. "l", // 108
  1652. "m", // 109
  1653. "n", // 110
  1654. "o", // 111
  1655. "p", // 112
  1656. "q", // 113
  1657. "r", // 114
  1658. "s", // 115
  1659. "t", // 116
  1660. "u", // 117
  1661. "v", // 118
  1662. "w", // 119
  1663. "x", // 120
  1664. "y", // 121
  1665. "z", // 122
  1666. "{", // 123
  1667. "|", // 124
  1668. "}", // 125
  1669. "~", // 126
  1670. "\\x7f", // 127
  1671. "\\x80", // 128
  1672. "\\x81", // 129
  1673. "\\x82", // 130
  1674. "\\x83", // 131
  1675. "\\x84", // 132
  1676. "\\x85", // 133
  1677. "\\x86", // 134
  1678. "\\x87", // 135
  1679. "\\x88", // 136
  1680. "\\x89", // 137
  1681. "\\x8a", // 138
  1682. "\\x8b", // 139
  1683. "\\x8c", // 140
  1684. "\\x8d", // 141
  1685. "\\x8e", // 142
  1686. "\\x8f", // 143
  1687. "\\x90", // 144
  1688. "\\x91", // 145
  1689. "\\x92", // 146
  1690. "\\x93", // 147
  1691. "\\x94", // 148
  1692. "\\x95", // 149
  1693. "\\x96", // 150
  1694. "\\x97", // 151
  1695. "\\x98", // 152
  1696. "\\x99", // 153
  1697. "\\x9a", // 154
  1698. "\\x9b", // 155
  1699. "\\x9c", // 156
  1700. "\\x9d", // 157
  1701. "\\x9e", // 158
  1702. "\\x9f", // 159
  1703. "\\xa0", // 160
  1704. "\\xa1", // 161
  1705. "\\xa2", // 162
  1706. "\\xa3", // 163
  1707. "\\xa4", // 164
  1708. "\\xa5", // 165
  1709. "\\xa6", // 166
  1710. "\\xa7", // 167
  1711. "\\xa8", // 168
  1712. "\\xa9", // 169
  1713. "\\xaa", // 170
  1714. "\\xab", // 171
  1715. "\\xac", // 172
  1716. "\\xad", // 173
  1717. "\\xae", // 174
  1718. "\\xaf", // 175
  1719. "\\xb0", // 176
  1720. "\\xb1", // 177
  1721. "\\xb2", // 178
  1722. "\\xb3", // 179
  1723. "\\xb4", // 180
  1724. "\\xb5", // 181
  1725. "\\xb6", // 182
  1726. "\\xb7", // 183
  1727. "\\xb8", // 184
  1728. "\\xb9", // 185
  1729. "\\xba", // 186
  1730. "\\xbb", // 187
  1731. "\\xbc", // 188
  1732. "\\xbd", // 189
  1733. "\\xbe", // 190
  1734. "\\xbf", // 191
  1735. "\\xc0", // 192
  1736. "\\xc1", // 193
  1737. "\\xc2", // 194
  1738. "\\xc3", // 195
  1739. "\\xc4", // 196
  1740. "\\xc5", // 197
  1741. "\\xc6", // 198
  1742. "\\xc7", // 199
  1743. "\\xc8", // 200
  1744. "\\xc9", // 201
  1745. "\\xca", // 202
  1746. "\\xcb", // 203
  1747. "\\xcc", // 204
  1748. "\\xcd", // 205
  1749. "\\xce", // 206
  1750. "\\xcf", // 207
  1751. "\\xd0", // 208
  1752. "\\xd1", // 209
  1753. "\\xd2", // 210
  1754. "\\xd3", // 211
  1755. "\\xd4", // 212
  1756. "\\xd5", // 213
  1757. "\\xd6", // 214
  1758. "\\xd7", // 215
  1759. "\\xd8", // 216
  1760. "\\xd9", // 217
  1761. "\\xda", // 218
  1762. "\\xdb", // 219
  1763. "\\xdc", // 220
  1764. "\\xdd", // 221
  1765. "\\xde", // 222
  1766. "\\xdf", // 223
  1767. "\\xe0", // 224
  1768. "\\xe1", // 225
  1769. "\\xe2", // 226
  1770. "\\xe3", // 227
  1771. "\\xe4", // 228
  1772. "\\xe5", // 229
  1773. "\\xe6", // 230
  1774. "\\xe7", // 231
  1775. "\\xe8", // 232
  1776. "\\xe9", // 233
  1777. "\\xea", // 234
  1778. "\\xeb", // 235
  1779. "\\xec", // 236
  1780. "\\xed", // 237
  1781. "\\xee", // 238
  1782. "\\xef", // 239
  1783. "\\xf0", // 240
  1784. "\\xf1", // 241
  1785. "\\xf2", // 242
  1786. "\\xf3", // 243
  1787. "\\xf4", // 244
  1788. "\\xf5", // 245
  1789. "\\xf6", // 246
  1790. "\\xf7", // 247
  1791. "\\xf8", // 248
  1792. "\\xf9", // 249
  1793. "\\xfa", // 250
  1794. "\\xfb", // 251
  1795. "\\xfc", // 252
  1796. "\\xfd", // 253
  1797. "\\xfe", // 254
  1798. "\\xff" // 255
  1799. };
  1800. void serialize_string(const std::string& value, std::ostream& str)
  1801. {
  1802. std::string::const_iterator it = value.begin();
  1803. std::string::const_iterator end = value.end();
  1804. U8 c;
  1805. for(; it != end; ++it)
  1806. {
  1807. c = (U8)(*it);
  1808. str << NOTATION_STRING_CHARACTERS[c];
  1809. }
  1810. }
  1811. int deserialize_boolean(
  1812. std::istream& istr,
  1813. LLSD& data,
  1814. const std::string& compare,
  1815. bool value)
  1816. {
  1817. //
  1818. // this method is a little goofy, because it gets the stream at
  1819. // the point where the t or f has already been
  1820. // consumed. Basically, parse for a patch to the string passed in
  1821. // starting at index 1. If it's a match:
  1822. // * assign data to value
  1823. // * return the number of bytes read
  1824. // otherwise:
  1825. // * set data to LLSD::null
  1826. // * return LLSDParser::PARSE_FAILURE (-1)
  1827. //
  1828. int bytes_read = 0;
  1829. std::string::size_type ii = 0;
  1830. char c = istr.peek();
  1831. while((++ii < compare.size())
  1832. && (tolower(c) == (int)compare[ii])
  1833. && istr.good())
  1834. {
  1835. istr.ignore();
  1836. ++bytes_read;
  1837. c = istr.peek();
  1838. }
  1839. if(compare.size() != ii)
  1840. {
  1841. data.clear();
  1842. return LLSDParser::PARSE_FAILURE;
  1843. }
  1844. data = value;
  1845. return bytes_read;
  1846. }
  1847. std::ostream& operator<<(std::ostream& s, const LLSD& llsd)
  1848. {
  1849. s << LLSDNotationStreamer(llsd);
  1850. return s;
  1851. }
  1852. //dirty little zippers -- yell at davep if these are horrid
  1853. //return a string containing gzipped bytes of binary serialized LLSD
  1854. // VERY inefficient -- creates several copies of LLSD block in memory
  1855. std::string zip_llsd(LLSD& data)
  1856. {
  1857. std::stringstream llsd_strm;
  1858. LLSDSerialize::toBinary(data, llsd_strm);
  1859. const U32 CHUNK = 65536;
  1860. z_stream strm;
  1861. strm.zalloc = Z_NULL;
  1862. strm.zfree = Z_NULL;
  1863. strm.opaque = Z_NULL;
  1864. S32 ret = deflateInit(&strm, Z_BEST_COMPRESSION);
  1865. if (ret != Z_OK)
  1866. {
  1867. llwarns << "Failed to compress LLSD block." << llendl;
  1868. return std::string();
  1869. }
  1870. std::string source = llsd_strm.str();
  1871. U8 out[CHUNK];
  1872. strm.avail_in = source.size();
  1873. strm.next_in = (U8*) source.data();
  1874. U8* output = NULL;
  1875. U32 cur_size = 0;
  1876. U32 have = 0;
  1877. do
  1878. {
  1879. strm.avail_out = CHUNK;
  1880. strm.next_out = out;
  1881. ret = deflate(&strm, Z_FINISH);
  1882. if (ret == Z_OK || ret == Z_STREAM_END)
  1883. { //copy result into output
  1884. if (strm.avail_out >= CHUNK)
  1885. {
  1886. free(output);
  1887. llwarns << "Failed to compress LLSD block." << llendl;
  1888. return std::string();
  1889. }
  1890. have = CHUNK-strm.avail_out;
  1891. output = (U8*) realloc(output, cur_size+have);
  1892. memcpy(output+cur_size, out, have);
  1893. cur_size += have;
  1894. }
  1895. else
  1896. {
  1897. free(output);
  1898. llwarns << "Failed to compress LLSD block." << llendl;
  1899. return std::string();
  1900. }
  1901. }
  1902. while (ret == Z_OK);
  1903. std::string::size_type size = cur_size;
  1904. std::string result((char*) output, size);
  1905. deflateEnd(&strm);
  1906. free(output);
  1907. #if 0 //verify results work with unzip_llsd
  1908. std::istringstream test(result);
  1909. LLSD test_sd;
  1910. if (!unzip_llsd(test_sd, test, result.size()))
  1911. {
  1912. llerrs << "Invalid compression result!" << llendl;
  1913. }
  1914. #endif
  1915. return result;
  1916. }
  1917. //decompress a block of LLSD from provided istream
  1918. // not very efficient -- creats a copy of decompressed LLSD block in memory
  1919. // and deserializes from that copy using LLSDSerialize
  1920. bool unzip_llsd(LLSD& data, std::istream& is, S32 size)
  1921. {
  1922. U8* result = NULL;
  1923. U32 cur_size = 0;
  1924. z_stream strm;
  1925. const U32 CHUNK = 65536;
  1926. U8 *in = new U8[size];
  1927. is.read((char*) in, size);
  1928. U8 out[CHUNK];
  1929. strm.zalloc = Z_NULL;
  1930. strm.zfree = Z_NULL;
  1931. strm.opaque = Z_NULL;
  1932. strm.avail_in = size;
  1933. strm.next_in = in;
  1934. S32 ret = inflateInit(&strm);
  1935. do
  1936. {
  1937. strm.avail_out = CHUNK;
  1938. strm.next_out = out;
  1939. ret = inflate(&strm, Z_NO_FLUSH);
  1940. if (ret == Z_STREAM_ERROR)
  1941. {
  1942. inflateEnd(&strm);
  1943. free(result);
  1944. delete [] in;
  1945. return false;
  1946. }
  1947. switch (ret)
  1948. {
  1949. case Z_NEED_DICT:
  1950. ret = Z_DATA_ERROR;
  1951. case Z_DATA_ERROR:
  1952. case Z_MEM_ERROR:
  1953. inflateEnd(&strm);
  1954. free(result);
  1955. delete [] in;
  1956. return false;
  1957. break;
  1958. }
  1959. U32 have = CHUNK-strm.avail_out;
  1960. result = (U8*) realloc(result, cur_size + have);
  1961. memcpy(result+cur_size, out, have);
  1962. cur_size += have;
  1963. } while (ret == Z_OK);
  1964. inflateEnd(&strm);
  1965. delete [] in;
  1966. if (ret != Z_STREAM_END)
  1967. {
  1968. free(result);
  1969. return false;
  1970. }
  1971. //result now points to the decompressed LLSD block
  1972. {
  1973. std::string res_str((char*) result, cur_size);
  1974. std::string deprecated_header("<? LLSD/Binary ?>");
  1975. if (res_str.substr(0, deprecated_header.size()) == deprecated_header)
  1976. {
  1977. res_str = res_str.substr(deprecated_header.size()+1, cur_size);
  1978. }
  1979. cur_size = res_str.size();
  1980. std::istringstream istr(res_str);
  1981. if (!LLSDSerialize::fromBinary(data, istr, cur_size))
  1982. {
  1983. llwarns << "Failed to unzip LLSD block" << llendl;
  1984. free(result);
  1985. return false;
  1986. }
  1987. }
  1988. free(result);
  1989. return true;
  1990. }