XmlSignatureStreamReader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // XmlSignatureStreamReader.cs: Wrap TextReader and eliminate \r
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C) 2005 Novell Inc.
  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. //
  30. // Use it to distinguish 
 and \r. \r is removed, while 
 is not.
  31. //
  32. //
  33. using System;
  34. using System.IO;
  35. using System.Runtime.InteropServices;
  36. namespace System.Security.Cryptography.Xml
  37. {
  38. internal class XmlSignatureStreamReader : TextReader
  39. {
  40. TextReader source;
  41. int cache = int.MinValue;
  42. public XmlSignatureStreamReader (TextReader input)
  43. {
  44. source =input;
  45. }
  46. public override void Close ()
  47. {
  48. source.Close ();
  49. }
  50. public override int Peek ()
  51. {
  52. // If source TextReader does not support Peek(),
  53. // it does not support too. Or it just returns EOF.
  54. if (source.Peek () == -1)
  55. return -1;
  56. if (cache != int.MinValue)
  57. return cache;
  58. cache = source.Read ();
  59. if (cache != '\r')
  60. return cache;
  61. // cache must be '\r' here.
  62. if (source.Peek () != '\n')
  63. return '\r';
  64. // Now Peek() returns '\n', so clear cache.
  65. cache = int.MinValue;
  66. return '\n';
  67. }
  68. public override int Read ()
  69. {
  70. if (cache != int.MinValue) {
  71. int ret = cache;
  72. cache = int.MinValue;
  73. return ret;
  74. }
  75. int i = source.Read ();
  76. if (i != '\r')
  77. return i;
  78. // read one more char (after '\r')
  79. cache = source.Read ();
  80. if (cache != '\n')
  81. return '\r';
  82. cache = int.MinValue;
  83. return '\n';
  84. }
  85. public override int ReadBlock (
  86. [In, Out] char [] buffer, int index, int count)
  87. {
  88. char [] tmp = new char [count];
  89. source.ReadBlock (tmp, 0, count);
  90. int j = index;
  91. for (int i = 0; i < count; j++) {
  92. if (tmp [i] == '\r') {
  93. if (++i < tmp.Length && tmp [i] == '\n')
  94. buffer [j] = tmp [i++];
  95. else
  96. buffer [j] = '\r';
  97. }
  98. else
  99. buffer [j] = tmp [i];
  100. }
  101. while (j < count) {
  102. int d = Read ();
  103. if (d < 0)
  104. break;
  105. buffer [j++] = (char) d;
  106. }
  107. return j;
  108. }
  109. // I have no idea what to do here, but I don't think it
  110. // makes sense.
  111. public override string ReadLine ()
  112. {
  113. return source.ReadLine ();
  114. }
  115. public override string ReadToEnd ()
  116. {
  117. return source.ReadToEnd ().Replace ("\r\n", "\n");
  118. }
  119. }
  120. }