|
| 1 | +// |
| 2 | +// NativeLibraryHelper.cs |
| 3 | +// This file is part of Ghostscript.NET library |
| 4 | +// |
| 5 | +// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan) |
| 6 | +// Copyright (c) 2013-2014 by Josip Habjan. All rights reserved. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining |
| 9 | +// a copy of this software and associated documentation files (the |
| 10 | +// "Software"), to deal in the Software without restriction, including |
| 11 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 12 | +// distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | +// permit persons to whom the Software is furnished to do so, subject to |
| 14 | +// the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be |
| 17 | +// included in all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 23 | +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 24 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +using System; |
| 28 | +using System.IO; |
| 29 | +using Microsoft.WinAny; |
| 30 | + |
| 31 | +namespace Ghostscript.NET |
| 32 | +{ |
| 33 | + /// <summary> |
| 34 | + /// Class that helps us to get various information about native libraries. |
| 35 | + /// </summary> |
| 36 | + internal class NativeLibraryHelper |
| 37 | + { |
| 38 | + /// <summary> |
| 39 | + /// Gets the image file machine type. |
| 40 | + /// </summary> |
| 41 | + /// <param name="path">Native library path.</param> |
| 42 | + /// <returns>Image file machine type.</returns> |
| 43 | + public static ushort GetImageFileMachineType(string path) |
| 44 | + { |
| 45 | + using(FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 46 | + { |
| 47 | + return GetImageFileMachineType(fs); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the image file machine type. |
| 53 | + /// </summary> |
| 54 | + /// <param name="buffer">Memory buffer representing native library.</param> |
| 55 | + /// <returns>Image file machine type.</returns> |
| 56 | + public static unsafe ushort GetImageFileMachineType(byte[] buffer) |
| 57 | + { |
| 58 | + fixed(byte *ptr = buffer) |
| 59 | + { |
| 60 | + using(UnmanagedMemoryStream ums = new UnmanagedMemoryStream(ptr, buffer.Length)) |
| 61 | + { |
| 62 | + return GetImageFileMachineType(ums); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets the image file machine type. |
| 69 | + /// </summary> |
| 70 | + /// <param name="srm">Stream representing native library.</param> |
| 71 | + /// <returns>Image file machine type.</returns> |
| 72 | + public static ushort GetImageFileMachineType(Stream srm) |
| 73 | + { |
| 74 | + using (BinaryReader reader = new BinaryReader(srm)) |
| 75 | + { |
| 76 | + // seek to the IMAGE_DOS_HEADER->e_lfanew position |
| 77 | + reader.BaseStream.Seek(0x3c, SeekOrigin.Begin); |
| 78 | + // read out the value |
| 79 | + uint e_lfanew = reader.ReadUInt32(); |
| 80 | + // seek the the file address of new exe header |
| 81 | + reader.BaseStream.Seek(e_lfanew, SeekOrigin.Begin); |
| 82 | + // read out the signature |
| 83 | + uint signature = reader.ReadUInt32(); |
| 84 | + // check if it's a signature we can handle |
| 85 | + if (signature != WinNT.IMAGE_NT_SIGNATURE) |
| 86 | + { |
| 87 | + return WinNT.IMAGE_FILE_MACHINE_UNKNOWN; |
| 88 | + } |
| 89 | + |
| 90 | + // read out and return IMAGE_FILE_HEADER->Machine value |
| 91 | + return reader.ReadUInt16(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Gets if native library is compiled as 64bit library. |
| 97 | + /// </summary> |
| 98 | + /// <param name="path">Native library path.</param> |
| 99 | + /// <returns>True if native library is compiled as 64 bit library.</returns> |
| 100 | + public static bool Is64BitLibrary(string path) |
| 101 | + { |
| 102 | + ushort machine = GetImageFileMachineType(path); |
| 103 | + return Is64BitMachineValue(machine); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Gets if native library is compiled as 64bit library. |
| 108 | + /// </summary> |
| 109 | + /// <param name="buffer">Memory buffer representing native library.</param> |
| 110 | + /// <returns>True if native library is compiled as 64 bit library.</returns> |
| 111 | + public static bool Is64BitLibrary(byte[] buffer) |
| 112 | + { |
| 113 | + ushort machine = GetImageFileMachineType(buffer); |
| 114 | + return Is64BitMachineValue(machine); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Gets if machine value represents 64 bit machine. |
| 119 | + /// </summary> |
| 120 | + /// <param name="machine">IMAGE_FILE_HEADER->Machine value.</param> |
| 121 | + private static bool Is64BitMachineValue(ushort machine) |
| 122 | + { |
| 123 | + switch(machine) |
| 124 | + { |
| 125 | + case WinNT.IMAGE_FILE_MACHINE_AMD64: |
| 126 | + case WinNT.IMAGE_FILE_MACHINE_IA64: |
| 127 | + return true; |
| 128 | + case WinNT.IMAGE_FILE_MACHINE_I386: |
| 129 | + return false; |
| 130 | + default: |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | +} |
0 commit comments