Utility.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Runtime.InteropServices;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Windows;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. namespace Standard;
  15. internal static class Utility
  16. {
  17. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  18. private static bool _MemCmp(IntPtr left, IntPtr right, long cb)
  19. {
  20. int num = 0;
  21. while ((long) num < cb - 8L)
  22. {
  23. long num2 = Marshal.ReadInt64(left, num);
  24. long num3 = Marshal.ReadInt64(right, num);
  25. if (num2 != num3)
  26. {
  27. return false;
  28. }
  29. num += 8;
  30. }
  31. while ((long) num < cb)
  32. {
  33. byte b = Marshal.ReadByte(left, num);
  34. byte b2 = Marshal.ReadByte(right, num);
  35. if (b != b2)
  36. {
  37. return false;
  38. }
  39. num++;
  40. }
  41. return true;
  42. }
  43. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  44. public static int RGB(Color c)
  45. {
  46. return (int) c.R | (int) c.G << 8 | (int) c.B << 16;
  47. }
  48. public static Color ColorFromArgbDword(uint color)
  49. {
  50. return Color.FromArgb((byte) ((color & 4278190080u) >> 24), (byte) ((color & 16711680u) >> 16), (byte) ((color & 65280u) >> 8), (byte) (color & 255u));
  51. }
  52. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  53. public static int GET_X_LPARAM(IntPtr lParam)
  54. {
  55. return Utility.LOWORD(lParam.ToInt32());
  56. }
  57. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  58. public static int GET_Y_LPARAM(IntPtr lParam)
  59. {
  60. return Utility.HIWORD(lParam.ToInt32());
  61. }
  62. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  63. public static int HIWORD(int i)
  64. {
  65. return (int) ((short) (i >> 16));
  66. }
  67. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  68. public static int LOWORD(int i)
  69. {
  70. return (int) ((short) (i & 65535));
  71. }
  72. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  73. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  74. public static bool AreStreamsEqual(Stream left, Stream right)
  75. {
  76. if (left == null)
  77. {
  78. return right == null;
  79. }
  80. if (right == null)
  81. {
  82. return false;
  83. }
  84. if (!left.CanRead || !right.CanRead)
  85. {
  86. throw new NotSupportedException("The streams can't be read for comparison");
  87. }
  88. if (left.Length != right.Length)
  89. {
  90. return false;
  91. }
  92. int num = (int) left.Length;
  93. left.Position = 0L;
  94. right.Position = 0L;
  95. int i = 0;
  96. int num2 = 0;
  97. byte[] array = new byte[512];
  98. byte[] array2 = new byte[512];
  99. GCHandle gchandle = GCHandle.Alloc(array, GCHandleType.Pinned);
  100. IntPtr left2 = gchandle.AddrOfPinnedObject();
  101. GCHandle gchandle2 = GCHandle.Alloc(array2, GCHandleType.Pinned);
  102. IntPtr right2 = gchandle2.AddrOfPinnedObject();
  103. bool result;
  104. try
  105. {
  106. while (i < num)
  107. {
  108. int num3 = left.Read(array, 0, array.Length);
  109. int num4 = right.Read(array2, 0, array2.Length);
  110. if (num3 != num4)
  111. {
  112. return false;
  113. }
  114. if (!Utility._MemCmp(left2, right2, (long) num3))
  115. {
  116. return false;
  117. }
  118. i += num3;
  119. num2 += num4;
  120. }
  121. result = true;
  122. }
  123. finally
  124. {
  125. gchandle.Free();
  126. gchandle2.Free();
  127. }
  128. return result;
  129. }
  130. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  131. public static bool GuidTryParse(string guidString, out Guid guid)
  132. {
  133. Verify.IsNeitherNullNorEmpty(guidString, "guidString");
  134. try
  135. {
  136. guid = new Guid(guidString);
  137. return true;
  138. }
  139. catch (FormatException)
  140. {
  141. }
  142. catch (OverflowException)
  143. {
  144. }
  145. guid = default(Guid);
  146. return false;
  147. }
  148. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  149. public static bool IsFlagSet(int value, int mask)
  150. {
  151. return 0 != (value & mask);
  152. }
  153. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  154. public static bool IsFlagSet(uint value, uint mask)
  155. {
  156. return 0u != (value & mask);
  157. }
  158. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  159. public static bool IsFlagSet(long value, long mask)
  160. {
  161. return 0L != (value & mask);
  162. }
  163. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  164. public static bool IsFlagSet(ulong value, ulong mask)
  165. {
  166. return 0UL != (value & mask);
  167. }
  168. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  169. public static bool IsOSVistaOrNewer
  170. {
  171. get
  172. {
  173. return Utility._osVersion >= new Version(6, 0);
  174. }
  175. }
  176. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  177. public static bool IsOSWindows7OrNewer
  178. {
  179. get
  180. {
  181. return Utility._osVersion >= new Version(6, 1);
  182. }
  183. }
  184. public static bool IsPresentationFrameworkVersionLessThan4
  185. {
  186. get
  187. {
  188. return Utility._presentationFrameworkVersion < new Version(4, 0);
  189. }
  190. }
  191. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  192. [SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
  193. public static IntPtr GenerateHICON(ImageSource image, Size dimensions)
  194. {
  195. if (image == null)
  196. {
  197. return IntPtr.Zero;
  198. }
  199. BitmapFrame bitmapFrame = image as BitmapFrame;
  200. if (bitmapFrame != null)
  201. {
  202. bitmapFrame = Utility.GetBestMatch(bitmapFrame.Decoder.Frames, (int) dimensions.Width, (int) dimensions.Height);
  203. }
  204. else
  205. {
  206. Rect rectangle = new Rect(0.0, 0.0, dimensions.Width, dimensions.Height);
  207. double num = dimensions.Width / dimensions.Height;
  208. double num2 = image.Width / image.Height;
  209. if (image.Width <= dimensions.Width && image.Height <= dimensions.Height)
  210. {
  211. rectangle = new Rect((dimensions.Width - image.Width) / 2.0, (dimensions.Height - image.Height) / 2.0, image.Width, image.Height);
  212. }
  213. else if (num > num2)
  214. {
  215. double num3 = image.Width / image.Height * dimensions.Width;
  216. rectangle = new Rect((dimensions.Width - num3) / 2.0, 0.0, num3, dimensions.Height);
  217. }
  218. else if (num < num2)
  219. {
  220. double num4 = image.Height / image.Width * dimensions.Height;
  221. rectangle = new Rect(0.0, (dimensions.Height - num4) / 2.0, dimensions.Width, num4);
  222. }
  223. DrawingVisual drawingVisual = new DrawingVisual();
  224. DrawingContext drawingContext = drawingVisual.RenderOpen();
  225. drawingContext.DrawImage(image, rectangle);
  226. drawingContext.Close();
  227. RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int) dimensions.Width, (int) dimensions.Height, 96.0, 96.0, PixelFormats.Pbgra32);
  228. renderTargetBitmap.Render(drawingVisual);
  229. bitmapFrame = BitmapFrame.Create(renderTargetBitmap);
  230. }
  231. IntPtr result;
  232. using (MemoryStream memoryStream = new MemoryStream())
  233. {
  234. new PngBitmapEncoder
  235. {
  236. Frames =
  237. {
  238. bitmapFrame
  239. }
  240. }.Save(memoryStream);
  241. using (ManagedIStream managedIStream = new ManagedIStream(memoryStream))
  242. {
  243. IntPtr zero = IntPtr.Zero;
  244. try
  245. {
  246. Status status = NativeMethods.GdipCreateBitmapFromStream(managedIStream, out zero);
  247. if (status != Status.Ok)
  248. {
  249. result = IntPtr.Zero;
  250. }
  251. else
  252. {
  253. IntPtr intPtr;
  254. status = NativeMethods.GdipCreateHICONFromBitmap(zero, out intPtr);
  255. if (status != Status.Ok)
  256. {
  257. result = IntPtr.Zero;
  258. }
  259. else
  260. {
  261. result = intPtr;
  262. }
  263. }
  264. }
  265. finally
  266. {
  267. Utility.SafeDisposeImage(ref zero);
  268. }
  269. }
  270. }
  271. return result;
  272. }
  273. public static BitmapFrame GetBestMatch(IList<BitmapFrame> frames, int width, int height)
  274. {
  275. return Utility._GetBestMatch(frames, Utility._GetBitDepth(), width, height);
  276. }
  277. private static int _MatchImage(BitmapFrame frame, int bitDepth, int width, int height, int bpp)
  278. {
  279. return 2 * Utility._WeightedAbs(bpp, bitDepth, false) + Utility._WeightedAbs(frame.PixelWidth, width, true) + Utility._WeightedAbs(frame.PixelHeight, height, true);
  280. }
  281. private static int _WeightedAbs(int valueHave, int valueWant, bool fPunish)
  282. {
  283. int num = valueHave - valueWant;
  284. if (num < 0)
  285. {
  286. num = (fPunish ? -2 : -1) * num;
  287. }
  288. return num;
  289. }
  290. private static BitmapFrame _GetBestMatch(IList<BitmapFrame> frames, int bitDepth, int width, int height)
  291. {
  292. int num = int.MaxValue;
  293. int num2 = 0;
  294. int index = 0;
  295. bool flag = frames[0].Decoder is IconBitmapDecoder;
  296. int num3 = 0;
  297. while (num3 < frames.Count && num != 0)
  298. {
  299. int num4 = flag ? frames[num3].Thumbnail.Format.BitsPerPixel : frames[num3].Format.BitsPerPixel;
  300. if (num4 == 0)
  301. {
  302. num4 = 8;
  303. }
  304. int num5 = Utility._MatchImage(frames[num3], bitDepth, width, height, num4);
  305. if (num5 < num)
  306. {
  307. index = num3;
  308. num2 = num4;
  309. num = num5;
  310. }
  311. else if (num5 == num && num2 < num4)
  312. {
  313. index = num3;
  314. num2 = num4;
  315. }
  316. num3++;
  317. }
  318. return frames[index];
  319. }
  320. private static int _GetBitDepth()
  321. {
  322. if (Utility.s_bitDepth == 0)
  323. {
  324. using (SafeDC desktop = SafeDC.GetDesktop())
  325. {
  326. Utility.s_bitDepth = NativeMethods.GetDeviceCaps(desktop, DeviceCap.BITSPIXEL) * NativeMethods.GetDeviceCaps(desktop, DeviceCap.PLANES);
  327. }
  328. }
  329. return Utility.s_bitDepth;
  330. }
  331. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  332. public static void SafeDeleteFile(string path)
  333. {
  334. if (!string.IsNullOrEmpty(path))
  335. {
  336. File.Delete(path);
  337. }
  338. }
  339. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  340. public static void SafeDeleteObject(ref IntPtr gdiObject)
  341. {
  342. IntPtr intPtr = gdiObject;
  343. gdiObject = IntPtr.Zero;
  344. if (IntPtr.Zero != intPtr)
  345. {
  346. NativeMethods.DeleteObject(intPtr);
  347. }
  348. }
  349. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  350. public static void SafeDestroyIcon(ref IntPtr hicon)
  351. {
  352. IntPtr intPtr = hicon;
  353. hicon = IntPtr.Zero;
  354. if (IntPtr.Zero != intPtr)
  355. {
  356. NativeMethods.DestroyIcon(intPtr);
  357. }
  358. }
  359. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  360. public static void SafeDestroyWindow(ref IntPtr hwnd)
  361. {
  362. IntPtr hwnd2 = hwnd;
  363. hwnd = IntPtr.Zero;
  364. if (NativeMethods.IsWindow(hwnd2))
  365. {
  366. NativeMethods.DestroyWindow(hwnd2);
  367. }
  368. }
  369. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  370. public static void SafeDispose<T>(ref T disposable) where T : IDisposable
  371. {
  372. IDisposable disposable2 = disposable;
  373. disposable = default(T);
  374. if (disposable2 != null)
  375. {
  376. disposable2.Dispose();
  377. }
  378. }
  379. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  380. public static void SafeDisposeImage(ref IntPtr gdipImage)
  381. {
  382. IntPtr intPtr = gdipImage;
  383. gdipImage = IntPtr.Zero;
  384. if (IntPtr.Zero != intPtr)
  385. {
  386. NativeMethods.GdipDisposeImage(intPtr);
  387. }
  388. }
  389. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  390. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  391. public static void SafeCoTaskMemFree(ref IntPtr ptr)
  392. {
  393. IntPtr intPtr = ptr;
  394. ptr = IntPtr.Zero;
  395. if (IntPtr.Zero != intPtr)
  396. {
  397. Marshal.FreeCoTaskMem(intPtr);
  398. }
  399. }
  400. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  401. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  402. public static void SafeFreeHGlobal(ref IntPtr hglobal)
  403. {
  404. IntPtr intPtr = hglobal;
  405. hglobal = IntPtr.Zero;
  406. if (IntPtr.Zero != intPtr)
  407. {
  408. Marshal.FreeHGlobal(intPtr);
  409. }
  410. }
  411. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  412. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  413. public static void SafeRelease<T>(ref T comObject) where T : class
  414. {
  415. T t = comObject;
  416. comObject = default(T);
  417. if (t != null)
  418. {
  419. Marshal.ReleaseComObject(t);
  420. }
  421. }
  422. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  423. public static void GeneratePropertyString(StringBuilder source, string propertyName, string value)
  424. {
  425. if (source.Length != 0)
  426. {
  427. source.Append(' ');
  428. }
  429. source.Append(propertyName);
  430. source.Append(": ");
  431. if (string.IsNullOrEmpty(value))
  432. {
  433. source.Append("<null>");
  434. return;
  435. }
  436. source.Append('"');
  437. source.Append(value);
  438. source.Append('"');
  439. }
  440. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  441. [Obsolete]
  442. public static string GenerateToString<T>(T @object) where T : struct
  443. {
  444. StringBuilder stringBuilder = new StringBuilder();
  445. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
  446. {
  447. if (stringBuilder.Length != 0)
  448. {
  449. stringBuilder.Append(", ");
  450. }
  451. object value = propertyInfo.GetValue(@object, null);
  452. string format = (value == null) ? "{0}: <null>" : "{0}: \"{1}\"";
  453. stringBuilder.AppendFormat(format, propertyInfo.Name, value);
  454. }
  455. return stringBuilder.ToString();
  456. }
  457. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  458. public static void CopyStream(Stream destination, Stream source)
  459. {
  460. destination.Position = 0L;
  461. if (source.CanSeek)
  462. {
  463. source.Position = 0L;
  464. destination.SetLength(source.Length);
  465. }
  466. byte[] array = new byte[4096];
  467. int num;
  468. do
  469. {
  470. num = source.Read(array, 0, array.Length);
  471. if (num != 0)
  472. {
  473. destination.Write(array, 0, num);
  474. }
  475. }
  476. while (array.Length == num);
  477. destination.Position = 0L;
  478. }
  479. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  480. public static string HashStreamMD5(Stream stm)
  481. {
  482. stm.Position = 0L;
  483. StringBuilder stringBuilder = new StringBuilder();
  484. using (MD5 md = MD5.Create())
  485. {
  486. foreach (byte b in md.ComputeHash(stm))
  487. {
  488. stringBuilder.Append(b.ToString("x2", CultureInfo.InvariantCulture));
  489. }
  490. }
  491. return stringBuilder.ToString();
  492. }
  493. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  494. public static void EnsureDirectory(string path)
  495. {
  496. if (!Directory.Exists(Path.GetDirectoryName(path)))
  497. {
  498. Directory.CreateDirectory(Path.GetDirectoryName(path));
  499. }
  500. }
  501. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  502. public static bool MemCmp(byte[] left, byte[] right, int cb)
  503. {
  504. GCHandle gchandle = GCHandle.Alloc(left, GCHandleType.Pinned);
  505. IntPtr left2 = gchandle.AddrOfPinnedObject();
  506. GCHandle gchandle2 = GCHandle.Alloc(right, GCHandleType.Pinned);
  507. IntPtr right2 = gchandle2.AddrOfPinnedObject();
  508. bool result = Utility._MemCmp(left2, right2, (long) cb);
  509. gchandle.Free();
  510. gchandle2.Free();
  511. return result;
  512. }
  513. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  514. public static string UrlDecode(string url)
  515. {
  516. if (url == null)
  517. {
  518. return null;
  519. }
  520. Utility._UrlDecoder urlDecoder = new Utility._UrlDecoder(url.Length, Encoding.UTF8);
  521. int length = url.Length;
  522. for (int i = 0; i < length; i++)
  523. {
  524. char c = url[i];
  525. if (c == '+')
  526. {
  527. urlDecoder.AddByte(32);
  528. }
  529. else
  530. {
  531. if (c == '%' && i < length - 2)
  532. {
  533. if (url[i + 1] == 'u' && i < length - 5)
  534. {
  535. int num = Utility._HexToInt(url[i + 2]);
  536. int num2 = Utility._HexToInt(url[i + 3]);
  537. int num3 = Utility._HexToInt(url[i + 4]);
  538. int num4 = Utility._HexToInt(url[i + 5]);
  539. if (num >= 0 && num2 >= 0 && num3 >= 0 && num4 >= 0)
  540. {
  541. urlDecoder.AddChar((char) (num << 12 | num2 << 8 | num3 << 4 | num4));
  542. i += 5;
  543. goto IL_12D;
  544. }
  545. }
  546. else
  547. {
  548. int num5 = Utility._HexToInt(url[i + 1]);
  549. int num6 = Utility._HexToInt(url[i + 2]);
  550. if (num5 >= 0 && num6 >= 0)
  551. {
  552. urlDecoder.AddByte((byte) (num5 << 4 | num6));
  553. i += 2;
  554. goto IL_12D;
  555. }
  556. }
  557. }
  558. if ((c & 'タ') == '\0')
  559. {
  560. urlDecoder.AddByte((byte) c);
  561. }
  562. else
  563. {
  564. urlDecoder.AddChar(c);
  565. }
  566. }
  567. IL_12D:;
  568. }
  569. return urlDecoder.GetString();
  570. }
  571. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  572. public static string UrlEncode(string url)
  573. {
  574. if (url == null)
  575. {
  576. return null;
  577. }
  578. byte[] array = Encoding.UTF8.GetBytes(url);
  579. bool flag = false;
  580. int num = 0;
  581. foreach (byte b in array)
  582. {
  583. if (b == 32)
  584. {
  585. flag = true;
  586. }
  587. else if (!Utility._UrlEncodeIsSafe(b))
  588. {
  589. num++;
  590. flag = true;
  591. }
  592. }
  593. if (flag)
  594. {
  595. byte[] array3 = new byte[array.Length + num * 2];
  596. int num2 = 0;
  597. foreach (byte b2 in array)
  598. {
  599. if (Utility._UrlEncodeIsSafe(b2))
  600. {
  601. array3[num2++] = b2;
  602. }
  603. else if (b2 == 32)
  604. {
  605. array3[num2++] = 43;
  606. }
  607. else
  608. {
  609. array3[num2++] = 37;
  610. array3[num2++] = Utility._IntToHex(b2 >> 4 & 15);
  611. array3[num2++] = Utility._IntToHex((int) (b2 & 15));
  612. }
  613. }
  614. array = array3;
  615. }
  616. return Encoding.ASCII.GetString(array);
  617. }
  618. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  619. private static bool _UrlEncodeIsSafe(byte b)
  620. {
  621. if (Utility._IsAsciiAlphaNumeric(b))
  622. {
  623. return true;
  624. }
  625. if (b != 33)
  626. {
  627. switch (b)
  628. {
  629. case 39:
  630. case 40:
  631. case 41:
  632. case 42:
  633. case 45:
  634. case 46:
  635. return true;
  636. case 43:
  637. case 44:
  638. break;
  639. default:
  640. if (b == 95)
  641. {
  642. return true;
  643. }
  644. break;
  645. }
  646. return false;
  647. }
  648. return true;
  649. }
  650. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  651. private static bool _IsAsciiAlphaNumeric(byte b)
  652. {
  653. return (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || (b >= 48 && b <= 57);
  654. }
  655. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  656. private static byte _IntToHex(int n)
  657. {
  658. if (n <= 9)
  659. {
  660. return (byte) (n + 48);
  661. }
  662. return (byte) (n - 10 + 65);
  663. }
  664. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  665. private static int _HexToInt(char h)
  666. {
  667. if (h >= '0' && h <= '9')
  668. {
  669. return (int) (h - '0');
  670. }
  671. if (h >= 'a' && h <= 'f')
  672. {
  673. return (int) (h - 'a' + '\n');
  674. }
  675. if (h >= 'A' && h <= 'F')
  676. {
  677. return (int) (h - 'A' + '\n');
  678. }
  679. return -1;
  680. }
  681. public static void AddDependencyPropertyChangeListener(object component, DependencyProperty property, EventHandler listener)
  682. {
  683. if (component == null)
  684. {
  685. return;
  686. }
  687. DependencyPropertyDescriptor dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(property, component.GetType());
  688. dependencyPropertyDescriptor.AddValueChanged(component, listener);
  689. }
  690. public static void RemoveDependencyPropertyChangeListener(object component, DependencyProperty property, EventHandler listener)
  691. {
  692. if (component == null)
  693. {
  694. return;
  695. }
  696. DependencyPropertyDescriptor dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(property, component.GetType());
  697. dependencyPropertyDescriptor.RemoveValueChanged(component, listener);
  698. }
  699. public static bool IsThicknessNonNegative(Thickness thickness)
  700. {
  701. return Utility.IsDoubleFiniteAndNonNegative(thickness.Top) && Utility.IsDoubleFiniteAndNonNegative(thickness.Left) && Utility.IsDoubleFiniteAndNonNegative(thickness.Bottom) && Utility.IsDoubleFiniteAndNonNegative(thickness.Right);
  702. }
  703. public static bool IsCornerRadiusValid(CornerRadius cornerRadius)
  704. {
  705. return Utility.IsDoubleFiniteAndNonNegative(cornerRadius.TopLeft) && Utility.IsDoubleFiniteAndNonNegative(cornerRadius.TopRight) && Utility.IsDoubleFiniteAndNonNegative(cornerRadius.BottomLeft) && Utility.IsDoubleFiniteAndNonNegative(cornerRadius.BottomRight);
  706. }
  707. public static bool IsDoubleFiniteAndNonNegative(double d)
  708. {
  709. return !double.IsNaN(d) && !double.IsInfinity(d) && d >= 0.0;
  710. }
  711. private static readonly Version _osVersion = Environment.OSVersion.Version;
  712. private static readonly Version _presentationFrameworkVersion = Assembly.GetAssembly(typeof(Window)).GetName().Version;
  713. private static int s_bitDepth;
  714. private class _UrlDecoder
  715. {
  716. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  717. public _UrlDecoder(int size, Encoding encoding)
  718. {
  719. this._encoding = encoding;
  720. this._charBuffer = new char[size];
  721. this._byteBuffer = new byte[size];
  722. }
  723. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  724. public void AddByte(byte b)
  725. {
  726. this._byteBuffer[this._byteCount++] = b;
  727. }
  728. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  729. public void AddChar(char ch)
  730. {
  731. this._FlushBytes();
  732. this._charBuffer[this._charCount++] = ch;
  733. }
  734. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  735. private void _FlushBytes()
  736. {
  737. if (this._byteCount > 0)
  738. {
  739. this._charCount += this._encoding.GetChars(this._byteBuffer, 0, this._byteCount, this._charBuffer, this._charCount);
  740. this._byteCount = 0;
  741. }
  742. }
  743. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  744. public string GetString()
  745. {
  746. this._FlushBytes();
  747. if (this._charCount > 0)
  748. {
  749. return new string(this._charBuffer, 0, this._charCount);
  750. }
  751. return "";
  752. }
  753. private readonly Encoding _encoding;
  754. private readonly char[] _charBuffer;
  755. private readonly byte[] _byteBuffer;
  756. private int _byteCount;
  757. private int _charCount;
  758. }
  759. }