From 774b63cd41b22f121fec4016fa90933f06289acc Mon Sep 17 00:00:00 2001
From: John Thacker <johnthacker@gmail.com>
Date: Tue, 14 Apr 2026 08:15:16 -0400
Subject: [PATCH] SMB2: Optimize PATTERN_V1 decompression

This is roughly 50x faster on a large decompression.

Ping #21191

AI-Assisted: no


(cherry picked from commit 0fe4d786017863c9496b7f88098796279dfa59aa)

Co-authored-by: John Thacker <johnthacker@gmail.com>
---
 epan/dissectors/packet-smb2.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

Index: wireshark-3.6.24/epan/dissectors/packet-smb2.c
===================================================================
--- wireshark-3.6.24.orig/epan/dissectors/packet-smb2.c
+++ wireshark-3.6.24/epan/dissectors/packet-smb2.c
@@ -10315,8 +10315,24 @@ dissect_smb2_compression_pattern_v1(prot
 	if (out && *ok) {
 		guint8 v = (guint8)pattern;
 
-		for (guint i = 0; i < times; i++)
+		/* Both of these are much faster than adding one byte at a,
+		 * time though the second is somewhat faster in testing due
+		 * to how compilers optimize a known length memset. */
+#if 0
+		guint8 *bytes = g_malloc(times);
+		memset(bytes, v, times);
+		wmem_array_append(out, bytes, times);
+		g_free(bytes);
+#else
+		guint8 sixty_four_bytes[64];
+		memset(sixty_four_bytes, v, 64);
+		unsigned i = 0;
+		for (i = 0; i < times/64; i++)
+			wmem_array_append(out, sixty_four_bytes, 64);
+
+		for (i *= 64; i < times; i++)
 			wmem_array_append(out, &v, 1);
+#endif
 	}
 
 	return offset;
