diff --git a/src/PDFtoImage/PdfiumViewer/NativeMethods.Pdfium.cs b/src/PDFtoImage/PdfiumViewer/NativeMethods.Pdfium.cs index a77ae40e..fd38de93 100644 --- a/src/PDFtoImage/PdfiumViewer/NativeMethods.Pdfium.cs +++ b/src/PDFtoImage/PdfiumViewer/NativeMethods.Pdfium.cs @@ -343,120 +343,112 @@ private static int FPDF_GetBlock(IntPtr param, uint position, IntPtr buffer, uin } #endregion - private const string _dllName = -#if NET6_0_OR_GREATER - "libpdfium" -#else - "pdfium" -#endif - ; - private static class Imports { - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_InitLibrary(); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_DestroyLibrary(); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public static extern IntPtr FPDF_LoadCustomDocument([MarshalAs(UnmanagedType.LPStruct)] FPDF_FILEACCESS access, string? password); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_CloseDocument(IntPtr document); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern int FPDF_GetPageCount(IntPtr document); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern uint FPDF_GetDocPermissions(IntPtr document); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr FPDFDOC_InitFormFillEnvironment(IntPtr document, FPDF_FORMFILLINFO formInfo); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_SetFormFieldHighlightColor(IntPtr hHandle, int fieldType, uint color); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_SetFormFieldHighlightAlpha(IntPtr hHandle, byte alpha); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FORM_DoDocumentJSAction(IntPtr hHandle); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FORM_DoDocumentOpenAction(IntPtr hHandle); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDFDOC_ExitFormFillEnvironment(IntPtr hHandle); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FORM_DoDocumentAAction(IntPtr hHandle, FPDFDOC_AACTION aaType); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr FPDF_LoadPage(IntPtr document, int page_index); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr FPDFText_LoadPage(IntPtr page); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FORM_OnAfterLoadPage(IntPtr page, IntPtr _form); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FORM_DoPageAAction(IntPtr page, IntPtr _form, FPDFPAGE_AACTION fPDFPAGE_AACTION); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern double FPDF_GetPageWidth(IntPtr page); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern double FPDF_GetPageHeight(IntPtr page); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FORM_OnBeforeClosePage(IntPtr page, IntPtr _form); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDFText_ClosePage(IntPtr text_page); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_ClosePage(IntPtr page); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_RenderPageBitmap(IntPtr bitmapHandle, IntPtr page, int start_x, int start_y, int size_x, int size_y, int rotate, FPDF flags); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern int FPDF_GetPageSizeByIndex(IntPtr document, int page_index, out double width, out double height); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr FPDFBitmap_CreateEx(int width, int height, int format, IntPtr first_scan, int stride); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDFBitmap_FillRect(IntPtr bitmapHandle, int left, int top, int width, int height, uint color); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDFBitmap_Destroy(IntPtr bitmapHandle); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern uint FPDFDest_GetDestPageIndex(IntPtr document, IntPtr dest); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr FPDFBookmark_GetFirstChild(IntPtr document, IntPtr bookmark); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr FPDFBookmark_GetNextSibling(IntPtr document, IntPtr bookmark); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern uint FPDFBookmark_GetTitle(IntPtr bookmark, byte[]? buffer, uint buflen); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr FPDFBookmark_GetDest(IntPtr document, IntPtr bookmark); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern uint FPDF_GetLastError(); #region Save/Edit APIs - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_FFLDraw(IntPtr form, IntPtr bitmap, IntPtr page, int start_x, int start_y, int size_x, int size_y, int rotate, FPDF flags); - [DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)] + [DllImport("pdfium", CallingConvention = CallingConvention.Cdecl)] public static extern void FPDF_RemoveFormFieldHighlight(IntPtr form); #endregion } diff --git a/src/PDFtoImage/PdfiumViewer/NativeMethods.cs b/src/PDFtoImage/PdfiumViewer/NativeMethods.cs index ce1ff557..b6d34524 100644 --- a/src/PDFtoImage/PdfiumViewer/NativeMethods.cs +++ b/src/PDFtoImage/PdfiumViewer/NativeMethods.cs @@ -10,46 +10,12 @@ internal static partial class NativeMethods { static NativeMethods() { - string? path = null; - try - { -#if NET7_0_OR_GREATER - string workingDirectory = Assembly.GetExecutingAssembly().Location; - if (string.IsNullOrWhiteSpace(workingDirectory)) - workingDirectory = Environment.ProcessPath!; - if (string.IsNullOrWhiteSpace(workingDirectory)) - workingDirectory = AppContext.BaseDirectory; -#elif NET6_0_OR_GREATER - var workingDirectory = Assembly.GetExecutingAssembly().GetName(false).CodeBase; - if (string.IsNullOrWhiteSpace(workingDirectory)) - workingDirectory = Environment.ProcessPath!; - if (string.IsNullOrWhiteSpace(workingDirectory)) - workingDirectory = AppContext.BaseDirectory; -#else - var workingDirectory = Assembly.GetExecutingAssembly().GetName(false).CodeBase; - if (string.IsNullOrWhiteSpace(workingDirectory)) - workingDirectory = Process.GetCurrentProcess().MainModule!.FileName!; - if (string.IsNullOrWhiteSpace(workingDirectory)) - workingDirectory = AppContext.BaseDirectory; -#endif - - path = Path.GetDirectoryName(new Uri(workingDirectory).LocalPath); - } - catch (Exception) { } - - LoadNativeLibrary(path); - } - - private static void LoadNativeLibrary(string? path = null) - { #if ANDROID || MONOANDROID LoadNativeLibraryAndroid(); -#elif NET6_0_OR_GREATER - LoadNativeLibraryNetCore(path); #elif NETFRAMEWORK - LoadNativeLibraryNetFX(path); + LoadNativeLibraryNetFX(); #else - throw new PlatformNotSupportedException("Unkown framework and/or platform."); + // .NET (Core) resolves the pdfium lib on its own #endif } @@ -69,105 +35,19 @@ private static void LoadNativeLibraryAndroid() Java.Lang.JavaSystem.LoadLibrary("pdfium"); } -#elif NET6_0_OR_GREATER - private static void LoadNativeLibraryNetCore(string? path) - { - if (path == null) - return; - - string runtimeIdentifier; - string pdfiumLibName; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - runtimeIdentifier = RuntimeInformation.ProcessArchitecture switch - { - Architecture.X86 => "win-x86", - Architecture.X64 => "win-x64", - Architecture.Arm64 => "win-arm64", - _ => throw new PlatformNotSupportedException("Only x86-64, x86 and arm64 are supported on Windows.") - }; - pdfiumLibName = "pdfium.dll"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - runtimeIdentifier = RuntimeInformation.ProcessArchitecture switch - { - Architecture.X64 => "linux-x64", - Architecture.Arm => "linux-arm", - Architecture.Arm64 => "linux-arm64", - _ => throw new PlatformNotSupportedException("Only x86-64, arm and arm64 are supported on Linux.") - }; - pdfiumLibName = "libpdfium.so"; - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - runtimeIdentifier = RuntimeInformation.ProcessArchitecture switch - { - Architecture.X64 => "osx-x64", - Architecture.Arm64 => "osx-arm64", - _ => throw new PlatformNotSupportedException("Only x86-64 and arm64 are supported on macOS.") - }; - pdfiumLibName = "libpdfium.dylib"; - } - else if (OperatingSystem.IsMacCatalyst()) - { - throw new NotSupportedException("Mac Catalyst is not supported."); - } - else if (OperatingSystem.IsIOS()) - { - throw new NotSupportedException("iOS and Mac Catalyst are not supported."); - } - else if (OperatingSystem.IsTvOS()) - { - throw new NotSupportedException("tvOS is not supported."); - } - else - { - throw new NotSupportedException("Only win-x86, win-x64, win-arm64, linux-x64, linux-arm, linux-arm64, osx-x64, and osx-arm64 are supported."); - } - - LoadLibrary(path, runtimeIdentifier, pdfiumLibName); - } -#endif - -#if !MONOANDROID && !ANDROID - private static string? _pdfiumLibPath; -#endif - -#if NET6_0_OR_GREATER && !ANDROID - private static void LoadLibrary(string path, string runtimeIdentifier, string pdfiumLibName) - { - if (File.Exists(Path.Combine(path, pdfiumLibName))) - { - // dotnet publish with a given runtime identifier (not portable) will put PDFium into the root folder - _pdfiumLibPath = Path.Combine(path, pdfiumLibName); - } - else - { - // in any other case there should be a runtimes folder - _pdfiumLibPath = Path.Combine(path, "runtimes", runtimeIdentifier, "native", pdfiumLibName); - } - - NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, ImportResolver); - NativeLibrary.Load(_pdfiumLibPath, Assembly.GetExecutingAssembly(), default); - } - - private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) - { - if (_pdfiumLibPath != null && libraryName != null && libraryName.Contains("pdfium")) - return NativeLibrary.Load(_pdfiumLibPath, assembly, searchPath); - - return IntPtr.Zero; - } #elif NETFRAMEWORK - private static void LoadNativeLibraryNetFX(string? path) + private static void LoadNativeLibraryNetFX() { - if (path == null) - return; + var workingDirectory = Assembly.GetExecutingAssembly().GetName(false).CodeBase; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = Process.GetCurrentProcess().MainModule!.FileName!; + if (string.IsNullOrWhiteSpace(workingDirectory)) + workingDirectory = AppContext.BaseDirectory; + + var path = Path.GetDirectoryName(new Uri(workingDirectory).LocalPath); string runtimeIdentifier; - + #if NET471_OR_GREATER runtimeIdentifier = RuntimeInformation.ProcessArchitecture switch { @@ -184,24 +64,25 @@ private static void LoadNativeLibraryNetFX(string? path) }; #endif - _pdfiumLibPath = Path.Combine(path, runtimeIdentifier, "pdfium.dll"); - LoadLibrary(_pdfiumLibPath); + var pdfiumLibPath = Path.Combine(path, runtimeIdentifier, "pdfium.dll"); + LoadLibrary(pdfiumLibPath); } - /// Loads the specified module into the address space of the calling process. - /// - /// The name of the module. This can be either a library module (a .dll file) or an executable module (an .exe file). The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file. If the string specifies a full path, the function searches only that path for the module. If the string specifies a relative path or a module name without a path, the function uses a standard search strategy to find the module; for more information, see the Remarks. If the function cannot find the module, the function fails. When specifying a path, be sure to use backslashes (\\), not forward slashes (/). For more information about paths, see Naming a File or Directory. If the string specifies a module name without a path and the file name extension is omitted, the function appends the default library extension .dll to the module name. To prevent the function from appending .dll to the module name, include a trailing point character (.) in the module name string. - /// Read more on docs.microsoft.com. - /// - /// - /// If the function succeeds, the return value is a handle to the module. If the function fails, the return value is NULL. To get extended error information, call GetLastError. - /// - /// - /// Learn more about this API from docs.microsoft.com. - /// - [DllImport("Kernel32", ExactSpelling = true, EntryPoint = "LoadLibraryW", SetLastError = true)] + /// Loads the specified module into the address space of the calling process. (LoadLibraryW) + /// + /// The name of the module. This can be either a library module (a .dll file) or an executable module (an .exe file). If the specified module is an executable module, static imports are not loaded; instead, the module is loaded as if by LoadLibraryEx with the `DONT_RESOLVE_DLL_REFERENCES` flag. The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file. If the string specifies a full path, the function searches only that path for the module. If the string specifies a relative path or a module name without a path, the function uses a standard search strategy to find the module; for more information, see the Remarks. If the function cannot find the module, the function fails. When specifying a path, be sure to use backslashes (\\), not forward slashes (/). For more information about paths, see Naming a File or Directory. If the string specifies a module name without a path and the file name extension is omitted, the function appends the default library extension ".DLL" to the module name. To prevent the function from appending ".DLL" to the module name, include a trailing point character (.) in the module name string. + /// Read more on docs.microsoft.com. + /// + /// + /// If the function succeeds, the return value is a handle to the module. If the function fails, the return value is NULL. To get extended error information, call GetLastError. + /// + /// + /// To enable or disable error messages displayed by the loader during DLL loads, use the SetErrorMode function. LoadLibrary can be used to load a library module into the address space of the process and return a handle that can be used in GetProcAddress to get the address of a DLL function. LoadLibrary can also be used to load other executable modules. For example, the function can specify an .exe file to get a handle that can be used in FindResource or LoadResource. However, do not use LoadLibrary to run an .exe file. Instead, use the CreateProcess function. If the specified module is a DLL that is not already loaded for the calling process, the system calls the DLL's DllMain function with the DLL_PROCESS_ATTACH value. If DllMain returns TRUE, LoadLibrary returns a handle to the module. If DllMain returns FALSE, the system unloads the DLL from the process address space and LoadLibrary returns NULL. It is not safe to call LoadLibrary from DllMain. For more information, see the Remarks section in DllMain. Module handles are not global or inheritable. A call to LoadLibrary by one process does not produce a handle that another process can use — for example, in calling GetProcAddress. The other process must make its own call to LoadLibrary for the module before calling GetProcAddress. If lpFileName does not include a path and there is more than one loaded module with the same base name and extension, the function returns a handle to the module that was loaded first. If no file name extension is specified in the lpFileName parameter, the default library extension .dll is appended. However, the file name string can include a trailing point character (.) to indicate that the module name has no extension. When no path is specified, the function searches for loaded modules whose base name matches the base name of the module to be loaded. If the name matches, the load succeeds. Otherwise, the function searches for the file. The first directory searched is the directory containing the image file used to create the calling process (for more information, see the CreateProcess function). Doing this allows private dynamic-link library (DLL) files associated with a process to be found without adding the process's installed directory to the PATH environment variable. If a relative path is specified, the entire relative path is appended to every token in the DLL search path list. To load a module from a relative path without searching any other path, use GetFullPathName to get a nonrelative path and call LoadLibrary with the nonrelative path. For more information on the DLL search order, see Dynamic-Link Library Search Order. The search path can be altered using the SetDllDirectory function. This solution is recommended instead of using SetCurrentDirectory or hard-coding the full path to the DLL. If a path is specified and there is a redirection file for the application, the function searches for the module in the application's directory. If the module exists in the application's directory, LoadLibrary ignores the specified path and loads the module from the application's directory. If the module does not exist in the application's directory, LoadLibrary loads the module from the specified directory. For more information, see Dynamic Link Library Redirection. If you call LoadLibrary with the name of an assembly without a path specification and the assembly is listed in the system compatible manifest, the call is automatically redirected to the side-by-side assembly. The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count). Windows Server 2003 and Windows XP:  The Visual C++ compiler supports a syntax that enables you to declare thread-local variables: _declspec(thread). If you use this syntax in a DLL, you will not be able to load the DLL explicitly using LoadLibrary on versions of Windows prior to Windows Vista. If your DLL will be loaded explicitly, you must use the thread local storage functions instead of _declspec(thread). For an example, see Using Thread Local Storage in a Dynamic Link Library.

Security Remarks

Do not use the SearchPath function to retrieve a path to a DLL for a subsequent LoadLibrary call. The SearchPath function uses a different search order than LoadLibrary and it does not use safe process search mode unless this is explicitly enabled by calling SetSearchPathMode with BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE. Therefore, SearchPath is likely to first search the user’s current working directory for the specified DLL. If an attacker has copied a malicious version of a DLL into the current working directory, the path retrieved by SearchPath will point to the malicious DLL, which LoadLibrary will then load. Do not make assumptions about the operating system version based on a LoadLibrary call that searches for a DLL. If the application is running in an environment where the DLL is legitimately not present but a malicious version of the DLL is in the search path, the malicious version of the DLL may be loaded. Instead, use the recommended techniques described in Getting the System Version.
+ /// Read more on docs.microsoft.com. + ///
+ [DllImport("kernel32.dll", ExactSpelling = true, EntryPoint = "LoadLibraryW", SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPWStr)] string lpLibFileName); #endif } -} +} \ No newline at end of file