summaryrefslogtreecommitdiff
path: root/mercurial/mpatch.c
diff options
context:
space:
mode:
Diffstat (limited to 'mercurial/mpatch.c')
-rw-r--r--mercurial/mpatch.c75
1 files changed, 58 insertions, 17 deletions
diff --git a/mercurial/mpatch.c b/mercurial/mpatch.c
index ab429b5..e85d20d 100644
--- a/mercurial/mpatch.c
+++ b/mercurial/mpatch.c
@@ -20,13 +20,48 @@
of the GNU General Public License, incorporated herein by reference.
*/
-#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
+/* Definitions to get compatibility with python 2.4 and earlier which
+ does not have Py_ssize_t. See also PEP 353.
+ Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t.
+*/
+#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
+typedef int Py_ssize_t;
+#define PY_SSIZE_T_MAX INT_MAX
+#define PY_SSIZE_T_MIN INT_MIN
+#endif
+
+#ifdef _WIN32
+#ifdef _MSC_VER
+/* msvc 6.0 has problems */
+#define inline __inline
+typedef unsigned long uint32_t;
+#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>
+#if defined __BEOS__ && !defined __HAIKU__
+#include <ByteOrder.h>
+#else
+#include <arpa/inet.h>
+#endif
+#include <inttypes.h>
+#endif
+
static char mpatch_doc[] = "Efficient binary patching.";
static PyObject *mpatch_Error;
@@ -39,7 +74,7 @@ struct flist {
struct frag *base, *head, *tail;
};
-static struct flist *lalloc(Py_ssize_t size)
+static struct flist *lalloc(int size)
{
struct flist *a = NULL;
@@ -69,7 +104,7 @@ static void lfree(struct flist *a)
}
}
-static Py_ssize_t lsize(struct flist *a)
+static int lsize(struct flist *a)
{
return a->tail - a->head;
}
@@ -198,11 +233,12 @@ static struct flist *combine(struct flist *a, struct flist *b)
}
/* decode a binary patch into a hunk list */
-static struct flist *decode(const char *bin, Py_ssize_t len)
+static struct flist *decode(const char *bin, int len)
{
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,9 +248,10 @@ static struct flist *decode(const char *bin, Py_ssize_t len)
lt = l->tail;
while (data <= end) {
- lt->start = getbe32(bin);
- lt->end = getbe32(bin + 4);
- lt->len = getbe32(bin + 8);
+ memcpy(decode, bin, 12);
+ lt->start = ntohl(decode[0]);
+ lt->end = ntohl(decode[1]);
+ lt->len = ntohl(decode[2]);
if (lt->start > lt->end)
break; /* sanity check */
bin = data + lt->len;
@@ -237,9 +274,9 @@ static struct flist *decode(const char *bin, Py_ssize_t len)
}
/* calculate the size of resultant text */
-static Py_ssize_t calcsize(Py_ssize_t len, struct flist *l)
+static int calcsize(int len, struct flist *l)
{
- Py_ssize_t outlen = 0, last = 0;
+ int outlen = 0, last = 0;
struct frag *f = l->head;
while (f != l->tail) {
@@ -259,7 +296,7 @@ static Py_ssize_t calcsize(Py_ssize_t len, struct flist *l)
return outlen;
}
-static int apply(char *buf, const char *orig, Py_ssize_t len, struct flist *l)
+static int apply(char *buf, const char *orig, int len, struct flist *l)
{
struct frag *f = l->head;
int last = 0;
@@ -284,9 +321,10 @@ static int apply(char *buf, const char *orig, Py_ssize_t len, struct flist *l)
}
/* recursively generate a patch of all bins between start and end */
-static struct flist *fold(PyObject *bins, Py_ssize_t start, Py_ssize_t end)
+static struct flist *fold(PyObject *bins, int start, int end)
{
- Py_ssize_t len, blen;
+ int len;
+ Py_ssize_t blen;
const char *buffer;
if (start + 1 == end) {
@@ -312,7 +350,8 @@ patches(PyObject *self, PyObject *args)
struct flist *patch;
const char *in;
char *out;
- Py_ssize_t len, outlen, inlen;
+ int len, outlen;
+ Py_ssize_t inlen;
if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
return NULL;
@@ -356,8 +395,9 @@ static PyObject *
patchedsize(PyObject *self, PyObject *args)
{
long orig, start, end, len, outlen = 0, last = 0;
- Py_ssize_t patchlen;
+ 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;
@@ -366,9 +406,10 @@ patchedsize(PyObject *self, PyObject *args)
data = bin + 12;
while (data <= binend) {
- start = getbe32(bin);
- end = getbe32(bin + 4);
- len = getbe32(bin + 8);
+ memcpy(decode, bin, 12);
+ start = ntohl(decode[0]);
+ end = ntohl(decode[1]);
+ len = ntohl(decode[2]);
if (start > end)
break; /* sanity check */
bin = data + len;