Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TypeLimiter.cs
Go to the documentation of this file.
1
using
System.Collections.Generic
;
2
using
System.Data.SqlTypes
;
3
using
System.Drawing
;
4
using
System.Linq
;
5
using
System.Numerics
;
6
using
System.Runtime.Serialization
;
7
8
namespace
System.Data
;
9
10
internal
sealed
class
TypeLimiter
11
{
12
private
sealed
class
Scope
:
IDisposable
13
{
14
private
static
readonly
HashSet<Type>
s_allowedTypes
=
new
HashSet<Type>
15
{
16
typeof
(
bool
),
17
typeof
(
char
),
18
typeof
(sbyte),
19
typeof
(
byte
),
20
typeof
(
short
),
21
typeof
(ushort),
22
typeof
(
int
),
23
typeof
(uint),
24
typeof
(
long
),
25
typeof
(ulong),
26
typeof
(
float
),
27
typeof
(
double
),
28
typeof
(decimal),
29
typeof
(
DateTime
),
30
typeof
(
DateTimeOffset
),
31
typeof
(
TimeSpan
),
32
typeof
(
string
),
33
typeof
(
Guid
),
34
typeof
(
SqlBinary
),
35
typeof
(
SqlBoolean
),
36
typeof
(
SqlByte
),
37
typeof
(
SqlBytes
),
38
typeof
(
SqlChars
),
39
typeof
(
SqlDateTime
),
40
typeof
(
SqlDecimal
),
41
typeof
(
SqlDouble
),
42
typeof
(
SqlGuid
),
43
typeof
(
SqlInt16
),
44
typeof
(
SqlInt32
),
45
typeof
(
SqlInt64
),
46
typeof
(
SqlMoney
),
47
typeof
(
SqlSingle
),
48
typeof
(
SqlString
),
49
typeof
(
object
),
50
typeof
(
Type
),
51
typeof
(
BigInteger
),
52
typeof
(
Uri
),
53
typeof
(
Color
),
54
typeof
(
Point
),
55
typeof
(
PointF
),
56
typeof
(
Rectangle
),
57
typeof
(
RectangleF
),
58
typeof
(
Size
),
59
typeof
(
SizeF
)
60
};
61
62
private
HashSet<Type>
m_allowedTypes
;
63
64
private
readonly
Scope
m_previousScope
;
65
66
private
readonly
DeserializationToken
m_deserializationToken
;
67
68
internal
Scope
(
Scope
previousScope
,
IEnumerable<Type>
allowedTypes
)
69
{
70
m_previousScope
=
previousScope
;
71
m_allowedTypes
=
new
HashSet<Type>
(
allowedTypes
.Where((
Type
type
) =>
type
!=
null
));
72
m_deserializationToken
=
SerializationInfo
.
StartDeserialization
();
73
}
74
75
public
void
Dispose
()
76
{
77
if
(
this
!=
s_activeScope
)
78
{
79
throw
new
ObjectDisposedException
(GetType().
FullName
);
80
}
81
m_deserializationToken
.
Dispose
();
82
s_activeScope
=
m_previousScope
;
83
}
84
85
public
bool
IsAllowedType
(
Type
type
)
86
{
87
if
(
IsTypeUnconditionallyAllowed
(
type
))
88
{
89
return
true
;
90
}
91
for
(
Scope
scope =
this
; scope !=
null
; scope = scope.
m_previousScope
)
92
{
93
if
(scope.m_allowedTypes.Contains(
type
))
94
{
95
return
true
;
96
}
97
}
98
Type
[]
array
= (
Type
[])
AppDomain
.
CurrentDomain
.GetData(
"System.Data.DataSetDefaultAllowedTypes"
);
99
if
(
array
!=
null
)
100
{
101
for
(
int
i = 0; i <
array
.Length; i++)
102
{
103
if
(
type
==
array
[i])
104
{
105
return
true
;
106
}
107
}
108
}
109
return
false
;
110
}
111
112
private
static
bool
IsTypeUnconditionallyAllowed
(
Type
type
)
113
{
114
while
(
true
)
115
{
116
if
(
s_allowedTypes
.Contains(
type
))
117
{
118
return
true
;
119
}
120
if
(
type
.IsEnum)
121
{
122
return
true
;
123
}
124
if
(
type
.IsSZArray)
125
{
126
type
=
type
.GetElementType();
127
continue
;
128
}
129
if
(!
type
.IsGenericType ||
type
.IsGenericTypeDefinition || !(
type
.GetGenericTypeDefinition() ==
typeof
(
List<>
)))
130
{
131
break
;
132
}
133
type
=
type
.GetGenericArguments()[0];
134
}
135
return
false
;
136
}
137
}
138
139
[
ThreadStatic
]
140
private
static
Scope
s_activeScope
;
141
142
private
Scope
m_instanceScope
;
143
144
private
static
bool
IsTypeLimitingDisabled
=>
System
.
LocalAppContextSwitches
.
AllowArbitraryTypeInstantiation
;
145
146
private
TypeLimiter
(
Scope
scope)
147
{
148
m_instanceScope
= scope;
149
}
150
151
public
static
TypeLimiter
Capture
()
152
{
153
Scope
scope =
s_activeScope
;
154
if
(scope ==
null
)
155
{
156
return
null
;
157
}
158
return
new
TypeLimiter
(scope);
159
}
160
161
public
static
void
EnsureTypeIsAllowed
(
Type
type
,
TypeLimiter
capturedLimiter
=
null
)
162
{
163
if
((
object
)
type
!=
null
)
164
{
165
Scope
scope =
capturedLimiter
?.m_instanceScope ??
s_activeScope
;
166
if
(scope !=
null
&& !scope.
IsAllowedType
(
type
))
167
{
168
throw
ExceptionBuilder
.
TypeNotAllowed
(
type
);
169
}
170
}
171
}
172
173
public
static
IDisposable
EnterRestrictedScope
(
DataSet
dataSet
)
174
{
175
if
(
IsTypeLimitingDisabled
)
176
{
177
return
null
;
178
}
179
return
s_activeScope
=
new
Scope
(
s_activeScope
,
GetPreviouslyDeclaredDataTypes
(
dataSet
));
180
}
181
182
public
static
IDisposable
EnterRestrictedScope
(
DataTable
dataTable
)
183
{
184
if
(
IsTypeLimitingDisabled
)
185
{
186
return
null
;
187
}
188
return
s_activeScope
=
new
Scope
(
s_activeScope
,
GetPreviouslyDeclaredDataTypes
(
dataTable
));
189
}
190
191
private
static
IEnumerable<Type>
GetPreviouslyDeclaredDataTypes
(
DataTable
dataTable
)
192
{
193
if
(
dataTable
==
null
)
194
{
195
return
Enumerable
.Empty<
Type
>();
196
}
197
return
from
DataColumn
column
in
dataTable.Columns
198
select
column
.DataType;
199
}
200
201
private
static
IEnumerable<Type>
GetPreviouslyDeclaredDataTypes
(
DataSet
dataSet
)
202
{
203
if
(
dataSet
==
null
)
204
{
205
return
Enumerable
.Empty<
Type
>();
206
}
207
return
dataSet
.Tables.Cast<
DataTable
>().
SelectMany
((
DataTable
table) =>
GetPreviouslyDeclaredDataTypes
(table));
208
}
209
}
System.AppDomain.CurrentDomain
static AppDomain CurrentDomain
Definition
AppDomain.cs:28
System.AppDomain
Definition
AppDomain.cs:17
System.Collections.Generic.Dictionary
Definition
Dictionary.cs:14
System.Data.DataColumn
Definition
DataColumn.cs:24
System.Data.DataSet
Definition
DataSet.cs:28
System.Data.DataTable
Definition
DataTable.cs:29
System.Data.ExceptionBuilder.TypeNotAllowed
static Exception TypeNotAllowed(Type type)
Definition
ExceptionBuilder.cs:160
System.Data.ExceptionBuilder
Definition
ExceptionBuilder.cs:8
System.Data.SqlTypes.SqlBytes
Definition
SqlBytes.cs:11
System.Data.SqlTypes.SqlChars
Definition
SqlChars.cs:11
System.Data.TypeLimiter.Scope.m_allowedTypes
HashSet< Type > m_allowedTypes
Definition
TypeLimiter.cs:62
System.Data.TypeLimiter.Scope.m_previousScope
readonly Scope m_previousScope
Definition
TypeLimiter.cs:64
System.Data.TypeLimiter.Scope.Dispose
void Dispose()
Definition
TypeLimiter.cs:75
System.Data.TypeLimiter.Scope.IsAllowedType
bool IsAllowedType(Type type)
Definition
TypeLimiter.cs:85
System.Data.TypeLimiter.Scope.s_allowedTypes
static readonly HashSet< Type > s_allowedTypes
Definition
TypeLimiter.cs:14
System.Data.TypeLimiter.Scope.Scope
Scope(Scope previousScope, IEnumerable< Type > allowedTypes)
Definition
TypeLimiter.cs:68
System.Data.TypeLimiter.Scope.m_deserializationToken
readonly DeserializationToken m_deserializationToken
Definition
TypeLimiter.cs:66
System.Data.TypeLimiter.Scope.IsTypeUnconditionallyAllowed
static bool IsTypeUnconditionallyAllowed(Type type)
Definition
TypeLimiter.cs:112
System.Data.TypeLimiter.Scope
Definition
TypeLimiter.cs:13
System.Data.TypeLimiter.EnterRestrictedScope
static IDisposable EnterRestrictedScope(DataSet dataSet)
Definition
TypeLimiter.cs:173
System.Data.TypeLimiter.Capture
static TypeLimiter Capture()
Definition
TypeLimiter.cs:151
System.Data.TypeLimiter.EnsureTypeIsAllowed
static void EnsureTypeIsAllowed(Type type, TypeLimiter capturedLimiter=null)
Definition
TypeLimiter.cs:161
System.Data.TypeLimiter.s_activeScope
static Scope s_activeScope
Definition
TypeLimiter.cs:140
System.Data.TypeLimiter.GetPreviouslyDeclaredDataTypes
static IEnumerable< Type > GetPreviouslyDeclaredDataTypes(DataSet dataSet)
Definition
TypeLimiter.cs:201
System.Data.TypeLimiter.EnterRestrictedScope
static IDisposable EnterRestrictedScope(DataTable dataTable)
Definition
TypeLimiter.cs:182
System.Data.TypeLimiter.GetPreviouslyDeclaredDataTypes
static IEnumerable< Type > GetPreviouslyDeclaredDataTypes(DataTable dataTable)
Definition
TypeLimiter.cs:191
System.Data.TypeLimiter.TypeLimiter
TypeLimiter(Scope scope)
Definition
TypeLimiter.cs:146
System.Data.TypeLimiter.IsTypeLimitingDisabled
static bool IsTypeLimitingDisabled
Definition
TypeLimiter.cs:144
System.Data.TypeLimiter.m_instanceScope
Scope m_instanceScope
Definition
TypeLimiter.cs:142
System.Data.TypeLimiter
Definition
TypeLimiter.cs:11
System.Linq.Enumerable
Definition
Enumerable.cs:9
System.LocalAppContextSwitches.AllowArbitraryTypeInstantiation
static bool AllowArbitraryTypeInstantiation
Definition
LocalAppContextSwitches.cs:10
System.LocalAppContextSwitches
Definition
LocalAppContextSwitches.cs:6
System.ObjectDisposedException
Definition
ObjectDisposedException.cs:9
System.Runtime.Serialization.SerializationInfo.StartDeserialization
static DeserializationToken StartDeserialization()
Definition
SerializationInfo.cs:558
System.Runtime.Serialization.SerializationInfo
Definition
SerializationInfo.cs:7
System.Type
Definition
Type.cs:14
System.Uri
Definition
Uri.cs:16
System.IDisposable
Definition
IDisposable.cs:4
System.Collections.Generic
Definition
IHashKeyCollection.cs:1
System.Data.SqlTypes
Definition
EComparison.cs:1
System.Data
Definition
ADP.cs:12
System.Drawing
Definition
ColorConverter.cs:8
System.Linq
Definition
ImmutableArrayExtensions.cs:4
System.Numerics
Definition
BitOperations.cs:7
System.Runtime.Serialization.CollectionKind.Dictionary
@ Dictionary
System.Runtime.Serialization
Definition
SerializationGuard.cs:3
System.TypeNameKind.FullName
@ FullName
System.ExceptionArgument.type
@ type
System.ExceptionArgument.array
@ array
System
Definition
BlockingCollection.cs:8
System.Data.SqlTypes.SqlBinary
Definition
SqlBinary.cs:11
System.Data.SqlTypes.SqlBoolean
Definition
SqlBoolean.cs:14
System.Data.SqlTypes.SqlByte
Definition
SqlByte.cs:14
System.Data.SqlTypes.SqlDateTime
Definition
SqlDateTime.cs:15
System.Data.SqlTypes.SqlDecimal
Definition
SqlDecimal.cs:11
System.Data.SqlTypes.SqlDouble
Definition
SqlDouble.cs:15
System.Data.SqlTypes.SqlGuid
Definition
SqlGuid.cs:14
System.Data.SqlTypes.SqlInt16
Definition
SqlInt16.cs:14
System.Data.SqlTypes.SqlInt32
Definition
SqlInt32.cs:14
System.Data.SqlTypes.SqlInt64
Definition
SqlInt64.cs:14
System.Data.SqlTypes.SqlMoney
Definition
SqlMoney.cs:12
System.Data.SqlTypes.SqlSingle
Definition
SqlSingle.cs:12
System.Data.SqlTypes.SqlString
Definition
SqlString.cs:16
System.DateTimeOffset
Definition
DateTimeOffset.cs:14
System.DateTime
Definition
DateTime.cs:15
System.Drawing.Color
Definition
Color.cs:14
System.Drawing.PointF
Definition
PointF.cs:11
System.Drawing.Point
Definition
Point.cs:11
System.Drawing.RectangleF
Definition
RectangleF.cs:11
System.Drawing.Rectangle
Definition
Rectangle.cs:11
System.Drawing.SizeF
Definition
SizeF.cs:12
System.Drawing.Size
Definition
Size.cs:11
System.Guid
Definition
Guid.cs:15
System.Numerics.BigInteger
Definition
BigInteger.cs:10
System.Runtime.Serialization.DeserializationToken.Dispose
void Dispose()
Definition
DeserializationToken.cs:12
System.Runtime.Serialization.DeserializationToken
Definition
DeserializationToken.cs:4
System.TimeSpan
Definition
TimeSpan.cs:10
source
System.Data.Common
System.Data
TypeLimiter.cs
Generated by
1.10.0