XmlBinaryFormat.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // XmlBinaryFormat.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 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. namespace System.Xml
  31. {
  32. internal class XmlBinaryFormat
  33. {
  34. public const byte EndElement = 0x01;
  35. public const byte Comment = 0x02;
  36. public const byte Array = 0x03;
  37. public const byte AttrString = 0x04;
  38. public const byte AttrStringPrefix = 0x05;
  39. public const byte AttrIndex = 0x06;
  40. public const byte AttrIndexPrefix = 0x07;
  41. public const byte DefaultNSString = 0x08;
  42. public const byte PrefixNSString = 0x09;
  43. public const byte DefaultNSIndex = 0x0A;
  44. public const byte PrefixNSIndex = 0x0B;
  45. public const byte PrefixNAttrIndexStart = 0x0C;
  46. public const byte PrefixNAttrIndexEnd = 0x0C + 26 - 1;
  47. public const byte PrefixNAttrStringStart = 0x26;
  48. public const byte PrefixNAttrStringEnd = 0x26 + 26 - 1;
  49. public const byte ElemString = 0x40;
  50. public const byte ElemStringPrefix = 0x41;
  51. public const byte ElemIndex = 0x42;
  52. public const byte ElemIndexPrefix = 0x43;
  53. public const byte PrefixNElemIndexStart = 0x44;
  54. public const byte PrefixNElemIndexEnd = 0x44 + 26 - 1;
  55. public const byte PrefixNElemStringStart = 0x5E;
  56. public const byte PrefixNElemStringEnd = 0x5E + 26 - 1;
  57. public const byte Zero = 0x80;
  58. public const byte One = 0x82;
  59. public const byte BoolFalse = 0x84;
  60. public const byte BoolTrue = 0x86;
  61. public const byte Int8 = 0x88;
  62. public const byte Int16 = 0x8A;
  63. public const byte Int32 = 0x8C;
  64. public const byte Int64 = 0x8E;
  65. public const byte Single = 0x90;
  66. public const byte Double = 0x92;
  67. public const byte Decimal = 0x94;
  68. public const byte DateTime = 0x96;
  69. public const byte Chars8 = 0x98;
  70. public const byte Chars16 = 0x9A;
  71. public const byte Chars32 = 0x9C;
  72. public const byte Bytes8 = 0x9E;
  73. public const byte Bytes16 = 0xA0;
  74. public const byte Bytes32 = 0xA2;
  75. public const byte EmptyText = 0xA8;
  76. public const byte TextIndex = 0xAA;
  77. public const byte UniqueId = 0xAC;
  78. public const byte TimeSpan = 0xAE;
  79. public const byte Guid = 0xB0;
  80. public const byte UInt64 = 0xB2;
  81. public const byte Bool = 0xB4; // e.g. for typed array
  82. public const byte Utf16_8 = 0xB6;
  83. public const byte Utf16_16 = 0xB8;
  84. public const byte Utf16_32 = 0xBA;
  85. public const byte QNameIndex = 0xBC;
  86. }
  87. /* Binary Format (incomplete):
  88. Literal strings are represented as UTF-8 string, with a length
  89. prefixed to the string itself.
  90. Key indices are based on the rules below:
  91. - dictionary strings which can be found in IXmlDictionary are
  92. doubled its Key. e.g. if the string.Key is 4, then the
  93. output is 8.
  94. - dictionary strings which cannot be found in IXmlDictionary
  95. are stored in the XmlBinaryWriterSession, and its output
  96. number is doubled + 1 e.g. if the string is the first
  97. non-dictionary entry, then the output is 1, and 7 for the
  98. fourth one.
  99. - When the index goes beyond 128, then it becomes 2 bytes,
  100. where the first byte becomes 0x80 + idx % 0x80 and
  101. the second byte becomes idx / 0x80.
  102. Below are operations. Prefixes are always raw strings.
  103. $string is length-prefixed string. @index is index as
  104. described above. [value] is length-prefixed raw array.
  105. // 2009-03-25: now that the binary format is open under OSP
  106. // [MC-NBFX], I have added some notes beyond current
  107. // implementation status (marked as TODO).
  108. 01 : EndElement
  109. 02 $value : Comment
  110. 03 : array
  111. 04 $name : local attribute by string
  112. 05 $prefix $name : global attribute by string
  113. 06 @name : local attribute by index
  114. 07 $prefix @name : global attribute by index
  115. 08 $name : default namespace by string
  116. 09 $prefix $name : prefixed namespace by string
  117. 0A @name : default namespace by index
  118. 0B $prefix @name : prefixed namespace by index
  119. 0C @name : global attribute by index,
  120. ... 0x25 : in current element's namespace
  121. 26 ... 0x3F : attributes with prefix
  122. 40 $name : element w/o namespace by string
  123. 41 $prefix $name : element with namespace by string
  124. 42 @name : element w/o namespace by index
  125. 43 $prefix @name : element with namespace by index
  126. 44 @name : global element by index,
  127. ... 0x5D : in current element's namespace
  128. 5E ... 0x77 : elements with prefix
  129. 98 $value : text/cdata/chars
  130. 99 $value : text/cdata/chars + EndElement
  131. FIXME: Below are not implemented:
  132. (Uri is simply 98, QName is 98 '{' ns '}' 98 name)
  133. Combined EndElement for below are supported:
  134. 80 : 0 (integer)
  135. 82 : 1 (integer)
  136. 84 : false (bool)
  137. 86 : true (bool)
  138. 88 : 1-byte integer
  139. 8A : 2-bytes integer
  140. 8C : 4-bytes integer
  141. 8E : 8-bytes integer
  142. 90 : single
  143. 92 : double
  144. 94 : decimal
  145. 96 : DateTime
  146. 98 : chars8
  147. 9A : chars16
  148. 9C : chars32
  149. 9E : bytes8 (base64)
  150. A0 : bytes16 (base64)
  151. A2 : bytes32 (base64)
  152. A4 : TODO: start of list
  153. A6 : TODO: end of list
  154. A8 : empty text
  155. AA : text index
  156. AC : UniqueId (IsGuid = true)
  157. AE : TimeSpan
  158. B0 : UUID
  159. B2 : UInt64
  160. B4 : bool text
  161. B6 : utf16_8
  162. B8 : utf16_16
  163. BA : utf16_32
  164. BC : QName index
  165. Error: PIs, doctype
  166. Ignored: XMLdecl
  167. */
  168. }