public CustomAllocatedObjectPool(AllocateDelegate allocate, FreeDelegate free, int initialSize, int maxItems) { Assert.IsNotNull(allocate); allocateDelegate = allocate; freeDelegate = free; this.free = new Stack <T>(initialSize); this.maxItems = maxItems; while (initialSize-- > 0) { this.free.Push(allocateDelegate()); } }
static PcreWrapper() { DllName = IntPtr.Size == 4 ? "pcre.dll" : "pcre64.dll"; _dllHandle = LoadLibrary(DllName); if (_dllHandle != IntPtr.Zero) { IntPtr ptr; ptr = GetVarFromDll(_dllHandle, "pcre_free"); pcre_free = (FreeDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(FreeDelegate)); ptr = GetVarFromDll(_dllHandle, "pcre_stack_free"); pcre_stack_free = (FreeDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(FreeDelegate)); ptr = GetVarFromDll(_dllHandle, "pcre_malloc"); pcre_malloc = (AllocDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(AllocDelegate)); ptr = GetVarFromDll(_dllHandle, "pcre_stack_malloc"); pcre_stack_malloc = (AllocDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(AllocDelegate)); iptr_pcre_callout = GetProcAddress(_dllHandle, "pcre_callout"); pcre_compile = (pcre_compile_delegate)GetDelegateFromDll(_dllHandle, "pcre_compile", typeof(pcre_compile_delegate)); pcre_compile2 = (pcre_compile2_delegate)GetDelegateFromDll(_dllHandle, "pcre_compile2", typeof(pcre_compile2_delegate)); pcre_study = (pcre_study_delegate)GetDelegateFromDll(_dllHandle, "pcre_study", typeof(pcre_study_delegate)); pcre_free_study = (pcre_free_study_delegate)GetDelegateFromDll(_dllHandle, "pcre_free_study", typeof(pcre_free_study_delegate)); pcre_exec = (pcre_exec_delegate)GetDelegateFromDll(_dllHandle, "pcre_exec", typeof(pcre_exec_delegate)); pcre_dfa_exec = (pcre_dfa_exec_delegate)GetDelegateFromDll(_dllHandle, "pcre_dfa_exec", typeof(pcre_dfa_exec_delegate)); pcre_copy_named_substring = (pcre_copy_named_substring_delegate)GetDelegateFromDll(_dllHandle, "pcre_copy_named_substring", typeof(pcre_copy_named_substring_delegate)); pcre_copy_substring = (pcre_copy_substring_delegate)GetDelegateFromDll(_dllHandle, "pcre_copy_substring", typeof(pcre_copy_substring_delegate)); pcre_get_named_substring = (pcre_get_named_substring_delegate)GetDelegateFromDll(_dllHandle, "pcre_get_named_substring", typeof(pcre_get_named_substring_delegate)); pcre_get_stringnumber = (pcre_get_stringnumber_delegate)GetDelegateFromDll(_dllHandle, "pcre_get_stringnumber", typeof(pcre_get_stringnumber_delegate)); pcre_get_stringtable_entries = (pcre_get_stringtable_entries_delegate)GetDelegateFromDll(_dllHandle, "pcre_get_stringtable_entries", typeof(pcre_get_stringtable_entries_delegate)); pcre_get_substring = (pcre_get_substring_delegate)GetDelegateFromDll(_dllHandle, "pcre_get_substring", typeof(pcre_get_substring_delegate)); pcre_get_substring_list = (pcre_get_substring_list_delegate)GetDelegateFromDll(_dllHandle, "pcre_get_substring_list", typeof(pcre_get_substring_list_delegate)); pcre_free_substring = (pcre_free_substring_delegate)GetDelegateFromDll(_dllHandle, "pcre_free_substring", typeof(pcre_free_substring_delegate)); pcre_free_substring_list = (pcre_free_substring_list_delegate)GetDelegateFromDll(_dllHandle, "pcre_free_substring_list", typeof(pcre_free_substring_list_delegate)); pcre_jit_exec = (pcre_jit_exec_delegate)GetDelegateFromDll(_dllHandle, "pcre_jit_exec", typeof(pcre_jit_exec_delegate)); pcre_jit_stack_alloc = (pcre_jit_stack_alloc_delegate)GetDelegateFromDll(_dllHandle, "pcre_jit_stack_alloc", typeof(pcre_jit_stack_alloc_delegate)); pcre_jit_stack_free = (pcre_jit_stack_free_delegate)GetDelegateFromDll(_dllHandle, "pcre_jit_stack_free", typeof(pcre_jit_stack_free_delegate)); pcre_assign_jit_stack = (pcre_assign_jit_stack_delegate)GetDelegateFromDll(_dllHandle, "pcre_assign_jit_stack", typeof(pcre_assign_jit_stack_delegate)); pcre_maketables = (pcre_maketables_delegate)GetDelegateFromDll(_dllHandle, "pcre_maketables", typeof(pcre_maketables_delegate)); pcre_fullinfo = (pcre_fullinfo_delegate)GetDelegateFromDll(_dllHandle, "pcre_fullinfo", typeof(pcre_fullinfo_delegate)); pcre_refcount = (pcre_refcount_delegate)GetDelegateFromDll(_dllHandle, "pcre_refcount", typeof(pcre_refcount_delegate)); pcre_config = (pcre_config_delegate)GetDelegateFromDll(_dllHandle, "pcre_config", typeof(pcre_config_delegate)); pcre_version = (pcre_version_delegate)GetDelegateFromDll(_dllHandle, "pcre_version", typeof(pcre_version_delegate)); pcre_pattern_to_host_byte_order = (pcre_pattern_to_host_byte_order_delegate)GetDelegateFromDll(_dllHandle, "pcre_pattern_to_host_byte_order", typeof(pcre_pattern_to_host_byte_order_delegate)); } }
public Allocator() { pointers = new HashSet <IntPtr>(); pointerMemory = new Dictionary <IntPtr, long>(); _allocationFunction = Allocation; _freeFunction = Free; _reallocationFunction = Reallocation; AllocationCallbacks = new AllocationCallbacks { Allocation = Marshal.GetFunctionPointerForDelegate(_allocationFunction), Free = Marshal.GetFunctionPointerForDelegate(_freeFunction), Reallocation = Marshal.GetFunctionPointerForDelegate(_reallocationFunction), }; }
static PcreWrapper() { IntPtr dllHandle = LoadLibrary(DllName); if (dllHandle != IntPtr.Zero) { IntPtr ptr; ptr = GetVarFromDll(dllHandle, "pcre_free"); pcre_free = (FreeDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(FreeDelegate)); ptr = GetVarFromDll(dllHandle, "pcre_stack_free"); pcre_stack_free = (FreeDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(FreeDelegate)); ptr = GetVarFromDll(dllHandle, "pcre_malloc"); pcre_malloc = (AllocDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(AllocDelegate)); ptr = GetVarFromDll(dllHandle, "pcre_stack_malloc"); pcre_stack_malloc = (AllocDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(AllocDelegate)); iptr_pcre_callout = GetProcAddress(dllHandle, "pcre_callout"); } }
public void Reset(int initialSize, FreeDelegate destructor) { _numUsed = 0; if (destructor != null) { while (free.Count > initialSize) { destructor(free.Pop()); } } else { while (free.Count > initialSize) { free.Pop(); } } }
/// <summary> /// Creates a new ccrush# instance. <para> </para> /// Make sure to create one only once and cache it as needed, since loading the DLLs into memory can negatively affect the performance. /// <param name="sharedLibPathOverride">[OPTIONAL] Don't look for a <c>lib/</c> folder and directly use this path as a pre-resolved, platform-specific shared lib/DLL file path. Pass this if you want to handle the various platform's paths yourself.</param> /// </summary> public CcrushSharpContext(string sharedLibPathOverride = null) { string os; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { os = "windows"; loadUtils = new SharedLibLoadUtilsWindows(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { os = "linux"; loadUtils = new SharedLibLoadUtilsLinux(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { os = "mac"; loadUtils = new SharedLibLoadUtilsMac(); } else { throw new PlatformNotSupportedException("Unsupported OS"); } if (!string.IsNullOrEmpty(sharedLibPathOverride)) { LoadedLibraryPath = sharedLibPathOverride; } else { string cpu; switch (RuntimeInformation.ProcessArchitecture) { case Architecture.X64: cpu = "x64"; break; case Architecture.X86: cpu = "x86"; break; case Architecture.Arm: cpu = "armeabi-v7a"; break; case Architecture.Arm64: cpu = "arm64-v8a"; break; default: throw new PlatformNotSupportedException("CPU Architecture not supported!"); } string path = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) ?? "."), "lib", cpu, os); if (!Directory.Exists(path)) { throw new PlatformNotSupportedException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. "); } bool found = false; foreach (string file in Directory.GetFiles(path)) { if (file.ToLower().Contains("ccrush")) { LoadedLibraryPath = Path.GetFullPath(Path.Combine(path, file)); found = true; break; } } if (!found) { throw new FileLoadException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. "); } } lib = loadUtils.LoadLibrary(LoadedLibraryPath); if (lib == IntPtr.Zero) { goto hell; } IntPtr compress = loadUtils.GetProcAddress(lib, "ccrush_compress"); if (compress == IntPtr.Zero) { goto hell; } IntPtr decompress = loadUtils.GetProcAddress(lib, "ccrush_decompress"); if (decompress == IntPtr.Zero) { goto hell; } IntPtr free = loadUtils.GetProcAddress(lib, "ccrush_free"); if (free == IntPtr.Zero) { goto hell; } IntPtr getVersionNumber = loadUtils.GetProcAddress(lib, "ccrush_get_version_nr"); if (getVersionNumber == IntPtr.Zero) { goto hell; } IntPtr getVersionNumberString = loadUtils.GetProcAddress(lib, "ccrush_get_version_nr_string"); if (getVersionNumberString == IntPtr.Zero) { goto hell; } compressDelegate = Marshal.GetDelegateForFunctionPointer <CompressDelegate>(compress); decompressDelegate = Marshal.GetDelegateForFunctionPointer <DecompressDelegate>(decompress); freeDelegate = Marshal.GetDelegateForFunctionPointer <FreeDelegate>(free); var getVersionNumberDelegate = Marshal.GetDelegateForFunctionPointer <GetVersionNumberDelegate>(getVersionNumber); var getVersionNumberStringDelegate = Marshal.GetDelegateForFunctionPointer <GetVersionNumberStringDelegate>(getVersionNumberString); Version = getVersionNumberDelegate.Invoke(); VersionString = Marshal.PtrToStringAnsi(getVersionNumberStringDelegate.Invoke()); return; hell: throw new Exception($"Failed to load one or more functions from the shared library \"{LoadedLibraryPath}\"!"); }
public CustomAllocatedObjectPool(AllocateDelegate allocate, FreeDelegate free, int initialSize) : this(allocate, free, initialSize, 0) { }
public CustomAllocatedObjectPool(AllocateDelegate allocate, FreeDelegate free) : this(allocate, free, 0, 0) { }
/// <summary> /// Creates a new pwcrypt instance. <para> </para> /// Make sure to create one only once and cache it as needed, since loading the DLLs into memory can negatively affect the performance. /// <param name="sharedLibPathOverride">[OPTIONAL] Don't look for a <c>lib/</c> folder and directly use this path as a pre-resolved, platform-specific shared lib/DLL file path. Pass this if you want to handle the various platform's paths yourself.</param> /// </summary> public PwcryptSharpContext(string sharedLibPathOverride = null) { string os; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { os = "windows"; loadUtils = new SharedLibLoadUtilsWindows(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { os = "linux"; loadUtils = new SharedLibLoadUtilsLinux(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { os = "mac"; loadUtils = new SharedLibLoadUtilsMac(); } else { throw new PlatformNotSupportedException("Unsupported OS"); } if (!string.IsNullOrEmpty(sharedLibPathOverride)) { LoadedLibraryPath = sharedLibPathOverride; } else { string cpu = RuntimeInformation.ProcessArchitecture switch { Architecture.X64 => "x64", Architecture.X86 => "x86", Architecture.Arm => "armeabi-v7a", Architecture.Arm64 => "arm64-v8a", _ => throw new PlatformNotSupportedException("CPU Architecture not supported!") }; string path = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) ?? "."), "lib", cpu, os); if (!Directory.Exists(path)) { throw new PlatformNotSupportedException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. "); } bool found = false; foreach (string file in Directory.GetFiles(path)) { if (file.ToLower().Contains("pwcrypt")) { LoadedLibraryPath = Path.GetFullPath(Path.Combine(path, file)); found = true; break; } } if (!found) { throw new FileLoadException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. "); } } lib = loadUtils.LoadLibrary(LoadedLibraryPath); if (lib == IntPtr.Zero) { goto hell; } IntPtr enableFprintf = loadUtils.GetProcAddress(lib, "pwcrypt_enable_fprintf"); if (enableFprintf == IntPtr.Zero) { goto hell; } IntPtr disableFprintf = loadUtils.GetProcAddress(lib, "pwcrypt_disable_fprintf"); if (disableFprintf == IntPtr.Zero) { goto hell; } IntPtr isFprintfEnabled = loadUtils.GetProcAddress(lib, "pwcrypt_is_fprintf_enabled"); if (isFprintfEnabled == IntPtr.Zero) { goto hell; } IntPtr getVersionNumber = loadUtils.GetProcAddress(lib, "pwcrypt_get_version_nr"); if (getVersionNumber == IntPtr.Zero) { goto hell; } IntPtr getVersionNumberString = loadUtils.GetProcAddress(lib, "pwcrypt_get_version_nr_string"); if (getVersionNumberString == IntPtr.Zero) { goto hell; } IntPtr getArgon2VersionNumber = loadUtils.GetProcAddress(lib, "pwcrypt_get_argon2_version_nr"); if (getArgon2VersionNumber == IntPtr.Zero) { goto hell; } IntPtr devUrandom = loadUtils.GetProcAddress(lib, "dev_urandom"); if (devUrandom == IntPtr.Zero) { goto hell; } IntPtr getFilesize = loadUtils.GetProcAddress(lib, "pwcrypt_get_filesize"); if (getFilesize == IntPtr.Zero) { goto hell; } IntPtr assessPasswordStrength = loadUtils.GetProcAddress(lib, "pwcrypt_assess_password_strength"); if (assessPasswordStrength == IntPtr.Zero) { goto hell; } IntPtr encrypt = loadUtils.GetProcAddress(lib, "pwcrypt_encrypt"); if (encrypt == IntPtr.Zero) { goto hell; } IntPtr decrypt = loadUtils.GetProcAddress(lib, "pwcrypt_decrypt"); if (decrypt == IntPtr.Zero) { goto hell; } IntPtr free = loadUtils.GetProcAddress(lib, "pwcrypt_free"); if (free == IntPtr.Zero) { goto hell; } enableFprintfDelegate = Marshal.GetDelegateForFunctionPointer <EnableFprintfDelegate>(enableFprintf); disableFprintfDelegate = Marshal.GetDelegateForFunctionPointer <DisableFprintfDelegate>(disableFprintf); isFprintfEnabledDelegate = Marshal.GetDelegateForFunctionPointer <IsFprintfEnabledDelegate>(isFprintfEnabled); getVersionNumberDelegate = Marshal.GetDelegateForFunctionPointer <GetVersionNumberDelegate>(getVersionNumber); getVersionNumberStringDelegate = Marshal.GetDelegateForFunctionPointer <GetVersionNumberStringDelegate>(getVersionNumberString); getArgon2VersionNumberDelegate = Marshal.GetDelegateForFunctionPointer <GetArgon2VersionNumberDelegate>(getArgon2VersionNumber); devUrandomDelegate = Marshal.GetDelegateForFunctionPointer <DevUrandomDelegate>(devUrandom); getFilesizeDelegate = Marshal.GetDelegateForFunctionPointer <GetFilesizeDelegate>(getFilesize); assessPasswordStrengthDelegate = Marshal.GetDelegateForFunctionPointer <AssessPasswordStrengthDelegate>(assessPasswordStrength); encryptDelegate = Marshal.GetDelegateForFunctionPointer <EncryptDelegate>(encrypt); decryptDelegate = Marshal.GetDelegateForFunctionPointer <DecryptDelegate>(decrypt); freeDelegate = Marshal.GetDelegateForFunctionPointer <FreeDelegate>(free); return; hell: throw new Exception($"Failed to load one or more functions from the shared library \"{LoadedLibraryPath}\"!"); }