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, gboolean *free_return)
208 char *escaped_str, *p;
210 static char *hex_digits = "0123456789abcdef";
216 for (i = 0; i < len; i++)
218 if (!valid_char (str[i]))
222 if (num_invalid == 0)
224 *free_return = FALSE;
228 escaped_str = g_malloc (len + num_invalid*3 + 1);
231 for (i = 0; i < len; i++)
233 if (valid_char (str[i]))
240 *p++ = hex_digits[(c >> 4) & 0xf];
241 *p++ = hex_digits[c & 0xf];
251 hex_unescape_string (const char *str, int *out_len, gboolean *free_return)
254 char *unescaped_str, *p;
260 if (strchr (str, '\\') == NULL)
264 *free_return = FALSE;
268 unescaped_str = g_malloc (len + 1);
271 for (i = 0; i < len; i++)
273 if (str[i] == '\\' &&
278 (g_ascii_xdigit_value (str[i+2]) << 4) |
279 g_ascii_xdigit_value (str[i+3]);
289 *out_len = p - unescaped_str;
291 return unescaped_str;
295 escape_xattr (GFileInfo *info,
296 const char *gio_attr, /* gio attribute name */
297 const char *value, /* Is zero terminated */
298 size_t len /* not including zero termination */)
301 gboolean free_escaped_val;
303 escaped_val = hex_escape_string (value, &free_escaped_val);
305 g_file_info_set_attribute_string (info, gio_attr, escaped_val);
307 if (free_escaped_val)
308 g_free (escaped_val);
312 get_one_xattr (const char *path,
314 const char *gio_attr,
316 gboolean follow_symlinks)
323 len = getxattr (path, xattr, value, sizeof (value)-1);
325 len = lgetxattr (path, xattr,value, sizeof (value)-1);
330 else if (len == -1 && errno == ERANGE)
333 len = getxattr (path, xattr, NULL, 0);
335 len = lgetxattr (path, xattr, NULL, 0);
340 value_p = g_malloc (len+1);
343 len = getxattr (path, xattr, value_p, len);
345 len = lgetxattr (path, xattr, value_p, len);
359 escape_xattr (info, gio_attr, value_p, len);
361 if (value_p != value)
365 #endif /* defined HAVE_XATTR */
368 get_xattrs (const char *path,
371 GFileAttributeMatcher *matcher,
372 gboolean follow_symlinks)
377 ssize_t list_res_size;
380 const char *attr, *attr2;
383 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
385 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
390 list_res_size = listxattr (path, NULL, 0);
392 list_res_size = llistxattr (path, NULL, 0);
394 if (list_res_size == -1 ||
398 list_size = list_res_size;
399 list = g_malloc (list_size);
404 list_res_size = listxattr (path, list, list_size);
406 list_res_size = llistxattr (path, list, list_size);
408 if (list_res_size == -1 && errno == ERANGE)
410 list_size = list_size * 2;
411 list = g_realloc (list, list_size);
415 if (list_res_size == -1)
419 while (list_res_size > 0)
421 if ((user && g_str_has_prefix (attr, "user.")) ||
422 (!user && !g_str_has_prefix (attr, "user.")))
424 char *escaped_attr, *gio_attr;
425 gboolean free_escaped_attr;
429 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
430 gio_attr = g_strconcat ("xattr:", escaped_attr, NULL);
434 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
435 gio_attr = g_strconcat ("xattr_sys:", escaped_attr, NULL);
438 if (free_escaped_attr)
439 g_free (escaped_attr);
441 get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
444 len = strlen (attr) + 1;
446 list_res_size -= len;
453 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
455 char *unescaped_attribute, *a;
456 gboolean free_unescaped_attribute;
458 attr2 = strchr (attr, ':');
461 attr2++; /* Skip ':' */
462 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
464 a = g_strconcat ("user.", unescaped_attribute, NULL);
466 a = unescaped_attribute;
468 get_one_xattr (path, info, attr, a, follow_symlinks);
473 if (free_unescaped_attribute)
474 g_free (unescaped_attribute);
478 #endif /* defined HAVE_XATTR */
483 get_one_xattr_from_fd (int fd,
485 const char *gio_attr,
492 len = fgetxattr (fd, xattr, value, sizeof (value)-1);
497 else if (len == -1 && errno == ERANGE)
499 len = fgetxattr (fd, xattr, NULL, 0);
504 value_p = g_malloc (len+1);
506 len = fgetxattr (fd, xattr, value_p, len);
520 escape_xattr (info, gio_attr, value_p, len);
522 if (value_p != value)
525 #endif /* defined HAVE_XATTR */
528 get_xattrs_from_fd (int fd,
531 GFileAttributeMatcher *matcher)
536 ssize_t list_res_size;
539 const char *attr, *attr2;
542 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
544 all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr_sys");
548 list_res_size = flistxattr (fd, NULL, 0);
550 if (list_res_size == -1 ||
554 list_size = list_res_size;
555 list = g_malloc (list_size);
559 list_res_size = flistxattr (fd, list, list_size);
561 if (list_res_size == -1 && errno == ERANGE)
563 list_size = list_size * 2;
564 list = g_realloc (list, list_size);
568 if (list_res_size == -1)
572 while (list_res_size > 0)
574 if ((user && g_str_has_prefix (attr, "user.")) ||
575 (!user && !g_str_has_prefix (attr, "user.")))
577 char *escaped_attr, *gio_attr;
578 gboolean free_escaped_attr;
582 escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
583 gio_attr = g_strconcat ("xattr:", escaped_attr, NULL);
587 escaped_attr = hex_escape_string (attr, &free_escaped_attr);
588 gio_attr = g_strconcat ("xattr_sys:", escaped_attr, NULL);
591 if (free_escaped_attr)
592 g_free (escaped_attr);
594 get_one_xattr_from_fd (fd, info, gio_attr, attr);
597 len = strlen (attr) + 1;
599 list_res_size -= len;
606 while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
608 char *unescaped_attribute, *a;
609 gboolean free_unescaped_attribute;
611 attr2 = strchr (attr, ':');
614 attr2++; /* Skip ':' */
615 unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
617 a = g_strconcat ("user.", unescaped_attribute, NULL);
619 a = unescaped_attribute;
621 get_one_xattr_from_fd (fd, info, attr, a);
626 if (free_unescaped_attribute)
627 g_free (unescaped_attribute);
631 #endif /* defined HAVE_XATTR */
636 set_xattr (char *filename,
637 const char *escaped_attribute,
638 const GFileAttributeValue *attr_value,
641 char *attribute, *value;
642 gboolean free_attribute, free_value;
643 int val_len, res, errsv;
647 if (attr_value == NULL)
649 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
650 _("Attribute value must be non-NULL"));
654 if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
656 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
657 _("Invalid attribute type (string expected)"));
661 if (!name_is_valid (escaped_attribute))
663 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
664 _("Invalid extended attribute name"));
668 if (g_str_has_prefix (escaped_attribute, "xattr:"))
670 escaped_attribute += 6;
675 g_assert (g_str_has_prefix (escaped_attribute, "xattr_sys:"));
676 escaped_attribute += 10;
680 attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
681 value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
685 a = g_strconcat ("user.", attribute, NULL);
689 res = setxattr (filename, a, value, val_len, 0);
703 g_set_error (error, G_IO_ERROR,
704 g_io_error_from_errno (errsv),
705 _("Error setting extended attribute '%s': %s"),
706 escaped_attribute, g_strerror (errno));
717 _g_local_file_info_get_parent_info (const char *dir,
718 GFileAttributeMatcher *attribute_matcher,
719 GLocalParentFileInfo *parent_info)
724 parent_info->writable = FALSE;
725 parent_info->is_sticky = FALSE;
726 parent_info->device = 0;
728 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME) ||
729 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE) ||
730 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH) ||
731 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT))
733 parent_info->writable = (g_access (dir, W_OK) == 0);
735 res = g_stat (dir, &statbuf);
738 * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
739 * renamed or deleted only by the owner of the file, by the owner of the directory, and
740 * by a privileged process.
744 parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
745 parent_info->owner = statbuf.st_uid;
746 parent_info->device = statbuf.st_dev;
752 get_access_rights (GFileAttributeMatcher *attribute_matcher,
755 struct stat *statbuf,
756 GLocalParentFileInfo *parent_info)
758 if (g_file_attribute_matcher_matches (attribute_matcher,
759 G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
760 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
761 g_access (path, R_OK) == 0);
763 if (g_file_attribute_matcher_matches (attribute_matcher,
764 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
765 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
766 g_access (path, W_OK) == 0);
768 if (g_file_attribute_matcher_matches (attribute_matcher,
769 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
770 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE,
771 g_access (path, X_OK) == 0);
779 if (parent_info->writable)
781 if (parent_info->is_sticky)
783 uid_t uid = geteuid ();
785 if (uid == statbuf->st_uid ||
786 uid == parent_info->owner ||
794 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME))
795 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME,
798 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE))
799 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
802 /* TODO: This means we can move it, but we should also look for a trash dir */
803 if (g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH))
804 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH,
810 set_info_from_stat (GFileInfo *info, struct stat *statbuf,
811 GFileAttributeMatcher *attribute_matcher)
815 file_type = G_FILE_TYPE_UNKNOWN;
817 if (S_ISREG (statbuf->st_mode))
818 file_type = G_FILE_TYPE_REGULAR;
819 else if (S_ISDIR (statbuf->st_mode))
820 file_type = G_FILE_TYPE_DIRECTORY;
821 else if (S_ISCHR (statbuf->st_mode) ||
822 S_ISBLK (statbuf->st_mode) ||
823 S_ISFIFO (statbuf->st_mode)
825 || S_ISSOCK (statbuf->st_mode)
828 file_type = G_FILE_TYPE_SPECIAL;
830 else if (S_ISLNK (statbuf->st_mode))
831 file_type = G_FILE_TYPE_SYMBOLIC_LINK;
834 g_file_info_set_file_type (info, file_type);
835 g_file_info_set_size (info, statbuf->st_size);
837 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE, statbuf->st_dev);
838 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_INODE, statbuf->st_ino);
839 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE, statbuf->st_mode);
840 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_NLINK, statbuf->st_nlink);
841 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_UID, statbuf->st_uid);
842 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_GID, statbuf->st_gid);
843 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_RDEV, statbuf->st_rdev);
844 #if defined (HAVE_STRUCT_STAT_BLKSIZE)
845 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE, statbuf->st_blksize);
847 #if defined (HAVE_STRUCT_STAT_BLOCKS)
848 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_UNIX_BLOCKS, statbuf->st_blocks);
851 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf->st_mtime);
852 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
853 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
854 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
855 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
858 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf->st_atime);
859 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
860 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
861 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
862 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
865 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf->st_ctime);
866 #if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
867 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
868 #elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
869 g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
872 if (g_file_attribute_matcher_matches (attribute_matcher,
873 G_FILE_ATTRIBUTE_ETAG_VALUE))
875 char *etag = _g_local_file_info_create_etag (statbuf);
876 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ETAG_VALUE, etag);
880 if (g_file_attribute_matcher_matches (attribute_matcher,
881 G_FILE_ATTRIBUTE_ID_FILE))
883 char *id = _g_local_file_info_create_file_id (statbuf);
884 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE, id);
888 if (g_file_attribute_matcher_matches (attribute_matcher,
889 G_FILE_ATTRIBUTE_ID_FS))
891 char *id = _g_local_file_info_create_fs_id (statbuf);
892 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_ID_FS, id);
898 make_valid_utf8 (const char *name)
901 const gchar *remainder, *invalid;
902 gint remaining_bytes, valid_bytes;
906 remaining_bytes = strlen (name);
908 while (remaining_bytes != 0)
910 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
912 valid_bytes = invalid - remainder;
915 string = g_string_sized_new (remaining_bytes);
917 g_string_append_len (string, remainder, valid_bytes);
918 /* append U+FFFD REPLACEMENT CHARACTER */
919 g_string_append (string, "\357\277\275");
921 remaining_bytes -= valid_bytes + 1;
922 remainder = invalid + 1;
926 return g_strdup (name);
928 g_string_append (string, remainder);
930 g_assert (g_utf8_validate (string->str, -1, NULL));
932 return g_string_free (string, FALSE);
936 convert_pwd_string_to_utf8 (char *pwd_str)
940 if (!g_utf8_validate (pwd_str, -1, NULL))
942 utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
943 if (utf8_string == NULL)
944 utf8_string = make_valid_utf8 (pwd_str);
947 utf8_string = g_strdup (pwd_str);
953 uid_data_free (UidData *data)
955 g_free (data->user_name);
956 g_free (data->real_name);
960 /* called with lock held */
962 lookup_uid_data (uid_t uid)
967 struct passwd *pwbufp;
970 if (uid_cache == NULL)
971 uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
973 data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
978 data = g_new0 (UidData, 1);
980 #if defined(HAVE_POSIX_GETPWUID_R)
981 getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
982 #elif defined(HAVE_NONPOSIX_GETPWUID_R)
983 pwbufp = getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer));
985 pwbufp = getpwuid (uid);
990 if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
991 data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
993 gecos = pwbufp->pw_gecos;
997 comma = strchr (gecos, ',');
1000 data->real_name = convert_pwd_string_to_utf8 (gecos);
1004 /* Default fallbacks */
1005 if (data->real_name == NULL)
1007 if (data->user_name != NULL)
1008 data->real_name = g_strdup (data->user_name);
1010 data->real_name = g_strdup_printf("user #%d", (int)uid);
1013 if (data->user_name == NULL)
1014 data->user_name = g_strdup_printf("%d", (int)uid);
1016 g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
1022 get_username_from_uid (uid_t uid)
1028 data = lookup_uid_data (uid);
1029 res = g_strdup (data->user_name);
1030 G_UNLOCK (uid_cache);
1036 get_realname_from_uid (uid_t uid)
1042 data = lookup_uid_data (uid);
1043 res = g_strdup (data->real_name);
1044 G_UNLOCK (uid_cache);
1050 /* called with lock held */
1052 lookup_gid_name (gid_t gid)
1057 struct group *gbufp;
1059 if (gid_cache == NULL)
1060 gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
1062 name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
1067 #if defined (HAVE_POSIX_GETGRGID_R)
1068 getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
1069 #elif defined (HAVE_NONPOSIX_GETGRGID_R)
1070 gbufp = getgrgid_r (gid, &gbuf, buffer, sizeof(buffer));
1072 gbufp = getgrgid (gid);
1075 if (gbufp != NULL &&
1076 gbufp->gr_name != NULL &&
1077 gbufp->gr_name[0] != 0)
1078 name = convert_pwd_string_to_utf8 (gbufp->gr_name);
1080 name = g_strdup_printf("%d", (int)gid);
1082 g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
1088 get_groupname_from_gid (gid_t gid)
1094 name = lookup_gid_name (gid);
1095 res = g_strdup (name);
1096 G_UNLOCK (gid_cache);
1101 get_content_type (const char *basename,
1103 struct stat *statbuf,
1104 gboolean is_symlink,
1105 gboolean symlink_broken,
1106 GFileQueryInfoFlags flags,
1110 (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
1111 return g_strdup ("inode/symlink");
1112 else if (S_ISDIR(statbuf->st_mode))
1113 return g_strdup ("inode/directory");
1114 else if (S_ISCHR(statbuf->st_mode))
1115 return g_strdup ("inode/chardevice");
1116 else if (S_ISBLK(statbuf->st_mode))
1117 return g_strdup ("inode/blockdevice");
1118 else if (S_ISFIFO(statbuf->st_mode))
1119 return g_strdup ("inode/fifo");
1121 else if (S_ISSOCK(statbuf->st_mode))
1122 return g_strdup ("inode/socket");
1127 gboolean result_uncertain;
1129 content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
1132 if (!fast && result_uncertain && path != NULL)
1134 guchar sniff_buffer[4096];
1138 sniff_length = _g_unix_content_type_get_sniff_len ();
1139 if (sniff_length > 4096)
1140 sniff_length = 4096;
1142 fd = open (path, O_RDONLY);
1147 res = read (fd, sniff_buffer, sniff_length);
1151 g_free (content_type);
1152 content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
1158 return content_type;
1164 thumb_digest_to_ascii (unsigned char digest[16], const char *suffix)
1166 static const char hex_digits[] = "0123456789abcdef";
1170 res = g_malloc (33 + strlen (suffix));
1172 for (i = 0; i < 16; i++) {
1173 res[2*i] = hex_digits[digest[i] >> 4];
1174 res[2*i+1] = hex_digits[digest[i] & 0xf];
1179 strcat (res, suffix);
1186 get_thumbnail_attributes (const char *path,
1190 unsigned char digest[16];
1194 uri = g_filename_to_uri (path, NULL, NULL);
1195 thumb_md5 (uri, digest);
1198 basename = thumb_digest_to_ascii (digest, ".png");
1200 filename = g_build_filename (g_get_home_dir(), ".thumbnails", "normal", basename, NULL);
1202 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1203 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH, filename);
1207 filename = g_build_filename (g_get_home_dir(), ".thumbnails", "fail", "gnome-thumbnail-factory", basename, NULL);
1209 if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
1210 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED, TRUE);
1218 _g_local_file_info_get (const char *basename,
1220 GFileAttributeMatcher *attribute_matcher,
1221 GFileQueryInfoFlags flags,
1222 GLocalParentFileInfo *parent_info,
1226 struct stat statbuf;
1227 struct stat statbuf2;
1229 gboolean is_symlink, symlink_broken;
1231 info = g_file_info_new ();
1233 /* Make sure we don't set any unwanted attributes */
1234 g_file_info_set_attribute_mask (info, attribute_matcher);
1236 g_file_info_set_name (info, basename);
1238 /* Avoid stat in trivial case */
1239 if (attribute_matcher == NULL)
1242 res = g_lstat (path, &statbuf);
1245 g_object_unref (info);
1246 g_set_error (error, G_IO_ERROR,
1247 g_io_error_from_errno (errno),
1248 _("Error stating file '%s': %s"),
1249 path, g_strerror (errno));
1254 is_symlink = S_ISLNK (statbuf.st_mode);
1258 symlink_broken = FALSE;
1262 g_file_info_set_is_symlink (info, TRUE);
1264 /* Unless NOFOLLOW was set we default to following symlinks */
1265 if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
1267 res = stat (path, &statbuf2);
1269 /* Report broken links as symlinks */
1273 symlink_broken = TRUE;
1277 set_info_from_stat (info, &statbuf, attribute_matcher);
1279 if (basename != NULL && basename[0] == '.')
1280 g_file_info_set_is_hidden (info, TRUE);
1282 if (basename != NULL && basename[strlen (basename) -1] == '~')
1283 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_STD_IS_BACKUP, TRUE);
1286 g_file_attribute_matcher_matches (attribute_matcher,
1287 G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET))
1289 char *link = read_link (path);
1290 g_file_info_set_symlink_target (info, link);
1294 if (g_file_attribute_matcher_matches (attribute_matcher,
1295 G_FILE_ATTRIBUTE_STD_DISPLAY_NAME))
1297 char *display_name = g_filename_display_basename (path);
1299 if (strstr (display_name, "\357\277\275") != NULL)
1301 char *p = display_name;
1302 display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
1305 g_file_info_set_display_name (info, display_name);
1306 g_free (display_name);
1309 if (g_file_attribute_matcher_matches (attribute_matcher,
1310 G_FILE_ATTRIBUTE_STD_EDIT_NAME))
1312 char *edit_name = g_filename_display_basename (path);
1313 g_file_info_set_edit_name (info, edit_name);
1318 if (g_file_attribute_matcher_matches (attribute_matcher,
1319 G_FILE_ATTRIBUTE_STD_COPY_NAME))
1321 char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1323 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_COPY_NAME, copy_name);
1327 if (g_file_attribute_matcher_matches (attribute_matcher,
1328 G_FILE_ATTRIBUTE_STD_CONTENT_TYPE) ||
1329 g_file_attribute_matcher_matches (attribute_matcher,
1330 G_FILE_ATTRIBUTE_STD_ICON))
1332 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, FALSE);
1336 g_file_info_set_content_type (info, content_type);
1338 if (g_file_attribute_matcher_matches (attribute_matcher,
1339 G_FILE_ATTRIBUTE_STD_ICON))
1341 char *mimetype_icon, *generic_mimetype_icon, *type_icon, *p;
1342 char *icon_names[3];
1346 mimetype_icon = g_strdup (content_type);
1348 while ((p = strchr(mimetype_icon, '/')) != NULL)
1351 p = strchr (content_type, '/');
1353 p = content_type + strlen (content_type);
1355 generic_mimetype_icon = g_malloc (p - content_type + strlen ("-x-generic") + 1);
1356 memcpy (generic_mimetype_icon, content_type, p - content_type);
1357 memcpy (generic_mimetype_icon + (p - content_type), "-x-generic", strlen ("-x-generic"));
1358 generic_mimetype_icon[(p - content_type) + strlen ("-x-generic")] = 0;
1360 /* TODO: Special case desktop dir? That could be expensive with xdg dirs... */
1361 if (strcmp (path, g_get_home_dir ()) == 0)
1362 type_icon = "user-home";
1363 else if (S_ISDIR (statbuf.st_mode))
1364 type_icon = "folder";
1365 else if (statbuf.st_mode & S_IXUSR)
1366 type_icon = "application-x-executable";
1368 type_icon = "text-x-generic";
1371 icon_names[i++] = mimetype_icon;
1372 icon_names[i++] = generic_mimetype_icon;
1373 if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
1374 strcmp (mimetype_icon, type_icon) != 0)
1375 icon_names[i++] = type_icon;
1377 icon = g_themed_icon_new_from_names (icon_names, i);
1378 g_file_info_set_icon (info, icon);
1380 g_object_unref (icon);
1381 g_free (mimetype_icon);
1382 g_free (generic_mimetype_icon);
1385 g_free (content_type);
1389 if (g_file_attribute_matcher_matches (attribute_matcher,
1390 G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE))
1392 char *content_type = get_content_type (basename, path, &statbuf, is_symlink, symlink_broken, flags, TRUE);
1396 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE, content_type);
1397 g_free (content_type);
1401 if (g_file_attribute_matcher_matches (attribute_matcher,
1402 G_FILE_ATTRIBUTE_OWNER_USER))
1406 name = get_username_from_uid (statbuf.st_uid);
1408 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER, name);
1412 if (g_file_attribute_matcher_matches (attribute_matcher,
1413 G_FILE_ATTRIBUTE_OWNER_USER_REAL))
1417 name = get_realname_from_uid (statbuf.st_uid);
1419 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER_REAL, name);
1423 if (g_file_attribute_matcher_matches (attribute_matcher,
1424 G_FILE_ATTRIBUTE_OWNER_GROUP))
1428 name = get_groupname_from_gid (statbuf.st_gid);
1430 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP, name);
1434 if (parent_info && parent_info->device != 0 &&
1435 g_file_attribute_matcher_matches (attribute_matcher, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT) &&
1436 statbuf.st_dev != parent_info->device)
1437 g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, TRUE);
1439 get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
1441 get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1442 get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1443 get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
1445 if (g_file_attribute_matcher_matches (attribute_matcher,
1446 G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
1447 get_thumbnail_attributes (path, info);
1449 g_file_info_unset_attribute_mask (info);
1455 _g_local_file_info_get_from_fd (int fd,
1459 struct stat stat_buf;
1460 GFileAttributeMatcher *matcher;
1463 if (fstat (fd, &stat_buf) == -1)
1465 g_set_error (error, G_IO_ERROR,
1466 g_io_error_from_errno (errno),
1467 _("Error stating file descriptor: %s"),
1468 g_strerror (errno));
1472 info = g_file_info_new ();
1474 matcher = g_file_attribute_matcher_new (attributes);
1476 /* Make sure we don't set any unwanted attributes */
1477 g_file_info_set_attribute_mask (info, matcher);
1479 set_info_from_stat (info, &stat_buf, matcher);
1482 if (g_file_attribute_matcher_matches (matcher, "selinux:context") &&
1483 is_selinux_enabled ())
1486 if (fgetfilecon_raw (fd, &context) >= 0)
1488 g_file_info_set_attribute_string (info, "selinux:context", context);
1494 get_xattrs_from_fd (fd, TRUE, info, matcher);
1495 get_xattrs_from_fd (fd, FALSE, info, matcher);
1497 g_file_attribute_matcher_unref (matcher);
1499 g_file_info_unset_attribute_mask (info);
1505 get_uint32 (const GFileAttributeValue *value,
1509 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
1511 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1512 _("Invalid attribute type (uint32 expected)"));
1516 *val_out = value->u.uint32;
1522 get_uint64 (const GFileAttributeValue *value,
1526 if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
1528 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1529 _("Invalid attribute type (uint64 expected)"));
1533 *val_out = value->u.uint64;
1538 #if defined(HAVE_SYMLINK)
1540 get_byte_string (const GFileAttributeValue *value,
1541 const char **val_out,
1544 if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
1546 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1547 _("Invalid attribute type (byte string expected)"));
1551 *val_out = value->u.string;
1558 set_unix_mode (char *filename,
1559 const GFileAttributeValue *value,
1564 if (!get_uint32 (value, &val, error))
1567 if (g_chmod (filename, val) == -1)
1569 g_set_error (error, G_IO_ERROR,
1570 g_io_error_from_errno (errno),
1571 _("Error setting permissions: %s"),
1572 g_strerror (errno));
1580 set_unix_uid_gid (char *filename,
1581 const GFileAttributeValue *uid_value,
1582 const GFileAttributeValue *gid_value,
1583 GFileQueryInfoFlags flags,
1593 if (!get_uint32 (uid_value, &val, error))
1602 if (!get_uint32 (gid_value, &val, error))
1609 if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
1610 res = lchown (filename, uid, gid);
1612 res = chown (filename, uid, gid);
1616 g_set_error (error, G_IO_ERROR,
1617 g_io_error_from_errno (errno),
1618 _("Error setting owner: %s"),
1619 g_strerror (errno));
1628 set_symlink (char *filename,
1629 const GFileAttributeValue *value,
1633 struct stat statbuf;
1635 if (!get_byte_string (value, &val, error))
1640 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1641 _("symlink must be non-NULL"));
1645 if (g_lstat (filename, &statbuf))
1647 g_set_error (error, G_IO_ERROR,
1648 g_io_error_from_errno (errno),
1649 _("Error setting symlink: %s"),
1650 g_strerror (errno));
1654 if (!S_ISLNK (statbuf.st_mode))
1656 g_set_error (error, G_IO_ERROR,
1657 G_IO_ERROR_NOT_SYMBOLIC_LINK,
1658 _("Error setting symlink: file is not a symlink"));
1662 if (g_unlink (filename))
1664 g_set_error (error, G_IO_ERROR,
1665 g_io_error_from_errno (errno),
1666 _("Error setting symlink: %s"),
1667 g_strerror (errno));
1671 if (symlink (filename, val) != 0)
1673 g_set_error (error, G_IO_ERROR,
1674 g_io_error_from_errno (errno),
1675 _("Error setting symlink: %s"),
1676 g_strerror (errno));
1685 lazy_stat (char *filename, struct stat *statbuf, gboolean *called_stat)
1692 res = g_stat (filename, statbuf);
1695 *called_stat = TRUE;
1703 set_mtime_atime (char *filename,
1704 const GFileAttributeValue *mtime_value,
1705 const GFileAttributeValue *mtime_usec_value,
1706 const GFileAttributeValue *atime_value,
1707 const GFileAttributeValue *atime_usec_value,
1713 struct stat statbuf;
1714 gboolean got_stat = FALSE;
1715 struct timeval times[2] = { {0, 0}, {0, 0} };
1720 if (!get_uint64 (atime_value, &val, error))
1722 times[0].tv_sec = val;
1726 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1728 times[0].tv_sec = statbuf.st_mtime;
1729 #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
1730 times[0].tv_usec = statbuf.st_atimensec / 1000;
1731 #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1732 times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
1737 if (atime_usec_value)
1739 if (!get_uint32 (atime_usec_value, &val_usec, error))
1741 times[0].tv_usec = val_usec;
1747 if (!get_uint64 (mtime_value, &val, error))
1749 times[1].tv_sec = val;
1753 if (lazy_stat (filename, &statbuf, &got_stat) == 0)
1755 times[1].tv_sec = statbuf.st_mtime;
1756 #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
1757 times[1].tv_usec = statbuf.st_mtimensec / 1000;
1758 #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
1759 times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
1764 if (mtime_usec_value)
1766 if (!get_uint32 (mtime_usec_value, &val_usec, error))
1768 times[1].tv_usec = val_usec;
1771 res = utimes(filename, times);
1774 g_set_error (error, G_IO_ERROR,
1775 g_io_error_from_errno (errno),
1776 _("Error setting owner: %s"),
1777 g_strerror (errno));
1785 _g_local_file_info_set_attribute (char *filename,
1786 const char *attribute,
1787 const GFileAttributeValue *value,
1788 GFileQueryInfoFlags flags,
1789 GCancellable *cancellable,
1792 if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
1793 return set_unix_mode (filename, value, error);
1796 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
1797 return set_unix_uid_gid (filename, value, NULL, flags, error);
1798 else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
1799 return set_unix_uid_gid (filename, NULL, value, flags, error);
1803 else if (strcmp (attribute, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET) == 0)
1804 return set_symlink (filename, value, error);
1808 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
1809 return set_mtime_atime (filename, value, NULL, NULL, NULL, error);
1810 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
1811 return set_mtime_atime (filename, NULL, value, NULL, NULL, error);
1812 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
1813 return set_mtime_atime (filename, NULL, NULL, value, NULL, error);
1814 else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
1815 return set_mtime_atime (filename, NULL, NULL, NULL, value, error);
1819 else if (g_str_has_prefix (attribute, "xattr:"))
1820 return set_xattr (filename, attribute, value, error);
1821 else if (g_str_has_prefix (attribute, "xattr_sys:"))
1822 return set_xattr (filename, attribute, value, error);
1825 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1826 _("Setting attribute %s not supported"), attribute);
1831 _g_local_file_info_set_attributes (char *filename,
1833 GFileQueryInfoFlags flags,
1834 GCancellable *cancellable,
1837 GFileAttributeValue *value, *uid, *gid;
1838 GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
1839 GFileAttributeStatus status;
1842 /* Handles setting multiple specified data in a single set, and takes care
1843 of ordering restrictions when setting attributes */
1847 /* Set symlink first, since this recreates the file */
1849 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1852 if (!set_symlink (filename, value, error))
1854 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1856 /* Don't set error multiple times */
1860 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1866 /* Group uid and gid setting into one call
1867 * Change ownership before permissions, since ownership changes can
1868 change permissions (e.g. setuid)
1870 uid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_UID);
1871 gid = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_GID);
1875 if (!set_unix_uid_gid (filename, uid, gid, flags, error))
1877 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1879 /* Don't set error multiple times */
1883 status = G_FILE_ATTRIBUTE_STATUS_SET;
1885 uid->status = status;
1887 gid->status = status;
1891 value = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE);
1894 if (!set_unix_mode (filename, value, error))
1896 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1898 /* Don't set error multiple times */
1902 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
1907 /* Group all time settings into one call
1908 * Change times as the last thing to avoid it changing due to metadata changes
1911 mtime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
1912 mtime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1913 atime = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
1914 atime_usec = g_file_info_get_attribute (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
1916 if (mtime || mtime_usec || atime || atime_usec)
1918 if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
1920 status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
1922 /* Don't set error multiple times */
1926 status = G_FILE_ATTRIBUTE_STATUS_SET;
1929 mtime->status = status;
1931 mtime_usec->status = status;
1933 atime->status = status;
1935 atime_usec->status = status;
1939 /* xattrs are handled by default callback */
1946 * This code implements the MD5 message-digest algorithm.
1947 * The algorithm is due to Ron Rivest. This code was
1948 * written by Colin Plumb in 1993, no copyright is claimed.
1949 * This code is in the public domain; do with it what you wish.
1951 * Equivalent code is available from RSA Data Security, Inc.
1952 * This code has been tested against that, and is equivalent,
1953 * except that you don't need to include two pages of legalese
1956 * To compute the message digest of a chunk of bytes, declare an
1957 * ThumbMD5Context structure, pass it to thumb_md5_init, call
1958 * thumb_md5_update as needed on buffers full of bytes, and then call
1959 * thumb_md5_final, which will fill a supplied 32-byte array with the
1960 * digest in ascii form.
1964 static void thumb_md5_init (struct ThumbMD5Context *context);
1965 static void thumb_md5_update (struct ThumbMD5Context *context,
1966 unsigned char const *buf,
1968 static void thumb_md5_final (unsigned char digest[16],
1969 struct ThumbMD5Context *context);
1970 static void thumb_md5_transform (guint32 buf[4],
1971 guint32 const in[16]);
1975 thumb_md5 (const char *string, unsigned char digest[16])
1977 struct ThumbMD5Context md5_context;
1979 thumb_md5_init (&md5_context);
1980 thumb_md5_update (&md5_context, (unsigned char *)string, strlen (string));
1981 thumb_md5_final (digest, &md5_context);
1984 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1985 #define byteReverse(buf, len) /* Nothing */
1989 * Note: this code is harmless on little-endian machines.
1992 byteReverse(unsigned char *buf, unsigned longs)
1996 t = (guint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
1997 ((unsigned) buf[1] << 8 | buf[0]);
1998 *(guint32 *) buf = t;
2006 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
2007 * initialization constants.
2010 thumb_md5_init (struct ThumbMD5Context *ctx)
2012 ctx->buf[0] = 0x67452301;
2013 ctx->buf[1] = 0xefcdab89;
2014 ctx->buf[2] = 0x98badcfe;
2015 ctx->buf[3] = 0x10325476;
2022 * Update context to reflect the concatenation of another buffer full
2026 thumb_md5_update (struct ThumbMD5Context *ctx,
2027 unsigned char const *buf,
2032 /* Update bitcount */
2035 if ((ctx->bits[0] = t + ((guint32) len << 3)) < t)
2036 ctx->bits[1]++; /* Carry from low to high */
2037 ctx->bits[1] += len >> 29;
2039 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
2041 /* Handle any leading odd-sized chunks */
2044 unsigned char *p = (unsigned char *) ctx->in + t;
2048 memcpy (p, buf, len);
2052 byteReverse (ctx->in, 16);
2053 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2058 /* Process data in 64-byte chunks */
2061 memcpy (ctx->in, buf, 64);
2062 byteReverse (ctx->in, 16);
2063 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2068 /* Handle any remaining bytes of data. */
2070 memcpy(ctx->in, buf, len);
2074 * Final wrapup - pad to 64-byte boundary with the bit pattern
2075 * 1 0* (64-bit count of bits processed, MSB-first)
2078 thumb_md5_final (unsigned char digest[16], struct ThumbMD5Context *ctx)
2083 /* Compute number of bytes mod 64 */
2084 count = (ctx->bits[0] >> 3) & 0x3F;
2086 /* Set the first char of padding to 0x80. This is safe since there is
2087 always at least one byte free */
2088 p = ctx->in + count;
2091 /* Bytes of padding needed to make 64 bytes */
2092 count = 64 - 1 - count;
2094 /* Pad out to 56 mod 64 */
2096 /* Two lots of padding: Pad the first block to 64 bytes */
2097 memset (p, 0, count);
2098 byteReverse (ctx->in, 16);
2099 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2101 /* Now fill the next block with 56 bytes */
2102 memset(ctx->in, 0, 56);
2104 /* Pad block to 56 bytes */
2105 memset(p, 0, count - 8);
2107 byteReverse(ctx->in, 14);
2109 /* Append length in bits and transform */
2110 ((guint32 *) ctx->in)[14] = ctx->bits[0];
2111 ((guint32 *) ctx->in)[15] = ctx->bits[1];
2113 thumb_md5_transform (ctx->buf, (guint32 *) ctx->in);
2114 byteReverse ((unsigned char *) ctx->buf, 4);
2115 memcpy (digest, ctx->buf, 16);
2116 memset (ctx, 0, sizeof(ctx)); /* In case it's sensitive */
2120 /* The four core functions - F1 is optimized somewhat */
2122 #define F1(x, y, z) (z ^ (x & (y ^ z)))
2123 #define F2(x, y, z) F1 (z, x, y)
2124 #define F3(x, y, z) (x ^ y ^ z)
2125 #define F4(x, y, z) (y ^ (x | ~z))
2127 /* This is the central step in the MD5 algorithm. */
2128 #define thumb_md5_step(f, w, x, y, z, data, s) \
2129 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
2132 * The core of the MD5 algorithm, this alters an existing MD5 hash to
2133 * reflect the addition of 16 longwords of new data. ThumbMD5Update blocks
2134 * the data and converts bytes into longwords for this routine.
2137 thumb_md5_transform (guint32 buf[4], guint32 const in[16])
2139 register guint32 a, b, c, d;
2146 thumb_md5_step(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
2147 thumb_md5_step(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
2148 thumb_md5_step(F1, c, d, a, b, in[2] + 0x242070db, 17);
2149 thumb_md5_step(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
2150 thumb_md5_step(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
2151 thumb_md5_step(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
2152 thumb_md5_step(F1, c, d, a, b, in[6] + 0xa8304613, 17);
2153 thumb_md5_step(F1, b, c, d, a, in[7] + 0xfd469501, 22);
2154 thumb_md5_step(F1, a, b, c, d, in[8] + 0x698098d8, 7);
2155 thumb_md5_step(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
2156 thumb_md5_step(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
2157 thumb_md5_step(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
2158 thumb_md5_step(F1, a, b, c, d, in[12] + 0x6b901122, 7);
2159 thumb_md5_step(F1, d, a, b, c, in[13] + 0xfd987193, 12);
2160 thumb_md5_step(F1, c, d, a, b, in[14] + 0xa679438e, 17);
2161 thumb_md5_step(F1, b, c, d, a, in[15] + 0x49b40821, 22);
2163 thumb_md5_step(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
2164 thumb_md5_step(F2, d, a, b, c, in[6] + 0xc040b340, 9);
2165 thumb_md5_step(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
2166 thumb_md5_step(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
2167 thumb_md5_step(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
2168 thumb_md5_step(F2, d, a, b, c, in[10] + 0x02441453, 9);
2169 thumb_md5_step(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
2170 thumb_md5_step(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
2171 thumb_md5_step(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
2172 thumb_md5_step(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
2173 thumb_md5_step(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
2174 thumb_md5_step(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
2175 thumb_md5_step(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
2176 thumb_md5_step(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
2177 thumb_md5_step(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
2178 thumb_md5_step(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
2180 thumb_md5_step(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
2181 thumb_md5_step(F3, d, a, b, c, in[8] + 0x8771f681, 11);
2182 thumb_md5_step(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
2183 thumb_md5_step(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
2184 thumb_md5_step(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
2185 thumb_md5_step(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
2186 thumb_md5_step(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
2187 thumb_md5_step(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
2188 thumb_md5_step(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
2189 thumb_md5_step(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
2190 thumb_md5_step(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
2191 thumb_md5_step(F3, b, c, d, a, in[6] + 0x04881d05, 23);
2192 thumb_md5_step(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
2193 thumb_md5_step(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
2194 thumb_md5_step(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
2195 thumb_md5_step(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
2197 thumb_md5_step(F4, a, b, c, d, in[0] + 0xf4292244, 6);
2198 thumb_md5_step(F4, d, a, b, c, in[7] + 0x432aff97, 10);
2199 thumb_md5_step(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
2200 thumb_md5_step(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
2201 thumb_md5_step(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
2202 thumb_md5_step(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
2203 thumb_md5_step(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
2204 thumb_md5_step(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
2205 thumb_md5_step(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
2206 thumb_md5_step(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
2207 thumb_md5_step(F4, c, d, a, b, in[6] + 0xa3014314, 15);
2208 thumb_md5_step(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
2209 thumb_md5_step(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
2210 thumb_md5_step(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
2211 thumb_md5_step(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
2212 thumb_md5_step(F4, b, c, d, a, in[9] + 0xeb86d391, 21);