[PATCH / request for votes] Support double byte characters

Josef Ridky <[email protected]>
Newsgroups gmane.network.net-snmp.devel
Message-ID <[email protected]>
Net-SNMP isn't able to handle double byte characters like UTF-8.
Though it can handle single byte characters (US-ASCII characters) properly, UTF-8 and ShiftJIS characters are displays as "........."

Steps to Reproduce:
- Edit /etc/snmp/snmptrapd.conf and add or uncomment the following line.

authCommunity log,execute,net public

    Start snmptrapd service.

# systemctl start snmptrapd

    Run following command.

# snmptrap -v 2c -c public localhost  ''  .1.3.6.1.4.1.8072.2.1.3 .1.3.6.1.4.1.8072.2.1.3 s "データ"

    Check the /var/log/messages or journalctl

Following patch brings support for double byte characters (UTF-8). 
All comments are welcome.

---
diff -urNp old/snmplib/mib.c new/snmplib/mib.c
--- old/snmplib/mib.c	2017-10-18 09:47:39.345569965 +0200
+++ new/snmplib/mib.c	2017-10-18 14:05:11.237452328 +0200
@@ -370,7 +371,31 @@ sprint_realloc_hexstring(u_char ** buf,
     return 1;
 }
 
+/**
+ * Check, if given character contains value specific for UTF-8 encoding.
+ *
+ * @param cp       character to check
+ *
+ * @return 0 on failure, 2 for two-byte character, 3 for three-byte character
+ *         and 4 for four-byte character
+ */
+int
+isUTF8MultiByte(const u_char cp)
+{
+    if(cp >= 0xC2 && cp <= 0xDF){
+        return 2;
+    }
+    
+    if(cp >= 0xE0 && cp <= 0xEF){
+        return 3;
+    }
 
+    if(cp >= 0xF2 && cp <= 0xFF){
+        return 4;
+    }
+
+    return 0;
+}
 
 /**
  * Prints an ascii string into a buffer.
@@ -396,22 +421,36 @@ sprint_realloc_asciistring(u_char ** buf
                            size_t * out_len, int allow_realloc,
                            const u_char * cp, size_t len)
 {
-    int             i;
+    int             i, j, multibyte;
 
     for (i = 0; i < (int) len; i++) {
-        if (isprint(*cp) || isspace(*cp)) {
-            if (*cp == '\\' || *cp == '"') {
+        if (isprint(*cp) || isspace(*cp) || isUTF8MultiByte(*cp) > 0) {
+
+            multibyte = isUTF8MultiByte(*cp);
+
+            if(multibyte > 0){
+                for(j = 0; j < multibyte; j++, i++) {
+                    if ((*out_len >= *buf_len) &&
+                        !(allow_realloc && snmp_realloc(buf, buf_len))) {
+                        return 0;
+                    }
+                    *(*buf + (*out_len)++) = *cp++;
+                    
+                }
+            } else {
+                if (*cp == '\\' || *cp == '"') {
+                    if ((*out_len >= *buf_len) &&
+                        !(allow_realloc && snmp_realloc(buf, buf_len))) {
+                        return 0;
+                    }
+                    *(*buf + (*out_len)++) = '\\';
+                }
                 if ((*out_len >= *buf_len) &&
                     !(allow_realloc && snmp_realloc(buf, buf_len))) {
                     return 0;
                 }
-                *(*buf + (*out_len)++) = '\\';
-            }
-            if ((*out_len >= *buf_len) &&
-                !(allow_realloc && snmp_realloc(buf, buf_len))) {
-                return 0;
+                *(*buf + (*out_len)++) = *cp++;
             }
-            *(*buf + (*out_len)++) = *cp++;
         } else {
             if ((*out_len >= *buf_len) &&
                 !(allow_realloc && snmp_realloc(buf, buf_len))) {
---

Regards

Josef Ridky
Associate Software Engineer
Core Services Team
Red Hat Czech, s.r.o.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.