[Commits] utils.c 1.9 1.10 utils.h 1.8 1.9

miras at claws-mail.org miras at claws-mail.org
Tue Dec 13 11:37:16 CET 2011


Update of /home/claws-mail/contacts/src
In directory claws-mail:/tmp/cvs-serv20680/src

Modified Files:
	utils.c utils.h 
Log Message:
2011-12-13 [mir]	0.6.0cvs40

	* plugins/ldap/ldap-plugin.c
	* src/utils.c
	* src/utils.h
	    Fix error when saving images as binary data.
	    RFC 4510 was somewhat vague in defining the
	    storage format for binary image data which
	    I wrongly interpreted in that way that binary
	    image data was to be stored base64 encoded.
	    Infact LDAP expects the raw image data and
	    will internally do the base64 encoding when
	    appropriate.

Index: utils.c
===================================================================
RCS file: /home/claws-mail/contacts/src/utils.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- utils.c	29 Nov 2011 07:53:27 -0000	1.9
+++ utils.c	13 Dec 2011 10:37:14 -0000	1.10
@@ -40,6 +40,9 @@
 #include <gtk/gtk.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netdb.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -2035,4 +2038,135 @@
 	}
 		
 	return res;
+}
+
+gchar* get_domain_name(void) {
+	struct addrinfo hints, *result, *rp;
+	int s;
+	gchar *dn = NULL, *pos, hostname[BUFSIZ];
+	
+	memset(&hostname, 0, BUFSIZ);
+	gethostname(hostname, 1023);
+	
+	memset(&hints, 0, sizeof(struct addrinfo));
+	hints.ai_family = AF_UNSPEC; 
+	hints.ai_socktype = SOCK_STREAM;
+	hints.ai_flags = AI_CANONNAME;
+	
+	s = getaddrinfo(hostname, "http", &hints, &result);
+	if (s != 0) {
+		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
+		exit(EXIT_FAILURE);
+	}
+	
+	for (rp = result; rp != NULL; rp = rp->ai_next) {
+		if (rp->ai_canonname) {
+			if ((pos = strchr(rp->ai_canonname, '.')) != NULL) {
+				dn = g_strdup(++pos);
+			}
+			else
+				dn = g_strdup(rp->ai_canonname);
+			break;
+		}
+	}
+	freeaddrinfo(result);
+	
+	return dn;
+}
+
+gchar* base64_pixbuf_to_jpeg(gchar* base64_png, gsize* length) {
+	gchar *picture = NULL, *buffer;
+	GdkPixbufLoader* pl;
+	GdkPixbuf* pixbuf;
+	GError *err = NULL;
+	guchar* image_buf;
+	gsize len;
+	
+	image_buf = g_base64_decode(base64_png, &len);
+	
+	pl = gdk_pixbuf_loader_new();
+
+	gdk_pixbuf_loader_write(pl, image_buf, len, &err); 
+	g_free(image_buf);
+
+	if (err) {
+		g_critical("Load: %s", err->message);
+		g_clear_error(&err); 
+		return NULL; 
+	} 
+	gdk_pixbuf_loader_close(pl, NULL);
+	
+	pixbuf = gdk_pixbuf_loader_get_pixbuf(pl);
+	GdkPixbufFormat* format = gdk_pixbuf_loader_get_format(pl);
+	gchar* name = gdk_pixbuf_format_get_name(format);
+	if (name && strcasecmp(name, "jpeg") == 0) {
+		if (gdk_pixbuf_save_to_buffer(pixbuf, &buffer, length, "jpeg", &err, "quality", "100", NULL)) {
+			picture = g_base64_encode((const guchar *) buffer, *length);
+			g_free(buffer);
+		}
+		else
+			picture = NULL;
+	}
+	else {
+		if (gdk_pixbuf_save_to_buffer(pixbuf, &buffer, length, name, &err, NULL)) {
+			picture = g_base64_encode((const guchar *) buffer, *length);
+			g_free(buffer);
+		}
+		else
+			picture = NULL;
+	}
+	if (! picture) {
+		g_critical("Write: %s", err->message);
+		g_clear_error(&err);
+	}
+	g_object_unref(pl);
+	
+	return picture;
+}
+
+gchar* jpeg_to_png_base64(guchar* jpeg, gsize length) {
+	gchar* base64 = NULL, *buffer;
+	GdkPixbufLoader* pl;
+	GdkPixbuf* pixbuf;
+	guchar* image_buf;
+	GError *err = NULL;
+	gsize len;
+/*	gint len1 = 0;
+	guint len2 = 0;
+	pl = gdk_pixbuf_loader_new_with_type("jpeg", &err);
+	if (err) {
+		g_critical("Init: %s", err->message);
+		g_clear_error(&err);
+		return NULL;
+	}*/
+	image_buf = g_new0(guchar, length * 3 / 4 );
+	//image_buf = g_base64_decode((gchar *) jpeg, &len);
+	//g_base64_decode_step((gchar *) jpeg, length, image_buf, &len1, &len2);
+	pl = gdk_pixbuf_loader_new();
+
+	//gdk_pixbuf_loader_write(pl, image_buf, length, &err);
+	gdk_pixbuf_loader_write(pl, jpeg, length, &err); 
+	g_free(image_buf);
+	if (err) {
+		g_critical("Load: %s", err->message);
+		g_clear_error(&err); 
+		return NULL; 
+	} 
+	gdk_pixbuf_loader_close(pl, NULL);
+	
+	pixbuf = gdk_pixbuf_loader_get_pixbuf(pl);
+	GdkPixbufFormat* format = gdk_pixbuf_loader_get_format(pl);
+	if (gdk_pixbuf_save_to_buffer(
+			pixbuf, &buffer, &len, gdk_pixbuf_format_get_name(format), &err, NULL)) {
+		base64 = g_base64_encode((const guchar *) buffer, len);
+		g_free(buffer);
+	}
+	else {
+		g_critical("Write: %s", err->message);
+		g_clear_error(&err);
+	}
+	
+	g_object_unref(pl);
+	
+	return base64;
 }
\ No newline at end of file

Index: utils.h
===================================================================
RCS file: /home/claws-mail/contacts/src/utils.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- utils.h	28 Nov 2011 21:45:28 -0000	1.8
+++ utils.h	13 Dec 2011 10:37:14 -0000	1.9
@@ -167,6 +167,9 @@
 gboolean email_compare_values(GSList* a, GSList* b, gboolean is_and);
 gchar* create_dummy_dn(const gchar* baseDN);
 void contact_change_free(gpointer contact_change);
+gchar* get_domain_name(void);
+gchar* base64_pixbuf_to_jpeg(gchar* base64_png, gsize* length);
+gchar* jpeg_to_png_base64(guchar* jpeg, gsize length);
 
 G_END_DECLS
 



More information about the Commits mailing list