4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* support.c - (non-GUI) useful routines */
31 #include <sys/param.h>
39 #include <libxml/parser.h>
53 static GHashTable
*uid_hash
= NULL
; /* UID -> User name */
54 static GHashTable
*gid_hash
= NULL
; /* GID -> Group name */
56 /* Static prototypes */
57 static void MD5Transform(guint32 buf
[4], guint32
const in
[16]);
59 /****************************************************************
60 * EXTERNAL INTERFACE *
61 ****************************************************************/
64 gint
object_getref(GObject
*object
)
66 return object
->ref_count
;
69 /* g_fscache_data_unref() the result! */
70 XMLwrapper
*xml_cache_load(const gchar
*pathname
)
72 static GFSCache
*xml_cache
= NULL
;
75 xml_cache
= g_fscache_new((GFSLoadFunc
) xml_new
,
76 (GFSRefFunc
) g_object_ref
,
77 (GFSRefFunc
) g_object_unref
,
78 (GFSGetRefFunc
) object_getref
,
80 return g_fscache_lookup(xml_cache
, pathname
);
83 /* Taking this node and each directly following node with the same name,
84 * return the one which matches the current LANG.
85 * Return the node itself if nothing matches.
87 static xmlNode
*best_lang(xmlNode
*first
)
89 xmlNode
*node
= first
;
90 const char *target_lang
= current_lang
? current_lang
: "en";
92 g_return_val_if_fail(first
!= NULL
, NULL
);
94 for (node
= first
->next
; node
; node
= node
->next
)
98 if (node
->type
!= XML_ELEMENT_NODE
)
101 /* Check names match... */
102 if (strcmp(node
->name
, first
->name
))
105 /* Check namespaces match... */
106 if ((node
->ns
== NULL
) != (first
->ns
== NULL
))
109 if (node
->ns
&& first
->ns
)
110 if (strcmp(node
->ns
->href
, first
->ns
->href
))
113 lang
= xmlNodeGetLang(node
);
117 if (strcmp(lang
, target_lang
) == 0)
128 /* Return the (first) child of this node with the given name.
130 * If there are several consecutive nodes with the same name but different
131 * xml:lang attributes, then the one matching the current locale is used,
132 * or the first one if none match.
134 xmlNode
*get_subnode(xmlNode
*node
, const char *namespaceURI
, const char *name
)
136 for (node
= node
->xmlChildrenNode
; node
; node
= node
->next
)
138 if (node
->type
!= XML_ELEMENT_NODE
)
141 if (strcmp(node
->name
, name
))
144 if (node
->ns
== NULL
|| namespaceURI
== NULL
)
146 if (node
->ns
== NULL
&& namespaceURI
== NULL
)
147 return best_lang(node
);
151 if (strcmp(node
->ns
->href
, namespaceURI
) == 0)
152 return best_lang(node
);
158 /* Save doc as XML as filename, 0 on success or -1 on failure */
159 int save_xml_file(xmlDocPtr doc
, const gchar
*filename
)
161 #if LIBXML_VERSION > 20400
162 if (xmlSaveFormatFileEnc(filename
, doc
, NULL
, 1) < 0)
167 out
= fopen(filename
, "w");
171 xmlDocDump(out
, doc
); /* Some versions return void */
180 /* Create a new SOAP message and return the document and the (empty)
183 xmlDocPtr
soap_new(xmlNodePtr
*ret_body
)
189 doc
= xmlNewDoc("1.0");
190 root
= xmlNewDocNode(doc
, NULL
, "Envelope", NULL
);
191 xmlDocSetRootElement(doc
, root
);
193 env_ns
= xmlNewNs(root
, SOAP_ENV_NS
, "env");
194 xmlSetNs(root
, env_ns
);
196 *ret_body
= xmlNewTextChild(root
, env_ns
, "Body", NULL
);
197 xmlNewNs(*ret_body
, ROX_NS
, "rox");
202 /* Like g_strdup, but does realpath() too (if possible) */
203 char *pathdup(const char *path
)
205 char real
[MAXPATHLEN
];
207 g_return_val_if_fail(path
!= NULL
, NULL
);
209 if (realpath(path
, real
))
210 return g_strdup(real
);
212 return g_strdup(path
);
215 /* Join the path to the leaf (adding a / between them) and
216 * return a pointer to a buffer with the result. Buffer is valid until
217 * the next call to make_path.
219 GString
*make_path(const char *dir
, const char *leaf
)
221 static GString
*buffer
= NULL
;
224 buffer
= g_string_new(NULL
);
226 g_return_val_if_fail(dir
!= NULL
, buffer
);
227 g_return_val_if_fail(leaf
!= NULL
, buffer
);
229 g_string_sprintf(buffer
, "%s%s%s",
231 dir
[0] == '/' && dir
[1] == '\0' ? "" : "/",
237 /* Return our complete host name for DND */
238 const char *our_host_name_for_dnd(void)
240 if (o_dnd_no_hostnames
.int_value
)
242 return our_host_name();
245 /* Return our complete host name, unconditionally */
246 const char *our_host_name(void)
248 static char *name
= NULL
;
254 if (gethostname(buffer
, 4096) == 0)
256 /* gethostname doesn't always return the full name... */
260 ent
= gethostbyname(buffer
);
261 name
= g_strdup(ent
? ent
->h_name
: buffer
);
265 g_warning("gethostname() failed - using localhost\n");
266 name
= g_strdup("localhost");
273 /* Create a child process. cd to dir first (if dir is non-NULL).
274 * If from_stderr is set, create a pipe for stderr and return the readable
276 * Returns the PID of the child, or 0 on failure (from_stderr is still valid).
278 pid_t
spawn_full(const char **argv
, const char *dir
, int *from_stderr
)
286 *from_stderr
= fd
[0];
297 return 0; /* Failure */
300 /* We are the child process */
304 if (fd
[1] != STDERR_FILENO
)
306 dup2(fd
[1], STDERR_FILENO
);
308 close_on_exec(STDERR_FILENO
, FALSE
);
314 fprintf(stderr
, "chdir() failed: %s\n",
316 execvp(argv
[0], (char **) argv
);
317 fprintf(stderr
, "execvp(%s, ...) failed: %s\n",
326 /* We are the parent */
330 void debug_free_string(void *data
)
332 g_print("Freeing string '%s'\n", (char *) data
);
336 const char *user_name(uid_t uid
)
341 uid_hash
= g_hash_table_new(NULL
, NULL
);
343 retval
= g_hash_table_lookup(uid_hash
, GINT_TO_POINTER(uid
));
347 struct passwd
*passwd
;
349 passwd
= getpwuid(uid
);
350 retval
= passwd
? g_strdup(passwd
->pw_name
)
351 : g_strdup_printf("[%d]", (int) uid
);
352 g_hash_table_insert(uid_hash
, GINT_TO_POINTER(uid
),
359 const char *group_name(gid_t gid
)
364 gid_hash
= g_hash_table_new(NULL
, NULL
);
366 retval
= g_hash_table_lookup(gid_hash
, GINT_TO_POINTER(gid
));
372 group
= getgrgid(gid
);
373 retval
= group
? g_strdup(group
->gr_name
)
374 : g_strdup_printf("[%d]", (int) gid
);
375 g_hash_table_insert(gid_hash
, GINT_TO_POINTER(gid
),
382 /* Return a string in the form '23Mb' in a static buffer valid until
385 char *format_size(off_t size
)
387 static char *buffer
= NULL
;
390 if (size
>= PRETTY_SIZE_LIMIT
)
394 if (size
>= PRETTY_SIZE_LIMIT
)
398 if (size
>= PRETTY_SIZE_LIMIT
)
417 buffer
= g_strdup_printf("%" SIZE_FMT
" %s", size
, units
);
422 /* Return a string in the form '23Mb' in a static buffer valid until
423 * the next call. Aligned to the right (5 chars).
425 char *format_size_aligned(off_t size
)
427 static char *buffer
= NULL
;
430 if (size
>= PRETTY_SIZE_LIMIT
)
434 if (size
>= PRETTY_SIZE_LIMIT
)
438 if (size
>= PRETTY_SIZE_LIMIT
)
456 buffer
= g_strdup_printf("%4" SIZE_FMT
"%c", size
, units
);
462 * Similar to format_size(), but this one uses a double argument since
463 * unsigned long isn't wide enough on all platforms and we must be able to
464 * sum sizes above 4 GB.
466 gchar
*format_double_size(double size
)
468 static gchar
*buf
= NULL
;
471 if (size
>= PRETTY_SIZE_LIMIT
)
475 if (size
>= PRETTY_SIZE_LIMIT
)
479 if (size
>= PRETTY_SIZE_LIMIT
)
499 buf
= g_strdup_printf("%.0f %s", size
, units
);
504 /* Ensure that str ends with a newline (or is empty) */
505 static void newline(GString
*str
)
507 if (str
->len
&& str
->str
[str
->len
- 1] != '\n')
508 g_string_append_c(str
, '\n');
511 /* Fork and exec argv. Wait and return the child's exit status.
513 * Returns the error string from the command if any, or NULL on success.
514 * If the process returns a non-zero exit status without producing a message,
515 * a suitable message is created.
516 * g_free() the result.
518 char *fork_exec_wait(const char **argv
)
526 errors
= g_string_new(NULL
);
528 child
= spawn_full(argv
, NULL
, &from_stderr
);
533 g_string_append(errors
, "fork: ");
534 g_string_append(errors
, g_strerror(errno
));
538 while (from_stderr
!= -1)
542 got
= read(from_stderr
, buffer
, sizeof(buffer
) - 1);
546 g_string_append(errors
, "read: ");
547 g_string_append(errors
, g_strerror(errno
));
552 g_string_append(errors
, buffer
);
557 if (waitpid(child
, &status
, 0) == -1)
562 g_string_append(errors
, "waitpid: ");
563 g_string_append(errors
, g_strerror(errno
));
569 if (!WIFEXITED(status
))
572 g_string_append(errors
, "(crashed?)");
574 else if (WEXITSTATUS(status
))
578 g_string_append(errors
, "ERROR");
585 if (from_stderr
!= -1)
588 if (errors
->len
&& errors
->str
[errors
->len
- 1] == '\n')
589 g_string_truncate(errors
, errors
->len
- 1);
593 char *retval
= errors
->str
;
594 g_string_free(errors
, FALSE
);
601 /* If a file has this UID and GID, which permissions apply to us?
602 * 0 = User, 1 = Group, 2 = World
604 gint
applicable(uid_t uid
, gid_t gid
)
614 for (i
= 0; i
< ngroups
; i
++)
616 if (supplemental_groups
[i
] == gid
)
623 /* Converts a file's mode to a string. Result is a pointer
624 * to a static buffer, valid until the next call.
626 char *pretty_permissions(mode_t m
)
628 static char buffer
[] = "rwx,rwx,rwx/UGT";
630 buffer
[0] = m
& S_IRUSR
? 'r' : '-';
631 buffer
[1] = m
& S_IWUSR
? 'w' : '-';
632 buffer
[2] = m
& S_IXUSR
? 'x' : '-';
634 buffer
[4] = m
& S_IRGRP
? 'r' : '-';
635 buffer
[5] = m
& S_IWGRP
? 'w' : '-';
636 buffer
[6] = m
& S_IXGRP
? 'x' : '-';
638 buffer
[8] = m
& S_IROTH
? 'r' : '-';
639 buffer
[9] = m
& S_IWOTH
? 'w' : '-';
640 buffer
[10] = m
& S_IXOTH
? 'x' : '-';
642 buffer
[12] = m
& S_ISUID
? 'U' : '-';
643 buffer
[13] = m
& S_ISGID
? 'G' : '-';
645 buffer
[14] = m
& S_ISVTX
? 'T' : '-';
654 /* Gets the canonical name for address and compares to our_host_name() */
655 static gboolean
is_local_address(char *address
)
659 ent
= gethostbyname(address
);
661 return strcmp(our_host_name(), ent
? ent
->h_name
: address
) == 0;
664 /* Convert a URI to a local pathname (or NULL if it isn't local).
665 * The returned pointer points inside the input string.
672 const char *get_local_path(const char *uri
)
676 char *path
, *uri_host
;
679 return uri
; /* Just a local path - no host part */
681 path
= strchr(uri
+ 2, '/');
683 return NULL
; /* //something */
686 return path
; /* ///path */
688 uri_host
= g_strndup(uri
+ 2, path
- uri
- 2);
689 if (is_local_address(uri_host
))
692 return path
; /* //myhost/path */
696 return NULL
; /* From a different host */
700 if (strncasecmp(uri
, "file:", 5))
701 return NULL
; /* Don't know this format */
706 return get_local_path(uri
);
712 /* Set the close-on-exec flag for this FD.
713 * TRUE means that an exec()'d process will not get the FD.
715 void close_on_exec(int fd
, gboolean close
)
717 if (fcntl(fd
, F_SETFD
, close
))
718 g_warning("fcntl() failed: %s\n", g_strerror(errno
));
721 void set_blocking(int fd
, gboolean blocking
)
723 if (fcntl(fd
, F_SETFL
, blocking
? 0 : O_NONBLOCK
))
724 g_warning("fcntl() failed: %s\n", g_strerror(errno
));
727 /* Format this time nicely. The result is a pointer to a static buffer,
728 * valid until the next call.
730 char *pretty_time(time_t *time
)
732 static char time_buf
[32];
734 if (strftime(time_buf
, sizeof(time_buf
),
735 TIME_FORMAT
, localtime(time
)) == 0)
742 # define O_NOFOLLOW 0x0
745 /* 'from' and 'to' are complete pathnames of files (not dirs or symlinks).
746 * This spawns 'cp' to do the copy if lstat() succeeds, otherwise we
747 * do the copy manually using vfs.
749 * Returns an error string, or NULL on success. g_free() the result.
751 * XXX: This was only used for libvfs...
753 guchar
*copy_file(const guchar
*from
, const guchar
*to
)
755 const char *argv
[] = {"cp", "-pRf", NULL
, NULL
, NULL
};
760 return fork_exec_wait(argv
);
763 /* 'word' has all special characters escaped so that it may be inserted
764 * into a shell command.
765 * Eg: 'My Dir?' becomes 'My\ Dir\?'. g_free() the result.
767 guchar
*shell_escape(const guchar
*word
)
772 tmp
= g_string_new(NULL
);
776 if (strchr(" ?*['\"$~\\|();!`&", *word
))
777 g_string_append_c(tmp
, '\\');
778 g_string_append_c(tmp
, *word
);
783 g_string_free(tmp
, FALSE
);
787 /* TRUE iff `sub' is (or would be) an object inside the directory `parent',
788 * (or the two are the same item/directory).
789 * FALSE if parent doesn't exist.
791 gboolean
is_sub_dir(const char *sub_obj
, const char *parent
)
793 struct stat parent_info
;
796 if (mc_lstat(parent
, &parent_info
))
797 return FALSE
; /* Parent doesn't exist */
799 /* For checking Copy/Move operations do a realpath first on sub
800 * (the destination), since copying into a symlink is the same as
801 * copying into the thing it points to. Don't realpath 'parent' though;
802 * copying a symlink just makes a new symlink.
804 * When checking if an icon depends on a file (parent), use realpath on
805 * sub (the icon) too.
807 sub
= pathdup(sub_obj
);
814 if (mc_lstat(sub
, &info
) == 0)
816 if (info
.st_dev
== parent_info
.st_dev
&&
817 info
.st_ino
== parent_info
.st_ino
)
824 slash
= strrchr(sub
, '/');
843 /* True if the string 'list' contains 'item'.
844 * Eg ("close", "close, help") -> TRUE
846 gboolean
in_list(const guchar
*item
, const guchar
*list
)
854 if (strncmp(item
, list
, len
) == 0 && !isalpha(list
[len
]))
856 list
= strchr(list
, ',');
859 while (isspace(*++list
))
866 /* Split a path into its components. Eg:
868 * /bob/fred -> ["bob", "fred"]
869 * ///a//b// -> ["a", "b"]
872 * The array and the strings in it must be freed after use.
874 GPtrArray
*split_path(const guchar
*path
)
879 g_return_val_if_fail(path
!= NULL
, NULL
);
881 array
= g_ptr_array_new();
885 while (path
[0] == '/')
890 slash
= strchr(path
, '/');
893 g_ptr_array_add(array
, g_strndup(path
, slash
- path
));
897 g_ptr_array_add(array
, g_strdup(path
));
904 /* Return the shortest path from 'from' to 'to'.
905 * Eg: get_relative_path("/a/b/c", "a/d/e") -> "../d/e"
907 guchar
*get_relative_path(const guchar
*from
, const guchar
*to
)
911 GPtrArray
*src
, *dst
;
914 src
= split_path(from
);
915 dst
= split_path(to
);
917 /* The last component of src doesn't matter... */
920 g_free(src
->pdata
[src
->len
- 1]);
921 g_ptr_array_remove_index(src
, src
->len
- 1);
924 /* Strip off common path elements... */
926 while (i
< src
->len
&& i
< dst
->len
)
928 guchar
*a
= (guchar
*) src
->pdata
[i
];
929 guchar
*b
= (guchar
*) dst
->pdata
[i
];
931 if (strcmp(a
, b
) != 0)
936 /* Go up one dir for each element remaining in src */
937 path
= g_string_new(NULL
);
938 for (j
= i
; j
< src
->len
; j
++)
939 g_string_append(path
, "../");
941 /* Go down one dir for each element remaining in dst */
942 for (j
= i
; j
< dst
->len
; j
++)
944 g_string_append(path
, (guchar
*) dst
->pdata
[j
]);
945 g_string_append_c(path
, '/');
948 if (path
->str
[path
->len
- 1] == '/')
949 g_string_truncate(path
, path
->len
- 1);
951 g_string_assign(path
, ".");
953 /* Free the arrays */
954 for (i
= 0; i
< src
->len
; i
++)
955 g_free(src
->pdata
[i
]);
956 g_ptr_array_free(src
, TRUE
);
957 for (i
= 0; i
< dst
->len
; i
++)
958 g_free(dst
->pdata
[i
]);
959 g_ptr_array_free(dst
, TRUE
);
962 g_string_free(path
, FALSE
);
968 * Interperet text as a boolean value. Return defvalue if we don't
971 int text_to_boolean(const char *text
, int defvalue
)
973 if(g_strcasecmp(text
, "true")==0)
975 else if(g_strcasecmp(text
, "false")==0)
977 else if(g_strcasecmp(text
, "yes")==0)
979 else if(g_strcasecmp(text
, "no")==0)
981 else if(isdigit(text
[0]))
987 /* Return the pathname that this symlink points to.
988 * NULL on error (not a symlink, path too long) and errno set.
989 * g_free() the result.
991 char *readlink_dup(const char *source
)
993 char path
[MAXPATHLEN
+ 1];
996 got
= readlink(source
, path
, MAXPATHLEN
);
997 if (got
< 0 || got
> MAXPATHLEN
)
1000 return g_strndup(path
, got
);
1004 * This code implements the MD5 message-digest algorithm.
1005 * The algorithm is due to Ron Rivest. The original code was
1006 * written by Colin Plumb in 1993, and put in the public domain.
1008 * Modified to use glib datatypes. Put under GPL to simplify
1009 * licensing for ROX-Filer. Taken from Debian's dpkg package.
1012 #define md5byte unsigned char
1014 typedef struct _MD5Context MD5Context
;
1016 struct _MD5Context
{
1022 #if G_BYTE_ORDER == G_BIG_ENDIAN
1023 void byteSwap(guint32
*buf
, unsigned words
)
1025 md5byte
*p
= (md5byte
*)buf
;
1028 *buf
++ = (guint32
)((unsigned)p
[3] << 8 | p
[2]) << 16 |
1029 ((unsigned)p
[1] << 8 | p
[0]);
1034 #define byteSwap(buf,words)
1038 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
1039 * initialization constants.
1041 static void MD5Init(MD5Context
*ctx
)
1043 ctx
->buf
[0] = 0x67452301;
1044 ctx
->buf
[1] = 0xefcdab89;
1045 ctx
->buf
[2] = 0x98badcfe;
1046 ctx
->buf
[3] = 0x10325476;
1053 * Update context to reflect the concatenation of another buffer full
1056 static void MD5Update(MD5Context
*ctx
, md5byte
const *buf
, unsigned len
)
1060 /* Update byte count */
1063 if ((ctx
->bytes
[0] = t
+ len
) < t
)
1064 ctx
->bytes
[1]++; /* Carry from low to high */
1066 t
= 64 - (t
& 0x3f); /* Space available in ctx->in (at least 1) */
1068 memcpy((md5byte
*)ctx
->in
+ 64 - t
, buf
, len
);
1071 /* First chunk is an odd size */
1072 memcpy((md5byte
*)ctx
->in
+ 64 - t
, buf
, t
);
1073 byteSwap(ctx
->in
, 16);
1074 MD5Transform(ctx
->buf
, ctx
->in
);
1078 /* Process data in 64-byte chunks */
1080 memcpy(ctx
->in
, buf
, 64);
1081 byteSwap(ctx
->in
, 16);
1082 MD5Transform(ctx
->buf
, ctx
->in
);
1087 /* Handle any remaining bytes of data. */
1088 memcpy(ctx
->in
, buf
, len
);
1092 * Final wrapup - pad to 64-byte boundary with the bit pattern
1093 * 1 0* (64-bit count of bits processed, MSB-first)
1094 * Returns the newly allocated string of the hash.
1096 static char *MD5Final(MD5Context
*ctx
)
1100 int count
= ctx
->bytes
[0] & 0x3f; /* Number of bytes in ctx->in */
1101 md5byte
*p
= (md5byte
*)ctx
->in
+ count
;
1104 /* Set the first char of padding to 0x80. There is always room. */
1107 /* Bytes of padding needed to make 56 bytes (-8..55) */
1108 count
= 56 - 1 - count
;
1110 if (count
< 0) { /* Padding forces an extra block */
1111 memset(p
, 0, count
+ 8);
1112 byteSwap(ctx
->in
, 16);
1113 MD5Transform(ctx
->buf
, ctx
->in
);
1114 p
= (md5byte
*)ctx
->in
;
1117 memset(p
, 0, count
);
1118 byteSwap(ctx
->in
, 14);
1120 /* Append length in bits and transform */
1121 ctx
->in
[14] = ctx
->bytes
[0] << 3;
1122 ctx
->in
[15] = ctx
->bytes
[1] << 3 | ctx
->bytes
[0] >> 29;
1123 MD5Transform(ctx
->buf
, ctx
->in
);
1125 byteSwap(ctx
->buf
, 4);
1127 retval
= g_malloc(33);
1128 bytes
= (guint8
*) ctx
->buf
;
1129 for (i
= 0; i
< 16; i
++)
1130 sprintf(retval
+ (i
* 2), "%02x", bytes
[i
]);
1138 /* The four core functions - F1 is optimized somewhat */
1140 /* #define F1(x, y, z) (x & y | ~x & z) */
1141 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1142 #define F2(x, y, z) F1(z, x, y)
1143 #define F3(x, y, z) (x ^ y ^ z)
1144 #define F4(x, y, z) (y ^ (x | ~z))
1146 /* This is the central step in the MD5 algorithm. */
1147 #define MD5STEP(f,w,x,y,z,in,s) \
1148 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
1151 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1152 * reflect the addition of 16 longwords of new data. MD5Update blocks
1153 * the data and converts bytes into longwords for this routine.
1155 static void MD5Transform(guint32 buf
[4], guint32
const in
[16])
1157 register guint32 a
, b
, c
, d
;
1164 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
1165 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
1166 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
1167 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
1168 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
1169 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
1170 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
1171 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
1172 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
1173 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
1174 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
1175 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
1176 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
1177 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
1178 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
1179 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
1181 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
1182 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
1183 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
1184 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
1185 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
1186 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1187 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1188 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1189 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1190 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1191 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1192 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1193 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1194 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1195 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1196 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1198 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1199 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1200 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1201 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1202 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1203 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1204 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1205 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1206 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1207 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1208 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1209 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1210 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1211 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1212 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1213 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1215 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1216 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1217 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1218 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1219 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1220 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1221 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1222 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1223 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1224 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1225 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1226 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1227 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1228 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1229 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1230 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);
1238 # endif /* ASM_MD5 */
1240 char *md5_hash(const char *message
)
1245 MD5Update(&ctx
, message
, strlen(message
));
1246 return MD5Final(&ctx
);
1249 /* Convert string 'src' from the current locale to UTF-8 */
1250 gchar
*to_utf8(const gchar
*src
)
1257 retval
= g_locale_to_utf8(src
, -1, NULL
, NULL
, NULL
);
1259 return retval
? retval
: g_strdup(src
);
1262 /* Convert string 'src' to the current locale from UTF-8 */
1263 gchar
*from_utf8(const gchar
*src
)
1270 retval
= g_locale_from_utf8(src
, -1, NULL
, NULL
, NULL
);
1272 return retval
? retval
: g_strdup(src
);