LLSDxmlEncode.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // a class for low level LLSD encoding into a provided StringBuilder
  28. // for cases where we already need to know the low level detail
  29. // and so using something like OSD or even protbuf is just a pure waste
  30. using System;
  31. using System.Globalization;
  32. using System.Text;
  33. using OpenMetaverse;
  34. namespace OpenSim.Framework
  35. {
  36. public static class LLSDxmlEncode
  37. {
  38. static readonly DateTime depoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  39. public static void AddStart(StringBuilder sb, bool addxmlversion = false)
  40. {
  41. if(addxmlversion)
  42. sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd>"); // legacy llsd xml name still valid
  43. else
  44. sb.Append("<llsd>");
  45. }
  46. // got tired of creating a stringbuilder all the time;
  47. public static StringBuilder Start(int size = 256, bool addxmlversion = false)
  48. {
  49. StringBuilder sb = new StringBuilder(size);
  50. if(addxmlversion)
  51. sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd>"); // legacy llsd xml name still valid
  52. else
  53. sb.Append("<llsd>");
  54. return sb;
  55. }
  56. public static void AddEnd(StringBuilder sb)
  57. {
  58. sb.Append("</llsd>");
  59. }
  60. public static string End(StringBuilder sb)
  61. {
  62. sb.Append("</llsd>");
  63. return sb.ToString();
  64. }
  65. // map == a list of key value pairs
  66. public static void AddMap(StringBuilder sb)
  67. {
  68. sb.Append("<map>");
  69. }
  70. public static void AddEndMap(StringBuilder sb)
  71. {
  72. sb.Append("</map>");
  73. }
  74. public static void AddEmptyMap(StringBuilder sb)
  75. {
  76. sb.Append("<map />");
  77. }
  78. // array == a list values
  79. public static void AddArray(StringBuilder sb)
  80. {
  81. sb.Append("<array>");
  82. }
  83. public static void AddEndArray(StringBuilder sb)
  84. {
  85. sb.Append("</array>");
  86. }
  87. public static void AddEmptyArray(StringBuilder sb)
  88. {
  89. sb.Append("<array />");
  90. }
  91. // undefined or null
  92. public static void AddUnknownElem(StringBuilder sb)
  93. {
  94. sb.Append("<undef />");
  95. }
  96. public static void AddElem(bool e, StringBuilder sb)
  97. {
  98. if(e)
  99. sb.Append("<boolean>1</boolean>");
  100. else
  101. sb.Append("<boolean />");
  102. }
  103. public static void AddElem(byte e, StringBuilder sb)
  104. {
  105. if(e == 0)
  106. sb.Append("<integer />");
  107. else
  108. {
  109. sb.Append("<integer>");
  110. sb.Append(e.ToString());
  111. sb.Append("</integer>");
  112. }
  113. }
  114. public static void AddElem(byte[] e, StringBuilder sb)
  115. {
  116. if(e == null || e.Length == 0)
  117. sb.Append("binary />");
  118. else
  119. {
  120. sb.Append("<binary>"); // encode64 is default
  121. sb.Append(Convert.ToBase64String(e,Base64FormattingOptions.None));
  122. sb.Append("</binary>");
  123. }
  124. }
  125. public static void AddElem(int e, StringBuilder sb)
  126. {
  127. if(e == 0)
  128. sb.Append("<integer />");
  129. else
  130. {
  131. sb.Append("<integer>");
  132. sb.Append(e.ToString());
  133. sb.Append("</integer>");
  134. }
  135. }
  136. public static void AddElem(float e, StringBuilder sb)
  137. {
  138. if(e == 0)
  139. sb.Append("<real />");
  140. else
  141. {
  142. sb.Append("<real>");
  143. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  144. sb.Append("</real>");
  145. }
  146. }
  147. public static void AddElem(Vector2 e, StringBuilder sb)
  148. {
  149. sb.Append("<map><key>x</key>");
  150. if(e.X == 0)
  151. sb.Append("<real /><key>y</key>");
  152. else
  153. {
  154. sb.Append("<real>");
  155. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  156. sb.Append("</real><key>y</key>");
  157. }
  158. if(e.Y == 0)
  159. sb.Append("<real /></map>");
  160. else
  161. {
  162. sb.Append("<real>");
  163. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  164. sb.Append("</real></map>");
  165. }
  166. }
  167. public static void AddElem(Vector3 e, StringBuilder sb)
  168. {
  169. sb.Append("<map>key>x</key>");
  170. if(e.X == 0)
  171. sb.Append("<real /><key>y</key>");
  172. else
  173. {
  174. sb.Append("<real>");
  175. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  176. sb.Append("</real><key>y</key>");
  177. }
  178. if(e.Y == 0)
  179. sb.Append("<real /><key>z</key>");
  180. else
  181. {
  182. sb.Append("<real>");
  183. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  184. sb.Append("</real><key>z</key>");
  185. }
  186. if(e.Z == 0)
  187. sb.Append("<real /></map>");
  188. else
  189. {
  190. sb.Append("<real>");
  191. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  192. sb.Append("</real></map>");
  193. }
  194. }
  195. public static void AddElem(Quaternion e, StringBuilder sb)
  196. {
  197. sb.Append("<map><key>x</key>");
  198. if(e.X == 0)
  199. sb.Append("<real /><key>y</key>");
  200. else
  201. {
  202. sb.Append("<real>");
  203. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  204. sb.Append("</real><key>y</key>");
  205. }
  206. if(e.Y == 0)
  207. sb.Append("<real /><key>z</key>");
  208. else
  209. {
  210. sb.Append("<real>");
  211. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  212. sb.Append("</real><key>z</key>");
  213. }
  214. if(e.Z == 0)
  215. sb.Append("<real /><key>w</key>");
  216. else
  217. {
  218. sb.Append("<real>");
  219. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  220. sb.Append("</real><key>w</key>");
  221. }
  222. if(e.W == 0)
  223. sb.Append("<real /></map>");
  224. else
  225. {
  226. sb.Append("<real>");
  227. sb.Append(e.W.ToString(CultureInfo.InvariantCulture));
  228. sb.Append("</real></map>");
  229. }
  230. }
  231. public static void AddElem(double e, StringBuilder sb)
  232. {
  233. if(e == 0)
  234. sb.Append("<real />");
  235. else
  236. {
  237. sb.Append("<real>");
  238. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  239. sb.Append("</real>");
  240. }
  241. }
  242. public static void AddElem(UUID e, StringBuilder sb)
  243. {
  244. if(e == UUID.Zero)
  245. sb.Append("<uuid />");
  246. else
  247. {
  248. sb.Append("<uuid>");
  249. EscapeToXML(e.ToString(), sb);
  250. sb.Append("</uuid>");
  251. }
  252. }
  253. public static void AddElem(string e, StringBuilder sb)
  254. {
  255. if(String.IsNullOrEmpty(e))
  256. sb.Append("<string />");
  257. else
  258. {
  259. sb.Append("<string>");
  260. EscapeToXML(e, sb);
  261. sb.Append("</string>");
  262. }
  263. }
  264. public static void AddRawElem(string e, StringBuilder sb)
  265. {
  266. if(String.IsNullOrEmpty(e))
  267. sb.Append("<string />");
  268. else
  269. {
  270. sb.Append("<string>");
  271. sb.Append(e);
  272. sb.Append("</string>");
  273. }
  274. }
  275. public static void AddElem(Uri e, StringBuilder sb)
  276. {
  277. if(e == null)
  278. {
  279. sb.Append("<uri />");
  280. return;
  281. }
  282. string s;
  283. if (e.IsAbsoluteUri)
  284. s = e.AbsoluteUri;
  285. else
  286. s = e.ToString();
  287. if(String.IsNullOrEmpty(s))
  288. sb.Append("<uri />");
  289. else
  290. {
  291. sb.Append("<uri>");
  292. sb.Append(s);
  293. sb.Append("</uri>");
  294. }
  295. }
  296. public static void AddElem(DateTime e, StringBuilder sb)
  297. {
  298. DateTime u = e.ToUniversalTime();
  299. if(u == depoch)
  300. {
  301. sb.Append("<date />");
  302. return;
  303. }
  304. string format;
  305. if(u.Hour == 0 && u.Minute == 0 && u.Second == 0)
  306. format = "yyyy-MM-dd";
  307. else if (u.Millisecond > 0)
  308. format = "yyyy-MM-ddTHH:mm:ss.ffZ";
  309. else
  310. format = "yyyy-MM-ddTHH:mm:ssZ";
  311. sb.Append("<date>");
  312. sb.Append(u.ToString(format,CultureInfo.InvariantCulture));
  313. sb.Append("</date>");
  314. }
  315. //************ key value *******************
  316. // assumes name is a valid llsd key
  317. public static void AddMap(string name, StringBuilder sb)
  318. {
  319. sb.Append("<key>");
  320. sb.Append(name);
  321. sb.Append("</key><map>");
  322. }
  323. public static void AddEmptyMap(string name, StringBuilder sb)
  324. {
  325. sb.Append("<key>");
  326. sb.Append(name);
  327. sb.Append("</key><map />");
  328. }
  329. // array == a list values
  330. public static void AddArray(string name, StringBuilder sb)
  331. {
  332. sb.Append("<key>");
  333. sb.Append(name);
  334. sb.Append("</key><array>");
  335. }
  336. public static void AddEmptyArray(string name, StringBuilder sb)
  337. {
  338. sb.Append("<key>");
  339. sb.Append(name);
  340. sb.Append("</key><array />");
  341. }
  342. // undefined or null
  343. public static void AddUnknownElem(string name, StringBuilder sb)
  344. {
  345. sb.Append("<key>");
  346. sb.Append(name);
  347. sb.Append("</key><undef />");
  348. }
  349. public static void AddElem(string name, bool e, StringBuilder sb)
  350. {
  351. sb.Append("<key>");
  352. sb.Append(name);
  353. sb.Append("</key>");
  354. if(e)
  355. sb.Append("<boolean>1</boolean>");
  356. else
  357. sb.Append("<boolean />");
  358. }
  359. public static void AddElem(string name, byte e, StringBuilder sb)
  360. {
  361. sb.Append("<key>");
  362. sb.Append(name);
  363. sb.Append("</key>");
  364. if(e == 0)
  365. sb.Append("<integer />");
  366. else
  367. {
  368. sb.Append("<integer>");
  369. sb.Append(e.ToString());
  370. sb.Append("</integer>");
  371. }
  372. }
  373. public static void AddElem(string name, byte[] e, StringBuilder sb)
  374. {
  375. sb.Append("<key>");
  376. sb.Append(name);
  377. sb.Append("</key>");
  378. if(e == null || e.Length == 0)
  379. sb.Append("binary />");
  380. else
  381. {
  382. sb.Append("<binary>"); // encode64 is default
  383. sb.Append(Convert.ToBase64String(e,Base64FormattingOptions.None));
  384. sb.Append("</binary>");
  385. }
  386. }
  387. public static void AddElem(string name, int e, StringBuilder sb)
  388. {
  389. sb.Append("<key>");
  390. sb.Append(name);
  391. sb.Append("</key>");
  392. if(e == 0)
  393. sb.Append("<integer />");
  394. else
  395. {
  396. sb.Append("<integer>");
  397. sb.Append(e.ToString());
  398. sb.Append("</integer>");
  399. }
  400. }
  401. public static void AddElem(string name, float e, StringBuilder sb)
  402. {
  403. sb.Append("<key>");
  404. sb.Append(name);
  405. sb.Append("</key>");
  406. if(e == 0)
  407. sb.Append("<real />");
  408. else
  409. {
  410. sb.Append("<real>");
  411. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  412. sb.Append("</real>");
  413. }
  414. }
  415. public static void AddElem(string name, Vector2 e, StringBuilder sb)
  416. {
  417. sb.Append("<key>");
  418. sb.Append(name);
  419. sb.Append("</key><map><key>x</key>");
  420. if(e.X == 0)
  421. sb.Append("<real /><key>y</key>");
  422. else
  423. {
  424. sb.Append("<real>");
  425. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  426. sb.Append("</real><key>y</key>");
  427. }
  428. if(e.Y == 0)
  429. sb.Append("<real /></map>");
  430. else
  431. {
  432. sb.Append("<real>");
  433. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  434. sb.Append("</real></map>");
  435. }
  436. }
  437. public static void AddElem(string name, Vector3 e, StringBuilder sb)
  438. {
  439. sb.Append("<key>");
  440. sb.Append(name);
  441. sb.Append("</key><map>key>x</key>");
  442. if(e.X == 0)
  443. sb.Append("<real /><key>y</key>");
  444. else
  445. {
  446. sb.Append("<real>");
  447. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  448. sb.Append("</real><key>y</key>");
  449. }
  450. if(e.Y == 0)
  451. sb.Append("<real /><key>z</key>");
  452. else
  453. {
  454. sb.Append("<real>");
  455. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  456. sb.Append("</real><key>z</key>");
  457. }
  458. if(e.Z == 0)
  459. sb.Append("<real /></map>");
  460. else
  461. {
  462. sb.Append("<real>");
  463. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  464. sb.Append("</real></map>");
  465. }
  466. }
  467. public static void AddElem(string name, Quaternion e, StringBuilder sb)
  468. {
  469. sb.Append("<key>");
  470. sb.Append(name);
  471. sb.Append("</key><map><key>x</key>");
  472. if(e.X == 0)
  473. sb.Append("<real /><key>y</key>");
  474. else
  475. {
  476. sb.Append("<real>");
  477. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  478. sb.Append("</real><key>y</key>");
  479. }
  480. if(e.Y == 0)
  481. sb.Append("<real /><key>z</key>");
  482. else
  483. {
  484. sb.Append("<real>");
  485. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  486. sb.Append("</real><key>z</key>");
  487. }
  488. if(e.Z == 0)
  489. sb.Append("<real /><key>w</key>");
  490. else
  491. {
  492. sb.Append("<real>");
  493. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  494. sb.Append("</real><key>w</key>");
  495. }
  496. if(e.W == 0)
  497. sb.Append("<real /></map>");
  498. else
  499. {
  500. sb.Append("<real>");
  501. sb.Append(e.W.ToString(CultureInfo.InvariantCulture));
  502. sb.Append("</real></map>");
  503. }
  504. }
  505. public static void AddElem(string name, double e, StringBuilder sb)
  506. {
  507. sb.Append("<key>");
  508. sb.Append(name);
  509. sb.Append("</key>");
  510. if(e == 0)
  511. sb.Append("<real />");
  512. else
  513. {
  514. sb.Append("<real>");
  515. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  516. sb.Append("</real>");
  517. }
  518. }
  519. public static void AddElem(string name, UUID e, StringBuilder sb)
  520. {
  521. sb.Append("<key>");
  522. sb.Append(name);
  523. sb.Append("</key>");
  524. if(e == UUID.Zero)
  525. sb.Append("<uuid />");
  526. else
  527. {
  528. sb.Append("<uuid>");
  529. EscapeToXML(e.ToString(), sb);
  530. sb.Append("</uuid>");
  531. }
  532. }
  533. public static void AddElem(string name, string e, StringBuilder sb)
  534. {
  535. sb.Append("<key>");
  536. sb.Append(name);
  537. sb.Append("</key>");
  538. if(String.IsNullOrEmpty(e))
  539. sb.Append("<string />");
  540. else
  541. {
  542. sb.Append("<string>");
  543. EscapeToXML(e.ToString(), sb);
  544. sb.Append("</string>");
  545. }
  546. }
  547. public static void AddRawElem(string name, string e, StringBuilder sb)
  548. {
  549. sb.Append("<key>");
  550. sb.Append(name);
  551. sb.Append("</key>");
  552. if(String.IsNullOrEmpty(e))
  553. sb.Append("<string />");
  554. else
  555. {
  556. sb.Append("<string>");
  557. sb.Append(e);
  558. sb.Append("</string>");
  559. }
  560. }
  561. public static void AddElem(string name, Uri e, StringBuilder sb)
  562. {
  563. sb.Append("<key>");
  564. sb.Append(name);
  565. sb.Append("</key>");
  566. if(e == null)
  567. {
  568. sb.Append("<uri />");
  569. return;
  570. }
  571. string s;
  572. if (e.IsAbsoluteUri)
  573. s = e.AbsoluteUri;
  574. else
  575. s = e.ToString();
  576. if(String.IsNullOrEmpty(s))
  577. sb.Append("<uri />");
  578. else
  579. {
  580. sb.Append("<uri>");
  581. sb.Append(s);
  582. sb.Append("</uri>");
  583. }
  584. }
  585. public static void AddElem(string name, DateTime e, StringBuilder sb)
  586. {
  587. sb.Append("<key>");
  588. sb.Append(name);
  589. sb.Append("</key>");
  590. DateTime u = e.ToUniversalTime();
  591. if(u == depoch)
  592. {
  593. sb.Append("<date />");
  594. return;
  595. }
  596. string format;
  597. if(u.Hour == 0 && u.Minute == 0 && u.Second == 0)
  598. format = "yyyy-MM-dd";
  599. else if (u.Millisecond > 0)
  600. format = "yyyy-MM-ddTHH:mm:ss.ffZ";
  601. else
  602. format = "yyyy-MM-ddTHH:mm:ssZ";
  603. sb.Append("<date>");
  604. sb.Append(u.ToString(format,CultureInfo.InvariantCulture));
  605. sb.Append("</date>");
  606. }
  607. public static void AddLLSD(string e, StringBuilder sb)
  608. {
  609. sb.Append(e);
  610. }
  611. public static void EscapeToXML(string s, StringBuilder sb)
  612. {
  613. int i;
  614. char c;
  615. int len = s.Length;
  616. for (i = 0; i < len; i++)
  617. {
  618. c = s[i];
  619. switch (c)
  620. {
  621. case '<':
  622. sb.Append("&lt;");
  623. break;
  624. case '>':
  625. sb.Append("&gt;");
  626. break;
  627. case '&':
  628. sb.Append("&amp;");
  629. break;
  630. case '"':
  631. sb.Append("&quot;");
  632. break;
  633. case '\\':
  634. sb.Append("&apos;");
  635. break;
  636. default:
  637. sb.Append(c);
  638. break;
  639. }
  640. }
  641. }
  642. }
  643. }