1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
* gnome-vfs-mime-sniff-buffer.c
* Utility for implementing gnome_vfs_mime_type_from_magic, and other
* mime-type sniffing calls.
*
* Copyright (C) 2000 Eazel, Inc.
* All rights reserved.
*
* This file is part of the Gnome Library.
*
* The Gnome Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* The Gnome Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include "gnome-vfs-mime-sniff-buffer.h"
#include "gnome-vfs-handle.h"
#include "gnome-vfs-mime-sniff-buffer-private.h"
#include "gnome-vfs-ops.h"
#include <string.h>
static GnomeVFSResult
handle_seek_glue (gpointer context, GnomeVFSSeekPosition whence,
GnomeVFSFileOffset offset)
{
GnomeVFSHandle *handle = (GnomeVFSHandle *)context;
return gnome_vfs_seek (handle, whence, offset);
}
static GnomeVFSResult
handle_read_glue (gpointer context, gpointer buffer,
GnomeVFSFileSize bytes, GnomeVFSFileSize *bytes_read)
{
GnomeVFSHandle *handle = (GnomeVFSHandle *)context;
return gnome_vfs_read (handle, buffer, bytes, bytes_read);
}
GnomeVFSMimeSniffBuffer *
_gnome_vfs_mime_sniff_buffer_new_from_handle (GnomeVFSHandle *file)
{
GnomeVFSMimeSniffBuffer *result;
result = g_new0 (GnomeVFSMimeSniffBuffer, 1);
result->owning = TRUE;
result->context = file;
result->seek = handle_seek_glue;
result->read = handle_read_glue;
return result;
}
GnomeVFSMimeSniffBuffer *
_gnome_vfs_mime_sniff_buffer_new_generic (GnomeVFSSniffBufferSeekCall seek_callback,
GnomeVFSSniffBufferReadCall read_callback,
gpointer context)
{
GnomeVFSMimeSniffBuffer * result;
result = g_new0 (GnomeVFSMimeSniffBuffer, 1);
result->owning = TRUE;
result->seek = seek_callback;
result->read = read_callback;
result->context = context;
return result;
}
GnomeVFSMimeSniffBuffer *
_gnome_vfs_mime_sniff_buffer_new_from_memory (const guchar *buffer,
gssize buffer_length)
{
GnomeVFSMimeSniffBuffer *result;
result = g_new0 (GnomeVFSMimeSniffBuffer, 1);
result->owning = TRUE;
result->buffer = g_malloc (buffer_length);
result->buffer_length = buffer_length;
memcpy (result->buffer, buffer, buffer_length);
result->read_whole_file = TRUE;
return result;
}
GnomeVFSMimeSniffBuffer *
gnome_vfs_mime_sniff_buffer_new_from_existing_data (const guchar *buffer,
gssize buffer_length)
{
GnomeVFSMimeSniffBuffer *result;
result = g_new0 (GnomeVFSMimeSniffBuffer, 1);
result->owning = FALSE;
result->buffer = (guchar *)buffer;
result->buffer_length = buffer_length;
result->read_whole_file = TRUE;
return result;
}
void
gnome_vfs_mime_sniff_buffer_free (GnomeVFSMimeSniffBuffer *buffer)
{
if (buffer->owning)
g_free (buffer->buffer);
g_free (buffer);
}
enum {
GNOME_VFS_SNIFF_BUFFER_INITIAL_CHUNK = 256,
GNOME_VFS_SNIFF_BUFFER_MIN_CHUNK = 128
};
GnomeVFSResult
_gnome_vfs_mime_sniff_buffer_get (GnomeVFSMimeSniffBuffer *buffer,
gssize size)
{
GnomeVFSResult result;
GnomeVFSFileSize bytes_to_read, bytes_read;
/* check to see if we already have enough data */
if (buffer->buffer_length >= size) {
return GNOME_VFS_OK;
}
/* if we've read the whole file, don't try to read any more */
if (buffer->read_whole_file) {
return GNOME_VFS_ERROR_EOF;
}
/* figure out how much to read */
bytes_to_read = size - buffer->buffer_length;
/* don't bother to read less than this */
if (bytes_to_read < GNOME_VFS_SNIFF_BUFFER_MIN_CHUNK) {
bytes_to_read = GNOME_VFS_SNIFF_BUFFER_MIN_CHUNK;
}
/* make room in buffer for new data */
buffer->buffer = g_realloc (buffer->buffer,
buffer->buffer_length + bytes_to_read);
/* read in more data */
result = (* buffer->read) (buffer->context,
buffer->buffer + buffer->buffer_length,
bytes_to_read,
&bytes_read);
if (result == GNOME_VFS_ERROR_EOF) {
buffer->read_whole_file = TRUE;
} else if (result != GNOME_VFS_OK) {
return result;
}
buffer->buffer_length += bytes_read;
/* check to see if we have enough data */
if (buffer->buffer_length >= size) {
return GNOME_VFS_OK;
}
/* not enough data */
return GNOME_VFS_ERROR_EOF;
}
/*
* gnome_vfs_get_mime_type_for_buffer:
* @buffer: a sniff buffer referencing either a file or data in memory
*
* This routine uses a magic database to guess the mime type of the
* data represented by @buffer.
*
* Returns a pointer to an internal copy of the mime-type for @buffer.
*/
const char *
gnome_vfs_get_mime_type_for_buffer (GnomeVFSMimeSniffBuffer *buffer)
{
return _gnome_vfs_get_mime_type_internal (buffer, NULL, FALSE);
}
|