]> granicus.if.org Git - postgresql/commitdiff
Fix code to do the right thing with mixed-endian clients and servers.
authorThomas G. Lockhart <lockhart@fourpalms.org>
Tue, 1 Jul 1997 00:32:27 +0000 (00:32 +0000)
committerThomas G. Lockhart <lockhart@fourpalms.org>
Tue, 1 Jul 1997 00:32:27 +0000 (00:32 +0000)
src/backend/libpq/pqcomprim.c

index 2756285b176ebf4c7fce95d29808c3c2186957a4..3501b63c7b6bdbb488b42e5ab967e6f36fca5ab5 100644 (file)
 
 
 /* --------------------------------------------------------------------- */
-/* Is the other way around than system ntoh/hton, so we roll our own
-       here */
-       
-#ifndef                BYTE_ORDER
+/* These definitions for ntoh/hton are the other way around from the
+ *  default system definitions, so we roll our own here.
+ */
+
+#ifndef        BYTE_ORDER
 #error BYTE_ORDER must be defined as LITTLE_ENDIAN, BIG_ENDIAN or PDP_ENDIAN
 #endif
 
 #  define hton_l(n) n
 #else  /* BYTE_ORDER != LITTLE_ENDIAN */
 #  if BYTE_ORDER == BIG_ENDIAN
-#    define ntoh_s(n) (u_short)(((u_char *) &n)[0] << 8 | ((u_char *) &n)[1])
-#    define ntoh_l(n) (u_long)(((u_char *)&n)[0] << 24 | \
-                                                       ((u_char *)&n)[1] << 16 | \
-                          ((u_char *)&n)[2] << 8 | ((u_char *)&n)[3])
-#    define hton_s(n) (u_short)(((u_char *) &n)[2] << 8 | ((u_char *) &n)[3])
+#    define ntoh_s(n) (u_short)(((u_char *)&n)[1] << 8 \
+                              | ((u_char *)&n)[0])
+#    define ntoh_l(n) (u_long) (((u_char *)&n)[3] << 24 \
+                              | ((u_char *)&n)[2] << 16 \
+                             | ((u_char *)&n)[1] <<  8 \
+                              | ((u_char *)&n)[0])
+#    define hton_s(n) (ntoh_s(n))
 #    define hton_l(n) (ntoh_l(n))
 #  else        /* BYTE_ORDER != BIG_ENDIAN */
 #    if BYTE_ORDER == PDP_ENDIAN
 int pqPutShort(int integer, FILE *f)
     {
     int retval = 0;
-    u_short n;
+    u_short n,s;
                
-    n = hton_s(integer);
+    s = integer;
+    n = hton_s(s);
     if(fwrite(&n, sizeof(u_short), 1, f) != 1)
        retval = EOF;