1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/types.h>
39 #include <selinux/selinux.h>
44 #if defined HAVE_SYS_XATTR_H
45 #include <sys/xattr.h>
46 #elif defined HAVE_ATTR_XATTR_H
47 #include <attr/xattr.h>
49 #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
50 #endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
52 #endif /* HAVE_XATTR */
54 #include <glib/gstdio.h>
57 #include "glocalfileinfo.h"
59 #include "gthemedicon.h"
60 #include "gcontenttype.h"
61 #include "gcontenttypeprivate.h"
65 struct ThumbMD5Context {
76 G_LOCK_DEFINE_STATIC (uid_cache);
77 static GHashTable *uid_cache = NULL;
79 G_LOCK_DEFINE_STATIC (gid_cache);
80 static GHashTable *gid_cache = NULL;
82 static void thumb_md5 (const char *string, unsigned char digest[16]);
85 _g_local_file_info_create_etag (struct stat *statbuf)
89 tv.tv_sec = statbuf->st_mtime;
90 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
91 tv.tv_usec = statbuf->st_mtimensec / 1000;
92 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
93 tv.tv_usec = statbuf->st_mtim.tv_nsec / 1000;
98 return g_strdup_printf ("%lu:%lu", tv.tv_sec, tv.tv_usec);
102 _g_local_file_info_create_file_id (struct stat *statbuf)
104 return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
105 (guint64) statbuf->st_dev,
106 (guint64) statbuf->st_ino);
110 _g_local_file_info_create_fs_id (struct stat *statbuf)
112 return g_strdup_printf ("l%" G_GUINT64_FORMAT,
113 (guint64) statbuf->st_dev);
118 read_link (const gchar *full_name)
125 buffer = g_malloc (size);
131 read_size = readlink (full_name, buffer, size);
137 if (read_size < size)
139 buffer[read_size] = 0;
143 buffer = g_realloc (buffer, size);
150 /* Get the SELinux security context */
152 get_selinux_context (const char *path,
154 GFileAttributeMatcher *attribute_matcher,
155 gboolean follow_symlinks)
160 if (!g_file_attribute_matcher_matches (attribute_matcher, "selinux:context"))
163 if (is_selinux_enabled ())
167 if (lgetfilecon_raw (path, &context) < 0)
172 if (getfilecon_raw (path, &context) < 0)
178 g_file_info_set_attribute_string (info, "selinux:context", context);
190 return c >= 32 && c <= 126 && c != '\\';
194 name_is_valid (const char *str)
198 if (!valid_char (*str++))
205 hex_escape_string (const char *str,
206 gboolean *free_return)
209 char *escaped_str, *p;
211 static char *hex_digits = "0123456789abcdef";
217 for (i = 0; i < len; i++)
219 if (!valid_char (str[i]))
223 if (num_invalid == 0)
225 *free_return = FALSE;
229 escaped_str = g_malloc (len + num_invalid*3 + 1);
232 for (i = 0; i < len; i++)
234 if (valid_char (str[i]))
241 *p++ = hex_digits[(c >> 4) & 0xf];
242 *p++ = hex_digits[c & 0xf];
252 hex_unescape_string (const char *str,
254 gboolean *free_return)
257 char *unescaped_str, *p;
263 if (strchr (str, '\\') == NULL)
267 *free_return = FALSE;
271 unescaped_str = g_malloc (len + 1);
274 for (i = 0; i < len; i++)
276 if (str[i] == '\\' &&
281 (g_ascii_xdigit_value (str[i+2]) << 4) |
282 g_ascii_xdigit_value (str[i+3]);
292 *out_len = p - unescaped_str;
294 return unescaped_str;
298 escape_xattr (GFileInfo *info,
299 const char *gio_attr, /* gio attribute name */
300 const char *value, /* Is zero terminated */
301 size_t len /* not including zero termination */)
304 gboolean free_escaped_val;
306 escaped_val = hex_escape_string (value, &free_escaped_val);
308 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
310 if (free_escaped_val)
311 g_free (escaped_val);
315 get_one_xattr (const char *path,
317 const char *gio_attr,
319 gboolean follow_symlinks)
326 len = getxattr (path, xattr, value, sizeof (value)-1);
328 len = lgetxattr (path, xattr,value, sizeof (value)-1);
333 else if (len == -1 && errno == ERANGE)
336 len = getxattr (path, xattr, NULL, 0);
338 len = lgetxattr (path, xattr, NULL, 0);
343 value_p = g_malloc (len+1);
346 len = getxattr (path, xattr, value_p, len);
348 len = lgetxattr (path, xattr, value_p, len);
362 escape_xattr (info, gio_attr, value_p, len);
364 if (value_p != value)
368 #endif /* defined HAVE_XATTR */
371 get_xattrs (const char *path,
374 GFileAttributeMatcher *matcher,
375 gboolean follow_symlinks)
380 ssize_t list_res_size;
383 const char *attr, *attr2;
386 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
388 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
393 list_res_size = listxattr (path, NULL, 0);
395 list_res_size = llistxattr (path, NULL, 0);
397 if (list_res_size == -1 ||
401 list_size = list_res_size;
402 list = g_malloc (list_size);
407 list_res_size = listxattr (path, list, list_size);
409 list_res_size = llistxattr (path, list, list_size);
411 if (list_res_size == -1 && errno == ERANGE)
413 list_size = list_size * 2;
414 list = g_realloc (list, list_size);
418 if (list_res_size == -1)
422 while (list_res_size > 0)
424 if ((user && g_str_has_prefix (attr, "user.")) ||
425 (!user && !g_str_has_prefix (attr, "user.")))
427 char *escaped_attr, *gio_attr;
428 gboolean free_escaped_attr;
432 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
433 gio_attr = g_strconcat ("xattr:", escaped_attr, NULL);
437 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
438 gio_attr = g_strconcat ("xattr_sys:", escaped_attr, NULL);
441 if (free_escaped_attr)
442 g_free (escaped_attr);
444 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
447 len = strlen (attr) + 1;
449 list_res_size -= len;
456 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
458 char *unescaped_attribute, *a;
459 gboolean free_unescaped_attribute;
461 attr2 = strchr (attr, ':');
464 attr2++; /* Skip ':' */
465 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
467 a = g_strconcat ("user.", unescaped_attribute, NULL);
469 a = unescaped_attribute;
471 get_one_xattr (path, info, attr, a, follow_symlinks);
476 if (free_unescaped_attribute)
477 g_free (unescaped_attribute);
481 #endif /* defined HAVE_XATTR */
486 get_one_xattr_from_fd (int fd,
488 const char *gio_attr,
495 len = fgetxattr (fd, xattr, value, sizeof (value)-1);
500 else if (len == -1 && errno == ERANGE)
502 len = fgetxattr (fd, xattr, NULL, 0);
507 value_p = g_malloc (len+1);
509 len = fgetxattr (fd, xattr, value_p, len);
523 escape_xattr (info, gio_attr, value_p, len);
525 if (value_p != value)
528 #endif /* defined HAVE_XATTR */
531 get_xattrs_from_fd (int fd,
534 GFileAttributeMatcher *matcher)
539 ssize_t list_res_size;
542 const char *attr, *attr2;
545 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
547 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
551 list_res_size = flistxattr (fd, NULL, 0);
553 if (list_res_size == -1 ||
557 list_size = list_res_size;
558 list = g_malloc (list_size);
562 list_res_size = flistxattr (fd, list, list_size);
564 if (list_res_size == -1 && errno == ERANGE)
566 list_size = list_size * 2;
567 list = g_realloc (list, list_size);
571 if (list_res_size == -1)
575 while (list_res_size > 0)
577 if ((user && g_str_has_prefix (attr, "user.")) ||
578 (!user && !g_str_has_prefix (attr, "user.")))
580 char *escaped_attr, *gio_attr;
581 gboolean free_escaped_attr;
585 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
586 gio_attr = g_strconcat ("xattr:", escaped_attr, NULL);
590 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
591 gio_attr = g_strconcat ("xattr_sys:", escaped_attr, NULL);
594 if (free_escaped_attr)
595 g_free (escaped_attr);
597 get_one_xattr_from_fd (fd, info, gio_attr, attr);
600 len = strlen (attr) + 1;
602 list_res_size -= len;
609 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
611 char *unescaped_attribute, *a;
612 gboolean free_unescaped_attribute;
614 attr2 = strchr (attr, ':');
617 attr2++; /* Skip ':' */
618 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
620 a = g_strconcat ("user.", unescaped_attribute, NULL);
622 a = unescaped_attribute;
624 get_one_xattr_from_fd (fd, info, attr, a);
629 if (free_unescaped_attribute)
630 g_free (unescaped_attribute);
634 #endif /* defined HAVE_XATTR */
639 set_xattr (char *filename,
640 const char *escaped_attribute,
641 const GFileAttributeValue *attr_value,
644 char *attribute, *value;
645 gboolean free_attribute, free_value;
646 int val_len, res, errsv;
650 if (attr_value == NULL)
652 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
653 _("Attribute value must be non-NULL"));
657 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
659 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
660 _("Invalid attribute type (string expected)"));
664 if (!name_is_valid (escaped_attribute))
666 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
667 _("Invalid extended attribute name"));
671 if (g_str_has_prefix (escaped_attribute, "xattr:"))
673 escaped_attribute += 6;
678 g_assert (g_str_has_prefix (escaped_attribute, "xattr_sys:"));
679 escaped_attribute += 10;
683 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
684 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
687 a = g_strconcat ("user.", attribute, NULL);
691 res = setxattr (filename, a, value, val_len, 0);
705 g_set_error (error, G_IO_ERROR,
706 g_io_error_from_errno (errsv),
707 _("Error setting extended attribute '%s': %s"),
708 escaped_attribute, g_strerror (errno));
719 _g_local_file_info_get_parent_info (const char *dir,
720 GFileAttributeMatcher *attribute_matcher,
721 GLocalParentFileInfo *parent_info)
726 parent_info->writable = FALSE;
727 parent_info->is_sticky = FALSE;
728 parent_info->device = 0;
730 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
731 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
732 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
733 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
735 parent_info->writable = (g_access (dir, W_OK) == 0);
737 res = g_stat (dir, &statbuf);
740 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
741 * renamed or deleted only by the owner of the file, by the owner of the directory, and
742 * by a privileged process.
746 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
747 parent_info->owner = statbuf.st_uid;
748 parent_info->device = statbuf.st_dev;
754 get_access_rights (GFileAttributeMatcher *attribute_matcher,
757 struct stat *statbuf,
758 GLocalParentFileInfo *parent_info)
760 if (g_file_attribute_matcher_matches (attribute_matcher,
761 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
762 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
763 g_access (path, R_OK) == 0);
765 if (g_file_attribute_matcher_matches (attribute_matcher,
766 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
767 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
768 g_access (path, W_OK) == 0);
770 if (g_file_attribute_matcher_matches (attribute_matcher,
771 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
772 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
773 g_access (path, X_OK) == 0);
781 if (parent_info->writable)
783 if (parent_info->is_sticky)
785 uid_t uid = geteuid ();
787 if (uid == statbuf->st_uid ||
788 uid == parent_info->owner ||
796 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
797 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
800 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
801 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
804 /* TODO: This means we can move it, but we should also look for a trash dir */
805 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
806 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
812 set_info_from_stat (GFileInfo *info,
813 struct stat *statbuf,
814 GFileAttributeMatcher *attribute_matcher)
818 file_type = G_FILE_TYPE_UNKNOWN;
820 if (S_ISREG (statbuf->st_mode))
821 file_type = G_FILE_TYPE_REGULAR;
822 else if (S_ISDIR (statbuf->st_mode))
823 file_type = G_FILE_TYPE_DIRECTORY;
824 else if (S_ISCHR (statbuf->st_mode) ||
825 S_ISBLK (statbuf->st_mode) ||
826 S_ISFIFO (statbuf->st_mode)
828 || S_ISSOCK (statbuf->st_mode)
831 file_type = G_FILE_TYPE_SPECIAL;
833 else if (S_ISLNK (statbuf->st_mode))
834 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
837 g_file_info_set_file_type (info, file_type);
838 g_file_info_set_size (info, statbuf->st_size);
840 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
841 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
842 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
843 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
844 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
845 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
846 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
847 #if defined (HAVE_STRUCT_STAT_BLKSIZE)
848 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
850 #if defined (HAVE_STRUCT_STAT_BLOCKS)
851 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
854 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
855 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
856 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
857 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
858 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
861 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
862 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
863 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
864 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
865 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
868 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
869 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
870 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
871 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
872 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
875 if (g_file_attribute_matcher_matches (attribute_matcher,
876 G_FILE_ATTRIBUTE_ETAG_VALUE))
878 char *etag = _g_local_file_info_create_etag (statbuf);
879 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
883 if (g_file_attribute_matcher_matches (attribute_matcher,
884 G_FILE_ATTRIBUTE_ID_FILE))
886 char *id = _g_local_file_info_create_file_id (statbuf);
887 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
891 if (g_file_attribute_matcher_matches (attribute_matcher,
892 G_FILE_ATTRIBUTE_ID_FS))
894 char *id = _g_local_file_info_create_fs_id (statbuf);
895 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FS, id);
901 make_valid_utf8 (const char *name)
904 const gchar *remainder, *invalid;
905 gint remaining_bytes, valid_bytes;
909 remaining_bytes = strlen (name);
911 while (remaining_bytes != 0)
913 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
915 valid_bytes = invalid - remainder;
918 string = g_string_sized_new (remaining_bytes);
920 g_string_append_len (string, remainder, valid_bytes);
921 /* append U+FFFD REPLACEMENT CHARACTER */
922 g_string_append (string, "\357\277\275");
924 remaining_bytes -= valid_bytes + 1;
925 remainder = invalid + 1;
929 return g_strdup (name);
931 g_string_append (string, remainder);
933 g_assert (g_utf8_validate (string->str, -1, NULL));
935 return g_string_free (string, FALSE);
939 convert_pwd_string_to_utf8 (char *pwd_str)
943 if (!g_utf8_validate (pwd_str, -1, NULL))
945 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
946 if (utf8_string == NULL)
947 utf8_string = make_valid_utf8 (pwd_str);
950 utf8_string = g_strdup (pwd_str);
956 uid_data_free (UidData *data)
958 g_free (data->user_name);
959 g_free (data->real_name);
963 /* called with lock held */
965 lookup_uid_data (uid_t uid)
970 struct passwd *pwbufp;
973 if (uid_cache == NULL)
974 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
976 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
981 data = g_new0 (UidData, 1);
983 #if defined(HAVE_POSIX_GETPWUID_R)
984 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
985 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
986 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
988 pwbufp = getpwuid (uid);
993 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
994 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
996 gecos = pwbufp->pw_gecos;
1000 comma = strchr (gecos, ',');
1003 data->real_name = convert_pwd_string_to_utf8 (gecos);
1007 /* Default fallbacks */
1008 if (data->real_name == NULL)
1010 if (data->user_name != NULL)
1011 data->real_name = g_strdup (data->user_name);
1013 data->real_name = g_strdup_printf("user #%d", (int)uid);
1016 if (data->user_name == NULL)
1017 data->user_name = g_strdup_printf("%d", (int)uid);
1019 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1025 get_username_from_uid (uid_t uid)
1031 data = lookup_uid_data (uid);
1032 res = g_strdup (data->user_name);
1033 G_UNLOCK (uid_cache);
1039 get_realname_from_uid (uid_t uid)
1045 data = lookup_uid_data (uid);
1046 res = g_strdup (data->real_name);
1047 G_UNLOCK (uid_cache);
1053 /* called with lock held */
1055 lookup_gid_name (gid_t gid)
1060 struct group *gbufp;
1062 if (gid_cache == NULL)
1063 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1065 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1070 #if defined (HAVE_POSIX_GETGRGID_R)
1071 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1072 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1073 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1075 gbufp = getgrgid (gid);
1078 if (gbufp != NULL &&
1079 gbufp->gr_name != NULL &&
1080 gbufp->gr_name[0] != 0)
1081 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1083 name = g_strdup_printf("%d", (int)gid);
1085 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1091 get_groupname_from_gid (gid_t gid)
1097 name = lookup_gid_name (gid);
1098 res = g_strdup (name);
1099 G_UNLOCK (gid_cache);
1104 get_content_type (const char *basename,
1106 struct stat *statbuf,
1107 gboolean is_symlink,
1108 gboolean symlink_broken,
1109 GFileQueryInfoFlags flags,
1113 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1114 return g_strdup ("inode/symlink");
1115 else if (S_ISDIR(statbuf->st_mode))
1116 return g_strdup ("inode/directory");
1117 else if (S_ISCHR(statbuf->st_mode))
1118 return g_strdup ("inode/chardevice");
1119 else if (S_ISBLK(statbuf->st_mode))
1120 return g_strdup ("inode/blockdevice");
1121 else if (S_ISFIFO(statbuf->st_mode))
1122 return g_strdup ("inode/fifo");
1124 else if (S_ISSOCK(statbuf->st_mode))
1125 return g_strdup ("inode/socket");
1130 gboolean result_uncertain;
1132 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1135 if (!fast && result_uncertain && path != NULL)
1137 guchar sniff_buffer[4096];
1141 sniff_length = _g_unix_content_type_get_sniff_len ();
1142 if (sniff_length > 4096)
1143 sniff_length = 4096;
1145 fd = open (path, O_RDONLY);
1150 res = read (fd, sniff_buffer, sniff_length);
1154 g_free (content_type);
1155 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1161 return content_type;
1167 thumb_digest_to_ascii (unsigned char digest[16],
1170 static const char hex_digits[] = "0123456789abcdef";
1174 res = g_malloc (33 + strlen (suffix));
1176 for (i = 0; i < 16; i++)
1178 res[2*i] = hex_digits[digest[i] >> 4];
1179 res[2*i+1] = hex_digits[digest[i] & 0xf];
1184 strcat (res, suffix);
1191 get_thumbnail_attributes (const char *path,
1195 unsigned char digest[16];
1199 uri = g_filename_to_uri (path, NULL, NULL);
1200 thumb_md5 (uri, digest);
1203 basename = thumb_digest_to_ascii (digest, ".png");
1205 filename = g_build_filename (g_get_home_dir(), ".thumbnails", "normal", basename, NULL);
1207 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1208 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1212 filename = g_build_filename (g_get_home_dir(), ".thumbnails", "fail", "gnome-thumbnail-factory", basename, NULL);
1214 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1215 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1223 _g_local_file_info_get (const char *basename,
1225 GFileAttributeMatcher *attribute_matcher,
1226 GFileQueryInfoFlags flags,
1227 GLocalParentFileInfo *parent_info,
1231 struct stat statbuf;
1232 struct stat statbuf2;
1234 gboolean is_symlink, symlink_broken;
1236 info = g_file_info_new ();
1238 /* Make sure we don't set any unwanted attributes */
1239 g_file_info_set_attribute_mask (info, attribute_matcher);
1241 g_file_info_set_name (info, basename);
1243 /* Avoid stat in trivial case */
1244 if (attribute_matcher == NULL)
1247 res = g_lstat (path, &statbuf);
1250 g_object_unref (info);
1251 g_set_error (error, G_IO_ERROR,
1252 g_io_error_from_errno (errno),
1253 _("Error stating file '%s': %s"),
1254 path, g_strerror (errno));
1259 is_symlink = S_ISLNK (statbuf.st_mode);
1263 symlink_broken = FALSE;
1267 g_file_info_set_is_symlink (info, TRUE);
1269 /* Unless NOFOLLOW was set we default to following symlinks */
1270 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1272 res = stat (path, &statbuf2);
1274 /* Report broken links as symlinks */
1278 symlink_broken = TRUE;
1282 set_info_from_stat (info, &statbuf, attribute_matcher);
1284 if (basename != NULL && basename[0] == '.')
1285 g_file_info_set_is_hidden (info, TRUE);
1287 if (basename != NULL && basename[strlen (basename) -1] == '~')
1288 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STD_IS_BACKUP, TRUE);
1291 g_file_attribute_matcher_matches (attribute_matcher,
1292 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET))
1294 char *link = read_link (path);
1295 g_file_info_set_symlink_target (info, link);
1299 if (g_file_attribute_matcher_matches (attribute_matcher,
1300 G_FILE_ATTRIBUTE_STD_DISPLAY_NAME))
1302 char *display_name = g_filename_display_basename (path);
1304 if (strstr (display_name, "\357\277\275") != NULL)
1306 char *p = display_name;
1307 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1310 g_file_info_set_display_name (info, display_name);
1311 g_free (display_name);
1314 if (g_file_attribute_matcher_matches (attribute_matcher,
1315 G_FILE_ATTRIBUTE_STD_EDIT_NAME))
1317 char *edit_name = g_filename_display_basename (path);
1318 g_file_info_set_edit_name (info, edit_name);
1323 if (g_file_attribute_matcher_matches (attribute_matcher,
1324 G_FILE_ATTRIBUTE_STD_COPY_NAME))
1326 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1328 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_COPY_NAME, copy_name);
1332 if (g_file_attribute_matcher_matches (attribute_matcher,
1333 G_FILE_ATTRIBUTE_STD_CONTENT_TYPE) ||
1334 g_file_attribute_matcher_matches (attribute_matcher,
1335 G_FILE_ATTRIBUTE_STD_ICON))
1337 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1341 g_file_info_set_content_type (info, content_type);
1343 if (g_file_attribute_matcher_matches (attribute_matcher,
1344 G_FILE_ATTRIBUTE_STD_ICON))
1346 char *mimetype_icon, *generic_mimetype_icon, *type_icon, *p;
1347 char *icon_names[3];
1351 mimetype_icon = g_strdup (content_type);
1353 while ((p = strchr(mimetype_icon, '/')) != NULL)
1356 p = strchr (content_type, '/');
1358 p = content_type + strlen (content_type);
1360 generic_mimetype_icon = g_malloc (p - content_type + strlen ("-x-generic") + 1);
1361 memcpy (generic_mimetype_icon, content_type, p - content_type);
1362 memcpy (generic_mimetype_icon + (p - content_type), "-x-generic", strlen ("-x-generic"));
1363 generic_mimetype_icon[(p - content_type) + strlen ("-x-generic")] = 0;
1365 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1366 if (strcmp (path, g_get_home_dir ()) == 0)
1367 type_icon = "user-home";
1368 else if (S_ISDIR (statbuf.st_mode))
1369 type_icon = "folder";
1370 else if (statbuf.st_mode & S_IXUSR)
1371 type_icon = "application-x-executable";
1373 type_icon = "text-x-generic";
1376 icon_names[i++] = mimetype_icon;
1377 icon_names[i++] = generic_mimetype_icon;
1378 if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
1379 strcmp (mimetype_icon, type_icon) != 0)
1380 icon_names[i++] = type_icon;
1382 icon = g_themed_icon_new_from_names (icon_names, i);
1383 g_file_info_set_icon (info, icon);
1385 g_object_unref (icon);
1386 g_free (mimetype_icon);
1387 g_free (generic_mimetype_icon);
1390 g_free (content_type);
1394 if (g_file_attribute_matcher_matches (attribute_matcher,
1395 G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE))
1397 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1401 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE, content_type);
1402 g_free (content_type);
1406 if (g_file_attribute_matcher_matches (attribute_matcher,
1407 G_FILE_ATTRIBUTE_OWNER_USER))
1411 name = get_username_from_uid (statbuf.st_uid);
1413 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1417 if (g_file_attribute_matcher_matches (attribute_matcher,
1418 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1422 name = get_realname_from_uid (statbuf.st_uid);
1424 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1428 if (g_file_attribute_matcher_matches (attribute_matcher,
1429 G_FILE_ATTRIBUTE_OWNER_GROUP))
1433 name = get_groupname_from_gid (statbuf.st_gid);
1435 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1439 if (parent_info && parent_info->device != 0 &&
1440 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1441 statbuf.st_dev != parent_info->device)
1442 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1444 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1446 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1447 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1448 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1450 if (g_file_attribute_matcher_matches (attribute_matcher,
1451 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1452 get_thumbnail_attributes (path, info);
1454 g_file_info_unset_attribute_mask (info);
1460 _g_local_file_info_get_from_fd (int fd,
1464 struct stat stat_buf;
1465 GFileAttributeMatcher *matcher;
1468 if (fstat (fd, &stat_buf) == -1)
1470 g_set_error (error, G_IO_ERROR,
1471 g_io_error_from_errno (errno),
1472 _("Error stating file descriptor: %s"),
1473 g_strerror (errno));
1477 info = g_file_info_new ();
1479 matcher = g_file_attribute_matcher_new (attributes);
1481 /* Make sure we don't set any unwanted attributes */
1482 g_file_info_set_attribute_mask (info, matcher);
1484 set_info_from_stat (info, &stat_buf, matcher);
1487 if (g_file_attribute_matcher_matches (matcher, "selinux:context") &&
1488 is_selinux_enabled ())
1491 if (fgetfilecon_raw (fd, &context) >= 0)
1493 g_file_info_set_attribute_string (info, "selinux:context", context);
1499 get_xattrs_from_fd (fd, TRUE, info, matcher);
1500 get_xattrs_from_fd (fd, FALSE, info, matcher);
1502 g_file_attribute_matcher_unref (matcher);
1504 g_file_info_unset_attribute_mask (info);
1510 get_uint32 (const GFileAttributeValue *value,
1514 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1516 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1517 _("Invalid attribute type (uint32 expected)"));
1521 *val_out = value->u.uint32;
1527 get_uint64 (const GFileAttributeValue *value,
1531 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1533 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1534 _("Invalid attribute type (uint64 expected)"));
1538 *val_out = value->u.uint64;
1543 #if defined(HAVE_SYMLINK)
1545 get_byte_string (const GFileAttributeValue *value,
1546 const char **val_out,
1549 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1551 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1552 _("Invalid attribute type (byte string expected)"));
1556 *val_out = value->u.string;
1563 set_unix_mode (char *filename,
1564 const GFileAttributeValue *value,
1569 if (!get_uint32 (value, &val, error))
1572 if (g_chmod (filename, val) == -1)
1574 g_set_error (error, G_IO_ERROR,
1575 g_io_error_from_errno (errno),
1576 _("Error setting permissions: %s"),
1577 g_strerror (errno));
1585 set_unix_uid_gid (char *filename,
1586 const GFileAttributeValue *uid_value,
1587 const GFileAttributeValue *gid_value,
1588 GFileQueryInfoFlags flags,
1598 if (!get_uint32 (uid_value, &val, error))
1607 if (!get_uint32 (gid_value, &val, error))
1614 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1615 res = lchown (filename, uid, gid);
1617 res = chown (filename, uid, gid);
1621 g_set_error (error, G_IO_ERROR,
1622 g_io_error_from_errno (errno),
1623 _("Error setting owner: %s"),
1624 g_strerror (errno));
1633 set_symlink (char *filename,
1634 const GFileAttributeValue *value,
1638 struct stat statbuf;
1640 if (!get_byte_string (value, &val, error))
1645 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1646 _("symlink must be non-NULL"));
1650 if (g_lstat (filename, &statbuf))
1652 g_set_error (error, G_IO_ERROR,
1653 g_io_error_from_errno (errno),
1654 _("Error setting symlink: %s"),
1655 g_strerror (errno));
1659 if (!S_ISLNK (statbuf.st_mode))
1661 g_set_error (error, G_IO_ERROR,
1662 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1663 _("Error setting symlink: file is not a symlink"));
1667 if (g_unlink (filename))
1669 g_set_error (error, G_IO_ERROR,
1670 g_io_error_from_errno (errno),
1671 _("Error setting symlink: %s"),
1672 g_strerror (errno));
1676 if (symlink (filename, val) != 0)
1678 g_set_error (error, G_IO_ERROR,
1679 g_io_error_from_errno (errno),
1680 _("Error setting symlink: %s"),
1681 g_strerror (errno));
1690 lazy_stat (char *filename,
1691 struct stat *statbuf,
1692 gboolean *called_stat)
1699 res = g_stat (filename, statbuf);
1702 *called_stat = TRUE;
1710 set_mtime_atime (char *filename,
1711 const GFileAttributeValue *mtime_value,
1712 const GFileAttributeValue *mtime_usec_value,
1713 const GFileAttributeValue *atime_value,
1714 const GFileAttributeValue *atime_usec_value,
1720 struct stat statbuf;
1721 gboolean got_stat = FALSE;
1722 struct timeval times[2] = { {0, 0}, {0, 0} };
1727 if (!get_uint64 (atime_value, &val, error))
1729 times[0].tv_sec = val;
1733 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1735 times[0].tv_sec = statbuf.st_mtime;
1736 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1737 times[0].tv_usec = statbuf.st_atimensec / 1000;
1738 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1739 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1744 if (atime_usec_value)
1746 if (!get_uint32 (atime_usec_value, &val_usec, error))
1748 times[0].tv_usec = val_usec;
1754 if (!get_uint64 (mtime_value, &val, error))
1756 times[1].tv_sec = val;
1760 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1762 times[1].tv_sec = statbuf.st_mtime;
1763 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1764 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1765 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1766 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1771 if (mtime_usec_value)
1773 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1775 times[1].tv_usec = val_usec;
1778 res = utimes(filename, times);
1781 g_set_error (error, G_IO_ERROR,
1782 g_io_error_from_errno (errno),
1783 _("Error setting owner: %s"),
1784 g_strerror (errno));
1792 _g_local_file_info_set_attribute (char *filename,
1793 const char *attribute,
1794 const GFileAttributeValue *value,
1795 GFileQueryInfoFlags flags,
1796 GCancellable *cancellable,
1799 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1800 return set_unix_mode (filename, value, error);
1803 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1804 return set_unix_uid_gid (filename, value, NULL, flags, error);
1805 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1806 return set_unix_uid_gid (filename, NULL, value, flags, error);
1810 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET) == 0)
1811 return set_symlink (filename, value, error);
1815 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1816 return set_mtime_atime (filename, value, NULL, NULL, NULL, error);
1817 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1818 return set_mtime_atime (filename, NULL, value, NULL, NULL, error);
1819 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1820 return set_mtime_atime (filename, NULL, NULL, value, NULL, error);
1821 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1822 return set_mtime_atime (filename, NULL, NULL, NULL, value, error);
1826 else if (g_str_has_prefix (attribute, "xattr:"))
1827 return set_xattr (filename, attribute, value, error);
1828 else if (g_str_has_prefix (attribute, "xattr_sys:"))
1829 return set_xattr (filename, attribute, value, error);
1832 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1833 _("Setting attribute %s not supported"), attribute);
1838 _g_local_file_info_set_attributes (char *filename,
1840 GFileQueryInfoFlags flags,
1841 GCancellable *cancellable,
1844 GFileAttributeValue *value, *uid, *gid;
1845 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
1846 GFileAttributeStatus status;
1849 /* Handles setting multiple specified data in a single set, and takes care
1850 of ordering restrictions when setting attributes */
1854 /* Set symlink first, since this recreates the file */
1856 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1859 if (!set_symlink (filename, value, error))
1861 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1863 /* Don't set error multiple times */
1867 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1873 /* Group uid and gid setting into one call
1874 * Change ownership before permissions, since ownership changes can
1875 change permissions (e.g. setuid)
1877 uid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_UID);
1878 gid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_GID);
1882 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
1884 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1886 /* Don't set error multiple times */
1890 status = G_FILE_ATTRIBUTE_STATUS_SET;
1892 uid->status = status;
1894 gid->status = status;
1898 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE);
1901 if (!set_unix_mode (filename, value, error))
1903 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1905 /* Don't set error multiple times */
1909 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1914 /* Group all time settings into one call
1915 * Change times as the last thing to avoid it changing due to metadata changes
1918 mtime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
1919 mtime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1920 atime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
1921 atime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
1923 if (mtime || mtime_usec || atime || atime_usec)
1925 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
1927 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1929 /* Don't set error multiple times */
1933 status = G_FILE_ATTRIBUTE_STATUS_SET;
1936 mtime->status = status;
1938 mtime_usec->status = status;
1940 atime->status = status;
1942 atime_usec->status = status;
1946 /* xattrs are handled by default callback */
1953 * This code implements the MD5 message-digest algorithm.
1954 * The algorithm is due to Ron Rivest. This code was
1955 * written by Colin Plumb in 1993, no copyright is claimed.
1956 * This code is in the public domain; do with it what you wish.
1958 * Equivalent code is available from RSA Data Security, Inc.
1959 * This code has been tested against that, and is equivalent,
1960 * except that you don't need to include two pages of legalese
1963 * To compute the message digest of a chunk of bytes, declare an
1964 * ThumbMD5Context structure, pass it to thumb_md5_init, call
1965 * thumb_md5_update as needed on buffers full of bytes, and then call
1966 * thumb_md5_final, which will fill a supplied 32-byte array with the
1967 * digest in ascii form.
1971 static void thumb_md5_init (struct ThumbMD5Context *context);
1972 static void thumb_md5_update (struct ThumbMD5Context *context,
1973 unsigned char const *buf,
1975 static void thumb_md5_final (unsigned char digest[16],
1976 struct ThumbMD5Context *context);
1977 static void thumb_md5_transform (guint32 buf[4],
1978 guint32 const in[16]);
1982 thumb_md5 (const char *string, unsigned char digest[16])
1984 struct ThumbMD5Context md5_context;
1986 thumb_md5_init (&md5_context);
1987 thumb_md5_update (&md5_context, (unsigned char *)string, strlen (string));
1988 thumb_md5_final (digest, &md5_context);
1991 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1992 #define byteReverse(buf, len) /* Nothing */
1996 * Note: this code is harmless on little-endian machines.
1999 byteReverse (unsigned char *buf,
2004 t = (guint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
2005 ((unsigned) buf[1] << 8 | buf[0]);
2006 *(guint32 *) buf = t;
2014 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
2015 * initialization constants.
2018 thumb_md5_init (struct ThumbMD5Context *ctx)
2020 ctx->buf[0] = 0x67452301;
2021 ctx->buf[1] = 0xefcdab89;
2022 ctx->buf[2] = 0x98badcfe;
2023 ctx->buf[3] = 0x10325476;
2030 * Update context to reflect the concatenation of another buffer full
2034 thumb_md5_update (struct ThumbMD5Context *ctx,
2035 unsigned char const *buf,
2040 /* Update bitcount */
2043 if ((ctx->bits[0] = t + ((guint32) len << 3)) < t)
2044 ctx->bits[1]++; /* Carry from low to high */
2045 ctx->bits[1] += len >> 29;
2047 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
2049 /* Handle any leading odd-sized chunks */
2052 unsigned char *p = (unsigned char *) ctx->in + t;
2056 memcpy (p, buf, len);
2060 byteReverse (ctx->in, 16);
2061 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2066 /* Process data in 64-byte chunks */
2069 memcpy (ctx->in, buf, 64);
2070 byteReverse (ctx->in, 16);
2071 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2076 /* Handle any remaining bytes of data. */
2078 memcpy(ctx->in, buf, len);
2082 * Final wrapup - pad to 64-byte boundary with the bit pattern
2083 * 1 0* (64-bit count of bits processed, MSB-first)
2086 thumb_md5_final (unsigned char digest[16],
2087 struct ThumbMD5Context *ctx)
2092 /* Compute number of bytes mod 64 */
2093 count = (ctx->bits[0] >> 3) & 0x3F;
2095 /* Set the first char of padding to 0x80. This is safe since there is
2096 always at least one byte free */
2097 p = ctx->in + count;
2100 /* Bytes of padding needed to make 64 bytes */
2101 count = 64 - 1 - count;
2103 /* Pad out to 56 mod 64 */
2105 /* Two lots of padding: Pad the first block to 64 bytes */
2106 memset (p, 0, count);
2107 byteReverse (ctx->in, 16);
2108 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2110 /* Now fill the next block with 56 bytes */
2111 memset(ctx->in, 0, 56);
2113 /* Pad block to 56 bytes */
2114 memset(p, 0, count - 8);
2116 byteReverse(ctx->in, 14);
2118 /* Append length in bits and transform */
2119 ((guint32 *) ctx->in)[14] = ctx->bits[0];
2120 ((guint32 *) ctx->in)[15] = ctx->bits[1];
2122 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2123 byteReverse ((unsigned char *) ctx->buf, 4);
2124 memcpy (digest, ctx->buf, 16);
2125 memset (ctx, 0, sizeof(ctx)); /* In case it's sensitive */
2129 /* The four core functions - F1 is optimized somewhat */
2131 #define F1(x, y, z) (z ^ (x & (y ^ z)))
2132 #define F2(x, y, z) F1 (z, x, y)
2133 #define F3(x, y, z) (x ^ y ^ z)
2134 #define F4(x, y, z) (y ^ (x | ~z))
2136 /* This is the central step in the MD5 algorithm. */
2137 #define thumb_md5_step(f, w, x, y, z, data, s) \
2138 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
2141 * The core of the MD5 algorithm, this alters an existing MD5 hash to
2142 * reflect the addition of 16 longwords of new data. ThumbMD5Update blocks
2143 * the data and converts bytes into longwords for this routine.
2146 thumb_md5_transform (guint32 buf[4],
2147 guint32 const in[16])
2149 register guint32 a, b, c, d;
2156 thumb_md5_step(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
2157 thumb_md5_step(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
2158 thumb_md5_step(F1, c, d, a, b, in[2] + 0x242070db, 17);
2159 thumb_md5_step(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
2160 thumb_md5_step(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
2161 thumb_md5_step(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
2162 thumb_md5_step(F1, c, d, a, b, in[6] + 0xa8304613, 17);
2163 thumb_md5_step(F1, b, c, d, a, in[7] + 0xfd469501, 22);
2164 thumb_md5_step(F1, a, b, c, d, in[8] + 0x698098d8, 7);
2165 thumb_md5_step(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
2166 thumb_md5_step(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
2167 thumb_md5_step(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
2168 thumb_md5_step(F1, a, b, c, d, in[12] + 0x6b901122, 7);
2169 thumb_md5_step(F1, d, a, b, c, in[13] + 0xfd987193, 12);
2170 thumb_md5_step(F1, c, d, a, b, in[14] + 0xa679438e, 17);
2171 thumb_md5_step(F1, b, c, d, a, in[15] + 0x49b40821, 22);
2173 thumb_md5_step(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
2174 thumb_md5_step(F2, d, a, b, c, in[6] + 0xc040b340, 9);
2175 thumb_md5_step(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
2176 thumb_md5_step(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
2177 thumb_md5_step(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
2178 thumb_md5_step(F2, d, a, b, c, in[10] + 0x02441453, 9);
2179 thumb_md5_step(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
2180 thumb_md5_step(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
2181 thumb_md5_step(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
2182 thumb_md5_step(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
2183 thumb_md5_step(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
2184 thumb_md5_step(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
2185 thumb_md5_step(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
2186 thumb_md5_step(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
2187 thumb_md5_step(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
2188 thumb_md5_step(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
2190 thumb_md5_step(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
2191 thumb_md5_step(F3, d, a, b, c, in[8] + 0x8771f681, 11);
2192 thumb_md5_step(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
2193 thumb_md5_step(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
2194 thumb_md5_step(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
2195 thumb_md5_step(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
2196 thumb_md5_step(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
2197 thumb_md5_step(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
2198 thumb_md5_step(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
2199 thumb_md5_step(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
2200 thumb_md5_step(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
2201 thumb_md5_step(F3, b, c, d, a, in[6] + 0x04881d05, 23);
2202 thumb_md5_step(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
2203 thumb_md5_step(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
2204 thumb_md5_step(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
2205 thumb_md5_step(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
2207 thumb_md5_step(F4, a, b, c, d, in[0] + 0xf4292244, 6);
2208 thumb_md5_step(F4, d, a, b, c, in[7] + 0x432aff97, 10);
2209 thumb_md5_step(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
2210 thumb_md5_step(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
2211 thumb_md5_step(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
2212 thumb_md5_step(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
2213 thumb_md5_step(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
2214 thumb_md5_step(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
2215 thumb_md5_step(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
2216 thumb_md5_step(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
2217 thumb_md5_step(F4, c, d, a, b, in[6] + 0xa3014314, 15);
2218 thumb_md5_step(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
2219 thumb_md5_step(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
2220 thumb_md5_step(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
2221 thumb_md5_step(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
2222 thumb_md5_step(F4, b, c, d, a, in[9] + 0xeb86d391, 21);