Small improvement.
[certviewer.git] / src / App.xaml.cs
blob4aea94507c87ff5d328e6fed8c874aa5f1b75884
1 using System;
2 using System.Runtime.CompilerServices;
3 using System.Runtime.InteropServices;
4 using System.Security;
5 using System.Windows;
6 using System.Windows.Interop;
7 using System.Windows.Media;
8 using Org.BouncyCastle.Asn1;
10 namespace CertViewer
12 public partial class App : Application
14 private static class Const
16 internal static readonly string MAX_KEY_SIZE = (1U << 16).ToString();
19 public App()
21 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler);
24 protected override void OnStartup(StartupEventArgs e)
26 const string BOUNCY = "Org.BouncyCastle.";
27 try
29 Environment.SetEnvironmentVariable(BOUNCY + "EC.Fp_MaxSize", Const.MAX_KEY_SIZE);
30 Environment.SetEnvironmentVariable(BOUNCY + "Rsa.MaxSize", Const.MAX_KEY_SIZE);
31 Environment.SetEnvironmentVariable(BOUNCY + "Asn1.AllowUnsafeInteger", string.Empty);
32 Environment.SetEnvironmentVariable(BOUNCY + "EC.Fp_Certainty", string.Empty);
33 Environment.SetEnvironmentVariable(BOUNCY + "Fpe.Disable", string.Empty);
34 Environment.SetEnvironmentVariable(BOUNCY + "Fpe.Disable_Ff1", string.Empty);
35 Environment.SetEnvironmentVariable(BOUNCY + "Pkcs1.Strict", string.Empty);
36 Environment.SetEnvironmentVariable(BOUNCY + "Pkcs12.IgnoreUselessPassword", string.Empty);
37 Environment.SetEnvironmentVariable(BOUNCY + "X509.Allow_Non-DER_TBSCert", string.Empty);
39 catch { }
40 try
42 if (Array.Exists(e.Args, str => StrCaseCmp(str, "--render-mode=software")) || StrCaseCmp(Environment.GetEnvironmentVariable("CertViewer.RenderMode"), "software"))
44 RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
47 catch { }
48 Require(typeof(Asn1Encodable));
51 private static void ExceptionHandler(object sender, UnhandledExceptionEventArgs args)
53 try
55 if (args.ExceptionObject is Exception exception)
57 FatalAppExit(0, $"{exception.GetType().Name}: {exception.Message}");
60 catch { }
61 Environment.Exit(-1);
64 private static bool StrCaseCmp(string s1, string s2)
66 if ((!ReferenceEquals(s1, null)) && (!ReferenceEquals(s2, null)))
68 return string.Equals(s1.Trim(), s2.Trim(), StringComparison.OrdinalIgnoreCase);
70 return false;
73 [MethodImpl(MethodImplOptions.NoInlining)]
74 private static void Require(Type _) { }
76 [DllImport("kernel32.dll", ExactSpelling=true, CharSet=CharSet.Unicode, EntryPoint="FatalAppExitW")]
77 [SuppressUnmanagedCodeSecurity]
78 private static extern void FatalAppExit(uint reserved, string message);