llsdserialize.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484
  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=viewergpl$
  8. *
  9. * Copyright (c) 2006-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #include "linden_common.h"
  35. #include <deque>
  36. #include <iostream>
  37. #include <memory>
  38. #if !LL_WINDOWS
  39. # include <netinet/in.h> // For htonl() and ntohl()
  40. #endif
  41. #include "boost/iostreams/device/array.hpp"
  42. #include "boost/iostreams/stream.hpp"
  43. #include "expat.h"
  44. #include "zlib.h" // For Davep's dirty little zip functions
  45. #include "llsdserialize.h"
  46. #include "llbase64.h"
  47. #include "llmemory.h"
  48. #include "llmemorystream.h"
  49. #include "llpointer.h"
  50. #include "llsd.h"
  51. #include "llstreamtools.h" // For fullread()
  52. #include "llstring.h"
  53. #include "lluri.h"
  54. // File constants
  55. constexpr S32 UNZIP_LLSD_MAX_DEPTH = 96;
  56. constexpr int MAX_HDR_LEN = 20;
  57. static const char LEGACY_NON_HEADER[] = "<llsd>";
  58. static const std::string LLSD_BINARY_HEADER("LLSD/Binary");
  59. static const std::string LLSD_XML_HEADER("LLSD/XML");
  60. static const std::string LLSD_NOTATION_HEADER("llsd/notation");
  61. // Used to deflate a gzipped asset (currently used for navmeshes)
  62. #define WINDOW_BITS 15
  63. #define ENABLE_ZLIB_GZIP 32
  64. // Helper templates
  65. #if LL_USE_NEW_DESERIALIZE
  66. template<class Formatter>
  67. LL_INLINE void format_using(const LLSD& data, std::ostream& ostr,
  68. LLSDFormatter::EFormatterOptions options =
  69. LLSDFormatter::OPTIONS_PRETTY_BINARY)
  70. {
  71. LLPointer<Formatter> f = new Formatter;
  72. f->format(data, ostr, options);
  73. }
  74. template<class Parser>
  75. LL_INLINE S32 parse_using(std::istream& istr, LLSD& data, size_t max_bytes,
  76. S32 max_depth = -1)
  77. {
  78. LLPointer<Parser> p = new Parser;
  79. return p->parse(istr, data, max_bytes, max_depth);
  80. }
  81. #endif
  82. ///////////////////////////////////////////////////////////////////////////////
  83. // LLSDSerialize class
  84. ///////////////////////////////////////////////////////////////////////////////
  85. //static
  86. void LLSDSerialize::serialize(const LLSD& sd, std::ostream& str,
  87. ELLSD_Serialize type,
  88. LLSDFormatter::EFormatterOptions options)
  89. {
  90. LLPointer<LLSDFormatter> f = NULL;
  91. switch (type)
  92. {
  93. case LLSD_BINARY:
  94. str << "<? " << LLSD_BINARY_HEADER << " ?>\n";
  95. f = new LLSDBinaryFormatter;
  96. break;
  97. case LLSD_XML:
  98. str << "<? " << LLSD_XML_HEADER << " ?>\n";
  99. f = new LLSDXMLFormatter;
  100. break;
  101. case LLSD_NOTATION:
  102. str << "<? " << LLSD_NOTATION_HEADER << " ?>\n";
  103. f = new LLSDNotationFormatter;
  104. break;
  105. default:
  106. llwarns << "serialize request for unknown ELLSD_Serialize"
  107. << llendl;
  108. }
  109. if (f.notNull())
  110. {
  111. f->format(sd, str, options);
  112. }
  113. }
  114. //static
  115. bool LLSDSerialize::deserialize(LLSD& sd, std::istream& str, llssize max_bytes)
  116. {
  117. char hdr_buf[MAX_HDR_LEN + 1] = "";
  118. bool fail_if_not_legacy = false;
  119. #if LL_USE_NEW_DESERIALIZE
  120. // Get the first line before anything. Do not read more than max_bytes:
  121. // this get() overload reads no more than (count-1) bytes into the
  122. // specified buffer. In the usual case when max_bytes exceed
  123. // ssizeof(hdr_buf), get() will read no more than sizeof(hdr_buf)-2.
  124. llssize max_hdr_read = MAX_HDR_LEN;
  125. if (max_bytes != LLSDSerialize::SIZE_UNLIMITED)
  126. {
  127. max_hdr_read = llmin(max_bytes + 1, max_hdr_read);
  128. }
  129. str.get(hdr_buf, max_hdr_read, '\n');
  130. S32 inbuf = (S32)str.gcount();
  131. // Reference: https://en.cppreference.com/w/cpp/io/basic_istream/get
  132. // When the get() above sees the specified delimiter '\n', it stops there
  133. // without pulling it from the stream. If it turns out that the stream does
  134. // NOT contain a header, and the content includes meaningful '\n', it is
  135. // important to pull that into hdr_buf too.
  136. if (inbuf < (S32)max_bytes && str.get(hdr_buf[inbuf]))
  137. {
  138. // Got the delimiting '\n'
  139. ++inbuf;
  140. // None of the following requires that hdr_buf contain a final '\0'
  141. // byte. We could store one if needed, since even the incremented
  142. // inbuf would not exceed sizeof(hdr_buf)-1, but there is no need.
  143. }
  144. std::string header(hdr_buf, (size_t)inbuf);
  145. if (str.fail())
  146. {
  147. str.clear();
  148. fail_if_not_legacy = true;
  149. }
  150. if (!strnicmp(LEGACY_NON_HEADER, hdr_buf, strlen(LEGACY_NON_HEADER)))
  151. {
  152. // Create a LLSD XML parser, and parse the first chunk read above.
  153. LLSDXMLParser x;
  154. // Parse the first part that was already read
  155. x.parsePart(hdr_buf, inbuf);
  156. // Parse the rest of it
  157. S32 parsed = x.parse(str, sd, max_bytes - inbuf);
  158. // Formally we should probably check (parsed != PARSE_FAILURE &&
  159. // parsed > 0), but since PARSE_FAILURE is -1, this suffices.
  160. return parsed > 0;
  161. }
  162. if (fail_if_not_legacy)
  163. {
  164. llwarns << "Deserialize LLSD parse failure" << llendl;
  165. return false;
  166. }
  167. // Remove the newline chars
  168. std::string::size_type lastchar = header.find_last_not_of("\r\n");
  169. if (lastchar != std::string::npos)
  170. {
  171. // It is important that find_last_not_of() returns size_type, which is
  172. // why lastchar explicitly declares the type above. erase(size_type)
  173. // erases from that offset to the end of the string, whereas
  174. // erase(iterator) erases only a single character.
  175. header.erase(lastchar + 1);
  176. }
  177. // Trim off the <? ... ?> header syntax
  178. std::string::size_type start = header.find_first_not_of("<? ");
  179. if (start != std::string::npos)
  180. {
  181. std::string::size_type end = header.find_first_of(" ?", start);
  182. if (end != std::string::npos)
  183. {
  184. header = header.substr(start, end - start);
  185. std::ws(str);
  186. }
  187. }
  188. // Create and invoke the parser as appropriate
  189. if (LLStringUtil::compareInsensitive(header, LLSD_BINARY_HEADER) == 0)
  190. {
  191. return parse_using<LLSDBinaryParser>(str, sd, max_bytes - inbuf) > 0;
  192. }
  193. if (LLStringUtil::compareInsensitive(header, LLSD_XML_HEADER) == 0)
  194. {
  195. return parse_using<LLSDXMLParser>(str, sd, max_bytes - inbuf) > 0;
  196. }
  197. if (LLStringUtil::compareInsensitive(header, LLSD_NOTATION_HEADER) == 0)
  198. {
  199. return parse_using<LLSDNotationParser>(str, sd, max_bytes - inbuf) > 0;
  200. }
  201. LLPointer<LLSDParser> p;
  202. if (inbuf && hdr_buf[0] == '<')
  203. {
  204. p = new LLSDXMLParser;
  205. LL_DEBUGS("Deserialize") << "No header found, assuming XML stream"
  206. << LL_ENDL;
  207. }
  208. else
  209. {
  210. p = new LLSDNotationParser;
  211. LL_DEBUGS("Deserialize") << "No header found, assuming notation stream"
  212. << LL_ENDL;
  213. }
  214. // Since we have already read 'inbuf' bytes into 'hdr_buf', prepend that
  215. // data to whatever remains in 'str'.
  216. LLMemoryStreamBuf already(reinterpret_cast<const U8*>(hdr_buf), inbuf);
  217. cat_streambuf prebuff(&already, str.rdbuf());
  218. std::istream prepend(&prebuff);
  219. return p->parse(prepend, sd, max_bytes) > 0;
  220. #else
  221. LLPointer<LLSDParser> p = NULL;
  222. // Get the first line before anything.
  223. str.get(hdr_buf, MAX_HDR_LEN, '\n');
  224. if (str.fail())
  225. {
  226. str.clear();
  227. fail_if_not_legacy = true;
  228. }
  229. bool legacy_no_header = false;
  230. std::string header;
  231. S32 inbuf = 0;
  232. if (!strnicmp(LEGACY_NON_HEADER, hdr_buf, strlen(LEGACY_NON_HEADER)))
  233. {
  234. legacy_no_header = true;
  235. inbuf = str.gcount();
  236. }
  237. else
  238. {
  239. if (fail_if_not_legacy)
  240. {
  241. goto fail;
  242. }
  243. // Remove the newline chars
  244. for (S32 i = 0; i < MAX_HDR_LEN; i++)
  245. {
  246. if (hdr_buf[i] == 0 || hdr_buf[i] == '\r' || hdr_buf[i] == '\n')
  247. {
  248. hdr_buf[i] = 0;
  249. break;
  250. }
  251. }
  252. header = hdr_buf;
  253. std::string::size_type start = std::string::npos;
  254. std::string::size_type end = std::string::npos;
  255. start = header.find_first_not_of("<? ");
  256. if (start != std::string::npos)
  257. {
  258. end = header.find_first_of(" ?", start);
  259. }
  260. if (start == std::string::npos || end == std::string::npos)
  261. {
  262. goto fail;
  263. }
  264. header = header.substr(start, end - start);
  265. std::ws(str);
  266. }
  267. // Create the parser as appropriate
  268. if (legacy_no_header)
  269. {
  270. // Create a LLSD XML parser, and parse the first chunk read above
  271. LLSDXMLParser* x = new LLSDXMLParser();
  272. // Parse the first part that was already read
  273. x->parsePart(hdr_buf, inbuf);
  274. // Parse the rest of it
  275. x->parseLines(str, sd);
  276. delete x;
  277. return true;
  278. }
  279. if (header == LLSD_BINARY_HEADER)
  280. {
  281. p = new LLSDBinaryParser;
  282. }
  283. else if (header == LLSD_XML_HEADER)
  284. {
  285. p = new LLSDXMLParser;
  286. }
  287. else if (header == LLSD_NOTATION_HEADER)
  288. {
  289. p = new LLSDNotationParser;
  290. }
  291. else
  292. {
  293. llwarns << "Deserialize request for unknown ELLSD_Serialize" << llendl;
  294. }
  295. if (p.notNull())
  296. {
  297. p->parse(str, sd, max_bytes);
  298. return true;
  299. }
  300. fail:
  301. llwarns << "deserialize LLSD parse failure" << llendl;
  302. return false;
  303. #endif
  304. }
  305. ///////////////////////////////////////////////////////////////////////////////
  306. // Endian handlers
  307. ///////////////////////////////////////////////////////////////////////////////
  308. #if LL_BIG_ENDIAN
  309. U64 ll_htonll(U64 hostlonglong) { return hostlonglong; }
  310. U64 ll_ntohll(U64 netlonglong) { return netlonglong; }
  311. F64 ll_htond(F64 hostlonglong) { return hostlonglong; }
  312. F64 ll_ntohd(F64 netlonglong) { return netlonglong; }
  313. #else
  314. // I read some comments one a indicating that doing an integer add here would
  315. // be faster than a bitwise or. For now, the or has programmer clarity, since
  316. // the intended outcome matches the operation.
  317. U64 ll_htonll(U64 hostlonglong)
  318. {
  319. return ((U64)(htonl((U32)((hostlonglong >> 32) & 0xFFFFFFFF))) |
  320. ((U64)(htonl((U32)(hostlonglong & 0xFFFFFFFF))) << 32));
  321. }
  322. U64 ll_ntohll(U64 netlonglong)
  323. {
  324. return ((U64)(ntohl((U32)((netlonglong >> 32) & 0xFFFFFFFF))) |
  325. ((U64)(ntohl((U32)(netlonglong & 0xFFFFFFFF))) << 32));
  326. }
  327. union LLEndianSwapper
  328. {
  329. F64 d;
  330. U64 i;
  331. };
  332. F64 ll_htond(F64 hostdouble)
  333. {
  334. LLEndianSwapper tmp;
  335. tmp.d = hostdouble;
  336. tmp.i = ll_htonll(tmp.i);
  337. return tmp.d;
  338. }
  339. F64 ll_ntohd(F64 netdouble)
  340. {
  341. LLEndianSwapper tmp;
  342. tmp.d = netdouble;
  343. tmp.i = ll_ntohll(tmp.i);
  344. return tmp.d;
  345. }
  346. #endif
  347. ///////////////////////////////////////////////////////////////////////////////
  348. // Local functions.
  349. ///////////////////////////////////////////////////////////////////////////////
  350. // Parses a delimited string.
  351. // istr: the stream to read from.
  352. // value [out]: the string which was found.
  353. // delim: the delimiter to use.
  354. // Returns number of bytes read off of the stream or PARSE_FAILURE (-1) on
  355. // failure.
  356. llssize deserialize_string_delim(std::istream& istr, std::string& value,
  357. char delim)
  358. {
  359. std::ostringstream write_buffer;
  360. bool found_escape = false;
  361. bool found_hex = false;
  362. bool found_digit = false;
  363. U8 byte = 0;
  364. llssize count = 0;
  365. while (true)
  366. {
  367. int next_byte = istr.get();
  368. ++count;
  369. if (istr.fail())
  370. {
  371. // If our stream is empty, break out
  372. value = write_buffer.str();
  373. return LLSDParser::PARSE_FAILURE;
  374. }
  375. char next_char = (char)next_byte; // Now that we know it is not EOF
  376. if (found_escape)
  377. {
  378. // next character(s) is a special sequence.
  379. if (found_hex)
  380. {
  381. if (found_digit)
  382. {
  383. found_digit = false;
  384. found_hex = false;
  385. found_escape = false;
  386. byte = byte << 4;
  387. byte |= hex_as_nybble(next_char);
  388. write_buffer << byte;
  389. byte = 0;
  390. }
  391. else
  392. {
  393. // next character is the first nybble of
  394. found_digit = true;
  395. byte = hex_as_nybble(next_char);
  396. }
  397. }
  398. else if (next_char == 'x')
  399. {
  400. found_hex = true;
  401. }
  402. else
  403. {
  404. switch (next_char)
  405. {
  406. case 'a':
  407. write_buffer << '\a';
  408. break;
  409. case 'b':
  410. write_buffer << '\b';
  411. break;
  412. case 'f':
  413. write_buffer << '\f';
  414. break;
  415. case 'n':
  416. write_buffer << '\n';
  417. break;
  418. case 'r':
  419. write_buffer << '\r';
  420. break;
  421. case 't':
  422. write_buffer << '\t';
  423. break;
  424. case 'v':
  425. write_buffer << '\v';
  426. break;
  427. default:
  428. write_buffer << next_char;
  429. break;
  430. }
  431. found_escape = false;
  432. }
  433. }
  434. else if (next_char == '\\')
  435. {
  436. found_escape = true;
  437. }
  438. else if (next_char == delim)
  439. {
  440. break;
  441. }
  442. else
  443. {
  444. write_buffer << next_char;
  445. }
  446. }
  447. value = write_buffer.str();
  448. return count;
  449. }
  450. // Reads a raw string off the stream.
  451. // istr: the stream to read from, with the (len) parameter leading the stream.
  452. // value [out]: the string which was found.
  453. // max_bytes: the maximum possible length of the string. Passing in a negative
  454. // value will skip this check.
  455. // Returns number of bytes read off of the stream or PARSE_FAILURE (-1) on
  456. // failure.
  457. llssize deserialize_string_raw(std::istream& istr, std::string& value,
  458. llssize max_bytes)
  459. {
  460. llssize count = 0;
  461. constexpr S32 BUF_LEN = 20;
  462. char buf[BUF_LEN];
  463. istr.get(buf, BUF_LEN - 1, ')');
  464. count += istr.gcount();
  465. int c = istr.get();
  466. c = istr.get();
  467. count += 2;
  468. if ((c == '"' || c == '\'') && buf[0] == '(')
  469. {
  470. // We probably have a valid raw string. Determine the size, and read
  471. // it. *FIX: This is memory inefficient.
  472. auto len = strtol(buf + 1, NULL, 0);
  473. if (max_bytes > 0 && len > max_bytes)
  474. {
  475. return LLSDParser::PARSE_FAILURE;
  476. }
  477. std::vector<char> buf;
  478. if (len)
  479. {
  480. buf.resize(len);
  481. count += fullread(istr, (char*)&buf[0], len);
  482. value.assign(buf.begin(), buf.end());
  483. }
  484. c = istr.get();
  485. ++count;
  486. if (c != '"' && c != '\'')
  487. {
  488. return LLSDParser::PARSE_FAILURE;
  489. }
  490. }
  491. else
  492. {
  493. return LLSDParser::PARSE_FAILURE;
  494. }
  495. return count;
  496. }
  497. // Figures out what kind of string it is (raw or delimited) and handoff.
  498. // istr: the stream to read from.
  499. // value [out]: the string which was found.
  500. // max_bytes: the maximum possible length of the string. Passing in a negative
  501. // value will skip this check.
  502. // Returns number of bytes read off of the stream or PARSE_FAILURE (-1) on
  503. // failure.
  504. llssize deserialize_string(std::istream& istr, std::string& value,
  505. llssize max_bytes)
  506. {
  507. int c = istr.get();
  508. if (istr.fail())
  509. {
  510. // No data in stream, bail out but mention the character we grabbed.
  511. return LLSDParser::PARSE_FAILURE;
  512. }
  513. llssize rv = LLSDParser::PARSE_FAILURE;
  514. switch (c)
  515. {
  516. case '\'':
  517. case '"':
  518. rv = deserialize_string_delim(istr, value, c);
  519. break;
  520. case 's':
  521. // Technically, less than max_bytes, but this is just meant to
  522. // catch egregious protocol errors. parse errors will be caught in
  523. // the case of incorrect counts.
  524. rv = deserialize_string_raw(istr, value, max_bytes);
  525. break;
  526. default:
  527. break;
  528. }
  529. if (rv == LLSDParser::PARSE_FAILURE)
  530. {
  531. return rv;
  532. }
  533. return rv + 1; // Account for the character grabbed at the top.
  534. }
  535. // Helper function for dealing with the different notation boolean format.
  536. // istr: the stream to read from, with the leading character stripped.
  537. // compare: the string to compare the boolean against
  538. // value: the value to assign to data if the parse succeeds.
  539. // Returns number of bytes read off of the stream or PARSE_FAILURE (-1) on
  540. // failure.
  541. llssize deserialize_boolean(std::istream& istr, LLSD& data,
  542. const std::string& compare, bool value)
  543. {
  544. // This method is a little goofy, because it gets the stream at the point
  545. // where the t or f has already been consumed. Basically, parse for a patch
  546. // to the string passed in starting at index 1. If it's a match:
  547. // * assign data to value
  548. // * return the number of bytes read
  549. // otherwise:
  550. // * set data to LLSD::null
  551. // * return LLSDParser::PARSE_FAILURE (-1)
  552. llssize bytes_read = 0;
  553. std::string::size_type ii = 0;
  554. char c = istr.peek();
  555. while (++ii < compare.size() && tolower(c) == (int)compare[ii] &&
  556. istr.good())
  557. {
  558. istr.ignore();
  559. ++bytes_read;
  560. c = istr.peek();
  561. }
  562. if (compare.size() != ii)
  563. {
  564. data.clear();
  565. return LLSDParser::PARSE_FAILURE;
  566. }
  567. data = value;
  568. return bytes_read;
  569. }
  570. static const char* NOTATION_STRING_CHARACTERS[256] =
  571. {
  572. "\\x00", // 0
  573. "\\x01", // 1
  574. "\\x02", // 2
  575. "\\x03", // 3
  576. "\\x04", // 4
  577. "\\x05", // 5
  578. "\\x06", // 6
  579. "\\a", // 7
  580. "\\b", // 8
  581. "\\t", // 9
  582. "\\n", // 10
  583. "\\v", // 11
  584. "\\f", // 12
  585. "\\r", // 13
  586. "\\x0e", // 14
  587. "\\x0f", // 15
  588. "\\x10", // 16
  589. "\\x11", // 17
  590. "\\x12", // 18
  591. "\\x13", // 19
  592. "\\x14", // 20
  593. "\\x15", // 21
  594. "\\x16", // 22
  595. "\\x17", // 23
  596. "\\x18", // 24
  597. "\\x19", // 25
  598. "\\x1a", // 26
  599. "\\x1b", // 27
  600. "\\x1c", // 28
  601. "\\x1d", // 29
  602. "\\x1e", // 30
  603. "\\x1f", // 31
  604. " ", // 32
  605. "!", // 33
  606. "\"", // 34
  607. "#", // 35
  608. "$", // 36
  609. "%", // 37
  610. "&", // 38
  611. "\\'", // 39
  612. "(", // 40
  613. ")", // 41
  614. "*", // 42
  615. "+", // 43
  616. ",", // 44
  617. "-", // 45
  618. ".", // 46
  619. "/", // 47
  620. "0", // 48
  621. "1", // 49
  622. "2", // 50
  623. "3", // 51
  624. "4", // 52
  625. "5", // 53
  626. "6", // 54
  627. "7", // 55
  628. "8", // 56
  629. "9", // 57
  630. ":", // 58
  631. ";", // 59
  632. "<", // 60
  633. "=", // 61
  634. ">", // 62
  635. "?", // 63
  636. "@", // 64
  637. "A", // 65
  638. "B", // 66
  639. "C", // 67
  640. "D", // 68
  641. "E", // 69
  642. "F", // 70
  643. "G", // 71
  644. "H", // 72
  645. "I", // 73
  646. "J", // 74
  647. "K", // 75
  648. "L", // 76
  649. "M", // 77
  650. "N", // 78
  651. "O", // 79
  652. "P", // 80
  653. "Q", // 81
  654. "R", // 82
  655. "S", // 83
  656. "T", // 84
  657. "U", // 85
  658. "V", // 86
  659. "W", // 87
  660. "X", // 88
  661. "Y", // 89
  662. "Z", // 90
  663. "[", // 91
  664. "\\\\", // 92
  665. "]", // 93
  666. "^", // 94
  667. "_", // 95
  668. "`", // 96
  669. "a", // 97
  670. "b", // 98
  671. "c", // 99
  672. "d", // 100
  673. "e", // 101
  674. "f", // 102
  675. "g", // 103
  676. "h", // 104
  677. "i", // 105
  678. "j", // 106
  679. "k", // 107
  680. "l", // 108
  681. "m", // 109
  682. "n", // 110
  683. "o", // 111
  684. "p", // 112
  685. "q", // 113
  686. "r", // 114
  687. "s", // 115
  688. "t", // 116
  689. "u", // 117
  690. "v", // 118
  691. "w", // 119
  692. "x", // 120
  693. "y", // 121
  694. "z", // 122
  695. "{", // 123
  696. "|", // 124
  697. "}", // 125
  698. "~", // 126
  699. "\\x7f", // 127
  700. "\\x80", // 128
  701. "\\x81", // 129
  702. "\\x82", // 130
  703. "\\x83", // 131
  704. "\\x84", // 132
  705. "\\x85", // 133
  706. "\\x86", // 134
  707. "\\x87", // 135
  708. "\\x88", // 136
  709. "\\x89", // 137
  710. "\\x8a", // 138
  711. "\\x8b", // 139
  712. "\\x8c", // 140
  713. "\\x8d", // 141
  714. "\\x8e", // 142
  715. "\\x8f", // 143
  716. "\\x90", // 144
  717. "\\x91", // 145
  718. "\\x92", // 146
  719. "\\x93", // 147
  720. "\\x94", // 148
  721. "\\x95", // 149
  722. "\\x96", // 150
  723. "\\x97", // 151
  724. "\\x98", // 152
  725. "\\x99", // 153
  726. "\\x9a", // 154
  727. "\\x9b", // 155
  728. "\\x9c", // 156
  729. "\\x9d", // 157
  730. "\\x9e", // 158
  731. "\\x9f", // 159
  732. "\\xa0", // 160
  733. "\\xa1", // 161
  734. "\\xa2", // 162
  735. "\\xa3", // 163
  736. "\\xa4", // 164
  737. "\\xa5", // 165
  738. "\\xa6", // 166
  739. "\\xa7", // 167
  740. "\\xa8", // 168
  741. "\\xa9", // 169
  742. "\\xaa", // 170
  743. "\\xab", // 171
  744. "\\xac", // 172
  745. "\\xad", // 173
  746. "\\xae", // 174
  747. "\\xaf", // 175
  748. "\\xb0", // 176
  749. "\\xb1", // 177
  750. "\\xb2", // 178
  751. "\\xb3", // 179
  752. "\\xb4", // 180
  753. "\\xb5", // 181
  754. "\\xb6", // 182
  755. "\\xb7", // 183
  756. "\\xb8", // 184
  757. "\\xb9", // 185
  758. "\\xba", // 186
  759. "\\xbb", // 187
  760. "\\xbc", // 188
  761. "\\xbd", // 189
  762. "\\xbe", // 190
  763. "\\xbf", // 191
  764. "\\xc0", // 192
  765. "\\xc1", // 193
  766. "\\xc2", // 194
  767. "\\xc3", // 195
  768. "\\xc4", // 196
  769. "\\xc5", // 197
  770. "\\xc6", // 198
  771. "\\xc7", // 199
  772. "\\xc8", // 200
  773. "\\xc9", // 201
  774. "\\xca", // 202
  775. "\\xcb", // 203
  776. "\\xcc", // 204
  777. "\\xcd", // 205
  778. "\\xce", // 206
  779. "\\xcf", // 207
  780. "\\xd0", // 208
  781. "\\xd1", // 209
  782. "\\xd2", // 210
  783. "\\xd3", // 211
  784. "\\xd4", // 212
  785. "\\xd5", // 213
  786. "\\xd6", // 214
  787. "\\xd7", // 215
  788. "\\xd8", // 216
  789. "\\xd9", // 217
  790. "\\xda", // 218
  791. "\\xdb", // 219
  792. "\\xdc", // 220
  793. "\\xdd", // 221
  794. "\\xde", // 222
  795. "\\xdf", // 223
  796. "\\xe0", // 224
  797. "\\xe1", // 225
  798. "\\xe2", // 226
  799. "\\xe3", // 227
  800. "\\xe4", // 228
  801. "\\xe5", // 229
  802. "\\xe6", // 230
  803. "\\xe7", // 231
  804. "\\xe8", // 232
  805. "\\xe9", // 233
  806. "\\xea", // 234
  807. "\\xeb", // 235
  808. "\\xec", // 236
  809. "\\xed", // 237
  810. "\\xee", // 238
  811. "\\xef", // 239
  812. "\\xf0", // 240
  813. "\\xf1", // 241
  814. "\\xf2", // 242
  815. "\\xf3", // 243
  816. "\\xf4", // 244
  817. "\\xf5", // 245
  818. "\\xf6", // 246
  819. "\\xf7", // 247
  820. "\\xf8", // 248
  821. "\\xf9", // 249
  822. "\\xfa", // 250
  823. "\\xfb", // 251
  824. "\\xfc", // 252
  825. "\\xfd", // 253
  826. "\\xfe", // 254
  827. "\\xff" // 255
  828. };
  829. // Does notation escaping of a string to an ostream.
  830. // value: the string to escape and serialize
  831. // str: the stream to serialize to.
  832. void serialize_string(const std::string& value, std::ostream& str)
  833. {
  834. std::string::const_iterator it = value.begin();
  835. std::string::const_iterator end = value.end();
  836. U8 c;
  837. for ( ; it != end; ++it)
  838. {
  839. c = (U8)(*it);
  840. str << NOTATION_STRING_CHARACTERS[c];
  841. }
  842. }
  843. std::ostream& operator<<(std::ostream& s, const LLSD& llsd)
  844. {
  845. s << LLSDNotationStreamer(llsd);
  846. return s;
  847. }
  848. /**
  849. * Local constants.
  850. */
  851. static const std::string NOTATION_TRUE_SERIAL("true");
  852. static const std::string NOTATION_FALSE_SERIAL("false");
  853. constexpr char BINARY_TRUE_SERIAL = '1';
  854. constexpr char BINARY_FALSE_SERIAL = '0';
  855. /**
  856. * LLSDParser
  857. */
  858. LLSDParser::LLSDParser()
  859. : mCheckLimits(true), mMaxBytesLeft(0), mParseLines(false)
  860. {
  861. }
  862. S32 LLSDParser::parse(std::istream& istr, LLSD& data, llssize max_bytes,
  863. S32 max_depth)
  864. {
  865. mCheckLimits = LLSDSerialize::SIZE_UNLIMITED != max_bytes;
  866. mMaxBytesLeft = max_bytes;
  867. return doParse(istr, data, max_depth);
  868. }
  869. // Parse using routine to get() lines, faster than parse()
  870. S32 LLSDParser::parseLines(std::istream& istr, LLSD& data)
  871. {
  872. mCheckLimits = false;
  873. mParseLines = true;
  874. return doParse(istr, data);
  875. }
  876. int LLSDParser::get(std::istream& istr) const
  877. {
  878. if (mCheckLimits)
  879. {
  880. --mMaxBytesLeft;
  881. }
  882. return istr.get();
  883. }
  884. std::istream& LLSDParser::get(std::istream& istr, char* s, std::streamsize n,
  885. char delim) const
  886. {
  887. istr.get(s, n, delim);
  888. if (mCheckLimits)
  889. {
  890. mMaxBytesLeft -= istr.gcount();
  891. }
  892. return istr;
  893. }
  894. std::istream& LLSDParser::get(std::istream& istr, std::streambuf& sb,
  895. char delim) const
  896. {
  897. istr.get(sb, delim);
  898. if (mCheckLimits)
  899. {
  900. mMaxBytesLeft -= istr.gcount();
  901. }
  902. return istr;
  903. }
  904. std::istream& LLSDParser::ignore(std::istream& istr) const
  905. {
  906. istr.ignore();
  907. if (mCheckLimits)
  908. {
  909. --mMaxBytesLeft;
  910. }
  911. return istr;
  912. }
  913. std::istream& LLSDParser::putback(std::istream& istr, char c) const
  914. {
  915. istr.putback(c);
  916. if (mCheckLimits)
  917. {
  918. ++mMaxBytesLeft;
  919. }
  920. return istr;
  921. }
  922. std::istream& LLSDParser::read(std::istream& istr, char* s,
  923. std::streamsize n) const
  924. {
  925. istr.read(s, n);
  926. if (mCheckLimits)
  927. {
  928. mMaxBytesLeft -= istr.gcount();
  929. }
  930. return istr;
  931. }
  932. void LLSDParser::account(llssize bytes) const
  933. {
  934. if (mCheckLimits)
  935. {
  936. mMaxBytesLeft -= bytes;
  937. }
  938. }
  939. /**
  940. * LLSDNotationParser
  941. */
  942. LLSDNotationParser::LLSDNotationParser()
  943. {
  944. }
  945. //virtual
  946. S32 LLSDNotationParser::doParse(std::istream& istr, LLSD& data,
  947. S32 max_depth) const
  948. {
  949. // map: { string:object, string:object }
  950. // array: [ object, object, object ]
  951. // undef: !
  952. // boolean: true | false | 1 | 0 | T | F | t | f | TRUE | FALSE
  953. // integer: i####
  954. // real: r####
  955. // uuid: u####
  956. // string: "g'day" | 'have a "nice" day' | s(size)"raw data"
  957. // uri: l"escaped"
  958. // date: d"YYYY-MM-DDTHH:MM:SS.FFZ"
  959. // binary: b##"ff3120ab1" | b(size)"raw data"
  960. if (max_depth == 0)
  961. {
  962. return PARSE_FAILURE;
  963. }
  964. char c = istr.peek();
  965. while (isspace(c))
  966. {
  967. // pop the whitespace.
  968. c = get(istr);
  969. c = istr.peek();
  970. continue;
  971. }
  972. if (!istr.good())
  973. {
  974. return 0;
  975. }
  976. S32 parse_count = 1;
  977. switch (c)
  978. {
  979. case '{':
  980. {
  981. S32 child_count = parseMap(istr, data, max_depth - 1);
  982. if (child_count == PARSE_FAILURE || data.isUndefined())
  983. {
  984. parse_count = PARSE_FAILURE;
  985. }
  986. else
  987. {
  988. parse_count += child_count;
  989. }
  990. if (istr.fail())
  991. {
  992. llinfos << "STREAM FAILURE reading map." << llendl;
  993. parse_count = PARSE_FAILURE;
  994. }
  995. break;
  996. }
  997. case '[':
  998. {
  999. S32 child_count = parseArray(istr, data, max_depth - 1);
  1000. if (child_count == PARSE_FAILURE || data.isUndefined())
  1001. {
  1002. parse_count = PARSE_FAILURE;
  1003. }
  1004. else
  1005. {
  1006. parse_count += child_count;
  1007. }
  1008. if (istr.fail())
  1009. {
  1010. llinfos << "STREAM FAILURE reading array." << llendl;
  1011. parse_count = PARSE_FAILURE;
  1012. }
  1013. break;
  1014. }
  1015. case '!':
  1016. c = get(istr);
  1017. data.clear();
  1018. break;
  1019. case '0':
  1020. c = get(istr);
  1021. data = false;
  1022. break;
  1023. case 'F':
  1024. case 'f':
  1025. ignore(istr);
  1026. c = istr.peek();
  1027. if (isalpha(c))
  1028. {
  1029. llssize cnt = deserialize_boolean(istr, data,
  1030. NOTATION_FALSE_SERIAL, false);
  1031. if (cnt == PARSE_FAILURE)
  1032. {
  1033. parse_count = cnt;
  1034. }
  1035. else
  1036. {
  1037. account(cnt);
  1038. }
  1039. }
  1040. else
  1041. {
  1042. data = false;
  1043. }
  1044. if (istr.fail())
  1045. {
  1046. llinfos << "STREAM FAILURE reading boolean." << llendl;
  1047. parse_count = PARSE_FAILURE;
  1048. }
  1049. break;
  1050. case '1':
  1051. c = get(istr);
  1052. data = true;
  1053. break;
  1054. case 'T':
  1055. case 't':
  1056. ignore(istr);
  1057. c = istr.peek();
  1058. if (isalpha(c))
  1059. {
  1060. llssize cnt = deserialize_boolean(istr, data, NOTATION_TRUE_SERIAL,
  1061. true);
  1062. if (cnt == PARSE_FAILURE)
  1063. {
  1064. parse_count = cnt;
  1065. }
  1066. else
  1067. {
  1068. account(cnt);
  1069. }
  1070. }
  1071. else
  1072. {
  1073. data = true;
  1074. }
  1075. if (istr.fail())
  1076. {
  1077. llinfos << "STREAM FAILURE reading boolean." << llendl;
  1078. parse_count = PARSE_FAILURE;
  1079. }
  1080. break;
  1081. case 'i':
  1082. {
  1083. c = get(istr);
  1084. S32 integer = 0;
  1085. istr >> integer;
  1086. data = integer;
  1087. if (istr.fail())
  1088. {
  1089. llinfos << "STREAM FAILURE reading integer." << llendl;
  1090. parse_count = PARSE_FAILURE;
  1091. }
  1092. break;
  1093. }
  1094. case 'r':
  1095. {
  1096. c = get(istr);
  1097. F64 real = 0.0;
  1098. istr >> real;
  1099. data = real;
  1100. if (istr.fail())
  1101. {
  1102. llinfos << "STREAM FAILURE reading real." << llendl;
  1103. parse_count = PARSE_FAILURE;
  1104. }
  1105. break;
  1106. }
  1107. case 'u':
  1108. {
  1109. c = get(istr);
  1110. LLUUID id;
  1111. istr >> id;
  1112. data = id;
  1113. if (istr.fail())
  1114. {
  1115. llinfos << "STREAM FAILURE reading uuid." << llendl;
  1116. parse_count = PARSE_FAILURE;
  1117. }
  1118. break;
  1119. }
  1120. case '\"':
  1121. case '\'':
  1122. case 's':
  1123. if (!parseString(istr, data))
  1124. {
  1125. parse_count = PARSE_FAILURE;
  1126. }
  1127. if (istr.fail())
  1128. {
  1129. llinfos << "STREAM FAILURE reading string." << llendl;
  1130. parse_count = PARSE_FAILURE;
  1131. }
  1132. break;
  1133. case 'l':
  1134. {
  1135. c = get(istr); // pop the 'l'
  1136. c = get(istr); // pop the delimiter
  1137. std::string str;
  1138. llssize cnt = deserialize_string_delim(istr, str, c);
  1139. if (cnt == PARSE_FAILURE)
  1140. {
  1141. parse_count = PARSE_FAILURE;
  1142. }
  1143. else
  1144. {
  1145. data = LLURI(str);
  1146. account(cnt);
  1147. }
  1148. if (istr.fail())
  1149. {
  1150. llinfos << "STREAM FAILURE reading link." << llendl;
  1151. parse_count = PARSE_FAILURE;
  1152. }
  1153. break;
  1154. }
  1155. case 'd':
  1156. {
  1157. c = get(istr); // pop the 'd'
  1158. c = get(istr); // pop the delimiter
  1159. std::string str;
  1160. llssize cnt = deserialize_string_delim(istr, str, c);
  1161. if (cnt == PARSE_FAILURE)
  1162. {
  1163. parse_count = PARSE_FAILURE;
  1164. }
  1165. else
  1166. {
  1167. data = LLDate(str);
  1168. account(cnt);
  1169. }
  1170. if (istr.fail())
  1171. {
  1172. llinfos << "STREAM FAILURE reading date." << llendl;
  1173. parse_count = PARSE_FAILURE;
  1174. }
  1175. break;
  1176. }
  1177. case 'b':
  1178. if (!parseBinary(istr, data))
  1179. {
  1180. parse_count = PARSE_FAILURE;
  1181. }
  1182. if (istr.fail())
  1183. {
  1184. llinfos << "STREAM FAILURE reading data." << llendl;
  1185. parse_count = PARSE_FAILURE;
  1186. }
  1187. break;
  1188. default:
  1189. parse_count = PARSE_FAILURE;
  1190. llinfos << "Unrecognized character while parsing: int(" << int(c)
  1191. << ")" << llendl;
  1192. break;
  1193. }
  1194. if (PARSE_FAILURE == parse_count)
  1195. {
  1196. data.clear();
  1197. }
  1198. return parse_count;
  1199. }
  1200. S32 LLSDNotationParser::parseMap(std::istream& istr, LLSD& map,
  1201. S32 max_depth) const
  1202. {
  1203. // map: { string:object, string:object }
  1204. map = LLSD::emptyMap();
  1205. S32 parse_count = 0;
  1206. char c = get(istr);
  1207. if (c == '{')
  1208. {
  1209. // eat commas, white
  1210. bool found_name = false;
  1211. std::string name;
  1212. c = get(istr);
  1213. while (c != '}' && istr.good())
  1214. {
  1215. if (!found_name)
  1216. {
  1217. if (c == '\"' || c == '\'' || c == 's')
  1218. {
  1219. putback(istr, c);
  1220. found_name = true;
  1221. llssize count = deserialize_string(istr, name,
  1222. mMaxBytesLeft);
  1223. if (count == PARSE_FAILURE)
  1224. {
  1225. return PARSE_FAILURE;
  1226. }
  1227. account(count);
  1228. }
  1229. c = get(istr);
  1230. }
  1231. else
  1232. {
  1233. if (isspace(c) || c == ':')
  1234. {
  1235. c = get(istr);
  1236. continue;
  1237. }
  1238. putback(istr, c);
  1239. LLSD child;
  1240. S32 count = doParse(istr, child, max_depth);
  1241. if (count > 0)
  1242. {
  1243. // There must be a value for every key, thus child_count
  1244. // must be greater than 0.
  1245. parse_count += count;
  1246. map.insert(name, child);
  1247. }
  1248. else
  1249. {
  1250. return PARSE_FAILURE;
  1251. }
  1252. found_name = false;
  1253. c = get(istr);
  1254. }
  1255. }
  1256. if (c != '}')
  1257. {
  1258. map.clear();
  1259. return PARSE_FAILURE;
  1260. }
  1261. }
  1262. return parse_count;
  1263. }
  1264. S32 LLSDNotationParser::parseArray(std::istream& istr, LLSD& array,
  1265. S32 max_depth) const
  1266. {
  1267. // array: [ object, object, object ]
  1268. array = LLSD::emptyArray();
  1269. S32 parse_count = 0;
  1270. char c = get(istr);
  1271. if (c == '[')
  1272. {
  1273. // eat commas, white
  1274. c = get(istr);
  1275. while (c != ']' && istr.good())
  1276. {
  1277. LLSD child;
  1278. if (isspace(c) || c == ',')
  1279. {
  1280. c = get(istr);
  1281. continue;
  1282. }
  1283. putback(istr, c);
  1284. S32 count = doParse(istr, child, max_depth);
  1285. if (count == PARSE_FAILURE)
  1286. {
  1287. return PARSE_FAILURE;
  1288. }
  1289. else
  1290. {
  1291. parse_count += count;
  1292. array.append(child);
  1293. }
  1294. c = get(istr);
  1295. }
  1296. if (c != ']')
  1297. {
  1298. return PARSE_FAILURE;
  1299. }
  1300. }
  1301. return parse_count;
  1302. }
  1303. bool LLSDNotationParser::parseString(std::istream& istr, LLSD& data) const
  1304. {
  1305. std::string value;
  1306. llssize count = deserialize_string(istr, value, mMaxBytesLeft);
  1307. if (count == PARSE_FAILURE)
  1308. {
  1309. return false;
  1310. }
  1311. account(count);
  1312. data = value;
  1313. return true;
  1314. }
  1315. bool LLSDNotationParser::parseBinary(std::istream& istr, LLSD& data) const
  1316. {
  1317. // binary: b##"ff3120ab1"
  1318. // or: b(len)"..."
  1319. // I want to manually control those values here to make sure the parser
  1320. // doesn't break when someone changes a constant somewhere else.
  1321. constexpr U32 BINARY_BUFFER_SIZE = 256;
  1322. constexpr U32 STREAM_GET_COUNT = 255;
  1323. // need to read the base out.
  1324. char buf[BINARY_BUFFER_SIZE];
  1325. get(istr, buf, STREAM_GET_COUNT, '"');
  1326. char c = get(istr);
  1327. if (c != '"') return false;
  1328. if (strncmp("b(", buf, 2) == 0)
  1329. {
  1330. // We probably have a valid raw binary stream. determine
  1331. // the size, and read it.
  1332. auto len = strtol(buf + 2, NULL, 0);
  1333. if (mCheckLimits && (len > mMaxBytesLeft)) return false;
  1334. LLSD::Binary value;
  1335. if (len)
  1336. {
  1337. value.resize(len);
  1338. account(fullread(istr, (char *)&value[0], len));
  1339. }
  1340. c = get(istr); // strip off the trailing double-quote
  1341. data = value;
  1342. }
  1343. else if (strncmp("b64", buf, 3) == 0)
  1344. {
  1345. std::stringstream coded_stream;
  1346. get(istr, *(coded_stream.rdbuf()), '\"');
  1347. c = get(istr);
  1348. std::string encoded(coded_stream.str());
  1349. S32 len = LLBase64::decodeLen(encoded.c_str());
  1350. LLSD::Binary value;
  1351. if (len)
  1352. {
  1353. value.resize(len);
  1354. len = LLBase64::decode(value.data(), encoded.c_str());
  1355. value.resize(len);
  1356. }
  1357. data = value;
  1358. }
  1359. else if (strncmp("b16", buf, 3) == 0)
  1360. {
  1361. // Yay, base 16. We pop the next character which is either a double
  1362. // quote or base 16 data. If it's a double quote, we're done parsing.
  1363. // If it's not, put the data back, and read the stream until the next
  1364. // double quote.
  1365. char* read;
  1366. U8 byte;
  1367. U8 byte_buffer[BINARY_BUFFER_SIZE];
  1368. U8* write;
  1369. LLSD::Binary value;
  1370. c = get(istr);
  1371. while (c != '"')
  1372. {
  1373. putback(istr, c);
  1374. read = buf;
  1375. write = byte_buffer;
  1376. get(istr, buf, STREAM_GET_COUNT, '"');
  1377. c = get(istr);
  1378. while (*read != '\0')
  1379. {
  1380. byte = hex_as_nybble(*read++);
  1381. byte = byte << 4;
  1382. byte |= hex_as_nybble(*read++);
  1383. *write++ = byte;
  1384. }
  1385. // Copy the data out of the byte buffer
  1386. value.insert(value.end(), byte_buffer, write);
  1387. }
  1388. data = value;
  1389. }
  1390. else
  1391. {
  1392. return false;
  1393. }
  1394. return true;
  1395. }
  1396. /**
  1397. * LLSDBinaryParser
  1398. */
  1399. LLSDBinaryParser::LLSDBinaryParser()
  1400. {
  1401. }
  1402. //virtual
  1403. S32 LLSDBinaryParser::doParse(std::istream& istr, LLSD& data,
  1404. S32 max_depth) const
  1405. {
  1406. /**
  1407. * Undefined: '!'<br>
  1408. * Boolean: '1' for true '0' for false<br>
  1409. * Integer: 'i' + 4 bytes network byte order<br>
  1410. * Real: 'r' + 8 bytes IEEE double<br>
  1411. * UUID: 'u' + 16 byte unsigned integer<br>
  1412. * String: 's' + 4 byte integer size + string<br>
  1413. * strings also secretly support the notation format
  1414. * Date: 'd' + 8 byte IEEE double for seconds since epoch<br>
  1415. * URI: 'l' + 4 byte integer size + string uri<br>
  1416. * Binary: 'b' + 4 byte integer size + binary data<br>
  1417. * Array: '[' + 4 byte integer size + all values + ']'<br>
  1418. * Map: '{' + 4 byte integer size every(key + value) + '}'<br>
  1419. * map keys are serialized as s + 4 byte integer size + string or
  1420. * in the notation format.
  1421. */
  1422. char c;
  1423. c = get(istr);
  1424. if (!istr.good())
  1425. {
  1426. return 0;
  1427. }
  1428. if (max_depth == 0)
  1429. {
  1430. return PARSE_FAILURE;
  1431. }
  1432. S32 parse_count = 1;
  1433. switch (c)
  1434. {
  1435. case '{':
  1436. {
  1437. S32 child_count = parseMap(istr, data, max_depth - 1);
  1438. if ((child_count == PARSE_FAILURE) || data.isUndefined())
  1439. {
  1440. parse_count = PARSE_FAILURE;
  1441. }
  1442. else
  1443. {
  1444. parse_count += child_count;
  1445. }
  1446. if (istr.fail())
  1447. {
  1448. llinfos << "STREAM FAILURE reading binary map." << llendl;
  1449. parse_count = PARSE_FAILURE;
  1450. }
  1451. break;
  1452. }
  1453. case '[':
  1454. {
  1455. S32 child_count = parseArray(istr, data, max_depth - 1);
  1456. if ((child_count == PARSE_FAILURE) || data.isUndefined())
  1457. {
  1458. parse_count = PARSE_FAILURE;
  1459. }
  1460. else
  1461. {
  1462. parse_count += child_count;
  1463. }
  1464. if (istr.fail())
  1465. {
  1466. llinfos << "STREAM FAILURE reading binary array." << llendl;
  1467. parse_count = PARSE_FAILURE;
  1468. }
  1469. break;
  1470. }
  1471. case '!':
  1472. data.clear();
  1473. break;
  1474. case '0':
  1475. data = false;
  1476. break;
  1477. case '1':
  1478. data = true;
  1479. break;
  1480. case 'i':
  1481. {
  1482. U32 value_nbo = 0;
  1483. read(istr, (char*)&value_nbo, sizeof(U32));
  1484. data = (S32)ntohl(value_nbo);
  1485. if (istr.fail())
  1486. {
  1487. llinfos << "STREAM FAILURE reading binary integer." << llendl;
  1488. }
  1489. break;
  1490. }
  1491. case 'r':
  1492. {
  1493. F64 real_nbo = 0.0;
  1494. read(istr, (char*)&real_nbo, sizeof(F64));
  1495. data = ll_ntohd(real_nbo);
  1496. if (istr.fail())
  1497. {
  1498. llinfos << "STREAM FAILURE reading binary real." << llendl;
  1499. }
  1500. break;
  1501. }
  1502. case 'u':
  1503. {
  1504. LLUUID id;
  1505. read(istr, (char*)(&id.mData), UUID_BYTES);
  1506. data = id;
  1507. if (istr.fail())
  1508. {
  1509. llinfos << "STREAM FAILURE reading binary uuid." << llendl;
  1510. }
  1511. break;
  1512. }
  1513. case '\'':
  1514. case '"':
  1515. {
  1516. std::string value;
  1517. llssize cnt = deserialize_string_delim(istr, value, c);
  1518. if (cnt == PARSE_FAILURE)
  1519. {
  1520. parse_count = PARSE_FAILURE;
  1521. }
  1522. else
  1523. {
  1524. data = value;
  1525. account(cnt);
  1526. }
  1527. if (istr.fail())
  1528. {
  1529. llinfos << "STREAM FAILURE reading binary (notation-style) string."
  1530. << llendl;
  1531. parse_count = PARSE_FAILURE;
  1532. }
  1533. break;
  1534. }
  1535. case 's':
  1536. {
  1537. std::string value;
  1538. if (parseString(istr, value))
  1539. {
  1540. data = value;
  1541. }
  1542. else
  1543. {
  1544. parse_count = PARSE_FAILURE;
  1545. }
  1546. if (istr.fail())
  1547. {
  1548. llinfos << "STREAM FAILURE reading binary string." << llendl;
  1549. parse_count = PARSE_FAILURE;
  1550. }
  1551. break;
  1552. }
  1553. case 'l':
  1554. {
  1555. std::string value;
  1556. if (parseString(istr, value))
  1557. {
  1558. data = LLURI(value);
  1559. }
  1560. else
  1561. {
  1562. parse_count = PARSE_FAILURE;
  1563. }
  1564. if (istr.fail())
  1565. {
  1566. llinfos << "STREAM FAILURE reading binary link." << llendl;
  1567. parse_count = PARSE_FAILURE;
  1568. }
  1569. break;
  1570. }
  1571. case 'd':
  1572. {
  1573. F64 real = 0.0;
  1574. read(istr, (char*)&real, sizeof(F64));
  1575. data = LLDate(real);
  1576. if (istr.fail())
  1577. {
  1578. llinfos << "STREAM FAILURE reading binary date." << llendl;
  1579. parse_count = PARSE_FAILURE;
  1580. }
  1581. break;
  1582. }
  1583. case 'b':
  1584. {
  1585. // We probably have a valid raw binary stream. Determine the size and
  1586. // read it.
  1587. U32 size_nbo = 0;
  1588. read(istr, (char*)&size_nbo, sizeof(U32));
  1589. S32 size = (S32)ntohl(size_nbo);
  1590. if (size < 0 || (mCheckLimits && size > mMaxBytesLeft))
  1591. {
  1592. parse_count = PARSE_FAILURE;
  1593. }
  1594. else
  1595. {
  1596. LLSD::Binary value;
  1597. if (size > 0)
  1598. {
  1599. value.resize(size);
  1600. account(fullread(istr, (char*)&value[0], size));
  1601. }
  1602. data = value;
  1603. }
  1604. if (istr.fail())
  1605. {
  1606. llinfos << "STREAM FAILURE reading binary." << llendl;
  1607. parse_count = PARSE_FAILURE;
  1608. }
  1609. break;
  1610. }
  1611. default:
  1612. parse_count = PARSE_FAILURE;
  1613. llinfos << "Unrecognized character while parsing: int(" << int(c)
  1614. << ")" << llendl;
  1615. break;
  1616. }
  1617. if (PARSE_FAILURE == parse_count)
  1618. {
  1619. data.clear();
  1620. }
  1621. return parse_count;
  1622. }
  1623. S32 LLSDBinaryParser::parseMap(std::istream& istr, LLSD& map,
  1624. S32 max_depth) const
  1625. {
  1626. map = LLSD::emptyMap();
  1627. U32 value_nbo = 0;
  1628. read(istr, (char*)&value_nbo, sizeof(U32));
  1629. S32 size = (S32)ntohl(value_nbo);
  1630. if (size < 0)
  1631. {
  1632. return PARSE_FAILURE;
  1633. }
  1634. S32 parse_count = 0;
  1635. S32 count = 0;
  1636. char c = get(istr);
  1637. while (c != '}' && (count < size) && istr.good())
  1638. {
  1639. std::string name;
  1640. switch (c)
  1641. {
  1642. case 'k':
  1643. if (!parseString(istr, name))
  1644. {
  1645. return PARSE_FAILURE;
  1646. }
  1647. break;
  1648. case '\'':
  1649. case '"':
  1650. {
  1651. llssize cnt = deserialize_string_delim(istr, name, c);
  1652. if (cnt == PARSE_FAILURE)
  1653. {
  1654. return PARSE_FAILURE;
  1655. }
  1656. account(cnt);
  1657. break;
  1658. }
  1659. }
  1660. LLSD child;
  1661. S32 child_count = doParse(istr, child, max_depth);
  1662. if (child_count > 0)
  1663. {
  1664. // There must be a value for every key, thus child_count must be
  1665. // greater than 0.
  1666. parse_count += child_count;
  1667. map.insert(name, child);
  1668. }
  1669. else
  1670. {
  1671. return PARSE_FAILURE;
  1672. }
  1673. ++count;
  1674. c = get(istr);
  1675. }
  1676. if (c != '}' || count < size)
  1677. {
  1678. // Make sure it is correctly terminated and we parsed as many as were
  1679. // said to be there.
  1680. return PARSE_FAILURE;
  1681. }
  1682. return parse_count;
  1683. }
  1684. S32 LLSDBinaryParser::parseArray(std::istream& istr, LLSD& array,
  1685. S32 max_depth) const
  1686. {
  1687. array = LLSD::emptyArray();
  1688. U32 value_nbo = 0;
  1689. read(istr, (char*)&value_nbo, sizeof(U32));
  1690. S32 size = (S32)ntohl(value_nbo);
  1691. if (size < 0)
  1692. {
  1693. return PARSE_FAILURE;
  1694. }
  1695. // *FIX: This would be a good place to reserve some space in the array...
  1696. S32 parse_count = 0;
  1697. S32 count = 0;
  1698. char c = istr.peek();
  1699. while (c != ']' && count < size && istr.good())
  1700. {
  1701. LLSD child;
  1702. S32 child_count = doParse(istr, child, max_depth);
  1703. if (PARSE_FAILURE == child_count)
  1704. {
  1705. return PARSE_FAILURE;
  1706. }
  1707. if (child_count)
  1708. {
  1709. parse_count += child_count;
  1710. array.append(child);
  1711. }
  1712. ++count;
  1713. c = istr.peek();
  1714. }
  1715. c = get(istr);
  1716. if (c != ']' || count < size)
  1717. {
  1718. // Make sure it is correctly terminated and we parsed as many as were
  1719. // said to be there.
  1720. return PARSE_FAILURE;
  1721. }
  1722. return parse_count;
  1723. }
  1724. bool LLSDBinaryParser::parseString(std::istream& istr,
  1725. std::string& value) const
  1726. {
  1727. // *FIX: This is memory inefficient.
  1728. U32 value_nbo = 0;
  1729. read(istr, (char*)&value_nbo, sizeof(U32));
  1730. S32 size = (S32)ntohl(value_nbo);
  1731. if (size < 0 || (mCheckLimits && size > mMaxBytesLeft))
  1732. {
  1733. return false;
  1734. }
  1735. if (size)
  1736. {
  1737. std::vector<char> buf;
  1738. buf.resize(size);
  1739. account(fullread(istr, &buf[0], size));
  1740. value.assign(buf.begin(), buf.end());
  1741. }
  1742. return true;
  1743. }
  1744. /**
  1745. * LLSDFormatter
  1746. */
  1747. LLSDFormatter::LLSDFormatter(bool bool_apha, const std::string& real_format,
  1748. EFormatterOptions options)
  1749. : mBoolAlpha(bool_apha),
  1750. mRealFormat(real_format),
  1751. mOptions(options)
  1752. {
  1753. }
  1754. S32 LLSDFormatter::format(const LLSD& data, std::ostream& ostr) const
  1755. {
  1756. // Pass options captured by constructor
  1757. return format(data, ostr, mOptions);
  1758. }
  1759. //virtual
  1760. S32 LLSDFormatter::format(const LLSD& data, std::ostream& ostr,
  1761. EFormatterOptions options) const
  1762. {
  1763. return format_impl(data, ostr, options, 0);
  1764. }
  1765. void LLSDFormatter::formatReal(LLSD::Real real, std::ostream& ostr) const
  1766. {
  1767. std::string buffer = llformat(mRealFormat.c_str(), real);
  1768. ostr << buffer;
  1769. }
  1770. /**
  1771. * LLSDNotationFormatter
  1772. */
  1773. LLSDNotationFormatter::LLSDNotationFormatter(bool bool_apha,
  1774. const std::string& real_format,
  1775. EFormatterOptions options)
  1776. : LLSDFormatter(bool_apha, real_format, options)
  1777. {
  1778. }
  1779. //static
  1780. std::string LLSDNotationFormatter::escapeString(const std::string& in)
  1781. {
  1782. std::ostringstream ostr;
  1783. serialize_string(in, ostr);
  1784. return ostr.str();
  1785. }
  1786. S32 LLSDNotationFormatter::format_impl(const LLSD& data, std::ostream& ostr,
  1787. EFormatterOptions options,
  1788. U32 level) const
  1789. {
  1790. S32 format_count = 1;
  1791. std::string pre;
  1792. std::string post;
  1793. if (options & LLSDFormatter::OPTIONS_PRETTY)
  1794. {
  1795. for (U32 i = 0; i < level; ++i)
  1796. {
  1797. pre += " ";
  1798. }
  1799. post = "\n";
  1800. }
  1801. switch (data.type())
  1802. {
  1803. case LLSD::TypeMap:
  1804. {
  1805. if (level) ostr << post << pre;
  1806. ostr << "{";
  1807. std::string inner_pre;
  1808. if (options & LLSDFormatter::OPTIONS_PRETTY)
  1809. {
  1810. inner_pre = pre + " ";
  1811. }
  1812. bool need_comma = false;
  1813. LLSD::map_const_iterator iter = data.beginMap();
  1814. LLSD::map_const_iterator end = data.endMap();
  1815. for ( ; iter != end; ++iter)
  1816. {
  1817. if (need_comma) ostr << ",";
  1818. need_comma = true;
  1819. ostr << post << inner_pre << '\'';
  1820. serialize_string(iter->first, ostr);
  1821. ostr << "':";
  1822. format_count += format_impl(iter->second, ostr,
  1823. options, level + 2);
  1824. }
  1825. ostr << post << pre << "}";
  1826. break;
  1827. }
  1828. case LLSD::TypeArray:
  1829. {
  1830. ostr << post << pre << "[";
  1831. bool need_comma = false;
  1832. LLSD::array_const_iterator iter = data.beginArray();
  1833. LLSD::array_const_iterator end = data.endArray();
  1834. for ( ; iter != end; ++iter)
  1835. {
  1836. if (need_comma) ostr << ",";
  1837. need_comma = true;
  1838. format_count += format_impl(*iter, ostr, options, level + 1);
  1839. }
  1840. ostr << "]";
  1841. break;
  1842. }
  1843. case LLSD::TypeUndefined:
  1844. ostr << "!";
  1845. break;
  1846. case LLSD::TypeBoolean:
  1847. #if (LL_WINDOWS || __GNUC__ > 2)
  1848. if (mBoolAlpha || (ostr.flags() & std::ios::boolalpha))
  1849. #else
  1850. if (mBoolAlpha || (ostr.flags() & 0x0100))
  1851. #endif
  1852. {
  1853. ostr << (data.asBoolean() ? NOTATION_TRUE_SERIAL
  1854. : NOTATION_FALSE_SERIAL);
  1855. }
  1856. else
  1857. {
  1858. ostr << (data.asBoolean() ? 1 : 0);
  1859. }
  1860. break;
  1861. case LLSD::TypeInteger:
  1862. ostr << "i" << data.asInteger();
  1863. break;
  1864. case LLSD::TypeReal:
  1865. ostr << "r";
  1866. if (mRealFormat.empty())
  1867. {
  1868. ostr << data.asReal();
  1869. }
  1870. else
  1871. {
  1872. formatReal(data.asReal(), ostr);
  1873. }
  1874. break;
  1875. case LLSD::TypeUUID:
  1876. ostr << "u" << data.asUUID();
  1877. break;
  1878. case LLSD::TypeString:
  1879. ostr << '\'';
  1880. serialize_string(data.asStringRef(), ostr);
  1881. ostr << '\'';
  1882. break;
  1883. case LLSD::TypeDate:
  1884. ostr << "d\"" << data.asDate() << "\"";
  1885. break;
  1886. case LLSD::TypeURI:
  1887. ostr << "l\"";
  1888. serialize_string(data.asString(), ostr);
  1889. ostr << "\"";
  1890. break;
  1891. case LLSD::TypeBinary:
  1892. {
  1893. const LLSD::Binary& buffer = data.asBinary();
  1894. size_t count = buffer.size();
  1895. if (options & LLSDFormatter::OPTIONS_PRETTY_BINARY)
  1896. {
  1897. ostr << "b16\"";
  1898. if (count)
  1899. {
  1900. std::ios_base::fmtflags old_flags = ostr.flags();
  1901. ostr.setf(std::ios::hex, std::ios::basefield);
  1902. ostr << std::uppercase;
  1903. auto oldfill(ostr.fill('0'));
  1904. auto oldwidth(ostr.width());
  1905. for (size_t i = 0; i < count; ++i)
  1906. {
  1907. // Have to re-state setw() before every conversion
  1908. ostr << std::setw(2) << (S32)buffer[i];
  1909. }
  1910. ostr.width(oldwidth);
  1911. ostr.fill(oldfill);
  1912. ostr.flags(old_flags);
  1913. }
  1914. }
  1915. else
  1916. {
  1917. ostr << "b(" << count << ")\"";
  1918. if (count)
  1919. {
  1920. ostr.write((const char*)&buffer[0], count);
  1921. }
  1922. }
  1923. ostr << "\"";
  1924. break;
  1925. }
  1926. default:
  1927. // *NOTE: This should never happen.
  1928. ostr << "!";
  1929. break;
  1930. }
  1931. return format_count;
  1932. }
  1933. /**
  1934. * LLSDBinaryFormatter
  1935. */
  1936. LLSDBinaryFormatter::LLSDBinaryFormatter(bool bool_apha,
  1937. const std::string& real_format,
  1938. EFormatterOptions options)
  1939. : LLSDFormatter(bool_apha, real_format, options)
  1940. {
  1941. }
  1942. //virtual
  1943. S32 LLSDBinaryFormatter::format_impl(const LLSD& data, std::ostream& ostr,
  1944. EFormatterOptions options,
  1945. U32 level) const
  1946. {
  1947. S32 format_count = 1;
  1948. switch (data.type())
  1949. {
  1950. case LLSD::TypeMap:
  1951. {
  1952. ostr.put('{');
  1953. U32 size_nbo = htonl(data.size());
  1954. ostr.write((const char*)(&size_nbo), sizeof(U32));
  1955. LLSD::map_const_iterator iter = data.beginMap();
  1956. LLSD::map_const_iterator end = data.endMap();
  1957. for ( ; iter != end; ++iter)
  1958. {
  1959. ostr.put('k');
  1960. formatString(iter->first, ostr);
  1961. format_count += format_impl(iter->second, ostr, options,
  1962. level + 1);
  1963. }
  1964. ostr.put('}');
  1965. break;
  1966. }
  1967. case LLSD::TypeArray:
  1968. {
  1969. ostr.put('[');
  1970. U32 size_nbo = htonl(data.size());
  1971. ostr.write((const char*)(&size_nbo), sizeof(U32));
  1972. LLSD::array_const_iterator iter = data.beginArray();
  1973. LLSD::array_const_iterator end = data.endArray();
  1974. for ( ; iter != end; ++iter)
  1975. {
  1976. format_count += format_impl(*iter, ostr, options, level + 1);
  1977. }
  1978. ostr.put(']');
  1979. break;
  1980. }
  1981. case LLSD::TypeUndefined:
  1982. ostr.put('!');
  1983. break;
  1984. case LLSD::TypeBoolean:
  1985. if (data.asBoolean())
  1986. {
  1987. ostr.put(BINARY_TRUE_SERIAL);
  1988. }
  1989. else
  1990. {
  1991. ostr.put(BINARY_FALSE_SERIAL);
  1992. }
  1993. break;
  1994. case LLSD::TypeInteger:
  1995. {
  1996. ostr.put('i');
  1997. U32 value_nbo = htonl(data.asInteger());
  1998. ostr.write((const char*)(&value_nbo), sizeof(U32));
  1999. break;
  2000. }
  2001. case LLSD::TypeReal:
  2002. {
  2003. ostr.put('r');
  2004. F64 value_nbo = ll_htond(data.asReal());
  2005. ostr.write((const char*)(&value_nbo), sizeof(F64));
  2006. break;
  2007. }
  2008. case LLSD::TypeUUID:
  2009. {
  2010. ostr.put('u');
  2011. LLUUID value = data.asUUID();
  2012. ostr.write((const char*)(&(value.mData)), UUID_BYTES);
  2013. break;
  2014. }
  2015. case LLSD::TypeString:
  2016. ostr.put('s');
  2017. formatString(data.asStringRef(), ostr);
  2018. break;
  2019. case LLSD::TypeDate:
  2020. {
  2021. ostr.put('d');
  2022. F64 value = data.asReal();
  2023. ostr.write((const char*)(&value), sizeof(F64));
  2024. break;
  2025. }
  2026. case LLSD::TypeURI:
  2027. ostr.put('l');
  2028. formatString(data.asString(), ostr);
  2029. break;
  2030. case LLSD::TypeBinary:
  2031. {
  2032. ostr.put('b');
  2033. const LLSD::Binary& buffer = data.asBinary();
  2034. U32 size_nbo = htonl(buffer.size());
  2035. ostr.write((const char*)(&size_nbo), sizeof(U32));
  2036. if (buffer.size())
  2037. {
  2038. ostr.write((const char*)&buffer[0], buffer.size());
  2039. }
  2040. break;
  2041. }
  2042. default:
  2043. // *NOTE: This should never happen.
  2044. ostr.put('!');
  2045. break;
  2046. }
  2047. return format_count;
  2048. }
  2049. void LLSDBinaryFormatter::formatString(const std::string& string,
  2050. std::ostream& ostr) const
  2051. {
  2052. U32 size_nbo = htonl(string.size());
  2053. ostr.write((const char*)(&size_nbo), sizeof(U32));
  2054. ostr.write(string.c_str(), string.size());
  2055. }
  2056. // Dirty little zippers: yell at Davep if these are horrid
  2057. constexpr U32 CHUNK_SIZE = 512 * 1024;
  2058. // Note: we cannot use too large a thread local storage, or we would cause
  2059. // crashes (seen under Linux with: static thread_local U8 tZipBuffer[65536]).
  2060. // So we instead use a thread_local unique_ptr pointing to a runtime allocated
  2061. // storage (thus allocated on the heap, but only once per thread); when the
  2062. // unique_ptr is destroyed at thread exit, it also destroys the storage. HB
  2063. static thread_local std::unique_ptr<U8[]> tZipperBufferp;
  2064. static U8* get_zipper_buffer()
  2065. {
  2066. if (!tZipperBufferp)
  2067. {
  2068. tZipperBufferp =
  2069. std::unique_ptr<U8[]>(new(std::nothrow) U8[CHUNK_SIZE]);
  2070. }
  2071. return tZipperBufferp.get();
  2072. }
  2073. // Returns a string containing gzipped bytes of binary serialized LLSD VERY
  2074. // inefficient (creates several copies of LLSD block in memory).
  2075. std::string zip_llsd(LLSD& data)
  2076. {
  2077. std::stringstream llsd_strm;
  2078. LLSDSerialize::toBinary(data, llsd_strm);
  2079. z_stream strm;
  2080. strm.zalloc = Z_NULL;
  2081. strm.zfree = Z_NULL;
  2082. strm.opaque = Z_NULL;
  2083. S32 ret = deflateInit(&strm, Z_BEST_COMPRESSION);
  2084. if (ret != Z_OK)
  2085. {
  2086. llwarns << "Failed to compress LLSD block." << llendl;
  2087. return std::string();
  2088. }
  2089. std::string source = llsd_strm.str();
  2090. strm.avail_in = (unsigned int)source.size();
  2091. strm.next_in = (unsigned char*)source.data();
  2092. U8* output = NULL;
  2093. llssize cur_size = 0;
  2094. U32 have = 0;
  2095. U8* out = get_zipper_buffer();
  2096. do
  2097. {
  2098. strm.avail_out = CHUNK_SIZE;
  2099. strm.next_out = out;
  2100. ret = deflate(&strm, Z_FINISH);
  2101. if (ret == Z_OK || ret == Z_STREAM_END)
  2102. {
  2103. // Copy result into output
  2104. if (strm.avail_out >= CHUNK_SIZE)
  2105. {
  2106. free(output);
  2107. llwarns << "Failed to compress LLSD block." << llendl;
  2108. return std::string();
  2109. }
  2110. have = CHUNK_SIZE - strm.avail_out;
  2111. output = (U8*)realloc(output, cur_size + have);
  2112. if (!output)
  2113. {
  2114. LLMemory::allocationFailed(cur_size + have);
  2115. llwarns << "Out of memory while decompressing LLSD." << llendl;
  2116. deflateEnd(&strm);
  2117. return std::string();
  2118. }
  2119. memcpy(output + cur_size, out, have);
  2120. cur_size += have;
  2121. }
  2122. else
  2123. {
  2124. free(output);
  2125. llwarns << "Failed to compress LLSD block." << llendl;
  2126. return std::string();
  2127. }
  2128. }
  2129. while (ret == Z_OK);
  2130. std::string::size_type size = cur_size;
  2131. std::string result((char*) output, size);
  2132. deflateEnd(&strm);
  2133. free(output);
  2134. #if 0 // Verify results work with unzip_llsd
  2135. std::istringstream test(result);
  2136. LLSD test_sd;
  2137. if (!unzip_llsd(test_sd, test, result.size()))
  2138. {
  2139. llerrs << "Invalid compression result !" << llendl;
  2140. }
  2141. #endif
  2142. return result;
  2143. }
  2144. // Decompress a block of LLSD from provided istream. Not very efficient:
  2145. // creates a copy of decompressed LLSD block in memory and deserializes
  2146. // from that copy using LLSDSerialize.
  2147. bool unzip_llsd(LLSD& data, std::istream& is, S32 size)
  2148. {
  2149. U8* in = new(std::nothrow) U8[size];
  2150. if (!in)
  2151. {
  2152. LLMemory::allocationFailed(size);
  2153. return false;
  2154. }
  2155. is.read((char*)in, size);
  2156. bool ret = unzip_llsd(data, in, size);
  2157. delete[] in;
  2158. return ret;
  2159. }
  2160. bool unzip_llsd(LLSD& data, const U8* in, S32 size)
  2161. {
  2162. U8* result = NULL;
  2163. llssize cur_size = 0;
  2164. z_stream strm;
  2165. strm.zalloc = Z_NULL;
  2166. strm.zfree = Z_NULL;
  2167. strm.opaque = Z_NULL;
  2168. strm.avail_in = (unsigned int)size;
  2169. strm.next_in = (unsigned char*)in;
  2170. S32 ret = inflateInit(&strm);
  2171. U8* out = get_zipper_buffer();
  2172. do
  2173. {
  2174. strm.avail_out = CHUNK_SIZE;
  2175. strm.next_out = out;
  2176. ret = inflate(&strm, Z_NO_FLUSH);
  2177. if (ret == Z_STREAM_ERROR)
  2178. {
  2179. inflateEnd(&strm);
  2180. if (result)
  2181. {
  2182. free(result);
  2183. }
  2184. LL_DEBUGS("UnzipLLSD") << "Z_STREAM_ERROR" << LL_ENDL;
  2185. return false;
  2186. }
  2187. switch (ret)
  2188. {
  2189. case Z_NEED_DICT:
  2190. LL_DEBUGS("UnzipLLSD") << "Z_NEED_DICT" << LL_ENDL;
  2191. ret = Z_DATA_ERROR;
  2192. case Z_DATA_ERROR:
  2193. case Z_MEM_ERROR:
  2194. inflateEnd(&strm);
  2195. if (result)
  2196. {
  2197. free(result);
  2198. }
  2199. LL_DEBUGS("UnzipLLSD") << (ret == Z_DATA_ERROR ? "Z_DATA_ERROR"
  2200. : "Z_MEM_ERROR")
  2201. << LL_ENDL;
  2202. return false;
  2203. default:
  2204. break;
  2205. }
  2206. U32 have = CHUNK_SIZE - strm.avail_out;
  2207. U8* tmp = (U8*)realloc(result, cur_size + have);
  2208. if (!tmp)
  2209. {
  2210. LLMemory::allocationFailed(cur_size + have);
  2211. if (result)
  2212. {
  2213. free(result);
  2214. }
  2215. inflateEnd(&strm);
  2216. return false;
  2217. }
  2218. result = tmp;
  2219. memcpy(result + cur_size, out, have);
  2220. cur_size += have;
  2221. }
  2222. while (ret == Z_OK);
  2223. inflateEnd(&strm);
  2224. if (ret != Z_STREAM_END)
  2225. {
  2226. LL_DEBUGS("UnzipLLSD") << "Error #" << ret << LL_ENDL;
  2227. if (result)
  2228. {
  2229. free(result);
  2230. }
  2231. return false;
  2232. }
  2233. // 'result' now points to the decompressed LLSD block
  2234. static char deprecated_header[] = "<? LLSD/Binary ?>";
  2235. static size_t deprecated_header_size = strlen(deprecated_header);
  2236. char* datap = (char*)result;
  2237. if (!strncmp(datap, deprecated_header, deprecated_header_size))
  2238. {
  2239. datap += deprecated_header_size;
  2240. cur_size -= deprecated_header_size;
  2241. }
  2242. boost::iostreams::stream<boost::iostreams::array_source> is(datap,
  2243. cur_size);
  2244. if (!LLSDSerialize::fromBinary(data, is, cur_size, UNZIP_LLSD_MAX_DEPTH))
  2245. {
  2246. llwarns << "Failed to unzip LLSD block" << llendl;
  2247. free(result);
  2248. return false;
  2249. }
  2250. free(result);
  2251. return true;
  2252. }
  2253. // This unzip function will only work with a gzip header and trailer - while
  2254. // the contents of the actual compressed data is the same for either format
  2255. // (gzip vs zlib), the headers and trailers are different for the formats.
  2256. U8* unzip_llsdNavMesh(bool& valid, size_t& outsize, const U8* in, S32 size)
  2257. {
  2258. if (size <= 0)
  2259. {
  2260. llwarns << "No data to unzip" << llendl;
  2261. return NULL;
  2262. }
  2263. U8* result = NULL;
  2264. llssize cur_size = 0;
  2265. z_stream strm;
  2266. strm.zalloc = Z_NULL;
  2267. strm.zfree = Z_NULL;
  2268. strm.opaque = Z_NULL;
  2269. strm.avail_in = (unsigned int)size;
  2270. strm.next_in = (unsigned char*)in;
  2271. U8* out = get_zipper_buffer();
  2272. valid = true;
  2273. S32 ret = inflateInit2(&strm, WINDOW_BITS | ENABLE_ZLIB_GZIP);
  2274. do
  2275. {
  2276. strm.avail_out = CHUNK_SIZE;
  2277. strm.next_out = out;
  2278. ret = inflate(&strm, Z_NO_FLUSH);
  2279. switch (ret)
  2280. {
  2281. case Z_STREAM_ERROR:
  2282. LL_DEBUGS("UnzipLLSD") << "Z_STREAM_ERROR" << LL_ENDL;
  2283. valid = false;
  2284. case Z_NEED_DICT:
  2285. LL_DEBUGS("UnzipLLSD") << "Z_NEED_DICT" << LL_ENDL;
  2286. valid = false;
  2287. case Z_DATA_ERROR:
  2288. LL_DEBUGS("UnzipLLSD") << "Z_DATA_ERROR" << LL_ENDL;
  2289. valid = false;
  2290. case Z_MEM_ERROR:
  2291. if (valid)
  2292. {
  2293. LL_DEBUGS("UnzipLLSD") << "Z_MEM_ERROR" << LL_ENDL;
  2294. valid = false;
  2295. }
  2296. inflateEnd(&strm);
  2297. if (result)
  2298. {
  2299. free(result);
  2300. }
  2301. return NULL;
  2302. default:
  2303. break;
  2304. }
  2305. U32 have = CHUNK_SIZE - strm.avail_out;
  2306. U8* tmp = (U8*)realloc(result, cur_size + have);
  2307. if (!tmp)
  2308. {
  2309. LLMemory::allocationFailed(cur_size + have);
  2310. if (result)
  2311. {
  2312. free(result);
  2313. }
  2314. inflateEnd(&strm);
  2315. valid = false;
  2316. return NULL;
  2317. }
  2318. result = tmp;
  2319. memcpy(result + cur_size, out, have);
  2320. cur_size += have;
  2321. }
  2322. while (ret == Z_OK);
  2323. inflateEnd(&strm);
  2324. if (ret != Z_STREAM_END)
  2325. {
  2326. if (result)
  2327. {
  2328. free(result);
  2329. }
  2330. valid = false;
  2331. LL_DEBUGS("UnzipLLSD") << "Error #" << ret << LL_ENDL;
  2332. return NULL;
  2333. }
  2334. // Result now points to the decompressed LLSD block
  2335. outsize = cur_size;
  2336. valid = true;
  2337. return result;
  2338. }
  2339. ///////////////////////////////////////////////////////////////////////////////
  2340. // LLSDXMLFormatter and LLSDXMLParser classes (used to be in a separate
  2341. // llsdserialize_xml.cpp file, despite the fact they implement classes declared
  2342. // in llsdserialize.h... Go figure ! HB)
  2343. ///////////////////////////////////////////////////////////////////////////////
  2344. ///////////////////////////////////////////////////////////////////////////////
  2345. // LLSDXMLFormatter class
  2346. ///////////////////////////////////////////////////////////////////////////////
  2347. LLSDXMLFormatter::LLSDXMLFormatter(bool bool_apha,
  2348. const std::string& real_format,
  2349. EFormatterOptions options)
  2350. : LLSDFormatter(bool_apha, real_format, options)
  2351. {
  2352. }
  2353. //virtual
  2354. S32 LLSDXMLFormatter::format(const LLSD& data, std::ostream& ostr,
  2355. EFormatterOptions options) const
  2356. {
  2357. std::streamsize old_precision = ostr.precision(25);
  2358. std::string post;
  2359. if (options & LLSDFormatter::OPTIONS_PRETTY)
  2360. {
  2361. post = "\n";
  2362. }
  2363. ostr << "<llsd>" << post;
  2364. S32 rv = format_impl(data, ostr, options, 1);
  2365. ostr << "</llsd>\n";
  2366. ostr.precision(old_precision);
  2367. return rv;
  2368. }
  2369. S32 LLSDXMLFormatter::format_impl(const LLSD& data, std::ostream& ostr,
  2370. EFormatterOptions options, U32 level) const
  2371. {
  2372. S32 format_count = 1;
  2373. std::string pre;
  2374. std::string post;
  2375. if (options & LLSDFormatter::OPTIONS_PRETTY)
  2376. {
  2377. for (U32 i = 0; i < level; ++i)
  2378. {
  2379. pre += " ";
  2380. }
  2381. post = "\n";
  2382. }
  2383. switch (data.type())
  2384. {
  2385. case LLSD::TypeMap:
  2386. if (0 == data.size())
  2387. {
  2388. ostr << pre << "<map />" << post;
  2389. }
  2390. else
  2391. {
  2392. ostr << pre << "<map>" << post;
  2393. LLSD::map_const_iterator iter = data.beginMap();
  2394. LLSD::map_const_iterator end = data.endMap();
  2395. for (; iter != end; ++iter)
  2396. {
  2397. ostr << pre << "<key>" << escapeString((*iter).first) << "</key>" << post;
  2398. format_count += format_impl((*iter).second, ostr, options, level + 1);
  2399. }
  2400. ostr << pre << "</map>" << post;
  2401. }
  2402. break;
  2403. case LLSD::TypeArray:
  2404. if (0 == data.size())
  2405. {
  2406. ostr << pre << "<array />" << post;
  2407. }
  2408. else
  2409. {
  2410. ostr << pre << "<array>" << post;
  2411. LLSD::array_const_iterator iter = data.beginArray();
  2412. LLSD::array_const_iterator end = data.endArray();
  2413. for(; iter != end; ++iter)
  2414. {
  2415. format_count += format_impl(*iter, ostr, options, level + 1);
  2416. }
  2417. ostr << pre << "</array>" << post;
  2418. }
  2419. break;
  2420. case LLSD::TypeUndefined:
  2421. ostr << pre << "<undef />" << post;
  2422. break;
  2423. case LLSD::TypeBoolean:
  2424. ostr << pre << "<boolean>";
  2425. if (mBoolAlpha || (ostr.flags() & std::ios::boolalpha))
  2426. {
  2427. ostr << (data.asBoolean() ? "true" : "false");
  2428. }
  2429. else
  2430. {
  2431. ostr << (data.asBoolean() ? 1 : 0);
  2432. }
  2433. ostr << "</boolean>" << post;
  2434. break;
  2435. case LLSD::TypeInteger:
  2436. ostr << pre << "<integer>" << data.asInteger() << "</integer>" << post;
  2437. break;
  2438. case LLSD::TypeReal:
  2439. ostr << pre << "<real>";
  2440. if (mRealFormat.empty())
  2441. {
  2442. ostr << data.asReal();
  2443. }
  2444. else
  2445. {
  2446. formatReal(data.asReal(), ostr);
  2447. }
  2448. ostr << "</real>" << post;
  2449. break;
  2450. case LLSD::TypeUUID:
  2451. if (data.asUUID().isNull())
  2452. {
  2453. ostr << pre << "<uuid />" << post;
  2454. }
  2455. else
  2456. {
  2457. ostr << pre << "<uuid>" << data.asUUID() << "</uuid>" << post;
  2458. }
  2459. break;
  2460. case LLSD::TypeString:
  2461. if (data.asStringRef().empty())
  2462. {
  2463. ostr << pre << "<string />" << post;
  2464. }
  2465. else
  2466. {
  2467. ostr << pre << "<string>" << escapeString(data.asStringRef())
  2468. << "</string>" << post;
  2469. }
  2470. break;
  2471. case LLSD::TypeDate:
  2472. ostr << pre << "<date>" << data.asDate() << "</date>" << post;
  2473. break;
  2474. case LLSD::TypeURI:
  2475. ostr << pre << "<uri>" << escapeString(data.asString()) << "</uri>"
  2476. << post;
  2477. break;
  2478. case LLSD::TypeBinary:
  2479. {
  2480. const LLSD::Binary& buffer = data.asBinary();
  2481. if (buffer.empty())
  2482. {
  2483. ostr << pre << "<binary />" << post;
  2484. }
  2485. else
  2486. {
  2487. ostr << pre << "<binary encoding=\"base64\">";
  2488. size_t buffsz = buffer.size();
  2489. size_t b64_buffer_len = LLBase64::encodeLen(buffsz);
  2490. char* b64_buffer = new char[b64_buffer_len];
  2491. b64_buffer_len = LLBase64::encode(b64_buffer, buffer.data(),
  2492. buffsz);
  2493. ostr.write(b64_buffer, b64_buffer_len - 1);
  2494. delete[] b64_buffer;
  2495. ostr << "</binary>" << post;
  2496. }
  2497. break;
  2498. }
  2499. default:
  2500. // *NOTE: This should never happen.
  2501. ostr << pre << "<undef />" << post;
  2502. break;
  2503. }
  2504. return format_count;
  2505. }
  2506. //static
  2507. std::string LLSDXMLFormatter::escapeString(const std::string& in)
  2508. {
  2509. std::ostringstream out;
  2510. for (std::string::const_iterator it = in.begin(),
  2511. end = in.end();
  2512. it != end; ++it)
  2513. {
  2514. switch (*it)
  2515. {
  2516. case '<':
  2517. out << "&lt;";
  2518. break;
  2519. case '>':
  2520. out << "&gt;";
  2521. break;
  2522. case '&':
  2523. out << "&amp;";
  2524. break;
  2525. case '\'':
  2526. out << "&apos;";
  2527. break;
  2528. case '"':
  2529. out << "&quot;";
  2530. break;
  2531. case '\t':
  2532. case '\n':
  2533. case '\r':
  2534. out << *it;
  2535. break;
  2536. default:
  2537. if (*it >= 0 && *it < 20)
  2538. {
  2539. // Do not output control codes
  2540. out << "?";
  2541. }
  2542. else
  2543. {
  2544. out << *it;
  2545. }
  2546. }
  2547. }
  2548. return out.str();
  2549. }
  2550. class LLSDXMLParser::Impl
  2551. {
  2552. protected:
  2553. LOG_CLASS(LLSDXMLParser);
  2554. public:
  2555. Impl(bool emit_errors);
  2556. ~Impl();
  2557. S32 parse(std::istream& input, LLSD& data);
  2558. S32 parseLines(std::istream& input, LLSD& data);
  2559. void parsePart(const char* buf, llssize len);
  2560. void reset();
  2561. private:
  2562. void startElementHandler(const XML_Char* name, const XML_Char** attributes);
  2563. void endElementHandler(const XML_Char* name);
  2564. void characterDataHandler(const XML_Char* data, int length);
  2565. static void sStartElementHandler(void* userData, const XML_Char* name,
  2566. const XML_Char** attributes);
  2567. static void sEndElementHandler(void* userData, const XML_Char* name);
  2568. static void sCharacterDataHandler(void* userData, const XML_Char* data,
  2569. int length);
  2570. void startSkipping();
  2571. enum Element
  2572. {
  2573. ELEMENT_LLSD,
  2574. ELEMENT_UNDEF,
  2575. ELEMENT_BOOL,
  2576. ELEMENT_INTEGER,
  2577. ELEMENT_REAL,
  2578. ELEMENT_STRING,
  2579. ELEMENT_UUID,
  2580. ELEMENT_DATE,
  2581. ELEMENT_URI,
  2582. ELEMENT_BINARY,
  2583. ELEMENT_MAP,
  2584. ELEMENT_ARRAY,
  2585. ELEMENT_KEY,
  2586. ELEMENT_UNKNOWN
  2587. };
  2588. static Element readElement(const XML_Char* name);
  2589. static const XML_Char* findAttribute(const XML_Char* name,
  2590. const XML_Char** pairs);
  2591. private:
  2592. XML_Parser mParser;
  2593. typedef std::deque<LLSD*> stack_queue_t;
  2594. stack_queue_t mStack;
  2595. LLSD mResult;
  2596. std::string mCurrentKey; // Current XML <tag>
  2597. std::string mCurrentContent; // String data between <tag> and </tag>
  2598. S32 mParseCount;
  2599. int mDepth;
  2600. int mSkipThrough;
  2601. bool mSkipping;
  2602. bool mInLLSDElement; // true if we are on LLSD
  2603. bool mGracefullStop; // true if we found the </llsd
  2604. bool mEmitErrors;
  2605. };
  2606. LLSDXMLParser::Impl::Impl(bool emit_errors)
  2607. : mEmitErrors(emit_errors)
  2608. {
  2609. mParser = XML_ParserCreate(NULL);
  2610. reset();
  2611. }
  2612. LLSDXMLParser::Impl::~Impl()
  2613. {
  2614. XML_ParserFree(mParser);
  2615. }
  2616. LL_INLINE bool is_eol(char c)
  2617. {
  2618. return (c == '\n' || c == '\r');
  2619. }
  2620. void clear_eol(std::istream& input)
  2621. {
  2622. char c = input.peek();
  2623. while (input.good() && is_eol(c))
  2624. {
  2625. input.get(c);
  2626. c = input.peek();
  2627. }
  2628. }
  2629. static unsigned get_till_eol(std::istream& input, char* buf, unsigned bufsize)
  2630. {
  2631. unsigned count = 0;
  2632. while (count < bufsize && input.good())
  2633. {
  2634. char c = input.get();
  2635. buf[count++] = c;
  2636. if (is_eol(c))
  2637. {
  2638. break;
  2639. }
  2640. }
  2641. return count;
  2642. }
  2643. S32 LLSDXMLParser::Impl::parse(std::istream& input, LLSD& data)
  2644. {
  2645. XML_Status status;
  2646. constexpr int BUFFER_SIZE = 1024;
  2647. void* buffer = NULL;
  2648. int count = 0;
  2649. while (input.good() && !input.eof())
  2650. {
  2651. buffer = XML_GetBuffer(mParser, BUFFER_SIZE);
  2652. // If we happened to end our last buffer right at the end of the LLSD,
  2653. // but the stream is still going we will get a null buffer here.
  2654. // Check for mGracefullStop.
  2655. if (!buffer)
  2656. {
  2657. break;
  2658. }
  2659. count = get_till_eol(input, (char*)buffer, BUFFER_SIZE);
  2660. if (!count)
  2661. {
  2662. break;
  2663. }
  2664. status = XML_ParseBuffer(mParser, count, false);
  2665. if (status == XML_STATUS_ERROR)
  2666. {
  2667. break;
  2668. }
  2669. }
  2670. // *FIX.: this code is buggy; if the stream was empty or not good, there is
  2671. // no buffer to parse, both the call to XML_ParseBuffer and the buffer
  2672. // manipulations are illegal futhermore, it is not clear that the expat
  2673. // buffer semantics are preserved
  2674. status = XML_ParseBuffer(mParser, 0, true);
  2675. if (status == XML_STATUS_ERROR && !mGracefullStop)
  2676. {
  2677. if (buffer)
  2678. {
  2679. ((char*)buffer)[count ? count - 1 : 0] = '\0';
  2680. if (mEmitErrors)
  2681. {
  2682. llwarns << "XML_STATUS_ERROR parsing: " << (char*)buffer
  2683. << llendl;
  2684. }
  2685. }
  2686. else if (mEmitErrors)
  2687. {
  2688. llwarns << "XML_STATUS_ERROR, NULL buffer" << llendl;
  2689. }
  2690. data = LLSD();
  2691. return LLSDParser::PARSE_FAILURE;
  2692. }
  2693. clear_eol(input);
  2694. data = mResult;
  2695. return mParseCount;
  2696. }
  2697. S32 LLSDXMLParser::Impl::parseLines(std::istream& input, LLSD& data)
  2698. {
  2699. XML_Status status = XML_STATUS_OK;
  2700. data = LLSD();
  2701. constexpr int BUFFER_SIZE = 1024;
  2702. // Must get rid of any leading \n, otherwise the stream gets into an
  2703. // error/eof state
  2704. clear_eol(input);
  2705. while (!mGracefullStop && input.good() && !input.eof())
  2706. {
  2707. void* buffer = XML_GetBuffer(mParser, BUFFER_SIZE);
  2708. // If we happened to end our last buffer right at the end of the LLSD,
  2709. // but the stream is still going we will get a null buffer here.
  2710. // Check for mGracefullStop.
  2711. // I do not think this is actually true - zero 2008-05-09
  2712. if (!buffer)
  2713. {
  2714. break;
  2715. }
  2716. // Get one line
  2717. input.getline((char*)buffer, BUFFER_SIZE);
  2718. std::streamsize num_read = input.gcount();
  2719. if (num_read > 0)
  2720. {
  2721. if (!input.good())
  2722. {
  2723. // Clear state that is set when we run out of buffer
  2724. input.clear();
  2725. }
  2726. // Re-insert with the \n that was absorbed by getline()
  2727. char * text = (char *) buffer;
  2728. if (text[num_read - 1] == 0)
  2729. {
  2730. text[num_read - 1] = '\n';
  2731. }
  2732. }
  2733. status = XML_ParseBuffer(mParser, num_read, false);
  2734. if (status == XML_STATUS_ERROR)
  2735. {
  2736. break;
  2737. }
  2738. }
  2739. if (status != XML_STATUS_ERROR && !mGracefullStop)
  2740. {
  2741. // Parse last bit
  2742. status = XML_ParseBuffer(mParser, 0, true);
  2743. }
  2744. if (status == XML_STATUS_ERROR && !mGracefullStop)
  2745. {
  2746. if (mEmitErrors)
  2747. {
  2748. llwarns << "XML_STATUS_ERROR" << llendl;
  2749. }
  2750. return LLSDParser::PARSE_FAILURE;
  2751. }
  2752. clear_eol(input);
  2753. data = mResult;
  2754. return mParseCount;
  2755. }
  2756. void LLSDXMLParser::Impl::reset()
  2757. {
  2758. mResult.clear();
  2759. mParseCount = 0;
  2760. mInLLSDElement = false;
  2761. mDepth = 0;
  2762. mGracefullStop = false;
  2763. mStack.clear();
  2764. mSkipping = false;
  2765. mCurrentKey.clear();
  2766. XML_ParserReset(mParser, "utf-8");
  2767. XML_SetUserData(mParser, this);
  2768. XML_SetElementHandler(mParser, sStartElementHandler, sEndElementHandler);
  2769. XML_SetCharacterDataHandler(mParser, sCharacterDataHandler);
  2770. }
  2771. void LLSDXMLParser::Impl::startSkipping()
  2772. {
  2773. mSkipping = true;
  2774. mSkipThrough = mDepth;
  2775. }
  2776. const XML_Char* LLSDXMLParser::Impl::findAttribute(const XML_Char* name,
  2777. const XML_Char** pairs)
  2778. {
  2779. while (pairs && *pairs)
  2780. {
  2781. if (strcmp(name, *pairs) == 0)
  2782. {
  2783. return *(pairs + 1);
  2784. }
  2785. pairs += 2;
  2786. }
  2787. return NULL;
  2788. }
  2789. void LLSDXMLParser::Impl::parsePart(const char* buf, llssize len)
  2790. {
  2791. if (buf && len > 0)
  2792. {
  2793. XML_Status status = XML_Parse(mParser, buf, len, false);
  2794. if (status == XML_STATUS_ERROR)
  2795. {
  2796. llinfos << "Unexpected XML parsing error at start" << llendl;
  2797. }
  2798. }
  2799. }
  2800. void LLSDXMLParser::Impl::startElementHandler(const XML_Char* name,
  2801. const XML_Char** attributes)
  2802. {
  2803. ++mDepth;
  2804. if (mSkipping)
  2805. {
  2806. return;
  2807. }
  2808. Element element = readElement(name);
  2809. mCurrentContent.clear();
  2810. switch (element)
  2811. {
  2812. case ELEMENT_LLSD:
  2813. if (mInLLSDElement)
  2814. {
  2815. return startSkipping();
  2816. }
  2817. mInLLSDElement = true;
  2818. return;
  2819. case ELEMENT_KEY:
  2820. if (mStack.empty() || !mStack.back()->isMap())
  2821. {
  2822. return startSkipping();
  2823. }
  2824. return;
  2825. case ELEMENT_BINARY:
  2826. {
  2827. const XML_Char* encoding = findAttribute("encoding", attributes);
  2828. if (encoding && strcmp("base64", encoding) != 0)
  2829. {
  2830. return startSkipping();
  2831. }
  2832. break;
  2833. }
  2834. default:
  2835. // All the rest are values, fall through
  2836. break;
  2837. }
  2838. if (!mInLLSDElement)
  2839. {
  2840. return startSkipping();
  2841. }
  2842. if (mStack.empty())
  2843. {
  2844. mStack.push_back(&mResult);
  2845. }
  2846. else if (mStack.back()->isMap())
  2847. {
  2848. if (mCurrentKey.empty())
  2849. {
  2850. return startSkipping();
  2851. }
  2852. LLSD& map = *mStack.back();
  2853. LLSD& newElement = map[mCurrentKey];
  2854. mStack.push_back(&newElement);
  2855. mCurrentKey.clear();
  2856. }
  2857. else if (mStack.back()->isArray())
  2858. {
  2859. LLSD& array = *mStack.back();
  2860. array.append(LLSD());
  2861. LLSD& newElement = array[array.size() - 1];
  2862. mStack.push_back(&newElement);
  2863. }
  2864. else
  2865. {
  2866. // Improperly nested value in a non-structure
  2867. return startSkipping();
  2868. }
  2869. ++mParseCount;
  2870. switch (element)
  2871. {
  2872. case ELEMENT_MAP:
  2873. *mStack.back() = LLSD::emptyMap();
  2874. break;
  2875. case ELEMENT_ARRAY:
  2876. *mStack.back() = LLSD::emptyArray();
  2877. break;
  2878. default:
  2879. // All the other values will be set in the end element handler
  2880. break;
  2881. }
  2882. }
  2883. void LLSDXMLParser::Impl::endElementHandler(const XML_Char* name)
  2884. {
  2885. --mDepth;
  2886. if (mSkipping)
  2887. {
  2888. if (mDepth < mSkipThrough)
  2889. {
  2890. mSkipping = false;
  2891. }
  2892. return;
  2893. }
  2894. Element element = readElement(name);
  2895. switch (element)
  2896. {
  2897. case ELEMENT_LLSD:
  2898. if (mInLLSDElement)
  2899. {
  2900. mInLLSDElement = false;
  2901. mGracefullStop = true;
  2902. XML_StopParser(mParser, false);
  2903. }
  2904. return;
  2905. case ELEMENT_KEY:
  2906. mCurrentKey = mCurrentContent;
  2907. return;
  2908. default:
  2909. // All rest are values, fall through
  2910. ;
  2911. }
  2912. if (!mInLLSDElement)
  2913. {
  2914. return;
  2915. }
  2916. LLSD& value = *mStack.back();
  2917. mStack.pop_back();
  2918. switch (element)
  2919. {
  2920. case ELEMENT_UNDEF:
  2921. value.clear();
  2922. break;
  2923. case ELEMENT_BOOL:
  2924. value = mCurrentContent == "true" || mCurrentContent == "1";
  2925. break;
  2926. case ELEMENT_INTEGER:
  2927. {
  2928. S32 i;
  2929. if (sscanf(mCurrentContent.c_str(), "%d", &i) == 1)
  2930. {
  2931. // See if sscanf works - it is faster
  2932. value = i;
  2933. }
  2934. else
  2935. {
  2936. value = LLSD(mCurrentContent).asInteger();
  2937. }
  2938. break;
  2939. }
  2940. case ELEMENT_REAL:
  2941. {
  2942. // sscanf() is sensitive to the locale and will fail decoding
  2943. // the decimal point for locales where the decimal separator
  2944. // is a comma... So, better not using sscanf() for this purpose.
  2945. // See http://jira.secondlife.com/browse/EXP-700
  2946. #if 0
  2947. F64 r;
  2948. if (sscanf(mCurrentContent.c_str(), "%lf", &r) == 1)
  2949. {
  2950. // See if sscanf works - it is faster
  2951. value = r;
  2952. }
  2953. else
  2954. {
  2955. value = LLSD(mCurrentContent).asReal();
  2956. }
  2957. #else
  2958. value = LLSD(mCurrentContent).asReal();
  2959. #endif
  2960. break;
  2961. }
  2962. case ELEMENT_STRING:
  2963. value = mCurrentContent;
  2964. break;
  2965. case ELEMENT_UUID:
  2966. value = LLSD(mCurrentContent).asUUID();
  2967. break;
  2968. case ELEMENT_DATE:
  2969. value = LLSD(mCurrentContent).asDate();
  2970. break;
  2971. case ELEMENT_URI:
  2972. value = LLSD(mCurrentContent).asURI();
  2973. break;
  2974. case ELEMENT_BINARY:
  2975. {
  2976. // Fix for white spaces in base64, created by python and other
  2977. // non-linden systems. HB: rewritten to avoid costly regex usage.
  2978. std::string stripped;
  2979. for (size_t i = 0, count = mCurrentContent.size(); i < count; ++i)
  2980. {
  2981. const char& c = mCurrentContent[i];
  2982. if (!isspace(c))
  2983. {
  2984. stripped += c;
  2985. }
  2986. }
  2987. size_t len = LLBase64::decodeLen(stripped.c_str());
  2988. LLSD::Binary buff;
  2989. buff.resize(len);
  2990. len = LLBase64::decode(buff.data(), stripped.c_str());
  2991. buff.resize(len);
  2992. value = buff;
  2993. break;
  2994. }
  2995. case ELEMENT_UNKNOWN:
  2996. value.clear();
  2997. break;
  2998. default:
  2999. // Other values, map and array, have already been set
  3000. break;
  3001. }
  3002. mCurrentContent.clear();
  3003. }
  3004. void LLSDXMLParser::Impl::characterDataHandler(const XML_Char* data,
  3005. int length)
  3006. {
  3007. mCurrentContent.append(data, length);
  3008. }
  3009. void LLSDXMLParser::Impl::sStartElementHandler(void* userData,
  3010. const XML_Char* name,
  3011. const XML_Char** attributes)
  3012. {
  3013. ((LLSDXMLParser::Impl*)userData)->startElementHandler(name, attributes);
  3014. }
  3015. void LLSDXMLParser::Impl::sEndElementHandler(void* userData,
  3016. const XML_Char* name)
  3017. {
  3018. ((LLSDXMLParser::Impl*)userData)->endElementHandler(name);
  3019. }
  3020. void LLSDXMLParser::Impl::sCharacterDataHandler(void* userData,
  3021. const XML_Char* data,
  3022. int length)
  3023. {
  3024. ((LLSDXMLParser::Impl*)userData)->characterDataHandler(data, length);
  3025. }
  3026. /*
  3027. This code is time critical
  3028. This is a sample of tag occurances of text in simstate file with ~8000 objects.
  3029. A tag pair (<key>something</key>) counts is counted as two:
  3030. key - 2680178
  3031. real - 1818362
  3032. integer - 906078
  3033. array - 295682
  3034. map - 191818
  3035. uuid - 177903
  3036. binary - 175748
  3037. string - 53482
  3038. undef - 40353
  3039. boolean - 33874
  3040. llsd - 16332
  3041. uri - 38
  3042. date - 1
  3043. */
  3044. LLSDXMLParser::Impl::Element LLSDXMLParser::Impl::readElement(const XML_Char* name)
  3045. {
  3046. XML_Char c = *name++;
  3047. switch (c)
  3048. {
  3049. case 'k':
  3050. if (strcmp(name, "ey") == 0)
  3051. {
  3052. return ELEMENT_KEY;
  3053. }
  3054. break;
  3055. case 'r':
  3056. if (strcmp(name, "eal") == 0)
  3057. {
  3058. return ELEMENT_REAL;
  3059. }
  3060. break;
  3061. case 'i':
  3062. if (strcmp(name, "nteger") == 0)
  3063. {
  3064. return ELEMENT_INTEGER;
  3065. }
  3066. break;
  3067. case 'a':
  3068. if (strcmp(name, "rray") == 0)
  3069. {
  3070. return ELEMENT_ARRAY;
  3071. }
  3072. break;
  3073. case 'm':
  3074. if (strcmp(name, "ap") == 0)
  3075. {
  3076. return ELEMENT_MAP;
  3077. }
  3078. break;
  3079. case 'u':
  3080. if (strcmp(name, "uid") == 0)
  3081. {
  3082. return ELEMENT_UUID;
  3083. }
  3084. if (strcmp(name, "ndef") == 0)
  3085. {
  3086. return ELEMENT_UNDEF;
  3087. }
  3088. if (strcmp(name, "ri") == 0)
  3089. {
  3090. return ELEMENT_URI;
  3091. }
  3092. break;
  3093. case 'b':
  3094. if (strcmp(name, "inary") == 0)
  3095. {
  3096. return ELEMENT_BINARY;
  3097. }
  3098. if (strcmp(name, "oolean") == 0)
  3099. {
  3100. return ELEMENT_BOOL;
  3101. }
  3102. break;
  3103. case 's':
  3104. if (strcmp(name, "tring") == 0)
  3105. {
  3106. return ELEMENT_STRING;
  3107. }
  3108. break;
  3109. case 'l':
  3110. if (strcmp(name, "lsd") == 0)
  3111. {
  3112. return ELEMENT_LLSD;
  3113. }
  3114. break;
  3115. case 'd':
  3116. if (strcmp(name, "ate") == 0)
  3117. {
  3118. return ELEMENT_DATE;
  3119. }
  3120. break;
  3121. }
  3122. return ELEMENT_UNKNOWN;
  3123. }
  3124. ///////////////////////////////////////////////////////////////////////////////
  3125. // LLSDXMLParser class
  3126. ///////////////////////////////////////////////////////////////////////////////
  3127. LLSDXMLParser::LLSDXMLParser(bool emit_errors)
  3128. : impl(*new Impl(emit_errors))
  3129. {
  3130. }
  3131. LLSDXMLParser::~LLSDXMLParser()
  3132. {
  3133. delete &impl;
  3134. }
  3135. void LLSDXMLParser::parsePart(const char* buf, llssize len)
  3136. {
  3137. impl.parsePart(buf, len);
  3138. }
  3139. //virtual
  3140. S32 LLSDXMLParser::doParse(std::istream& input, LLSD& data, S32) const
  3141. {
  3142. if (mParseLines)
  3143. {
  3144. // Use line-based reading (faster code)
  3145. return impl.parseLines(input, data);
  3146. }
  3147. return impl.parse(input, data);
  3148. }
  3149. //virtual
  3150. void LLSDXMLParser::doReset()
  3151. {
  3152. impl.reset();
  3153. }