PageRenderTime 30ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llsdserialize.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 797 lines | 230 code | 81 blank | 486 comment | 0 complexity | aafd9e52eebb23a3cf8a03442417eda2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsdserialize.h
  3. * @author Phoenix
  4. * @date 2006-02-26
  5. * @brief Declaration of parsers and formatters for LLSD
  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. #ifndef LL_LLSDSERIALIZE_H
  29. #define LL_LLSDSERIALIZE_H
  30. #include <iosfwd>
  31. #include "llpointer.h"
  32. #include "llrefcount.h"
  33. #include "llsd.h"
  34. /**
  35. * @class LLSDParser
  36. * @brief Abstract base class for LLSD parsers.
  37. */
  38. class LL_COMMON_API LLSDParser : public LLRefCount
  39. {
  40. protected:
  41. /**
  42. * @brief Destructor
  43. */
  44. virtual ~LLSDParser();
  45. public:
  46. /**
  47. * @brief Anonymous enum to indicate parsing failure.
  48. */
  49. enum
  50. {
  51. PARSE_FAILURE = -1
  52. };
  53. /**
  54. * @brief Constructor
  55. */
  56. LLSDParser();
  57. /**
  58. * @brief Call this method to parse a stream for LLSD.
  59. *
  60. * This method parses the istream for a structured data. This
  61. * method assumes that the istream is a complete llsd object --
  62. * for example an opened and closed map with an arbitrary nesting
  63. * of elements. This method will return after reading one data
  64. * object, allowing continued reading from the stream by the
  65. * caller.
  66. * @param istr The input stream.
  67. * @param data[out] The newly parse structured data.
  68. * @param max_bytes The maximum number of bytes that will be in
  69. * the stream. Pass in LLSDSerialize::SIZE_UNLIMITED (-1) to set no
  70. * byte limit.
  71. * @return Returns the number of LLSD objects parsed into
  72. * data. Returns PARSE_FAILURE (-1) on parse failure.
  73. */
  74. S32 parse(std::istream& istr, LLSD& data, S32 max_bytes);
  75. /** Like parse(), but uses a different call (istream.getline()) to read by lines
  76. * This API is better suited for XML, where the parse cannot tell
  77. * where the document actually ends.
  78. */
  79. S32 parseLines(std::istream& istr, LLSD& data);
  80. /**
  81. * @brief Resets the parser so parse() or parseLines() can be called again for another <llsd> chunk.
  82. */
  83. void reset() { doReset(); };
  84. protected:
  85. /**
  86. * @brief Pure virtual base for doing the parse.
  87. *
  88. * This method parses the istream for a structured data. This
  89. * method assumes that the istream is a complete llsd object --
  90. * for example an opened and closed map with an arbitrary nesting
  91. * of elements. This method will return after reading one data
  92. * object, allowing continued reading from the stream by the
  93. * caller.
  94. * @param istr The input stream.
  95. * @param data[out] The newly parse structured data.
  96. * @return Returns the number of LLSD objects parsed into
  97. * data. Returns PARSE_FAILURE (-1) on parse failure.
  98. */
  99. virtual S32 doParse(std::istream& istr, LLSD& data) const = 0;
  100. /**
  101. * @brief Virtual default function for resetting the parser
  102. */
  103. virtual void doReset() {};
  104. /* @name Simple istream helper methods
  105. *
  106. * These helper methods exist to help correctly use the
  107. * mMaxBytesLeft without really thinking about it for most simple
  108. * operations. Use of the streamtools in llstreamtools.h will
  109. * require custom wrapping.
  110. */
  111. //@{
  112. /**
  113. * @brief get a byte off the stream
  114. *
  115. * @param istr The istream to work with.
  116. * @return returns the next character.
  117. */
  118. int get(std::istream& istr) const;
  119. /**
  120. * @brief get several bytes off the stream into a buffer.
  121. *
  122. * @param istr The istream to work with.
  123. * @param s The buffer to get into
  124. * @param n Extract maximum of n-1 bytes and null temrinate.
  125. * @param delim Delimiter to get until found.
  126. * @return Returns istr.
  127. */
  128. std::istream& get(
  129. std::istream& istr,
  130. char* s,
  131. std::streamsize n,
  132. char delim) const;
  133. /**
  134. * @brief get several bytes off the stream into a streambuf
  135. *
  136. * @param istr The istream to work with.
  137. * @param sb The streambuf to read into
  138. * @param delim Delimiter to get until found.
  139. * @return Returns istr.
  140. */
  141. std::istream& get(
  142. std::istream& istr,
  143. std::streambuf& sb,
  144. char delim) const;
  145. /**
  146. * @brief ignore the next byte on the istream
  147. *
  148. * @param istr The istream to work with.
  149. * @return Returns istr.
  150. */
  151. std::istream& ignore(std::istream& istr) const;
  152. /**
  153. * @brief put the last character retrieved back on the stream
  154. *
  155. * @param istr The istream to work with.
  156. * @param c The character to put back
  157. * @return Returns istr.
  158. */
  159. std::istream& putback(std::istream& istr, char c) const;
  160. /**
  161. * @brief read a block of n characters into a buffer
  162. *
  163. * @param istr The istream to work with.
  164. * @param s The buffer to read into
  165. * @param n The number of bytes to read.
  166. * @return Returns istr.
  167. */
  168. std::istream& read(std::istream& istr, char* s, std::streamsize n) const;
  169. //@}
  170. protected:
  171. /**
  172. * @brief Accunt for bytes read outside of the istream helpers.
  173. *
  174. * Conceptually const since it only modifies mutable members.
  175. * @param bytes The number of bytes read.
  176. */
  177. void account(S32 bytes) const;
  178. protected:
  179. /**
  180. * @brief boolean to set if byte counts should be checked during parsing.
  181. */
  182. bool mCheckLimits;
  183. /**
  184. * @brief The maximum number of bytes left to be parsed.
  185. */
  186. mutable S32 mMaxBytesLeft;
  187. /**
  188. * @brief Use line-based reading to get text
  189. */
  190. bool mParseLines;
  191. };
  192. /**
  193. * @class LLSDNotationParser
  194. * @brief Parser which handles the original notation format for LLSD.
  195. */
  196. class LL_COMMON_API LLSDNotationParser : public LLSDParser
  197. {
  198. protected:
  199. /**
  200. * @brief Destructor
  201. */
  202. virtual ~LLSDNotationParser();
  203. public:
  204. /**
  205. * @brief Constructor
  206. */
  207. LLSDNotationParser();
  208. protected:
  209. /**
  210. * @brief Call this method to parse a stream for LLSD.
  211. *
  212. * This method parses the istream for a structured data. This
  213. * method assumes that the istream is a complete llsd object --
  214. * for example an opened and closed map with an arbitrary nesting
  215. * of elements. This method will return after reading one data
  216. * object, allowing continued reading from the stream by the
  217. * caller.
  218. * @param istr The input stream.
  219. * @param data[out] The newly parse structured data. Undefined on failure.
  220. * @return Returns the number of LLSD objects parsed into
  221. * data. Returns PARSE_FAILURE (-1) on parse failure.
  222. */
  223. virtual S32 doParse(std::istream& istr, LLSD& data) const;
  224. private:
  225. /**
  226. * @brief Parse a map from the istream
  227. *
  228. * @param istr The input stream.
  229. * @param map The map to add the parsed data.
  230. * @return Returns The number of LLSD objects parsed into data.
  231. */
  232. S32 parseMap(std::istream& istr, LLSD& map) const;
  233. /**
  234. * @brief Parse an array from the istream.
  235. *
  236. * @param istr The input stream.
  237. * @param array The array to append the parsed data.
  238. * @return Returns The number of LLSD objects parsed into data.
  239. */
  240. S32 parseArray(std::istream& istr, LLSD& array) const;
  241. /**
  242. * @brief Parse a string from the istream and assign it to data.
  243. *
  244. * @param istr The input stream.
  245. * @param data[out] The data to assign.
  246. * @return Retuns true if a complete string was parsed.
  247. */
  248. bool parseString(std::istream& istr, LLSD& data) const;
  249. /**
  250. * @brief Parse binary data from the stream.
  251. *
  252. * @param istr The input stream.
  253. * @param data[out] The data to assign.
  254. * @return Retuns true if a complete blob was parsed.
  255. */
  256. bool parseBinary(std::istream& istr, LLSD& data) const;
  257. };
  258. /**
  259. * @class LLSDXMLParser
  260. * @brief Parser which handles XML format LLSD.
  261. */
  262. class LL_COMMON_API LLSDXMLParser : public LLSDParser
  263. {
  264. protected:
  265. /**
  266. * @brief Destructor
  267. */
  268. virtual ~LLSDXMLParser();
  269. public:
  270. /**
  271. * @brief Constructor
  272. */
  273. LLSDXMLParser();
  274. protected:
  275. /**
  276. * @brief Call this method to parse a stream for LLSD.
  277. *
  278. * This method parses the istream for a structured data. This
  279. * method assumes that the istream is a complete llsd object --
  280. * for example an opened and closed map with an arbitrary nesting
  281. * of elements. This method will return after reading one data
  282. * object, allowing continued reading from the stream by the
  283. * caller.
  284. * @param istr The input stream.
  285. * @param data[out] The newly parse structured data.
  286. * @return Returns the number of LLSD objects parsed into
  287. * data. Returns PARSE_FAILURE (-1) on parse failure.
  288. */
  289. virtual S32 doParse(std::istream& istr, LLSD& data) const;
  290. /**
  291. * @brief Virtual default function for resetting the parser
  292. */
  293. virtual void doReset();
  294. private:
  295. class Impl;
  296. Impl& impl;
  297. void parsePart(const char* buf, int len);
  298. friend class LLSDSerialize;
  299. };
  300. /**
  301. * @class LLSDBinaryParser
  302. * @brief Parser which handles binary formatted LLSD.
  303. */
  304. class LL_COMMON_API LLSDBinaryParser : public LLSDParser
  305. {
  306. protected:
  307. /**
  308. * @brief Destructor
  309. */
  310. virtual ~LLSDBinaryParser();
  311. public:
  312. /**
  313. * @brief Constructor
  314. */
  315. LLSDBinaryParser();
  316. protected:
  317. /**
  318. * @brief Call this method to parse a stream for LLSD.
  319. *
  320. * This method parses the istream for a structured data. This
  321. * method assumes that the istream is a complete llsd object --
  322. * for example an opened and closed map with an arbitrary nesting
  323. * of elements. This method will return after reading one data
  324. * object, allowing continued reading from the stream by the
  325. * caller.
  326. * @param istr The input stream.
  327. * @param data[out] The newly parse structured data.
  328. * @return Returns the number of LLSD objects parsed into
  329. * data. Returns -1 on parse failure.
  330. */
  331. virtual S32 doParse(std::istream& istr, LLSD& data) const;
  332. private:
  333. /**
  334. * @brief Parse a map from the istream
  335. *
  336. * @param istr The input stream.
  337. * @param map The map to add the parsed data.
  338. * @return Returns The number of LLSD objects parsed into data.
  339. */
  340. S32 parseMap(std::istream& istr, LLSD& map) const;
  341. /**
  342. * @brief Parse an array from the istream.
  343. *
  344. * @param istr The input stream.
  345. * @param array The array to append the parsed data.
  346. * @return Returns The number of LLSD objects parsed into data.
  347. */
  348. S32 parseArray(std::istream& istr, LLSD& array) const;
  349. /**
  350. * @brief Parse a string from the istream and assign it to data.
  351. *
  352. * @param istr The input stream.
  353. * @param value[out] The string to assign.
  354. * @return Retuns true if a complete string was parsed.
  355. */
  356. bool parseString(std::istream& istr, std::string& value) const;
  357. };
  358. /**
  359. * @class LLSDFormatter
  360. * @brief Abstract base class for formatting LLSD.
  361. */
  362. class LL_COMMON_API LLSDFormatter : public LLRefCount
  363. {
  364. protected:
  365. /**
  366. * @brief Destructor
  367. */
  368. virtual ~LLSDFormatter();
  369. public:
  370. /**
  371. * Options for output
  372. */
  373. typedef enum e_formatter_options_type
  374. {
  375. OPTIONS_NONE = 0,
  376. OPTIONS_PRETTY = 1
  377. } EFormatterOptions;
  378. /**
  379. * @brief Constructor
  380. */
  381. LLSDFormatter();
  382. /**
  383. * @brief Set the boolean serialization format.
  384. *
  385. * @param alpha Serializes boolean as alpha if true.
  386. */
  387. void boolalpha(bool alpha);
  388. /**
  389. * @brief Set the real format
  390. *
  391. * By default, the formatter will use default double serialization
  392. * which is frequently frustrating for many applications. You can
  393. * set the precision on the stream independently, but that still
  394. * might not work depending on the value.
  395. * EXAMPLES:<br>
  396. * %.2f<br>
  397. * @param format A format string which follows the printf format
  398. * rules. Specify an empty string to return to default formatting.
  399. */
  400. void realFormat(const std::string& format);
  401. /**
  402. * @brief Call this method to format an LLSD to a stream.
  403. *
  404. * @param data The data to write.
  405. * @param ostr The destination stream for the data.
  406. * @return Returns The number of LLSD objects fomatted out
  407. */
  408. virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const = 0;
  409. protected:
  410. /**
  411. * @brief Helper method which appropriately obeys the real format.
  412. *
  413. * @param real The real value to format.
  414. * @param ostr The destination stream for the data.
  415. */
  416. void formatReal(LLSD::Real real, std::ostream& ostr) const;
  417. protected:
  418. bool mBoolAlpha;
  419. std::string mRealFormat;
  420. };
  421. /**
  422. * @class LLSDNotationFormatter
  423. * @brief Formatter which outputs the original notation format for LLSD.
  424. */
  425. class LL_COMMON_API LLSDNotationFormatter : public LLSDFormatter
  426. {
  427. protected:
  428. /**
  429. * @brief Destructor
  430. */
  431. virtual ~LLSDNotationFormatter();
  432. public:
  433. /**
  434. * @brief Constructor
  435. */
  436. LLSDNotationFormatter();
  437. /**
  438. * @brief Helper static method to return a notation escaped string
  439. *
  440. * This method will return the notation escaped string, but not
  441. * the surrounding serialization identifiers such as a double or
  442. * single quote. It will be up to the caller to embed those as
  443. * appropriate.
  444. * @param in The raw, unescaped string.
  445. * @return Returns an escaped string appropriate for serialization.
  446. */
  447. static std::string escapeString(const std::string& in);
  448. /**
  449. * @brief Call this method to format an LLSD to a stream.
  450. *
  451. * @param data The data to write.
  452. * @param ostr The destination stream for the data.
  453. * @return Returns The number of LLSD objects fomatted out
  454. */
  455. virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
  456. };
  457. /**
  458. * @class LLSDXMLFormatter
  459. * @brief Formatter which outputs the LLSD as XML.
  460. */
  461. class LL_COMMON_API LLSDXMLFormatter : public LLSDFormatter
  462. {
  463. protected:
  464. /**
  465. * @brief Destructor
  466. */
  467. virtual ~LLSDXMLFormatter();
  468. public:
  469. /**
  470. * @brief Constructor
  471. */
  472. LLSDXMLFormatter();
  473. /**
  474. * @brief Helper static method to return an xml escaped string
  475. *
  476. * @param in A valid UTF-8 string.
  477. * @return Returns an escaped string appropriate for serialization.
  478. */
  479. static std::string escapeString(const std::string& in);
  480. /**
  481. * @brief Call this method to format an LLSD to a stream.
  482. *
  483. * @param data The data to write.
  484. * @param ostr The destination stream for the data.
  485. * @return Returns The number of LLSD objects fomatted out
  486. */
  487. virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
  488. protected:
  489. /**
  490. * @brief Implementation to format the data. This is called recursively.
  491. *
  492. * @param data The data to write.
  493. * @param ostr The destination stream for the data.
  494. * @return Returns The number of LLSD objects fomatted out
  495. */
  496. S32 format_impl(const LLSD& data, std::ostream& ostr, U32 options, U32 level) const;
  497. };
  498. /**
  499. * @class LLSDBinaryFormatter
  500. * @brief Formatter which outputs the LLSD as a binary notation format.
  501. *
  502. * The binary format is a compact and efficient representation of
  503. * structured data useful for when transmitting over a small data pipe
  504. * or when transmission frequency is very high.<br>
  505. *
  506. * The normal boolalpha and real format commands are ignored.<br>
  507. *
  508. * All integers are transmitted in network byte order. The format is:<br>
  509. * Undefined: '!'<br>
  510. * Boolean: character '1' for true character '0' for false<br>
  511. * Integer: 'i' + 4 bytes network byte order<br>
  512. * Real: 'r' + 8 bytes IEEE double<br>
  513. * UUID: 'u' + 16 byte unsigned integer<br>
  514. * String: 's' + 4 byte integer size + string<br>
  515. * Date: 'd' + 8 byte IEEE double for seconds since epoch<br>
  516. * URI: 'l' + 4 byte integer size + string uri<br>
  517. * Binary: 'b' + 4 byte integer size + binary data<br>
  518. * Array: '[' + 4 byte integer size + all values + ']'<br>
  519. * Map: '{' + 4 byte integer size every(key + value) + '}'<br>
  520. * map keys are serialized as 'k' + 4 byte integer size + string
  521. */
  522. class LL_COMMON_API LLSDBinaryFormatter : public LLSDFormatter
  523. {
  524. protected:
  525. /**
  526. * @brief Destructor
  527. */
  528. virtual ~LLSDBinaryFormatter();
  529. public:
  530. /**
  531. * @brief Constructor
  532. */
  533. LLSDBinaryFormatter();
  534. /**
  535. * @brief Call this method to format an LLSD to a stream.
  536. *
  537. * @param data The data to write.
  538. * @param ostr The destination stream for the data.
  539. * @return Returns The number of LLSD objects fomatted out
  540. */
  541. virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
  542. protected:
  543. /**
  544. * @brief Helper method to serialize strings
  545. *
  546. * This method serializes a network byte order size and the raw
  547. * string contents.
  548. * @param string The string to write.
  549. * @param ostr The destination stream for the data.
  550. */
  551. void formatString(const std::string& string, std::ostream& ostr) const;
  552. };
  553. /**
  554. * @class LLSDNotationStreamFormatter
  555. * @brief Formatter which is specialized for use on streams which
  556. * outputs the original notation format for LLSD.
  557. *
  558. * This class is useful for doing inline stream operations. For example:
  559. *
  560. * <code>
  561. * LLSD sd;<br>
  562. * sd["foo"] = "bar";<br>
  563. * std::stringstream params;<br>
  564. * params << "[{'version':i1}," << LLSDOStreamer<LLSDNotationFormatter>(sd)
  565. * << "]";
  566. * </code>
  567. *
  568. * *NOTE - formerly this class inherited from its template parameter Formatter,
  569. * but all insnatiations passed in LLRefCount subclasses. This conflicted with
  570. * the auto allocation intended for this class template (demonstrated in the
  571. * example above). -brad
  572. */
  573. template <class Formatter>
  574. class LLSDOStreamer
  575. {
  576. public:
  577. /**
  578. * @brief Constructor
  579. */
  580. LLSDOStreamer(const LLSD& data, U32 options = LLSDFormatter::OPTIONS_NONE) :
  581. mSD(data), mOptions(options) {}
  582. /**
  583. * @brief Stream operator.
  584. *
  585. * Use this inline during construction during a stream operation.
  586. * @param str The destination stream for serialized output.
  587. * @param The formatter which will output it's LLSD.
  588. * @return Returns the stream passed in after streaming mSD.
  589. */
  590. friend std::ostream& operator<<(
  591. std::ostream& str,
  592. const LLSDOStreamer<Formatter>& formatter)
  593. {
  594. LLPointer<Formatter> f = new Formatter;
  595. f->format(formatter.mSD, str, formatter.mOptions);
  596. return str;
  597. }
  598. protected:
  599. LLSD mSD;
  600. U32 mOptions;
  601. };
  602. typedef LLSDOStreamer<LLSDNotationFormatter> LLSDNotationStreamer;
  603. typedef LLSDOStreamer<LLSDXMLFormatter> LLSDXMLStreamer;
  604. /**
  605. * @class LLSDSerialize
  606. * @brief Serializer / deserializer for the various LLSD formats
  607. */
  608. class LL_COMMON_API LLSDSerialize
  609. {
  610. public:
  611. enum ELLSD_Serialize
  612. {
  613. LLSD_BINARY, LLSD_XML
  614. };
  615. /**
  616. * @brief anonymouse enumeration for useful max_bytes constants.
  617. */
  618. enum
  619. {
  620. // Setting an unlimited size is discouraged and should only be
  621. // used when reading cin or another stream source which does
  622. // not provide access to size.
  623. SIZE_UNLIMITED = -1,
  624. };
  625. /*
  626. * Generic in/outs
  627. */
  628. static void serialize(const LLSD& sd, std::ostream& str, ELLSD_Serialize,
  629. U32 options = LLSDFormatter::OPTIONS_NONE);
  630. /**
  631. * @brief Examine a stream, and parse 1 sd object out based on contents.
  632. *
  633. * @param sd [out] The data found on the stream
  634. * @param str The incoming stream
  635. * @param max_bytes the maximum number of bytes to parse
  636. * @return Returns true if the stream appears to contain valid data
  637. */
  638. static bool deserialize(LLSD& sd, std::istream& str, S32 max_bytes);
  639. /*
  640. * Notation Methods
  641. */
  642. static S32 toNotation(const LLSD& sd, std::ostream& str)
  643. {
  644. LLPointer<LLSDNotationFormatter> f = new LLSDNotationFormatter;
  645. return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
  646. }
  647. static S32 fromNotation(LLSD& sd, std::istream& str, S32 max_bytes)
  648. {
  649. LLPointer<LLSDNotationParser> p = new LLSDNotationParser;
  650. return p->parse(str, sd, max_bytes);
  651. }
  652. static LLSD fromNotation(std::istream& str, S32 max_bytes)
  653. {
  654. LLPointer<LLSDNotationParser> p = new LLSDNotationParser;
  655. LLSD sd;
  656. (void)p->parse(str, sd, max_bytes);
  657. return sd;
  658. }
  659. /*
  660. * XML Methods
  661. */
  662. static S32 toXML(const LLSD& sd, std::ostream& str)
  663. {
  664. LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter;
  665. return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
  666. }
  667. static S32 toPrettyXML(const LLSD& sd, std::ostream& str)
  668. {
  669. LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter;
  670. return f->format(sd, str, LLSDFormatter::OPTIONS_PRETTY);
  671. }
  672. static S32 fromXMLEmbedded(LLSD& sd, std::istream& str)
  673. {
  674. // no need for max_bytes since xml formatting is not
  675. // subvertable by bad sizes.
  676. LLPointer<LLSDXMLParser> p = new LLSDXMLParser;
  677. return p->parse(str, sd, LLSDSerialize::SIZE_UNLIMITED);
  678. }
  679. // Line oriented parser, 30% faster than fromXML(), but can
  680. // only be used when you know you have the complete XML
  681. // document available in the stream.
  682. static S32 fromXMLDocument(LLSD& sd, std::istream& str)
  683. {
  684. LLPointer<LLSDXMLParser> p = new LLSDXMLParser();
  685. return p->parseLines(str, sd);
  686. }
  687. static S32 fromXML(LLSD& sd, std::istream& str)
  688. {
  689. return fromXMLEmbedded(sd, str);
  690. // return fromXMLDocument(sd, str);
  691. }
  692. /*
  693. * Binary Methods
  694. */
  695. static S32 toBinary(const LLSD& sd, std::ostream& str)
  696. {
  697. LLPointer<LLSDBinaryFormatter> f = new LLSDBinaryFormatter;
  698. return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
  699. }
  700. static S32 fromBinary(LLSD& sd, std::istream& str, S32 max_bytes)
  701. {
  702. LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser;
  703. return p->parse(str, sd, max_bytes);
  704. }
  705. static LLSD fromBinary(std::istream& str, S32 max_bytes)
  706. {
  707. LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser;
  708. LLSD sd;
  709. (void)p->parse(str, sd, max_bytes);
  710. return sd;
  711. }
  712. };
  713. //dirty little zip functions -- yell at davep
  714. LL_COMMON_API std::string zip_llsd(LLSD& data);
  715. LL_COMMON_API bool unzip_llsd(LLSD& data, std::istream& is, S32 size);
  716. #endif // LL_LLSDSERIALIZE_H