When outputting MIB values for hex digit, the code will output a leading 0 for
values less than 16 if NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT is set. The attached
patch makes the following changes:

- only call netsnmp_ds_get_boolean once per string, not once per character.
  This is really a performance tweak, so I'm ok with leaving it out if people
  have objections. In that case, though, at the very least the (value < 16)
  check should be the first condition, so at least the functions is only
  called for values where we need the configuration check.
- only do leading 0 checks for a width of 1 (one byte). A single leading 0
  doesn't make sense (IMHO) for multibyte hex values
- default to a leading 0, regardless of NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT, for
  single byte hex output when there is no separator character and no hint from
  the MIB (or there is no more hint data and we are simply repeating the last
  known hint).

Some examples (assuming NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT is not set):

Value   Hint    Current output  Patched output
AA01BB  "1x"    AA1BB           AA01BB
AA01BB  "1x:"   AA:1:BB         AA:1:BB (no change)

01AA02  "1x"    1AA2            01AA02
01AA02  "1x:"   1:AA:2          1:AA:2 (no change)

01AA02  "1x:1x" 1:AA2           1:AA02
diff --git a/net-snmp/snmplib/mib.c b/net-snmp/snmplib/mib.c
index 00ccc58..761d3c1 100644
--- a/net-snmp/snmplib/mib.c
+++ b/net-snmp/snmplib/mib.c
@@ -465,6 +465,7 @@ sprint_realloc_octet_string(u_char ** buf, size_t * buf_len,
         int             repeat, width = 1;
         long            value;
         char            code = 'd', separ = 0, term = 0, ch, intbuf[16];
+        char            hex2digit = -1;
         u_char         *ecp;
 
         if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
@@ -511,9 +512,22 @@ sprint_realloc_octet_string(u_char ** buf, size_t * buf_len,
                 }
                 switch (code) {
                 case 'x':
-                    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
-                                               NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT)
-                                       && value < 16) {
+                    if ((char)-1 == hex2digit)
+                        hex2digit = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
+                                                           NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT);
+                    /*
+                     * if value is < 16, it will be a single hex digit. If the
+                     * width is 1 (we are outputting a byte at a time), pat it
+                     * to 2 digits if NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT is set
+                     * or all of the following are true:
+                     *  - we do not have a separation character
+                     *  - there is no hint left (or there never was a hint)
+                     *
+                     * e.g. for the data 0xAA01BB, would anyone really ever
+                     * want the string "AA1BB"??
+                     */
+                    if (((value < 16) && (1 == width)) &&
+                        (hex2digit || ((0 == separ) && (0 == *hint)))) {
                         sprintf(intbuf, "0%lx", value);
                     } else {
                         sprintf(intbuf, "%lx", value);
------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to