]> granicus.if.org Git - imagemagick/commitdiff
(no commit message)
authorcristy <urban-warrior@git.imagemagick.org>
Wed, 10 Aug 2011 02:47:13 +0000 (02:47 +0000)
committercristy <urban-warrior@git.imagemagick.org>
Wed, 10 Aug 2011 02:47:13 +0000 (02:47 +0000)
MagickCore/utility.c
coders/emf.c

index 4aaf7a714ef541c7ceb24773353c4dc32405fd72..b369536e204b26f2486fadbf6437a8169f867eee 100644 (file)
@@ -1102,6 +1102,124 @@ MagickExport ssize_t GetMagickPageSize(void)
 %   o  attributes: the path attributes are returned here.
 %
 */
+
+#if defined(MAGICKCORE_HAVE__WFOPEN)
+static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
+{
+  register const unsigned char
+    *p;
+
+  if (utf16 != (wchar_t *) NULL)
+    {
+      register wchar_t
+        *q;
+
+      wchar_t
+        c;
+
+      /*
+        Convert UTF-8 to UTF-16.
+      */
+      q=utf16;
+      for (p=utf8; *p != '\0'; p++)
+      {
+        if ((*p & 0x80) == 0)
+          *q=(*p);
+        else
+          if ((*p & 0xE0) == 0xC0)
+            {
+              c=(*p);
+              *q=(c & 0x1F) << 6;
+              p++;
+              if ((*p & 0xC0) != 0x80)
+                return(0);
+              *q|=(*p & 0x3F);
+            }
+          else
+            if ((*p & 0xF0) == 0xE0)
+              {
+                c=(*p);
+                *q=c << 12;
+                p++;
+                if ((*p & 0xC0) != 0x80)
+                  return(0);
+                c=(*p);
+                *q|=(c & 0x3F) << 6;
+                p++;
+                if ((*p & 0xC0) != 0x80)
+                  return(0);
+                *q|=(*p & 0x3F);
+              }
+            else
+              return(0);
+        q++;
+      }
+      *q++='\0';
+      return(q-utf16);
+    }
+  /*
+    Compute UTF-16 string length.
+  */
+  for (p=utf8; *p != '\0'; p++)
+  {
+    if ((*p & 0x80) == 0)
+      ;
+    else
+      if ((*p & 0xE0) == 0xC0)
+        {
+          p++;
+          if ((*p & 0xC0) != 0x80)
+            return(0);
+        }
+      else
+        if ((*p & 0xF0) == 0xE0)
+          {
+            p++;
+            if ((*p & 0xC0) != 0x80)
+              return(0);
+            p++;
+            if ((*p & 0xC0) != 0x80)
+              return(0);
+         }
+       else
+         return(0);
+  }
+  return(p-utf8);
+}
+
+static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
+{
+  size_t
+    length;
+
+  wchar_t
+    *utf16;
+
+  length=UTF8ToUTF16(source,(wchar_t *) NULL);
+  if (length == 0)
+    {
+      register ssize_t
+        i;
+
+      /*
+        Not UTF-8, just copy.
+      */
+      length=strlen((const char *) source);
+      utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
+      if (utf16 == (wchar_t *) NULL)
+        return((wchar_t *) NULL);
+      for (i=0; i <= (ssize_t) length; i++)
+        utf16[i]=source[i];
+      return(utf16);
+    }
+  utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
+  if (utf16 == (wchar_t *) NULL)
+    return((wchar_t *) NULL);
+  length=UTF8ToUTF16(source,utf16);
+  return(utf16);
+}
+#endif
+
 MagickExport MagickBooleanType GetPathAttributes(const char *path,
   void *attributes)
 {
@@ -1117,17 +1235,12 @@ MagickExport MagickBooleanType GetPathAttributes(const char *path,
   status=stat(path,(struct stat *) attributes) == 0 ? MagickTrue : MagickFalse;
 #else
   {
-    int
-      count;
-
     wchar_t
       *unicode_path;
 
-    count=MultiByteToWideChar(CP_ACP,0,path,-1,(wchar_t *) NULL,0);
-    unicode_path=(wchar_t *) AcquireQuantumMemory(count,sizeof(*unicode_path));
+    unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
     if (unicode_path == (wchar_t *) NULL)
       return(MagickFalse);
-    count=MultiByteToWideChar(CP_ACP,0,path,-1,unicode_path,count);
     status=wstat(unicode_path,(struct stat *) attributes) == 0 ? MagickTrue :
       MagickFalse;
     unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
@@ -1811,26 +1924,19 @@ MagickExport FILE *OpenMagickStream(const char *path,const char *mode)
   file=(FILE *) NULL;
 #if defined(MAGICKCORE_HAVE__WFOPEN)
   {
-    int
-      count;
-
     wchar_t
       *unicode_mode,
       *unicode_path;
 
-    count=MultiByteToWideChar(CP_ACP,0,path,-1,(wchar_t *) NULL,0);
-    unicode_path=(wchar_t *) AcquireQuantumMemory(count,sizeof(*unicode_path));
+    unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
     if (unicode_path == (wchar_t *) NULL)
       return((FILE *) NULL);
-    count=MultiByteToWideChar(CP_ACP,0,path,-1,unicode_path,count);
-    count=MultiByteToWideChar(CP_ACP,0,mode,-1,(wchar_t *) NULL,0);
-    unicode_mode=(wchar_t *) AcquireQuantumMemory(count,sizeof(*unicode_mode));
+    unicode_mode=ConvertUTF8ToUTF16((const unsigned char *) mode);
     if (unicode_mode == (wchar_t *) NULL)
       {
         unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
         return((FILE *) NULL);
       }
-    count=MultiByteToWideChar(CP_ACP,0,mode,-1,unicode_mode,count);
     file=_wfopen(unicode_path,unicode_mode);
     unicode_mode=(wchar_t *) RelinquishMagickMemory(unicode_mode);
     unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
index e1f0262426ae33e269996902a72a6fbeb2aac43d..777890584bf649a4e5ae4341b428c6ff32af5211 100644 (file)
@@ -163,6 +163,124 @@ static MagickBooleanType IsWMF(const unsigned char *magick,const size_t length)
 %
 */
 
+
+#if defined(MAGICKCORE_HAVE__WFOPEN)
+static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
+{
+  register const unsigned char
+    *p;
+
+  if (utf16 != (wchar_t *) NULL)
+    {
+      register wchar_t
+        *q;
+
+      wchar_t
+        c;
+
+      /*
+        Convert UTF-8 to UTF-16.
+      */
+      q=utf16;
+      for (p=utf8; *p != '\0'; p++)
+      {
+        if ((*p & 0x80) == 0)
+          *q=(*p);
+        else
+          if ((*p & 0xE0) == 0xC0)
+            {
+              c=(*p);
+              *q=(c & 0x1F) << 6;
+              p++;
+              if ((*p & 0xC0) != 0x80)
+                return(0);
+              *q|=(*p & 0x3F);
+            }
+          else
+            if ((*p & 0xF0) == 0xE0)
+              {
+                c=(*p);
+                *q=c << 12;
+                p++;
+                if ((*p & 0xC0) != 0x80)
+                  return(0);
+                c=(*p);
+                *q|=(c & 0x3F) << 6;
+                p++;
+                if ((*p & 0xC0) != 0x80)
+                  return(0);
+                *q|=(*p & 0x3F);
+              }
+            else
+              return(0);
+        q++;
+      }
+      *q++='\0';
+      return(q-utf16);
+    }
+  /*
+    Compute UTF-16 string length.
+  */
+  for (p=utf8; *p != '\0'; p++)
+  {
+    if ((*p & 0x80) == 0)
+      ;
+    else
+      if ((*p & 0xE0) == 0xC0)
+        {
+          p++;
+          if ((*p & 0xC0) != 0x80)
+            return(0);
+        }
+      else
+        if ((*p & 0xF0) == 0xE0)
+          {
+            p++;
+            if ((*p & 0xC0) != 0x80)
+              return(0);
+            p++;
+            if ((*p & 0xC0) != 0x80)
+              return(0);
+         }
+       else
+         return(0);
+  }
+  return(p-utf8);
+}
+
+static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source)
+{
+  size_t
+    length;
+
+  wchar_t
+    *utf16;
+
+  length=UTF8ToUTF16(source,(wchar_t *) NULL);
+  if (length == 0)
+    {
+      register ssize_t
+        i;
+
+      /*
+        Not UTF-8, just copy.
+      */
+      length=strlen((char *) source);
+      utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
+      if (utf16 == (wchar_t *) NULL)
+        return((wchar_t *) NULL);
+      for (i=0; i <= (ssize_t) length; i++)
+        utf16[i]=source[i];
+      return(utf16);
+    }
+  utf16=(wchar_t *) AcquireQuantumMemory(length+1,sizeof(*utf16));
+  if (utf16 == (wchar_t *) NULL)
+    return((wchar_t *) NULL);
+  length=UTF8ToUTF16(source,utf16);
+  return(utf16);
+}
+#endif
+
 /*
   This method reads either an enhanced metafile, a regular 16bit Windows
   metafile, or an Aldus Placeable metafile and converts it into an enhanced
@@ -214,18 +332,12 @@ static HENHMETAFILE ReadEnhMetaFile(const char *path,ssize_t *width,
 #if defined(MAGICKCORE_HAVE__WFOPEN)
   if (hTemp == (HENHMETAFILE) NULL)
     {
-      int
-        count;
-
       wchar_t
         *unicode_path;
 
-      count=MultiByteToWideChar(CP_ACP,0,path,-1,(wchar_t) NULL,0);
-      unicode_path=(wchar_t *) AcquireQuantumMemory(count,
-        sizeof(*unicode_path));
+      unicode_path=ConvertUTF8ToUTF16((const unsigned char *) path);
       if (unicode_path != (wchar_t *) NULL)
         {
-          count=MultiByteToWideChar(CP_ACP,0,path,-1,unicode_path,count);
           hTemp=GetEnhMetaFileW(unicode_path);
           unicode_path=(wchar_t *) RelinquishMagickMemory(unicode_path);
         }