mercurial/mpatch_module.c
author Yuya Nishihara <yuya@tcha.org>
Fri, 22 Apr 2016 21:46:33 +0900
changeset 31807 e6eb86b154c5
parent 29749 155f0cc3f813
child 32358 5fc3459d0493
permissions -rw-r--r--
templater: provide loop counter as "index" keyword This was originally written for JSON templating where we would have to be careful to not add extra comma, but seems generally useful. Inner loop started by % operator has its own counter.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     1
/*
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     2
 mpatch.c - efficient binary patching for Mercurial
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     3
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     4
 This implements a patch algorithm that's O(m + nlog n) where m is the
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     5
 size of the output and n is the number of patches.
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     6
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     7
 Given a list of binary patches, it unpacks each into a hunk list,
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     8
 then combines the hunk lists with a treewise recursion to form a
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
     9
 single hunk list. This hunk list is then applied to the original
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    10
 text.
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    11
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    12
 The text (or binary) fragments are copied directly from their source
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    13
 Python objects into a preallocated output string to avoid the
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    14
 allocation of intermediate Python objects. Working memory is about 2x
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    15
 the total number of hunks.
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    16
2859
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2543
diff changeset
    17
 Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    18
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    19
 This software may be used and distributed according to the terms
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    20
 of the GNU General Public License, incorporated herein by reference.
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    21
*/
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    22
16758
9a8ab5c47f84 mpatch: use Py_ssize_t for string length
Adrian Buehlmann <adrian@cadifra.com>
parents: 16757
diff changeset
    23
#define PY_SSIZE_T_CLEAN
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    24
#include <Python.h>
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    25
#include <stdlib.h>
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    26
#include <string.h>
2468
1ac0574f1768 mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2083
diff changeset
    27
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    28
#include "util.h"
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents: 28782
diff changeset
    29
#include "bitmanipulation.h"
29691
e9a0bcc9314d mpatch: change Py_ssize_t to ssize_t in places that will be later copied
Maciej Fijalkowski <fijall@gmail.com>
parents: 29444
diff changeset
    30
#include "compat.h"
29693
b9b9f9a92481 mpatch: split mpatch into two files
Maciej Fijalkowski <fijall@gmail.com>
parents: 29692
diff changeset
    31
#include "mpatch.h"
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    32
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    33
static char mpatch_doc[] = "Efficient binary patching.";
1722
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
    34
static PyObject *mpatch_Error;
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    35
29749
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    36
static void setpyerr(int r)
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    37
{
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    38
	switch (r) {
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    39
	case MPATCH_ERR_NO_MEM:
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    40
		PyErr_NoMemory();
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    41
		break;
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    42
	case MPATCH_ERR_CANNOT_BE_DECODED:
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    43
		PyErr_SetString(mpatch_Error, "patch cannot be decoded");
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    44
		break;
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    45
	case MPATCH_ERR_INVALID_PATCH:
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    46
		PyErr_SetString(mpatch_Error, "invalid patch");
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    47
		break;
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    48
	}
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    49
}
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    50
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    51
struct mpatch_flist *cpygetitem(void *bins, ssize_t pos)
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    52
{
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    53
	const char *buffer;
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    54
	struct mpatch_flist *res;
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    55
	ssize_t blen;
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    56
	int r;
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    57
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    58
	PyObject *tmp = PyList_GetItem((PyObject*)bins, pos);
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    59
	if (!tmp)
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    60
		return NULL;
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    61
	if (PyObject_AsCharBuffer(tmp, &buffer, (Py_ssize_t*)&blen))
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    62
		return NULL;
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    63
	if ((r = mpatch_decode(buffer, blen, &res)) < 0) {
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    64
		if (!PyErr_Occurred())
29749
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
    65
			setpyerr(r);
29740
21ac534d7d30 mpatch: remove superfluous whitespaces
Yuya Nishihara <yuya@tcha.org>
parents: 29694
diff changeset
    66
		return NULL;
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    67
	}
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    68
	return res;
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    69
}
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    70
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    71
static PyObject *
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    72
patches(PyObject *self, PyObject *args)
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    73
{
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    74
	PyObject *text, *bins, *result;
29692
6b3a8d034b69 mpatch: provide things that will be exported later with a prefixed name
Maciej Fijalkowski <fijall@gmail.com>
parents: 29691
diff changeset
    75
	struct mpatch_flist *patch;
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
    76
	const char *in;
29742
b410e26692a4 mpatch: silence warning about maybe-uninitialized variable
Yuya Nishihara <yuya@tcha.org>
parents: 29740
diff changeset
    77
	int r = 0;
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
    78
	char *out;
16757
923bd97b86a0 mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents: 16437
diff changeset
    79
	Py_ssize_t len, outlen, inlen;
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    80
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
    81
	if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    82
		return NULL;
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    83
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    84
	len = PyList_Size(bins);
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    85
	if (!len) {
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    86
		/* nothing to do */
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    87
		Py_INCREF(text);
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    88
		return text;
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    89
	}
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
    90
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
    91
	if (PyObject_AsCharBuffer(text, &in, &inlen))
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
    92
		return NULL;
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
    93
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    94
	patch = mpatch_fold(bins, cpygetitem, 0, len);
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    95
	if (!patch) { /* error already set or memory error */
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    96
		if (!PyErr_Occurred())
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    97
			PyErr_NoMemory();
1722
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
    98
		return NULL;
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
    99
	}
128
d6afb6dbf9f2 Add safety checking to mpatch
mpm@selenic.com
parents: 82
diff changeset
   100
29692
6b3a8d034b69 mpatch: provide things that will be exported later with a prefixed name
Maciej Fijalkowski <fijall@gmail.com>
parents: 29691
diff changeset
   101
	outlen = mpatch_calcsize(inlen, patch);
1722
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   102
	if (outlen < 0) {
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
   103
		r = (int)outlen;
1722
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   104
		result = NULL;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   105
		goto cleanup;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   106
	}
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   107
	result = PyBytes_FromStringAndSize(NULL, outlen);
1722
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   108
	if (!result) {
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   109
		result = NULL;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   110
		goto cleanup;
128
d6afb6dbf9f2 Add safety checking to mpatch
mpm@selenic.com
parents: 82
diff changeset
   111
	}
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   112
	out = PyBytes_AsString(result);
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
   113
	if ((r = mpatch_apply(out, in, inlen, patch)) < 0) {
1722
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   114
		Py_DECREF(result);
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   115
		result = NULL;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   116
	}
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
   117
cleanup:
29692
6b3a8d034b69 mpatch: provide things that will be exported later with a prefixed name
Maciej Fijalkowski <fijall@gmail.com>
parents: 29691
diff changeset
   118
	mpatch_lfree(patch);
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
   119
	if (!result && !PyErr_Occurred())
29749
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
   120
		setpyerr(r);
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   121
	return result;
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   122
}
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   123
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   124
/* calculate size of a patched file directly */
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   125
static PyObject *
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   126
patchedsize(PyObject *self, PyObject *args)
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   127
{
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   128
	long orig, start, end, len, outlen = 0, last = 0, pos = 0;
16758
9a8ab5c47f84 mpatch: use Py_ssize_t for string length
Adrian Buehlmann <adrian@cadifra.com>
parents: 16757
diff changeset
   129
	Py_ssize_t patchlen;
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   130
	char *bin;
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   131
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   132
	if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen))
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   133
		return NULL;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   134
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   135
	while (pos >= 0 && pos < patchlen) {
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   136
		start = getbe32(bin + pos);
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   137
		end = getbe32(bin + pos + 4);
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   138
		len = getbe32(bin + pos + 8);
4358
11dc22eb8e8d Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3138
diff changeset
   139
		if (start > end)
11dc22eb8e8d Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3138
diff changeset
   140
			break; /* sanity check */
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   141
		pos += 12 + len;
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   142
		outlen += start - last;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   143
		last = end;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   144
		outlen += len;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   145
	}
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   146
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
   147
	if (pos != patchlen) {
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   148
		if (!PyErr_Occurred())
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   149
			PyErr_SetString(mpatch_Error, "patch cannot be decoded");
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   150
		return NULL;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   151
	}
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   152
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   153
	outlen += orig - last;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   154
	return Py_BuildValue("l", outlen);
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   155
}
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   156
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   157
static PyMethodDef methods[] = {
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   158
	{"patches", patches, METH_VARARGS, "apply a series of patches\n"},
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
   159
	{"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"},
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   160
	{NULL, NULL}
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   161
};
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   162
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   163
#ifdef IS_PY3K
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   164
static struct PyModuleDef mpatch_module = {
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   165
	PyModuleDef_HEAD_INIT,
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   166
	"mpatch",
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   167
	mpatch_doc,
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   168
	-1,
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   169
	methods
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   170
};
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   171
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   172
PyMODINIT_FUNC PyInit_mpatch(void)
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   173
{
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   174
	PyObject *m;
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   175
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   176
	m = PyModule_Create(&mpatch_module);
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   177
	if (m == NULL)
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   178
		return NULL;
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   179
28782
f736f98e16ca mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents: 28657
diff changeset
   180
	mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError",
f736f98e16ca mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents: 28657
diff changeset
   181
					  NULL, NULL);
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   182
	Py_INCREF(mpatch_Error);
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   183
	PyModule_AddObject(m, "mpatchError", mpatch_Error);
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   184
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   185
	return m;
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   186
}
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   187
#else
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   188
PyMODINIT_FUNC
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   189
initmpatch(void)
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   190
{
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   191
	Py_InitModule3("mpatch", methods, mpatch_doc);
28782
f736f98e16ca mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents: 28657
diff changeset
   192
	mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError",
f736f98e16ca mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents: 28657
diff changeset
   193
					  NULL, NULL);
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
   194
}
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   195
#endif