XmlInputStream.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 : Encoding.UTF8)
  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. Encoding enc;
  65. Stream stream;
  66. byte[] buffer;
  67. int bufLength;
  68. int bufPos;
  69. static XmlException encodingException = new XmlException ("invalid encoding specification.");
  70. public XmlInputStream (Stream stream)
  71. {
  72. Initialize (stream);
  73. }
  74. private void Initialize (Stream stream)
  75. {
  76. buffer = new byte [1024];
  77. this.stream = stream;
  78. enc = Encoding.UTF8; // Default to UTF8 if we can't guess it
  79. bufLength = stream.Read (buffer, 0, buffer.Length);
  80. if (bufLength == -1 || bufLength == 0) {
  81. return;
  82. }
  83. int c = ReadByteSpecial ();
  84. switch (c) {
  85. case 0xFF:
  86. c = ReadByteSpecial ();
  87. if (c == 0xFE) {
  88. // BOM-ed little endian utf-16
  89. enc = Encoding.Unicode;
  90. } else {
  91. // It doesn't start from "<?xml" then its encoding is utf-8
  92. bufPos = 0;
  93. }
  94. break;
  95. case 0xFE:
  96. c = ReadByteSpecial ();
  97. if (c == 0xFF) {
  98. // BOM-ed big endian utf-16
  99. enc = Encoding.BigEndianUnicode;
  100. return;
  101. } else {
  102. // It doesn't start from "<?xml" then its encoding is utf-8
  103. bufPos = 0;
  104. }
  105. break;
  106. case 0xEF:
  107. c = ReadByteSpecial ();
  108. if (c == 0xBB) {
  109. c = ReadByteSpecial ();
  110. if (c != 0xBF) {
  111. bufPos = 0;
  112. }
  113. } else {
  114. buffer [--bufPos] = 0xEF;
  115. }
  116. break;
  117. case '<':
  118. // try to get encoding name from XMLDecl.
  119. if (bufLength >= 5 && Encoding.ASCII.GetString (buffer, 1, 4) == "?xml") {
  120. bufPos += 4;
  121. c = SkipWhitespace ();
  122. // version. It is optional here.
  123. if (c == 'v') {
  124. while (c >= 0) {
  125. c = ReadByteSpecial ();
  126. if (c == '0') { // 0 of 1.0
  127. ReadByteSpecial ();
  128. break;
  129. }
  130. }
  131. c = SkipWhitespace ();
  132. }
  133. if (c == 'e') {
  134. int remaining = bufLength - bufPos;
  135. if (remaining >= 7 && Encoding.ASCII.GetString(buffer, bufPos, 7) == "ncoding") {
  136. bufPos += 7;
  137. c = SkipWhitespace();
  138. if (c != '=')
  139. throw encodingException;
  140. c = SkipWhitespace ();
  141. int quoteChar = c;
  142. StringBuilder sb = new StringBuilder ();
  143. while (true) {
  144. c = ReadByteSpecial ();
  145. if (c == quoteChar)
  146. break;
  147. else if (c < 0)
  148. throw encodingException;
  149. sb.Append ((char) c);
  150. }
  151. string encodingName = sb.ToString ();
  152. if (!XmlChar.IsValidIANAEncoding (encodingName))
  153. throw encodingException;
  154. enc = Encoding.GetEncoding (encodingName);
  155. }
  156. }
  157. }
  158. bufPos = 0;
  159. break;
  160. default:
  161. bufPos = 0;
  162. break;
  163. }
  164. }
  165. // Just like readbyte, but grows the buffer too.
  166. int ReadByteSpecial ()
  167. {
  168. if (bufLength > bufPos)
  169. return buffer [bufPos++];
  170. byte [] newbuf = new byte [buffer.Length * 2];
  171. Buffer.BlockCopy (buffer, 0, newbuf, 0, bufLength);
  172. int nbytes = stream.Read (newbuf, bufLength, buffer.Length);
  173. if (nbytes == -1 || nbytes == 0)
  174. return -1;
  175. bufLength += nbytes;
  176. buffer = newbuf;
  177. return buffer [bufPos++];
  178. }
  179. // skips whitespace and returns misc char that was read from stream
  180. private int SkipWhitespace ()
  181. {
  182. int c;
  183. while (true) {
  184. c = ReadByteSpecial ();
  185. switch ((char) c) {
  186. case '\r': goto case ' ';
  187. case '\n': goto case ' ';
  188. case '\t': goto case ' ';
  189. case ' ':
  190. continue;
  191. default:
  192. return c;
  193. }
  194. }
  195. throw new InvalidOperationException ();
  196. }
  197. public Encoding ActualEncoding {
  198. get { return enc; }
  199. }
  200. #region Public Overrides
  201. public override bool CanRead {
  202. get {
  203. if (bufLength > bufPos)
  204. return true;
  205. else
  206. return stream.CanRead;
  207. }
  208. }
  209. // FIXME: It should support base stream's CanSeek.
  210. public override bool CanSeek {
  211. get { return false; } // stream.CanSeek; }
  212. }
  213. public override bool CanWrite {
  214. get { return false; }
  215. }
  216. public override long Length {
  217. get {
  218. return stream.Length;
  219. }
  220. }
  221. public override long Position {
  222. get {
  223. return stream.Position - bufLength + bufPos;
  224. }
  225. set {
  226. if(value < bufLength)
  227. bufPos = (int)value;
  228. else
  229. stream.Position = value - bufLength;
  230. }
  231. }
  232. public override void Close ()
  233. {
  234. stream.Close ();
  235. }
  236. public override void Flush ()
  237. {
  238. stream.Flush ();
  239. }
  240. public override int Read (byte[] buffer, int offset, int count)
  241. {
  242. int ret;
  243. if (count <= bufLength - bufPos) { // all from buffer
  244. Array.Copy (this.buffer, bufPos, buffer, offset, count);
  245. bufPos += count;
  246. ret = count;
  247. } else {
  248. int bufRest = bufLength - bufPos;
  249. if (bufLength > bufPos) {
  250. Array.Copy (this.buffer, bufPos, buffer, offset, bufRest);
  251. bufPos += bufRest;
  252. }
  253. ret = bufRest +
  254. stream.Read (buffer, offset + bufRest, count - bufRest);
  255. }
  256. return ret;
  257. }
  258. public override int ReadByte ()
  259. {
  260. if (bufLength > bufPos) {
  261. return buffer [bufPos++];
  262. }
  263. return stream.ReadByte ();
  264. }
  265. public override long Seek (long offset, System.IO.SeekOrigin origin)
  266. {
  267. int bufRest = bufLength - bufPos;
  268. if (origin == SeekOrigin.Current)
  269. if (offset < bufRest)
  270. return buffer [bufPos + offset];
  271. else
  272. return stream.Seek (offset - bufRest, origin);
  273. else
  274. return stream.Seek (offset, origin);
  275. }
  276. public override void SetLength (long value)
  277. {
  278. stream.SetLength (value);
  279. }
  280. public override void Write (byte[] buffer, int offset, int count)
  281. {
  282. throw new NotSupportedException ();
  283. }
  284. #endregion
  285. }
  286. }