From 93715c46a99c96d6c866968312691ec9ab0f6a03 Mon Sep 17 00:00:00 2001 From: jcid Date: Sun, 7 Oct 2007 00:36:34 +0200 Subject: Initial revision --- src/misc.c | 271 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 271 insertions(+) create mode 100644 src/misc.c (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 00000000..f7ab00d3 --- /dev/null +++ b/src/misc.c @@ -0,0 +1,271 @@ +/* + * File: misc.c + * + * Copyright (C) 2000 Jorge Arellano Cid , + * Jörgen Viksell + * + * This program 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; either version 3 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include + +#include "msg.h" +#include "misc.h" + + +/* + * Escape characters as %XX sequences. + * Return value: New string. + */ +char *a_Misc_escape_chars(const char *str, char *esc_set) +{ + static const char *hex = "0123456789ABCDEF"; + char *p = NULL; + Dstr *dstr; + int i; + + dstr = dStr_sized_new(64); + for (i = 0; str[i]; ++i) { + if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) { + dStr_append_c(dstr, '%'); + dStr_append_c(dstr, hex[(str[i] >> 4) & 15]); + dStr_append_c(dstr, hex[str[i] & 15]); + } else { + dStr_append_c(dstr, str[i]); + } + } + p = dstr->str; + dStr_free(dstr, FALSE); + + return p; +} + + +#define TAB_SIZE 8 +/* + * Takes a string and converts any tabs to spaces. + */ +char *a_Misc_expand_tabs(const char *str) +{ + Dstr *New = dStr_new(""); + int len, i, j, pos, old_pos; + char *val; + + if ((len = strlen(str))) { + for (pos = 0, i = 0; i < len; i++) { + if (str[i] == '\t') { + /* Fill with whitespaces until the next tab. */ + old_pos = pos; + pos += TAB_SIZE - (pos % TAB_SIZE); + for (j = old_pos; j < pos; j++) + dStr_append_c(New, ' '); + } else { + dStr_append_c(New, str[i]); + pos++; + } + } + } + val = New->str; + dStr_free(New, FALSE); + return val; +} + +/* TODO: could use dStr ADT! */ +typedef struct ContentType_ { + const char *str; + int len; +} ContentType_t; + +static const ContentType_t MimeTypes[] = { + { "application/octet-stream", 24 }, + { "text/html", 9 }, + { "text/plain", 10 }, + { "image/gif", 9 }, + { "image/png", 9 }, + { "image/jpeg", 10 }, + { NULL, 0 } +}; + +/* + * Detects 'Content-Type' from a data stream sample. + * + * It uses the magic(5) logic from file(1). Currently, it + * only checks the few mime types that Dillo supports. + * + * 'Data' is a pointer to the first bytes of the raw data. + * + * Return value: (0 on success, 1 on doubt, 2 on lack of data). + */ +int a_Misc_get_content_type_from_data(void *Data, size_t Size, const char **PT) +{ + int st = 1; /* default to "doubt' */ + int Type = 0; /* default to "application/octet-stream" */ + char *p = Data; + size_t i, non_ascci; + + /* HTML try */ + for (i = 0; i < Size && isspace(p[i]); ++i); + if ((Size - i >= 5 && !dStrncasecmp(p+i, "= 5 && !dStrncasecmp(p+i, "= 6 && !dStrncasecmp(p+i, "= 14 && !dStrncasecmp(p+i, "= 17 && !dStrncasecmp(p+i, "