2007-05-12 02:50:35 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
|
|
|
* Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
|
|
|
|
* Copyright (C) 1997 Josh MacDonald
|
|
|
|
*
|
2016-01-04 03:05:27 +08:00
|
|
|
* gimppluginmanager-file-procedure.c
|
2007-05-12 02:50:35 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2007-05-12 02:50:35 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2007-05-12 02:50:35 +08:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-07-12 05:27:07 +08:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2007-05-12 02:50:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2016-01-02 02:37:10 +08:00
|
|
|
#include <gegl.h>
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
|
2016-01-04 03:05:27 +08:00
|
|
|
#include "plug-in-types.h"
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2016-01-04 03:05:27 +08:00
|
|
|
#include "core/gimp-utils.h"
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2016-01-04 03:05:27 +08:00
|
|
|
#include "gimppluginmanager-file-procedure.h"
|
|
|
|
#include "gimppluginprocedure.h"
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2017-07-29 04:35:13 +08:00
|
|
|
#include "gimp-log.h"
|
|
|
|
|
2007-05-12 02:50:35 +08:00
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2018-03-25 04:49:01 +08:00
|
|
|
/* positive values indicate the length of a matching magic */
|
2016-04-25 05:56:57 +08:00
|
|
|
|
|
|
|
FILE_MATCH_NONE = 0,
|
|
|
|
FILE_MATCH_SIZE = -1
|
2007-05-12 02:50:35 +08:00
|
|
|
} FileMatchType;
|
|
|
|
|
|
|
|
|
|
|
|
/* local function prototypes */
|
|
|
|
|
|
|
|
static GimpPlugInProcedure * file_proc_find_by_prefix (GSList *procs,
|
2014-07-07 06:46:25 +08:00
|
|
|
GFile *file,
|
2007-05-12 02:50:35 +08:00
|
|
|
gboolean skip_magic);
|
|
|
|
static GimpPlugInProcedure * file_proc_find_by_extension (GSList *procs,
|
2014-07-07 06:46:25 +08:00
|
|
|
GFile *file,
|
2016-05-02 00:32:04 +08:00
|
|
|
gboolean skip_magic);
|
2007-05-12 02:50:35 +08:00
|
|
|
static GimpPlugInProcedure * file_proc_find_by_name (GSList *procs,
|
2014-07-07 06:46:25 +08:00
|
|
|
GFile *file,
|
2007-05-12 02:50:35 +08:00
|
|
|
gboolean skip_magic);
|
|
|
|
|
|
|
|
static void file_convert_string (const gchar *instr,
|
|
|
|
gchar *outmem,
|
|
|
|
gint maxmem,
|
|
|
|
gint *nmem);
|
|
|
|
static FileMatchType file_check_single_magic (const gchar *offset,
|
|
|
|
const gchar *type,
|
|
|
|
const gchar *value,
|
|
|
|
const guchar *file_head,
|
|
|
|
gint headsize,
|
2014-07-05 06:57:02 +08:00
|
|
|
GFile *file,
|
|
|
|
GInputStream *input);
|
2007-05-12 02:50:35 +08:00
|
|
|
static FileMatchType file_check_magic_list (GSList *magics_list,
|
|
|
|
const guchar *head,
|
|
|
|
gint headsize,
|
2014-07-05 06:57:02 +08:00
|
|
|
GFile *file,
|
|
|
|
GInputStream *input);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
GimpPlugInProcedure *
|
2014-07-07 06:46:25 +08:00
|
|
|
file_procedure_find (GSList *procs,
|
|
|
|
GFile *file,
|
|
|
|
GError **error)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
|
|
|
GimpPlugInProcedure *file_proc;
|
2014-07-07 06:46:25 +08:00
|
|
|
GimpPlugInProcedure *size_matched_proc = NULL;
|
|
|
|
gint size_match_count = 0;
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (procs != NULL, NULL);
|
2014-07-07 06:46:25 +08:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2007-05-12 02:50:35 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2016-05-02 00:32:04 +08:00
|
|
|
/* First, check magicless prefixes/suffixes */
|
|
|
|
file_proc = file_proc_find_by_name (procs, file, TRUE);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
if (file_proc)
|
|
|
|
return file_proc;
|
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
/* Then look for magics, but not on remote files */
|
|
|
|
if (g_file_is_native (file))
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
GSList *list;
|
|
|
|
GInputStream *input = NULL;
|
|
|
|
gboolean opened = FALSE;
|
|
|
|
gsize head_size = 0;
|
|
|
|
guchar head[256];
|
|
|
|
FileMatchType best_match_val = FILE_MATCH_NONE;
|
|
|
|
GimpPlugInProcedure *best_file_proc = NULL;
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
for (list = procs; list; list = g_slist_next (list))
|
2014-07-07 06:46:25 +08:00
|
|
|
{
|
2014-07-23 16:49:44 +08:00
|
|
|
file_proc = list->data;
|
2014-07-07 06:46:25 +08:00
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
if (file_proc->magics_list)
|
|
|
|
{
|
|
|
|
if (G_UNLIKELY (! opened))
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2014-07-23 16:49:44 +08:00
|
|
|
input = G_INPUT_STREAM (g_file_read (file, NULL, error));
|
2014-07-05 06:57:02 +08:00
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
if (input)
|
2014-07-05 06:57:02 +08:00
|
|
|
{
|
2014-07-23 16:49:44 +08:00
|
|
|
g_input_stream_read_all (input,
|
|
|
|
head, sizeof (head),
|
|
|
|
&head_size, NULL, error);
|
|
|
|
|
|
|
|
if (head_size < 4)
|
|
|
|
{
|
|
|
|
g_object_unref (input);
|
|
|
|
input = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GDataInputStream *data_input;
|
|
|
|
|
|
|
|
data_input = g_data_input_stream_new (input);
|
|
|
|
g_object_unref (input);
|
|
|
|
input = G_INPUT_STREAM (data_input);
|
|
|
|
}
|
2014-07-05 06:57:02 +08:00
|
|
|
}
|
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
opened = TRUE;
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
if (head_size >= 4)
|
|
|
|
{
|
|
|
|
FileMatchType match_val;
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
match_val = file_check_magic_list (file_proc->magics_list,
|
|
|
|
head, head_size,
|
|
|
|
file, input);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
if (match_val == FILE_MATCH_SIZE)
|
|
|
|
{
|
|
|
|
/* Use it only if no other magic matches */
|
|
|
|
size_match_count++;
|
|
|
|
size_matched_proc = file_proc;
|
|
|
|
}
|
|
|
|
else if (match_val != FILE_MATCH_NONE)
|
|
|
|
{
|
2017-07-29 04:35:13 +08:00
|
|
|
GIMP_LOG (MAGIC_MATCH,
|
|
|
|
"magic match %d on %s\n",
|
|
|
|
match_val,
|
|
|
|
gimp_object_get_name (file_proc));
|
2014-07-07 06:46:25 +08:00
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
if (match_val > best_match_val)
|
|
|
|
{
|
|
|
|
best_match_val = match_val;
|
|
|
|
best_file_proc = file_proc;
|
|
|
|
}
|
2014-07-23 16:49:44 +08:00
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 16:49:44 +08:00
|
|
|
if (input)
|
2016-04-25 05:56:57 +08:00
|
|
|
g_object_unref (input);
|
|
|
|
|
|
|
|
if (best_file_proc)
|
2014-07-23 16:49:44 +08:00
|
|
|
{
|
2017-07-29 04:35:13 +08:00
|
|
|
GIMP_LOG (MAGIC_MATCH,
|
|
|
|
"best magic match on %s\n",
|
|
|
|
gimp_object_get_name (best_file_proc));
|
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
return best_file_proc;
|
2014-07-23 16:49:44 +08:00
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 06:46:25 +08:00
|
|
|
if (size_match_count == 1)
|
|
|
|
return size_matched_proc;
|
|
|
|
|
2012-11-18 04:21:32 +08:00
|
|
|
/* As a last resort, try matching by name, not skipping magic procs */
|
2014-07-07 06:46:25 +08:00
|
|
|
file_proc = file_proc_find_by_name (procs, file, FALSE);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
if (file_proc)
|
|
|
|
{
|
|
|
|
/* we found a procedure, clear error that might have been set */
|
|
|
|
g_clear_error (error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* set an error message unless one was already set */
|
|
|
|
if (error && *error == NULL)
|
2016-12-21 11:05:32 +08:00
|
|
|
g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Unknown file type"));
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return file_proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
GimpPlugInProcedure *
|
2014-07-07 06:46:25 +08:00
|
|
|
file_procedure_find_by_prefix (GSList *procs,
|
|
|
|
GFile *file)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2014-07-07 06:46:25 +08:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-07 06:46:25 +08:00
|
|
|
return file_proc_find_by_prefix (procs, file, FALSE);
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GimpPlugInProcedure *
|
2014-07-07 06:46:25 +08:00
|
|
|
file_procedure_find_by_extension (GSList *procs,
|
|
|
|
GFile *file)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2014-07-07 06:46:25 +08:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2016-05-02 00:32:04 +08:00
|
|
|
return file_proc_find_by_extension (procs, file, FALSE);
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2014-07-05 02:01:08 +08:00
|
|
|
GimpPlugInProcedure *
|
|
|
|
file_procedure_find_by_mime_type (GSList *procs,
|
|
|
|
const gchar *mime_type)
|
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
|
|
|
|
g_return_val_if_fail (mime_type != NULL, NULL);
|
|
|
|
|
|
|
|
for (list = procs; list; list = g_slist_next (list))
|
|
|
|
{
|
|
|
|
GimpPlugInProcedure *proc = list->data;
|
2017-05-05 05:22:37 +08:00
|
|
|
GSList *mime;
|
2014-07-05 02:01:08 +08:00
|
|
|
|
2017-05-05 05:22:37 +08:00
|
|
|
for (mime = proc->mime_types_list; mime; mime = g_slist_next (mime))
|
|
|
|
{
|
|
|
|
if (! strcmp (mime_type, mime->data))
|
|
|
|
return proc;
|
|
|
|
}
|
2014-07-05 02:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
|
|
|
static GimpPlugInProcedure *
|
2014-07-07 06:46:25 +08:00
|
|
|
file_proc_find_by_prefix (GSList *procs,
|
|
|
|
GFile *file,
|
|
|
|
gboolean skip_magic)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2014-07-07 06:46:25 +08:00
|
|
|
gchar *uri = g_file_get_uri (file);
|
2007-05-12 02:50:35 +08:00
|
|
|
GSList *p;
|
|
|
|
|
|
|
|
for (p = procs; p; p = g_slist_next (p))
|
|
|
|
{
|
|
|
|
GimpPlugInProcedure *proc = p->data;
|
|
|
|
GSList *prefixes;
|
|
|
|
|
|
|
|
if (skip_magic && proc->magics_list)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (prefixes = proc->prefixes_list;
|
|
|
|
prefixes;
|
|
|
|
prefixes = g_slist_next (prefixes))
|
|
|
|
{
|
|
|
|
if (g_str_has_prefix (uri, prefixes->data))
|
2014-07-07 06:46:25 +08:00
|
|
|
{
|
|
|
|
g_free (uri);
|
|
|
|
return proc;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-07 06:46:25 +08:00
|
|
|
g_free (uri);
|
|
|
|
|
2007-05-12 02:50:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GimpPlugInProcedure *
|
2014-07-07 06:46:25 +08:00
|
|
|
file_proc_find_by_extension (GSList *procs,
|
|
|
|
GFile *file,
|
2016-05-02 00:32:04 +08:00
|
|
|
gboolean skip_magic)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-01-04 03:05:27 +08:00
|
|
|
gchar *ext = gimp_file_get_extension (file);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-08 08:58:59 +08:00
|
|
|
if (ext)
|
2014-07-07 06:46:25 +08:00
|
|
|
{
|
2014-07-08 08:58:59 +08:00
|
|
|
GSList *p;
|
2012-11-18 04:21:32 +08:00
|
|
|
|
2014-07-08 08:58:59 +08:00
|
|
|
for (p = procs; p; p = g_slist_next (p))
|
|
|
|
{
|
|
|
|
GimpPlugInProcedure *proc = p->data;
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-08 08:58:59 +08:00
|
|
|
if (skip_magic && proc->magics_list)
|
|
|
|
continue;
|
2012-11-18 04:21:32 +08:00
|
|
|
|
2014-07-08 08:58:59 +08:00
|
|
|
if (g_slist_find_custom (proc->extensions_list,
|
|
|
|
ext + 1,
|
|
|
|
(GCompareFunc) g_ascii_strcasecmp))
|
|
|
|
{
|
|
|
|
g_free (ext);
|
2013-05-03 02:05:35 +08:00
|
|
|
|
2014-07-08 08:58:59 +08:00
|
|
|
return proc;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 08:58:59 +08:00
|
|
|
g_free (ext);
|
|
|
|
}
|
2014-07-07 06:46:25 +08:00
|
|
|
|
2007-05-12 02:50:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GimpPlugInProcedure *
|
2014-07-07 06:46:25 +08:00
|
|
|
file_proc_find_by_name (GSList *procs,
|
|
|
|
GFile *file,
|
|
|
|
gboolean skip_magic)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
|
|
|
GimpPlugInProcedure *proc;
|
|
|
|
|
2016-05-02 00:32:04 +08:00
|
|
|
proc = file_proc_find_by_prefix (procs, file, skip_magic);
|
2012-11-18 04:21:32 +08:00
|
|
|
|
|
|
|
if (! proc)
|
2016-05-02 00:32:04 +08:00
|
|
|
proc = file_proc_find_by_extension (procs, file, skip_magic);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
file_convert_string (const gchar *instr,
|
|
|
|
gchar *outmem,
|
|
|
|
gint maxmem,
|
|
|
|
gint *nmem)
|
|
|
|
{
|
|
|
|
/* Convert a string in C-notation to array of char */
|
|
|
|
const guchar *uin = (const guchar *) instr;
|
|
|
|
guchar *uout = (guchar *) outmem;
|
|
|
|
guchar tmp[5], *tmpptr;
|
|
|
|
guint k;
|
|
|
|
|
|
|
|
while ((*uin != '\0') && ((((gchar *) uout) - outmem) < maxmem))
|
|
|
|
{
|
|
|
|
if (*uin != '\\') /* Not an escaped character ? */
|
|
|
|
{
|
|
|
|
*(uout++) = *(uin++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*(++uin) == '\0')
|
|
|
|
{
|
|
|
|
*(uout++) = '\\';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*uin)
|
|
|
|
{
|
|
|
|
case '0': case '1': case '2': case '3': /* octal */
|
|
|
|
for (tmpptr = tmp; (tmpptr - tmp) <= 3;)
|
|
|
|
{
|
|
|
|
*(tmpptr++) = *(uin++);
|
|
|
|
if ( (*uin == '\0') || (!g_ascii_isdigit (*uin))
|
|
|
|
|| (*uin == '8') || (*uin == '9'))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*tmpptr = '\0';
|
|
|
|
sscanf ((gchar *) tmp, "%o", &k);
|
|
|
|
*(uout++) = k;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a': *(uout++) = '\a'; uin++; break;
|
|
|
|
case 'b': *(uout++) = '\b'; uin++; break;
|
|
|
|
case 't': *(uout++) = '\t'; uin++; break;
|
|
|
|
case 'n': *(uout++) = '\n'; uin++; break;
|
|
|
|
case 'v': *(uout++) = '\v'; uin++; break;
|
|
|
|
case 'f': *(uout++) = '\f'; uin++; break;
|
|
|
|
case 'r': *(uout++) = '\r'; uin++; break;
|
|
|
|
|
|
|
|
default : *(uout++) = *(uin++); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*nmem = ((gchar *) uout) - outmem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FileMatchType
|
|
|
|
file_check_single_magic (const gchar *offset,
|
|
|
|
const gchar *type,
|
|
|
|
const gchar *value,
|
|
|
|
const guchar *file_head,
|
|
|
|
gint headsize,
|
2014-07-05 06:57:02 +08:00
|
|
|
GFile *file,
|
|
|
|
GInputStream *input)
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
FileMatchType found = FILE_MATCH_NONE;
|
|
|
|
glong offs;
|
|
|
|
gulong num_testval;
|
2016-04-25 05:56:57 +08:00
|
|
|
gulong num_operator_val;
|
2007-05-12 02:50:35 +08:00
|
|
|
gint numbytes, k;
|
|
|
|
const gchar *num_operator_ptr;
|
|
|
|
gchar num_operator;
|
|
|
|
|
|
|
|
/* Check offset */
|
|
|
|
if (sscanf (offset, "%ld", &offs) != 1)
|
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
|
|
|
|
/* Check type of test */
|
|
|
|
num_operator_ptr = NULL;
|
|
|
|
num_operator = '\0';
|
|
|
|
|
|
|
|
if (g_str_has_prefix (type, "byte"))
|
|
|
|
{
|
|
|
|
numbytes = 1;
|
|
|
|
num_operator_ptr = type + strlen ("byte");
|
|
|
|
}
|
|
|
|
else if (g_str_has_prefix (type, "short"))
|
|
|
|
{
|
|
|
|
numbytes = 2;
|
|
|
|
num_operator_ptr = type + strlen ("short");
|
|
|
|
}
|
|
|
|
else if (g_str_has_prefix (type, "long"))
|
|
|
|
{
|
|
|
|
numbytes = 4;
|
|
|
|
num_operator_ptr = type + strlen ("long");
|
|
|
|
}
|
|
|
|
else if (g_str_has_prefix (type, "size"))
|
|
|
|
{
|
|
|
|
numbytes = 5;
|
|
|
|
}
|
|
|
|
else if (strcmp (type, "string") == 0)
|
|
|
|
{
|
|
|
|
numbytes = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check numerical operator value if present */
|
|
|
|
if (num_operator_ptr && (*num_operator_ptr == '&'))
|
|
|
|
{
|
|
|
|
if (g_ascii_isdigit (num_operator_ptr[1]))
|
|
|
|
{
|
|
|
|
if (num_operator_ptr[1] != '0') /* decimal */
|
2016-04-25 05:56:57 +08:00
|
|
|
sscanf (num_operator_ptr+1, "%lu", &num_operator_val);
|
2007-05-12 02:50:35 +08:00
|
|
|
else if (num_operator_ptr[2] == 'x') /* hexadecimal */
|
2016-04-25 05:56:57 +08:00
|
|
|
sscanf (num_operator_ptr+3, "%lx", &num_operator_val);
|
2007-05-12 02:50:35 +08:00
|
|
|
else /* octal */
|
2016-04-25 05:56:57 +08:00
|
|
|
sscanf (num_operator_ptr+2, "%lo", &num_operator_val);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
num_operator = *num_operator_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
if (numbytes > 0)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
/* Numerical test */
|
|
|
|
|
2007-05-12 02:50:35 +08:00
|
|
|
gchar num_test = '=';
|
|
|
|
gulong fileval = 0;
|
|
|
|
|
|
|
|
/* Check test value */
|
|
|
|
if ((value[0] == '>') || (value[0] == '<'))
|
|
|
|
{
|
|
|
|
num_test = value[0];
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
num_testval = strtol (value, NULL, 0);
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
if (numbytes == 5)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
/* Check for file size */
|
|
|
|
|
2014-07-05 06:57:02 +08:00
|
|
|
GFileInfo *info = g_file_query_info (file,
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_SIZE,
|
|
|
|
G_FILE_QUERY_INFO_NONE,
|
|
|
|
NULL, NULL);
|
|
|
|
if (! info)
|
2007-05-12 02:50:35 +08:00
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
|
2014-07-05 06:57:02 +08:00
|
|
|
fileval = g_file_info_get_size (info);
|
|
|
|
g_object_unref (info);
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
else if (offs >= 0 &&
|
2016-04-25 05:56:57 +08:00
|
|
|
(offs + numbytes <= headsize))
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
/* We have it in memory */
|
|
|
|
|
2007-05-12 02:50:35 +08:00
|
|
|
for (k = 0; k < numbytes; k++)
|
|
|
|
fileval = (fileval << 8) | (glong) file_head[offs + k];
|
|
|
|
}
|
2016-04-25 05:56:57 +08:00
|
|
|
else
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
/* Read it from file */
|
|
|
|
|
2014-07-05 06:57:02 +08:00
|
|
|
if (! g_seekable_seek (G_SEEKABLE (input), offs,
|
|
|
|
(offs >= 0) ? G_SEEK_SET : G_SEEK_END,
|
|
|
|
NULL, NULL))
|
2007-05-12 02:50:35 +08:00
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
|
|
|
|
for (k = 0; k < numbytes; k++)
|
2014-07-05 06:57:02 +08:00
|
|
|
{
|
|
|
|
guchar byte;
|
|
|
|
GError *error = NULL;
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-05 06:57:02 +08:00
|
|
|
byte = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (input),
|
|
|
|
NULL, &error);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_clear_error (&error);
|
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileval = (fileval << 8) | byte;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num_operator == '&')
|
2016-04-25 05:56:57 +08:00
|
|
|
fileval &= num_operator_val;
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
if (num_test == '<')
|
2016-04-25 05:56:57 +08:00
|
|
|
{
|
|
|
|
if (fileval < num_testval)
|
|
|
|
found = numbytes;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
else if (num_test == '>')
|
2016-04-25 05:56:57 +08:00
|
|
|
{
|
|
|
|
if (fileval > num_testval)
|
|
|
|
found = numbytes;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
else
|
2016-04-25 05:56:57 +08:00
|
|
|
{
|
|
|
|
if (fileval == num_testval)
|
|
|
|
found = numbytes;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
if (found && (numbytes == 5))
|
|
|
|
found = FILE_MATCH_SIZE;
|
|
|
|
}
|
2016-04-25 05:56:57 +08:00
|
|
|
else if (numbytes == 0)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
/* String test */
|
|
|
|
|
2007-05-12 02:50:35 +08:00
|
|
|
gchar mem_testval[256];
|
|
|
|
|
|
|
|
file_convert_string (value,
|
|
|
|
mem_testval, sizeof (mem_testval),
|
|
|
|
&numbytes);
|
|
|
|
|
|
|
|
if (numbytes <= 0)
|
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
|
|
|
|
if (offs >= 0 &&
|
2016-04-25 05:56:57 +08:00
|
|
|
(offs + numbytes <= headsize))
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
/* We have it in memory */
|
|
|
|
|
|
|
|
if (memcmp (mem_testval, file_head + offs, numbytes) == 0)
|
|
|
|
found = numbytes;
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
2016-04-25 05:56:57 +08:00
|
|
|
else
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
/* Read it from file */
|
|
|
|
|
2014-07-05 06:57:02 +08:00
|
|
|
if (! g_seekable_seek (G_SEEKABLE (input), offs,
|
|
|
|
(offs >= 0) ? G_SEEK_SET : G_SEEK_END,
|
|
|
|
NULL, NULL))
|
2007-05-12 02:50:35 +08:00
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
for (k = 0; k < numbytes; k++)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2014-07-05 06:57:02 +08:00
|
|
|
guchar byte;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
byte = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (input),
|
|
|
|
NULL, &error);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_clear_error (&error);
|
2016-04-25 05:56:57 +08:00
|
|
|
|
2014-07-05 06:57:02 +08:00
|
|
|
return FILE_MATCH_NONE;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2014-07-05 06:57:02 +08:00
|
|
|
if (byte != mem_testval[k])
|
2016-04-25 05:56:57 +08:00
|
|
|
return FILE_MATCH_NONE;
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
2016-04-25 05:56:57 +08:00
|
|
|
|
|
|
|
found = numbytes;
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FileMatchType
|
|
|
|
file_check_magic_list (GSList *magics_list,
|
|
|
|
const guchar *head,
|
|
|
|
gint headsize,
|
2014-07-05 06:57:02 +08:00
|
|
|
GFile *file,
|
|
|
|
GInputStream *input)
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
gboolean and = FALSE;
|
|
|
|
gboolean found = FALSE;
|
|
|
|
FileMatchType best_match_val = FILE_MATCH_NONE;
|
|
|
|
FileMatchType match_val = FILE_MATCH_NONE;
|
|
|
|
|
|
|
|
for (; magics_list; magics_list = magics_list->next)
|
2007-05-12 02:50:35 +08:00
|
|
|
{
|
2016-04-25 05:56:57 +08:00
|
|
|
const gchar *offset;
|
|
|
|
const gchar *type;
|
|
|
|
const gchar *value;
|
|
|
|
FileMatchType single_match_val = FILE_MATCH_NONE;
|
2007-05-12 02:50:35 +08:00
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
if ((offset = magics_list->data) == NULL) return FILE_MATCH_NONE;
|
|
|
|
if ((magics_list = magics_list->next) == NULL) return FILE_MATCH_NONE;
|
|
|
|
if ((type = magics_list->data) == NULL) return FILE_MATCH_NONE;
|
|
|
|
if ((magics_list = magics_list->next) == NULL) return FILE_MATCH_NONE;
|
|
|
|
if ((value = magics_list->data) == NULL) return FILE_MATCH_NONE;
|
|
|
|
|
|
|
|
single_match_val = file_check_single_magic (offset, type, value,
|
|
|
|
head, headsize,
|
|
|
|
file, input);
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
if (and)
|
2016-04-25 05:56:57 +08:00
|
|
|
found = found && (single_match_val != FILE_MATCH_NONE);
|
2007-05-12 02:50:35 +08:00
|
|
|
else
|
2016-04-25 05:56:57 +08:00
|
|
|
found = (single_match_val != FILE_MATCH_NONE);
|
|
|
|
|
2016-04-25 07:04:07 +08:00
|
|
|
if (found)
|
2016-04-25 05:56:57 +08:00
|
|
|
{
|
2016-04-25 07:04:07 +08:00
|
|
|
if (match_val == FILE_MATCH_NONE)
|
2016-04-25 05:56:57 +08:00
|
|
|
{
|
2016-04-25 07:04:07 +08:00
|
|
|
/* if we have no match yet, this is it in any case */
|
|
|
|
|
|
|
|
match_val = single_match_val;
|
2016-04-25 05:56:57 +08:00
|
|
|
}
|
2016-04-25 07:04:07 +08:00
|
|
|
else if (single_match_val != FILE_MATCH_NONE)
|
2016-04-25 05:56:57 +08:00
|
|
|
{
|
2016-04-25 07:04:07 +08:00
|
|
|
/* else if we have a match on this one, combine it with the
|
|
|
|
* existing return value
|
2016-04-25 05:56:57 +08:00
|
|
|
*/
|
|
|
|
|
2016-04-25 07:04:07 +08:00
|
|
|
if (single_match_val == FILE_MATCH_SIZE)
|
|
|
|
{
|
|
|
|
/* if we already have a magic match, simply increase
|
|
|
|
* that by one to indicate "better match", not perfect
|
|
|
|
* but better than losing the additional size match
|
|
|
|
* entirely
|
|
|
|
*/
|
|
|
|
if (match_val != FILE_MATCH_SIZE)
|
|
|
|
match_val += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* if we already have a magic match, simply add to its
|
|
|
|
* length; otherwise if we already have a size match,
|
|
|
|
* combine it with this match, see comment above
|
|
|
|
*/
|
|
|
|
if (match_val != FILE_MATCH_SIZE)
|
|
|
|
match_val += single_match_val;
|
|
|
|
else
|
|
|
|
match_val = single_match_val + 1;
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 05:56:57 +08:00
|
|
|
}
|
2016-04-25 07:04:07 +08:00
|
|
|
else
|
2016-04-25 05:56:57 +08:00
|
|
|
{
|
2016-04-25 07:04:07 +08:00
|
|
|
match_val = FILE_MATCH_NONE;
|
2016-04-25 05:56:57 +08:00
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
|
|
|
|
and = (strchr (offset, '&') != NULL);
|
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
if (! and)
|
2016-04-25 07:04:07 +08:00
|
|
|
{
|
|
|
|
/* when done with this 'and' list, update best_match_val */
|
|
|
|
|
|
|
|
if (best_match_val == FILE_MATCH_NONE)
|
|
|
|
{
|
|
|
|
/* if we have no best match yet, this is it */
|
|
|
|
|
|
|
|
best_match_val = match_val;
|
|
|
|
}
|
|
|
|
else if (match_val != FILE_MATCH_NONE)
|
|
|
|
{
|
|
|
|
/* otherwise if this was a match, update the best match, note
|
|
|
|
* that by using MAX we will not overwrite a magic match
|
|
|
|
* with a size match
|
|
|
|
*/
|
|
|
|
|
|
|
|
best_match_val = MAX (best_match_val, match_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
match_val = FILE_MATCH_NONE;
|
|
|
|
}
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2016-04-25 05:56:57 +08:00
|
|
|
return best_match_val;
|
2007-05-12 02:50:35 +08:00
|
|
|
}
|