1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
//
// System.Xml.XmlReaderBinarySupport.cs
//
// Author:
// Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
//
// (C)2004 Novell Inc,
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Text;
namespace System.Xml
{
internal class XmlReaderBinarySupport
{
public delegate int CharGetter (
char [] buffer, int offset, int length);
public enum CommandState {
None,
ReadElementContentAsBase64,
ReadContentAsBase64,
ReadElementContentAsBinHex,
ReadContentAsBinHex
}
public XmlReaderBinarySupport (XmlReader reader)
{
this.reader = reader;
Reset ();
}
XmlReader reader;
CharGetter getter;
byte [] base64Cache = new byte [3];
int base64CacheStartsAt;
CommandState state;
StringBuilder textCache;
bool hasCache;
bool dontReset;
public CharGetter Getter {
get { return getter; }
set { getter = value; }
}
public void Reset ()
{
if (!dontReset) {
dontReset = true;
if (hasCache) {
switch (reader.NodeType) {
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
reader.Read ();
break;
}
switch (state) {
case CommandState.ReadElementContentAsBase64:
case CommandState.ReadElementContentAsBinHex:
reader.Read ();
break;
}
}
base64CacheStartsAt = -1;
state = CommandState.None;
hasCache = false;
dontReset = false;
}
}
InvalidOperationException StateError (CommandState action)
{
return new InvalidOperationException (
String.Format ("Invalid attempt to read binary content by {0}, while once binary reading was started by {1}", action, state));
}
private void CheckState (bool element, CommandState action)
{
if (state == CommandState.None) {
if (textCache == null)
textCache = new StringBuilder ();
else
textCache.Length = 0;
if (action == CommandState.None)
return; // for ReadValueChunk()
if (reader.ReadState != ReadState.Interactive)
return;
switch (reader.NodeType) {
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
if (!element) {
state = action;
return;
}
break;
case XmlNodeType.Element:
if (element) {
if (!reader.IsEmptyElement)
reader.Read ();
state = action;
return;
}
break;
}
throw new XmlException ((element ?
"Reader is not positioned on an element."
: "Reader is not positioned on a text node."));
}
if (state == action)
return;
throw StateError (action);
}
public int ReadElementContentAsBase64 (
byte [] buffer, int offset, int length)
{
CheckState (true, CommandState.ReadElementContentAsBase64);
return ReadBase64 (buffer, offset, length);
}
public int ReadContentAsBase64 (
byte [] buffer, int offset, int length)
{
CheckState (false, CommandState.ReadContentAsBase64);
return ReadBase64 (buffer, offset, length);
}
public int ReadElementContentAsBinHex (
byte [] buffer, int offset, int length)
{
CheckState (true, CommandState.ReadElementContentAsBinHex);
return ReadBinHex (buffer, offset, length);
}
public int ReadContentAsBinHex (
byte [] buffer, int offset, int length)
{
CheckState (false, CommandState.ReadContentAsBinHex);
return ReadBinHex (buffer, offset, length);
}
public int ReadBase64 (byte [] buffer, int offset, int length)
{
if (offset < 0)
throw CreateArgumentOutOfRangeException ("offset", offset, "Offset must be non-negative integer.");
else if (length < 0)
throw CreateArgumentOutOfRangeException ("length", length, "Length must be non-negative integer.");
else if (buffer.Length < offset + length)
throw new ArgumentOutOfRangeException ("buffer length is smaller than the sum of offset and length.");
if (reader.IsEmptyElement)
return 0;
if (length == 0) // It does not raise an error.
return 0;
int bufIndex = offset;
int bufLast = offset + length;
if (base64CacheStartsAt >= 0) {
for (int i = base64CacheStartsAt; i < 3; i++) {
buffer [bufIndex++] = base64Cache [base64CacheStartsAt++];
if (bufIndex == bufLast)
return bufLast - offset;
}
}
for (int i = 0; i < 3; i++)
base64Cache [i] = 0;
base64CacheStartsAt = -1;
int max = (int) System.Math.Ceiling (4.0 / 3 * length);
int additional = max % 4;
if (additional > 0)
max += 4 - additional;
char [] chars = new char [max];
int charsLength = getter != null ?
getter (chars, 0, max) :
ReadValueChunk (chars, 0, max);
byte b = 0;
byte work = 0;
for (int i = 0; i < charsLength - 3; i++) {
if ((i = SkipIgnorableBase64Chars (chars, charsLength, i)) == charsLength)
break;
b = (byte) (GetBase64Byte (chars [i]) << 2);
if (bufIndex < bufLast)
buffer [bufIndex] = b;
else if (b != 0) {
if (base64CacheStartsAt < 0)
base64CacheStartsAt = 0;
base64Cache [0] = b;
}
// charsLength mod 4 might not equals to 0.
if (++i == charsLength)
break;
if ((i = SkipIgnorableBase64Chars (chars, charsLength, i)) == charsLength)
break;
b = GetBase64Byte (chars [i]);
work = (byte) (b >> 4);
if (bufIndex < bufLast) {
buffer [bufIndex] += work;
bufIndex++;
}
else if (work != 0) {
if (base64CacheStartsAt < 0)
base64CacheStartsAt = 0;
base64Cache [0] += work;
}
work = (byte) ((b & 0xf) << 4);
if (bufIndex < bufLast) {
buffer [bufIndex] = work;
}
else if (work != 0) {
if (base64CacheStartsAt < 0)
base64CacheStartsAt = 1;
base64Cache [1] = work;
}
if (++i == charsLength)
break;
if ((i = SkipIgnorableBase64Chars (chars, charsLength, i)) == charsLength)
break;
b = GetBase64Byte (chars [i]);
work = (byte) (b >> 2);
if (bufIndex < bufLast) {
buffer [bufIndex] += work;
bufIndex++;
}
else if (work != 0) {
if (base64CacheStartsAt < 0)
base64CacheStartsAt = 1;
base64Cache [1] += work;
}
work = (byte) ((b & 3) << 6);
if (bufIndex < bufLast)
buffer [bufIndex] = work;
else if (work != 0) {
if (base64CacheStartsAt < 0)
base64CacheStartsAt = 2;
base64Cache [2] = work;
}
if (++i == charsLength)
break;
if ((i = SkipIgnorableBase64Chars (chars, charsLength, i)) == charsLength)
break;
work = GetBase64Byte (chars [i]);
if (bufIndex < bufLast) {
buffer [bufIndex] += work;
bufIndex++;
}
else if (work != 0) {
if (base64CacheStartsAt < 0)
base64CacheStartsAt = 2;
base64Cache [2] += work;
}
}
int ret = System.Math.Min (bufLast - offset, bufIndex - offset);
if (ret < length && charsLength > 0)
return ret + ReadBase64 (buffer, offset + ret, length - ret);
else
return ret;
}
// Since ReadBase64() is processed for every 4 chars, it does
// not handle '=' here.
private byte GetBase64Byte (char ch)
{
switch (ch) {
case '+':
return 62;
case '/':
return 63;
default:
if (ch >= 'A' && ch <= 'Z')
return (byte) (ch - 'A');
else if (ch >= 'a' && ch <= 'z')
return (byte) (ch - 'a' + 26);
else if (ch >= '0' && ch <= '9')
return (byte) (ch - '0' + 52);
else
throw new XmlException ("Invalid Base64 character was found.");
}
}
private int SkipIgnorableBase64Chars (char [] chars, int charsLength, int i)
{
while (chars [i] == '=' || XmlChar.IsWhitespace (chars [i]))
if (charsLength == ++i)
break;
return i;
}
static Exception CreateArgumentOutOfRangeException (string name, object value, string message)
{
return new ArgumentOutOfRangeException (
#if !NET_2_1
name, value,
#endif
message);
}
public int ReadBinHex (byte [] buffer, int offset, int length)
{
if (offset < 0)
throw CreateArgumentOutOfRangeException ("offset", offset, "Offset must be non-negative integer.");
else if (length < 0)
throw CreateArgumentOutOfRangeException ("length", length, "Length must be non-negative integer.");
else if (buffer.Length < offset + length)
throw new ArgumentOutOfRangeException ("buffer length is smaller than the sum of offset and length.");
if (length == 0)
return 0;
char [] chars = new char [length * 2];
int charsLength = getter != null ?
getter (chars, 0, length * 2) :
ReadValueChunk (chars, 0, length * 2);
return XmlConvert.FromBinHexString (chars, offset, charsLength, buffer);
}
public int ReadValueChunk (
char [] buffer, int offset, int length)
{
CommandState backup = state;
if (state == CommandState.None)
CheckState (false, CommandState.None);
if (offset < 0)
throw CreateArgumentOutOfRangeException ("offset", offset, "Offset must be non-negative integer.");
else if (length < 0)
throw CreateArgumentOutOfRangeException ("length", length, "Length must be non-negative integer.");
else if (buffer.Length < offset + length)
throw new ArgumentOutOfRangeException ("buffer length is smaller than the sum of offset and length.");
if (length == 0)
return 0;
if (!hasCache) {
if (reader.IsEmptyElement)
return 0;
}
bool loop = true;
while (loop && textCache.Length < length) {
switch (reader.NodeType) {
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
if (hasCache) {
switch (reader.NodeType) {
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
Read ();
break;
default:
loop = false;
break;
}
}
textCache.Append (reader.Value);
hasCache = true;
break;
default:
loop = false;
break;
}
}
state = backup;
int min = textCache.Length;
if (min > length)
min = length;
string str = textCache.ToString (0, min);
textCache.Remove (0, str.Length);
str.CopyTo (0, buffer, offset, str.Length);
if (min < length && loop)
return min + ReadValueChunk (buffer, offset + min, length - min);
else
return min;
}
private bool Read ()
{
dontReset = true;
bool b = reader.Read ();
dontReset = false;
return b;
}
}
}
|