ref: 6df1963a5bd8aeda90153ace9b29cccdcab348f9
parent: 24a5f2f68d074edd5e6663982bea9fd09ba7605c
author: Ori Bernstein <[email protected]>
date: Tue Jan 3 15:28:07 EST 2012
Use standard endian swapping. The nice ones aren't supported on osx. Fuck you.
--- a/parse/pickle.c
+++ b/parse/pickle.c
@@ -9,12 +9,10 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
-#ifndef _BSD_SOURCE
-#define _BSD_SOURCE
-#endif
-#include <endian.h>
+#include <arpa/inet.h>
#include "parse.h"
+
static void wrtype(FILE *fd, Type *val);
static Type *rdtype(FILE *fd);
static void wrstab(FILE *fd, Stab *val);
@@ -22,6 +20,32 @@
static void wrsym(FILE *fd, Sym *val);
static Sym *rdsym(FILE *fd);
+static vlong be64(vlong v)
+{
+ if (htonl(42) != 42)
+ return ((vlong)htonl(v >> 32) << 32) | htonl((uint32_t)v);
+ else
+ return v;
+}
+
+static vlong host64(vlong v)
+{
+ if (htonl(42) != 42) /* we need to swap */
+ return (vlong)ntohl(v >> 32) << 32 | htonl((uint32_t)v);
+ else
+ return v;
+}
+
+static long be32(long v)
+{
+ return htonl(v);
+}
+
+static long host32(long v)
+{
+ return ntohl(v);
+}
+
static void wrbyte(FILE *fd, char val)
{
if (fputc(val, fd) == EOF)
@@ -39,7 +63,7 @@
static void wrint(FILE *fd, int32_t val)
{
- val = htobe32(val);
+ val = be32(val);
if (fwrite(&val, sizeof(int32_t), 1, fd) == EOF)
die("Unexpected EOF");
}
@@ -50,7 +74,7 @@
if (fread(&val, sizeof(uint32_t), 1, fd) == EOF)
die("Unexpected EOF");
- return be32toh(val);
+ return host32(val);
}
static void wrstr(FILE *fd, char *val)
@@ -90,7 +114,7 @@
} u;
u.fval = val;
- u.ival = htobe64(u.ival);
+ u.ival = be64(u.ival);
if (fwrite(&u.ival, sizeof(uvlong), 1, fd) == EOF)
die("Unexpected EOF");
}
@@ -104,7 +128,7 @@
if (fread(&u.ival, sizeof(uvlong), 1, fd) == EOF)
die("Unexpected EOF");
- u.ival = be64toh(u.ival);
+ u.ival = host64(u.ival);
return u.fval;
}