XmlBinaryDictionaryReader.cs 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. //
  2. // XmlBinaryDictionaryReader.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005, 2007 Novell, Inc. http://www.novell.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Collections.Generic;
  33. using System.Globalization;
  34. using System.IO;
  35. using System.Text;
  36. using QName = System.Xml.XmlQualifiedName;
  37. using BF = System.Xml.XmlBinaryFormat;
  38. namespace System.Xml
  39. {
  40. // FIXME:
  41. // - native value data (7B-82, 8D-A0) are not implemented.
  42. // - support XmlDictionaryReaderQuotas.
  43. // - support XmlBinaryReaderSession.
  44. // - handle namespaces as expected.
  45. internal class XmlBinaryDictionaryReader : XmlDictionaryReader, IXmlNamespaceResolver
  46. {
  47. internal interface ISource
  48. {
  49. int Position { get; }
  50. int ReadByte ();
  51. int Read (byte [] data, int offset, int count);
  52. BinaryReader Reader { get; }
  53. }
  54. internal class StreamSource : ISource
  55. {
  56. BinaryReader stream;
  57. int position;
  58. public StreamSource (Stream stream)
  59. {
  60. this.stream = new BinaryReader (stream);
  61. }
  62. public int Position {
  63. get { return position - 1; }
  64. }
  65. public BinaryReader Reader {
  66. get { return stream; }
  67. }
  68. public int ReadByte ()
  69. {
  70. if (stream.PeekChar () < 0)
  71. return -1;
  72. position++;
  73. return stream.ReadByte ();
  74. }
  75. public int Read (byte [] data, int offset, int count)
  76. {
  77. int ret = stream.Read (data, offset, count);
  78. position += ret;
  79. return ret;
  80. }
  81. }
  82. class NodeInfo
  83. {
  84. public NodeInfo ()
  85. {
  86. }
  87. public NodeInfo (bool isAttr)
  88. {
  89. IsAttributeValue = isAttr;
  90. }
  91. public bool IsAttributeValue;
  92. public int Position;
  93. public string Prefix;
  94. public XmlDictionaryString DictLocalName;
  95. public XmlDictionaryString DictNS;
  96. public XmlNodeType NodeType;
  97. public object TypedValue;
  98. public byte ValueType;
  99. // -1 for nothing,
  100. // -2 for that of element (only for attribute),
  101. // 0 or more to fill later
  102. public int NSSlot;
  103. string name;
  104. string local_name;
  105. string ns;
  106. string value;
  107. public string LocalName {
  108. get { return DictLocalName != null ? DictLocalName.Value : local_name; }
  109. set {
  110. DictLocalName = null;
  111. local_name = value;
  112. }
  113. }
  114. public string NS {
  115. get { return DictNS != null ? DictNS.Value : ns; }
  116. set {
  117. DictNS = null;
  118. ns = value;
  119. }
  120. }
  121. public string Name {
  122. get {
  123. if (name == null)
  124. name = Prefix.Length > 0 ?
  125. String.Concat (Prefix, ":", LocalName) :
  126. LocalName;
  127. return name;
  128. }
  129. }
  130. public string Value {
  131. get {
  132. if (BF.AttrString <= ValueType && ValueType <= BF.GlobalAttrIndexInElemNS)
  133. return value; // attribute
  134. switch (ValueType) {
  135. case 0:
  136. case BF.Comment:
  137. case BF.Text:
  138. case BF.EmptyText:
  139. return value;
  140. case BF.Zero:
  141. case BF.One:
  142. return XmlConvert.ToString ((int) TypedValue);
  143. case BF.Int8:
  144. return XmlConvert.ToString ((byte) TypedValue);
  145. case BF.Int16:
  146. return XmlConvert.ToString ((short) TypedValue);
  147. case BF.Int32:
  148. return XmlConvert.ToString ((int) TypedValue);
  149. case BF.Int64:
  150. return XmlConvert.ToString ((long) TypedValue);
  151. case BF.Single:
  152. return XmlConvert.ToString ((float) TypedValue);
  153. case BF.Double:
  154. return XmlConvert.ToString ((double) TypedValue);
  155. case BF.DateTime:
  156. return XmlConvert.ToString ((DateTime) TypedValue, XmlDateTimeSerializationMode.RoundtripKind);
  157. case BF.TimeSpan:
  158. return XmlConvert.ToString ((TimeSpan) TypedValue);
  159. case BF.Guid:
  160. return XmlConvert.ToString ((Guid) TypedValue);
  161. case BF.UniqueIdFromGuid:
  162. return TypedValue.ToString ();
  163. case BF.Base64:
  164. case BF.Base64Fixed:
  165. return Convert.ToBase64String ((byte []) TypedValue);
  166. default:
  167. throw new NotImplementedException ("ValueType " + ValueType + " on node " + NodeType);
  168. }
  169. }
  170. set { this.value = value; }
  171. }
  172. public virtual void Reset ()
  173. {
  174. Position = 0;
  175. DictLocalName = DictNS = null;
  176. LocalName = NS = Prefix = Value = String.Empty;
  177. NodeType = XmlNodeType.None;
  178. TypedValue = null;
  179. ValueType = 0;
  180. NSSlot = -1;
  181. }
  182. }
  183. class AttrNodeInfo : NodeInfo
  184. {
  185. public int ValueIndex;
  186. public override void Reset ()
  187. {
  188. base.Reset ();
  189. ValueIndex = -1;
  190. }
  191. }
  192. ISource source;
  193. IXmlDictionary dictionary;
  194. XmlDictionaryReaderQuotas quota;
  195. XmlBinaryReaderSession session;
  196. OnXmlDictionaryReaderClose on_close;
  197. XmlParserContext context;
  198. ReadState state = ReadState.Initial;
  199. NodeInfo node;
  200. NodeInfo current;
  201. List<AttrNodeInfo> attributes = new List<AttrNodeInfo> ();
  202. List<NodeInfo> attr_values = new List<NodeInfo> ();
  203. List<NodeInfo> node_stack = new List<NodeInfo> ();
  204. List<QName> ns_store = new List<QName> ();
  205. Dictionary<int,XmlDictionaryString> ns_dict_store =
  206. new Dictionary<int,XmlDictionaryString> ();
  207. int attr_count;
  208. int attr_value_count;
  209. int current_attr = -1;
  210. int depth = 0;
  211. // used during Read()
  212. int ns_slot;
  213. // next byte in the source (one byte token ahead always
  214. // happens because there is no "end of start element" mark).
  215. int next = -1;
  216. bool is_next_end_element;
  217. // temporary buffer for utf8enc.GetString()
  218. byte [] tmp_buffer = new byte [128];
  219. UTF8Encoding utf8enc = new UTF8Encoding ();
  220. public XmlBinaryDictionaryReader (byte [] buffer, int offset,
  221. int count, IXmlDictionary dictionary,
  222. XmlDictionaryReaderQuotas quota,
  223. XmlBinaryReaderSession session,
  224. OnXmlDictionaryReaderClose onClose)
  225. {
  226. source = /*new ArraySource (buffer, offset, count);*/
  227. new StreamSource (new MemoryStream (buffer, offset, count));
  228. Initialize (dictionary, quota, session, onClose);
  229. }
  230. public XmlBinaryDictionaryReader (Stream stream,
  231. IXmlDictionary dictionary,
  232. XmlDictionaryReaderQuotas quota,
  233. XmlBinaryReaderSession session,
  234. OnXmlDictionaryReaderClose onClose)
  235. {
  236. source = new StreamSource (stream);
  237. Initialize (dictionary, quota, session, onClose);
  238. }
  239. private void Initialize (IXmlDictionary dictionary,
  240. XmlDictionaryReaderQuotas quotas,
  241. XmlBinaryReaderSession session,
  242. OnXmlDictionaryReaderClose onClose)
  243. {
  244. if (quotas == null)
  245. throw new ArgumentNullException ("quotas");
  246. if (dictionary == null)
  247. dictionary = new XmlDictionary ();
  248. this.dictionary = dictionary;
  249. this.quota = quotas;
  250. if (session == null)
  251. session = new XmlBinaryReaderSession ();
  252. this.session = session;
  253. on_close = onClose;
  254. NameTable nt = new NameTable ();
  255. this.context = new XmlParserContext (nt,
  256. new XmlNamespaceManager (nt),
  257. null, XmlSpace.None);
  258. current = node = new NodeInfo ();
  259. current.Reset ();
  260. }
  261. public override int AttributeCount {
  262. get { return attr_count; }
  263. }
  264. public override string BaseURI {
  265. get { return context.BaseURI; }
  266. }
  267. public override int Depth {
  268. get { return depth; }
  269. }
  270. public override bool EOF {
  271. get { return state == ReadState.EndOfFile || state == ReadState.Error; }
  272. }
  273. #if !NET_2_1
  274. public override bool HasValue {
  275. get { return current.Value.Length > 0; }
  276. }
  277. #endif
  278. public override bool IsEmptyElement {
  279. get { return false; }
  280. }
  281. public override XmlNodeType NodeType {
  282. get { return current.NodeType; }
  283. }
  284. public override string Prefix {
  285. get { return current.Prefix; }
  286. }
  287. public override string LocalName {
  288. get { return current.LocalName; }
  289. }
  290. public override string NamespaceURI {
  291. get { return current.NS; }
  292. }
  293. public override XmlNameTable NameTable {
  294. get { return context.NameTable; }
  295. }
  296. public override XmlDictionaryReaderQuotas Quotas {
  297. get { return quota; }
  298. }
  299. public override ReadState ReadState {
  300. get { return state; }
  301. }
  302. public override string Value {
  303. get { return current.Value; }
  304. }
  305. public override void Close ()
  306. {
  307. if (on_close != null)
  308. on_close (this);
  309. }
  310. public override string GetAttribute (int i)
  311. {
  312. if (i >= attr_count)
  313. throw new ArgumentOutOfRangeException (String.Format ("Specified attribute index is {0} and should be less than {1}", i, attr_count));
  314. return attributes [i].Value;
  315. }
  316. public override string GetAttribute (string name)
  317. {
  318. for (int i = 0; i < attributes.Count; i++)
  319. if (attributes [i].Name == name)
  320. return attributes [i].Value;
  321. return null;
  322. }
  323. public override string GetAttribute (string localName, string ns)
  324. {
  325. for (int i = 0; i < attributes.Count; i++)
  326. if (attributes [i].LocalName == localName &&
  327. attributes [i].NS == ns)
  328. return attributes [i].Value;
  329. return null;
  330. }
  331. public IDictionary<string,string> GetNamespacesInScope (
  332. XmlNamespaceScope scope)
  333. {
  334. return context.NamespaceManager.GetNamespacesInScope (scope);
  335. }
  336. public string LookupPrefix (string ns)
  337. {
  338. return context.NamespaceManager.LookupPrefix (NameTable.Get (ns));
  339. }
  340. public override string LookupNamespace (string prefix)
  341. {
  342. return context.NamespaceManager.LookupNamespace (
  343. NameTable.Get (prefix));
  344. }
  345. public override bool MoveToElement ()
  346. {
  347. bool ret = current_attr >= 0;
  348. current_attr = -1;
  349. current = node;
  350. return ret;
  351. }
  352. public override bool MoveToFirstAttribute ()
  353. {
  354. if (attr_count == 0)
  355. return false;
  356. current_attr = 0;
  357. current = attributes [current_attr];
  358. return true;
  359. }
  360. public override bool MoveToNextAttribute ()
  361. {
  362. if (++current_attr < attr_count) {
  363. current = attributes [current_attr];
  364. return true;
  365. } else {
  366. --current_attr;
  367. return false;
  368. }
  369. }
  370. public override void MoveToAttribute (int i)
  371. {
  372. if (i >= attr_count)
  373. throw new ArgumentOutOfRangeException (String.Format ("Specified attribute index is {0} and should be less than {1}", i, attr_count));
  374. current_attr = i;
  375. current = attributes [i];
  376. }
  377. public override bool MoveToAttribute (string name)
  378. {
  379. for (int i = 0; i < attributes.Count; i++) {
  380. if (attributes [i].Name == name) {
  381. MoveToAttribute (i);
  382. return true;
  383. }
  384. }
  385. return false;
  386. }
  387. public override bool MoveToAttribute (string localName, string ns)
  388. {
  389. for (int i = 0; i < attributes.Count; i++) {
  390. if (attributes [i].LocalName == localName &&
  391. attributes [i].NS == ns) {
  392. MoveToAttribute (i);
  393. return true;
  394. }
  395. }
  396. return false;
  397. }
  398. #if !NET_2_1
  399. public override bool ReadAttributeValue ()
  400. {
  401. if (current_attr < 0)
  402. return false;
  403. int start = attributes [current_attr].ValueIndex;
  404. int end = current_attr + 1 == attr_count ? attr_value_count : attributes [current_attr + 1].ValueIndex;
  405. if (start == end)
  406. return false;
  407. if (!current.IsAttributeValue) {
  408. current = attr_values [start];
  409. return true;
  410. }
  411. for (int i = start; i < end; i++) {
  412. if (current == attr_values [i] && i + 1 < end) {
  413. current = attr_values [i + 1];
  414. return true;
  415. }
  416. }
  417. return false;
  418. }
  419. #endif
  420. public override bool Read ()
  421. {
  422. switch (state) {
  423. case ReadState.Closed:
  424. case ReadState.EndOfFile:
  425. case ReadState.Error:
  426. return false;
  427. }
  428. // clear.
  429. state = ReadState.Interactive;
  430. attr_count = 0;
  431. attr_value_count = 0;
  432. ns_slot = 0;
  433. current = node;
  434. if (node.NodeType == XmlNodeType.Element) {
  435. // push element scope
  436. depth++;
  437. if (node_stack.Count <= depth) {
  438. node_stack.Add (node);
  439. node = new NodeInfo ();
  440. }
  441. else
  442. node = node_stack [depth];
  443. node.Reset ();
  444. current = node;
  445. }
  446. if (is_next_end_element) {
  447. is_next_end_element = false;
  448. ProcessEndElement ();
  449. return true;
  450. }
  451. node.Reset ();
  452. int ident = next >= 0 ? next : source.ReadByte ();
  453. next = -1;
  454. // check end of source.
  455. if (ident < 0) {
  456. state = ReadState.EndOfFile;
  457. current.Reset ();
  458. return false;
  459. }
  460. is_next_end_element = ident > 0x80 && (ident & 1) == 1;
  461. ident -= is_next_end_element ? 1 : 0;
  462. /*
  463. if (0x3F <= ident && ident <= 0x42)
  464. ReadElementBinary ((byte) ident);
  465. else {
  466. switch (ident) {
  467. case 0x3C: // end element
  468. ProcessEndElement ();
  469. break;
  470. case 0x3D: // comment
  471. node.Value = ReadUTF8 ();
  472. node.NodeType = XmlNodeType.Comment;
  473. break;
  474. default:
  475. ReadTextOrValue ((byte) ident, node, false);
  476. break;
  477. }
  478. }
  479. */
  480. switch (ident) {
  481. case BF.EndElement:
  482. ProcessEndElement ();
  483. break;
  484. case BF.Comment:
  485. node.Value = ReadUTF8 ();
  486. node.ValueType = BF.Comment;
  487. node.NodeType = XmlNodeType.Comment;
  488. break;
  489. case BF.ElemString:
  490. case BF.ElemStringPrefix:
  491. case BF.ElemIndex:
  492. case BF.ElemIndexPrefix:
  493. ReadElementBinary ((byte) ident);
  494. break;
  495. default:
  496. ReadTextOrValue ((byte) ident, node, false);
  497. break;
  498. }
  499. return true;
  500. }
  501. private void ProcessEndElement ()
  502. {
  503. if (depth == 0)
  504. throw new XmlException ("Unexpected end of element while there is no element started.");
  505. current = node = node_stack [--depth];
  506. node.NodeType = XmlNodeType.EndElement;
  507. context.NamespaceManager.PopScope ();
  508. }
  509. private void ReadElementBinary (int ident)
  510. {
  511. // element
  512. node.NodeType = XmlNodeType.Element;
  513. node.Prefix = String.Empty;
  514. context.NamespaceManager.PushScope ();
  515. switch (ident) {
  516. case BF.ElemString:
  517. node.LocalName = ReadUTF8 ();
  518. break;
  519. case BF.ElemStringPrefix:
  520. node.Prefix = ReadUTF8 ();
  521. node.NSSlot = ns_slot++;
  522. goto case BF.ElemString;
  523. case BF.ElemIndex:
  524. node.DictLocalName = ReadDictName ();
  525. break;
  526. case BF.ElemIndexPrefix:
  527. node.Prefix = ReadUTF8 ();
  528. node.NSSlot = ns_slot++;
  529. goto case BF.ElemIndex;
  530. }
  531. bool loop = true;
  532. do {
  533. ident = next < 0 ? ReadByteOrError () : next;
  534. next = -1;
  535. switch (ident) {
  536. case BF.AttrString:
  537. case BF.AttrStringPrefix:
  538. case BF.AttrIndex:
  539. case BF.AttrIndexPrefix:
  540. case BF.GlobalAttrIndex:
  541. case BF.GlobalAttrIndexInElemNS:
  542. ReadAttribute ((byte) ident);
  543. break;
  544. case BF.DefaultNSString:
  545. case BF.PrefixNSString:
  546. case BF.DefaultNSIndex:
  547. case BF.PrefixNSIndex:
  548. ReadNamespace ((byte) ident);
  549. break;
  550. default:
  551. next = ident;
  552. loop = false;
  553. break;
  554. }
  555. /*
  556. if (ident < 4) {
  557. // attributes
  558. if (attributes.Count == attr_count)
  559. attributes.Add (new AttrNodeInfo ());
  560. AttrNodeInfo a = attributes [attr_count++];
  561. a.Reset ();
  562. a.Position = source.Position;
  563. switch (ident) {
  564. case 0:
  565. a.LocalName = ReadUTF8 ();
  566. break;
  567. case 1:
  568. a.Prefix = ReadUTF8 ();
  569. goto case 0;
  570. case 2:
  571. a.DictLocalName = ReadDictName ();
  572. break;
  573. case 3:
  574. a.Prefix = ReadUTF8 ();
  575. goto case 2;
  576. }
  577. ReadAttributeValueBinary (a);
  578. }
  579. else if (ident < 6) {
  580. // namespaces
  581. string prefix = ident == 4 ?
  582. String.Empty : ReadUTF8 ();
  583. string ns = ReadUTF8 ();
  584. ns_store.Add (new QName (prefix, ns));
  585. context.NamespaceManager.AddNamespace (prefix, ns);
  586. }
  587. else if (0x22 <= ident && ident < 0x3C) {
  588. // attributes with predefined ns index
  589. if (attributes.Count == attr_count)
  590. attributes.Add (new AttrNodeInfo ());
  591. AttrNodeInfo a = attributes [attr_count++];
  592. a.Reset ();
  593. a.Position = source.Position;
  594. a.NSSlot = ident - 0x22;
  595. a.LocalName = ReadUTF8 ();
  596. ReadAttributeValueBinary (a);
  597. }
  598. else {
  599. next = ident;
  600. break;
  601. }
  602. */
  603. } while (loop);
  604. #if true
  605. node.NS = context.NamespaceManager.LookupNamespace (node.Prefix) ?? String.Empty;
  606. foreach (AttrNodeInfo a in attributes)
  607. if (a.Prefix.Length > 0)
  608. a.NS = context.NamespaceManager.LookupNamespace (a.Prefix);
  609. //Console.WriteLine ("[{0}-{1}->{3}/{2:X02}]", node.Prefix, node.LocalName, ident, node.NS);
  610. #else
  611. if (node.Prefix.Length == 0)
  612. foreach (QName q in ns_store)
  613. if (q.Name.Length == 0) {
  614. node.NS = q.Namespace;
  615. break;
  616. }
  617. else if (node.NSSlot >= 0)
  618. FillNamespaceBySlot (node);
  619. foreach (AttrNodeInfo a in attributes) {
  620. if (a.NSSlot >= 0) {
  621. /*
  622. if (a.NSSlot >= ns_store.Count)
  623. throw new XmlException (String.Format ("Binary XML data is not valid. An attribute node has an invalid index at position {0}. Index is {1}.", a.Position, a.NSSlot));
  624. a.NS = ns_store [a.NSSlot].Namespace;
  625. a.Prefix = ns_store [a.NSSlot].Name;
  626. */
  627. FillNamespaceBySlot (a);
  628. }
  629. else if (a.NSSlot == -2) {
  630. a.NS = node.NS;
  631. a.Prefix = node.Prefix;
  632. }
  633. }
  634. #endif
  635. ns_store.Clear ();
  636. ns_dict_store.Clear ();
  637. }
  638. void FillNamespaceBySlot (NodeInfo n)
  639. {
  640. if (n.NSSlot >= ns_store.Count)
  641. throw new XmlException (String.Format ("Binary XML data is not valid. The '{2}' node has an invalid index. Index is {1}. The position in the stream is at {0}.", n.Position, n.NSSlot, n.NodeType));
  642. n.NS = ns_store [n.NSSlot].Namespace;
  643. //n.Prefix = ns_store [n.NSSlot].Name;
  644. }
  645. private void ReadAttribute (byte ident)
  646. {
  647. if (attributes.Count == attr_count)
  648. attributes.Add (new AttrNodeInfo ());
  649. AttrNodeInfo a = attributes [attr_count++];
  650. a.Reset ();
  651. a.Position = source.Position;
  652. switch (ident) {
  653. case BF.AttrString:
  654. a.LocalName = ReadUTF8 ();
  655. break;
  656. case BF.AttrStringPrefix:
  657. a.Prefix = ReadUTF8 ();
  658. a.NSSlot = ns_slot++;
  659. goto case BF.AttrString;
  660. case BF.AttrIndex:
  661. a.DictLocalName = ReadDictName ();
  662. break;
  663. case BF.AttrIndexPrefix:
  664. a.Prefix = ReadUTF8 ();
  665. a.NSSlot = ns_slot++;
  666. goto case BF.AttrIndex;
  667. case BF.GlobalAttrIndex:
  668. a.NSSlot = ns_slot++;
  669. a.DictLocalName = ReadDictName ();
  670. // FIXME: retrieve namespace
  671. break;
  672. case BF.GlobalAttrIndexInElemNS:
  673. a.Prefix = node.Prefix;
  674. a.DictLocalName = ReadDictName ();
  675. a.NSSlot = -2;
  676. break;
  677. }
  678. ReadAttributeValueBinary (a);
  679. }
  680. private void ReadNamespace (byte ident)
  681. {
  682. string prefix = null, ns = null;
  683. XmlDictionaryString dns;
  684. switch (ident) {
  685. case BF.DefaultNSString:
  686. prefix = String.Empty;
  687. ns = ReadUTF8 ();
  688. break;
  689. case BF.PrefixNSString:
  690. prefix = ReadUTF8 ();
  691. ns = ReadUTF8 ();
  692. break;
  693. case BF.DefaultNSIndex:
  694. prefix = String.Empty;
  695. dns = ReadDictName ();
  696. ns_dict_store.Add (ns_store.Count, dns);
  697. ns = dns.Value;
  698. break;
  699. case BF.PrefixNSIndex:
  700. prefix = ReadUTF8 ();
  701. dns = ReadDictName ();
  702. ns_dict_store.Add (ns_store.Count, dns);
  703. ns = dns.Value;
  704. break;
  705. }
  706. ns_store.Add (new QName (prefix, ns));
  707. context.NamespaceManager.AddNamespace (prefix, ns);
  708. }
  709. private void ReadAttributeValueBinary (AttrNodeInfo a)
  710. {
  711. a.ValueIndex = attr_value_count;
  712. do {
  713. if (attr_value_count == attr_values.Count)
  714. attr_values.Add (new NodeInfo (true));
  715. NodeInfo v = attr_values [attr_value_count++];
  716. v.Reset ();
  717. int ident = ReadByteOrError ();
  718. is_next_end_element = ident > 0x80 && (ident & 1) == 1;
  719. ident -= is_next_end_element ? 1 : 0;
  720. if (!ReadTextOrValue ((byte) ident, v, true) || is_next_end_element)
  721. break;
  722. } while (true);
  723. }
  724. private bool ReadTextOrValue (byte ident, NodeInfo node, bool canSkip)
  725. {
  726. node.Value = null;
  727. node.ValueType = ident;
  728. node.NodeType = XmlNodeType.Text;
  729. switch (ident) {
  730. case BF.Zero:
  731. node.TypedValue = 0;
  732. break;
  733. case BF.One:
  734. node.TypedValue = 1;
  735. break;
  736. case BF.BoolFalse:
  737. node.TypedValue = false;
  738. break;
  739. case BF.BoolTrue:
  740. node.TypedValue = true;
  741. break;
  742. case BF.Int8:
  743. node.TypedValue = ReadByteOrError ();
  744. break;
  745. case BF.Int16:
  746. node.TypedValue = source.Reader.ReadInt16 ();
  747. break;
  748. case BF.Int32:
  749. node.TypedValue = source.Reader.ReadInt32 ();
  750. break;
  751. case BF.Int64:
  752. node.TypedValue = source.Reader.ReadInt64 ();
  753. break;
  754. case BF.Single:
  755. node.TypedValue = source.Reader.ReadSingle ();
  756. break;
  757. case BF.Double:
  758. node.TypedValue = source.Reader.ReadDouble ();
  759. break;
  760. case BF.Decimal:
  761. int [] bits = new int [4];
  762. bits [3] = source.Reader.ReadInt32 ();
  763. bits [2] = source.Reader.ReadInt32 ();
  764. bits [0] = source.Reader.ReadInt32 ();
  765. bits [1] = source.Reader.ReadInt32 ();
  766. node.TypedValue = new Decimal (bits);
  767. break;
  768. case BF.DateTime:
  769. node.TypedValue = new DateTime (source.Reader.ReadInt64 ());
  770. break;
  771. //case BF.UniqueId: // identical to .Text
  772. case BF.Base64:
  773. byte [] base64 = new byte [ReadVariantSize ()];
  774. source.Reader.Read (base64, 0, base64.Length);
  775. node.TypedValue = base64;
  776. break;
  777. case BF.Base64Fixed:
  778. base64 = new byte [source.Reader.ReadInt16 ()];
  779. source.Reader.Read (base64, 0, base64.Length);
  780. node.TypedValue = base64;
  781. break;
  782. case BF.TimeSpan:
  783. node.TypedValue = new TimeSpan (source.Reader.ReadInt64 ());
  784. break;
  785. case BF.UniqueIdFromGuid:
  786. byte [] guid = new byte [16];
  787. source.Reader.Read (guid, 0, guid.Length);
  788. node.TypedValue = new UniqueId (new Guid (guid));
  789. break;
  790. case BF.Guid:
  791. guid = new byte [16];
  792. source.Reader.Read (guid, 0, guid.Length);
  793. node.TypedValue = new Guid (guid);
  794. break;
  795. case BF.Text:
  796. node.Value = ReadUTF8 ();
  797. node.NodeType = XmlNodeType.Text;
  798. break;
  799. case BF.EmptyText:
  800. node.Value = String.Empty;
  801. node.NodeType = XmlNodeType.Text;
  802. break;
  803. default:
  804. if (!canSkip)
  805. throw new ArgumentException (String.Format ("Unexpected binary XML data at position {1}: {0:X}", ident + (is_next_end_element ? 1 : 0), source.Position));
  806. next = ident;
  807. return false;
  808. }
  809. return true;
  810. /*
  811. if (ident == 0x8B) {
  812. // empty text
  813. node.Value = String.Empty;
  814. node.NodeType = XmlNodeType.Text;
  815. }
  816. else if (0x83 <= ident && ident <= 0x85 ||
  817. 0x9D <= ident && ident <= 0x9F) {
  818. // text
  819. int sizeSpec = ident > 0x90 ? ident - 0x9D : ident - 0x83;
  820. node.Value = ReadUTF8 (sizeSpec);
  821. node.NodeType = XmlNodeType.Text;
  822. is_next_end_element = ident > 0x90;
  823. }
  824. else {
  825. switch (ident) {
  826. case 0x7B: // byte
  827. case 0x7C: // short
  828. case 0x7D: // int
  829. case 0x7E: // long
  830. case 0x7F: // float
  831. case 0x80: // double
  832. case 0x81: // decimal
  833. case 0x82: // DateTime
  834. case 0x8D: // UniqueId
  835. case 0x8E: // TimeSpan
  836. case 0x8F: // Guid
  837. case 0xA0: // base64Binary
  838. Console.WriteLine ("At position {0}({0:X})", source.Position);
  839. throw new NotImplementedException ();
  840. default:
  841. if (!canSkip)
  842. throw new ArgumentException (String.Format ("Unexpected binary XML data at position {1}: {0:X}", ident, source.Position));
  843. next = ident;
  844. return false;
  845. }
  846. }
  847. return true;
  848. */
  849. }
  850. private int ReadVariantSize ()
  851. {
  852. int size = 0;
  853. // If sizeSpec < 0, then it is variant size specifier.
  854. // Otherwise it is fixed size s = sizeSpec + 1 byte(s).
  855. do {
  856. size <<= 7;
  857. byte got = ReadByteOrError ();
  858. size += got;
  859. if (got < 0x80)
  860. break;
  861. size -= 0x80;
  862. } while (true);
  863. return size;
  864. }
  865. private string ReadUTF8 ()
  866. {
  867. int size = ReadVariantSize ();
  868. if (size == 0)
  869. return String.Empty;
  870. if (tmp_buffer.Length < size) {
  871. int extlen = tmp_buffer.Length * 2;
  872. tmp_buffer = new byte [size < extlen ? extlen : size];
  873. }
  874. size = source.Read (tmp_buffer, 0, size);
  875. return utf8enc.GetString (tmp_buffer, 0, size);
  876. }
  877. private XmlDictionaryString ReadDictName ()
  878. {
  879. int key = ReadVariantSize ();
  880. XmlDictionaryString s;
  881. if ((key & 1) == 1) {
  882. if (session.TryLookup (key >> 1, out s))
  883. return s;
  884. } else {
  885. if (dictionary.TryLookup (key >> 1, out s))
  886. return s;
  887. }
  888. throw new XmlException (String.Format ("Input XML binary stream is invalid. No matching XML dictionary string entry at {0}. Binary stream position at {1}", key, source.Position));
  889. }
  890. private byte ReadByteOrError ()
  891. {
  892. int ret = source.ReadByte ();
  893. if (ret < 0)
  894. throw new XmlException (String.Format ("Unexpected end of binary stream. Position is at {0}", source.Position));
  895. return (byte) ret;
  896. }
  897. public override void ResolveEntity ()
  898. {
  899. throw new NotSupportedException ("this XmlReader does not support ResolveEntity.");
  900. }
  901. public override bool TryGetBase64ContentLength (out int length)
  902. {
  903. length = 0;
  904. if (current.ValueType != BF.Base64 &&
  905. current.ValueType != BF.Base64Fixed)
  906. return false;
  907. length = ((byte []) current.TypedValue).Length;
  908. return true;
  909. }
  910. public override string ReadContentAsString ()
  911. {
  912. string value = Value;
  913. do {
  914. switch (NodeType) {
  915. case XmlNodeType.Element:
  916. case XmlNodeType.EndElement:
  917. return value;
  918. case XmlNodeType.Text:
  919. value += Value;
  920. break;
  921. }
  922. } while (Read ());
  923. return value;
  924. }
  925. #region read typed content
  926. public override int ReadContentAsInt ()
  927. {
  928. int ret = GetIntValue ();
  929. Read ();
  930. return ret;
  931. }
  932. int GetIntValue ()
  933. {
  934. switch (node.ValueType) {
  935. case BF.Zero:
  936. return 0;
  937. case BF.One:
  938. return 1;
  939. case BF.Int8:
  940. return (byte) current.TypedValue;
  941. case BF.Int16:
  942. return (short) current.TypedValue;
  943. case BF.Int32:
  944. return (int) current.TypedValue;
  945. }
  946. throw new InvalidOperationException ("Current content is not an integer");
  947. }
  948. public override long ReadContentAsLong ()
  949. {
  950. if (node.ValueType == BF.Int64) {
  951. long v = (long) current.TypedValue;
  952. Read ();
  953. return v;
  954. }
  955. return ReadContentAsInt ();
  956. }
  957. public override float ReadContentAsFloat ()
  958. {
  959. if (node.ValueType != BF.Single)
  960. throw new InvalidOperationException ("Current content is not a single");
  961. float v = (float) current.TypedValue;
  962. Read ();
  963. return v;
  964. }
  965. public override double ReadContentAsDouble ()
  966. {
  967. if (node.ValueType != BF.Double)
  968. throw new InvalidOperationException ("Current content is not a double");
  969. double v = (double) current.TypedValue;
  970. Read ();
  971. return v;
  972. }
  973. // FIXME: this is not likely to consume sequential base64 nodes.
  974. public override byte [] ReadContentAsBase64 ()
  975. {
  976. byte [] ret = null;
  977. if (node.ValueType != BF.Base64 &&
  978. node.ValueType != BF.Base64Fixed)
  979. throw new InvalidOperationException ("Current content is not base64");
  980. while (NodeType == XmlNodeType.Text &&
  981. (node.ValueType == BF.Base64 || node.ValueType == BF.Base64Fixed)) {
  982. if (ret == null)
  983. ret = (byte []) node.TypedValue;
  984. else {
  985. byte [] tmp = (byte []) node.TypedValue;
  986. byte [] tmp2 = new byte [ret.Length + tmp.Length];
  987. Array.Copy (ret, tmp2, ret.Length);
  988. Array.Copy (tmp, 0, tmp2, ret.Length, tmp.Length);
  989. ret = tmp2;
  990. }
  991. Read ();
  992. //MoveToContent ();
  993. }
  994. return ret;
  995. }
  996. public override Guid ReadContentAsGuid ()
  997. {
  998. if (node.ValueType != BF.Guid)
  999. throw new InvalidOperationException ("Current content is not a Guid");
  1000. Guid ret = (Guid) node.TypedValue;
  1001. Read ();
  1002. return ret;
  1003. }
  1004. public override UniqueId ReadContentAsUniqueId ()
  1005. {
  1006. switch (node.ValueType) {
  1007. case BF.Text:
  1008. UniqueId ret = new UniqueId (node.Value);
  1009. Read ();
  1010. return ret;
  1011. case BF.UniqueIdFromGuid:
  1012. ret = (UniqueId) node.TypedValue;
  1013. Read ();
  1014. return ret;
  1015. default:
  1016. throw new InvalidOperationException ("Current content is not a UniqueId");
  1017. }
  1018. }
  1019. #endregion
  1020. }
  1021. }