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;
+ const gchar *cid;
+ guint cid_len, i;
struct rspamd_image *img;
img = (struct rspamd_image *)part->specific.img;
}
PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, tp) {
- if (IS_TEXT_PART_HTML (tp) && tp->html != NULL &&
- tp->html->images != NULL) {
- for (j = 0; j < tp->html->images->len; j ++) {
- himg = g_ptr_array_index (tp->html->images, j);
-
- if ((himg->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED) &&
- himg->src) {
- html_cid = himg->src;
-
- if (strncmp (html_cid, "cid:", 4) == 0) {
- html_cid += 4;
- }
-
- if (strlen (html_cid) == cid_len &&
- memcmp (html_cid, cid, cid_len) == 0) {
- img->html_image = himg;
- himg->embedded_image = img;
-
- msg_debug_images ("found linked image by cid: <%s>",
- cid);
-
- if (himg->height == 0) {
- himg->height = img->height;
- }
-
- if (himg->width == 0) {
- himg->width = img->width;
- }
- }
+ if (IS_TEXT_PART_HTML (tp) && tp->html != NULL) {
+ himg = rspamd_html_find_embedded_image(tp->html, cid, cid_len);
+
+ if (himg != NULL) {
+ img->html_image = himg;
+ himg->embedded_image = img;
+
+ msg_debug_images ("found linked image by cid: <%s>",
+ cid);
+
+ if (himg->height == 0) {
+ himg->height = img->height;
+ }
+
+ if (himg->width == 0) {
+ himg->width = img->width;
}
}
}
return hc;
}
+static auto
+html_find_image_by_cid(const html_content &hc, std::string_view cid)
+ -> std::optional<const html_image *>
+{
+ for (const auto *html_image : hc.images) {
+ /* Filter embedded images */
+ if (html_image->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED &&
+ html_image->src != nullptr) {
+ if (cid == html_image->src) {
+ return html_image;
+ }
+ }
+ }
+
+ return std::nullopt;
+}
+
}
void *
}
return tag->name.data();
+}
+
+struct html_image*
+rspamd_html_find_embedded_image(void *html_content,
+ const char *cid, gsize cid_len)
+{
+ auto *hc = rspamd::html::html_content::from_ptr(html_content);
+
+ auto maybe_img = rspamd::html::html_find_image_by_cid(*hc, {cid, cid_len});
+
+ if (maybe_img) {
+ return (html_image *)maybe_img.value();
+ }
+
+ return nullptr;
}
\ No newline at end of file