/
XmlDictionaryReaderQuotas.cs
186 lines (165 loc) · 6.27 KB
/
XmlDictionaryReaderQuotas.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.Serialization;
using System.ComponentModel;
namespace System.Xml
{
[Flags]
public enum XmlDictionaryReaderQuotaTypes
{
MaxDepth = 0x01,
MaxStringContentLength = 0x02,
MaxArrayLength = 0x04,
MaxBytesPerRead = 0x08,
MaxNameTableCharCount = 0x10
}
public sealed class XmlDictionaryReaderQuotas
{
private bool _readOnly;
private int _maxStringContentLength;
private int _maxArrayLength;
private int _maxDepth;
private int _maxNameTableCharCount;
private int _maxBytesPerRead;
private XmlDictionaryReaderQuotaTypes _modifiedQuotas;
private const int DefaultMaxDepth = 32;
private const int DefaultMaxStringContentLength = 8192;
private const int DefaultMaxArrayLength = 16384;
private const int DefaultMaxBytesPerRead = 4096;
private const int DefaultMaxNameTableCharCount = 16384;
private static readonly XmlDictionaryReaderQuotas s_maxQuota = new XmlDictionaryReaderQuotas(int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue,
XmlDictionaryReaderQuotaTypes.MaxDepth | XmlDictionaryReaderQuotaTypes.MaxStringContentLength | XmlDictionaryReaderQuotaTypes.MaxArrayLength | XmlDictionaryReaderQuotaTypes.MaxBytesPerRead | XmlDictionaryReaderQuotaTypes.MaxNameTableCharCount);
public XmlDictionaryReaderQuotas()
{
_maxDepth = DefaultMaxDepth;
_maxStringContentLength = DefaultMaxStringContentLength;
_maxArrayLength = DefaultMaxArrayLength;
_maxBytesPerRead = DefaultMaxBytesPerRead;
_maxNameTableCharCount = DefaultMaxNameTableCharCount;
}
private XmlDictionaryReaderQuotas(int maxDepth, int maxStringContentLength, int maxArrayLength, int maxBytesPerRead, int maxNameTableCharCount, XmlDictionaryReaderQuotaTypes modifiedQuotas)
{
_maxDepth = maxDepth;
_maxStringContentLength = maxStringContentLength;
_maxArrayLength = maxArrayLength;
_maxBytesPerRead = maxBytesPerRead;
_maxNameTableCharCount = maxNameTableCharCount;
_modifiedQuotas = modifiedQuotas;
MakeReadOnly();
}
public static XmlDictionaryReaderQuotas Max
{
get
{
return s_maxQuota;
}
}
public void CopyTo(XmlDictionaryReaderQuotas quotas)
{
ArgumentNullException.ThrowIfNull(quotas);
if (quotas._readOnly)
throw new InvalidOperationException(SR.QuotaCopyReadOnly);
InternalCopyTo(quotas);
}
internal void InternalCopyTo(XmlDictionaryReaderQuotas quotas)
{
quotas._maxStringContentLength = _maxStringContentLength;
quotas._maxArrayLength = _maxArrayLength;
quotas._maxDepth = _maxDepth;
quotas._maxNameTableCharCount = _maxNameTableCharCount;
quotas._maxBytesPerRead = _maxBytesPerRead;
quotas._modifiedQuotas = _modifiedQuotas;
}
[DefaultValue(DefaultMaxStringContentLength)]
public int MaxStringContentLength
{
get
{
return _maxStringContentLength;
}
set
{
if (_readOnly)
throw new InvalidOperationException(SR.Format(SR.QuotaIsReadOnly, "MaxStringContentLength"));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
_maxStringContentLength = value;
_modifiedQuotas |= XmlDictionaryReaderQuotaTypes.MaxStringContentLength;
}
}
[DefaultValue(DefaultMaxArrayLength)]
public int MaxArrayLength
{
get
{
return _maxArrayLength;
}
set
{
if (_readOnly)
throw new InvalidOperationException(SR.Format(SR.QuotaIsReadOnly, "MaxArrayLength"));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
_maxArrayLength = value;
_modifiedQuotas |= XmlDictionaryReaderQuotaTypes.MaxArrayLength;
}
}
[DefaultValue(DefaultMaxBytesPerRead)]
public int MaxBytesPerRead
{
get
{
return _maxBytesPerRead;
}
set
{
if (_readOnly)
throw new InvalidOperationException(SR.Format(SR.QuotaIsReadOnly, "MaxBytesPerRead"));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
_maxBytesPerRead = value;
_modifiedQuotas |= XmlDictionaryReaderQuotaTypes.MaxBytesPerRead;
}
}
[DefaultValue(DefaultMaxDepth)]
public int MaxDepth
{
get
{
return _maxDepth;
}
set
{
if (_readOnly)
throw new InvalidOperationException(SR.Format(SR.QuotaIsReadOnly, "MaxDepth"));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
_maxDepth = value;
_modifiedQuotas |= XmlDictionaryReaderQuotaTypes.MaxDepth;
}
}
[DefaultValue(DefaultMaxNameTableCharCount)]
public int MaxNameTableCharCount
{
get
{
return _maxNameTableCharCount;
}
set
{
if (_readOnly)
throw new InvalidOperationException(SR.Format(SR.QuotaIsReadOnly, "MaxNameTableCharCount"));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
_maxNameTableCharCount = value;
_modifiedQuotas |= XmlDictionaryReaderQuotaTypes.MaxNameTableCharCount;
}
}
public XmlDictionaryReaderQuotaTypes ModifiedQuotas
{
get
{
return _modifiedQuotas;
}
}
internal void MakeReadOnly()
{
_readOnly = true;
}
}
}