2003-06-30 16:44:03 +04:00
|
|
|
/*
|
2004-03-11 05:19:14 +03:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-03-15 18:53:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2003-12-27 03:11:57 +03:00
|
|
|
#include <string.h>
|
2003-03-15 18:53:20 +03:00
|
|
|
#include <unixlib/local.h>
|
2004-03-11 05:19:14 +03:00
|
|
|
#include "oslib/mimemap.h"
|
2003-03-15 18:53:20 +03:00
|
|
|
#include "oslib/osfile.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "content/content.h"
|
|
|
|
#include "content/fetch.h"
|
2010-04-07 13:28:01 +04:00
|
|
|
#include "content/hlcache.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "riscos/gui.h"
|
|
|
|
#include "utils/config.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utils.h"
|
2003-03-15 18:53:20 +03:00
|
|
|
|
2005-07-27 19:31:48 +04:00
|
|
|
/* type_map must be in sorted order by file_type */
|
|
|
|
struct type_entry {
|
|
|
|
bits file_type;
|
|
|
|
char mime_type[40];
|
|
|
|
};
|
|
|
|
static const struct type_entry type_map[] = {
|
2006-07-06 04:07:11 +04:00
|
|
|
{0x132, "image/ico"},
|
|
|
|
{0x188, "application/x-shockwave-flash"},
|
2005-07-27 19:31:48 +04:00
|
|
|
{0x695, "image/gif"},
|
2006-02-26 05:25:19 +03:00
|
|
|
{0x69c, "image/x-ms-bmp"},
|
2007-07-13 12:25:34 +04:00
|
|
|
{0xaad, "image/svg+xml"},
|
2005-07-27 19:31:48 +04:00
|
|
|
{0xaff, "image/x-drawfile"},
|
|
|
|
{0xb60, "image/png"},
|
|
|
|
{0xc85, "image/jpeg"},
|
2007-07-21 04:20:04 +04:00
|
|
|
{0xd94, "image/x-artworks"},
|
2005-07-27 19:31:48 +04:00
|
|
|
{0xf78, "image/jng"},
|
|
|
|
{0xf79, "text/css"},
|
|
|
|
{0xf83, "image/mng"},
|
|
|
|
{0xfaf, "text/html"},
|
|
|
|
{0xff9, "image/x-riscos-sprite"},
|
|
|
|
{0xfff, "text/plain"},
|
|
|
|
};
|
|
|
|
#define TYPE_MAP_COUNT (sizeof(type_map) / sizeof(type_map[0]))
|
|
|
|
|
2005-07-27 05:30:58 +04:00
|
|
|
#define BUF_SIZE (256)
|
|
|
|
static char type_buf[BUF_SIZE];
|
2003-03-15 18:53:20 +03:00
|
|
|
|
2005-07-27 19:31:48 +04:00
|
|
|
|
|
|
|
static int cmp_type(const void *x, const void *y);
|
|
|
|
|
2003-03-15 18:53:20 +03:00
|
|
|
/**
|
2004-03-11 05:19:14 +03:00
|
|
|
* Determine the MIME type of a local file.
|
2005-07-27 05:30:58 +04:00
|
|
|
*
|
|
|
|
* \param unix_path Unix style path to file on disk
|
|
|
|
* \return Pointer to MIME type string (should not be freed) - invalidated
|
2006-07-06 04:07:11 +04:00
|
|
|
* on next call to fetch_filetype.
|
2003-03-15 18:53:20 +03:00
|
|
|
*/
|
|
|
|
const char *fetch_filetype(const char *unix_path)
|
|
|
|
{
|
2005-07-27 19:31:48 +04:00
|
|
|
struct type_entry *t;
|
2003-03-15 18:53:20 +03:00
|
|
|
unsigned int len = strlen(unix_path) + 100;
|
2004-12-09 22:09:56 +03:00
|
|
|
char *path = calloc(len, 1);
|
2005-07-27 05:30:58 +04:00
|
|
|
char *r, *slash;
|
2003-03-15 18:53:20 +03:00
|
|
|
os_error *error;
|
2005-07-27 05:30:58 +04:00
|
|
|
bits file_type, temp;
|
2006-07-06 04:07:11 +04:00
|
|
|
int objtype;
|
2003-03-15 18:53:20 +03:00
|
|
|
|
2004-12-09 22:09:56 +03:00
|
|
|
if (!path) {
|
2005-07-27 05:30:58 +04:00
|
|
|
LOG(("Insufficient memory for calloc"));
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return "application/riscos";
|
2004-12-09 22:09:56 +03:00
|
|
|
}
|
2003-03-15 18:53:20 +03:00
|
|
|
LOG(("unix_path = '%s'", unix_path));
|
|
|
|
|
|
|
|
/* convert path to RISC OS format and read file type */
|
2004-07-31 16:56:25 +04:00
|
|
|
r = __riscosify(unix_path, 0, __RISCOSIFY_NO_SUFFIX, path, len, 0);
|
2003-03-15 18:53:20 +03:00
|
|
|
if (r == 0) {
|
|
|
|
LOG(("__riscosify failed"));
|
2005-07-27 05:30:58 +04:00
|
|
|
free(path);
|
2003-03-15 18:53:20 +03:00
|
|
|
return "application/riscos";
|
|
|
|
}
|
|
|
|
LOG(("riscos path '%s'", path));
|
|
|
|
|
2006-07-06 04:07:11 +04:00
|
|
|
error = xosfile_read_stamped_no_path(path, &objtype, 0, 0, 0, 0,
|
|
|
|
&file_type);
|
2005-07-27 05:30:58 +04:00
|
|
|
if (error) {
|
|
|
|
LOG(("xosfile_read_stamped_no_path failed: %s",
|
|
|
|
error->errmess));
|
|
|
|
free(path);
|
2003-03-15 18:53:20 +03:00
|
|
|
return "application/riscos";
|
|
|
|
}
|
|
|
|
|
2006-07-06 04:07:11 +04:00
|
|
|
if (objtype == osfile_IS_DIR) {
|
|
|
|
sprintf(type_buf, "application/x-netsurf-directory");
|
|
|
|
return (const char *)type_buf;
|
|
|
|
}
|
|
|
|
|
2010-12-27 17:03:45 +03:00
|
|
|
/* If filetype is text or data, and the file has an extension, try to
|
|
|
|
* map the extension to a filetype via the MimeMap file. */
|
|
|
|
if (file_type == osfile_TYPE_TEXT || file_type == osfile_TYPE_DATA) {
|
2005-07-27 05:30:58 +04:00
|
|
|
slash = strrchr(path, '/');
|
|
|
|
if (slash) {
|
|
|
|
error = xmimemaptranslate_extension_to_filetype(
|
|
|
|
slash+1, &temp);
|
|
|
|
if (error)
|
|
|
|
/* ignore error and leave file_type alone */
|
2007-08-07 08:01:06 +04:00
|
|
|
LOG(("xmimemaptranslate_extension_to_filetype: "
|
|
|
|
"0x%x %s",
|
|
|
|
error->errnum, error->errmess));
|
2005-07-27 05:30:58 +04:00
|
|
|
else
|
|
|
|
file_type = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-27 19:31:48 +04:00
|
|
|
/* search for MIME type in our internal table */
|
|
|
|
t = bsearch(&file_type, type_map, TYPE_MAP_COUNT,
|
|
|
|
sizeof(type_map[0]), cmp_type);
|
|
|
|
if (t) {
|
|
|
|
/* found, so return it */
|
|
|
|
free(path);
|
|
|
|
return t->mime_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not in internal table, so ask MimeMap */
|
2005-07-27 05:30:58 +04:00
|
|
|
error = xmimemaptranslate_filetype_to_mime_type(file_type, type_buf);
|
|
|
|
if (error) {
|
|
|
|
LOG(("0x%x %s", error->errnum, error->errmess));
|
|
|
|
free(path);
|
2003-03-15 18:53:20 +03:00
|
|
|
return "application/riscos";
|
2005-07-27 05:30:58 +04:00
|
|
|
}
|
|
|
|
/* make sure we're NULL terminated. If we're not, the MimeMap
|
|
|
|
* module's probably written past the end of the buffer from
|
|
|
|
* SVC mode. Short of rewriting MimeMap with an incompatible API,
|
|
|
|
* there's nothing we can do about it.
|
|
|
|
*/
|
|
|
|
type_buf[BUF_SIZE - 1] = '\0';
|
2003-03-15 18:53:20 +03:00
|
|
|
|
2005-07-27 05:30:58 +04:00
|
|
|
free(path);
|
2003-03-15 18:53:20 +03:00
|
|
|
|
2005-07-27 05:30:58 +04:00
|
|
|
LOG(("mime type '%s'", type_buf));
|
|
|
|
return (const char *)type_buf;
|
2004-03-22 00:32:15 +03:00
|
|
|
|
2005-07-27 05:30:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a MIME type for a local file
|
|
|
|
*
|
|
|
|
* \param ro_path RISC OS style path to file on disk
|
|
|
|
* \return MIME type string (on heap, caller should free), or NULL
|
|
|
|
*/
|
|
|
|
char *fetch_mimetype(const char *ro_path)
|
|
|
|
{
|
|
|
|
os_error *e;
|
|
|
|
bits filetype = 0, load;
|
|
|
|
int objtype;
|
|
|
|
char *mime = calloc(BUF_SIZE, sizeof(char));
|
|
|
|
char *slash;
|
2006-08-26 02:46:38 +04:00
|
|
|
struct type_entry *t;
|
2004-12-09 22:09:56 +03:00
|
|
|
|
|
|
|
if (!mime) {
|
2005-07-27 05:30:58 +04:00
|
|
|
LOG(("Insufficient memory for calloc"));
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return 0;
|
2004-12-09 22:09:56 +03:00
|
|
|
}
|
2004-03-22 00:32:15 +03:00
|
|
|
|
2005-07-27 05:30:58 +04:00
|
|
|
e = xosfile_read_no_path(ro_path, &objtype, &load, 0, 0, 0);
|
2006-07-06 10:42:54 +04:00
|
|
|
if (e) {
|
|
|
|
LOG(("xosfile_read_no_path: 0x%x: %s",
|
|
|
|
e->errnum, e->errmess));
|
|
|
|
free(mime);
|
2005-07-27 05:30:58 +04:00
|
|
|
return 0;
|
2006-07-06 10:42:54 +04:00
|
|
|
}
|
2004-03-22 01:31:30 +03:00
|
|
|
|
2006-07-06 10:42:54 +04:00
|
|
|
if (objtype == osfile_IS_DIR) {
|
|
|
|
free(mime);
|
2005-07-27 05:30:58 +04:00
|
|
|
return 0; /* directories are pointless */
|
2006-07-06 10:42:54 +04:00
|
|
|
}
|
2004-03-22 00:32:15 +03:00
|
|
|
|
2005-07-27 05:30:58 +04:00
|
|
|
if ((load >> 20) & 0xFFF) {
|
|
|
|
filetype = (load>>8) & 0x000FFF;
|
|
|
|
}
|
|
|
|
else {
|
2006-07-06 10:42:54 +04:00
|
|
|
free(mime);
|
2005-07-27 05:30:58 +04:00
|
|
|
return 0; /* no idea */
|
|
|
|
}
|
2004-03-22 00:32:15 +03:00
|
|
|
|
2005-07-27 05:30:58 +04:00
|
|
|
/* If filetype is text and the file has an extension, try to map the
|
|
|
|
* extension to a filetype via the MimeMap file. */
|
|
|
|
slash = strrchr(ro_path, '/');
|
|
|
|
if (slash && filetype == osfile_TYPE_TEXT) {
|
|
|
|
e = xmimemaptranslate_extension_to_filetype(slash+1, &load);
|
|
|
|
if (e)
|
|
|
|
/* if we get an error here, simply ignore it and
|
|
|
|
* leave filetype unchanged */
|
|
|
|
LOG(("0x%x %s", e->errnum, e->errmess));
|
|
|
|
else
|
|
|
|
filetype = load;
|
|
|
|
}
|
2004-03-22 00:32:15 +03:00
|
|
|
|
2006-08-26 02:46:38 +04:00
|
|
|
/* search for MIME type in our internal table */
|
|
|
|
t = bsearch(&filetype, type_map, TYPE_MAP_COUNT,
|
|
|
|
sizeof(type_map[0]), cmp_type);
|
|
|
|
if (t) {
|
|
|
|
/* found, so return it */
|
|
|
|
strncpy(mime, t->mime_type, BUF_SIZE);
|
|
|
|
return mime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not in internal table, so ask MimeMap */
|
2005-07-27 05:30:58 +04:00
|
|
|
e = xmimemaptranslate_filetype_to_mime_type(filetype, mime);
|
2006-07-06 10:42:54 +04:00
|
|
|
if (e) {
|
|
|
|
LOG(("xmimemaptranslate_filetype_to_mime_type: 0x%x: %s",
|
|
|
|
e->errnum, e->errmess));
|
|
|
|
free(mime);
|
2005-07-27 05:30:58 +04:00
|
|
|
return 0;
|
2006-07-06 10:42:54 +04:00
|
|
|
}
|
2005-07-27 05:30:58 +04:00
|
|
|
/* make sure we're NULL terminated. If we're not, the MimeMap
|
|
|
|
* module's probably written past the end of the buffer from
|
|
|
|
* SVC mode. Short of rewriting MimeMap with an incompatible API,
|
|
|
|
* there's nothing we can do about it.
|
|
|
|
*/
|
|
|
|
mime[BUF_SIZE - 1] = '\0';
|
|
|
|
|
|
|
|
return mime;
|
2003-03-15 18:53:20 +03:00
|
|
|
}
|
|
|
|
|
2005-07-27 19:31:48 +04:00
|
|
|
/**
|
|
|
|
* Comparison function for bsearch
|
|
|
|
*/
|
|
|
|
int cmp_type(const void *x, const void *y)
|
|
|
|
{
|
|
|
|
const bits *p = x;
|
|
|
|
const struct type_entry *q = y;
|
|
|
|
return *p < q->file_type ? -1 : (*p == q->file_type ? 0 : +1);
|
|
|
|
}
|
|
|
|
|
2004-03-11 05:19:14 +03:00
|
|
|
/**
|
|
|
|
* Determine the RISC OS filetype for a content.
|
2005-07-27 05:30:58 +04:00
|
|
|
*
|
|
|
|
* \param content The content to examine.
|
|
|
|
* \return The RISC OS filetype corresponding to the content
|
2004-03-11 05:19:14 +03:00
|
|
|
*/
|
2010-04-07 13:28:01 +04:00
|
|
|
int ro_content_filetype(hlcache_handle *c)
|
2004-03-11 05:19:14 +03:00
|
|
|
{
|
|
|
|
int file_type;
|
|
|
|
os_error *error;
|
|
|
|
|
2010-04-07 13:12:46 +04:00
|
|
|
file_type = ro_content_filetype_from_type(content_get_type(c));
|
2005-12-31 07:35:34 +03:00
|
|
|
if (file_type != 0)
|
|
|
|
return file_type;
|
|
|
|
|
2010-04-07 13:12:46 +04:00
|
|
|
error = xmimemaptranslate_mime_type_to_filetype(
|
|
|
|
content_get_mime_type(c), (bits *) &file_type);
|
2005-12-31 07:35:34 +03:00
|
|
|
if (error)
|
|
|
|
return 0xffd;
|
2009-03-27 05:24:00 +03:00
|
|
|
|
2005-12-31 07:35:34 +03:00
|
|
|
return file_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the RISC OS filetype from a content type.
|
|
|
|
*
|
|
|
|
* \param type The content type to examine.
|
|
|
|
* \return The RISC OS filetype corresponding to the content, or 0 for unknown
|
|
|
|
*/
|
|
|
|
int ro_content_filetype_from_type(content_type type) {
|
|
|
|
switch (type) {
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_HTML: return 0xfaf;
|
|
|
|
case CONTENT_TEXTPLAIN: return 0xfff;
|
|
|
|
case CONTENT_CSS: return 0xf79;
|
2008-09-16 02:45:44 +04:00
|
|
|
#if defined(WITH_MNG) || defined(WITH_PNG)
|
|
|
|
case CONTENT_PNG: return 0xb60;
|
|
|
|
#endif
|
2004-09-14 03:59:30 +04:00
|
|
|
#ifdef WITH_MNG
|
2004-07-26 00:45:16 +04:00
|
|
|
case CONTENT_JNG: return 0xf78;
|
2004-09-14 03:59:30 +04:00
|
|
|
case CONTENT_MNG: return 0xf84;
|
|
|
|
#endif
|
|
|
|
#ifdef WITH_JPEG
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_JPEG: return 0xc85;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_GIF
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_GIF: return 0x695;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
2006-02-25 21:51:51 +03:00
|
|
|
#ifdef WITH_BMP
|
|
|
|
case CONTENT_BMP: return 0x69c;
|
2006-02-26 05:25:19 +03:00
|
|
|
case CONTENT_ICO: return 0x132;
|
2006-02-25 21:51:51 +03:00
|
|
|
#endif
|
2004-09-14 03:59:30 +04:00
|
|
|
#ifdef WITH_SPRITE
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_SPRITE: return 0xff9;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_DRAW
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_DRAW: return 0xaff;
|
2005-12-10 17:31:33 +03:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_ARTWORKS
|
|
|
|
case CONTENT_ARTWORKS: return 0xd94;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
2008-07-29 00:56:24 +04:00
|
|
|
#ifdef WITH_NS_SVG
|
2007-07-13 12:25:34 +04:00
|
|
|
case CONTENT_SVG: return 0xaad;
|
2008-07-29 00:56:24 +04:00
|
|
|
#endif
|
2004-03-11 05:19:14 +03:00
|
|
|
default: break;
|
|
|
|
}
|
2005-12-31 07:35:34 +03:00
|
|
|
return 0;
|
2004-03-11 05:19:14 +03:00
|
|
|
}
|
2008-05-23 17:29:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the type of a local file.
|
|
|
|
*
|
|
|
|
* \param unix_path Unix style path to file on disk
|
|
|
|
* \return File type
|
|
|
|
*/
|
|
|
|
bits ro_filetype_from_unix_path(const char *unix_path)
|
|
|
|
{
|
|
|
|
unsigned int len = strlen(unix_path) + 100;
|
|
|
|
char *path = calloc(len, 1);
|
2008-05-23 18:17:31 +04:00
|
|
|
char *r;
|
2008-05-23 17:29:37 +04:00
|
|
|
os_error *error;
|
|
|
|
bits file_type;
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
LOG(("Insufficient memory for calloc"));
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return osfile_TYPE_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert path to RISC OS format and read file type */
|
|
|
|
r = __riscosify(unix_path, 0, __RISCOSIFY_NO_SUFFIX, path, len, 0);
|
|
|
|
if (r == 0) {
|
|
|
|
LOG(("__riscosify failed"));
|
|
|
|
free(path);
|
|
|
|
return osfile_TYPE_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = xosfile_read_stamped_no_path(path, 0, 0, 0, 0, 0,
|
|
|
|
&file_type);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfile_read_stamped_no_path failed: %s",
|
|
|
|
error->errmess));
|
|
|
|
free(path);
|
|
|
|
return osfile_TYPE_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
return file_type;
|
|
|
|
}
|
|
|
|
|