commit 5cb0023: [Minor] Core: Rework images processing API

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Jan 15 12:56:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-01-15 11:07:46 +0000
URL: https://github.com/rspamd/rspamd/commit/5cb0023612469ee88a2d86a6e6bfd8b44e830fef

[Minor] Core: Rework images processing API

---
 src/libmime/images.c | 63 +++++++++++++++++++++++++++++++---------------------
 src/libmime/images.h |  9 ++++++++
 src/libserver/html.c |  7 ++++++
 src/libserver/html.h |  5 +++++
 4 files changed, 59 insertions(+), 25 deletions(-)

diff --git a/src/libmime/images.c b/src/libmime/images.c
index c4d20b4ad..b9a54b1a0 100644
--- a/src/libmime/images.c
+++ b/src/libmime/images.c
@@ -92,14 +92,14 @@ detect_image_type (rspamd_ftok_t *data)
 
 
 static struct rspamd_image *
-process_png_image (struct rspamd_task *task, rspamd_ftok_t *data)
+process_png_image (rspamd_mempool_t *pool, rspamd_ftok_t *data)
 {
 	struct rspamd_image *img;
 	guint32 t;
 	const guint8 *p;
 
 	if (data->len < 24) {
-		msg_info_task ("bad png detected (maybe striped)");
+		msg_info_pool ("bad png detected (maybe striped)");
 		return NULL;
 	}
 
@@ -107,11 +107,11 @@ process_png_image (struct rspamd_task *task, rspamd_ftok_t *data)
 	/* Skip signature and read header section */
 	p = data->begin + 12;
 	if (memcmp (p, "IHDR", 4) != 0) {
-		msg_info_task ("png doesn't begins with IHDR section");
+		msg_info_pool ("png doesn't begins with IHDR section");
 		return NULL;
 	}
 
-	img = rspamd_mempool_alloc0 (task->task_pool, sizeof (struct rspamd_image));
+	img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image));
 	img->type = IMAGE_TYPE_PNG;
 	img->data = data;
 
@@ -126,13 +126,13 @@ process_png_image (struct rspamd_task *task, rspamd_ftok_t *data)
 }
 
 static struct rspamd_image *
-process_jpg_image (struct rspamd_task *task, rspamd_ftok_t *data)
+process_jpg_image (rspamd_mempool_t *pool, rspamd_ftok_t *data)
 {
 	const guint8 *p, *end;
 	guint16 h, w;
 	struct rspamd_image *img;
 
-	img = rspamd_mempool_alloc0 (task->task_pool, sizeof (struct rspamd_image));
+	img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image));
 	img->type = IMAGE_TYPE_JPG;
 	img->data = data;
 
@@ -169,18 +169,18 @@ process_jpg_image (struct rspamd_task *task, rspamd_ftok_t *data)
 }
 
 static struct rspamd_image *
-process_gif_image (struct rspamd_task *task, rspamd_ftok_t *data)
+process_gif_image (rspamd_mempool_t *pool, rspamd_ftok_t *data)
 {
 	struct rspamd_image *img;
 	const guint8 *p;
 	guint16 t;
 
 	if (data->len < 10) {
-		msg_info_task ("bad gif detected (maybe striped)");
+		msg_info_pool ("bad gif detected (maybe striped)");
 		return NULL;
 	}
 
-	img = rspamd_mempool_alloc0 (task->task_pool, sizeof (struct rspamd_image));
+	img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image));
 	img->type = IMAGE_TYPE_GIF;
 	img->data = data;
 
@@ -194,18 +194,18 @@ process_gif_image (struct rspamd_task *task, rspamd_ftok_t *data)
 }
 
 static struct rspamd_image *
-process_bmp_image (struct rspamd_task *task, rspamd_ftok_t *data)
+process_bmp_image (rspamd_mempool_t *pool, rspamd_ftok_t *data)
 {
 	struct rspamd_image *img;
 	gint32 t;
 	const guint8 *p;
 
 	if (data->len < 28) {
-		msg_info_task ("bad bmp detected (maybe striped)");
+		msg_info_pool ("bad bmp detected (maybe striped)");
 		return NULL;
 	}
 
-	img = rspamd_mempool_alloc0 (task->task_pool, sizeof (struct rspamd_image));
+	img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image));
 	img->type = IMAGE_TYPE_BMP;
 	img->data = data;
 	p = data->begin + 18;
@@ -558,31 +558,26 @@ rspamd_image_normalize (struct rspamd_task *task, struct rspamd_image *img)
 #endif
 }
 
-static void
-process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
+struct rspamd_image*
+rspamd_maybe_process_image (rspamd_mempool_t *pool,
+							rspamd_ftok_t *data)
 {
 	enum rspamd_image_type type;
 	struct rspamd_image *img = NULL;
-	struct rspamd_mime_header *rh;
-	struct rspamd_mime_text_part *tp;
-	struct html_image *himg;
-	const gchar *cid, *html_cid;
-	guint cid_len, i, j;
-	GPtrArray *ar;
 
-	if ((type = detect_image_type (&part->parsed_data)) != IMAGE_TYPE_UNKNOWN) {
+	if ((type = detect_image_type (data)) != IMAGE_TYPE_UNKNOWN) {
 		switch (type) {
 		case IMAGE_TYPE_PNG:
-			img = process_png_image (task, &part->parsed_data);
+			img = process_png_image (pool, data);
 			break;
 		case IMAGE_TYPE_JPG:
-			img = process_jpg_image (task, &part->parsed_data);
+			img = process_jpg_image (pool, data);
 			break;
 		case IMAGE_TYPE_GIF:
-			img = process_gif_image (task, &part->parsed_data);
+			img = process_gif_image (pool, data);
 			break;
 		case IMAGE_TYPE_BMP:
-			img = process_bmp_image (task, &part->parsed_data);
+			img = process_bmp_image (pool, data);
 			break;
 		default:
 			img = NULL;
@@ -590,6 +585,23 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
 		}
 	}
 
+	return img;
+}
+
+static void
+process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
+{
+	struct rspamd_mime_header *rh;
+	struct rspamd_mime_text_part *tp;
+	struct html_image *himg;
+	const gchar *cid, *html_cid;
+	guint cid_len, i, j;
+	GPtrArray *ar;
+	struct rspamd_image *img;
+
+
+	img = rspamd_maybe_process_image (task->task_pool, &part->parsed_data);
+
 	if (img != NULL) {
 		debug_task ("detected %s image of size %ud x %ud in message <%s>",
 			rspamd_image_type_str (img->type),
@@ -643,6 +655,7 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
 								if (strlen (html_cid) == cid_len &&
 										memcmp (html_cid, cid, cid_len) == 0) {
 									img->html_image = himg;
+									himg->embedded_image = img;
 
 									debug_task ("found linked image by cid: <%s>",
 											cid);
diff --git a/src/libmime/images.h b/src/libmime/images.h
index 55aa6873f..39a7dc8c5 100644
--- a/src/libmime/images.h
+++ b/src/libmime/images.h
@@ -35,6 +35,15 @@ struct rspamd_image {
  */
 void rspamd_images_process (struct rspamd_task *task);
 
+/**
+ * Processes image in raw data
+ * @param task
+ * @param data
+ * @return
+ */
+struct rspamd_image* rspamd_maybe_process_image (rspamd_mempool_t *pool,
+		rspamd_ftok_t *data);
+
 /*
  * Get textual representation of an image's type
  */
diff --git a/src/libserver/html.c b/src/libserver/html.c
index 8ebb3862e..9bf15e0a2 100644
--- a/src/libserver/html.c
+++ b/src/libserver/html.c
@@ -1512,6 +1512,13 @@ rspamd_html_process_img_tag (rspamd_mempool_t *pool, struct html_tag *tag,
 				/* We have an embedded image */
 				img->flags |= RSPAMD_HTML_FLAG_IMAGE_EMBEDDED;
 			}
+			if (comp->len > sizeof ("data:") - 1 && memcmp (comp->start,
+					"data:", sizeof ("data:") - 1) == 0) {
+				/* We have an embedded image in HTML tag */
+				img->flags |=
+						(RSPAMD_HTML_FLAG_IMAGE_EMBEDDED|RSPAMD_HTML_FLAG_IMAGE_DATA);
+
+			}
 			else {
 				img->flags |= RSPAMD_HTML_FLAG_IMAGE_EXTERNAL;
 				if (img->src) {
diff --git a/src/libserver/html.h b/src/libserver/html.h
index c5a989f0e..0400672ac 100644
--- a/src/libserver/html.h
+++ b/src/libserver/html.h
@@ -24,6 +24,7 @@
  */
 #define RSPAMD_HTML_FLAG_IMAGE_EMBEDDED (1 << 0)
 #define RSPAMD_HTML_FLAG_IMAGE_EXTERNAL (1 << 1)
+#define RSPAMD_HTML_FLAG_IMAGE_DATA (1 << 2)
 
 enum html_component_type {
 	RSPAMD_HTML_COMPONENT_NAME = 0,
@@ -43,12 +44,16 @@ struct html_tag_component {
 	const guchar *start;
 };
 
+
+struct rspamd_image;
+
 struct html_image {
 	guint height;
 	guint width;
 	guint flags;
 	gchar *src;
 	struct rspamd_url *url;
+	struct rspamd_image *embedded_image;
 	struct html_tag *tag;
 };
 


More information about the Commits mailing list