XmlInputStream.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // System.Xml.XmlInputStream
  3. // encoding-specification-wise XML input stream and reader
  4. //
  5. // Author:
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C)2003 Atsushi Enomoto
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Text;
  33. using System.Xml;
  34. namespace System.Xml
  35. {
  36. #region XmlStreamReader
  37. internal class XmlStreamReader : StreamReader
  38. {
  39. XmlInputStream input;
  40. XmlStreamReader (XmlInputStream input)
  41. : base (input, input.ActualEncoding != null ? input.ActualEncoding : XmlInputStream.StrictUTF8)
  42. {
  43. this.input = input;
  44. }
  45. public XmlStreamReader (Stream input)
  46. : this (new XmlInputStream (input))
  47. {
  48. }
  49. public override void Close ()
  50. {
  51. this.input.Close ();
  52. }
  53. protected override void Dispose (bool disposing)
  54. {
  55. base.Dispose (disposing);
  56. if (disposing) {
  57. Close ();
  58. }
  59. }
  60. }
  61. #endregion
  62. class XmlInputStream : Stream
  63. {
  64. public static readonly Encoding StrictUTF8;
  65. static XmlInputStream ()
  66. {
  67. StrictUTF8 = new UTF8Encoding (false, true);
  68. }
  69. Encoding enc;
  70. Stream stream;
  71. byte[] buffer;
  72. int bufLength;
  73. int bufPos;
  74. static XmlException encodingException = new XmlException ("invalid encoding specification.");
  75. public XmlInputStream (Stream stream)
  76. {
  77. Initialize (stream);
  78. }
  79. private void Initialize (Stream stream)
  80. {
  81. buffer = new byte [64];
  82. this.stream = stream;
  83. enc = StrictUTF8; // Default to UTF8 if we can't guess it
  84. bufLength = stream.Read (buffer, 0, buffer.Length);
  85. if (bufLength == -1 || bufLength == 0) {
  86. return;
  87. }
  88. int c = ReadByteSpecial ();
  89. switch (c) {
  90. case 0xFF:
  91. c = ReadByteSpecial ();
  92. if (c == 0xFE) {
  93. // BOM-ed little endian utf-16
  94. enc = Encoding.Unicode;
  95. } else {
  96. // It doesn't start from "<?xml" then its encoding is utf-8
  97. bufPos = 0;
  98. }
  99. break;
  100. case 0xFE:
  101. c = ReadByteSpecial ();
  102. if (c == 0xFF) {
  103. // BOM-ed big endian utf-16
  104. enc = Encoding.BigEndianUnicode;
  105. return;
  106. } else {
  107. // It doesn't start from "<?xml" then its encoding is utf-8
  108. bufPos = 0;
  109. }
  110. break;
  111. case 0xEF:
  112. c = ReadByteSpecial ();
  113. if (c == 0xBB) {
  114. c = ReadByteSpecial ();
  115. if (c != 0xBF) {
  116. bufPos = 0;
  117. }
  118. } else {
  119. buffer [--bufPos] = 0xEF;
  120. }
  121. break;
  122. case '<':
  123. // try to get encoding name from XMLDecl.
  124. if (bufLength >= 5 && Encoding.ASCII.GetString (buffer, 1, 4) == "?xml") {
  125. bufPos += 4;
  126. c = SkipWhitespace ();
  127. // version. It is optional here.
  128. if (c == 'v') {
  129. while (c >= 0) {
  130. c = ReadByteSpecial ();
  131. if (c == '0') { // 0 of 1.0
  132. ReadByteSpecial ();
  133. break;
  134. }
  135. }
  136. c = SkipWhitespace ();
  137. }
  138. if (c == 'e') {
  139. int remaining = bufLength - bufPos;
  140. if (remaining >= 7 && Encoding.ASCII.GetString(buffer, bufPos, 7) == "ncoding") {
  141. bufPos += 7;
  142. c = SkipWhitespace();
  143. if (c != '=')
  144. throw encodingException;
  145. c = SkipWhitespace ();
  146. int quoteChar = c;
  147. StringBuilder sb = new StringBuilder ();
  148. while (true) {
  149. c = ReadByteSpecial ();
  150. if (c == quoteChar)
  151. break;
  152. else if (c < 0)
  153. throw encodingException;
  154. sb.Append ((char) c);
  155. }
  156. string encodingName = sb.ToString ();
  157. if (!XmlChar.IsValidIANAEncoding (encodingName))
  158. throw encodingException;
  159. enc = Encoding.GetEncoding (encodingName);
  160. }
  161. }
  162. }
  163. bufPos = 0;
  164. break;
  165. default:
  166. bufPos = 0;
  167. break;
  168. }
  169. }
  170. // Just like readbyte, but grows the buffer too.
  171. int ReadByteSpecial ()
  172. {
  173. if (bufLength > bufPos)
  174. return buffer [bufPos++];
  175. byte [] newbuf = new byte [buffer.Length * 2];
  176. Buffer.BlockCopy (buffer, 0, newbuf, 0, bufLength);
  177. int nbytes = stream.Read (newbuf, bufLength, buffer.Length);
  178. if (nbytes == -1 || nbytes == 0)
  179. return -1;
  180. bufLength += nbytes;
  181. buffer = newbuf;
  182. return buffer [bufPos++];
  183. }
  184. // skips whitespace and returns misc char that was read from stream
  185. private int SkipWhitespace ()
  186. {
  187. int c;
  188. while (true) {
  189. c = ReadByteSpecial ();
  190. switch ((char) c) {
  191. case '\r': goto case ' ';
  192. case '\n': goto case ' ';
  193. case '\t': goto case ' ';
  194. case ' ':
  195. continue;
  196. default:
  197. return c;
  198. }
  199. }
  200. throw new InvalidOperationException ();
  201. }
  202. public Encoding ActualEncoding {
  203. get { return enc; }
  204. }
  205. #region Public Overrides
  206. public override bool CanRead {
  207. get {
  208. if (bufLength > bufPos)
  209. return true;
  210. else
  211. return stream.CanRead;
  212. }
  213. }
  214. // FIXME: It should support base stream's CanSeek.
  215. public override bool CanSeek {
  216. get { return false; } // stream.CanSeek; }
  217. }
  218. public override bool CanWrite {
  219. get { return false; }
  220. }
  221. public override long Length {
  222. get {
  223. return stream.Length;
  224. }
  225. }
  226. public override long Position {
  227. get {
  228. return stream.Position - bufLength + bufPos;
  229. }
  230. set {
  231. if(value < bufLength)
  232. bufPos = (int)value;
  233. else
  234. stream.Position = value - bufLength;
  235. }
  236. }
  237. public override void Close ()
  238. {
  239. stream.Close ();
  240. }
  241. public override void Flush ()
  242. {
  243. stream.Flush ();
  244. }
  245. public override int Read (byte[] buffer, int offset, int count)
  246. {
  247. int ret;
  248. if (count <= bufLength - bufPos) { // all from buffer
  249. Buffer.BlockCopy (this.buffer, bufPos, buffer, offset, count);
  250. bufPos += count;
  251. ret = count;
  252. } else {
  253. int bufRest = bufLength - bufPos;
  254. if (bufLength > bufPos) {
  255. Buffer.BlockCopy (this.buffer, bufPos, buffer, offset, bufRest);
  256. bufPos += bufRest;
  257. }
  258. ret = bufRest +
  259. stream.Read (buffer, offset + bufRest, count - bufRest);
  260. }
  261. return ret;
  262. }
  263. public override int ReadByte ()
  264. {
  265. if (bufLength > bufPos) {
  266. return buffer [bufPos++];
  267. }
  268. return stream.ReadByte ();
  269. }
  270. public override long Seek (long offset, System.IO.SeekOrigin origin)
  271. {
  272. int bufRest = bufLength - bufPos;
  273. if (origin == SeekOrigin.Current)
  274. if (offset < bufRest)
  275. return buffer [bufPos + offset];
  276. else
  277. return stream.Seek (offset - bufRest, origin);
  278. else
  279. return stream.Seek (offset, origin);
  280. }
  281. public override void SetLength (long value)
  282. {
  283. stream.SetLength (value);
  284. }
  285. public override void Write (byte[] buffer, int offset, int count)
  286. {
  287. throw new NotSupportedException ();
  288. }
  289. #endregion
  290. }
  291. }