diff options
author | Jacek Antonelli | 2008-08-15 23:45:34 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:45:34 -0500 |
commit | cd17687f01420952712a500107e0f93e7ab8d5f8 (patch) | |
tree | ce48c2b706f2c1176290e39fb555fbdf6648ce01 /linden/indra/llcommon/llsdserialize.h | |
parent | Second Life viewer sources 1.19.0.5 (diff) | |
download | meta-impy-cd17687f01420952712a500107e0f93e7ab8d5f8.zip meta-impy-cd17687f01420952712a500107e0f93e7ab8d5f8.tar.gz meta-impy-cd17687f01420952712a500107e0f93e7ab8d5f8.tar.bz2 meta-impy-cd17687f01420952712a500107e0f93e7ab8d5f8.tar.xz |
Second Life viewer sources 1.19.1.0
Diffstat (limited to '')
-rw-r--r-- | linden/indra/llcommon/llsdserialize.h | 232 |
1 files changed, 184 insertions, 48 deletions
diff --git a/linden/indra/llcommon/llsdserialize.h b/linden/indra/llcommon/llsdserialize.h index e1e81d5..41e0aa5 100644 --- a/linden/indra/llcommon/llsdserialize.h +++ b/linden/indra/llcommon/llsdserialize.h | |||
@@ -40,7 +40,7 @@ | |||
40 | 40 | ||
41 | /** | 41 | /** |
42 | * @class LLSDParser | 42 | * @class LLSDParser |
43 | * @brief Abstract base class for simple LLSD parsers. | 43 | * @brief Abstract base class for LLSD parsers. |
44 | */ | 44 | */ |
45 | class LLSDParser : public LLRefCount | 45 | class LLSDParser : public LLRefCount |
46 | { | 46 | { |
@@ -52,6 +52,14 @@ protected: | |||
52 | 52 | ||
53 | public: | 53 | public: |
54 | /** | 54 | /** |
55 | * @brief Anonymous enum to indicate parsing failure. | ||
56 | */ | ||
57 | enum | ||
58 | { | ||
59 | PARSE_FAILURE = -1 | ||
60 | }; | ||
61 | |||
62 | /** | ||
55 | * @brief Constructor | 63 | * @brief Constructor |
56 | */ | 64 | */ |
57 | LLSDParser(); | 65 | LLSDParser(); |
@@ -67,12 +75,122 @@ public: | |||
67 | * caller. | 75 | * caller. |
68 | * @param istr The input stream. | 76 | * @param istr The input stream. |
69 | * @param data[out] The newly parse structured data. | 77 | * @param data[out] The newly parse structured data. |
70 | * @return Returns The number of LLSD objects parsed into data. | 78 | * @param max_bytes The maximum number of bytes that will be in |
79 | * the stream. Pass in LLSDSerialize::SIZE_UNLIMITED (-1) to set no | ||
80 | * byte limit. | ||
81 | * @return Returns the number of LLSD objects parsed into | ||
82 | * data. Returns PARSE_FAILURE (-1) on parse failure. | ||
83 | */ | ||
84 | S32 parse(std::istream& istr, LLSD& data, S32 max_bytes); | ||
85 | |||
86 | protected: | ||
87 | /** | ||
88 | * @brief Pure virtual base for doing the parse. | ||
89 | * | ||
90 | * This method parses the istream for a structured data. This | ||
91 | * method assumes that the istream is a complete llsd object -- | ||
92 | * for example an opened and closed map with an arbitrary nesting | ||
93 | * of elements. This method will return after reading one data | ||
94 | * object, allowing continued reading from the stream by the | ||
95 | * caller. | ||
96 | * @param istr The input stream. | ||
97 | * @param data[out] The newly parse structured data. | ||
98 | * @return Returns the number of LLSD objects parsed into | ||
99 | * data. Returns PARSE_FAILURE (-1) on parse failure. | ||
100 | */ | ||
101 | virtual S32 doParse(std::istream& istr, LLSD& data) const = 0; | ||
102 | |||
103 | /* @name Simple istream helper methods | ||
104 | * | ||
105 | * These helper methods exist to help correctly use the | ||
106 | * mMaxBytesLeft without really thinking about it for most simple | ||
107 | * operations. Use of the streamtools in llstreamtools.h will | ||
108 | * require custom wrapping. | ||
109 | */ | ||
110 | //@{ | ||
111 | /** | ||
112 | * @brief get a byte off the stream | ||
113 | * | ||
114 | * @param istr The istream to work with. | ||
115 | * @return returns the next character. | ||
116 | */ | ||
117 | int get(std::istream& istr) const; | ||
118 | |||
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 | /** | ||
135 | * @brief get several bytes off the stream into a streambuf | ||
136 | * | ||
137 | * @param istr The istream to work with. | ||
138 | * @param sb The streambuf to read into | ||
139 | * @param delim Delimiter to get until found. | ||
140 | * @return Returns istr. | ||
141 | */ | ||
142 | std::istream& get( | ||
143 | std::istream& istr, | ||
144 | std::streambuf& sb, | ||
145 | char delim) const; | ||
146 | |||
147 | /** | ||
148 | * @brief ignore the next byte on the istream | ||
149 | * | ||
150 | * @param istr The istream to work with. | ||
151 | * @return Returns istr. | ||
152 | */ | ||
153 | std::istream& ignore(std::istream& istr) const; | ||
154 | |||
155 | /** | ||
156 | * @brief put the last character retrieved back on the stream | ||
157 | * | ||
158 | * @param istr The istream to work with. | ||
159 | * @param c The character to put back | ||
160 | * @return Returns istr. | ||
161 | */ | ||
162 | std::istream& putback(std::istream& istr, char c) const; | ||
163 | |||
164 | /** | ||
165 | * @brief read a block of n characters into a buffer | ||
166 | * | ||
167 | * @param istr The istream to work with. | ||
168 | * @param s The buffer to read into | ||
169 | * @param n The number of bytes to read. | ||
170 | * @return Returns istr. | ||
171 | */ | ||
172 | std::istream& read(std::istream& istr, char* s, std::streamsize n) const; | ||
173 | //@} | ||
174 | |||
175 | protected: | ||
176 | /** | ||
177 | * @brief Accunt for bytes read outside of the istream helpers. | ||
178 | * | ||
179 | * Conceptually const since it only modifies mutable members. | ||
180 | * @param bytes The number of bytes read. | ||
71 | */ | 181 | */ |
72 | virtual S32 parse(std::istream& istr, LLSD& data) const = 0; | 182 | void account(S32 bytes) const; |
73 | 183 | ||
74 | protected: | 184 | protected: |
185 | /** | ||
186 | * @brief boolean to set if byte counts should be checked during parsing. | ||
187 | */ | ||
188 | bool mCheckLimits; | ||
75 | 189 | ||
190 | /** | ||
191 | * @brief The maximum number of bytes left to be parsed. | ||
192 | */ | ||
193 | mutable S32 mMaxBytesLeft; | ||
76 | }; | 194 | }; |
77 | 195 | ||
78 | /** | 196 | /** |
@@ -91,8 +209,9 @@ public: | |||
91 | /** | 209 | /** |
92 | * @brief Constructor | 210 | * @brief Constructor |
93 | */ | 211 | */ |
94 | LLSDNotationParser() {} | 212 | LLSDNotationParser(); |
95 | 213 | ||
214 | protected: | ||
96 | /** | 215 | /** |
97 | * @brief Call this method to parse a stream for LLSD. | 216 | * @brief Call this method to parse a stream for LLSD. |
98 | * | 217 | * |
@@ -105,21 +224,9 @@ public: | |||
105 | * @param istr The input stream. | 224 | * @param istr The input stream. |
106 | * @param data[out] The newly parse structured data. Undefined on failure. | 225 | * @param data[out] The newly parse structured data. Undefined on failure. |
107 | * @return Returns the number of LLSD objects parsed into | 226 | * @return Returns the number of LLSD objects parsed into |
108 | * data. Returns -1 on parse failure. | 227 | * data. Returns PARSE_FAILURE (-1) on parse failure. |
109 | */ | ||
110 | virtual S32 parse(std::istream& istr, LLSD& data) const; | ||
111 | |||
112 | /** | ||
113 | * @brief Simple notation parse. | ||
114 | * | ||
115 | * This simplified parser cannot not distinguish between a failed | ||
116 | * parse and a parse which yields a single undefined LLSD. You can | ||
117 | * use this if error checking will be implicit in the use of the | ||
118 | * results of the parse. | ||
119 | * @param istr The input stream. | ||
120 | * @return Returns the parsed LLSD object. | ||
121 | */ | 228 | */ |
122 | static LLSD parse(std::istream& istr); | 229 | virtual S32 doParse(std::istream& istr, LLSD& data) const; |
123 | 230 | ||
124 | private: | 231 | private: |
125 | /** | 232 | /** |
@@ -145,16 +252,18 @@ private: | |||
145 | * | 252 | * |
146 | * @param istr The input stream. | 253 | * @param istr The input stream. |
147 | * @param data[out] The data to assign. | 254 | * @param data[out] The data to assign. |
255 | * @return Retuns true if a complete string was parsed. | ||
148 | */ | 256 | */ |
149 | void parseString(std::istream& istr, LLSD& data) const; | 257 | bool parseString(std::istream& istr, LLSD& data) const; |
150 | 258 | ||
151 | /** | 259 | /** |
152 | * @brief Parse binary data from the stream. | 260 | * @brief Parse binary data from the stream. |
153 | * | 261 | * |
154 | * @param istr The input stream. | 262 | * @param istr The input stream. |
155 | * @param data[out] The data to assign. | 263 | * @param data[out] The data to assign. |
264 | * @return Retuns true if a complete blob was parsed. | ||
156 | */ | 265 | */ |
157 | void parseBinary(std::istream& istr, LLSD& data) const; | 266 | bool parseBinary(std::istream& istr, LLSD& data) const; |
158 | }; | 267 | }; |
159 | 268 | ||
160 | /** | 269 | /** |
@@ -175,6 +284,7 @@ public: | |||
175 | */ | 284 | */ |
176 | LLSDXMLParser(); | 285 | LLSDXMLParser(); |
177 | 286 | ||
287 | protected: | ||
178 | /** | 288 | /** |
179 | * @brief Call this method to parse a stream for LLSD. | 289 | * @brief Call this method to parse a stream for LLSD. |
180 | * | 290 | * |
@@ -186,15 +296,16 @@ public: | |||
186 | * caller. | 296 | * caller. |
187 | * @param istr The input stream. | 297 | * @param istr The input stream. |
188 | * @param data[out] The newly parse structured data. | 298 | * @param data[out] The newly parse structured data. |
189 | * @return Returns the number of LLSD objects parsed into data. | 299 | * @return Returns the number of LLSD objects parsed into |
300 | * data. Returns PARSE_FAILURE (-1) on parse failure. | ||
190 | */ | 301 | */ |
191 | virtual S32 parse(std::istream& istr, LLSD& data) const; | 302 | virtual S32 doParse(std::istream& istr, LLSD& data) const; |
192 | 303 | ||
193 | private: | 304 | private: |
194 | class Impl; | 305 | class Impl; |
195 | Impl& impl; | 306 | Impl& impl; |
196 | 307 | ||
197 | void parsePart(const char *buf, int len); | 308 | void parsePart(const char* buf, int len); |
198 | friend class LLSDSerialize; | 309 | friend class LLSDSerialize; |
199 | }; | 310 | }; |
200 | 311 | ||
@@ -216,6 +327,7 @@ public: | |||
216 | */ | 327 | */ |
217 | LLSDBinaryParser(); | 328 | LLSDBinaryParser(); |
218 | 329 | ||
330 | protected: | ||
219 | /** | 331 | /** |
220 | * @brief Call this method to parse a stream for LLSD. | 332 | * @brief Call this method to parse a stream for LLSD. |
221 | * | 333 | * |
@@ -227,21 +339,10 @@ public: | |||
227 | * caller. | 339 | * caller. |
228 | * @param istr The input stream. | 340 | * @param istr The input stream. |
229 | * @param data[out] The newly parse structured data. | 341 | * @param data[out] The newly parse structured data. |
230 | * @return Returns the number of LLSD objects parsed into data. | 342 | * @return Returns the number of LLSD objects parsed into |
231 | */ | 343 | * data. Returns -1 on parse failure. |
232 | virtual S32 parse(std::istream& istr, LLSD& data) const; | ||
233 | |||
234 | /** | ||
235 | * @brief Simple notation parse. | ||
236 | * | ||
237 | * This simplified parser cannot not distinguish between a failed | ||
238 | * parse and a parse which yields a single undefined LLSD. You can | ||
239 | * use this if error checking will be implicit in the use of the | ||
240 | * results of the parse. | ||
241 | * @param istr The input stream. | ||
242 | * @return Returns the parsed LLSD object. | ||
243 | */ | 344 | */ |
244 | static LLSD parse(std::istream& istr); | 345 | virtual S32 doParse(std::istream& istr, LLSD& data) const; |
245 | 346 | ||
246 | private: | 347 | private: |
247 | /** | 348 | /** |
@@ -267,8 +368,9 @@ private: | |||
267 | * | 368 | * |
268 | * @param istr The input stream. | 369 | * @param istr The input stream. |
269 | * @param value[out] The string to assign. | 370 | * @param value[out] The string to assign. |
371 | * @return Retuns true if a complete string was parsed. | ||
270 | */ | 372 | */ |
271 | void parseString(std::istream& istr, std::string& value) const; | 373 | bool parseString(std::istream& istr, std::string& value) const; |
272 | }; | 374 | }; |
273 | 375 | ||
274 | 376 | ||
@@ -544,7 +646,7 @@ typedef LLSDOStreamer<LLSDXMLFormatter> LLSDXMLStreamer; | |||
544 | 646 | ||
545 | /** | 647 | /** |
546 | * @class LLSDSerialize | 648 | * @class LLSDSerialize |
547 | * @Serializer / deserializer for the various LLSD formats | 649 | * @brief Serializer / deserializer for the various LLSD formats |
548 | */ | 650 | */ |
549 | class LLSDSerialize | 651 | class LLSDSerialize |
550 | { | 652 | { |
@@ -554,12 +656,32 @@ public: | |||
554 | LLSD_BINARY, LLSD_XML | 656 | LLSD_BINARY, LLSD_XML |
555 | }; | 657 | }; |
556 | 658 | ||
659 | /** | ||
660 | * @brief anonymouse enumeration for useful max_bytes constants. | ||
661 | */ | ||
662 | enum | ||
663 | { | ||
664 | // Setting an unlimited size is discouraged and should only be | ||
665 | // used when reading cin or another stream source which does | ||
666 | // not provide access to size. | ||
667 | SIZE_UNLIMITED = -1, | ||
668 | }; | ||
669 | |||
557 | /* | 670 | /* |
558 | * Generic in/outs | 671 | * Generic in/outs |
559 | */ | 672 | */ |
560 | static void serialize(const LLSD& sd, std::ostream& str, ELLSD_Serialize, | 673 | static void serialize(const LLSD& sd, std::ostream& str, ELLSD_Serialize, |
561 | U32 options = LLSDFormatter::OPTIONS_NONE); | 674 | U32 options = LLSDFormatter::OPTIONS_NONE); |
562 | static bool deserialize(LLSD& sd, std::istream& str); | 675 | |
676 | /** | ||
677 | * @breif Examine a stream, and parse 1 sd object out based on contents. | ||
678 | * | ||
679 | * @param sd [out] The data found on the stream | ||
680 | * @param str The incoming stream | ||
681 | * @param max_bytes the maximum number of bytes to parse | ||
682 | * @return Returns true if the stream appears to contain valid data | ||
683 | */ | ||
684 | static bool deserialize(LLSD& sd, std::istream& str, S32 max_bytes); | ||
563 | 685 | ||
564 | /* | 686 | /* |
565 | * Notation Methods | 687 | * Notation Methods |
@@ -569,10 +691,17 @@ public: | |||
569 | LLPointer<LLSDNotationFormatter> f = new LLSDNotationFormatter; | 691 | LLPointer<LLSDNotationFormatter> f = new LLSDNotationFormatter; |
570 | return f->format(sd, str, LLSDFormatter::OPTIONS_NONE); | 692 | return f->format(sd, str, LLSDFormatter::OPTIONS_NONE); |
571 | } | 693 | } |
572 | static S32 fromNotation(LLSD& sd, std::istream& str) | 694 | static S32 fromNotation(LLSD& sd, std::istream& str, S32 max_bytes) |
695 | { | ||
696 | LLPointer<LLSDNotationParser> p = new LLSDNotationParser; | ||
697 | return p->parse(str, sd, max_bytes); | ||
698 | } | ||
699 | static LLSD fromNotation(std::istream& str, S32 max_bytes) | ||
573 | { | 700 | { |
574 | LLPointer<LLSDNotationParser> p = new LLSDNotationParser; | 701 | LLPointer<LLSDNotationParser> p = new LLSDNotationParser; |
575 | return p->parse(str, sd); | 702 | LLSD sd; |
703 | (void)p->parse(str, sd, max_bytes); | ||
704 | return sd; | ||
576 | } | 705 | } |
577 | 706 | ||
578 | /* | 707 | /* |
@@ -588,10 +717,13 @@ public: | |||
588 | LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter; | 717 | LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter; |
589 | return f->format(sd, str, LLSDFormatter::OPTIONS_PRETTY); | 718 | return f->format(sd, str, LLSDFormatter::OPTIONS_PRETTY); |
590 | } | 719 | } |
720 | |||
591 | static S32 fromXML(LLSD& sd, std::istream& str) | 721 | static S32 fromXML(LLSD& sd, std::istream& str) |
592 | { | 722 | { |
723 | // no need for max_bytes since xml formatting is not | ||
724 | // subvertable by bad sizes. | ||
593 | LLPointer<LLSDXMLParser> p = new LLSDXMLParser; | 725 | LLPointer<LLSDXMLParser> p = new LLSDXMLParser; |
594 | return p->parse(str, sd); | 726 | return p->parse(str, sd, LLSDSerialize::SIZE_UNLIMITED); |
595 | } | 727 | } |
596 | 728 | ||
597 | /* | 729 | /* |
@@ -602,14 +734,18 @@ public: | |||
602 | LLPointer<LLSDBinaryFormatter> f = new LLSDBinaryFormatter; | 734 | LLPointer<LLSDBinaryFormatter> f = new LLSDBinaryFormatter; |
603 | return f->format(sd, str, LLSDFormatter::OPTIONS_NONE); | 735 | return f->format(sd, str, LLSDFormatter::OPTIONS_NONE); |
604 | } | 736 | } |
605 | static S32 fromBinary(LLSD& sd, std::istream& str) | 737 | static S32 fromBinary(LLSD& sd, std::istream& str, S32 max_bytes) |
606 | { | 738 | { |
607 | LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser; | 739 | LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser; |
608 | return p->parse(str, sd); | 740 | return p->parse(str, sd, max_bytes); |
741 | } | ||
742 | static LLSD fromBinary(std::istream& str, S32 max_bytes) | ||
743 | { | ||
744 | LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser; | ||
745 | LLSD sd; | ||
746 | (void)p->parse(str, sd, max_bytes); | ||
747 | return sd; | ||
609 | } | 748 | } |
610 | private: | ||
611 | static const char *LLSDBinaryHeader; | ||
612 | static const char *LLSDXMLHeader; | ||
613 | }; | 749 | }; |
614 | 750 | ||
615 | #endif // LL_LLSDSERIALIZE_H | 751 | #endif // LL_LLSDSERIALIZE_H |