r99: *** empty log message ***
[ctsim.git] / libctsupport / byteorder.cpp
diff --git a/libctsupport/byteorder.cpp b/libctsupport/byteorder.cpp
new file mode 100644 (file)
index 0000000..f7734fa
--- /dev/null
@@ -0,0 +1,342 @@
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include "kstddef.h"
+#include "byteorder.h"
+
+inline void
+SwapBytes2 (void* buffer)
+{
+  unsigned char* p = static_cast<unsigned char*>(buffer);
+  unsigned char temp = p[0];
+  p[0] = p[1];
+  p[1] = temp;
+}
+
+// 0<->3  1<->2 = 0123 -> 3210
+inline void
+SwapBytes4 (void* buffer)
+{
+  unsigned char* p = static_cast<unsigned char*>(buffer);
+  unsigned char temp = p[0];
+  p[0] = p[3];
+  p[3] = temp;
+  temp = p[1];
+  p[1] = p[2];
+  p[2] = temp;
+}
+
+// 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
+inline void
+SwapBytes8 (void* buffer)
+{
+  unsigned char* p = static_cast<unsigned char*>(buffer);
+  unsigned char temp = p[0];
+  p[0] = p[7];
+  p[7] = temp;
+  temp = p[1];
+  p[1] = p[6];
+  p[6] = temp;
+  temp = p[2];
+  p[2] = p[5];
+  p[5] = temp;
+  temp = p[3];
+  p[3] = p[4];
+  p[4] = temp;
+}
+
+void 
+ConvertNetworkOrder (void* buffer, size_t bytes)
+{
+#if ! defined (WORDS_BIGENDIAN)
+    if (bytes < 2)
+       return;
+
+    char* start = static_cast<char*>(buffer);
+    char* end = start + bytes - 1;   // last byte
+    size_t nSwap = bytes / 2;
+    
+    while (nSwap-- > 0) {
+       unsigned char c = *start;
+       *start++ = *end;
+       *end-- = c;
+    }
+#endif    
+}
+
+void 
+ConvertReverseNetworkOrder (void* buffer, size_t bytes)
+{
+#if defined (WORDS_BIGENDIAN)
+    if (bytes < 2)
+       return;
+
+    char* start = static_cast<char*>(buffer);
+    char* end = start + bytes - 1;  // last byte 
+    size_t nSwap = bytes / 2;
+    
+    while (nSwap-- > 0) {
+       unsigned char c = *start;
+       *start++ = *end;
+       *end-- = c;
+    }
+#endif    
+}
+
+bool
+write_nint16 (kuint16 const *n_in, int fd)
+{
+    kuint16 n = *n_in;
+
+#ifndef WORDS_BIGENDIAN
+    SwapBytes2 (&n);
+#endif
+
+    if (write (fd, &n, 2) != 2)
+       return false;
+    else
+       return true;
+}
+
+bool 
+read_nint16 (kuint16 *n_out, int fd)
+{
+    if (read (fd, n_out, 2) != 2)
+      return false;
+
+#ifndef WORDS_BIGENDIAN
+    SwapBytes2 (n_out);
+#endif
+    
+    return true;
+}
+
+bool 
+write_nint32 (kuint32 const *n_in, int fd)
+{
+    kuint32 n = *n_in;
+
+#ifndef WORDS_BIGENDIAN
+    SwapBytes4(&n);
+#endif
+
+    if (write (fd, &n, 4) != 4)
+      return false;
+    else
+      return true;
+}
+
+bool 
+read_nint32 (kuint32 *n_out, int fd)
+{
+  if (read (fd, n_out, 4) != 4)
+    return false;
+
+#ifndef WORDS_BIGENDIAN
+  SwapBytes4 (n_out);
+#endif
+
+    return true;
+}
+
+bool 
+write_nfloat32 (kfloat32 const *f_in, int fd)
+{
+  kfloat32 f = *f_in;
+
+#ifndef WORDS_BIGENDIAN
+  SwapBytes4 (&f);
+#endif
+
+  if (write (fd, &f, 4) != 4)
+    return false;
+  else
+    return true;
+}
+
+bool 
+read_nfloat32 (kfloat32 *f_out, int fd)
+{
+  if (read (fd, f_out, 4) != 4)
+    return false;
+
+#ifndef WORDS_BIGENDIAN
+  SwapBytes4(f_out);
+#endif
+
+  return true;
+}
+
+bool 
+write_nfloat64 (kfloat64 const *f_in, int fd)
+{
+  kfloat64 f = *f_in;
+
+#ifndef WORDS_BIGENDIAN
+  SwapBytes8 (&f);
+#endif
+
+  if (write (fd, &f, 8) != 8)
+    return false;
+  else
+    return true;
+}
+
+bool 
+read_nfloat64 (kfloat64 *f_out, int fd)
+{
+  if (read (fd, f_out, 8) != 8)
+    return false;
+
+#ifndef WORDS_BIGENDIAN
+  SwapBytes8 (f_out);
+#endif
+    
+  return true;
+}
+
+
+
+onetorderstream& onetorderstream::writeInt16 (kuint16 n) {
+#ifndef WORDS_BIGENDIAN
+  SwapBytes2 (&n);
+#endif
+  write (&n, 2);
+  return (*this);
+}
+
+inetorderstream& inetorderstream::readInt16 (kuint16& n) {
+  read (&n, 2);
+#ifndef WORDS_BIGENDIAN
+  SwapBytes2 (&n);
+#endif
+  return (*this);
+}
+
+onetorderstream& onetorderstream::writeInt32 (kuint32 n) {
+#ifndef WORDS_BIGENDIAN
+  SwapBytes4(&n);
+#endif
+  write (&n, 4);
+  return (*this);
+}
+
+inetorderstream& inetorderstream::readInt32 (kuint32& n) {
+  read (&n, 4);
+#ifndef WORDS_BIGENDIAN
+  SwapBytes4 (&n);
+#endif
+  return (*this);
+}
+
+onetorderstream& onetorderstream::writeFloat32 (kfloat32 n) {
+#ifndef WORDS_BIGENDIAN
+  SwapBytes4 (&n);
+#endif
+  write (&n, 4);
+  return (*this);
+}
+
+inetorderstream& inetorderstream::readFloat32 (kfloat32& n) {
+  read (&n, 4);
+#ifndef WORDS_BIGENDIAN
+  SwapBytes4 (&n);
+#endif
+  return (*this);
+}
+
+onetorderstream& onetorderstream::writeFloat64 (kfloat64 n) {
+#ifndef WORDS_BIGENDIAN
+  SwapBytes8 (&n);
+#endif
+  write (&n, 8);
+  return (*this);
+}
+
+inetorderstream& inetorderstream::readFloat64 (kfloat64& n) {
+  read (&n, 8);
+#ifndef WORDS_BIGENDIAN
+  SwapBytes8 (&n);
+#endif
+  return (*this);
+}
+
+
+
+void
+write_rnint16 (kuint16 n, ostream& ostr)
+{
+#ifdef WORDS_BIGENDIAN
+  SwapBytes2 (&n);
+#endif
+  ostr.write (&n, 2);
+}
+
+void
+read_rnint16 (kuint16& n, istream& istr)
+{
+  istr.read (&n, 2);
+#ifdef WORDS_BIGENDIAN
+  SwapBytes2 (&n);
+#endif
+}
+
+void
+write_rnint32 (kuint32 n, ostream& ostr)
+{
+#ifdef WORDS_BIGENDIAN
+  SwapBytes4(&n);
+#endif
+  ostr.write (&n, 4);
+}
+
+void
+read_rnint32 (kuint32& n, istream istr)
+{
+  istr.read (&n, 4);
+#ifdef WORDS_BIGENDIAN
+  SwapBytes4 (&n);
+#endif
+}
+
+void
+write_rnfloat32 (kfloat32 n, ostream ostr)
+{
+#ifdef WORDS_BIGENDIAN
+  SwapBytes4 (&n);
+#endif
+  ostr.write (&n, 4);
+}
+
+void
+read_rnfloat32 (kfloat32& n, istream istr)
+{
+  istr.read (&n, 4);
+#ifdef WORDS_BIGENDIAN
+  SwapBytes4 (&n);
+#endif
+}
+
+void
+write_rnfloat64 (kfloat64 n, ostream ostr)
+{
+#ifdef WORDS_BIGENDIAN
+  SwapBytes8 (&n);
+#endif
+  ostr.write (&n, 8);
+}
+
+void
+read_rnfloat64 (kfloat64& n, istream istr)
+{
+  istr.read (&n, 8);
+#ifdef WORDS_BIGENDIAN
+  SwapBytes8 (&n);
+#endif
+}
+