changeset 16437:d126a0d16856

util.h: replace ntohl/htonl with get/putbe32
author Matt Mackall <mpm@selenic.com>
date Mon, 16 Apr 2012 11:26:00 -0500
parents e98460f6089d
children 28a90cdf0ca0
files mercurial/bdiff.c mercurial/mpatch.c mercurial/parsers.c mercurial/util.h
diffstat 4 files changed, 41 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bdiff.c	Fri Apr 13 10:08:08 2012 +0200
+++ b/mercurial/bdiff.c	Mon Apr 16 11:26:00 2012 -0500
@@ -338,7 +338,6 @@
 	PyObject *result = NULL;
 	struct line *al, *bl;
 	struct hunk l, *h;
-	uint32_t encode[3];
 	int an, bn, len = 0, la, lb, count;
 
 	if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
@@ -375,10 +374,9 @@
 	for (h = l.next; h; h = h->next) {
 		if (h->a1 != la || h->b1 != lb) {
 			len = bl[h->b1].l - bl[lb].l;
-			encode[0] = htonl(al[la].l - al->l);
-			encode[1] = htonl(al[h->a1].l - al->l);
-			encode[2] = htonl(len);
-			memcpy(rb, encode, 12);
+			putbe32(al[la].l - al->l, rb);
+			putbe32(al[h->a1].l - al->l, rb + 4);
+			putbe32(len, rb + 8);
 			memcpy(rb + 12, bl[lb].l, len);
 			rb += 12 + len;
 		}
--- a/mercurial/mpatch.c	Fri Apr 13 10:08:08 2012 +0200
+++ b/mercurial/mpatch.c	Mon Apr 16 11:26:00 2012 -0500
@@ -202,7 +202,6 @@
 	struct flist *l;
 	struct frag *lt;
 	const char *data = bin + 12, *end = bin + len;
-	uint32_t decode[3]; /* for dealing with alignment issues */
 
 	/* assume worst case size, we won't have many of these lists */
 	l = lalloc(len / 12);
@@ -212,10 +211,9 @@
 	lt = l->tail;
 
 	while (data <= end) {
-		memcpy(decode, bin, 12);
-		lt->start = ntohl(decode[0]);
-		lt->end = ntohl(decode[1]);
-		lt->len = ntohl(decode[2]);
+		lt->start = getbe32(bin);
+		lt->end = getbe32(bin + 4);
+		lt->len = getbe32(bin + 8);
 		if (lt->start > lt->end)
 			break; /* sanity check */
 		bin = data + lt->len;
@@ -361,7 +359,6 @@
 	long orig, start, end, len, outlen = 0, last = 0;
 	int patchlen;
 	char *bin, *binend, *data;
-	uint32_t decode[3]; /* for dealing with alignment issues */
 
 	if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen))
 		return NULL;
@@ -370,10 +367,9 @@
 	data = bin + 12;
 
 	while (data <= binend) {
-		memcpy(decode, bin, 12);
-		start = ntohl(decode[0]);
-		end = ntohl(decode[1]);
-		len = ntohl(decode[2]);
+		start = getbe32(bin);
+		end = getbe32(bin + 4);
+		len = getbe32(bin + 8);
 		if (start > end)
 			break; /* sanity check */
 		bin = data + len;
--- a/mercurial/parsers.c	Fri Apr 13 10:08:08 2012 +0200
+++ b/mercurial/parsers.c	Mon Apr 16 11:26:00 2012 -0500
@@ -143,7 +143,6 @@
 	int state, mode, size, mtime;
 	unsigned int flen;
 	int len;
-	uint32_t decode[4]; /* for alignment */
 
 	if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
 			      &PyDict_Type, &dmap,
@@ -166,11 +165,10 @@
 	while (cur < end - 17) {
 		/* unpack header */
 		state = *cur;
-		memcpy(decode, cur + 1, 16);
-		mode = ntohl(decode[0]);
-		size = ntohl(decode[1]);
-		mtime = ntohl(decode[2]);
-		flen = ntohl(decode[3]);
+		mode = getbe32(cur + 1);
+		size = getbe32(cur + 5);
+		mtime = getbe32(cur + 9);
+		flen = getbe32(cur + 13);
 		cur += 17;
 		if (cur + flen > end || cur + flen < cur) {
 			PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
@@ -308,7 +306,6 @@
  */
 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
 {
-	uint32_t decode[8]; /* to enforce alignment with inline data */
 	uint64_t offset_flags;
 	int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
 	const char *c_node_id;
@@ -351,22 +348,20 @@
 	if (data == NULL)
 		return NULL;
 
-	memcpy(decode, data, 8 * sizeof(uint32_t));
-
-	offset_flags = ntohl(decode[1]);
+	offset_flags = getbe32(data + 4);
 	if (pos == 0) /* mask out version number for the first entry */
 		offset_flags &= 0xFFFF;
 	else {
-		uint32_t offset_high = ntohl(decode[0]);
+		uint32_t offset_high = getbe32(data);
 		offset_flags |= ((uint64_t)offset_high) << 32;
 	}
 
-	comp_len = ntohl(decode[2]);
-	uncomp_len = ntohl(decode[3]);
-	base_rev = ntohl(decode[4]);
-	link_rev = ntohl(decode[5]);
-	parent_1 = ntohl(decode[6]);
-	parent_2 = ntohl(decode[7]);
+	comp_len = getbe32(data + 8);
+	uncomp_len = getbe32(data + 12);
+	base_rev = getbe32(data + 16);
+	link_rev = getbe32(data + 20);
+	parent_1 = getbe32(data + 24);
+	parent_2 = getbe32(data + 28);
 	c_node_id = data + 32;
 
 	entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
@@ -940,8 +935,8 @@
 		uint32_t comp_len;
 		const char *old_data;
 		/* 3rd element of header is length of compressed inline data */
-		memcpy(&comp_len, data + 8, sizeof(uint32_t));
-		incr = hdrsize + ntohl(comp_len);
+		comp_len = getbe32(data + 8);
+		incr = hdrsize + comp_len;
 		if (incr < hdrsize)
 			break;
 		if (offsets)
--- a/mercurial/util.h	Fri Apr 13 10:08:08 2012 +0200
+++ b/mercurial/util.h	Mon Apr 16 11:26:00 2012 -0500
@@ -125,13 +125,6 @@
 #else
 #include <stdint.h>
 #endif
-static uint32_t ntohl(uint32_t x)
-{
-	return ((x & 0x000000ffUL) << 24) |
-	       ((x & 0x0000ff00UL) <<  8) |
-	       ((x & 0x00ff0000UL) >>  8) |
-	       ((x & 0xff000000UL) >> 24);
-}
 #else
 /* not windows */
 #include <sys/types.h>
@@ -151,4 +144,22 @@
 #define inline __inline
 #endif
 
+static inline uint32_t getbe32(const char *c)
+{
+	const unsigned char *d = (const unsigned char *)c;
+
+	return ((d[0] << 24) |
+		(d[1] << 16) |
+		(d[2] << 8) |
+		(d[3]));
+}
+
+static inline void putbe32(uint32_t x, char *c)
+{
+	c[0] = (x >> 24) & 0xff;
+	c[1] = (x >> 16) & 0xff;
+	c[2] = (x >> 8) & 0xff;
+	c[3] = (x) & 0xff;
+}
+
 #endif /* _HG_UTIL_H_ */