Revert part of 52896 and (for now) all of 52935. As Jeff pointed out,
[metze/wireshark/wip.git] / version_info.c
index 3b6a7afe2f5163d2e35205a30b2a3fd0e1cf99ed..73a833e651b20e4316b359e261294fb8b1146fb5 100644 (file)
@@ -22,9 +22,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #include <glib.h>
 
@@ -53,6 +51,7 @@
 
 #ifdef HAVE_OS_X_FRAMEWORKS
 #include <CoreFoundation/CoreFoundation.h>
+#include "cfutils.h"
 #endif
 
 #ifdef HAVE_LIBCAP
@@ -192,25 +191,16 @@ static char *
 get_string_from_dictionary(CFPropertyListRef dict, CFStringRef key)
 {
        CFStringRef cfstring;
-       CFIndex string_len;
-       char *string;
 
-       cfstring = CFDictionaryGetValue(dict, key);
+       cfstring = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)dict,
+           (const void *)key);
        if (cfstring == NULL)
                return NULL;
        if (CFGetTypeID(cfstring) != CFStringGetTypeID()) {
                /* It isn't a string.  Punt. */
                return NULL;
        }
-       string_len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstring),
-           kCFStringEncodingUTF8);
-       string = g_malloc(string_len + 1);
-       if (!CFStringGetCString(cfstring, string, string_len + 1,
-           kCFStringEncodingUTF8)) {
-               g_free(string);
-               return NULL;
-       }
-       return string;
+       return CFString_to_C_string(cfstring);
 }
 
 /*
@@ -220,11 +210,13 @@ get_string_from_dictionary(CFPropertyListRef dict, CFStringRef key)
 static gboolean
 get_os_x_version_info(GString *str)
 {
-       static const char system_version_plist_path[] =
+       static const UInt8 server_version_plist_path[] =
+           "/System/Library/CoreServices/ServerVersion.plist";
+       static const UInt8 system_version_plist_path[] =
            "/System/Library/CoreServices/SystemVersion.plist";
-       CFURLRef system_version_plist_file_url;
-       CFReadStreamRef system_version_plist_stream;
-       CFDictionaryRef system_version_dict;
+       CFURLRef version_plist_file_url;
+       CFReadStreamRef version_plist_stream;
+       CFDictionaryRef version_dict;
        char *string;
 
        /*
@@ -234,72 +226,101 @@ get_os_x_version_info(GString *str)
         * Alas, Gestalt() is deprecated in Mountain Lion, so the build
         * fails if you treat deprecation warnings as fatal.  I don't
         * know of any replacement API, so we fall back on reading
-        * /System/Library/CoreServices/SystemVersion.plist
+        * /System/Library/CoreServices/ServerVersion.plist if it
+        * exists, otherwise /System/Library/CoreServices/SystemVersion.plist,
         * and using ProductUserVisibleVersion.  We also get the build
-        * version from ProductBuildVersion.
-        *
-        * XXX - on OS X Server, do we need to read the server plist in
-        * /System/Library/CoreServices/ServerVersion.plist - i.e., if
-        * it exists, use it rather than SystemVersion.plist?
+        * version from ProductBuildVersion and the product name from
+        * ProductName.
         */
-       system_version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
-           system_version_plist_path, sizeof system_version_plist_path - 1,
+       version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
+           server_version_plist_path, sizeof server_version_plist_path - 1,
            false);
-       if (system_version_plist_file_url == NULL)
-               return FALSE;
-       system_version_plist_stream = CFReadStreamCreateWithFile(NULL,
-           system_version_plist_file_url);
-       CFRelease(system_version_plist_file_url);
-       if (system_version_plist_stream == NULL)
+       if (version_plist_file_url == NULL)
                return FALSE;
-       if (!CFReadStreamOpen(system_version_plist_stream)) {
-               CFRelease(system_version_plist_stream);
+       version_plist_stream = CFReadStreamCreateWithFile(NULL,
+           version_plist_file_url);
+       CFRelease(version_plist_file_url);
+       if (version_plist_stream == NULL)
                return FALSE;
+       if (!CFReadStreamOpen(version_plist_stream)) {
+               CFRelease(version_plist_stream);
+
+               /*
+                * Try SystemVersion.plist.
+                */
+               version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
+                   system_version_plist_path, sizeof system_version_plist_path - 1,
+                   false);
+               if (version_plist_file_url == NULL)
+                       return FALSE;
+               version_plist_stream = CFReadStreamCreateWithFile(NULL,
+                   version_plist_file_url);
+               CFRelease(version_plist_file_url);
+               if (version_plist_stream == NULL)
+                       return FALSE;
+               if (!CFReadStreamOpen(version_plist_stream)) {
+                       CFRelease(version_plist_stream);
+                       return FALSE;
+               }
        }
 #ifdef HAVE_CFPROPERTYLISTCREATEWITHSTREAM
-       system_version_dict = CFPropertyListCreateWithStream(NULL,
-           system_version_plist_stream, 0, kCFPropertyListImmutable,
+       version_dict = (CFDictionaryRef)CFPropertyListCreateWithStream(NULL,
+           version_plist_stream, 0, kCFPropertyListImmutable,
            NULL, NULL);
 #else
-       system_version_dict = CFPropertyListCreateFromStream(NULL,
-           system_version_plist_stream, 0, kCFPropertyListImmutable,
+       version_dict = (CFDictionaryRef)CFPropertyListCreateFromStream(NULL,
+           version_plist_stream, 0, kCFPropertyListImmutable,
            NULL, NULL);
 #endif
-       if (system_version_dict == NULL)
+       if (version_dict == NULL) {
+               CFRelease(version_plist_stream);
                return FALSE;
-       if (CFGetTypeID(system_version_dict) != CFDictionaryGetTypeID()) {
+       }
+       if (CFGetTypeID(version_dict) != CFDictionaryGetTypeID()) {
                /* This is *supposed* to be a dictionary.  Punt. */
-               CFRelease(system_version_dict);
-               CFReadStreamClose(system_version_plist_stream);
-               CFRelease(system_version_plist_stream);
+               CFRelease(version_dict);
+               CFReadStreamClose(version_plist_stream);
+               CFRelease(version_plist_stream);
                return FALSE;
        }
+       /* Get the product name string. */
+       string = get_string_from_dictionary(version_dict,
+           CFSTR("ProductName"));
+       if (string == NULL) {
+               CFRelease(version_dict);
+               CFReadStreamClose(version_plist_stream);
+               CFRelease(version_plist_stream);
+               return FALSE;
+       }
+       g_string_append_printf(str, "%s", string);
+       g_free(string);
+
        /* Get the OS version string. */
-       string = get_string_from_dictionary(system_version_dict,
+       string = get_string_from_dictionary(version_dict,
            CFSTR("ProductUserVisibleVersion"));
        if (string == NULL) {
-               CFRelease(system_version_dict);
-               CFReadStreamClose(system_version_plist_stream);
-               CFRelease(system_version_plist_stream);
+               CFRelease(version_dict);
+               CFReadStreamClose(version_plist_stream);
+               CFRelease(version_plist_stream);
                return FALSE;
        }
-       g_string_append_printf(str, "OS X %s", string);
+       g_string_append_printf(str, " %s", string);
        g_free(string);
 
        /* Get the build string */
-       string = get_string_from_dictionary(system_version_dict,
+       string = get_string_from_dictionary(version_dict,
            CFSTR("ProductBuildVersion"));
        if (string == NULL) {
-               CFRelease(system_version_dict);
-               CFReadStreamClose(system_version_plist_stream);
-               CFRelease(system_version_plist_stream);
+               CFRelease(version_dict);
+               CFReadStreamClose(version_plist_stream);
+               CFRelease(version_plist_stream);
                return FALSE;
        }
        g_string_append_printf(str, ", build %s", string);
        g_free(string);
-       CFRelease(system_version_dict);
-       CFReadStreamClose(system_version_plist_stream);
-       CFRelease(system_version_plist_stream);
+       CFRelease(version_dict);
+       CFReadStreamClose(version_plist_stream);
+       CFRelease(version_plist_stream);
        return TRUE;
 }
 #endif
@@ -456,6 +477,12 @@ void get_os_version_info(GString *str)
                        case 1:
                                g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
                                break;
+                       case 2:
+                               g_string_append_printf(str, is_nt_workstation ? "Windows 8" : "Windows Server 2012");
+                               break;
+                       case 3:
+                               g_string_append_printf(str, is_nt_workstation ? "Windows 8.1" : "Windows Server 2012 R2");
+                               break;
                        default:
                                g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
                                                       info.dwMajorVersion, info.dwMinorVersion);
@@ -583,6 +610,94 @@ void get_os_version_info(GString *str)
 }
 
 
+/*
+ * Get the CPU info, and append it to the GString
+ */
+
+#if defined(_MSC_VER)
+static void
+do_cpuid(int *CPUInfo, guint32 selector){
+       __cpuid(CPUInfo, selector);
+}
+#elif defined(__GNUC__)
+#if defined(__x86_64__)  
+static inline void
+do_cpuid(guint32 *CPUInfo, int selector)
+{
+       __asm__ __volatile__("cpuid"
+                                               : "=a" (CPUInfo[0]),
+                                                       "=b" (CPUInfo[1]),
+                                                       "=c" (CPUInfo[2]),
+                                                       "=d" (CPUInfo[3])
+                                               : "a"(selector));
+}
+#else /* (__i386__) */
+/* would need a test if older proccesors have the cpuid instruction */
+static void
+do_cpuid(guint32 *CPUInfo, int selector _U_){
+       CPUInfo[0] = 0;
+}
+
+#endif /* defined(__x86_64__)*/
+#else /* Other compilers */
+static void
+do_cpuid(guint32 *CPUInfo, int selector _U_){
+       CPUInfo[0] = 0;
+}
+#endif
+
+/*
+ * Get CPU info on platforms where the cpuid instruction can be used skip 32 bit versions for GCC
+ *     http://www.intel.com/content/dam/www/public/us/en/documents/application-notes/processor-identification-cpuid-instruction-note.pdf
+ * the get_cpuid() routine will return 0 in CPUInfo[0] if cpuinfo isn't available.
+ */
+
+static void get_cpu_info(GString *str _U_)
+{
+#if defined(_MSC_VER)
+       int CPUInfo[4];
+#else
+       guint32 CPUInfo[4];
+#endif
+       char CPUBrandString[0x40];
+       unsigned nExIds;
+
+       /* http://msdn.microsoft.com/en-us/library/hskdteyh(v=vs.100).aspx */
+
+       /* Calling __cpuid with 0x80000000 as the InfoType argument*/
+       /* gets the number of valid extended IDs.*/
+       do_cpuid(CPUInfo, 0x80000000);
+       nExIds = CPUInfo[0];
+
+       if( nExIds<0x80000005)
+               return;
+       memset(CPUBrandString, 0, sizeof(CPUBrandString));
+
+       /* Interpret CPU brand string.*/
+       do_cpuid(CPUInfo, 0x80000002);
+       memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
+       do_cpuid(CPUInfo, 0x80000003);
+       memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
+       do_cpuid(CPUInfo, 0x80000004);
+       memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
+
+       g_string_append_printf(str, "\n%s", CPUBrandString);
+
+}
+
+static void get_mem_info(GString *str _U_)
+{
+#if defined(_WIN32)
+       MEMORYSTATUSEX statex;
+
+       statex.dwLength = sizeof (statex);
+       
+       if(GlobalMemoryStatusEx (&statex))
+               g_string_append_printf(str, ", with ""%" G_GINT64_MODIFIER "d" "MB of physical memory.\n", statex.ullTotalPhys/(1024*1024));
+#endif
+
+}
+
 /*
  * Get various library run-time versions, and the OS version, and append
  * them to the specified GString.
@@ -621,6 +736,12 @@ get_runtime_version_info(GString *str, void (*additional_info)(GString *))
 
        g_string_append(str, ".");
 
+       /* CPU Info */
+       get_cpu_info(str);
+
+       /* Get info about installed memory Windows only */
+       get_mem_info(str);
+
        /* Compiler info */
 
        /*
@@ -708,7 +829,7 @@ const char *
 get_copyright_info(void)
 {
        return
-"Copyright 1998-2012 Gerald Combs <gerald@wireshark.org> and contributors.\n"
+"Copyright 1998-2013 Gerald Combs <gerald@wireshark.org> and contributors.\n"
 "This is free software; see the source for copying conditions. There is NO\n"
 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
 }