]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/utility-private.h
(no commit message)
[imagemagick] / MagickCore / utility-private.h
index 7d5380286406693a47665493cb3e10128f78e3ee..d144bbc8eee44d88584371785ec7fe44e8c0f9a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization
+  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization
   dedicated to making software imaging solutions freely available.
 
   You may not use this file except in compliance with the License.
 #ifndef _MAGICKCORE_UTILITY_PRIVATE_H
 #define _MAGICKCORE_UTILITY_PRIVATE_H
 
+#include "MagickCore/memory_.h"
+#include "MagickCore/nt-base.h"
+#include "MagickCore/nt-base-private.h"
+
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
 #endif
 
-#include "MagickCore/memory_.h"
-#include "MagickCore/nt-base-private.h"
-
 extern MagickPrivate char
   **GetPathComponents(const char *,size_t *),
   **ListFiles(const char *,const char *,size_t *);
 
 extern MagickPrivate MagickBooleanType
-  GetExecutionPath(char *,const size_t);
+  GetExecutionPath(char *,const size_t),
+  ShredFile(const char *);
 
 extern MagickPrivate ssize_t
   GetMagickPageSize(void);
@@ -44,214 +46,218 @@ extern MagickPrivate void
   Windows UTF8 compatibility methods.
 */
 
-#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
-static inline int MultiByteToWideCharacter(const char *string,
-  WCHAR **wide_string,size_t *extent)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
+static inline wchar_t *create_wchar_path(const char *utf8)
 {
-  size_t
-    length;
-
-  *extent=0;
-  if (wide_string == (WCHAR **) NULL)
-    return(0);
-  *wide_string=(WCHAR *) NULL;
-  if (string == (const char *) NULL)
-    return(0);
-  length=strlen(string)+1;
-  *wide_string=(WCHAR *) AcquireQuantumMemory(length,sizeof(*wide_string));
-  if (*wide_string == (WCHAR *) NULL)
-    return(-1);
-  return(mbstowcs_s(extent,*wide_string,length,string,_TRUNCATE));
+  int
+    count;
+  wchar_t
+    *wideChar;
+
+  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
+  if (count > MAX_PATH)
+    {
+      char
+        buffer[MaxTextExtent];
+
+      wchar_t
+        shortPath[MAX_PATH],
+        *longPath;
+
+      (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
+      count+=4;
+      longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
+      if (longPath == (wchar_t *) NULL)
+        return((wchar_t *) NULL);
+      count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
+      if (count != 0)
+        count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
+      longPath=(wchar_t *) RelinquishMagickMemory(longPath);
+      if (count < 5)
+        return((wchar_t *) NULL);
+      wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
+      wcscpy(wideChar,shortPath+4);
+      return(wideChar);
+    }
+  wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
+  if (wideChar == (wchar_t *) NULL)
+    return((wchar_t *) NULL);
+  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
+  if (count == 0)
+    {
+      wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
+      return((wchar_t *) NULL);
+    }
+  return(wideChar);
 }
 #endif
 
 static inline int access_utf8(const char *path,int mode)
 {
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
   return(access(path,mode));
 #else
    int
      status;
 
-   ssize_t
-     extent;
+   wchar_t
+     *path_wide;
 
-   WCHAR
-     *wide_path;
-
-   status=MultiByteToWideCharacter(path,&wide_path,&extent);
-   if (status != 0)
-     return(status);
-   status=_waccess(wide_path,mode);
-   wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
+     return(-1);
+   status=_waccess(path_wide,mode);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
    return(status);
 #endif
 }
 
 static inline FILE *fopen_utf8(const char *path,const char *mode)
 {
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
   return(fopen(path,mode));
 #else
    FILE
      *file;
 
-   int
-     status;
-
-   ssize_t
-     extent;
-
-   WCHAR
-     *wide_mode,
-     *wide_path;
+   wchar_t
+     *mode_wide,
+     *path_wide;
 
-   status=MultiByteToWideCharacter(path,&wide_path,&extent);
-   if (status != 0)
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
      return((FILE *) NULL);
-   status=MultiByteToWideCharacter(mode,&wide_mode,&extent);
-   if (status != 0)
+   mode_wide=create_wchar_path(mode);
+   if (mode_wide == (wchar_t *) NULL)
      {
-       wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
+       path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
        return((FILE *) NULL);
      }
-   file=_wfopen(wide_path,wide_mode);
-   wide_mode=(WCHAR *) RelinquishMagickMemory(wide_mode);
-   wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
+   file=_wfopen(path_wide,mode_wide);
+   mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
    return(file);
 #endif
 }
 
+#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
+typedef int
+  mode_t;
+#endif
+
 static inline int open_utf8(const char *path,int flags,mode_t mode)
 {
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
   return(open(path,flags,mode));
 #else
    int
      status;
 
-   ssize_t
-     extent;
-
-   WCHAR
-     *wide_path;
+   wchar_t
+     *path_wide;
 
-   status=MultiByteToWideCharacter(path,&wide_path,&extent);
-   if (status != 0)
-     return(status);
-   status=_wopen(wide_path,flags,mode);
-   wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
+     return(-1);
+   status=_wopen(path_wide,flags,mode);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
    return(status);
 #endif
 }
 
 static inline FILE *popen_utf8(const char *command,const char *type)
 {
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
-  return(fopen(command,type));
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(popen(command,type));
 #else
    FILE
      *file;
 
-   int
-     status;
-
-   ssize_t
-     extent;
-
-   WCHAR
-     *wide_type,
-     *wide_command;
+   wchar_t
+     *type_wide,
+     *command_wide;
 
-   status=MultiByteToWideCharacter(command,&wide_command,&extent);
-   if (status != 0)
+   command_wide=create_wchar_path(command);
+   if (command_wide == (wchar_t *) NULL)
      return((FILE *) NULL);
-   status=MultiByteToWideCharacter(type,&wide_type,&extent);
-   if (status != 0)
+   type_wide=create_wchar_path(type);
+   if (type_wide == (wchar_t *) NULL)
      {
-       wide_command=(WCHAR *) RelinquishMagickMemory(wide_command);
+       command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
        return((FILE *) NULL);
      }
-   file=_wpopen(wide_command,wide_type);
-   wide_type=(WCHAR *) RelinquishMagickMemory(wide_type);
-   wide_command=(WCHAR *) RelinquishMagickMemory(wide_command);
+   file=_wpopen(command_wide,type_wide);
+   type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
+   command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
    return(file);
 #endif
 }
 
 static inline int remove_utf8(const char *path)
 {
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
   return(unlink(path));
 #else
    int
      status;
 
-   ssize_t
-     extent;
-
-   WCHAR
-     *wide_path;
+   wchar_t
+     *path_wide;
 
-   status=MultiByteToWideCharacter(path,&wide_path,&extent);
-   if (status != 0)
-     return(status);
-   status=_wremove(wide_path);
-   wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
+     return(-1);
+   status=_wremove(path_wide);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
    return(status);
 #endif
 }
 
 static inline int rename_utf8(const char *source,const char *destination)
 {
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
   return(rename(source,destination));
 #else
    int
      status;
 
-   ssize_t
-     extent;
+   wchar_t
+     *destination_wide,
+     *source_wide;
 
-   WCHAR
-     *wide_destination,
-     *wide_source;
-
-   status=MultiByteToWideCharacter(source,&wide_source,&extent);
-   if (status != 0)
-     return(status);
-   status=MultiByteToWideCharacter(destination,&wide_destination,&extent);
-   if (status != 0)
+   source_wide=create_wchar_path(source);
+   if (source_wide == (wchar_t *) NULL)
+     return(-1);
+   destination_wide=create_wchar_path(destination);
+   if (destination_wide == (wchar_t *) NULL)
      {
-       wide_source=(WCHAR *) RelinquishMagickMemory(wide_source);
-       return(status);
+       source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
+       return(-1);
      }
-   status=_wrename(wide_source,wide_destination);
-   wide_destination=(WCHAR *) RelinquishMagickMemory(wide_destination);
-   wide_source=(WCHAR *) RelinquishMagickMemory(wide_source);
+   status=_wrename(source_wide,destination_wide);
+   destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
+   source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
    return(status);
 #endif
 }
 
 static inline int stat_utf8(const char *path,struct stat *attributes)
 {
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
   return(stat(path,attributes));
 #else
    int
      status;
 
-   ssize_t
-     extent;
-
-   WCHAR
-     *wide_path;
+   wchar_t
+     *path_wide;
 
-   status=MultiByteToWideCharacter(path,&wide_path,&extent);
-   if (status != 0)
-     return(status);
-   status=_wstat64(wide_path,attributes);
-   wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
+   path_wide=create_wchar_path(path);
+   if (path_wide == (WCHAR *) NULL)
+     return(-1);
+   status=wstat(path_wide,attributes);
+   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
    return(status);
 #endif
 }