annotate src/misc.c @ 1699:f4a6b351012d

Removed some obsolete copyright notices that lingered after rewrites.
author Jorge Arellano Cid <jcid@dillo.org>
date Sun, 29 Aug 2010 21:58:07 -0400
parents cf7f2d3312fb
children e44231f9c6ca
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: misc.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
4 * Copyright (C) 2000-2007 Jorge Arellano Cid <jcid@dillo.org>,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 #include <ctype.h>
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
16 #include <assert.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17
1128
fbe2ce98a4b8 make tab expansion for plain text utf8 aware
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 930
diff changeset
18 #include "utf8.hh"
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include "msg.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #include "misc.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 * Escape characters as %XX sequences.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 * Return value: New string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 */
306
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 274
diff changeset
26 char *a_Misc_escape_chars(const char *str, const char *esc_set)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 {
1141
corvid <corvid@lavabit.com>
parents: 1128
diff changeset
28 static const char *const hex = "0123456789ABCDEF";
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 char *p = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 Dstr *dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 for (i = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 dStr_append_c(dstr, '%');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 dStr_append_c(dstr, hex[(str[i] >> 4) & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 dStr_append_c(dstr, hex[str[i] & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 dStr_append_c(dstr, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 p = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 #define TAB_SIZE 8
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 * Takes a string and converts any tabs to spaces.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 */
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
53 int
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
54 a_Misc_expand_tabs(char **start, char *end, char *buf, int buflen)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 {
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
56 int j, pos = 0, written = 0, old_pos, char_len;
1128
fbe2ce98a4b8 make tab expansion for plain text utf8 aware
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 930
diff changeset
57 uint_t code;
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
58 static const int combining_char_space = 32;
1128
fbe2ce98a4b8 make tab expansion for plain text utf8 aware
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 930
diff changeset
59
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
60 while (*start < end && written < buflen - TAB_SIZE - combining_char_space) {
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
61 code = a_Utf8_decode(*start, end, &char_len);
1160
ea163ab97e87 faster expand tabs
corvid <corvid@lavabit.com>
parents: 1145
diff changeset
62
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
63 if (code == '\t') {
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
64 /* Fill with whitespaces until the next tab. */
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
65 old_pos = pos;
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
66 pos += TAB_SIZE - (pos % TAB_SIZE);
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
67 for (j = old_pos; j < pos; j++)
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
68 buf[written++] = ' ';
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
69 } else {
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
70 assert(char_len <= 4);
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
71 for (j = 0; j < char_len; j++)
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
72 buf[written++] = (*start)[j];
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
73 pos++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 }
1128
fbe2ce98a4b8 make tab expansion for plain text utf8 aware
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 930
diff changeset
75
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
76 *start += char_len;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 }
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
78
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
79 /* If following chars are combining chars (e.g. accents) add them to the
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
80 * buffer. We have reserved combining_char_space bytes for this.
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
81 * If there should be more combining chars, we split nevertheless.
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
82 */
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
83 while (*start < end && written < buflen - 4) {
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
84 code = a_Utf8_decode(*start, end, &char_len);
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
85
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
86 if (! a_Utf8_combining_char(code))
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
87 break;
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
88
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
89 assert(char_len <= 4);
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
90 for (j = 0; j < char_len; j++)
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
91 buf[written++] = (*start)[j];
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1453
diff changeset
92
1453
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
93 *start += char_len;
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
94 }
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
95
328111d18d57 respect UTF-8 when splitting long lines in plain.cc (noticed by corvid)
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1317
diff changeset
96 return written;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 /* TODO: could use dStr ADT! */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 typedef struct ContentType_ {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 const char *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 } ContentType_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 static const ContentType_t MimeTypes[] = {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 { "application/octet-stream", 24 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 { "text/html", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 { "text/plain", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 { "image/gif", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 { "image/png", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 { "image/jpeg", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 { NULL, 0 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
115 typedef enum {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
116 DT_OCTET_STREAM = 0,
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
117 DT_TEXT_HTML,
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
118 DT_TEXT_PLAIN,
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
119 DT_IMAGE_GIF,
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
120 DT_IMAGE_PNG,
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
121 DT_IMAGE_JPG,
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
122 } DetectedContentType;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
123
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 * Detects 'Content-Type' from a data stream sample.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 * It uses the magic(5) logic from file(1). Currently, it
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 * only checks the few mime types that Dillo supports.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 * 'Data' is a pointer to the first bytes of the raw data.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 * Return value: (0 on success, 1 on doubt, 2 on lack of data).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 int a_Misc_get_content_type_from_data(void *Data, size_t Size, const char **PT)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 {
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
136 size_t i, non_ascci, non_ascci_text, bin_chars;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
137 char *p = Data;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 int st = 1; /* default to "doubt' */
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
139 DetectedContentType Type = DT_OCTET_STREAM; /* default to binary */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 /* HTML try */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1186
diff changeset
142 for (i = 0; i < Size && dIsspace(p[i]); ++i);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 if ((Size - i >= 5 && !dStrncasecmp(p+i, "<html", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 (Size - i >= 5 && !dStrncasecmp(p+i, "<head", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 (Size - i >= 6 && !dStrncasecmp(p+i, "<title", 6)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 (Size - i >= 14 && !dStrncasecmp(p+i, "<!doctype html", 14)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 /* this line is workaround for FTP through the Squid proxy */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 (Size - i >= 17 && !dStrncasecmp(p+i, "<!-- HTML listing", 17))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
150 Type = DT_TEXT_HTML;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 /* Images */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 } else if (Size >= 4 && !dStrncasecmp(p, "GIF8", 4)) {
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
154 Type = DT_IMAGE_GIF;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 } else if (Size >= 4 && !dStrncasecmp(p, "\x89PNG", 4)) {
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
157 Type = DT_IMAGE_PNG;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 } else if (Size >= 2 && !dStrncasecmp(p, "\xff\xd8", 2)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 /* JPEG has the first 2 bytes set to 0xffd8 in BigEndian - looking
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 * at the character representation should be machine independent. */
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
162 Type = DT_IMAGE_JPG;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 /* Text */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 } else {
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
167 /* Heuristic for "text/plain"
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
168 * {ASCII, LATIN1, UTF8, KOI8-R, CP-1251}
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
169 * All in the above set regard [00-31] as control characters.
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
170 * LATIN1: [7F-9F] unused
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
171 * CP-1251 {7F,98} unused (two characters).
930
b277eed3119c whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents: 368
diff changeset
172 *
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
173 * We'll use [0-31] as indicators of non-text content.
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
174 * Better heuristics are welcomed! :-) */
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
175
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
176 non_ascci = non_ascci_text = bin_chars = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 Size = MIN (Size, 256);
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
178 for (i = 0; i < Size; i++) {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
179 int ch = (uchar_t) p[i];
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1186
diff changeset
180 if (ch < 32 && !dIsspace(ch))
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
181 ++bin_chars;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
182 if (ch > 126)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 ++non_ascci;
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
184 if (ch > 190)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
185 ++non_ascci_text;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
186 }
1145
9b57a492918b Improved recognition heuristics for plain text in utf8
corvid <corvid@lavabit.com>
parents: 1141
diff changeset
187 if (bin_chars == 0 && (non_ascci - non_ascci_text) <= Size/10) {
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
188 /* Let's say text: if "rare" chars are <= 10% */
1145
9b57a492918b Improved recognition heuristics for plain text in utf8
corvid <corvid@lavabit.com>
parents: 1141
diff changeset
189 Type = DT_TEXT_PLAIN;
9b57a492918b Improved recognition heuristics for plain text in utf8
corvid <corvid@lavabit.com>
parents: 1141
diff changeset
190 } else if (Size > 0) {
9b57a492918b Improved recognition heuristics for plain text in utf8
corvid <corvid@lavabit.com>
parents: 1141
diff changeset
191 /* a special check for UTF-8 */
9b57a492918b Improved recognition heuristics for plain text in utf8
corvid <corvid@lavabit.com>
parents: 1141
diff changeset
192 Size = a_Utf8_end_of_char(p, Size - 1) + 1;
9b57a492918b Improved recognition heuristics for plain text in utf8
corvid <corvid@lavabit.com>
parents: 1141
diff changeset
193 if (a_Utf8_test(p, Size) > 0)
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
194 Type = DT_TEXT_PLAIN;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
195 }
1145
9b57a492918b Improved recognition heuristics for plain text in utf8
corvid <corvid@lavabit.com>
parents: 1141
diff changeset
196 if (Size >= 256)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 *PT = MimeTypes[Type].str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 /*
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
205 * Parse Content-Type string, e.g., "text/html; charset=utf-8".
1272
83a462cf5d02 follow RFC a bit more closely for Content-Type parsing
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
206 * Content-Type is defined in RFC 2045 section 5.1.
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
207 */
1317
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
208 void a_Misc_parse_content_type(const char *type, char **major, char **minor,
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
209 char **charset)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
210 {
1272
83a462cf5d02 follow RFC a bit more closely for Content-Type parsing
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
211 static const char tspecials_space[] = "()<>@,;:\\\"/[]?= ";
1317
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
212 const char *str, *s;
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
213
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
214 if (major)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
215 *major = NULL;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
216 if (minor)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
217 *minor = NULL;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
218 if (charset)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
219 *charset = NULL;
1317
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
220 if (!(str = type))
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
221 return;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
222
1272
83a462cf5d02 follow RFC a bit more closely for Content-Type parsing
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
223 for (s = str; *s && !iscntrl((uchar_t)*s) && !strchr(tspecials_space, *s);
83a462cf5d02 follow RFC a bit more closely for Content-Type parsing
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
224 s++) ;
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
225 if (major)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
226 *major = dStrndup(str, s - str);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
227
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
228 if (*s == '/') {
1272
83a462cf5d02 follow RFC a bit more closely for Content-Type parsing
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
229 for (str = ++s;
83a462cf5d02 follow RFC a bit more closely for Content-Type parsing
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
230 *s && !iscntrl((uchar_t)*s) && !strchr(tspecials_space, *s); s++) ;
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
231 if (minor)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
232 *minor = dStrndup(str, s - str);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
233 }
1317
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
234 if (charset && *s &&
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
235 (dStrncasecmp(type, "text/", 5) == 0 ||
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
236 dStrncasecmp(type, "application/xhtml+xml", 21) == 0)) {
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
237 /* "charset" parameter defined for text media type in RFC 2046,
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
238 * application/xhtml+xml in RFC 3236.
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
239 *
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
240 * Note that RFC 3023 lists some main xml media types and provides
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
241 * the convention of using the "+xml" minor type suffix for other
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
242 * xml types, so it would be reasonable to check for that suffix if
a251eba3613b look for charset parameter with application/xhtml+xml media type
corvid <corvid@lavabit.com>
parents: 1272
diff changeset
243 * we have need to care about various xml types someday.
1272
83a462cf5d02 follow RFC a bit more closely for Content-Type parsing
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
244 */
250
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
245 const char terminators[] = " ;\t";
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
246 const char key[] = "charset";
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
247
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
248 if ((s = dStristr(str, key)) &&
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
249 (s == str || strchr(terminators, s[-1]))) {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
250 s += sizeof(key) - 1;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
251 for ( ; *s == ' ' || *s == '\t'; ++s);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
252 if (*s == '=') {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
253 size_t len;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
254 for (++s; *s == ' ' || *s == '\t'; ++s);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
255 if ((len = strcspn(s, terminators))) {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
256 if (*s == '"' && s[len-1] == '"' && len > 1) {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
257 /* quoted string */
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
258 s++;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
259 len -= 2;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
260 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
261 *charset = dStrndup(s, len);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
262 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
263 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
264 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
265 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
266 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
267
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
268 /*
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
269 * Compare two Content-Type strings.
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
270 * Return 0 if they are equivalent, and 1 otherwise.
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
271 */
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
272 int a_Misc_content_type_cmp(const char *ct1, const char *ct2)
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
273 {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
274 char *major1, *major2, *minor1, *minor2, *charset1, *charset2;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
275 int ret;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
276
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
277 if ((!ct1 || !*ct1) && (!ct2 || !*ct2))
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
278 return 0;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
279 if ((!ct1 || !*ct1) || (!ct2 || !*ct2))
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
280 return 1;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
281
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
282 a_Misc_parse_content_type(ct1, &major1, &minor1, &charset1);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
283 a_Misc_parse_content_type(ct2, &major2, &minor2, &charset2);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
284
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
285 if (major1 && major2 && !dStrcasecmp(major1, major2) &&
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
286 minor1 && minor2 && !dStrcasecmp(minor1, minor2) &&
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
287 ((!charset1 && !charset2) ||
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
288 (charset1 && charset2 && !dStrcasecmp(charset1, charset2)) ||
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
289 (!charset1 && charset2 && !dStrcasecmp(charset2, "UTF-8")) ||
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
290 (charset1 && !charset2 && !dStrcasecmp(charset1, "UTF-8")))) {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
291 ret = 0;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
292 } else {
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
293 ret = 1;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
294 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
295 dFree(major1); dFree(major2);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
296 dFree(minor1); dFree(minor2);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
297 dFree(charset1); dFree(charset2);
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
298
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
299 return ret;
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
300 }
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
301
fce9380ee68a - Switched from charset to content-type for handling data.
jcid
parents: 180
diff changeset
302 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
303 * Check the server-supplied 'Content-Type' against our detected type.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
304 * (some servers seem to default to "text/plain").
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
305 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
306 * Return value:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307 * 0, if they match
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308 * -1, if a mismatch is detected
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
309 *
155
fb8da086d7da + s/there're/there are/g
jcid
parents: 35
diff changeset
310 * There are many MIME types Dillo doesn't know, they're handled
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
311 * as "application/octet-stream" (as the SPEC says).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
312 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
313 * A mismatch happens when receiving a binary stream as
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314 * "text/plain" or "text/html", or an image that's not an image of its kind.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
316 * Note: this is a basic security procedure.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
318 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
319 int a_Misc_content_type_check(const char *EntryType, const char *DetectedType)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
321 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
322 int st = -1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
323
274
1deca4cad4c4 - Implemented OBJECT as link (similar to FRAME).
jcid
parents: 250
diff changeset
324 _MSG("Type check: [Srv: %s Det: %s]\n", EntryType, DetectedType);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
325
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
326 if (!EntryType)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
327 return 0; /* there's no mismatch without server type */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
328
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
329 for (i = 1; MimeTypes[i].str; ++i)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
330 if (dStrncasecmp(EntryType, MimeTypes[i].str, MimeTypes[i].len) == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
331 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
332
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
333 if (!MimeTypes[i].str) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
334 /* type not found, no mismatch */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
335 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
336 } else if (dStrncasecmp(EntryType, "image/", 6) == 0 &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337 !dStrncasecmp(DetectedType,MimeTypes[i].str,MimeTypes[i].len)){
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338 /* An image, and there's an exact match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
339 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
340 } else if (dStrncasecmp(EntryType, "text/", 5) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341 dStrncasecmp(DetectedType, "application/", 12)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342 /* Not an application sent as text */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
345
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
346 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
347 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350 * Parse a geometry string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
351 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
352 int a_Misc_parse_geometry(char *str, int *x, int *y, int *w, int *h)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
353 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
354 char *p, *t1, *t2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
355 int n1, n2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
356 int ret = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
357
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
358 if ((p = strchr(str, 'x')) || (p = strchr(str, 'X'))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
359 n1 = strtol(str, &t1, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
360 n2 = strtol(++p, &t2, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
361 if (t1 != str && t2 != p) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
362 *w = n1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
363 *h = n2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
364 ret = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
365 /* parse x,y now */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
366 p = t2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
367 n1 = strtol(p, &t1, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
368 n2 = strtol(t1, &t2, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
369 if (t1 != p && t2 != t1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
370 *x = n1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
371 *y = n2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
372 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
373 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
374 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
375 _MSG("geom: w,h,x,y = (%d,%d,%d,%d)\n", *w, *h, *x, *y);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
376 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
377 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
378
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
379 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
380 * Encodes string using base64 encoding.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
381 * Return value: new string or NULL if input string is empty.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
382 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
383 char *a_Misc_encode_base64(const char *in)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
384 {
1141
corvid <corvid@lavabit.com>
parents: 1128
diff changeset
385 static const char *const base64_hex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
corvid <corvid@lavabit.com>
parents: 1128
diff changeset
386 "abcdefghijklmnopqrstuvwxyz"
corvid <corvid@lavabit.com>
parents: 1128
diff changeset
387 "0123456789+/";
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
388 char *out = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
389 int len, i = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
390
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
391 if (in == NULL) return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
392 len = strlen(in);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
393
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
394 out = (char *)dMalloc((len + 2) / 3 * 4 + 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
395
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
396 for (; len >= 3; len -= 3) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
397 out[i++] = base64_hex[in[0] >> 2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
398 out[i++] = base64_hex[((in[0]<<4) & 0x30) | (in[1]>>4)];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
399 out[i++] = base64_hex[((in[1]<<2) & 0x3c) | (in[2]>>6)];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
400 out[i++] = base64_hex[in[2] & 0x3f];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
401 in += 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
402 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
403
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
404 if (len > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
405 unsigned char fragment;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
406 out[i++] = base64_hex[in[0] >> 2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
407 fragment = (in[0] << 4) & 0x30;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
408 if (len > 1) fragment |= in[1] >> 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
409 out[i++] = base64_hex[fragment];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
410 out[i++] = (len < 2) ? '=' : base64_hex[(in[1] << 2) & 0x3c];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
411 out[i++] = '=';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
412 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
413 out[i] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
414 return out;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
415 }
180
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
416
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
417 /*
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
418 * Load a local file into a dStr.
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
419 * Return value: dStr on success, NULL on error.
368
2242da885677 - s/todo:/TODO:/g
jcid
parents: 340
diff changeset
420 * TODO: a filesize threshold may be implemented.
180
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
421 */
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
422 Dstr *a_Misc_file2dstr(const char *filename)
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
423 {
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
424 FILE *F_in;
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
425 int n;
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
426 char buf[4096];
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
427 Dstr *dstr = NULL;
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
428
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
429 if ((F_in = fopen(filename, "r"))) {
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
430 dstr = dStr_sized_new(4096);
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
431 while ((n = fread (buf, 1, 4096, F_in)) > 0) {
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
432 dStr_append_l(dstr, buf, n);
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
433 }
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
434 fclose(F_in);
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
435 }
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
436 return dstr;
0038a2943cc2 - Made file inputs free the loaded file after the page is left.
jcid
parents: 155
diff changeset
437 }