annotate src/misc.c @ 35:d9e7b35430de

Updated copyright lines
author jcid
date Mon, 05 Nov 2007 21:25:14 +0100
parents 6ee11bf9e3ea
children fb8da086d7da
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 Jörgen Viksell <vsksga@hotmail.com>
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
5 * Copyright (C) 2000-2007 Jorge Arellano Cid <jcid@dillo.org>,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18
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 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 * Escape characters as %XX sequences.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 * Return value: New string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 char *a_Misc_escape_chars(const char *str, char *esc_set)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 static const char *hex = "0123456789ABCDEF";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 char *p = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 Dstr *dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 for (i = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 dStr_append_c(dstr, '%');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 dStr_append_c(dstr, hex[(str[i] >> 4) & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 dStr_append_c(dstr, hex[str[i] & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 dStr_append_c(dstr, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 p = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 #define TAB_SIZE 8
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 * Takes a string and converts any tabs to spaces.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 char *a_Misc_expand_tabs(const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 Dstr *New = dStr_new("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 int len, i, j, pos, old_pos;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 char *val;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 if ((len = strlen(str))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 for (pos = 0, i = 0; i < len; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 if (str[i] == '\t') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 /* Fill with whitespaces until the next tab. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 old_pos = pos;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 pos += TAB_SIZE - (pos % TAB_SIZE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 for (j = old_pos; j < pos; j++)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 dStr_append_c(New, ' ');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 dStr_append_c(New, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 pos++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 val = New->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 dStr_free(New, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 return val;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 /* TODO: could use dStr ADT! */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 typedef struct ContentType_ {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 const char *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 } ContentType_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 static const ContentType_t MimeTypes[] = {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 { "application/octet-stream", 24 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 { "text/html", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 { "text/plain", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 { "image/gif", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 { "image/png", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 { "image/jpeg", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 { NULL, 0 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 * Detects 'Content-Type' from a data stream sample.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 * It uses the magic(5) logic from file(1). Currently, it
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 * only checks the few mime types that Dillo supports.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 * 'Data' is a pointer to the first bytes of the raw data.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 * Return value: (0 on success, 1 on doubt, 2 on lack of data).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 int a_Misc_get_content_type_from_data(void *Data, size_t Size, const char **PT)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 int st = 1; /* default to "doubt' */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 int Type = 0; /* default to "application/octet-stream" */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 char *p = Data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 size_t i, non_ascci;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 /* HTML try */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 for (i = 0; i < Size && isspace(p[i]); ++i);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 if ((Size - i >= 5 && !dStrncasecmp(p+i, "<html", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 (Size - i >= 5 && !dStrncasecmp(p+i, "<head", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 (Size - i >= 6 && !dStrncasecmp(p+i, "<title", 6)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 (Size - i >= 14 && !dStrncasecmp(p+i, "<!doctype html", 14)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 /* this line is workaround for FTP through the Squid proxy */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 (Size - i >= 17 && !dStrncasecmp(p+i, "<!-- HTML listing", 17))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 Type = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 /* Images */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 } else if (Size >= 4 && !dStrncasecmp(p, "GIF8", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 Type = 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 } else if (Size >= 4 && !dStrncasecmp(p, "\x89PNG", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 Type = 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 } else if (Size >= 2 && !dStrncasecmp(p, "\xff\xd8", 2)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 /* JPEG has the first 2 bytes set to 0xffd8 in BigEndian - looking
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 * at the character representation should be machine independent. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 Type = 5;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 /* Text */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 /* We'll assume "text/plain" if the set of chars above 127 is <= 10
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 * in a 256-bytes sample. Better heuristics are welcomed! :-) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 non_ascci = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 Size = MIN (Size, 256);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 for (i = 0; i < Size; i++)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 if ((uchar_t) p[i] > 127)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 ++non_ascci;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 if (Size == 256) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 Type = (non_ascci > 10) ? 0 : 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 Type = (non_ascci > 0) ? 0 : 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 *PT = MimeTypes[Type].str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 * Check the server-supplied 'Content-Type' against our detected type.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 * (some servers seem to default to "text/plain").
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 * Return value:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 * 0, if they match
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 * -1, if a mismatch is detected
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 * There're many MIME types Dillo doesn't know, they're handled
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 * as "application/octet-stream" (as the SPEC says).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 * A mismatch happens when receiving a binary stream as
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 * "text/plain" or "text/html", or an image that's not an image of its kind.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 * Note: this is a basic security procedure.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 int a_Misc_content_type_check(const char *EntryType, const char *DetectedType)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 int st = -1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 _MSG("Type check: [Srv: %s Det: %s]\n", EntryType, DetectedType);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 if (!EntryType)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 return 0; /* there's no mismatch without server type */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 for (i = 1; MimeTypes[i].str; ++i)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 if (dStrncasecmp(EntryType, MimeTypes[i].str, MimeTypes[i].len) == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 if (!MimeTypes[i].str) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 /* type not found, no mismatch */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 } else if (dStrncasecmp(EntryType, "image/", 6) == 0 &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 !dStrncasecmp(DetectedType,MimeTypes[i].str,MimeTypes[i].len)){
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 /* An image, and there's an exact match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 } else if (dStrncasecmp(EntryType, "text/", 5) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 dStrncasecmp(DetectedType, "application/", 12)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 /* Not an application sent as text */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 * Parse a geometry string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 int a_Misc_parse_geometry(char *str, int *x, int *y, int *w, int *h)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 char *p, *t1, *t2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 int n1, n2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 int ret = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 if ((p = strchr(str, 'x')) || (p = strchr(str, 'X'))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 n1 = strtol(str, &t1, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216 n2 = strtol(++p, &t2, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 if (t1 != str && t2 != p) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 *w = n1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 *h = n2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220 ret = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 /* parse x,y now */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 p = t2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223 n1 = strtol(p, &t1, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 n2 = strtol(t1, &t2, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 if (t1 != p && t2 != t1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 *x = n1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 *y = n2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 _MSG("geom: w,h,x,y = (%d,%d,%d,%d)\n", *w, *h, *x, *y);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236 * Encodes string using base64 encoding.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 * Return value: new string or NULL if input string is empty.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239 char *a_Misc_encode_base64(const char *in)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 static const char *base64_hex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 "abcdefghijklmnopqrstuvwxyz"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243 "0123456789+/";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 char *out = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245 int len, i = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 if (in == NULL) return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 len = strlen(in);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 out = (char *)dMalloc((len + 2) / 3 * 4 + 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 for (; len >= 3; len -= 3) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 out[i++] = base64_hex[in[0] >> 2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 out[i++] = base64_hex[((in[0]<<4) & 0x30) | (in[1]>>4)];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255 out[i++] = base64_hex[((in[1]<<2) & 0x3c) | (in[2]>>6)];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 out[i++] = base64_hex[in[2] & 0x3f];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257 in += 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 if (len > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 unsigned char fragment;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262 out[i++] = base64_hex[in[0] >> 2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 fragment = (in[0] << 4) & 0x30;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 if (len > 1) fragment |= in[1] >> 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265 out[i++] = base64_hex[fragment];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266 out[i++] = (len < 2) ? '=' : base64_hex[(in[1] << 2) & 0x3c];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 out[i++] = '=';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269 out[i] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 return out;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271 }