Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DbConnectionOptions.cs
Go to the documentation of this file.
4using System.Text;
6
7namespace System.Data.Common;
8
9[DefaultMember("Item")]
10internal class DbConnectionOptions
11{
28
29 internal readonly bool _useOdbcRules;
30
31 internal readonly bool _hasUserIdKeyword;
32
33 private static readonly Regex s_connectionStringValidKeyRegex = new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled);
34
35 private static readonly Regex s_connectionStringValidValueRegex = new Regex("^[^\0]*$", RegexOptions.Compiled);
36
37 private static readonly Regex s_connectionStringQuoteValueRegex = new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled);
38
39 private static readonly Regex s_connectionStringQuoteOdbcValueRegex = new Regex("^\\{([^\\}\0]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
40
41 private readonly string _usersConnectionString;
42
44
45 internal readonly NameValuePair _keyChain;
46
47 internal readonly bool _hasPasswordKeyword;
48
61
62 internal static void AppendKeyValuePairBuilder(StringBuilder builder, string keyName, string keyValue, bool useOdbcRules)
63 {
64 ADP.CheckArgumentNull(builder, "builder");
66 if (keyName == null || !s_connectionStringValidKeyRegex.IsMatch(keyName))
67 {
69 }
70 if (keyValue != null && !IsValueValidInternal(keyValue))
71 {
73 }
74 if (0 < builder.Length && ';' != builder[builder.Length - 1])
75 {
76 builder.Append(';');
77 }
78 if (useOdbcRules)
79 {
80 builder.Append(keyName);
81 }
82 else
83 {
84 builder.Append(keyName.Replace("=", "=="));
85 }
86 builder.Append('=');
87 if (keyValue == null)
88 {
89 return;
90 }
91 if (useOdbcRules)
92 {
93 if (0 < keyValue.Length && ('{' == keyValue[0] || 0 <= keyValue.IndexOf(';') || string.Equals("Driver", keyName, StringComparison.OrdinalIgnoreCase)) && !s_connectionStringQuoteOdbcValueRegex.IsMatch(keyValue))
94 {
95 builder.Append('{').Append(keyValue.Replace("}", "}}")).Append('}');
96 }
97 else
98 {
99 builder.Append(keyValue);
100 }
101 }
103 {
104 builder.Append(keyValue);
105 }
106 else if (keyValue.Contains('"') && !keyValue.Contains('\''))
107 {
108 builder.Append('\'');
109 builder.Append(keyValue);
110 builder.Append('\'');
111 }
112 else
113 {
114 builder.Append('"');
115 builder.Append(keyValue.Replace("\"", "\"\""));
116 builder.Append('"');
117 }
118 }
119
120 internal static void ValidateKeyValuePair(string keyword, string value)
121 {
122 if (keyword == null || !s_connectionStringValidKeyRegex.IsMatch(keyword))
123 {
125 }
126 if (value != null && !s_connectionStringValidValueRegex.IsMatch(value))
127 {
128 throw ADP.InvalidValue(keyword);
129 }
130 }
131
132 private static string GetKeyName(StringBuilder buffer)
133 {
134 int num = buffer.Length;
135 while (0 < num && char.IsWhiteSpace(buffer[num - 1]))
136 {
137 num--;
138 }
139 return buffer.ToString(0, num).ToLowerInvariant();
140 }
141
142 private static string GetKeyValue(StringBuilder buffer, bool trimWhitespace)
143 {
144 int num = buffer.Length;
145 int i = 0;
146 if (trimWhitespace)
147 {
148 for (; i < num && char.IsWhiteSpace(buffer[i]); i++)
149 {
150 }
151 while (0 < num && char.IsWhiteSpace(buffer[num - 1]))
152 {
153 num--;
154 }
155 }
156 return buffer.ToString(i, num - i);
157 }
158
160 {
162 buffer.Length = 0;
163 keyname = null;
164 keyvalue = null;
165 char c = '\0';
168 {
170 switch (parserState)
171 {
172 case ParserState.NothingYet:
173 if (';' == c || char.IsWhiteSpace(c))
174 {
175 continue;
176 }
177 if (c == '\0')
178 {
179 parserState = ParserState.NullTermination;
180 continue;
181 }
182 if (char.IsControl(c))
183 {
185 }
187 if ('=' != c)
188 {
190 goto IL_0248;
191 }
192 parserState = ParserState.KeyEqual;
193 continue;
194 case ParserState.Key:
195 if ('=' == c)
196 {
197 parserState = ParserState.KeyEqual;
198 continue;
199 }
200 if (!char.IsWhiteSpace(c) && char.IsControl(c))
201 {
203 }
204 goto IL_0248;
205 case ParserState.KeyEqual:
206 if (!useOdbcRules && '=' == c)
207 {
209 goto IL_0248;
210 }
212 if (string.IsNullOrEmpty(keyname))
213 {
215 }
216 buffer.Length = 0;
217 parserState = ParserState.KeyEnd;
218 goto case ParserState.KeyEnd;
219 case ParserState.KeyEnd:
220 if (char.IsWhiteSpace(c))
221 {
222 continue;
223 }
224 if (useOdbcRules)
225 {
226 if ('{' == c)
227 {
228 parserState = ParserState.BraceQuoteValue;
229 goto IL_0248;
230 }
231 }
232 else
233 {
234 if ('\'' == c)
235 {
236 parserState = ParserState.SingleQuoteValue;
237 continue;
238 }
239 if ('"' == c)
240 {
241 parserState = ParserState.DoubleQuoteValue;
242 continue;
243 }
244 }
245 if (';' == c || c == '\0')
246 {
247 break;
248 }
249 if (char.IsControl(c))
250 {
252 }
253 parserState = ParserState.UnquotedValue;
254 goto IL_0248;
255 case ParserState.UnquotedValue:
256 if (!char.IsWhiteSpace(c) && (char.IsControl(c) || ';' == c))
257 {
258 break;
259 }
260 goto IL_0248;
261 case ParserState.DoubleQuoteValue:
262 if ('"' == c)
263 {
264 parserState = ParserState.DoubleQuoteValueQuote;
265 continue;
266 }
267 if (c == '\0')
268 {
270 }
271 goto IL_0248;
272 case ParserState.DoubleQuoteValueQuote:
273 if ('"' == c)
274 {
275 parserState = ParserState.DoubleQuoteValue;
276 goto IL_0248;
277 }
279 parserState = ParserState.QuotedValueEnd;
280 goto case ParserState.QuotedValueEnd;
281 case ParserState.SingleQuoteValue:
282 if ('\'' == c)
283 {
284 parserState = ParserState.SingleQuoteValueQuote;
285 continue;
286 }
287 if (c == '\0')
288 {
290 }
291 goto IL_0248;
292 case ParserState.SingleQuoteValueQuote:
293 if ('\'' == c)
294 {
295 parserState = ParserState.SingleQuoteValue;
296 goto IL_0248;
297 }
299 parserState = ParserState.QuotedValueEnd;
300 goto case ParserState.QuotedValueEnd;
301 case ParserState.BraceQuoteValue:
302 if ('}' == c)
303 {
304 parserState = ParserState.BraceQuoteValueQuote;
305 }
306 else if (c == '\0')
307 {
309 }
310 goto IL_0248;
311 case ParserState.BraceQuoteValueQuote:
312 if ('}' == c)
313 {
314 parserState = ParserState.BraceQuoteValue;
315 goto IL_0248;
316 }
318 parserState = ParserState.QuotedValueEnd;
319 goto case ParserState.QuotedValueEnd;
320 case ParserState.QuotedValueEnd:
321 if (char.IsWhiteSpace(c))
322 {
323 continue;
324 }
325 if (';' != c)
326 {
327 if (c == '\0')
328 {
329 parserState = ParserState.NullTermination;
330 continue;
331 }
333 }
334 break;
335 case ParserState.NullTermination:
336 if (c == '\0' || char.IsWhiteSpace(c))
337 {
338 continue;
339 }
341 default:
342 {
343 throw ADP.InternalError(ADP.InternalErrorCode.InvalidParserState1);
344 }
345 IL_0248:
346 buffer.Append(c);
347 continue;
348 }
349 break;
350 }
351 switch (parserState)
352 {
353 case ParserState.Key:
354 case ParserState.DoubleQuoteValue:
355 case ParserState.SingleQuoteValue:
356 case ParserState.BraceQuoteValue:
358 case ParserState.KeyEqual:
360 if (string.IsNullOrEmpty(keyname))
361 {
363 }
364 break;
365 case ParserState.UnquotedValue:
366 {
368 char c2 = keyvalue[keyvalue.Length - 1];
369 if (!useOdbcRules && ('\'' == c2 || '"' == c2))
370 {
372 }
373 break;
374 }
375 case ParserState.DoubleQuoteValueQuote:
376 case ParserState.SingleQuoteValueQuote:
377 case ParserState.BraceQuoteValueQuote:
378 case ParserState.QuotedValueEnd:
380 break;
381 default:
382 throw ADP.InternalError(ADP.InternalErrorCode.InvalidParserState2);
383 case ParserState.NothingYet:
384 case ParserState.KeyEnd:
385 case ParserState.NullTermination:
386 break;
387 }
388 if (';' == c && currentPosition < connectionString.Length)
389 {
391 }
392 return currentPosition;
393 }
394
395 private static bool IsValueValidInternal(string keyvalue)
396 {
397 if (keyvalue != null)
398 {
399 return -1 == keyvalue.IndexOf('\0');
400 }
401 return true;
402 }
403
404 private static bool IsKeyNameValid([NotNullWhen(true)] string keyname)
405 {
406 if (keyname != null)
407 {
408 if (0 < keyname.Length && ';' != keyname[0] && !char.IsWhiteSpace(keyname[0]))
409 {
410 return -1 == keyname.IndexOf('\0');
411 }
412 return false;
413 }
414 return false;
415 }
416
418 {
421 NameValuePair result = null;
422 int num = 0;
423 int length = connectionString.Length;
424 while (num < length)
425 {
426 int num2 = num;
428 if (string.IsNullOrEmpty(keyname))
429 {
430 break;
431 }
432 string value;
433 string text = ((synonyms == null) ? keyname : (synonyms.TryGetValue(keyname, out value) ? value : null));
434 if (!IsKeyNameValid(text))
435 {
437 }
439 {
441 }
442 if (nameValuePair != null)
443 {
444 NameValuePair nameValuePair3 = (nameValuePair.Next = new NameValuePair(text, keyvalue, num - num2));
446 }
447 else if (buildChain)
448 {
449 result = (nameValuePair = new NameValuePair(text, keyvalue, num - num2));
450 }
451 }
452 return result;
453 }
454}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
static Exception InternalError(InternalErrorCode internalError)
Definition ADP.cs:917
static ArgumentException ConnectionStringSyntax(int index)
Definition ADP.cs:815
static ArgumentException InvalidValue(string parameterName)
Definition ADP.cs:169
static ArgumentException InvalidKeyname(string parameterName)
Definition ADP.cs:164
static void CheckArgumentNull([NotNull] object value, string parameterName)
Definition ADP.cs:782
static ArgumentException KeywordNotSupported(string keyword)
Definition ADP.cs:820
static void CheckArgumentLength(string value, string parameterName)
Definition ADP.cs:86
static readonly Regex s_connectionStringValidValueRegex
static void AppendKeyValuePairBuilder(StringBuilder builder, string keyName, string keyValue, bool useOdbcRules)
static bool IsValueValidInternal(string keyvalue)
static NameValuePair ParseInternal(Dictionary< string, string > parsetable, string connectionString, bool buildChain, Dictionary< string, string > synonyms, bool firstKey)
static string GetKeyValue(StringBuilder buffer, bool trimWhitespace)
DbConnectionOptions(string connectionString, Dictionary< string, string > synonyms, bool useOdbcRules)
readonly Dictionary< string, string > _parsetable
static int GetKeyValuePair(string connectionString, int currentPosition, StringBuilder buffer, bool useOdbcRules, out string keyname, out string keyvalue)
static void ValidateKeyValuePair(string keyword, string value)
static readonly Regex s_connectionStringQuoteValueRegex
static readonly Regex s_connectionStringValidKeyRegex
static bool IsKeyNameValid([NotNullWhen(true)] string keyname)
static string GetKeyName(StringBuilder buffer)
static readonly Regex s_connectionStringQuoteOdbcValueRegex