diff --git a/mercurial/bdiff.c b/mercurial/bdiff.c --- a/mercurial/bdiff.c +++ b/mercurial/bdiff.c @@ -338,7 +338,6 @@ static PyObject *bdiff(PyObject *self, P 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 @@ static PyObject *bdiff(PyObject *self, P 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; } diff --git a/mercurial/mpatch.c b/mercurial/mpatch.c --- a/mercurial/mpatch.c +++ b/mercurial/mpatch.c @@ -202,7 +202,6 @@ static struct flist *decode(const char * 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 @@ static struct flist *decode(const char * 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 @@ patchedsize(PyObject *self, PyObject *ar 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 @@ patchedsize(PyObject *self, PyObject *ar 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; diff --git a/mercurial/parsers.c b/mercurial/parsers.c --- a/mercurial/parsers.c +++ b/mercurial/parsers.c @@ -143,7 +143,6 @@ static PyObject *parse_dirstate(PyObject 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 @@ static PyObject *parse_dirstate(PyObject 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 const char *index_deref(indexObje */ 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 @@ static PyObject *index_get(indexObject * 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 @@ static long inline_scan(indexObject *sel 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) diff --git a/mercurial/util.h b/mercurial/util.h --- a/mercurial/util.h +++ b/mercurial/util.h @@ -125,13 +125,6 @@ typedef unsigned __int64 uint64_t; #else #include #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 @@ -151,4 +144,22 @@ static uint32_t ntohl(uint32_t x) #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_ */