diff src/util.c @ 4853:fbfdacf7c611

[gaim-migrate @ 5180] This is better auto-getting of icons for oscar. Ethan made me write a tobase16 fucntion and a from16 function (and he done gave me some good advice for that, too) which convert to and from arbitrary data and a null terminated string of hex. I use these to get and store the md5 checksum for each icon sent to you. This way, Gaim will request the icon when the md5sum differs. Er, so the md5sum is stored in blist.xml. Previously, Gaim would only request the icon if you did not have any buddy icon for the other person. Now it will request it if the local md5sum differs from the server md5sum. To sum it up again, if their icon changes, gaim will request the new one. I tried using the base64 functions, but they look like they want to work with null terminated strings, rather than arbitrary data. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sat, 22 Mar 2003 07:29:34 +0000
parents 0ed37c803503
children 912294585edf
line wrap: on
line diff
--- a/src/util.c	Sat Mar 22 06:11:12 2003 +0000
+++ b/src/util.c	Sat Mar 22 07:29:34 2003 +0000
@@ -466,6 +466,59 @@
 		*size = len;
 }
 
+/*
+ * Converts raw data to a pretty, null-terminated base16 string.
+ */
+char *tobase16(const char *data, int length)
+{
+	int i;
+	char *ascii = NULL;
+
+	if (!data || !length)
+		return NULL;
+
+	ascii = (char *)malloc(length*2 + 1);
+
+	for (i=0; i<length; i++)
+		snprintf(&ascii[i*2], 3, "%02hhx", data[i]);
+
+	return ascii;
+}
+
+/*
+ * Converts a null-terminated string of hexidecimal to raw data.
+ */
+int frombase16(const char *ascii, char **raw)
+{
+	int len, i, accumulator;
+	char *data;
+
+	if (!ascii || !(len = strlen(ascii)) || (len % 2))
+		return 0;
+
+	data = (char *)malloc((len/2)*sizeof(char));
+	for (i=0; i<len; i++) {
+		if (!(i % 2))
+			accumulator = 0;
+		else
+			accumulator = accumulator << 4;
+		if (isdigit(ascii[i]))
+			accumulator |= ascii[i]-48;
+		else switch(ascii[i]) {
+			case 'a':  case 'A':  accumulator|=10;  break;
+			case 'b':  case 'B':  accumulator|=11;  break;
+			case 'c':  case 'C':  accumulator|=12;  break;
+			case 'd':  case 'D':  accumulator|=13;  break;
+			case 'e':  case 'E':  accumulator|=14;  break;
+			case 'f':  case 'F':  accumulator|=15;  break;
+		}
+		if (i % 2)
+			data[(i-1)/2] = accumulator;
+	}
+
+	*raw = data;
+	return len/2;
+}
 
 char *normalize(const char *s)
 {