From 873908db36a23afa01c831add54adcdfbb1aa158 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Fri, 25 Apr 2025 18:22:52 -0700
Subject: [PATCH] Create smb_msg_is_ascii()

This function returns true if the message *header* explicitly claims that the
message contains only US-ASCII chars.
---
 src/smblib/smblib.c | 14 ++++++++++++++
 src/smblib/smblib.h |  1 +
 2 files changed, 15 insertions(+)

diff --git a/src/smblib/smblib.c b/src/smblib/smblib.c
index c7e3226633..d5e7e830e7 100644
--- a/src/smblib/smblib.c
+++ b/src/smblib/smblib.c
@@ -1801,6 +1801,20 @@ bool smb_msg_is_utf8(const smbmsg_t* msg)
 	return msg->text_charset != NULL && stricmp(msg->text_charset, "utf-8") == 0;
 }
 
+bool smb_msg_is_ascii(const smbmsg_t* msg)
+{
+	for (int i = 0; i < msg->total_hfields; i++) {
+		switch (msg->hfield[i].type) {
+			case FIDOCTRL:
+				if (strncmp(msg->hfield_dat[i], "CHRS: ASCII", 11) == 0)
+					return true;
+		}
+	}
+	if (msg->ftn_charset != NULL && strncmp(msg->ftn_charset, "ASCII", 5) == 0)
+		return true;
+	return msg->text_charset != NULL && stricmp(msg->text_charset, "us-ascii") == 0;
+}
+
 uint16_t smb_voted_already(smb_t* smb, uint32_t msgnum, const char* name, enum smb_net_type net_type, void* net_addr)
 {
 	uint16_t votes = 0;
diff --git a/src/smblib/smblib.h b/src/smblib/smblib.h
index 572b78cafa..8f262b97c7 100644
--- a/src/smblib/smblib.h
+++ b/src/smblib/smblib.h
@@ -177,6 +177,7 @@ SMBEXPORT uint32_t  smb_last_in_thread(smb_t*, smbmsg_t*);
 SMBEXPORT size_t    smb_idxreclen(smb_t*);
 SMBEXPORT uint32_t  smb_count_idx_records(smb_t*, uint16_t mask, uint16_t cmp);
 SMBEXPORT bool      smb_msg_is_utf8(const smbmsg_t*);
+SMBEXPORT bool      smb_msg_is_ascii(const smbmsg_t*);
 SMBEXPORT size_t    smb_msg_count(smb_t*, unsigned types);
 SMBEXPORT enum smb_msg_type smb_msg_type(smb_msg_attr_t);
 
-- 
GitLab