1
0

LLSDxmlEncode.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. public const string LLSDEmpty = "<llsd><map /></llsd>";
  47. // got tired of creating a stringbuilder all the time;
  48. public static StringBuilder Start(int size = 4096, bool addxmlversion = false)
  49. {
  50. StringBuilder sb = osStringBuilderCache.Acquire(size);
  51. if(addxmlversion)
  52. sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd>"); // legacy llsd xml name still valid
  53. else
  54. sb.Append("<llsd>");
  55. return sb;
  56. }
  57. public static void AddEnd(StringBuilder sb)
  58. {
  59. sb.Append("</llsd>");
  60. }
  61. public static string End(StringBuilder sb)
  62. {
  63. sb.Append("</llsd>");
  64. return osStringBuilderCache.GetStringAndRelease(sb);
  65. }
  66. public static byte[] EndToNBBytes(StringBuilder sb)
  67. {
  68. sb.Append("</llsd>");
  69. return Util.UTF8NBGetbytes(osStringBuilderCache.GetStringAndRelease(sb));
  70. }
  71. // map == a list of key value pairs
  72. public static void AddMap(StringBuilder sb)
  73. {
  74. sb.Append("<map>");
  75. }
  76. public static void AddEndMap(StringBuilder sb)
  77. {
  78. sb.Append("</map>");
  79. }
  80. public static void AddEmptyMap(StringBuilder sb)
  81. {
  82. sb.Append("<map />");
  83. }
  84. // array == a list values
  85. public static void AddArray(StringBuilder sb)
  86. {
  87. sb.Append("<array>");
  88. }
  89. public static void AddEndArray(StringBuilder sb)
  90. {
  91. sb.Append("</array>");
  92. }
  93. public static void AddEndMapAndArray(StringBuilder sb)
  94. {
  95. sb.Append("</map></array>");
  96. }
  97. public static void AddEmptyArray(StringBuilder sb)
  98. {
  99. sb.Append("<array />");
  100. }
  101. // undefined or null
  102. public static void AddUnknownElem(StringBuilder sb)
  103. {
  104. sb.Append("<undef />");
  105. }
  106. public static void AddElem(bool e, StringBuilder sb)
  107. {
  108. if(e)
  109. sb.Append("<boolean>1</boolean>");
  110. else
  111. sb.Append("<boolean>0</boolean>");
  112. }
  113. public static void AddElem(byte e, StringBuilder sb)
  114. {
  115. if(e == 0)
  116. sb.Append("<integer />");
  117. else
  118. {
  119. sb.Append("<integer>");
  120. sb.Append(e.ToString());
  121. sb.Append("</integer>");
  122. }
  123. }
  124. public static void AddElem(byte[] e, StringBuilder sb)
  125. {
  126. if(e == null || e.Length == 0)
  127. sb.Append("binary />");
  128. else
  129. {
  130. sb.Append("<binary>"); // encode64 is default
  131. base64Encode(e, sb);
  132. sb.Append("</binary>");
  133. }
  134. }
  135. public static void AddElem(byte[] e, int start, int length, StringBuilder sb)
  136. {
  137. if (start + length >= e.Length)
  138. length = e.Length - start;
  139. if (e == null || e.Length == 0 || length <= 0)
  140. sb.Append("binary />");
  141. else
  142. {
  143. sb.Append("<binary>"); // encode64 is default
  144. base64Encode(e, start, length, sb);
  145. sb.Append("</binary>");
  146. }
  147. }
  148. public static void AddElem(int e, StringBuilder sb)
  149. {
  150. if(e == 0)
  151. sb.Append("<integer />");
  152. else
  153. {
  154. sb.Append("<integer>");
  155. sb.Append(e.ToString());
  156. sb.Append("</integer>");
  157. }
  158. }
  159. public static void AddElem(uint e, StringBuilder sb)
  160. {
  161. AddElem(uintToByteArray(e), sb);
  162. }
  163. public static void AddElem(ulong e, StringBuilder sb)
  164. {
  165. AddElem(ulongToByteArray(e), sb);
  166. }
  167. public static void AddElem(float e, StringBuilder sb)
  168. {
  169. if(e == 0)
  170. sb.Append("<real>0</real>");
  171. else
  172. {
  173. sb.Append("<real>");
  174. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  175. sb.Append("</real>");
  176. }
  177. }
  178. public static void AddElem(Vector2 e, StringBuilder sb)
  179. {
  180. sb.Append("<array>");
  181. if(e.X == 0)
  182. sb.Append("<real>0</real>");
  183. else
  184. {
  185. sb.Append("<real>");
  186. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  187. sb.Append("</real>");
  188. }
  189. if(e.Y == 0)
  190. sb.Append("<real>0</real></array>");
  191. else
  192. {
  193. sb.Append("<real>");
  194. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  195. sb.Append("</real></array>");
  196. }
  197. }
  198. public static void AddElem(Vector3 e, StringBuilder sb)
  199. {
  200. sb.Append("<array>");
  201. if(e.X == 0)
  202. sb.Append("<real>0</real>");
  203. else
  204. {
  205. sb.Append("<real>");
  206. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  207. sb.Append("</real>");
  208. }
  209. if(e.Y == 0)
  210. sb.Append("<real>0</real>");
  211. else
  212. {
  213. sb.Append("<real>");
  214. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  215. sb.Append("</real>");
  216. }
  217. if(e.Z == 0)
  218. sb.Append("<real>0</real></array>");
  219. else
  220. {
  221. sb.Append("<real>");
  222. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  223. sb.Append("</real></array>");
  224. }
  225. }
  226. public static void AddElem(Quaternion e, StringBuilder sb)
  227. {
  228. sb.Append("<array>");
  229. if(e.X == 0)
  230. sb.Append("<real>0</real>");
  231. else
  232. {
  233. sb.Append("<real>");
  234. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  235. sb.Append("</real>");
  236. }
  237. if(e.Y == 0)
  238. sb.Append("<real>0</real>");
  239. else
  240. {
  241. sb.Append("<real>");
  242. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  243. sb.Append("</real>");
  244. }
  245. if(e.Z == 0)
  246. sb.Append("<real>0</real>");
  247. else
  248. {
  249. sb.Append("<real>");
  250. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  251. sb.Append("</real>");
  252. }
  253. if(e.W == 0)
  254. sb.Append("<real>0</real></array>");
  255. else
  256. {
  257. sb.Append("<real>");
  258. sb.Append(e.W.ToString(CultureInfo.InvariantCulture));
  259. sb.Append("</real></array>");
  260. }
  261. }
  262. public static void AddElem(double e, StringBuilder sb)
  263. {
  264. if(e == 0)
  265. sb.Append("<real>0</real>");
  266. else
  267. {
  268. sb.Append("<real>");
  269. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  270. sb.Append("</real>");
  271. }
  272. }
  273. public static void AddElem(UUID e, StringBuilder sb)
  274. {
  275. if(e == UUID.Zero)
  276. sb.Append("<uuid />");
  277. else
  278. {
  279. sb.Append("<uuid>");
  280. EscapeToXML(e.ToString(), sb);
  281. sb.Append("</uuid>");
  282. }
  283. }
  284. public static void AddElem(string e, StringBuilder sb)
  285. {
  286. if(String.IsNullOrEmpty(e))
  287. sb.Append("<string />");
  288. else
  289. {
  290. sb.Append("<string>");
  291. EscapeToXML(e, sb);
  292. sb.Append("</string>");
  293. }
  294. }
  295. public static void AddRawElem(string e, StringBuilder sb)
  296. {
  297. if(String.IsNullOrEmpty(e))
  298. return;
  299. sb.Append(e);
  300. }
  301. public static void AddElem(Uri e, StringBuilder sb)
  302. {
  303. if(e == null)
  304. {
  305. sb.Append("<uri />");
  306. return;
  307. }
  308. string s;
  309. if (e.IsAbsoluteUri)
  310. s = e.AbsoluteUri;
  311. else
  312. s = e.ToString();
  313. if(String.IsNullOrEmpty(s))
  314. sb.Append("<uri />");
  315. else
  316. {
  317. sb.Append("<uri>");
  318. sb.Append(s);
  319. sb.Append("</uri>");
  320. }
  321. }
  322. public static void AddElem(DateTime e, StringBuilder sb)
  323. {
  324. DateTime u = e.ToUniversalTime();
  325. if(u == depoch)
  326. {
  327. sb.Append("<date />");
  328. return;
  329. }
  330. string format;
  331. if(u.Hour == 0 && u.Minute == 0 && u.Second == 0)
  332. format = "yyyy-MM-dd";
  333. else if (u.Millisecond > 0)
  334. format = "yyyy-MM-ddTHH:mm:ss.ffZ";
  335. else
  336. format = "yyyy-MM-ddTHH:mm:ssZ";
  337. sb.Append("<date>");
  338. sb.Append(u.ToString(format,CultureInfo.InvariantCulture));
  339. sb.Append("</date>");
  340. }
  341. //************ key value *******************
  342. // assumes name is a valid llsd key
  343. public static void AddMap(string name, StringBuilder sb)
  344. {
  345. sb.Append("<key>");
  346. sb.Append(name);
  347. sb.Append("</key><map>");
  348. }
  349. public static void AddEmptyMap(string name, StringBuilder sb)
  350. {
  351. sb.Append("<key>");
  352. sb.Append(name);
  353. sb.Append("</key><map />");
  354. }
  355. // array == a list values
  356. public static void AddArray(string name, StringBuilder sb)
  357. {
  358. sb.Append("<key>");
  359. sb.Append(name);
  360. sb.Append("</key><array>");
  361. }
  362. public static void AddArrayAndMap(string name, StringBuilder sb)
  363. {
  364. sb.Append("<key>");
  365. sb.Append(name);
  366. sb.Append("</key><array><map>");
  367. }
  368. public static void AddEmptyArray(string name, StringBuilder sb)
  369. {
  370. sb.Append("<key>");
  371. sb.Append(name);
  372. sb.Append("</key><array />");
  373. }
  374. // undefined or null
  375. public static void AddUnknownElem(string name, StringBuilder sb)
  376. {
  377. sb.Append("<key>");
  378. sb.Append(name);
  379. sb.Append("</key><undef />");
  380. }
  381. public static void AddElem(string name, bool e, StringBuilder sb)
  382. {
  383. sb.Append("<key>");
  384. sb.Append(name);
  385. sb.Append("</key>");
  386. if(e)
  387. sb.Append("<boolean>1</boolean>");
  388. else
  389. sb.Append("<boolean>0</boolean>");
  390. }
  391. public static void AddElem(string name, byte e, StringBuilder sb)
  392. {
  393. sb.Append("<key>");
  394. sb.Append(name);
  395. sb.Append("</key>");
  396. if(e == 0)
  397. sb.Append("<integer />");
  398. else
  399. {
  400. sb.Append("<integer>");
  401. sb.Append(e.ToString());
  402. sb.Append("</integer>");
  403. }
  404. }
  405. public static void AddElem(string name, byte[] e, StringBuilder sb)
  406. {
  407. sb.Append("<key>");
  408. sb.Append(name);
  409. sb.Append("</key>");
  410. if (e == null || e.Length == 0)
  411. sb.Append("binary />");
  412. else
  413. {
  414. sb.Append("<binary>"); // encode64 is default
  415. base64Encode(e, sb);
  416. sb.Append("</binary>");
  417. }
  418. }
  419. public static void AddElem(string name, byte[] e, int start, int length, StringBuilder sb)
  420. {
  421. sb.Append("<key>");
  422. sb.Append(name);
  423. sb.Append("</key>");
  424. if (start + length >= e.Length)
  425. length = e.Length - start;
  426. if (e == null || e.Length == 0 || length <= 0)
  427. sb.Append("binary />");
  428. else
  429. {
  430. sb.Append("<binary>"); // encode64 is default
  431. base64Encode(e, start, length, sb);
  432. sb.Append("</binary>");
  433. }
  434. }
  435. public static void AddElem(string name, int e, StringBuilder sb)
  436. {
  437. sb.Append("<key>");
  438. sb.Append(name);
  439. sb.Append("</key>");
  440. if(e == 0)
  441. sb.Append("<integer />");
  442. else
  443. {
  444. sb.Append("<integer>");
  445. sb.Append(e.ToString());
  446. sb.Append("</integer>");
  447. }
  448. }
  449. public static void AddElem(string name, uint e, StringBuilder sb)
  450. {
  451. AddElem(name, uintToByteArray(e), sb);
  452. }
  453. public static void AddElem(string name, ulong e, StringBuilder sb)
  454. {
  455. AddElem(name, ulongToByteArray(e), sb);
  456. }
  457. public static void AddElem(string name, float e, StringBuilder sb)
  458. {
  459. sb.Append("<key>");
  460. sb.Append(name);
  461. sb.Append("</key>");
  462. if(e == 0)
  463. sb.Append("<real>0</real>");
  464. else
  465. {
  466. sb.Append("<real>");
  467. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  468. sb.Append("</real>");
  469. }
  470. }
  471. public static void AddElem(string name, Vector2 e, StringBuilder sb)
  472. {
  473. sb.Append("<key>");
  474. sb.Append(name);
  475. sb.Append("</key><array>>");
  476. if(e.X == 0)
  477. sb.Append("<real>0</real>");
  478. else
  479. {
  480. sb.Append("<real>");
  481. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  482. sb.Append("</real>");
  483. }
  484. if(e.Y == 0)
  485. sb.Append("<real>0</real></array>");
  486. else
  487. {
  488. sb.Append("<real>");
  489. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  490. sb.Append("</real></array>");
  491. }
  492. }
  493. public static void AddElem(string name, Vector3 e, StringBuilder sb)
  494. {
  495. sb.Append("<key>");
  496. sb.Append(name);
  497. sb.Append("</key><array>");
  498. if(e.X == 0)
  499. sb.Append("<real>0</real>");
  500. else
  501. {
  502. sb.Append("<real>");
  503. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  504. sb.Append("</real>");
  505. }
  506. if(e.Y == 0)
  507. sb.Append("<real>0</real>");
  508. else
  509. {
  510. sb.Append("<real>");
  511. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  512. sb.Append("</real>");
  513. }
  514. if(e.Z == 0)
  515. sb.Append("<real>0</real></array>");
  516. else
  517. {
  518. sb.Append("<real>");
  519. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  520. sb.Append("</real></array>");
  521. }
  522. }
  523. public static void AddElem(string name, Quaternion e, StringBuilder sb)
  524. {
  525. sb.Append("<key>");
  526. sb.Append(name);
  527. sb.Append("</key><array>");
  528. if(e.X == 0)
  529. sb.Append("<real>0</real>");
  530. else
  531. {
  532. sb.Append("<real>");
  533. sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
  534. sb.Append("</real>");
  535. }
  536. if(e.Y == 0)
  537. sb.Append("<real>0</real>");
  538. else
  539. {
  540. sb.Append("<real>");
  541. sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
  542. sb.Append("</real>");
  543. }
  544. if(e.Z == 0)
  545. sb.Append("<real>0</real>");
  546. else
  547. {
  548. sb.Append("<real>");
  549. sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
  550. sb.Append("</real>");
  551. }
  552. if(e.W == 0)
  553. sb.Append("<real>0</real></array>");
  554. else
  555. {
  556. sb.Append("<real>");
  557. sb.Append(e.W.ToString(CultureInfo.InvariantCulture));
  558. sb.Append("</real></array>");
  559. }
  560. }
  561. public static void AddElem(string name, double e, StringBuilder sb)
  562. {
  563. sb.Append("<key>");
  564. sb.Append(name);
  565. sb.Append("</key>");
  566. if(e == 0)
  567. sb.Append("<real>0</real>");
  568. else
  569. {
  570. sb.Append("<real>");
  571. sb.Append(e.ToString(CultureInfo.InvariantCulture));
  572. sb.Append("</real>");
  573. }
  574. }
  575. public static void AddElem(string name, UUID e, StringBuilder sb)
  576. {
  577. sb.Append("<key>");
  578. sb.Append(name);
  579. sb.Append("</key>");
  580. if(e == UUID.Zero)
  581. sb.Append("<uuid />");
  582. else
  583. {
  584. sb.Append("<uuid>");
  585. EscapeToXML(e.ToString(), sb);
  586. sb.Append("</uuid>");
  587. }
  588. }
  589. public static void AddElem(string name, string e, StringBuilder sb)
  590. {
  591. sb.Append("<key>");
  592. sb.Append(name);
  593. sb.Append("</key>");
  594. if(String.IsNullOrEmpty(e))
  595. sb.Append("<string />");
  596. else
  597. {
  598. sb.Append("<string>");
  599. EscapeToXML(e, sb);
  600. sb.Append("</string>");
  601. }
  602. }
  603. public static void AddRawElem(string name, string e, StringBuilder sb)
  604. {
  605. if (String.IsNullOrEmpty(e))
  606. return;
  607. sb.Append("<key>");
  608. sb.Append(name);
  609. sb.Append("</key>");
  610. sb.Append(e);
  611. }
  612. public static void AddElem(string name, Uri e, StringBuilder sb)
  613. {
  614. sb.Append("<key>");
  615. sb.Append(name);
  616. sb.Append("</key>");
  617. if(e == null)
  618. {
  619. sb.Append("<uri />");
  620. return;
  621. }
  622. string s;
  623. if (e.IsAbsoluteUri)
  624. s = e.AbsoluteUri;
  625. else
  626. s = e.ToString();
  627. if(String.IsNullOrEmpty(s))
  628. sb.Append("<uri />");
  629. else
  630. {
  631. sb.Append("<uri>");
  632. sb.Append(s);
  633. sb.Append("</uri>");
  634. }
  635. }
  636. public static void AddElem(string name, DateTime e, StringBuilder sb)
  637. {
  638. sb.Append("<key>");
  639. sb.Append(name);
  640. sb.Append("</key>");
  641. DateTime u = e.ToUniversalTime();
  642. if(u == depoch)
  643. {
  644. sb.Append("<date />");
  645. return;
  646. }
  647. string format;
  648. if(u.Hour == 0 && u.Minute == 0 && u.Second == 0)
  649. format = "yyyy-MM-dd";
  650. else if (u.Millisecond > 0)
  651. format = "yyyy-MM-ddTHH:mm:ss.ffZ";
  652. else
  653. format = "yyyy-MM-ddTHH:mm:ssZ";
  654. sb.Append("<date>");
  655. sb.Append(u.ToString(format,CultureInfo.InvariantCulture));
  656. sb.Append("</date>");
  657. }
  658. public static void AddLLSD(string e, StringBuilder sb)
  659. {
  660. sb.Append(e);
  661. }
  662. public static void AddLLSD(string name, string e, StringBuilder sb)
  663. {
  664. sb.Append("<key>");
  665. sb.Append(name);
  666. sb.Append("</key>");
  667. sb.Append(e);
  668. }
  669. public static void EscapeToXML(string s, StringBuilder sb)
  670. {
  671. char c;
  672. for (int i = 0; i < s.Length; ++i)
  673. {
  674. c = s[i];
  675. switch (c)
  676. {
  677. case '<':
  678. sb.Append("&lt;");
  679. break;
  680. case '>':
  681. sb.Append("&gt;");
  682. break;
  683. case '&':
  684. sb.Append("&amp;");
  685. break;
  686. case '"':
  687. sb.Append("&quot;");
  688. break;
  689. case '\\':
  690. sb.Append("&apos;");
  691. break;
  692. default:
  693. sb.Append(c);
  694. break;
  695. }
  696. }
  697. }
  698. public static byte[] ulongToByteArray(ulong uLongValue)
  699. {
  700. return new byte[8]
  701. {
  702. (byte)(uLongValue >> 56),
  703. (byte)(uLongValue >> 48),
  704. (byte)(uLongValue >> 40),
  705. (byte)(uLongValue >> 32),
  706. (byte)(uLongValue >> 24),
  707. (byte)(uLongValue >> 16),
  708. (byte)(uLongValue >> 8),
  709. (byte)uLongValue
  710. };
  711. }
  712. public static byte[] uintToByteArray(uint value)
  713. {
  714. return new byte[4]
  715. {
  716. (byte)(value >> 24),
  717. (byte)(value >> 16),
  718. (byte)(value >> 8),
  719. (byte)value
  720. };
  721. }
  722. static readonly char[] base64Chars = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
  723. 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
  724. 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
  725. 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
  726. '8','9','+','/'};
  727. public static unsafe void base64Encode(byte[] data, StringBuilder sb)
  728. {
  729. int lenMod3 = data.Length % 3;
  730. int len = data.Length - lenMod3;
  731. fixed (byte* d = data)
  732. {
  733. fixed (char* b64 = base64Chars)
  734. {
  735. int i = 0;
  736. while(i < len)
  737. {
  738. sb.Append(b64[d[i] >> 2]);
  739. sb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
  740. sb.Append(b64[((d[i + 1] & 0x0f) << 2) | ((d[i + 2] & 0xc0) >> 6)]);
  741. sb.Append(b64[d[i + 2] & 0x3f]);
  742. i += 3;
  743. }
  744. switch (lenMod3)
  745. {
  746. case 2:
  747. {
  748. i = len;
  749. sb.Append(b64[d[i] >> 2]);
  750. sb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
  751. sb.Append(b64[((d[i + 1] & 0x0f) << 2)]);
  752. sb.Append('=');
  753. break;
  754. }
  755. case 1:
  756. {
  757. i = len;
  758. sb.Append(b64[d[i] >> 2]);
  759. sb.Append(b64[(d[i] & 0x03) << 4]);
  760. sb.Append("==");
  761. break;
  762. }
  763. }
  764. }
  765. }
  766. }
  767. public static unsafe void base64Encode(byte[] data, int start, int length, StringBuilder sb)
  768. {
  769. int lenMod3 = length % 3;
  770. int len = start + (length - lenMod3);
  771. fixed (byte* d = data)
  772. {
  773. fixed (char* b64 = base64Chars)
  774. {
  775. int i = start;
  776. while(i < len)
  777. {
  778. sb.Append(b64[(d[i] & 0xfc) >> 2]);
  779. sb.Append(b64[((d[i] & 0x03) << 4) | (d[i + 1] >> 4)]);
  780. sb.Append(b64[((d[i + 1] & 0x0f) << 2) | (d[i + 2] >> 6)]);
  781. sb.Append(b64[d[i + 2] & 0x3f]);
  782. i += 3;
  783. }
  784. switch (lenMod3)
  785. {
  786. case 2:
  787. {
  788. i = len;
  789. sb.Append(b64[d[i] >> 2]);
  790. sb.Append(b64[((d[i] & 0x03) << 4) | (d[i + 1] >> 4)]);
  791. sb.Append(b64[((d[i + 1] & 0x0f) << 2)]);
  792. sb.Append('=');
  793. break;
  794. }
  795. case 1:
  796. {
  797. i = len;
  798. sb.Append(b64[d[i] >> 2]);
  799. sb.Append(b64[(d[i] & 0x03) << 4]);
  800. sb.Append("==");
  801. break;
  802. }
  803. }
  804. }
  805. }
  806. }
  807. }
  808. }