]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - utility.cpp
Imported Upstream version 5.39.1+svn3124
[mirror_smartmontools-debian.git] / utility.cpp
index 5f6bf1081eda76f2650c431b4d9768b711622051..ab7f29fd35ef7c48905c5e35c55bcb885c1b16a8 100644 (file)
@@ -50,7 +50,7 @@
 #include "atacmds.h"
 #include "dev_interface.h"
 
-const char * utility_cpp_cvsid = "$Id: utility.cpp 3022 2010-01-01 17:02:00Z chrfranke $"
+const char * utility_cpp_cvsid = "$Id: utility.cpp 3090 2010-04-28 11:03:11Z chrfranke $"
                                  UTILITY_H_CVSID INT64_H_CVSID;
 
 const char * packet_types[] = {
@@ -259,15 +259,23 @@ const char *packetdevicetype(int type){
   return "Unknown";
 }
 
+// Runtime check of byte ordering, throws if different from isbigendian().
+void check_endianness()
+{
+  union {
+    // Force compile error if int type is not 32bit.
+    unsigned char c[sizeof(unsigned) == 4 ? 4 : -1];
+    unsigned i;
+  } x = {{1,2,3,4}};
+
+  int big = -1;
+  switch (x.i) {
+    case 0x01020304: big = 1; break;
+    case 0x04030201: big = 0; break;
+  }
 
-// Returns 1 if machine is big endian, else zero.  This is a run-time
-// rather than a compile-time function.  We could do it at
-// compile-time but in principle there are architectures that can run
-// with either byte-ordering.
-int isbigendian(){
-  short i=0x0100;
-  char *tmp=(char *)&i;
-  return *tmp;
+  if (big != (isbigendian() ? 1 : 0))
+    throw std::logic_error("CPU endianness does not match compile time test");
 }
 
 // Utility function prints date and time and timezone into a character