Get rid of TestBigEndian and AC_C_BIGENDIAN.
[metze/wireshark/wip.git] / epan / oids.c
index 64aa406da36591ecc84dc594657a3471b2a9a75e..28ea769d8352c2852e84d4d6446e43f3c7fa46a8 100644 (file)
@@ -7,19 +7,7 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "config.h"
@@ -28,7 +16,7 @@
 #include <stdio.h>
 #include <string.h>
 
-#include <wsutil/report_err.h>
+#include <wsutil/report_message.h>
 
 #include "wmem/wmem.h"
 #include "uat.h"
@@ -37,6 +25,7 @@
 #include "packet.h"
 #include "wsutil/filesystem.h"
 #include "dissectors/packet-ber.h"
+#include <wsutil/ws_printf.h> /* ws_debug_printf */
 
 #ifdef HAVE_LIBSMI
 #include <smi.h>
@@ -46,7 +35,7 @@ static gboolean load_smi_modules = FALSE;
 static gboolean suppress_smi_errors = FALSE;
 #endif
 
-#define D(level,args) do if (debuglevel >= level) { printf args; printf("\n"); fflush(stdout); } while(0)
+#define D(level,args) do if (debuglevel >= level) { ws_debug_printf args; ws_debug_printf("\n"); fflush(stdout); } while(0)
 
 #include "oids.h"
 
@@ -204,7 +193,7 @@ extern void oid_add_from_encoded(const char* name, const guint8 *oid, gint oid_l
                add_oid(name,OID_KIND_UNKNOWN,NULL,NULL,subids_len,subids);
                wmem_free(NULL, sub);
        } else {
-               gchar* bytestr = (gchar*)bytestring_to_str(NULL, oid, oid_len, ':');
+               gchar* bytestr = bytestring_to_str(NULL, oid, oid_len, ':');
                D(1,("Failed to add Oid: %s [%d]%s ",name?name:"NULL", oid_len, bytestr));
                wmem_free(NULL, bytestr);
        }
@@ -437,7 +426,7 @@ static inline oid_kind_t smikind(SmiNode* sN, oid_key_t** key_p) {
 
                                oid1 = smiRenderOID(sN->oidlen, sN->oid, SMI_RENDER_QUALIFIED);
                                oid2 = smiRenderOID(elNode->oidlen, elNode->oid, SMI_RENDER_NAME);
-                               k->name = g_strdup_printf("%s.%s", oid1, oid2);
+                               k->name = g_strconcat(oid1, ".", oid2, NULL);
                                smi_free (oid1);
                                smi_free (oid2);
 
@@ -635,24 +624,28 @@ static void register_mibs(void) {
                        if ( typedata && oid_data->value_hfid == -2 ) {
                                SmiNamedNumber* smiEnum;
                                hf_register_info hf;
+                               char *name;
+                               char *blurb;
+
+                               name = g_strdup(oid_data->name);
+                               blurb = smiRenderOID(smiNode->oidlen, smiNode->oid, SMI_RENDER_ALL);
+                               /* Don't allow duplicate blurb/name */
+                               if (strcmp(blurb, name) == 0) {
+                                       smi_free(blurb);
+                                       blurb = NULL;
+                               }
 
                                hf.p_id                     = &(oid_data->value_hfid);
-                               hf.hfinfo.name              = g_strdup(oid_data->name);
+                               hf.hfinfo.name              = name;
                                hf.hfinfo.abbrev            = alnumerize(oid_data->name);
                                hf.hfinfo.type              = typedata->ft_type;
                                hf.hfinfo.display           = typedata->display;
                                hf.hfinfo.strings           = NULL;
                                hf.hfinfo.bitmask           = 0;
-                               hf.hfinfo.blurb             = smiRenderOID(smiNode->oidlen, smiNode->oid, SMI_RENDER_ALL);
+                               hf.hfinfo.blurb             = blurb;
                                /* HFILL */
                                HFILL_INIT(hf);
 
-                               /* Don't allow duplicate blurb/name */
-                               if (strcmp(hf.hfinfo.blurb, hf.hfinfo.name) == 0) {
-                                       smi_free((void *) hf.hfinfo.blurb);
-                                       hf.hfinfo.blurb = NULL;
-                               }
-
                                oid_data->value_hfid = -1;
 
                                if ( IS_ENUMABLE(hf.hfinfo.type) && (smiEnum = smiGetFirstNamedNumber(smiType))) {
@@ -696,8 +689,8 @@ static void register_mibs(void) {
                                        bits->data[n].hfid = -1;
                                        bits->data[n].offset = smiEnum->value.value.integer32 / 8;
 
-                                       hf2.hfinfo.name = g_strdup_printf("%s:%s",oid_data->name,smiEnum->name);
-                                       hf2.hfinfo.abbrev = g_strdup_printf("%s.%s",base,ext);
+                                       hf2.hfinfo.name = g_strconcat("%s:%s",oid_data->name, ":", smiEnum->name, NULL);
+                                       hf2.hfinfo.abbrev = g_strconcat(base, ".", ext, NULL);
 
                                        g_free(base);
                                        g_free(ext);
@@ -709,27 +702,25 @@ static void register_mibs(void) {
 
                        if ((key = oid_data->key)) {
                                for(; key; key = key->next) {
-                                       hf_register_info hf;
-
-                                       hf.p_id                     = &(key->hfid);
-                                       hf.hfinfo.name              = key->name;
-                                       hf.hfinfo.abbrev            = alnumerize(key->name);
-                                       hf.hfinfo.type              = key->ft_type;
-                                       hf.hfinfo.display           = key->display;
-                                       hf.hfinfo.strings           = NULL;
-                                       hf.hfinfo.bitmask           = 0;
-                                       hf.hfinfo.blurb             = NULL;
-                                       /* HFILL */
-                                       HFILL_INIT(hf);
-
                                        D(5,("\t\t\tIndex: name=%s subids=%u key_type=%d",
                                                 key->name, key->num_subids, key->key_type ));
 
                                        if (key->hfid == -2) {
+                                               hf_register_info hf;
+
+                                               hf.p_id                     = &(key->hfid);
+                                               hf.hfinfo.name              = key->name;
+                                               hf.hfinfo.abbrev            = alnumerize(key->name);
+                                               hf.hfinfo.type              = key->ft_type;
+                                               hf.hfinfo.display           = key->display;
+                                               hf.hfinfo.strings           = NULL;
+                                               hf.hfinfo.bitmask           = 0;
+                                               hf.hfinfo.blurb             = NULL;
+                                               /* HFILL */
+                                               HFILL_INIT(hf);
+
                                                wmem_array_append_one(hfa,hf);
                                                key->hfid = -1;
-                                       } else {
-                                               g_free((void*)hf.hfinfo.abbrev);
                                        }
                                }
                        }
@@ -760,12 +751,16 @@ void oid_pref_init(module_t *nameres)
 
     prefs_register_bool_preference(nameres, "load_smi_modules",
                                   "Enable OID resolution",
-                                  "You must restart Wireshark for this change to take effect",
+                                  "Resolve Object IDs to object names from the MIB and PIB"
+                                  " modules defined below."
+                                  " You must restart Wireshark for this change to take effect",
                                   &load_smi_modules);
 
     prefs_register_bool_preference(nameres, "suppress_smi_errors",
                                   "Suppress SMI errors",
-                                  "Some errors can be ignored. If unsure, set to false.",
+                                  "While loading MIB or PIB modules errors may be detected,"
+                                  " which are reported. Some errors can be ignored."
+                                  " If unsure, set to false.",
                                   &suppress_smi_errors);
 
     smi_paths_uat = uat_new("SMI Paths",
@@ -787,13 +782,14 @@ void oid_pref_init(module_t *nameres)
                             NULL,
                             smi_mod_free_cb,
                             restart_needed_warning,
+                            NULL,
                             smi_paths_fields);
 
     prefs_register_uat_preference(nameres,
                                   "smi_paths",
                                   "SMI (MIB and PIB) paths",
-                                  "Search paths for SMI (MIB and PIB) modules. You must\n"
-                                  "restart Wireshark for these changes to take effect.",
+                                  "Search paths for SMI (MIB and PIB) modules. You must"
+                                  " restart Wireshark for these changes to take effect.",
                                   smi_paths_uat);
 
     smi_modules_uat = uat_new("SMI Modules",
@@ -815,13 +811,14 @@ void oid_pref_init(module_t *nameres)
                               NULL,
                               smi_mod_free_cb,
                               restart_needed_warning,
+                              NULL,
                               smi_fields);
 
     prefs_register_uat_preference(nameres,
                                   "smi_modules",
                                   "SMI (MIB and PIB) modules",
-                                  "List of enabled SMI (MIB and PIB) modules. You must\n"
-                                  "restart Wireshark for these changes to take effect.",
+                                  "List of SMI (MIB and PIB) modules to load. You must"
+                                  " restart Wireshark for these changes to take effect.",
                                   smi_modules_uat);
 
 #else
@@ -860,24 +857,27 @@ char* oid_subid2string(wmem_allocator_t *scope, guint32* subids, guint len) {
        return rel_oid_subid2string(scope, subids, len, TRUE);
 }
 char* rel_oid_subid2string(wmem_allocator_t *scope, guint32* subids, guint len, gboolean is_absolute) {
-       char *s, *w;
+
+       wmem_strbuf_t *oid_str;
+       gsize oid_str_len;
 
        if(!subids || len == 0)
                return wmem_strdup(scope, "*** Empty OID ***");
 
-       s = (char *)wmem_alloc0(scope, ((len)*11)+2);
-       w = s;
+       oid_str = wmem_strbuf_new(scope, "");
 
        if (!is_absolute)
-               *w++ = '.';
+               wmem_strbuf_append_c(oid_str, '.');
 
        do {
-               w += g_snprintf(w,12,"%u.",*subids++);
+               wmem_strbuf_append_printf(oid_str, "%u.",*subids++);
        } while(--len);
 
-       if (w!=s) *(w-1) = '\0'; else *(s) = '\0';
+       /* Remove trailing "." (which is guaranteed to be there) */
+       oid_str_len = wmem_strbuf_get_len(oid_str);
+       wmem_strbuf_truncate(oid_str, oid_str_len - 1);
 
-       return s;
+       return wmem_strbuf_finalize(oid_str);
 }
 
 static guint check_num_oid(const char* str) {
@@ -937,6 +937,7 @@ guint oid_string2subid(wmem_allocator_t *scope, const char* str, guint32** subid
                        subid += *r - '0';
 
                        if( subids >= subids_overflow ||  subid > 0xffffffff) {
+                               wmem_free(scope, *subids_p);
                                *subids_p=NULL;
                                return 0;
                        }
@@ -1122,9 +1123,13 @@ guint oid_subid2encoded(wmem_allocator_t *scope, guint subids_len, guint32* subi
                switch(len) {
                        default: *bytes_p=NULL; return 0;
                        case 5: *(b++) = ((subid & 0xF0000000) >> 28) | 0x80;
+                       /* FALL THROUGH */
                        case 4: *(b++) = ((subid & 0x0FE00000) >> 21) | 0x80;
+                       /* FALL THROUGH */
                        case 3: *(b++) = ((subid & 0x001FC000) >> 14) | 0x80;
+                       /* FALL THROUGH */
                        case 2: *(b++) = ((subid & 0x00003F80) >> 7)  | 0x80;
+                       /* FALL THROUGH */
                        case 1: *(b++) =   subid & 0x0000007F ; break;
                }
        }
@@ -1212,7 +1217,7 @@ gchar *oid_resolved(wmem_allocator_t *scope, guint32 num_subids, guint32* subids
                          *str1 = oid_subid2string(NULL, subids,matched),
                          *str2 = oid_subid2string(NULL, &(subids[matched]),left);
 
-               ret = wmem_strdup_printf(scope, "%s.%s", oid->name ? oid->name : str1, str2);
+               ret = wmem_strconcat(scope, oid->name ? oid->name : str1, ".", str2, NULL);
                wmem_free(NULL, str1);
                wmem_free(NULL, str2);
                return ret;