Fix a Mach-O magic check. Spotted by mbealby@gmail.com.
[reactos.git] / reactos / lib / kernel32 / file / bintype.c
index fa8e6ce..11e1498 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: bintype.c,v 1.1 2004/05/02 14:47:05 weiden Exp $
+/* $Id$
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
 /* INCLUDES *****************************************************************/
 
 #include <k32.h>
-#include <ddk/ntifs.h>
 
 #define NDEBUG
 #include "../include/debug.h"
 
-
 /* FUNCTIONS ****************************************************************/
 
 /* Check whether a file is an OS/2 or a very old Windows executable
@@ -27,7 +25,7 @@
  * FIXME: is reading the module imports the only way of discerning
  *        old Windows binaries from OS/2 ones ? At least it seems so...
  */
-STATIC DWORD STDCALL
+static DWORD STDCALL
 InternalIsOS2OrOldWin(HANDLE hFile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
 {
   DWORD CurPos;
@@ -35,28 +33,28 @@ InternalIsOS2OrOldWin(HANDLE hFile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
   LPSTR nametab = NULL;
   DWORD Read, Ret;
   int i;
-  
+
   Ret = BINARY_OS216;
   CurPos = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
-  
+
   /* read modref table */
-  if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_modtab, NULL, FILE_BEGIN) == -1) ||
+  if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_modtab, NULL, FILE_BEGIN) == (DWORD)-1) ||
      (!(modtab = HeapAlloc(GetProcessHeap(), 0, ne->ne_cmod * sizeof(WORD)))) ||
      (!(ReadFile(hFile, modtab, ne->ne_cmod * sizeof(WORD), &Read, NULL))) ||
-     (Read != ne->ne_cmod * sizeof(WORD)))
+     (Read != (DWORD)ne->ne_cmod * sizeof(WORD)))
   {
     goto broken;
   }
-  
+
   /* read imported names table */
-  if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_imptab, NULL, FILE_BEGIN) == -1) ||
+  if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_imptab, NULL, FILE_BEGIN) == (DWORD)-1) ||
      (!(nametab = HeapAlloc(GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab))) ||
      (!(ReadFile(hFile, nametab, ne->ne_enttab - ne->ne_imptab, &Read, NULL))) ||
-     (Read != ne->ne_enttab - ne->ne_imptab))
+     (Read != (DWORD)ne->ne_enttab - ne->ne_imptab))
   {
     goto broken;
   }
-  
+
   for(i = 0; i < ne->ne_cmod; i++)
   {
     LPSTR module;
@@ -68,10 +66,10 @@ InternalIsOS2OrOldWin(HANDLE hFile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
       goto done;
     }
   }
-  
+
   broken:
   DPRINT("InternalIsOS2OrOldWin(): Binary file seems to be broken\n");
-  
+
   done:
   HeapFree(GetProcessHeap(), 0, modtab);
   HeapFree(GetProcessHeap(), 0, nametab);
@@ -79,7 +77,7 @@ InternalIsOS2OrOldWin(HANDLE hFile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
   return Ret;
 }
 
-STATIC DWORD STDCALL
+static DWORD STDCALL
 InternalGetBinaryType(HANDLE hFile)
 {
   union
@@ -101,14 +99,14 @@ InternalGetBinaryType(HANDLE hFile)
   } Header;
   char magic[4];
   DWORD Read;
-  
-  if((SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == -1) ||
+
+  if((SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == (DWORD)-1) ||
      (!ReadFile(hFile, &Header, sizeof(Header), &Read, NULL) ||
       (Read != sizeof(Header))))
   {
     return BINARY_UNKNOWN;
   }
-  
+
   if(!memcmp(Header.elf.magic, "\177ELF", sizeof(Header.elf.magic)))
   {
     /* FIXME: we don't bother to check byte order, architecture, etc. */
@@ -121,10 +119,10 @@ InternalGetBinaryType(HANDLE hFile)
     }
     return BINARY_UNKNOWN;
   }
-  
+
   /* Mach-o File with Endian set to Big Endian  or Little Endian*/
   if(Header.macho.magic == 0xFEEDFACE ||
-     Header.macho.magic == 0xECAFDEEF)
+     Header.macho.magic == 0xCEFAEDFE)
   {
     switch(Header.macho.filetype)
     {
@@ -134,7 +132,7 @@ InternalGetBinaryType(HANDLE hFile)
     }
     return BINARY_UNKNOWN;
   }
-  
+
   /* Not ELF, try DOS */
   if(Header.mz.e_magic == IMAGE_DOS_SIGNATURE)
   {
@@ -145,13 +143,13 @@ InternalGetBinaryType(HANDLE hFile)
      * This will tell us if there is more header information
      * to read or not.
      */
-    if((SetFilePointer(hFile, Header.mz.e_lfanew, NULL, FILE_BEGIN) == -1) ||
+    if((SetFilePointer(hFile, Header.mz.e_lfanew, NULL, FILE_BEGIN) == (DWORD)-1) ||
        (!ReadFile(hFile, magic, sizeof(magic), &Read, NULL) ||
         (Read != sizeof(magic))))
     {
       return BINARY_DOS;
     }
-    
+
     /* Reading the magic field succeeded so
      * we will try to determine what type it is.
      */
@@ -163,14 +161,14 @@ InternalGetBinaryType(HANDLE hFile)
       {
         return BINARY_DOS;
       }
-      
+
       /* FIXME - detect 32/64 bit */
-      
+
       if(FileHeader.Characteristics & IMAGE_FILE_DLL)
         return BINARY_PE_DLL32;
       return BINARY_PE_EXE32;
     }
-    
+
     if(!memcmp(magic, "NE", 1))
     {
       /* This is a Windows executable (NE) header.  This can
@@ -186,7 +184,7 @@ InternalGetBinaryType(HANDLE hFile)
         /* Couldn't read header, so abort. */
         return BINARY_DOS;
       }
-      
+
       switch(ne.ne_exetyp)
       {
         case 2:
@@ -214,29 +212,29 @@ GetBinaryTypeW (
 {
   HANDLE hFile;
   DWORD BinType;
-  
+
   if(!lpApplicationName || !lpBinaryType)
   {
     SetLastError(ERROR_INVALID_PARAMETER);
     return FALSE;
   }
-  
+
   hFile = CreateFileW(lpApplicationName, GENERIC_READ, FILE_SHARE_READ, NULL,
                       OPEN_EXISTING, 0, 0);
   if(hFile == INVALID_HANDLE_VALUE)
   {
     return FALSE;
   }
-  
+
   BinType = InternalGetBinaryType(hFile);
   CloseHandle(hFile);
-  
+
   switch(BinType)
   {
     case BINARY_UNKNOWN:
     {
       WCHAR *dot;
-      
+
       /*
        * guess from filename
        */
@@ -289,7 +287,7 @@ GetBinaryTypeW (
       return FALSE;
     }
   }
-  
+
   DPRINT1("Invalid binary type returned!\n", BinType);
   return FALSE;
 }
@@ -305,33 +303,18 @@ GetBinaryTypeA (
     LPDWORD lpBinaryType
     )
 {
-  ANSI_STRING FileNameA;
-  UNICODE_STRING FileName;
-  NTSTATUS Status;
-  BOOL Ret;
-  
+  PWCHAR ApplicationNameW;
+
   if(!lpApplicationName || !lpBinaryType)
   {
     SetLastError(ERROR_INVALID_PARAMETER);
     return FALSE;
   }
-  
-  RtlInitAnsiString(&FileNameA, (LPSTR)lpApplicationName);
-  
-  if(bIsFileApiAnsi)
-    Status = RtlAnsiStringToUnicodeString(&FileName, &FileNameA, TRUE);
-  else
-    Status = RtlOemStringToUnicodeString(&FileName, &FileNameA, TRUE);
-  if(!NT_SUCCESS(Status))
-  {
-    SetLastErrorByStatus(Status);
-    return FALSE;
-  }
-  
-  Ret = GetBinaryTypeW(FileName.Buffer, lpBinaryType);
-  
-  RtlFreeUnicodeString(&FileName);
-  return Ret;
+
+  if (!(ApplicationNameW = FilenameA2W(lpApplicationName, FALSE)))
+     return FALSE;
+
+  return GetBinaryTypeW(ApplicationNameW, lpBinaryType);
 }
 
 /* EOF */