NetSurf
Data Structures | Macros | Functions
mimesniff.c File Reference

MIME type sniffer implementation. More...

#include <string.h>
#include <strings.h>
#include "utils/http.h"
#include "utils/utils.h"
#include "utils/corestrings.h"
#include "content/content_factory.h"
#include "content/mimesniff.h"
Include dependency graph for mimesniff.c:

Go to the source code of this file.

Data Structures

struct  map_s
 

Macros

#define SIG(t, s, x)   { (const uint8_t *) s, SLEN(s), x, t }
 
#define SIG(t, s, x)   { (const uint8_t *) s, SLEN(s), x, t }
 
#define SIG(t, s, x)   { (const uint8_t *) s, SLEN(s), x, t }
 
#define SIG(t, s, x)   { (const uint8_t *) s, SLEN(s), x, t }
 
#define SIG(t, s)   { (const uint8_t *) s, SLEN(s), t }
 
#define RDF_NS   "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 
#define RSS_NS   "http://purl.org/rss/1.0"
 
#define MATCH(s)
 
#define S(s)   { s, SLEN(s) }
 

Functions

static bool mimesniff__has_binary_octets (const uint8_t *data, size_t len)
 
static nserror mimesniff__match_mp4 (const uint8_t *data, size_t len, lwc_string **effective_type)
 
static nserror mimesniff__match_unknown_ws (const uint8_t *data, size_t len, lwc_string **effective_type)
 
static nserror mimesniff__match_unknown_bom (const uint8_t *data, size_t len, lwc_string **effective_type)
 
static nserror mimesniff__match_unknown_riff (const uint8_t *data, size_t len, lwc_string **effective_type)
 
static nserror mimesniff__match_unknown_exact (const uint8_t *data, size_t len, bool allow_unsafe, lwc_string **effective_type)
 
static nserror mimesniff__match_unknown (const uint8_t *data, size_t len, bool allow_unsafe, lwc_string **effective_type)
 
static nserror mimesniff__compute_unknown (const uint8_t *data, size_t len, lwc_string **effective_type)
 
static nserror mimesniff__compute_text_or_binary (const uint8_t *data, size_t len, lwc_string **effective_type)
 
static nserror mimesniff__compute_image (lwc_string *official_type, const uint8_t *data, size_t len, lwc_string **effective_type)
 
static nserror mimesniff__compute_feed_or_html (const uint8_t *data, size_t len, lwc_string **effective_type)
 
nserror mimesniff_compute_effective_type (const char *content_type_header, const uint8_t *data, size_t len, bool sniff_allowed, bool image_only, lwc_string **effective_type)
 Compute the effective MIME type for an object. More...
 

Detailed Description

MIME type sniffer implementation.

Spec version: 2011-11-27

Definition in file mimesniff.c.

Macro Definition Documentation

◆ MATCH

#define MATCH (   s)
Value:
SLEN(s) <= (size_t) (end - data) && \
memcmp(data, s, SLEN(s)) == 0
#define SLEN(x)
Calculate length of constant C string.
Definition: utils.h:88

◆ RDF_NS

#define RDF_NS   "http://www.w3.org/1999/02/22-rdf-syntax-ns#"

◆ RSS_NS

#define RSS_NS   "http://purl.org/rss/1.0"

◆ S

#define S (   s)    { s, SLEN(s) }

◆ SIG [1/5]

#define SIG (   t,
 
)    { (const uint8_t *) s, SLEN(s), t }

◆ SIG [2/5]

#define SIG (   t,
  s,
 
)    { (const uint8_t *) s, SLEN(s), x, t }

◆ SIG [3/5]

#define SIG (   t,
  s,
 
)    { (const uint8_t *) s, SLEN(s), x, t }

◆ SIG [4/5]

#define SIG (   t,
  s,
 
)    { (const uint8_t *) s, SLEN(s), x, t }

◆ SIG [5/5]

#define SIG (   t,
  s,
 
)    { (const uint8_t *) s, SLEN(s), x, t }

Function Documentation

◆ mimesniff__compute_feed_or_html()

static nserror mimesniff__compute_feed_or_html ( const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 426 of file mimesniff.c.

References MATCH, min, NSERROR_NEED_DATA, NSERROR_OK, RDF_NS, RSS_NS, and SLEN.

Referenced by mimesniff_compute_effective_type().

Here is the caller graph for this function:

◆ mimesniff__compute_image()

static nserror mimesniff__compute_image ( lwc_string *  official_type,
const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 371 of file mimesniff.c.

References image_types, NSERROR_NEED_DATA, NSERROR_OK, SIG, SLEN, and type.

Referenced by mimesniff_compute_effective_type().

Here is the caller graph for this function:

◆ mimesniff__compute_text_or_binary()

static nserror mimesniff__compute_text_or_binary ( const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 338 of file mimesniff.c.

References map_s::len, mimesniff__has_binary_octets(), mimesniff__match_unknown(), min, NSERROR_NEED_DATA, and NSERROR_OK.

Referenced by mimesniff_compute_effective_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mimesniff__compute_unknown()

static nserror mimesniff__compute_unknown ( const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 314 of file mimesniff.c.

References map_s::len, mimesniff__has_binary_octets(), mimesniff__match_unknown(), min, NSERROR_NEED_DATA, and NSERROR_OK.

Referenced by mimesniff_compute_effective_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mimesniff__has_binary_octets()

static bool mimesniff__has_binary_octets ( const uint8_t *  data,
size_t  len 
)
static

Definition at line 43 of file mimesniff.c.

Referenced by mimesniff__compute_text_or_binary(), and mimesniff__compute_unknown().

Here is the caller graph for this function:

◆ mimesniff__match_mp4()

static nserror mimesniff__match_mp4 ( const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 61 of file mimesniff.c.

References NSERROR_NOT_FOUND, and NSERROR_OK.

Referenced by mimesniff__match_unknown().

Here is the caller graph for this function:

◆ mimesniff__match_unknown()

static nserror mimesniff__match_unknown ( const uint8_t *  data,
size_t  len,
bool  allow_unsafe,
lwc_string **  effective_type 
)
static

Definition at line 286 of file mimesniff.c.

References map_s::len, mimesniff__match_mp4(), mimesniff__match_unknown_bom(), mimesniff__match_unknown_exact(), mimesniff__match_unknown_riff(), mimesniff__match_unknown_ws(), NSERROR_NOT_FOUND, and NSERROR_OK.

Referenced by mimesniff__compute_text_or_binary(), and mimesniff__compute_unknown().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mimesniff__match_unknown_bom()

static nserror mimesniff__match_unknown_bom ( const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 197 of file mimesniff.c.

References map_s::len, NSERROR_NOT_FOUND, NSERROR_OK, map_s::sig, SIG, and map_s::type.

Referenced by mimesniff__match_unknown().

Here is the caller graph for this function:

◆ mimesniff__match_unknown_exact()

static nserror mimesniff__match_unknown_exact ( const uint8_t *  data,
size_t  len,
bool  allow_unsafe,
lwc_string **  effective_type 
)
static

Definition at line 245 of file mimesniff.c.

References map_s::len, NSERROR_NOT_FOUND, NSERROR_OK, map_s::safe, map_s::sig, SIG, and map_s::type.

Referenced by mimesniff__match_unknown().

Here is the caller graph for this function:

◆ mimesniff__match_unknown_riff()

static nserror mimesniff__match_unknown_riff ( const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 220 of file mimesniff.c.

References map_s::len, NSERROR_NOT_FOUND, NSERROR_OK, map_s::sig, SIG, SLEN, and map_s::type.

Referenced by mimesniff__match_unknown().

Here is the caller graph for this function:

◆ mimesniff__match_unknown_ws()

static nserror mimesniff__match_unknown_ws ( const uint8_t *  data,
size_t  len,
lwc_string **  effective_type 
)
static

Definition at line 124 of file mimesniff.c.

References map_s::len, NSERROR_NOT_FOUND, NSERROR_OK, map_s::sig, SIG, and map_s::type.

Referenced by mimesniff__match_unknown().

Here is the caller graph for this function:

◆ mimesniff_compute_effective_type()

nserror mimesniff_compute_effective_type ( const char *  content_type_header,
const uint8_t *  data,
size_t  len,
bool  sniff_allowed,
bool  image_only,
lwc_string **  effective_type 
)

Compute the effective MIME type for an object.

The implementation uses the sniffing algorithm described in http://mimesniff.spec.whatwg.org/

Parameters
content_type_headerSource content type header
dataFirst data chunk, or NULL
lenLength of data, in bytes
sniff_allowedWhether MIME type sniffing is allowed
image_onlySniff image types only
effective_typeLocation to receive computed type
Returns
NSERROR_OK on success, NSERROR_NEED_DATA iff data is NULL and data is needed NSERROR_NOT_FOUND if sniffing is prohibited and no Content-Type header was found

Definition at line 563 of file mimesniff.c.

References content_factory_type_from_mime_type(), CONTENT_IMAGE, http_content_type_destroy(), http_parse_content_type(), http_content_type::media_type, mimesniff__compute_feed_or_html(), mimesniff__compute_image(), mimesniff__compute_text_or_binary(), mimesniff__compute_unknown(), NSERROR_NOT_FOUND, NSERROR_OK, S, and SLEN.

Referenced by hlcache_llcache_callback().

Here is the call graph for this function:
Here is the caller graph for this function: