Magicless file formats

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Targa files have no magic header, and cannot be reliably identified that way.
The TGA plugin tried to register some nonsense magic string which didn't work
anyway, and any attempt to load a TGA file without explicitly selecting the
load format will usually cause gimp to handle it as a group 3 fax or
something. (A correct TGA magic pattern *could* be constructed, but it would
be so generic that it would match a lot of other files as well.)

A reasonable solution is to match the file suffix against all formats that
do not have a magic pattern first, before any attempt to determine the file
type by the header.

I'm a newcomer to gimp development so I didn't know where to send the patch,
but it is short so I included it here.


--- gimp-1.1.17/plug-ins/common/tga.c.orig	Wed Feb 23 01:19:04 2000
+++ gimp-1.1.17/plug-ins/common/tga.c	Wed Feb 23 03:44:05 2000
@@ -252,10 +252,10 @@
                           nsave_args, 0,
                           save_args, NULL);
 
-  gimp_register_magic_load_handler ("file_tga_load",
-				    "tga",
-				    "",
-				    "0&,byte,10,2&,byte,1,3&,byte,>0,3,byte,<9");
+  gimp_register_load_handler ("file_tga_load",
+			      "tga",
+			      "");
+		
   gimp_register_save_handler       ("file_tga_save",
 				    "tga",
 				    "");
--- gimp-1.1.17/app/fileops.c.orig	Wed Feb 23 01:21:27 2000
+++ gimp-1.1.17/app/fileops.c	Wed Feb 23 01:42:04 2000
@@ -1754,16 +1754,64 @@
   gtk_widget_set_sensitive (GTK_WIDGET (filesave), TRUE);
 }
 
+static PlugInProcDef *
+file_proc_find_by_name(GSList *procs,
+		       gchar *filename,
+		       gboolean skip_magic)
+{
+  GSList *p;
+  gchar *ext = strrchr(filename, '.');
+  if(ext)
+    ext++;
+
+  for(p = procs; p; p = p->next)
+    {
+      PlugInProcDef *proc = p->data;
+      GSList *prefixes;
+
+      if(skip_magic && proc->magics_list)
+	continue;
+      for(prefixes = proc->prefixes_list; prefixes; prefixes = prefixes->next)
+	{
+	  if(strncmp(filename, prefixes->data, strlen(prefixes->data)) == 0)
+	    return proc;
+	}
+     }
+
+  for(p = procs; p; p = p->next)
+    {
+      PlugInProcDef *proc = p->data;
+      GSList *extensions;
+
+      for(extensions = proc->extensions_list; ext && extensions;
+	  extensions = extensions->next)
+	{
+	  gchar *p1 = ext;
+	  gchar *p2 = (gchar *)extensions->data;
+
+          if(skip_magic && proc->magics_list)
+	    continue;
+	  while (*p1 && *p2)
+	    {
+	      if (tolower (*p1) != tolower (*p2))
+		break;
+	      p1++;
+	      p2++;
+	    }
+	  if (!(*p1) && !(*p2))
+	    return proc;
+	}
+    }
+
+  return NULL;
+}
+
 PlugInProcDef *
 file_proc_find (GSList *procs,
 		gchar  *filename)
 {
   PlugInProcDef *file_proc, *size_matched_proc;
   GSList *all_procs = procs;
-  GSList *extensions;
-  GSList *prefixes;
-  gchar *extension;
-  gchar *p1, *p2;
   FILE *ifp = NULL;
   gint head_size = -2, size_match_count = 0;
   gint match_val;
@@ -1771,11 +1819,11 @@
 
   size_matched_proc = NULL;
 
-  extension = strrchr (filename, '.');
-  if (extension)
-    extension += 1;
+  /* First, check magicless prefixes/suffixes */
+  if( (file_proc = file_proc_find_by_name(all_procs, filename, TRUE)) != NULL)
+    return file_proc;
 
-  /* At first look for magics */
+  /* Then look for magics */
   while (procs)
     {
       file_proc = procs->data;
@@ -1810,46 +1858,8 @@
   if (size_match_count == 1)
     return (size_matched_proc);
 
-  procs = all_procs;
-  while (procs)
-    {
-      file_proc = procs->data;
-      procs = procs->next;
-
-      for (prefixes = file_proc->prefixes_list; prefixes; prefixes = prefixes->next)
-	{
-	  p1 = filename;
-	  p2 = (gchar *) prefixes->data;
-
-	  if (strncmp (filename, prefixes->data, strlen (prefixes->data)) == 0)
-	    return file_proc;
-	}
-     }
-
-  procs = all_procs;
-  while (procs)
-    {
-      file_proc = procs->data;
-      procs = procs->next;
-
-      for (extensions = file_proc->extensions_list; extension && extensions; extensions = extensions->next)
-	{
-	  p1 = extension;
-	  p2 = (gchar *) extensions->data;
-
-	  while (*p1 && *p2)
-	    {
-	      if (tolower (*p1) != tolower (*p2))
-		break;
-	      p1 += 1;
-	      p2 += 1;
-	    }
-	  if (!(*p1) && !(*p2))
-	    return file_proc;
-	}
-    }
-
-  return NULL;
+  /* As a last ditch, try matching by name */
+  return file_proc_find_by_name(all_procs, filename, FALSE);
 }
 
 static void


[Index of Archives]     [Video For Linux]     [Photo]     [Yosemite News]     [gtk]     [GIMP for Windows]     [KDE]     [GEGL]     [Gimp's Home]     [Gimp on GUI]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux