XmlBufferReader.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Xml
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Runtime;
  11. using System.Runtime.Serialization;
  12. using System.Security;
  13. using System.Text;
  14. class XmlBufferReader
  15. {
  16. XmlDictionaryReader reader;
  17. Stream stream;
  18. byte[] streamBuffer;
  19. byte[] buffer;
  20. int offsetMin;
  21. int offsetMax;
  22. IXmlDictionary dictionary;
  23. XmlBinaryReaderSession session;
  24. byte[] guid;
  25. int offset;
  26. const int maxBytesPerChar = 3;
  27. char[] chars;
  28. int windowOffset;
  29. int windowOffsetMax;
  30. ValueHandle listValue;
  31. static byte[] emptyByteArray = new byte[0];
  32. static XmlBufferReader empty = new XmlBufferReader(emptyByteArray);
  33. public XmlBufferReader(XmlDictionaryReader reader)
  34. {
  35. this.reader = reader;
  36. }
  37. public XmlBufferReader(byte[] buffer)
  38. {
  39. this.reader = null;
  40. this.buffer = buffer;
  41. }
  42. static public XmlBufferReader Empty
  43. {
  44. get
  45. {
  46. return empty;
  47. }
  48. }
  49. public byte[] Buffer
  50. {
  51. get
  52. {
  53. return buffer;
  54. }
  55. }
  56. public bool IsStreamed
  57. {
  58. get
  59. {
  60. return stream != null;
  61. }
  62. }
  63. public void SetBuffer(Stream stream, IXmlDictionary dictionary, XmlBinaryReaderSession session)
  64. {
  65. if (streamBuffer == null)
  66. {
  67. streamBuffer = new byte[128];
  68. }
  69. SetBuffer(stream, streamBuffer, 0, 0, dictionary, session);
  70. this.windowOffset = 0;
  71. this.windowOffsetMax = streamBuffer.Length;
  72. }
  73. public void SetBuffer(byte[] buffer, int offset, int count, IXmlDictionary dictionary, XmlBinaryReaderSession session)
  74. {
  75. SetBuffer(null, buffer, offset, count, dictionary, session);
  76. }
  77. void SetBuffer(Stream stream, byte[] buffer, int offset, int count, IXmlDictionary dictionary, XmlBinaryReaderSession session)
  78. {
  79. this.stream = stream;
  80. this.buffer = buffer;
  81. this.offsetMin = offset;
  82. this.offset = offset;
  83. this.offsetMax = offset + count;
  84. this.dictionary = dictionary;
  85. this.session = session;
  86. }
  87. public void Close()
  88. {
  89. if (streamBuffer != null && streamBuffer.Length > 4096)
  90. {
  91. streamBuffer = null;
  92. }
  93. if (stream != null)
  94. {
  95. stream.Close();
  96. this.stream = null;
  97. }
  98. this.buffer = emptyByteArray;
  99. this.offset = 0;
  100. this.offsetMax = 0;
  101. this.windowOffset = 0;
  102. this.windowOffsetMax = 0;
  103. this.dictionary = null;
  104. this.session = null;
  105. }
  106. public bool EndOfFile
  107. {
  108. get
  109. {
  110. return offset == offsetMax && !TryEnsureByte();
  111. }
  112. }
  113. public byte GetByte()
  114. {
  115. int offset = this.offset;
  116. if (offset < offsetMax)
  117. return buffer[offset];
  118. else
  119. return GetByteHard();
  120. }
  121. public void SkipByte()
  122. {
  123. Advance(1);
  124. }
  125. byte GetByteHard()
  126. {
  127. EnsureByte();
  128. return buffer[offset];
  129. }
  130. public byte[] GetBuffer(int count, out int offset)
  131. {
  132. offset = this.offset;
  133. if (offset <= this.offsetMax - count)
  134. return buffer;
  135. return GetBufferHard(count, out offset);
  136. }
  137. public byte[] GetBuffer(int count, out int offset, out int offsetMax)
  138. {
  139. offset = this.offset;
  140. if (offset <= this.offsetMax - count)
  141. {
  142. offsetMax = this.offset + count;
  143. }
  144. else
  145. {
  146. TryEnsureBytes(Math.Min(count, windowOffsetMax - offset));
  147. offsetMax = this.offsetMax;
  148. }
  149. return buffer;
  150. }
  151. public byte[] GetBuffer(out int offset, out int offsetMax)
  152. {
  153. offset = this.offset;
  154. offsetMax = this.offsetMax;
  155. return buffer;
  156. }
  157. byte[] GetBufferHard(int count, out int offset)
  158. {
  159. offset = this.offset;
  160. EnsureBytes(count);
  161. return buffer;
  162. }
  163. void EnsureByte()
  164. {
  165. if (!TryEnsureByte())
  166. XmlExceptionHelper.ThrowUnexpectedEndOfFile(reader);
  167. }
  168. bool TryEnsureByte()
  169. {
  170. if (stream == null)
  171. return false;
  172. if (offsetMax >= windowOffsetMax)
  173. XmlExceptionHelper.ThrowMaxBytesPerReadExceeded(reader, windowOffsetMax - windowOffset);
  174. if (offsetMax >= buffer.Length)
  175. return TryEnsureBytes(1);
  176. int b = stream.ReadByte();
  177. if (b == -1)
  178. return false;
  179. buffer[offsetMax++] = (byte)b;
  180. return true;
  181. }
  182. void EnsureBytes(int count)
  183. {
  184. if (!TryEnsureBytes(count))
  185. XmlExceptionHelper.ThrowUnexpectedEndOfFile(reader);
  186. }
  187. bool TryEnsureBytes(int count)
  188. {
  189. if (stream == null)
  190. return false;
  191. if (offset > int.MaxValue - count)
  192. XmlExceptionHelper.ThrowMaxBytesPerReadExceeded(reader, windowOffsetMax - windowOffset);
  193. int newOffsetMax = offset + count;
  194. if (newOffsetMax < offsetMax)
  195. return true;
  196. if (newOffsetMax > windowOffsetMax)
  197. XmlExceptionHelper.ThrowMaxBytesPerReadExceeded(reader, windowOffsetMax - windowOffset);
  198. if (newOffsetMax > buffer.Length)
  199. {
  200. byte[] newBuffer = new byte[Math.Max(newOffsetMax, buffer.Length * 2)];
  201. System.Buffer.BlockCopy(this.buffer, 0, newBuffer, 0, offsetMax);
  202. buffer = newBuffer;
  203. streamBuffer = newBuffer;
  204. }
  205. int needed = newOffsetMax - offsetMax;
  206. while (needed > 0)
  207. {
  208. int actual = stream.Read(buffer, offsetMax, needed);
  209. if (actual == 0)
  210. return false;
  211. offsetMax += actual;
  212. needed -= actual;
  213. }
  214. return true;
  215. }
  216. #if NO
  217. void ReadByte(byte b)
  218. {
  219. if (BufferReader.GetByte() != b)
  220. XmlExceptionHelper.ThrowTokenExpected(this, ((char)b).ToString(), (char)BufferReader.GetByte());
  221. }
  222. #endif
  223. public void Advance(int count)
  224. {
  225. Fx.Assert(this.offset + count <= offsetMax, "");
  226. this.offset += count;
  227. }
  228. public void InsertBytes(byte[] buffer, int offset, int count)
  229. {
  230. Fx.Assert(stream != null, "");
  231. if (offsetMax > buffer.Length - count)
  232. {
  233. byte[] newBuffer = new byte[offsetMax + count];
  234. System.Buffer.BlockCopy(this.buffer, 0, newBuffer, 0, this.offsetMax);
  235. this.buffer = newBuffer;
  236. this.streamBuffer = newBuffer;
  237. }
  238. System.Buffer.BlockCopy(this.buffer, this.offset, this.buffer, this.offset + count, this.offsetMax - this.offset);
  239. offsetMax += count;
  240. System.Buffer.BlockCopy(buffer, offset, this.buffer, this.offset, count);
  241. }
  242. public void SetWindow(int windowOffset, int windowLength)
  243. {
  244. // [0...elementOffset-1][elementOffset..offset][offset..offsetMax-1][offsetMax..buffer.Length]
  245. // ^--Elements, Attributes in scope
  246. // ^-- The node just consumed
  247. // ^--Data buffered, not consumed
  248. // ^--Unused space
  249. if (windowOffset > int.MaxValue - windowLength)
  250. windowLength = int.MaxValue - windowOffset;
  251. if (offset != windowOffset)
  252. {
  253. System.Buffer.BlockCopy(buffer, offset, buffer, windowOffset, offsetMax - offset);
  254. offsetMax = windowOffset + (offsetMax - offset);
  255. offset = windowOffset;
  256. }
  257. this.windowOffset = windowOffset;
  258. this.windowOffsetMax = Math.Max(windowOffset + windowLength, offsetMax);
  259. }
  260. public int Offset
  261. {
  262. get
  263. {
  264. return offset;
  265. }
  266. set
  267. {
  268. Fx.Assert(value >= offsetMin && value <= offsetMax, "");
  269. this.offset = value;
  270. }
  271. }
  272. public int ReadBytes(int count)
  273. {
  274. Fx.Assert(count >= 0, "");
  275. int offset = this.offset;
  276. if (offset > offsetMax - count)
  277. EnsureBytes(count);
  278. this.offset += count;
  279. return offset;
  280. }
  281. public int ReadMultiByteUInt31()
  282. {
  283. int i = GetByte();
  284. Advance(1);
  285. if ((i & 0x80) == 0)
  286. return i;
  287. i &= 0x7F;
  288. int j = GetByte();
  289. Advance(1);
  290. i |= ((j & 0x7F) << 7);
  291. if ((j & 0x80) == 0)
  292. return i;
  293. int k = GetByte();
  294. Advance(1);
  295. i |= ((k & 0x7F) << 14);
  296. if ((k & 0x80) == 0)
  297. return i;
  298. int l = GetByte();
  299. Advance(1);
  300. i |= ((l & 0x7F) << 21);
  301. if ((l & 0x80) == 0)
  302. return i;
  303. int m = GetByte();
  304. Advance(1);
  305. i |= (m << 28);
  306. if ((m & 0xF8) != 0)
  307. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  308. return i;
  309. }
  310. public int ReadUInt8()
  311. {
  312. byte b = GetByte();
  313. Advance(1);
  314. return b;
  315. }
  316. public int ReadInt8()
  317. {
  318. return (sbyte)ReadUInt8();
  319. }
  320. public int ReadUInt16()
  321. {
  322. int offset;
  323. byte[] buffer = GetBuffer(2, out offset);
  324. int i = buffer[offset + 0] + (buffer[offset + 1] << 8);
  325. Advance(2);
  326. return i;
  327. }
  328. public int ReadInt16()
  329. {
  330. return (Int16)ReadUInt16();
  331. }
  332. public int ReadInt32()
  333. {
  334. int offset;
  335. byte[] buffer = GetBuffer(4, out offset);
  336. byte b1 = buffer[offset + 0];
  337. byte b2 = buffer[offset + 1];
  338. byte b3 = buffer[offset + 2];
  339. byte b4 = buffer[offset + 3];
  340. Advance(4);
  341. return (((((b4 << 8) + b3) << 8) + b2) << 8) + b1;
  342. }
  343. public int ReadUInt31()
  344. {
  345. int i = ReadInt32();
  346. if (i < 0)
  347. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  348. return i;
  349. }
  350. public long ReadInt64()
  351. {
  352. Int64 lo = (UInt32)ReadInt32();
  353. Int64 hi = (UInt32)ReadInt32();
  354. return (hi << 32) + lo;
  355. }
  356. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  357. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  358. [SecuritySafeCritical]
  359. unsafe public float ReadSingle()
  360. {
  361. int offset;
  362. byte[] buffer = GetBuffer(ValueHandleLength.Single, out offset);
  363. float value;
  364. byte* pb = (byte*)&value;
  365. Fx.Assert(sizeof(float) == 4, "");
  366. pb[0] = buffer[offset + 0];
  367. pb[1] = buffer[offset + 1];
  368. pb[2] = buffer[offset + 2];
  369. pb[3] = buffer[offset + 3];
  370. Advance(ValueHandleLength.Single);
  371. return value;
  372. }
  373. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  374. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  375. [SecuritySafeCritical]
  376. unsafe public double ReadDouble()
  377. {
  378. int offset;
  379. byte[] buffer = GetBuffer(ValueHandleLength.Double, out offset);
  380. double value;
  381. byte* pb = (byte*)&value;
  382. Fx.Assert(sizeof(double) == 8, "");
  383. pb[0] = buffer[offset + 0];
  384. pb[1] = buffer[offset + 1];
  385. pb[2] = buffer[offset + 2];
  386. pb[3] = buffer[offset + 3];
  387. pb[4] = buffer[offset + 4];
  388. pb[5] = buffer[offset + 5];
  389. pb[6] = buffer[offset + 6];
  390. pb[7] = buffer[offset + 7];
  391. Advance(ValueHandleLength.Double);
  392. return value;
  393. }
  394. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  395. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  396. [SecuritySafeCritical]
  397. unsafe public decimal ReadDecimal()
  398. {
  399. const int SignMask = unchecked((int)0x80000000);
  400. const int ScaleMask = 0x00FF0000;
  401. int offset;
  402. byte[] buffer = GetBuffer(ValueHandleLength.Decimal, out offset);
  403. byte b1 = buffer[offset + 0];
  404. byte b2 = buffer[offset + 1];
  405. byte b3 = buffer[offset + 2];
  406. byte b4 = buffer[offset + 3];
  407. int flags = (((((b4 << 8) + b3) << 8) + b2) << 8) + b1;
  408. //this logic mirrors the code in Decimal(int []) ctor.
  409. if ((flags & ~(SignMask | ScaleMask)) == 0 && (flags & ScaleMask) <= (28 << 16))
  410. {
  411. decimal value;
  412. byte* pb = (byte*)&value;
  413. for (int i = 0; i < sizeof(decimal); i++)
  414. pb[i] = buffer[offset + i];
  415. Advance(ValueHandleLength.Decimal);
  416. return value;
  417. }
  418. else
  419. {
  420. XmlExceptionHelper.ThrowInvalidBinaryFormat(this.reader);
  421. }
  422. //compiler doesn't know that XmlExceptionHelper.ThrowInvalidBinaryFormat always throws,
  423. //so we have to have a return statement here even though we shouldn't hit this code path...
  424. Fx.Assert("A decimal value should have been returned or an exception should have been thrown.");
  425. return default(decimal);
  426. }
  427. public UniqueId ReadUniqueId()
  428. {
  429. int offset;
  430. byte[] buffer = GetBuffer(ValueHandleLength.UniqueId, out offset);
  431. UniqueId uniqueId = new UniqueId(buffer, offset);
  432. Advance(ValueHandleLength.UniqueId);
  433. return uniqueId;
  434. }
  435. public DateTime ReadDateTime()
  436. {
  437. long value = 0;
  438. try
  439. {
  440. value = ReadInt64();
  441. return DateTime.FromBinary(value);
  442. }
  443. catch (ArgumentException exception)
  444. {
  445. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTime", exception));
  446. }
  447. catch (FormatException exception)
  448. {
  449. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTime", exception));
  450. }
  451. catch (OverflowException exception)
  452. {
  453. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTime", exception));
  454. }
  455. }
  456. public TimeSpan ReadTimeSpan()
  457. {
  458. long value = 0;
  459. try
  460. {
  461. value = ReadInt64();
  462. return TimeSpan.FromTicks(value);
  463. }
  464. catch (ArgumentException exception)
  465. {
  466. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "TimeSpan", exception));
  467. }
  468. catch (FormatException exception)
  469. {
  470. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "TimeSpan", exception));
  471. }
  472. catch (OverflowException exception)
  473. {
  474. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "TimeSpan", exception));
  475. }
  476. }
  477. public Guid ReadGuid()
  478. {
  479. int offset;
  480. byte[] buffer = GetBuffer(ValueHandleLength.Guid, out offset);
  481. Guid guid = GetGuid(offset);
  482. Advance(ValueHandleLength.Guid);
  483. return guid;
  484. }
  485. public string ReadUTF8String(int length)
  486. {
  487. int offset;
  488. byte[] buffer = GetBuffer(length, out offset);
  489. char[] chars = GetCharBuffer(length);
  490. int charCount = GetChars(offset, length, chars);
  491. string value = new string(chars, 0, charCount);
  492. Advance(length);
  493. return value;
  494. }
  495. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  496. [SecurityCritical]
  497. unsafe public void UnsafeReadArray(byte* dst, byte* dstMax)
  498. {
  499. UnsafeReadArray(dst, (int)(dstMax - dst));
  500. }
  501. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code. Caller needs to validate arguments.")]
  502. [SecurityCritical]
  503. unsafe void UnsafeReadArray(byte* dst, int length)
  504. {
  505. if (stream != null)
  506. {
  507. const int chunk = 256;
  508. while (length >= chunk)
  509. {
  510. byte[] _buffer = GetBuffer(chunk, out offset);
  511. for (int i = 0; i < chunk; i++)
  512. {
  513. *dst++ = _buffer[offset + i];
  514. }
  515. Advance(chunk);
  516. length -= chunk;
  517. }
  518. }
  519. if (length > 0)
  520. {
  521. byte[] buffer = GetBuffer(length, out offset);
  522. fixed (byte* _src = &buffer[offset])
  523. {
  524. byte* src = _src;
  525. byte* dstMax = dst + length;
  526. while (dst < dstMax)
  527. {
  528. *dst = *src;
  529. dst++;
  530. src++;
  531. }
  532. }
  533. Advance(length);
  534. }
  535. }
  536. char[] GetCharBuffer(int count)
  537. {
  538. if (count > 1024)
  539. return new char[count];
  540. if (chars == null || chars.Length < count)
  541. chars = new char[count];
  542. return chars;
  543. }
  544. int GetChars(int offset, int length, char[] chars)
  545. {
  546. byte[] buffer = this.buffer;
  547. for (int i = 0; i < length; i++)
  548. {
  549. byte b = buffer[offset + i];
  550. if (b >= 0x80)
  551. return i + XmlConverter.ToChars(buffer, offset + i, length - i, chars, i);
  552. chars[i] = (char)b;
  553. }
  554. return length;
  555. }
  556. int GetChars(int offset, int length, char[] chars, int charOffset)
  557. {
  558. byte[] buffer = this.buffer;
  559. for (int i = 0; i < length; i++)
  560. {
  561. byte b = buffer[offset + i];
  562. if (b >= 0x80)
  563. return i + XmlConverter.ToChars(buffer, offset + i, length - i, chars, charOffset + i);
  564. chars[charOffset + i] = (char)b;
  565. }
  566. return length;
  567. }
  568. public string GetString(int offset, int length)
  569. {
  570. char[] chars = GetCharBuffer(length);
  571. int charCount = GetChars(offset, length, chars);
  572. return new string(chars, 0, charCount);
  573. }
  574. public string GetUnicodeString(int offset, int length)
  575. {
  576. return XmlConverter.ToStringUnicode(buffer, offset, length);
  577. }
  578. public string GetString(int offset, int length, XmlNameTable nameTable)
  579. {
  580. char[] chars = GetCharBuffer(length);
  581. int charCount = GetChars(offset, length, chars);
  582. return nameTable.Add(chars, 0, charCount);
  583. }
  584. public int GetEscapedChars(int offset, int length, char[] chars)
  585. {
  586. byte[] buffer = this.buffer;
  587. int charCount = 0;
  588. int textOffset = offset;
  589. int offsetMax = offset + length;
  590. while (true)
  591. {
  592. while (offset < offsetMax && IsAttrChar(buffer[offset]))
  593. offset++;
  594. charCount += GetChars(textOffset, offset - textOffset, chars, charCount);
  595. if (offset == offsetMax)
  596. break;
  597. textOffset = offset;
  598. if (buffer[offset] == '&')
  599. {
  600. while (offset < offsetMax && buffer[offset] != ';')
  601. offset++;
  602. offset++;
  603. int ch = GetCharEntity(textOffset, offset - textOffset);
  604. textOffset = offset;
  605. if (ch > char.MaxValue)
  606. {
  607. SurrogateChar surrogate = new SurrogateChar(ch);
  608. chars[charCount++] = surrogate.HighChar;
  609. chars[charCount++] = surrogate.LowChar;
  610. }
  611. else
  612. {
  613. chars[charCount++] = (char)ch;
  614. }
  615. }
  616. else if (buffer[offset] == '\n' || buffer[offset] == '\t')
  617. {
  618. chars[charCount++] = ' ';
  619. offset++;
  620. textOffset = offset;
  621. }
  622. else // '\r'
  623. {
  624. chars[charCount++] = ' ';
  625. offset++;
  626. if (offset < offsetMax && buffer[offset] == '\n')
  627. offset++;
  628. textOffset = offset;
  629. }
  630. }
  631. return charCount;
  632. }
  633. bool IsAttrChar(int ch)
  634. {
  635. switch (ch)
  636. {
  637. case '&':
  638. case '\r':
  639. case '\t':
  640. case '\n':
  641. return false;
  642. default:
  643. return true;
  644. }
  645. }
  646. public string GetEscapedString(int offset, int length)
  647. {
  648. char[] chars = GetCharBuffer(length);
  649. int charCount = GetEscapedChars(offset, length, chars);
  650. return new string(chars, 0, charCount);
  651. }
  652. public string GetEscapedString(int offset, int length, XmlNameTable nameTable)
  653. {
  654. char[] chars = GetCharBuffer(length);
  655. int charCount = GetEscapedChars(offset, length, chars);
  656. return nameTable.Add(chars, 0, charCount);
  657. }
  658. int GetLessThanCharEntity(int offset, int length)
  659. {
  660. byte[] buffer = this.buffer;
  661. if (length != 4 ||
  662. buffer[offset + 1] != (byte)'l' ||
  663. buffer[offset + 2] != (byte)'t')
  664. {
  665. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  666. }
  667. return (int)'<';
  668. }
  669. int GetGreaterThanCharEntity(int offset, int length)
  670. {
  671. byte[] buffer = this.buffer;
  672. if (length != 4 ||
  673. buffer[offset + 1] != (byte)'g' ||
  674. buffer[offset + 2] != (byte)'t')
  675. {
  676. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  677. }
  678. return (int)'>';
  679. }
  680. int GetQuoteCharEntity(int offset, int length)
  681. {
  682. byte[] buffer = this.buffer;
  683. if (length != 6 ||
  684. buffer[offset + 1] != (byte)'q' ||
  685. buffer[offset + 2] != (byte)'u' ||
  686. buffer[offset + 3] != (byte)'o' ||
  687. buffer[offset + 4] != (byte)'t')
  688. {
  689. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  690. }
  691. return (int)'"';
  692. }
  693. int GetAmpersandCharEntity(int offset, int length)
  694. {
  695. byte[] buffer = this.buffer;
  696. if (length != 5 ||
  697. buffer[offset + 1] != (byte)'a' ||
  698. buffer[offset + 2] != (byte)'m' ||
  699. buffer[offset + 3] != (byte)'p')
  700. {
  701. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  702. }
  703. return (int)'&';
  704. }
  705. int GetApostropheCharEntity(int offset, int length)
  706. {
  707. byte[] buffer = this.buffer;
  708. if (length != 6 ||
  709. buffer[offset + 1] != (byte)'a' ||
  710. buffer[offset + 2] != (byte)'p' ||
  711. buffer[offset + 3] != (byte)'o' ||
  712. buffer[offset + 4] != (byte)'s')
  713. {
  714. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  715. }
  716. return (int)'\'';
  717. }
  718. int GetDecimalCharEntity(int offset, int length)
  719. {
  720. byte[] buffer = this.buffer;
  721. Fx.Assert(buffer[offset + 0] == '&', "");
  722. Fx.Assert(buffer[offset + 1] == '#', "");
  723. Fx.Assert(buffer[offset + length - 1] == ';', "");
  724. int value = 0;
  725. for (int i = 2; i < length - 1; i++)
  726. {
  727. byte ch = buffer[offset + i];
  728. if (ch < (byte)'0' || ch > (byte)'9')
  729. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  730. value = value * 10 + (ch - '0');
  731. if (value > SurrogateChar.MaxValue)
  732. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  733. }
  734. return value;
  735. }
  736. int GetHexCharEntity(int offset, int length)
  737. {
  738. byte[] buffer = this.buffer;
  739. Fx.Assert(buffer[offset + 0] == '&', "");
  740. Fx.Assert(buffer[offset + 1] == '#', "");
  741. Fx.Assert(buffer[offset + 2] == 'x', "");
  742. Fx.Assert(buffer[offset + length - 1] == ';', "");
  743. int value = 0;
  744. for (int i = 3; i < length - 1; i++)
  745. {
  746. byte ch = buffer[offset + i];
  747. int digit = 0;
  748. if (ch >= '0' && ch <= '9')
  749. digit = (ch - '0');
  750. else if (ch >= 'a' && ch <= 'f')
  751. digit = 10 + (ch - 'a');
  752. else if (ch >= 'A' && ch <= 'F')
  753. digit = 10 + (ch - 'A');
  754. else
  755. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  756. Fx.Assert(digit >= 0 && digit < 16, "");
  757. value = value * 16 + digit;
  758. if (value > SurrogateChar.MaxValue)
  759. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  760. }
  761. return value;
  762. }
  763. public int GetCharEntity(int offset, int length)
  764. {
  765. if (length < 3)
  766. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  767. byte[] buffer = this.buffer;
  768. Fx.Assert(buffer[offset] == '&', "");
  769. Fx.Assert(buffer[offset + length - 1] == ';', "");
  770. switch (buffer[offset + 1])
  771. {
  772. case (byte)'l':
  773. return GetLessThanCharEntity(offset, length);
  774. case (byte)'g':
  775. return GetGreaterThanCharEntity(offset, length);
  776. case (byte)'a':
  777. if (buffer[offset + 2] == (byte)'m')
  778. return GetAmpersandCharEntity(offset, length);
  779. else
  780. return GetApostropheCharEntity(offset, length);
  781. case (byte)'q':
  782. return GetQuoteCharEntity(offset, length);
  783. case (byte)'#':
  784. if (buffer[offset + 2] == (byte)'x')
  785. return GetHexCharEntity(offset, length);
  786. else
  787. return GetDecimalCharEntity(offset, length);
  788. default:
  789. XmlExceptionHelper.ThrowInvalidCharRef(reader);
  790. return 0;
  791. }
  792. }
  793. public bool IsWhitespaceKey(int key)
  794. {
  795. string s = GetDictionaryString(key).Value;
  796. for (int i = 0; i < s.Length; i++)
  797. {
  798. if (!XmlConverter.IsWhitespace(s[i]))
  799. return false;
  800. }
  801. return true;
  802. }
  803. public bool IsWhitespaceUTF8(int offset, int length)
  804. {
  805. byte[] buffer = this.buffer;
  806. for (int i = 0; i < length; i++)
  807. {
  808. if (!XmlConverter.IsWhitespace((char)buffer[offset + i]))
  809. return false;
  810. }
  811. return true;
  812. }
  813. public bool IsWhitespaceUnicode(int offset, int length)
  814. {
  815. byte[] buffer = this.buffer;
  816. for (int i = 0; i < length; i += sizeof(char))
  817. {
  818. char ch = (char)GetInt16(offset + i);
  819. if (!XmlConverter.IsWhitespace(ch))
  820. return false;
  821. }
  822. return true;
  823. }
  824. public bool Equals2(int key1, int key2, XmlBufferReader bufferReader2)
  825. {
  826. // If the keys aren't from the same dictionary, they still might be the same
  827. if (key1 == key2)
  828. return true;
  829. else
  830. return GetDictionaryString(key1).Value == bufferReader2.GetDictionaryString(key2).Value;
  831. }
  832. public bool Equals2(int key1, XmlDictionaryString xmlString2)
  833. {
  834. if ((key1 & 1) == 0 && xmlString2.Dictionary == dictionary)
  835. return xmlString2.Key == (key1 >> 1);
  836. else
  837. return GetDictionaryString(key1).Value == xmlString2.Value;
  838. }
  839. public bool Equals2(int offset1, int length1, byte[] buffer2)
  840. {
  841. int length2 = buffer2.Length;
  842. if (length1 != length2)
  843. return false;
  844. byte[] buffer1 = this.buffer;
  845. for (int i = 0; i < length1; i++)
  846. {
  847. if (buffer1[offset1 + i] != buffer2[i])
  848. return false;
  849. }
  850. return true;
  851. }
  852. public bool Equals2(int offset1, int length1, XmlBufferReader bufferReader2, int offset2, int length2)
  853. {
  854. if (length1 != length2)
  855. return false;
  856. byte[] buffer1 = this.buffer;
  857. byte[] buffer2 = bufferReader2.buffer;
  858. for (int i = 0; i < length1; i++)
  859. {
  860. if (buffer1[offset1 + i] != buffer2[offset2 + i])
  861. return false;
  862. }
  863. return true;
  864. }
  865. public bool Equals2(int offset1, int length1, int offset2, int length2)
  866. {
  867. if (length1 != length2)
  868. return false;
  869. if (offset1 == offset2)
  870. return true;
  871. byte[] buffer = this.buffer;
  872. for (int i = 0; i < length1; i++)
  873. {
  874. if (buffer[offset1 + i] != buffer[offset2 + i])
  875. return false;
  876. }
  877. return true;
  878. }
  879. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  880. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  881. [SecuritySafeCritical]
  882. unsafe public bool Equals2(int offset1, int length1, string s2)
  883. {
  884. int byteLength = length1;
  885. int charLength = s2.Length;
  886. // N unicode chars will be represented in at least N bytes, but
  887. // no more than N * 3 bytes. If the byte count falls outside of this
  888. // range, then the strings cannot be equal.
  889. if (byteLength < charLength || byteLength > charLength * maxBytesPerChar)
  890. return false;
  891. byte[] buffer = this.buffer;
  892. if (length1 < 8)
  893. {
  894. int length = Math.Min(byteLength, charLength);
  895. int offset = offset1;
  896. for (int i = 0; i < length; i++)
  897. {
  898. byte b = buffer[offset + i];
  899. if (b >= 0x80)
  900. return XmlConverter.ToString(buffer, offset1, length1) == s2;
  901. if (s2[i] != (char)b)
  902. return false;
  903. }
  904. return byteLength == charLength;
  905. }
  906. else
  907. {
  908. int length = Math.Min(byteLength, charLength);
  909. fixed (byte* _pb = &buffer[offset1])
  910. {
  911. byte* pb = _pb;
  912. byte* pbMax = pb + length;
  913. fixed (char* _pch = s2)
  914. {
  915. char* pch = _pch;
  916. // Try to do the fast comparison in ascii space
  917. int t = 0;
  918. while (pb < pbMax && *pb < 0x80)
  919. {
  920. t = *pb - (byte)(*pch);
  921. // The code generated is better if we break out then return
  922. if (t != 0)
  923. break;
  924. pb++;
  925. pch++;
  926. }
  927. if (t != 0)
  928. return false;
  929. if (pb == pbMax)
  930. return (byteLength == charLength);
  931. }
  932. }
  933. return XmlConverter.ToString(buffer, offset1, length1) == s2;
  934. }
  935. }
  936. public int Compare(int offset1, int length1, int offset2, int length2)
  937. {
  938. byte[] buffer = this.buffer;
  939. int length = Math.Min(length1, length2);
  940. for (int i = 0; i < length; i++)
  941. {
  942. int s = buffer[offset1 + i] - buffer[offset2 + i];
  943. if (s != 0)
  944. return s;
  945. }
  946. return length1 - length2;
  947. }
  948. public byte GetByte(int offset)
  949. {
  950. return buffer[offset];
  951. }
  952. public int GetInt8(int offset)
  953. {
  954. return (sbyte)GetByte(offset);
  955. }
  956. public int GetInt16(int offset)
  957. {
  958. byte[] buffer = this.buffer;
  959. return (Int16)(buffer[offset] + (buffer[offset + 1] << 8));
  960. }
  961. public int GetInt32(int offset)
  962. {
  963. byte[] buffer = this.buffer;
  964. byte b1 = buffer[offset + 0];
  965. byte b2 = buffer[offset + 1];
  966. byte b3 = buffer[offset + 2];
  967. byte b4 = buffer[offset + 3];
  968. return (((((b4 << 8) + b3) << 8) + b2) << 8) + b1;
  969. }
  970. public long GetInt64(int offset)
  971. {
  972. byte[] buffer = this.buffer;
  973. byte b1, b2, b3, b4;
  974. b1 = buffer[offset + 0];
  975. b2 = buffer[offset + 1];
  976. b3 = buffer[offset + 2];
  977. b4 = buffer[offset + 3];
  978. Int64 lo = (UInt32)(((((b4 << 8) + b3) << 8) + b2) << 8) + b1;
  979. b1 = buffer[offset + 4];
  980. b2 = buffer[offset + 5];
  981. b3 = buffer[offset + 6];
  982. b4 = buffer[offset + 7];
  983. Int64 hi = (UInt32)(((((b4 << 8) + b3) << 8) + b2) << 8) + b1;
  984. return (hi << 32) + lo;
  985. }
  986. public ulong GetUInt64(int offset)
  987. {
  988. return (ulong)GetInt64(offset);
  989. }
  990. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  991. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  992. [SecuritySafeCritical]
  993. unsafe public float GetSingle(int offset)
  994. {
  995. byte[] buffer = this.buffer;
  996. float value;
  997. byte* pb = (byte*)&value;
  998. Fx.Assert(sizeof(float) == 4, "");
  999. pb[0] = buffer[offset + 0];
  1000. pb[1] = buffer[offset + 1];
  1001. pb[2] = buffer[offset + 2];
  1002. pb[3] = buffer[offset + 3];
  1003. return value;
  1004. }
  1005. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1006. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1007. [SecuritySafeCritical]
  1008. unsafe public double GetDouble(int offset)
  1009. {
  1010. byte[] buffer = this.buffer;
  1011. double value;
  1012. byte* pb = (byte*)&value;
  1013. Fx.Assert(sizeof(double) == 8, "");
  1014. pb[0] = buffer[offset + 0];
  1015. pb[1] = buffer[offset + 1];
  1016. pb[2] = buffer[offset + 2];
  1017. pb[3] = buffer[offset + 3];
  1018. pb[4] = buffer[offset + 4];
  1019. pb[5] = buffer[offset + 5];
  1020. pb[6] = buffer[offset + 6];
  1021. pb[7] = buffer[offset + 7];
  1022. return value;
  1023. }
  1024. [Fx.Tag.SecurityNote(Critical = "Contains unsafe code.",
  1025. Safe = "Unsafe code is effectively encapsulated, all inputs are validated.")]
  1026. [SecuritySafeCritical]
  1027. public unsafe decimal GetDecimal(int offset)
  1028. {
  1029. const int SignMask = unchecked((int)0x80000000);
  1030. const int ScaleMask = 0x00FF0000;
  1031. byte[] buffer = this.buffer;
  1032. byte b1 = buffer[offset + 0];
  1033. byte b2 = buffer[offset + 1];
  1034. byte b3 = buffer[offset + 2];
  1035. byte b4 = buffer[offset + 3];
  1036. int flags = (((((b4 << 8) + b3) << 8) + b2) << 8) + b1;
  1037. //this logic mirrors the code in Decimal(int []) ctor.
  1038. if ((flags & ~(SignMask | ScaleMask)) == 0 && (flags & ScaleMask) <= (28 << 16))
  1039. {
  1040. decimal value;
  1041. byte* pb = (byte*)&value;
  1042. for (int i = 0; i < sizeof(decimal); i++)
  1043. pb[i] = buffer[offset + i];
  1044. return value;
  1045. }
  1046. else
  1047. {
  1048. XmlExceptionHelper.ThrowInvalidBinaryFormat(this.reader);
  1049. }
  1050. //compiler doesn't know that XmlExceptionHelper.ThrowInvalidBinaryFormat always throws,
  1051. //so we have to have a return statement here even though we shouldn't hit this code path...
  1052. Fx.Assert("A decimal value should have been returned or an exception should have been thrown.");
  1053. return default(decimal);
  1054. }
  1055. public UniqueId GetUniqueId(int offset)
  1056. {
  1057. return new UniqueId(this.buffer, offset);
  1058. }
  1059. public Guid GetGuid(int offset)
  1060. {
  1061. if (guid == null)
  1062. guid = new byte[16];
  1063. System.Buffer.BlockCopy(buffer, offset, guid, 0, guid.Length);
  1064. return new Guid(guid);
  1065. }
  1066. public void GetBase64(int srcOffset, byte[] buffer, int dstOffset, int count)
  1067. {
  1068. System.Buffer.BlockCopy(this.buffer, srcOffset, buffer, dstOffset, count);
  1069. }
  1070. public XmlBinaryNodeType GetNodeType()
  1071. {
  1072. return (XmlBinaryNodeType)GetByte();
  1073. }
  1074. public void SkipNodeType()
  1075. {
  1076. SkipByte();
  1077. }
  1078. public object[] GetList(int offset, int count)
  1079. {
  1080. int bufferOffset = this.Offset;
  1081. this.Offset = offset;
  1082. try
  1083. {
  1084. object[] objects = new object[count];
  1085. for (int i = 0; i < count; i++)
  1086. {
  1087. XmlBinaryNodeType nodeType = GetNodeType();
  1088. SkipNodeType();
  1089. Fx.Assert(nodeType != XmlBinaryNodeType.StartListText, "");
  1090. ReadValue(nodeType, listValue);
  1091. objects[i] = listValue.ToObject();
  1092. }
  1093. return objects;
  1094. }
  1095. finally
  1096. {
  1097. this.Offset = bufferOffset;
  1098. }
  1099. }
  1100. public XmlDictionaryString GetDictionaryString(int key)
  1101. {
  1102. IXmlDictionary keyDictionary;
  1103. if ((key & 1) != 0)
  1104. {
  1105. keyDictionary = session;
  1106. }
  1107. else
  1108. {
  1109. keyDictionary = dictionary;
  1110. }
  1111. XmlDictionaryString s;
  1112. if (!keyDictionary.TryLookup(key >> 1, out s))
  1113. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  1114. return s;
  1115. }
  1116. public int ReadDictionaryKey()
  1117. {
  1118. int key = ReadMultiByteUInt31();
  1119. if ((key & 1) != 0)
  1120. {
  1121. if (session == null)
  1122. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  1123. int sessionKey = (key >> 1);
  1124. XmlDictionaryString xmlString;
  1125. if (!session.TryLookup(sessionKey, out xmlString))
  1126. {
  1127. if (sessionKey < XmlDictionaryString.MinKey || sessionKey > XmlDictionaryString.MaxKey)
  1128. XmlExceptionHelper.ThrowXmlDictionaryStringIDOutOfRange(this.reader);
  1129. XmlExceptionHelper.ThrowXmlDictionaryStringIDUndefinedSession(this.reader, sessionKey);
  1130. }
  1131. }
  1132. else
  1133. {
  1134. if (dictionary == null)
  1135. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  1136. int staticKey = (key >> 1);
  1137. XmlDictionaryString xmlString;
  1138. if (!dictionary.TryLookup(staticKey, out xmlString))
  1139. {
  1140. if (staticKey < XmlDictionaryString.MinKey || staticKey > XmlDictionaryString.MaxKey)
  1141. XmlExceptionHelper.ThrowXmlDictionaryStringIDOutOfRange(this.reader);
  1142. XmlExceptionHelper.ThrowXmlDictionaryStringIDUndefinedStatic(this.reader, staticKey);
  1143. }
  1144. }
  1145. return key;
  1146. }
  1147. public void ReadValue(XmlBinaryNodeType nodeType, ValueHandle value)
  1148. {
  1149. switch (nodeType)
  1150. {
  1151. case XmlBinaryNodeType.EmptyText:
  1152. value.SetValue(ValueHandleType.Empty);
  1153. break;
  1154. case XmlBinaryNodeType.ZeroText:
  1155. value.SetValue(ValueHandleType.Zero);
  1156. break;
  1157. case XmlBinaryNodeType.OneText:
  1158. value.SetValue(ValueHandleType.One);
  1159. break;
  1160. case XmlBinaryNodeType.TrueText:
  1161. value.SetValue(ValueHandleType.True);
  1162. break;
  1163. case XmlBinaryNodeType.FalseText:
  1164. value.SetValue(ValueHandleType.False);
  1165. break;
  1166. case XmlBinaryNodeType.BoolText:
  1167. value.SetValue(ReadUInt8() != 0 ? ValueHandleType.True : ValueHandleType.False);
  1168. break;
  1169. case XmlBinaryNodeType.Chars8Text:
  1170. ReadValue(value, ValueHandleType.UTF8, ReadUInt8());
  1171. break;
  1172. case XmlBinaryNodeType.Chars16Text:
  1173. ReadValue(value, ValueHandleType.UTF8, ReadUInt16());
  1174. break;
  1175. case XmlBinaryNodeType.Chars32Text:
  1176. ReadValue(value, ValueHandleType.UTF8, ReadUInt31());
  1177. break;
  1178. case XmlBinaryNodeType.UnicodeChars8Text:
  1179. ReadUnicodeValue(value, ReadUInt8());
  1180. break;
  1181. case XmlBinaryNodeType.UnicodeChars16Text:
  1182. ReadUnicodeValue(value, ReadUInt16());
  1183. break;
  1184. case XmlBinaryNodeType.UnicodeChars32Text:
  1185. ReadUnicodeValue(value, ReadUInt31());
  1186. break;
  1187. case XmlBinaryNodeType.Bytes8Text:
  1188. ReadValue(value, ValueHandleType.Base64, ReadUInt8());
  1189. break;
  1190. case XmlBinaryNodeType.Bytes16Text:
  1191. ReadValue(value, ValueHandleType.Base64, ReadUInt16());
  1192. break;
  1193. case XmlBinaryNodeType.Bytes32Text:
  1194. ReadValue(value, ValueHandleType.Base64, ReadUInt31());
  1195. break;
  1196. case XmlBinaryNodeType.DictionaryText:
  1197. value.SetDictionaryValue(ReadDictionaryKey());
  1198. break;
  1199. case XmlBinaryNodeType.UniqueIdText:
  1200. ReadValue(value, ValueHandleType.UniqueId, ValueHandleLength.UniqueId);
  1201. break;
  1202. case XmlBinaryNodeType.GuidText:
  1203. ReadValue(value, ValueHandleType.Guid, ValueHandleLength.Guid);
  1204. break;
  1205. case XmlBinaryNodeType.DecimalText:
  1206. ReadValue(value, ValueHandleType.Decimal, ValueHandleLength.Decimal);
  1207. break;
  1208. case XmlBinaryNodeType.Int8Text:
  1209. ReadValue(value, ValueHandleType.Int8, ValueHandleLength.Int8);
  1210. break;
  1211. case XmlBinaryNodeType.Int16Text:
  1212. ReadValue(value, ValueHandleType.Int16, ValueHandleLength.Int16);
  1213. break;
  1214. case XmlBinaryNodeType.Int32Text:
  1215. ReadValue(value, ValueHandleType.Int32, ValueHandleLength.Int32);
  1216. break;
  1217. case XmlBinaryNodeType.Int64Text:
  1218. ReadValue(value, ValueHandleType.Int64, ValueHandleLength.Int64);
  1219. break;
  1220. case XmlBinaryNodeType.UInt64Text:
  1221. ReadValue(value, ValueHandleType.UInt64, ValueHandleLength.UInt64);
  1222. break;
  1223. case XmlBinaryNodeType.FloatText:
  1224. ReadValue(value, ValueHandleType.Single, ValueHandleLength.Single);
  1225. break;
  1226. case XmlBinaryNodeType.DoubleText:
  1227. ReadValue(value, ValueHandleType.Double, ValueHandleLength.Double);
  1228. break;
  1229. case XmlBinaryNodeType.TimeSpanText:
  1230. ReadValue(value, ValueHandleType.TimeSpan, ValueHandleLength.TimeSpan);
  1231. break;
  1232. case XmlBinaryNodeType.DateTimeText:
  1233. ReadValue(value, ValueHandleType.DateTime, ValueHandleLength.DateTime);
  1234. break;
  1235. case XmlBinaryNodeType.StartListText:
  1236. ReadList(value);
  1237. break;
  1238. case XmlBinaryNodeType.QNameDictionaryText:
  1239. ReadQName(value);
  1240. break;
  1241. default:
  1242. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  1243. break;
  1244. }
  1245. }
  1246. void ReadValue(ValueHandle value, ValueHandleType type, int length)
  1247. {
  1248. int offset = ReadBytes(length);
  1249. value.SetValue(type, offset, length);
  1250. }
  1251. void ReadUnicodeValue(ValueHandle value, int length)
  1252. {
  1253. if ((length & 1) != 0)
  1254. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  1255. ReadValue(value, ValueHandleType.Unicode, length);
  1256. }
  1257. void ReadList(ValueHandle value)
  1258. {
  1259. if (listValue == null)
  1260. {
  1261. listValue = new ValueHandle(this);
  1262. }
  1263. int count = 0;
  1264. int offset = this.Offset;
  1265. while (true)
  1266. {
  1267. XmlBinaryNodeType nodeType = GetNodeType();
  1268. SkipNodeType();
  1269. if (nodeType == XmlBinaryNodeType.StartListText)
  1270. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  1271. if (nodeType == XmlBinaryNodeType.EndListText)
  1272. break;
  1273. ReadValue(nodeType, listValue);
  1274. count++;
  1275. }
  1276. value.SetValue(ValueHandleType.List, offset, count);
  1277. }
  1278. public void ReadQName(ValueHandle value)
  1279. {
  1280. int prefix = ReadUInt8();
  1281. if (prefix >= 26)
  1282. XmlExceptionHelper.ThrowInvalidBinaryFormat(reader);
  1283. int key = ReadDictionaryKey();
  1284. value.SetQNameValue(prefix, key);
  1285. }
  1286. public int[] GetRows()
  1287. {
  1288. if (buffer == null)
  1289. {
  1290. return new int[1] { 0 };
  1291. }
  1292. ArrayList list = new ArrayList();
  1293. list.Add(offsetMin);
  1294. for (int i = offsetMin; i < offsetMax; i++)
  1295. {
  1296. if (buffer[i] == (byte)13 || buffer[i] == (byte)10)
  1297. {
  1298. if (i + 1 < offsetMax && buffer[i + 1] == (byte)10)
  1299. i++;
  1300. list.Add(i + 1);
  1301. }
  1302. }
  1303. return (int[])list.ToArray(typeof(int));
  1304. }
  1305. }
  1306. }