2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Large (and important) sections of this file were adapted from
24 * ROX-Filer source code, Copyright (C) 2003, the ROX-Filer team,
25 * originally licensed under the GPL v2 or greater (as above).
40 #include <glib/gstdio.h>
42 #include "thumbnails.h"
43 #include "icons/icons.h"
46 #ifdef __CYGWIN_USE_BIG_TYPES__
47 #define ST_SIZE_FMT "%lld"
49 #define ST_SIZE_FMT "%ld"
52 /* FIXME -- on some systems this may need to me "lld", see ROX-Filer code */
53 #define ST_SIZE_FMT "%ld"
56 #undef MIN /* quit yer whining, gcc */
59 /* We need MAX macro and some system does not offer it */
60 #define MAX(a,b) (((a)>(b))?(a):(b))
63 #define HOME_DIR g_get_home_dir()
66 #define THUMB_DIR "\\THUMBNAILS\\" /* viking maps default viking\maps */
67 #define THUMB_SUB_DIR "normal\\"
69 #define THUMB_DIR "/.thumbnails/"
70 #define THUMB_SUB_DIR "normal/"
73 #define PIXMAP_THUMB_SIZE 128
75 static char *md5_hash(const char *message);
76 static GdkPixbuf *save_thumbnail(const char *pathname, GdkPixbuf *full);
77 static GdkPixbuf *child_create_thumbnail(const gchar *path);
79 gboolean a_thumbnails_exists ( const gchar *filename )
81 GdkPixbuf *pixbuf = a_thumbnails_get(filename);
84 g_object_unref ( G_OBJECT ( pixbuf ) );
90 GdkPixbuf *a_thumbnails_get_default ()
92 return gdk_pixbuf_from_pixdata ( &thumbnails_pixbuf, FALSE, NULL );
95 /* filename must be absolute. you could have a function to make sure it exists and absolutize it */
97 void a_thumbnails_create(const gchar *filename)
99 GdkPixbuf *pixbuf = a_thumbnails_get(filename);
102 pixbuf = child_create_thumbnail(filename);
105 g_object_unref ( G_OBJECT ( pixbuf ) );
108 GdkPixbuf *a_thumbnails_scale_pixbuf(GdkPixbuf *src, int max_w, int max_h)
112 w = gdk_pixbuf_get_width(src);
113 h = gdk_pixbuf_get_height(src);
115 if (w <= max_w && h <= max_h)
117 g_object_ref ( G_OBJECT ( src ) );
122 float scale_x = ((float) w) / max_w;
123 float scale_y = ((float) h) / max_h;
124 float scale = MAX(scale_x, scale_y);
125 int dest_w = w / scale;
126 int dest_h = h / scale;
128 return gdk_pixbuf_scale_simple(src,
131 GDK_INTERP_BILINEAR);
135 static GdkPixbuf *child_create_thumbnail(const gchar *path)
137 GdkPixbuf *image, *tmpbuf;
139 image = gdk_pixbuf_new_from_file(path, NULL);
143 tmpbuf = gdk_pixbuf_apply_embedded_orientation(image);
144 g_object_unref(G_OBJECT(image));
149 GdkPixbuf *thumb = save_thumbnail(path, image);
150 g_object_unref ( G_OBJECT ( image ) );
157 static GdkPixbuf *save_thumbnail(const char *pathname, GdkPixbuf *full)
161 int original_width, original_height;
162 const gchar* orientation;
164 char *md5, *swidth, *sheight, *ssize, *smtime, *uri;
169 if (stat(pathname, &info) != 0)
172 thumb = a_thumbnails_scale_pixbuf(full, PIXMAP_THUMB_SIZE, PIXMAP_THUMB_SIZE);
174 orientation = gdk_pixbuf_get_option (full, "orientation");
176 original_width = gdk_pixbuf_get_width(full);
177 original_height = gdk_pixbuf_get_height(full);
180 swidth = g_strdup_printf("%d", original_width);
181 sheight = g_strdup_printf("%d", original_height);
182 ssize = g_strdup_printf(ST_SIZE_FMT, info.st_size);
183 smtime = g_strdup_printf("%ld", (long) info.st_mtime);
185 path = file_realpath_dup(pathname);
186 uri = g_strconcat("file://", path, NULL);
190 to = g_string_new(HOME_DIR);
191 g_string_append(to, THUMB_DIR);
192 g_string_append(to, THUMB_SUB_DIR);
193 g_mkdir_with_parents(to->str, 0700);
194 g_string_append(to, md5);
195 name_len = to->len + 4; /* Truncate to this length when renaming */
197 g_string_append_printf(to, ".png.Viking");
199 g_string_append_printf(to, ".png.Viking-%ld", (long) getpid());
204 old_mask = umask(0077);
205 gdk_pixbuf_save(thumb, to->str, "png", NULL,
206 "tEXt::Thumb::Image::Width", swidth,
207 "tEXt::Thumb::Image::Height", sheight,
208 "tEXt::Thumb::Size", ssize,
209 "tEXt::Thumb::MTime", smtime,
210 "tEXt::Thumb::URI", uri,
211 "tEXt::Software", PROJECT,
212 "tEXt::Software::Orientation", orientation ? orientation : "0",
216 /* We create the file ###.png.ROX-Filer-PID and rename it to avoid
217 * a race condition if two programs create the same thumb at
223 final = g_strndup(to->str, name_len);
224 if (rename(to->str, final))
226 g_warning("Failed to rename '%s' to '%s': %s",
227 to->str, final, g_strerror(errno));
228 g_object_unref ( G_OBJECT(thumb) );
229 thumb = NULL; /* return NULL */
235 g_string_free(to, TRUE);
246 GdkPixbuf *a_thumbnails_get(const gchar *pathname)
248 GdkPixbuf *thumb = NULL;
249 char *thumb_path, *md5, *uri, *path;
250 const char *ssize, *smtime;
253 path = file_realpath_dup(pathname);
254 uri = g_strconcat("file://", path, NULL);
258 thumb_path = g_strdup_printf("%s%s%s%s.png", HOME_DIR, THUMB_DIR, THUMB_SUB_DIR, md5);
262 thumb = gdk_pixbuf_new_from_file(thumb_path, NULL);
266 /* Note that these don't need freeing... */
267 ssize = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::Size");
271 smtime = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::MTime");
275 if (stat(path, &info) != 0)
278 if (info.st_mtime != atol(smtime) || info.st_size != atol(ssize))
284 g_object_unref ( G_OBJECT ( thumb ) );
293 * This code implements the MD5 message-digest algorithm.
294 * The algorithm is due to Ron Rivest. The original code was
295 * written by Colin Plumb in 1993, and put in the public domain.
297 * Modified to use glib datatypes. Put under GPL to simplify
298 * licensing for ROX-Filer. Taken from Debian's dpkg package.
302 #define md5byte unsigned char
304 typedef struct _MD5Context MD5Context;
312 static void MD5Init(MD5Context *ctx);
313 static void MD5Update(MD5Context *ctx, md5byte const *buf, unsigned len);
314 static char *MD5Final(MD5Context *ctx);
315 static void MD5Transform(guint32 buf[4], guint32 const in[16]);
317 #if G_BYTE_ORDER == G_BIG_ENDIAN
318 static void byteSwap(guint32 *buf, unsigned words)
320 md5byte *p = (md5byte *)buf;
323 *buf++ = (guint32)((unsigned)p[3] << 8 | p[2]) << 16 |
324 ((unsigned)p[1] << 8 | p[0]);
329 #define byteSwap(buf,words)
333 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
334 * initialization constants.
336 static void MD5Init(MD5Context *ctx)
338 ctx->buf[0] = 0x67452301;
339 ctx->buf[1] = 0xefcdab89;
340 ctx->buf[2] = 0x98badcfe;
341 ctx->buf[3] = 0x10325476;
348 * Update context to reflect the concatenation of another buffer full
351 static void MD5Update(MD5Context *ctx, md5byte const *buf, unsigned len)
355 /* Update byte count */
358 if ((ctx->bytes[0] = t + len) < t)
359 ctx->bytes[1]++; /* Carry from low to high */
361 t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
363 memcpy((md5byte *)ctx->in + 64 - t, buf, len);
366 /* First chunk is an odd size */
367 memcpy((md5byte *)ctx->in + 64 - t, buf, t);
368 byteSwap(ctx->in, 16);
369 MD5Transform(ctx->buf, ctx->in);
373 /* Process data in 64-byte chunks */
375 memcpy(ctx->in, buf, 64);
376 byteSwap(ctx->in, 16);
377 MD5Transform(ctx->buf, ctx->in);
382 /* Handle any remaining bytes of data. */
383 memcpy(ctx->in, buf, len);
387 * Final wrapup - pad to 64-byte boundary with the bit pattern
388 * 1 0* (64-bit count of bits processed, MSB-first)
389 * Returns the newly allocated string of the hash.
391 static char *MD5Final(MD5Context *ctx)
395 int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
396 md5byte *p = (md5byte *)ctx->in + count;
399 /* Set the first char of padding to 0x80. There is always room. */
402 /* Bytes of padding needed to make 56 bytes (-8..55) */
403 count = 56 - 1 - count;
405 if (count < 0) { /* Padding forces an extra block */
406 memset(p, 0, count + 8);
407 byteSwap(ctx->in, 16);
408 MD5Transform(ctx->buf, ctx->in);
409 p = (md5byte *)ctx->in;
413 byteSwap(ctx->in, 14);
415 /* Append length in bits and transform */
416 ctx->in[14] = ctx->bytes[0] << 3;
417 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
418 MD5Transform(ctx->buf, ctx->in);
420 byteSwap(ctx->buf, 4);
422 retval = g_malloc(33);
423 bytes = (guint8 *) ctx->buf;
424 for (i = 0; i < 16; i++)
425 sprintf(retval + (i * 2), "%02x", bytes[i]);
433 /* The four core functions - F1 is optimized somewhat */
435 /* #define F1(x, y, z) (x & y | ~x & z) */
436 #define F1(x, y, z) (z ^ (x & (y ^ z)))
437 #define F2(x, y, z) F1(z, x, y)
438 #define F3(x, y, z) (x ^ y ^ z)
439 #define F4(x, y, z) (y ^ (x | ~z))
441 /* This is the central step in the MD5 algorithm. */
442 #define MD5STEP(f,w,x,y,z,in,s) \
443 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
446 * The core of the MD5 algorithm, this alters an existing MD5 hash to
447 * reflect the addition of 16 longwords of new data. MD5Update blocks
448 * the data and converts bytes into longwords for this routine.
450 static void MD5Transform(guint32 buf[4], guint32 const in[16])
452 register guint32 a, b, c, d;
459 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
460 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
461 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
462 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
463 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
464 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
465 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
466 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
467 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
468 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
469 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
470 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
471 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
472 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
473 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
474 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
476 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
477 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
478 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
479 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
480 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
481 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
482 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
483 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
484 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
485 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
486 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
487 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
488 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
489 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
490 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
491 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
493 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
494 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
495 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
496 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
497 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
498 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
499 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
500 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
501 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
502 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
503 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
504 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
505 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
506 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
507 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
508 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
510 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
511 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
512 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
513 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
514 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
515 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
516 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
517 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
518 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
519 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
520 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
521 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
522 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
523 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
524 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
525 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
533 # endif /* ASM_MD5 */
535 static char *md5_hash(const char *message)
540 MD5Update(&ctx, (md5byte *) message, strlen(message));
541 return MD5Final(&ctx);