XmlBinaryWriter.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. // Uncomment to turn on logging of non-dictionary strings written to binary writers.
  5. // This can help identify element/attribute name/ns that could be written as XmlDictionaryStrings to get better compactness and performance.
  6. // #define LOG_NON_DICTIONARY_WRITES
  7. namespace System.Xml
  8. {
  9. using System.IO;
  10. using System.Runtime;
  11. using System.Runtime.Serialization;
  12. using System.Security;
  13. using System.Text;
  14. public interface IXmlBinaryWriterInitializer
  15. {
  16. void SetOutput(Stream stream, IXmlDictionary dictionary, XmlBinaryWriterSession session, bool ownsStream);
  17. }
  18. class XmlBinaryNodeWriter : XmlStreamNodeWriter
  19. {
  20. IXmlDictionary dictionary;
  21. XmlBinaryWriterSession session;
  22. bool inAttribute;
  23. bool inList;
  24. bool wroteAttributeValue;
  25. AttributeValue attributeValue;
  26. const int maxBytesPerChar = 3;
  27. int textNodeOffset;
  28. public XmlBinaryNodeWriter()
  29. {
  30. // Sanity check on node values
  31. Fx.Assert(XmlBinaryNodeType.MaxAttribute < XmlBinaryNodeType.MinElement &&
  32. XmlBinaryNodeType.MaxElement < XmlBinaryNodeType.MinText &&
  33. (int)XmlBinaryNodeType.MaxText < 256, "NodeTypes enumeration messed up");
  34. }
  35. public void SetOutput(Stream stream, IXmlDictionary dictionary, XmlBinaryWriterSession session, bool ownsStream)
  36. {
  37. this.dictionary = dictionary;
  38. this.session = session;
  39. this.inAttribute = false;
  40. this.inList = false;
  41. this.attributeValue.Clear();
  42. this.textNodeOffset = -1;
  43. SetOutput(stream, ownsStream, null);
  44. }
  45. void WriteNode(XmlBinaryNodeType nodeType)
  46. {
  47. WriteByte((byte)nodeType);
  48. textNodeOffset = -1;
  49. }
  50. void WroteAttributeValue()
  51. {
  52. if (wroteAttributeValue && !inList)
  53. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlOnlySingleValue)));
  54. wroteAttributeValue = true;
  55. }
  56. void WriteTextNode(XmlBinaryNodeType nodeType)
  57. {
  58. if (inAttribute)
  59. WroteAttributeValue();
  60. Fx.Assert(nodeType >= XmlBinaryNodeType.MinText && nodeType <= XmlBinaryNodeType.MaxText && ((byte)nodeType & 1) == 0, "Invalid nodeType");
  61. WriteByte((byte)nodeType);
  62. textNodeOffset = this.BufferOffset - 1;
  63. }
  64. byte[] GetTextNodeBuffer(int size, out int offset)
  65. {
  66. if (inAttribute)
  67. WroteAttributeValue();
  68. byte[] buffer = GetBuffer(size, out offset);
  69. textNodeOffset = offset;
  70. return buffer;
  71. }
  72. void WriteTextNodeWithLength(XmlBinaryNodeType nodeType, int length)
  73. {
  74. Fx.Assert(nodeType == XmlBinaryNodeType.Chars8Text || nodeType == XmlBinaryNodeType.Bytes8Text || nodeType == XmlBinaryNodeType.UnicodeChars8Text, "");
  75. int offset;
  76. byte[] buffer = GetTextNodeBuffer(5, out offset);
  77. if (length < 256)
  78. {
  79. buffer[offset + 0] = (byte)nodeType;
  80. buffer[offset + 1] = (byte)length;
  81. Advance(2);
  82. }
  83. else if (length < 65536)
  84. {
  85. buffer[offset + 0] = (byte)(nodeType + 1 * 2); // WithEndElements interleave
  86. buffer[offset + 1] = (byte)length;
  87. length >>= 8;
  88. buffer[offset + 2] = (byte)length;
  89. Advance(3);
  90. }
  91. else
  92. {
  93. buffer[offset + 0] = (byte)(nodeType + 2 * 2); // WithEndElements interleave
  94. buffer[offset + 1] = (byte)length;
  95. length >>= 8;
  96. buffer[offset + 2] = (byte)length;
  97. length >>= 8;
  98. buffer[offset + 3] = (byte)length;
  99. length >>= 8;
  100. buffer[offset + 4] = (byte)length;
  101. Advance(5);
  102. }
  103. }
  104. void WriteTextNodeWithInt64(XmlBinaryNodeType nodeType, Int64 value)
  105. {
  106. int offset;
  107. byte[] buffer = GetTextNodeBuffer(9, out offset);
  108. buffer[offset + 0] = (byte)nodeType;
  109. buffer[offset + 1] = (byte)value;
  110. value >>= 8;
  111. buffer[offset + 2] = (byte)value;
  112. value >>= 8;
  113. buffer[offset + 3] = (byte)value;
  114. value >>= 8;
  115. buffer[offset + 4] = (byte)value;
  116. value >>= 8;
  117. buffer[offset + 5] = (byte)value;
  118. value >>= 8;
  119. buffer[offset + 6] = (byte)value;
  120. value >>= 8;
  121. buffer[offset + 7] = (byte)value;
  122. value >>= 8;
  123. buffer[offset + 8] = (byte)value;
  124. Advance(9);
  125. }
  126. public override void WriteDeclaration()
  127. {
  128. }
  129. public override void WriteStartElement(string prefix, string localName)
  130. {
  131. if (prefix.Length == 0)
  132. {
  133. WriteNode(XmlBinaryNodeType.ShortElement);
  134. WriteName(localName);
  135. }
  136. else
  137. {
  138. char ch = prefix[0];
  139. if (prefix.Length == 1 && ch >= 'a' && ch <= 'z')
  140. {
  141. WritePrefixNode(XmlBinaryNodeType.PrefixElementA, ch - 'a');
  142. WriteName(localName);
  143. }
  144. else
  145. {
  146. WriteNode(XmlBinaryNodeType.Element);
  147. WriteName(prefix);
  148. WriteName(localName);
  149. }
  150. }
  151. }
  152. void WritePrefixNode(XmlBinaryNodeType nodeType, int ch)
  153. {
  154. WriteNode((XmlBinaryNodeType)((int)nodeType + ch));
  155. }
  156. public override void WriteStartElement(string prefix, XmlDictionaryString localName)
  157. {
  158. int key;
  159. if (!TryGetKey(localName, out key))
  160. {
  161. #if LOG_NON_DICTIONARY_WRITES
  162. XmlBinaryWriter.OnNonDictionaryWrite("WriteStartElement", true, localName.Value, "...");
  163. #endif
  164. WriteStartElement(prefix, localName.Value);
  165. }
  166. else
  167. {
  168. if (prefix.Length == 0)
  169. {
  170. WriteNode(XmlBinaryNodeType.ShortDictionaryElement);
  171. WriteDictionaryString(localName, key);
  172. }
  173. else
  174. {
  175. char ch = prefix[0];
  176. if (prefix.Length == 1 && ch >= 'a' && ch <= 'z')
  177. {
  178. WritePrefixNode(XmlBinaryNodeType.PrefixDictionaryElementA, ch - 'a');
  179. WriteDictionaryString(localName, key);
  180. }
  181. else
  182. {
  183. WriteNode(XmlBinaryNodeType.DictionaryElement);
  184. WriteName(prefix);
  185. WriteDictionaryString(localName, key);
  186. }
  187. }
  188. }
  189. }
  190. public override void WriteEndStartElement(bool isEmpty)
  191. {
  192. if (isEmpty)
  193. {
  194. WriteEndElement();
  195. }
  196. }
  197. public override void WriteEndElement(string prefix, string localName)
  198. {
  199. WriteEndElement();
  200. }
  201. void WriteEndElement()
  202. {
  203. if (textNodeOffset != -1)
  204. {
  205. byte[] buffer = this.StreamBuffer;
  206. XmlBinaryNodeType nodeType = (XmlBinaryNodeType)buffer[textNodeOffset];
  207. Fx.Assert(nodeType >= XmlBinaryNodeType.MinText && nodeType <= XmlBinaryNodeType.MaxText && ((byte)nodeType & 1) == 0, "");
  208. buffer[textNodeOffset] = (byte)(nodeType + 1);
  209. textNodeOffset = -1;
  210. }
  211. else
  212. {
  213. WriteNode(XmlBinaryNodeType.EndElement);
  214. }
  215. }
  216. public override void WriteStartAttribute(string prefix, string localName)
  217. {
  218. if (prefix.Length == 0)
  219. {
  220. WriteNode(XmlBinaryNodeType.ShortAttribute);
  221. WriteName(localName);
  222. }
  223. else
  224. {
  225. char ch = prefix[0];
  226. if (prefix.Length == 1 && ch >= 'a' && ch <= 'z')
  227. {
  228. WritePrefixNode(XmlBinaryNodeType.PrefixAttributeA, ch - 'a');
  229. WriteName(localName);
  230. }
  231. else
  232. {
  233. WriteNode(XmlBinaryNodeType.Attribute);
  234. WriteName(prefix);
  235. WriteName(localName);
  236. }
  237. }
  238. inAttribute = true;
  239. wroteAttributeValue = false;
  240. }
  241. public override void WriteStartAttribute(string prefix, XmlDictionaryString localName)
  242. {
  243. int key;
  244. if (!TryGetKey(localName, out key))
  245. {
  246. #if LOG_NON_DICTIONARY_WRITES
  247. XmlBinaryWriter.OnNonDictionaryWrite("WriteStartAttribute", true, localName.Value, "...");
  248. #endif
  249. WriteStartAttribute(prefix, localName.Value);
  250. }
  251. else
  252. {
  253. if (prefix.Length == 0)
  254. {
  255. WriteNode(XmlBinaryNodeType.ShortDictionaryAttribute);
  256. WriteDictionaryString(localName, key);
  257. }
  258. else
  259. {
  260. char ch = prefix[0];
  261. if (prefix.Length == 1 && ch >= 'a' && ch <= 'z')
  262. {
  263. WritePrefixNode(XmlBinaryNodeType.PrefixDictionaryAttributeA, ch - 'a');
  264. WriteDictionaryString(localName, key);
  265. }
  266. else
  267. {
  268. WriteNode(XmlBinaryNodeType.DictionaryAttribute);
  269. WriteName(prefix);
  270. WriteDictionaryString(localName, key);
  271. }
  272. }
  273. inAttribute = true;
  274. wroteAttributeValue = false;
  275. }
  276. }
  277. public override void WriteEndAttribute()
  278. {
  279. inAttribute = false;
  280. if (!wroteAttributeValue)
  281. {
  282. attributeValue.WriteTo(this);
  283. }
  284. textNodeOffset = -1;
  285. }
  286. public override void WriteXmlnsAttribute(string prefix, string ns)
  287. {
  288. if (prefix.Length == 0)
  289. {
  290. WriteNode(XmlBinaryNodeType.ShortXmlnsAttribute);
  291. WriteName(ns);
  292. }
  293. else
  294. {
  295. WriteNode(XmlBinaryNodeType.XmlnsAttribute);
  296. WriteName(prefix);
  297. WriteName(ns);
  298. }
  299. }
  300. public override void WriteXmlnsAttribute(string prefix, XmlDictionaryString ns)
  301. {
  302. int key;
  303. if (!TryGetKey(ns, out key))
  304. {
  305. WriteXmlnsAttribute(prefix, ns.Value);
  306. }
  307. else
  308. {
  309. if (prefix.Length == 0)
  310. {
  311. WriteNode(XmlBinaryNodeType.ShortDictionaryXmlnsAttribute);
  312. WriteDictionaryString(ns, key);
  313. }
  314. else
  315. {
  316. WriteNode(XmlBinaryNodeType.DictionaryXmlnsAttribute);
  317. WriteName(prefix);
  318. WriteDictionaryString(ns, key);
  319. }
  320. }
  321. }
  322. bool TryGetKey(XmlDictionaryString s, out int key)
  323. {
  324. key = -1;
  325. if (s.Dictionary == dictionary)
  326. {
  327. key = s.Key * 2;
  328. return true;
  329. }
  330. XmlDictionaryString t;
  331. if (dictionary != null && dictionary.TryLookup(s, out t))
  332. {
  333. Fx.Assert(t.Dictionary == dictionary, "");
  334. key = t.Key * 2;
  335. return true;
  336. }
  337. if (session == null)
  338. return false;
  339. int sessionKey;
  340. if (!session.TryLookup(s, out sessionKey))
  341. {
  342. if (!session.TryAdd(s, out sessionKey))
  343. return false;
  344. }
  345. key = sessionKey * 2 + 1;
  346. return true;
  347. }
  348. void WriteDictionaryString(XmlDictionaryString s, int key)
  349. {
  350. WriteMultiByteInt32(key);
  351. }
  352. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  353. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  354. [SecuritySafeCritical]
  355. unsafe void WriteName(string s)
  356. {
  357. int length = s.Length;
  358. if (length == 0)
  359. {
  360. WriteByte(0);
  361. }
  362. else
  363. {
  364. fixed (char* pch = s)
  365. {
  366. UnsafeWriteName(pch, length);
  367. }
  368. }
  369. }
  370. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  371. [SecurityCritical]
  372. unsafe void UnsafeWriteName(char* chars, int charCount)
  373. {
  374. if (charCount < 128 / maxBytesPerChar)
  375. {
  376. // Optimize if we know we can fit the converted string in the buffer
  377. // so we don't have to make a pass to count the bytes
  378. // 1 byte for the length
  379. int offset;
  380. byte[] buffer = GetBuffer(1 + charCount * maxBytesPerChar, out offset);
  381. int length = UnsafeGetUTF8Chars(chars, charCount, buffer, offset + 1);
  382. Fx.Assert(length < 128, "");
  383. buffer[offset] = (byte)length;
  384. Advance(1 + length);
  385. }
  386. else
  387. {
  388. int byteCount = UnsafeGetUTF8Length(chars, charCount);
  389. WriteMultiByteInt32(byteCount);
  390. UnsafeWriteUTF8Chars(chars, charCount);
  391. }
  392. }
  393. void WriteMultiByteInt32(int i)
  394. {
  395. int offset;
  396. byte[] buffer = GetBuffer(5, out offset);
  397. int startOffset = offset;
  398. while ((i & 0xFFFFFF80) != 0)
  399. {
  400. buffer[offset++] = (byte)((i & 0x7F) | 0x80);
  401. i >>= 7;
  402. }
  403. buffer[offset++] = (byte)i;
  404. Advance(offset - startOffset);
  405. }
  406. public override void WriteComment(string value)
  407. {
  408. WriteNode(XmlBinaryNodeType.Comment);
  409. WriteName(value);
  410. }
  411. public override void WriteCData(string value)
  412. {
  413. WriteText(value);
  414. }
  415. void WriteEmptyText()
  416. {
  417. WriteTextNode(XmlBinaryNodeType.EmptyText);
  418. }
  419. public override void WriteBoolText(bool value)
  420. {
  421. if (value)
  422. {
  423. WriteTextNode(XmlBinaryNodeType.TrueText);
  424. }
  425. else
  426. {
  427. WriteTextNode(XmlBinaryNodeType.FalseText);
  428. }
  429. }
  430. public override void WriteInt32Text(int value)
  431. {
  432. if (value >= -128 && value < 128)
  433. {
  434. if (value == 0)
  435. {
  436. WriteTextNode(XmlBinaryNodeType.ZeroText);
  437. }
  438. else if (value == 1)
  439. {
  440. WriteTextNode(XmlBinaryNodeType.OneText);
  441. }
  442. else
  443. {
  444. int offset;
  445. byte[] buffer = GetTextNodeBuffer(2, out offset);
  446. buffer[offset + 0] = (byte)XmlBinaryNodeType.Int8Text;
  447. buffer[offset + 1] = (byte)value;
  448. Advance(2);
  449. }
  450. }
  451. else if (value >= -32768 && value < 32768)
  452. {
  453. int offset;
  454. byte[] buffer = GetTextNodeBuffer(3, out offset);
  455. buffer[offset + 0] = (byte)XmlBinaryNodeType.Int16Text;
  456. buffer[offset + 1] = (byte)value;
  457. value >>= 8;
  458. buffer[offset + 2] = (byte)value;
  459. Advance(3);
  460. }
  461. else
  462. {
  463. int offset;
  464. byte[] buffer = GetTextNodeBuffer(5, out offset);
  465. buffer[offset + 0] = (byte)XmlBinaryNodeType.Int32Text;
  466. buffer[offset + 1] = (byte)value;
  467. value >>= 8;
  468. buffer[offset + 2] = (byte)value;
  469. value >>= 8;
  470. buffer[offset + 3] = (byte)value;
  471. value >>= 8;
  472. buffer[offset + 4] = (byte)value;
  473. Advance(5);
  474. }
  475. }
  476. public override void WriteInt64Text(Int64 value)
  477. {
  478. if (value >= int.MinValue && value <= int.MaxValue)
  479. {
  480. WriteInt32Text((int)value);
  481. }
  482. else
  483. {
  484. WriteTextNodeWithInt64(XmlBinaryNodeType.Int64Text, value);
  485. }
  486. }
  487. public override void WriteUInt64Text(UInt64 value)
  488. {
  489. if (value <= Int64.MaxValue)
  490. {
  491. WriteInt64Text((Int64)value);
  492. }
  493. else
  494. {
  495. WriteTextNodeWithInt64(XmlBinaryNodeType.UInt64Text, (Int64)value);
  496. }
  497. }
  498. void WriteInt64(Int64 value)
  499. {
  500. int offset;
  501. byte[] buffer = GetBuffer(8, out offset);
  502. buffer[offset + 0] = (byte)value;
  503. value >>= 8;
  504. buffer[offset + 1] = (byte)value;
  505. value >>= 8;
  506. buffer[offset + 2] = (byte)value;
  507. value >>= 8;
  508. buffer[offset + 3] = (byte)value;
  509. value >>= 8;
  510. buffer[offset + 4] = (byte)value;
  511. value >>= 8;
  512. buffer[offset + 5] = (byte)value;
  513. value >>= 8;
  514. buffer[offset + 6] = (byte)value;
  515. value >>= 8;
  516. buffer[offset + 7] = (byte)value;
  517. Advance(8);
  518. }
  519. public override void WriteBase64Text(byte[] trailBytes, int trailByteCount, byte[] base64Buffer, int base64Offset, int base64Count)
  520. {
  521. if (inAttribute)
  522. {
  523. attributeValue.WriteBase64Text(trailBytes, trailByteCount, base64Buffer, base64Offset, base64Count);
  524. }
  525. else
  526. {
  527. int length = trailByteCount + base64Count;
  528. if (length > 0)
  529. {
  530. WriteTextNodeWithLength(XmlBinaryNodeType.Bytes8Text, length);
  531. if (trailByteCount > 0)
  532. {
  533. int offset;
  534. byte[] buffer = GetBuffer(trailByteCount, out offset);
  535. for (int i = 0; i < trailByteCount; i++)
  536. buffer[offset + i] = trailBytes[i];
  537. Advance(trailByteCount);
  538. }
  539. if (base64Count > 0)
  540. {
  541. WriteBytes(base64Buffer, base64Offset, base64Count);
  542. }
  543. }
  544. else
  545. {
  546. WriteEmptyText();
  547. }
  548. }
  549. }
  550. public override void WriteText(XmlDictionaryString value)
  551. {
  552. if (inAttribute)
  553. {
  554. attributeValue.WriteText(value);
  555. }
  556. else
  557. {
  558. int key;
  559. if (!TryGetKey(value, out key))
  560. {
  561. WriteText(value.Value);
  562. }
  563. else
  564. {
  565. WriteTextNode(XmlBinaryNodeType.DictionaryText);
  566. WriteDictionaryString(value, key);
  567. }
  568. }
  569. }
  570. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  571. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  572. [SecuritySafeCritical]
  573. unsafe public override void WriteText(string value)
  574. {
  575. if (inAttribute)
  576. {
  577. attributeValue.WriteText(value);
  578. }
  579. else
  580. {
  581. if (value.Length > 0)
  582. {
  583. fixed (char* pch = value)
  584. {
  585. UnsafeWriteText(pch, value.Length);
  586. }
  587. }
  588. else
  589. {
  590. WriteEmptyText();
  591. }
  592. }
  593. }
  594. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  595. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  596. [SecuritySafeCritical]
  597. unsafe public override void WriteText(char[] chars, int offset, int count)
  598. {
  599. if (inAttribute)
  600. {
  601. attributeValue.WriteText(new string(chars, offset, count));
  602. }
  603. else
  604. {
  605. if (count > 0)
  606. {
  607. fixed (char* pch = &chars[offset])
  608. {
  609. UnsafeWriteText(pch, count);
  610. }
  611. }
  612. else
  613. {
  614. WriteEmptyText();
  615. }
  616. }
  617. }
  618. public override void WriteText(byte[] chars, int charOffset, int charCount)
  619. {
  620. WriteTextNodeWithLength(XmlBinaryNodeType.Chars8Text, charCount);
  621. WriteBytes(chars, charOffset, charCount);
  622. }
  623. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  624. [SecurityCritical]
  625. unsafe void UnsafeWriteText(char* chars, int charCount)
  626. {
  627. // Callers should handle zero
  628. Fx.Assert(charCount > 0, "");
  629. if (charCount == 1)
  630. {
  631. char ch = chars[0];
  632. if (ch == '0')
  633. {
  634. WriteTextNode(XmlBinaryNodeType.ZeroText);
  635. return;
  636. }
  637. if (ch == '1')
  638. {
  639. WriteTextNode(XmlBinaryNodeType.OneText);
  640. return;
  641. }
  642. }
  643. if (charCount <= byte.MaxValue / maxBytesPerChar)
  644. {
  645. // Optimize if we know we can fit the converted string in the buffer
  646. // so we don't have to make a pass to count the bytes
  647. int offset;
  648. byte[] buffer = GetBuffer(1 + 1 + charCount * maxBytesPerChar, out offset);
  649. int length = UnsafeGetUTF8Chars(chars, charCount, buffer, offset + 2);
  650. if (length / 2 <= charCount)
  651. {
  652. buffer[offset] = (byte)XmlBinaryNodeType.Chars8Text;
  653. }
  654. else
  655. {
  656. buffer[offset] = (byte)XmlBinaryNodeType.UnicodeChars8Text;
  657. length = UnsafeGetUnicodeChars(chars, charCount, buffer, offset + 2);
  658. }
  659. textNodeOffset = offset;
  660. Fx.Assert(length <= byte.MaxValue, "");
  661. buffer[offset + 1] = (byte)length;
  662. Advance(2 + length);
  663. }
  664. else
  665. {
  666. int byteCount = UnsafeGetUTF8Length(chars, charCount);
  667. if (byteCount / 2 > charCount)
  668. {
  669. WriteTextNodeWithLength(XmlBinaryNodeType.UnicodeChars8Text, charCount * 2);
  670. UnsafeWriteUnicodeChars(chars, charCount);
  671. }
  672. else
  673. {
  674. WriteTextNodeWithLength(XmlBinaryNodeType.Chars8Text, byteCount);
  675. UnsafeWriteUTF8Chars(chars, charCount);
  676. }
  677. }
  678. }
  679. public override void WriteEscapedText(string value)
  680. {
  681. WriteText(value);
  682. }
  683. public override void WriteEscapedText(XmlDictionaryString value)
  684. {
  685. WriteText(value);
  686. }
  687. public override void WriteEscapedText(char[] chars, int offset, int count)
  688. {
  689. WriteText(chars, offset, count);
  690. }
  691. public override void WriteEscapedText(byte[] chars, int offset, int count)
  692. {
  693. WriteText(chars, offset, count);
  694. }
  695. public override void WriteCharEntity(int ch)
  696. {
  697. if (ch > char.MaxValue)
  698. {
  699. SurrogateChar sch = new SurrogateChar(ch);
  700. char[] chars = new char[2] { sch.HighChar, sch.LowChar, };
  701. WriteText(chars, 0, 2);
  702. }
  703. else
  704. {
  705. char[] chars = new char[1] { (char)ch };
  706. WriteText(chars, 0, 1);
  707. }
  708. }
  709. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  710. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  711. [SecuritySafeCritical]
  712. unsafe public override void WriteFloatText(float f)
  713. {
  714. long l;
  715. if (f >= long.MinValue && f <= long.MaxValue && (l = (long)f) == f)
  716. {
  717. WriteInt64Text(l);
  718. }
  719. else
  720. {
  721. int offset;
  722. byte[] buffer = GetTextNodeBuffer(1 + sizeof(float), out offset);
  723. byte* bytes = (byte*)&f;
  724. buffer[offset + 0] = (byte)XmlBinaryNodeType.FloatText;
  725. buffer[offset + 1] = bytes[0];
  726. buffer[offset + 2] = bytes[1];
  727. buffer[offset + 3] = bytes[2];
  728. buffer[offset + 4] = bytes[3];
  729. Advance(1 + sizeof(float));
  730. }
  731. }
  732. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  733. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  734. [SecuritySafeCritical]
  735. unsafe public override void WriteDoubleText(double d)
  736. {
  737. float f;
  738. if (d >= float.MinValue && d <= float.MaxValue && (f = (float)d) == d)
  739. {
  740. WriteFloatText(f);
  741. }
  742. else
  743. {
  744. int offset;
  745. byte[] buffer = GetTextNodeBuffer(1 + sizeof(double), out offset);
  746. byte* bytes = (byte*)&d;
  747. buffer[offset + 0] = (byte)XmlBinaryNodeType.DoubleText;
  748. buffer[offset + 1] = bytes[0];
  749. buffer[offset + 2] = bytes[1];
  750. buffer[offset + 3] = bytes[2];
  751. buffer[offset + 4] = bytes[3];
  752. buffer[offset + 5] = bytes[4];
  753. buffer[offset + 6] = bytes[5];
  754. buffer[offset + 7] = bytes[6];
  755. buffer[offset + 8] = bytes[7];
  756. Advance(1 + sizeof(double));
  757. }
  758. }
  759. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  760. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  761. [SecuritySafeCritical]
  762. unsafe public override void WriteDecimalText(decimal d)
  763. {
  764. int offset;
  765. byte[] buffer = GetTextNodeBuffer(1 + sizeof(decimal), out offset);
  766. byte* bytes = (byte*)&d;
  767. buffer[offset++] = (byte)XmlBinaryNodeType.DecimalText;
  768. for (int i = 0; i < sizeof(decimal); i++)
  769. {
  770. buffer[offset++] = bytes[i];
  771. }
  772. Advance(1 + sizeof(decimal));
  773. }
  774. public override void WriteDateTimeText(DateTime dt)
  775. {
  776. WriteTextNodeWithInt64(XmlBinaryNodeType.DateTimeText, dt.ToBinary());
  777. }
  778. public override void WriteUniqueIdText(UniqueId value)
  779. {
  780. if (value.IsGuid)
  781. {
  782. int offset;
  783. byte[] buffer = GetTextNodeBuffer(17, out offset);
  784. buffer[offset] = (byte)XmlBinaryNodeType.UniqueIdText;
  785. value.TryGetGuid(buffer, offset + 1);
  786. Advance(17);
  787. }
  788. else
  789. {
  790. WriteText(value.ToString());
  791. }
  792. }
  793. public override void WriteGuidText(Guid guid)
  794. {
  795. int offset;
  796. byte[] buffer = GetTextNodeBuffer(17, out offset);
  797. buffer[offset] = (byte)XmlBinaryNodeType.GuidText;
  798. Buffer.BlockCopy(guid.ToByteArray(), 0, buffer, offset + 1, 16);
  799. Advance(17);
  800. }
  801. public override void WriteTimeSpanText(TimeSpan value)
  802. {
  803. WriteTextNodeWithInt64(XmlBinaryNodeType.TimeSpanText, value.Ticks);
  804. }
  805. public override void WriteStartListText()
  806. {
  807. Fx.Assert(!inList, "");
  808. inList = true;
  809. WriteNode(XmlBinaryNodeType.StartListText);
  810. }
  811. public override void WriteListSeparator()
  812. {
  813. }
  814. public override void WriteEndListText()
  815. {
  816. Fx.Assert(inList, "");
  817. inList = false;
  818. wroteAttributeValue = true;
  819. WriteNode(XmlBinaryNodeType.EndListText);
  820. }
  821. public void WriteArrayNode()
  822. {
  823. WriteNode(XmlBinaryNodeType.Array);
  824. }
  825. void WriteArrayInfo(XmlBinaryNodeType nodeType, int count)
  826. {
  827. WriteNode(nodeType);
  828. WriteMultiByteInt32(count);
  829. }
  830. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  831. [SecurityCritical]
  832. unsafe public void UnsafeWriteArray(XmlBinaryNodeType nodeType, int count, byte* array, byte* arrayMax)
  833. {
  834. WriteArrayInfo(nodeType, count);
  835. UnsafeWriteArray(array, (int)(arrayMax - array));
  836. }
  837. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  838. [SecurityCritical]
  839. unsafe void UnsafeWriteArray(byte* array, int byteCount)
  840. {
  841. base.UnsafeWriteBytes(array, byteCount);
  842. }
  843. public void WriteDateTimeArray(DateTime[] array, int offset, int count)
  844. {
  845. WriteArrayInfo(XmlBinaryNodeType.DateTimeTextWithEndElement, count);
  846. for (int i = 0; i < count; i++)
  847. {
  848. WriteInt64(array[offset + i].ToBinary());
  849. }
  850. }
  851. public void WriteGuidArray(Guid[] array, int offset, int count)
  852. {
  853. WriteArrayInfo(XmlBinaryNodeType.GuidTextWithEndElement, count);
  854. for (int i = 0; i < count; i++)
  855. {
  856. byte[] buffer = array[offset + i].ToByteArray();
  857. WriteBytes(buffer, 0, 16);
  858. }
  859. }
  860. public void WriteTimeSpanArray(TimeSpan[] array, int offset, int count)
  861. {
  862. WriteArrayInfo(XmlBinaryNodeType.TimeSpanTextWithEndElement, count);
  863. for (int i = 0; i < count; i++)
  864. {
  865. WriteInt64(array[offset + i].Ticks);
  866. }
  867. }
  868. public override void WriteQualifiedName(string prefix, XmlDictionaryString localName)
  869. {
  870. if (prefix.Length == 0)
  871. {
  872. WriteText(localName);
  873. }
  874. else
  875. {
  876. char ch = prefix[0];
  877. int key;
  878. if (prefix.Length == 1 && (ch >= 'a' && ch <= 'z') && TryGetKey(localName, out key))
  879. {
  880. WriteTextNode(XmlBinaryNodeType.QNameDictionaryText);
  881. WriteByte((byte)(ch - 'a'));
  882. WriteDictionaryString(localName, key);
  883. }
  884. else
  885. {
  886. WriteText(prefix);
  887. WriteText(":");
  888. WriteText(localName);
  889. }
  890. }
  891. }
  892. protected override void FlushBuffer()
  893. {
  894. base.FlushBuffer();
  895. textNodeOffset = -1;
  896. }
  897. public override void Close()
  898. {
  899. base.Close();
  900. attributeValue.Clear();
  901. }
  902. struct AttributeValue
  903. {
  904. string captureText;
  905. XmlDictionaryString captureXText;
  906. MemoryStream captureStream;
  907. public void Clear()
  908. {
  909. captureText = null;
  910. captureXText = null;
  911. captureStream = null;
  912. }
  913. public void WriteText(string s)
  914. {
  915. if (captureStream != null)
  916. {
  917. captureText = XmlConverter.Base64Encoding.GetString(captureStream.GetBuffer(), 0, (int)captureStream.Length);
  918. captureStream = null;
  919. }
  920. if (captureXText != null)
  921. {
  922. captureText = captureXText.Value;
  923. captureXText = null;
  924. }
  925. if (captureText == null || captureText.Length == 0)
  926. {
  927. captureText = s;
  928. }
  929. else
  930. {
  931. captureText += s;
  932. }
  933. }
  934. public void WriteText(XmlDictionaryString s)
  935. {
  936. if (captureText != null || captureStream != null)
  937. {
  938. WriteText(s.Value);
  939. }
  940. else
  941. {
  942. captureXText = s;
  943. }
  944. }
  945. public void WriteBase64Text(byte[] trailBytes, int trailByteCount, byte[] buffer, int offset, int count)
  946. {
  947. if (captureText != null || captureXText != null)
  948. {
  949. if (trailByteCount > 0)
  950. {
  951. WriteText(XmlConverter.Base64Encoding.GetString(trailBytes, 0, trailByteCount));
  952. }
  953. WriteText(XmlConverter.Base64Encoding.GetString(buffer, offset, count));
  954. }
  955. else
  956. {
  957. if (captureStream == null)
  958. captureStream = new MemoryStream();
  959. if (trailByteCount > 0)
  960. captureStream.Write(trailBytes, 0, trailByteCount);
  961. captureStream.Write(buffer, offset, count);
  962. }
  963. }
  964. public void WriteTo(XmlBinaryNodeWriter writer)
  965. {
  966. if (captureText != null)
  967. {
  968. writer.WriteText(captureText);
  969. captureText = null;
  970. }
  971. else if (captureXText != null)
  972. {
  973. writer.WriteText(captureXText);
  974. captureXText = null;
  975. }
  976. else if (captureStream != null)
  977. {
  978. writer.WriteBase64Text(null, 0, captureStream.GetBuffer(), 0, (int)captureStream.Length);
  979. captureStream = null;
  980. }
  981. else
  982. {
  983. writer.WriteEmptyText();
  984. }
  985. }
  986. }
  987. }
  988. class XmlBinaryWriter : XmlBaseWriter, IXmlBinaryWriterInitializer
  989. {
  990. XmlBinaryNodeWriter writer;
  991. char[] chars;
  992. byte[] bytes;
  993. #if LOG_NON_DICTIONARY_WRITES
  994. static readonly bool logCallStackOnNonDictionaryWrites = true;
  995. static readonly bool logWriteStringNonDictionaryWrites = true;
  996. internal static void OnNonDictionaryWrite(string apiName, bool isDictionaryMismatchIssue, string s1, string s2)
  997. {
  998. StringBuilder log = new StringBuilder();
  999. log.AppendFormat("> Text string write in binary: {0}(\"{1}\"", apiName, s1);
  1000. if (s2 != null)
  1001. log.AppendFormat(", \"{0}\"", s2);
  1002. log.AppendLine(")");
  1003. if (isDictionaryMismatchIssue)
  1004. {
  1005. log.AppendLine(" Issue: Dictionary mismatch between XmlDictionaryWriter and XmlDictionaryString");
  1006. }
  1007. if (logCallStackOnNonDictionaryWrites)
  1008. {
  1009. string callStack = new System.Diagnostics.StackTrace().ToString();
  1010. log.AppendLine(" Callstack:");
  1011. log.AppendFormat("{0}", callStack.Substring(callStack.IndexOf(Environment.NewLine) + Environment.NewLine.Length));
  1012. }
  1013. Console.WriteLine("{0}", log);
  1014. }
  1015. public override void WriteStartAttribute(string prefix, string localName, string namespaceUri)
  1016. {
  1017. OnNonDictionaryWrite("WriteStartAttribute", false, localName, namespaceUri);
  1018. base.WriteStartAttribute(prefix, localName, namespaceUri);
  1019. }
  1020. public override void WriteStartElement(string prefix, string localName, string namespaceUri)
  1021. {
  1022. OnNonDictionaryWrite("WriteStartElement", false, localName, namespaceUri);
  1023. base.WriteStartElement(prefix, localName, namespaceUri);
  1024. }
  1025. public override void WriteString(string value)
  1026. {
  1027. if (logWriteStringNonDictionaryWrites &&
  1028. !typeof(XmlBinaryWriter).Assembly.Equals(System.Reflection.Assembly.GetCallingAssembly()))
  1029. OnNonDictionaryWrite("WriteString", false, value, null);
  1030. base.WriteString(value);
  1031. }
  1032. #endif
  1033. public void SetOutput(Stream stream, IXmlDictionary dictionary, XmlBinaryWriterSession session, bool ownsStream)
  1034. {
  1035. if (stream == null)
  1036. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("stream"));
  1037. if (writer == null)
  1038. writer = new XmlBinaryNodeWriter();
  1039. writer.SetOutput(stream, dictionary, session, ownsStream);
  1040. SetOutput(writer);
  1041. }
  1042. protected override XmlSigningNodeWriter CreateSigningNodeWriter()
  1043. {
  1044. return new XmlSigningNodeWriter(false);
  1045. }
  1046. protected override void WriteTextNode(XmlDictionaryReader reader, bool attribute)
  1047. {
  1048. Type type = reader.ValueType;
  1049. if (type == typeof(string))
  1050. {
  1051. XmlDictionaryString value;
  1052. if (reader.TryGetValueAsDictionaryString(out value))
  1053. {
  1054. WriteString(value);
  1055. }
  1056. else
  1057. {
  1058. if (reader.CanReadValueChunk)
  1059. {
  1060. if (chars == null)
  1061. {
  1062. chars = new char[256];
  1063. }
  1064. int count;
  1065. while ((count = reader.ReadValueChunk(chars, 0, chars.Length)) > 0)
  1066. {
  1067. this.WriteChars(chars, 0, count);
  1068. }
  1069. }
  1070. else
  1071. {
  1072. WriteString(reader.Value);
  1073. }
  1074. }
  1075. if (!attribute)
  1076. {
  1077. reader.Read();
  1078. }
  1079. }
  1080. else if (type == typeof(byte[]))
  1081. {
  1082. if (reader.CanReadBinaryContent)
  1083. {
  1084. // Its best to read in buffers that are a multiple of 3 so we don't break base64 boundaries when converting text
  1085. if (bytes == null)
  1086. {
  1087. bytes = new byte[384];
  1088. }
  1089. int count;
  1090. while ((count = reader.ReadValueAsBase64(bytes, 0, bytes.Length)) > 0)
  1091. {
  1092. this.WriteBase64(bytes, 0, count);
  1093. }
  1094. }
  1095. else
  1096. {
  1097. WriteString(reader.Value);
  1098. }
  1099. if (!attribute)
  1100. {
  1101. reader.Read();
  1102. }
  1103. }
  1104. else if (type == typeof(int))
  1105. WriteValue(reader.ReadContentAsInt());
  1106. else if (type == typeof(long))
  1107. WriteValue(reader.ReadContentAsLong());
  1108. else if (type == typeof(bool))
  1109. WriteValue(reader.ReadContentAsBoolean());
  1110. else if (type == typeof(double))
  1111. WriteValue(reader.ReadContentAsDouble());
  1112. else if (type == typeof(DateTime))
  1113. WriteValue(reader.ReadContentAsDateTime());
  1114. else if (type == typeof(float))
  1115. WriteValue(reader.ReadContentAsFloat());
  1116. else if (type == typeof(decimal))
  1117. WriteValue(reader.ReadContentAsDecimal());
  1118. else if (type == typeof(UniqueId))
  1119. WriteValue(reader.ReadContentAsUniqueId());
  1120. else if (type == typeof(Guid))
  1121. WriteValue(reader.ReadContentAsGuid());
  1122. else if (type == typeof(TimeSpan))
  1123. WriteValue(reader.ReadContentAsTimeSpan());
  1124. else
  1125. WriteValue(reader.ReadContentAsObject());
  1126. }
  1127. void WriteStartArray(string prefix, string localName, string namespaceUri, int count)
  1128. {
  1129. StartArray(count);
  1130. writer.WriteArrayNode();
  1131. WriteStartElement(prefix, localName, namespaceUri);
  1132. WriteEndElement();
  1133. }
  1134. void WriteStartArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, int count)
  1135. {
  1136. StartArray(count);
  1137. writer.WriteArrayNode();
  1138. WriteStartElement(prefix, localName, namespaceUri);
  1139. WriteEndElement();
  1140. }
  1141. void WriteEndArray()
  1142. {
  1143. EndArray();
  1144. }
  1145. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  1146. [SecurityCritical]
  1147. unsafe void UnsafeWriteArray(string prefix, string localName, string namespaceUri,
  1148. XmlBinaryNodeType nodeType, int count, byte* array, byte* arrayMax)
  1149. {
  1150. WriteStartArray(prefix, localName, namespaceUri, count);
  1151. writer.UnsafeWriteArray(nodeType, count, array, arrayMax);
  1152. WriteEndArray();
  1153. }
  1154. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  1155. [SecurityCritical]
  1156. unsafe void UnsafeWriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri,
  1157. XmlBinaryNodeType nodeType, int count, byte* array, byte* arrayMax)
  1158. {
  1159. WriteStartArray(prefix, localName, namespaceUri, count);
  1160. writer.UnsafeWriteArray(nodeType, count, array, arrayMax);
  1161. WriteEndArray();
  1162. }
  1163. void CheckArray(Array array, int offset, int count)
  1164. {
  1165. if (array == null)
  1166. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("array"));
  1167. if (offset < 0)
  1168. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
  1169. if (offset > array.Length)
  1170. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.OffsetExceedsBufferSize, array.Length)));
  1171. if (count < 0)
  1172. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
  1173. if (count > array.Length - offset)
  1174. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, array.Length - offset)));
  1175. }
  1176. // bool
  1177. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1178. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1179. [SecuritySafeCritical]
  1180. unsafe public override void WriteArray(string prefix, string localName, string namespaceUri, bool[] array, int offset, int count)
  1181. {
  1182. if (Signing)
  1183. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1184. else
  1185. {
  1186. CheckArray(array, offset, count);
  1187. if (count > 0)
  1188. {
  1189. fixed (bool* items = &array[offset])
  1190. {
  1191. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.BoolTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1192. }
  1193. }
  1194. }
  1195. }
  1196. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1197. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1198. [SecuritySafeCritical]
  1199. unsafe public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, bool[] array, int offset, int count)
  1200. {
  1201. if (Signing)
  1202. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1203. else
  1204. {
  1205. CheckArray(array, offset, count);
  1206. if (count > 0)
  1207. {
  1208. fixed (bool* items = &array[offset])
  1209. {
  1210. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.BoolTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1211. }
  1212. }
  1213. }
  1214. }
  1215. // Int16
  1216. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1217. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1218. [SecuritySafeCritical]
  1219. unsafe public override void WriteArray(string prefix, string localName, string namespaceUri, Int16[] array, int offset, int count)
  1220. {
  1221. if (Signing)
  1222. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1223. else
  1224. {
  1225. CheckArray(array, offset, count);
  1226. if (count > 0)
  1227. {
  1228. fixed (Int16* items = &array[offset])
  1229. {
  1230. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.Int16TextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1231. }
  1232. }
  1233. }
  1234. }
  1235. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1236. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1237. [SecuritySafeCritical]
  1238. unsafe public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int16[] array, int offset, int count)
  1239. {
  1240. if (Signing)
  1241. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1242. else
  1243. {
  1244. CheckArray(array, offset, count);
  1245. if (count > 0)
  1246. {
  1247. fixed (Int16* items = &array[offset])
  1248. {
  1249. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.Int16TextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1250. }
  1251. }
  1252. }
  1253. }
  1254. // Int32
  1255. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1256. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1257. [SecuritySafeCritical]
  1258. unsafe public override void WriteArray(string prefix, string localName, string namespaceUri, Int32[] array, int offset, int count)
  1259. {
  1260. if (Signing)
  1261. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1262. else
  1263. {
  1264. CheckArray(array, offset, count);
  1265. if (count > 0)
  1266. {
  1267. fixed (Int32* items = &array[offset])
  1268. {
  1269. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.Int32TextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1270. }
  1271. }
  1272. }
  1273. }
  1274. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1275. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1276. [SecuritySafeCritical]
  1277. unsafe public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int32[] array, int offset, int count)
  1278. {
  1279. if (Signing)
  1280. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1281. else
  1282. {
  1283. CheckArray(array, offset, count);
  1284. if (count > 0)
  1285. {
  1286. fixed (Int32* items = &array[offset])
  1287. {
  1288. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.Int32TextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1289. }
  1290. }
  1291. }
  1292. }
  1293. // Int64
  1294. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1295. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1296. [SecuritySafeCritical]
  1297. unsafe public override void WriteArray(string prefix, string localName, string namespaceUri, Int64[] array, int offset, int count)
  1298. {
  1299. if (Signing)
  1300. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1301. else
  1302. {
  1303. CheckArray(array, offset, count);
  1304. if (count > 0)
  1305. {
  1306. fixed (Int64* items = &array[offset])
  1307. {
  1308. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.Int64TextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1309. }
  1310. }
  1311. }
  1312. }
  1313. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1314. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1315. [SecuritySafeCritical]
  1316. unsafe public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int64[] array, int offset, int count)
  1317. {
  1318. if (Signing)
  1319. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1320. else
  1321. {
  1322. CheckArray(array, offset, count);
  1323. if (count > 0)
  1324. {
  1325. fixed (Int64* items = &array[offset])
  1326. {
  1327. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.Int64TextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1328. }
  1329. }
  1330. }
  1331. }
  1332. // float
  1333. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1334. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1335. [SecuritySafeCritical]
  1336. unsafe public override void WriteArray(string prefix, string localName, string namespaceUri, float[] array, int offset, int count)
  1337. {
  1338. if (Signing)
  1339. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1340. else
  1341. {
  1342. CheckArray(array, offset, count);
  1343. if (count > 0)
  1344. {
  1345. fixed (float* items = &array[offset])
  1346. {
  1347. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.FloatTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1348. }
  1349. }
  1350. }
  1351. }
  1352. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1353. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1354. [SecuritySafeCritical]
  1355. unsafe public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, float[] array, int offset, int count)
  1356. {
  1357. if (Signing)
  1358. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1359. else
  1360. {
  1361. CheckArray(array, offset, count);
  1362. if (count > 0)
  1363. {
  1364. fixed (float* items = &array[offset])
  1365. {
  1366. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.FloatTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1367. }
  1368. }
  1369. }
  1370. }
  1371. // double
  1372. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1373. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1374. [SecuritySafeCritical]
  1375. unsafe public override void WriteArray(string prefix, string localName, string namespaceUri, double[] array, int offset, int count)
  1376. {
  1377. if (Signing)
  1378. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1379. else
  1380. {
  1381. CheckArray(array, offset, count);
  1382. if (count > 0)
  1383. {
  1384. fixed (double* items = &array[offset])
  1385. {
  1386. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.DoubleTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1387. }
  1388. }
  1389. }
  1390. }
  1391. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1392. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1393. [SecuritySafeCritical]
  1394. unsafe public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, double[] array, int offset, int count)
  1395. {
  1396. if (Signing)
  1397. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1398. else
  1399. {
  1400. CheckArray(array, offset, count);
  1401. if (count > 0)
  1402. {
  1403. fixed (double* items = &array[offset])
  1404. {
  1405. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.DoubleTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1406. }
  1407. }
  1408. }
  1409. }
  1410. // decimal
  1411. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1412. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1413. [SecuritySafeCritical]
  1414. unsafe public override void WriteArray(string prefix, string localName, string namespaceUri, decimal[] array, int offset, int count)
  1415. {
  1416. if (Signing)
  1417. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1418. else
  1419. {
  1420. CheckArray(array, offset, count);
  1421. if (count > 0)
  1422. {
  1423. fixed (decimal* items = &array[offset])
  1424. {
  1425. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.DecimalTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1426. }
  1427. }
  1428. }
  1429. }
  1430. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1431. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1432. [SecuritySafeCritical]
  1433. unsafe public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, decimal[] array, int offset, int count)
  1434. {
  1435. if (Signing)
  1436. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1437. else
  1438. {
  1439. CheckArray(array, offset, count);
  1440. if (count > 0)
  1441. {
  1442. fixed (decimal* items = &array[offset])
  1443. {
  1444. UnsafeWriteArray(prefix, localName, namespaceUri, XmlBinaryNodeType.DecimalTextWithEndElement, count, (byte*)items, (byte*)&items[count]);
  1445. }
  1446. }
  1447. }
  1448. }
  1449. // DateTime
  1450. public override void WriteArray(string prefix, string localName, string namespaceUri, DateTime[] array, int offset, int count)
  1451. {
  1452. if (Signing)
  1453. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1454. else
  1455. {
  1456. CheckArray(array, offset, count);
  1457. if (count > 0)
  1458. {
  1459. WriteStartArray(prefix, localName, namespaceUri, count);
  1460. writer.WriteDateTimeArray(array, offset, count);
  1461. WriteEndArray();
  1462. }
  1463. }
  1464. }
  1465. public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, DateTime[] array, int offset, int count)
  1466. {
  1467. if (Signing)
  1468. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1469. else
  1470. {
  1471. CheckArray(array, offset, count);
  1472. if (count > 0)
  1473. {
  1474. WriteStartArray(prefix, localName, namespaceUri, count);
  1475. writer.WriteDateTimeArray(array, offset, count);
  1476. WriteEndArray();
  1477. }
  1478. }
  1479. }
  1480. // Guid
  1481. public override void WriteArray(string prefix, string localName, string namespaceUri, Guid[] array, int offset, int count)
  1482. {
  1483. if (Signing)
  1484. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1485. else
  1486. {
  1487. CheckArray(array, offset, count);
  1488. if (count > 0)
  1489. {
  1490. WriteStartArray(prefix, localName, namespaceUri, count);
  1491. writer.WriteGuidArray(array, offset, count);
  1492. WriteEndArray();
  1493. }
  1494. }
  1495. }
  1496. public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, Guid[] array, int offset, int count)
  1497. {
  1498. if (Signing)
  1499. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1500. else
  1501. {
  1502. CheckArray(array, offset, count);
  1503. if (count > 0)
  1504. {
  1505. WriteStartArray(prefix, localName, namespaceUri, count);
  1506. writer.WriteGuidArray(array, offset, count);
  1507. WriteEndArray();
  1508. }
  1509. }
  1510. }
  1511. // TimeSpan
  1512. public override void WriteArray(string prefix, string localName, string namespaceUri, TimeSpan[] array, int offset, int count)
  1513. {
  1514. if (Signing)
  1515. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1516. else
  1517. {
  1518. CheckArray(array, offset, count);
  1519. if (count > 0)
  1520. {
  1521. WriteStartArray(prefix, localName, namespaceUri, count);
  1522. writer.WriteTimeSpanArray(array, offset, count);
  1523. WriteEndArray();
  1524. }
  1525. }
  1526. }
  1527. public override void WriteArray(string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri, TimeSpan[] array, int offset, int count)
  1528. {
  1529. if (Signing)
  1530. base.WriteArray(prefix, localName, namespaceUri, array, offset, count);
  1531. else
  1532. {
  1533. CheckArray(array, offset, count);
  1534. if (count > 0)
  1535. {
  1536. WriteStartArray(prefix, localName, namespaceUri, count);
  1537. writer.WriteTimeSpanArray(array, offset, count);
  1538. WriteEndArray();
  1539. }
  1540. }
  1541. }
  1542. }
  1543. }