]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: Add message_part_is_equal
authorAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 9 Nov 2020 07:16:27 +0000 (09:16 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Wed, 11 Nov 2020 11:53:36 +0000 (11:53 +0000)
Returns TRUE if two parts are equal.

src/lib-mail/message-part.c
src/lib-mail/message-part.h

index c7be9a8f2b7b8742054a0e12ea4a8bb61e7ad7aa..340dbbb95a608cbf1628975575f673b28632a981 100644 (file)
@@ -59,3 +59,45 @@ message_part_by_idx(struct message_part *parts, unsigned int idx)
 
        return message_sub_part_by_idx(parts, idx);
 }
+
+bool message_part_is_equal(const struct message_part *p1,
+                          const struct message_part *p2)
+{
+       /* This cannot be p1 && p2, because then we would return
+          TRUE when either part is NULL, and we should return FALSE */
+       while (p1 != NULL || p2 != NULL) {
+               /* If either part is NULL, return false */
+               if ((p1 != NULL) != (p2 != NULL))
+                       return FALSE;
+
+               /* Expect that both either have children, or both
+                  do not have children */
+               if ((p1->children != NULL) != (p2->children != NULL))
+                       return FALSE;
+
+               /* If there are children, ensure they are equal */
+               if (p1->children != NULL) {
+                       if (!message_part_is_equal(p1->children, p2->children))
+                               return FALSE;
+               }
+
+               /* If any of these properties differ, then parts are not equal */
+               if (p1->physical_pos != p2->physical_pos ||
+                   p1->header_size.physical_size != p2->header_size.physical_size ||
+                   p1->header_size.virtual_size != p2->header_size.virtual_size ||
+                   p1->header_size.lines != p2->header_size.lines ||
+                   p1->body_size.physical_size != p2->body_size.physical_size ||
+                   p1->body_size.virtual_size != p2->body_size.virtual_size ||
+                   p1->body_size.lines != p2->body_size.lines ||
+                   p1->children_count != p2->children_count ||
+                   p1->flags != p2->flags)
+                       return FALSE;
+
+               /* Move forward */
+               p1 = p1->next;
+               p2 = p2->next;
+       }
+
+       /* Parts are equal */
+       return TRUE;
+}
index 36d9da9b6e0bd34c3356885f48fad07f57ee2edf..7f6afa374898a6dbd8ea00222c16adba403aaf9a 100644 (file)
@@ -50,4 +50,14 @@ unsigned int message_part_to_idx(const struct message_part *part);
 struct message_part *
 message_part_by_idx(struct message_part *parts, unsigned int idx);
 
+/* Returns TRUE when message parts are considered equal. Equality is determined
+   to be TRUE, when
+
+  - both parts are NULL
+  - both parts are not NULL, and
+    - both parts children are equal
+    - both parts have same position, sizes, line counts and flags. */
+bool message_part_is_equal(const struct message_part *p1,
+                          const struct message_part *p2) ATTR_NULL(1, 2);
+
 #endif