annotate mercurial/cext/mpatch.c @ 36809:3c15b84ab66c

hgweb: teach WSGI parser about query strings Currently, req.form uses cgi.parse() to populate form data. Depending on the request, form data can come from POST multipart/form-data, application/x-www-form-urlencoded, or the URL query string. Putting all these things into one data structure makes it difficult to reason about how exactly parameters got to the request. It can lead to wonkiness such as pulling parameters from both the URL and POST data. This commit teaches our WSGI request parser about argument data in query strings. We populate fields containing the query string data and only the query string data so it can't be confused with POST data. Differential Revision: https://phab.mercurial-scm.org/D2737
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 08 Mar 2018 11:21:46 -0800
parents 186c6df3a373
children 77492c10a35b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents: 28782
diff changeset
28 #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
29 #include "compat.h"
29693
b9b9f9a92481 mpatch: split mpatch into two files
Maciej Fijalkowski <fijall@gmail.com>
parents: 29692
diff changeset
30 #include "mpatch.h"
34438
b90e8da190da cext: reorder #include
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32371
diff changeset
31 #include "util.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
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
58 PyObject *tmp = PyList_GetItem((PyObject *)bins, pos);
29694
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;
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
61 if (PyObject_AsCharBuffer(tmp, &buffer, (Py_ssize_t *)&blen))
29694
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
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
71 static PyObject *patches(PyObject *self, PyObject *args)
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
72 {
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
73 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
74 struct mpatch_flist *patch;
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
75 const char *in;
29742
b410e26692a4 mpatch: silence warning about maybe-uninitialized variable
Yuya Nishihara <yuya@tcha.org>
parents: 29740
diff changeset
76 int r = 0;
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
77 char *out;
16757
923bd97b86a0 mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents: 16437
diff changeset
78 Py_ssize_t len, outlen, inlen;
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
79
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
80 if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
81 return NULL;
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
82
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
83 len = PyList_Size(bins);
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
84 if (!len) {
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
85 /* nothing to do */
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
86 Py_INCREF(text);
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
87 return text;
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
88 }
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
89
5444
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
90 if (PyObject_AsCharBuffer(text, &in, &inlen))
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
91 return NULL;
a0952e4e52eb mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents: 4377
diff changeset
92
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
93 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
94 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
95 if (!PyErr_Occurred())
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
96 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
97 return NULL;
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
98 }
128
d6afb6dbf9f2 Add safety checking to mpatch
mpm@selenic.com
parents: 82
diff changeset
99
29692
6b3a8d034b69 mpatch: provide things that will be exported later with a prefixed name
Maciej Fijalkowski <fijall@gmail.com>
parents: 29691
diff changeset
100 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
101 if (outlen < 0) {
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
102 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
103 result = NULL;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
104 goto cleanup;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
105 }
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
106 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
107 if (!result) {
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
108 result = NULL;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
109 goto cleanup;
128
d6afb6dbf9f2 Add safety checking to mpatch
mpm@selenic.com
parents: 82
diff changeset
110 }
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
111 out = PyBytes_AsString(result);
36361
a2d11d23bb25 patches: release the GIL while applying the patch
Boris Feld <boris.feld@octobus.net>
parents: 36227
diff changeset
112 /* clang-format off */
a2d11d23bb25 patches: release the GIL while applying the patch
Boris Feld <boris.feld@octobus.net>
parents: 36227
diff changeset
113 {
a2d11d23bb25 patches: release the GIL while applying the patch
Boris Feld <boris.feld@octobus.net>
parents: 36227
diff changeset
114 Py_BEGIN_ALLOW_THREADS
a2d11d23bb25 patches: release the GIL while applying the patch
Boris Feld <boris.feld@octobus.net>
parents: 36227
diff changeset
115 r = mpatch_apply(out, in, inlen, patch);
a2d11d23bb25 patches: release the GIL while applying the patch
Boris Feld <boris.feld@octobus.net>
parents: 36227
diff changeset
116 Py_END_ALLOW_THREADS
a2d11d23bb25 patches: release the GIL while applying the patch
Boris Feld <boris.feld@octobus.net>
parents: 36227
diff changeset
117 }
a2d11d23bb25 patches: release the GIL while applying the patch
Boris Feld <boris.feld@octobus.net>
parents: 36227
diff changeset
118 /* clang-format on */
35941
3028a3215a2e patches: move assignment outside the conditional
Boris Feld <boris.feld@octobus.net>
parents: 34438
diff changeset
119 if (r < 0) {
1722
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
120 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
121 result = NULL;
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
122 }
681c5c211b92 catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 597
diff changeset
123 cleanup:
29692
6b3a8d034b69 mpatch: provide things that will be exported later with a prefixed name
Maciej Fijalkowski <fijall@gmail.com>
parents: 29691
diff changeset
124 mpatch_lfree(patch);
29694
55dd12204b8e mpatch: remove dependency on Python.h in mpatch.c
Maciej Fijalkowski <fijall@gmail.com>
parents: 29693
diff changeset
125 if (!result && !PyErr_Occurred())
29749
155f0cc3f813 mpatch: raise MemoryError instead of mpatchError if lalloc() failed
Yuya Nishihara <yuya@tcha.org>
parents: 29742
diff changeset
126 setpyerr(r);
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
127 return result;
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
128 }
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
129
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
130 /* calculate size of a patched file directly */
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
131 static PyObject *patchedsize(PyObject *self, PyObject *args)
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
132 {
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
133 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
134 Py_ssize_t patchlen;
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
135 char *bin;
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
136
36620
186c6df3a373 py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()
Yuya Nishihara <yuya@tcha.org>
parents: 36361
diff changeset
137 if (!PyArg_ParseTuple(args, PY23("ls#", "ly#"), &orig, &bin, &patchlen))
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
138 return NULL;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
139
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
140 while (pos >= 0 && pos < patchlen) {
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
141 start = getbe32(bin + pos);
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
142 end = getbe32(bin + pos + 4);
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
143 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
144 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
145 break; /* sanity check */
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
146 pos += 12 + len;
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
147 outlen += start - last;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
148 last = end;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
149 outlen += len;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
150 }
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
151
20167
09e41ac6289d mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents: 16758
diff changeset
152 if (pos != patchlen) {
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
153 if (!PyErr_Occurred())
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
154 PyErr_SetString(mpatch_Error,
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
155 "patch cannot be decoded");
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
156 return NULL;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
157 }
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
158
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
159 outlen += orig - last;
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
160 return Py_BuildValue("l", outlen);
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
161 }
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1978
diff changeset
162
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
163 static PyMethodDef methods[] = {
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
164 {"patches", patches, METH_VARARGS, "apply a series of patches\n"},
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
165 {"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"},
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
166 {NULL, NULL},
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
167 };
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
168
32358
5fc3459d0493 mpatch: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 29749
diff changeset
169 static const int version = 1;
5fc3459d0493 mpatch: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 29749
diff changeset
170
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
171 #ifdef IS_PY3K
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
172 static struct PyModuleDef mpatch_module = {
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
173 PyModuleDef_HEAD_INIT, "mpatch", mpatch_doc, -1, methods,
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
174 };
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 PyMODINIT_FUNC PyInit_mpatch(void)
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
177 {
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
178 PyObject *m;
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
179
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
180 m = PyModule_Create(&mpatch_module);
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
181 if (m == NULL)
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
182 return NULL;
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
183
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
184 mpatch_Error =
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
185 PyErr_NewException("mercurial.cext.mpatch.mpatchError", NULL, NULL);
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
186 Py_INCREF(mpatch_Error);
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
187 PyModule_AddObject(m, "mpatchError", mpatch_Error);
32358
5fc3459d0493 mpatch: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 29749
diff changeset
188 PyModule_AddIntConstant(m, "version", version);
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
189
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
190 return m;
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
191 }
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
192 #else
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
193 PyMODINIT_FUNC initmpatch(void)
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
194 {
32358
5fc3459d0493 mpatch: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 29749
diff changeset
195 PyObject *m;
5fc3459d0493 mpatch: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 29749
diff changeset
196 m = Py_InitModule3("mpatch", methods, mpatch_doc);
36227
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
197 mpatch_Error =
69080ee1fb0e mpatch: allow clang-format oversight
Augie Fackler <augie@google.com>
parents: 35941
diff changeset
198 PyErr_NewException("mercurial.cext.mpatch.mpatchError", NULL, NULL);
32358
5fc3459d0493 mpatch: add version to help detect breaking binary changes
Jun Wu <quark@fb.com>
parents: 29749
diff changeset
199 PyModule_AddIntConstant(m, "version", version);
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents:
diff changeset
200 }
11360
2ac98313b26c mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
201 #endif