Show More
@@ -1,55 +1,54 | |||
|
1 | 1 | # Files that just need to be migrated to the formatter. |
|
2 | 2 | # Do not add new files here! |
|
3 | 3 | mercurial/cext/dirs.c |
|
4 | 4 | mercurial/cext/manifest.c |
|
5 | mercurial/cext/mpatch.c | |
|
6 | 5 | mercurial/cext/osutil.c |
|
7 | 6 | mercurial/cext/revlog.c |
|
8 | 7 | # Vendored code that we should never format: |
|
9 | 8 | contrib/python-zstandard/c-ext/bufferutil.c |
|
10 | 9 | contrib/python-zstandard/c-ext/compressiondict.c |
|
11 | 10 | contrib/python-zstandard/c-ext/compressionparams.c |
|
12 | 11 | contrib/python-zstandard/c-ext/compressionwriter.c |
|
13 | 12 | contrib/python-zstandard/c-ext/compressobj.c |
|
14 | 13 | contrib/python-zstandard/c-ext/compressor.c |
|
15 | 14 | contrib/python-zstandard/c-ext/compressoriterator.c |
|
16 | 15 | contrib/python-zstandard/c-ext/constants.c |
|
17 | 16 | contrib/python-zstandard/c-ext/decompressionwriter.c |
|
18 | 17 | contrib/python-zstandard/c-ext/decompressobj.c |
|
19 | 18 | contrib/python-zstandard/c-ext/decompressor.c |
|
20 | 19 | contrib/python-zstandard/c-ext/decompressoriterator.c |
|
21 | 20 | contrib/python-zstandard/c-ext/frameparams.c |
|
22 | 21 | contrib/python-zstandard/c-ext/python-zstandard.h |
|
23 | 22 | contrib/python-zstandard/zstd.c |
|
24 | 23 | contrib/python-zstandard/zstd/common/bitstream.h |
|
25 | 24 | contrib/python-zstandard/zstd/common/entropy_common.c |
|
26 | 25 | contrib/python-zstandard/zstd/common/error_private.c |
|
27 | 26 | contrib/python-zstandard/zstd/common/error_private.h |
|
28 | 27 | contrib/python-zstandard/zstd/common/fse.h |
|
29 | 28 | contrib/python-zstandard/zstd/common/fse_decompress.c |
|
30 | 29 | contrib/python-zstandard/zstd/common/huf.h |
|
31 | 30 | contrib/python-zstandard/zstd/common/mem.h |
|
32 | 31 | contrib/python-zstandard/zstd/common/pool.c |
|
33 | 32 | contrib/python-zstandard/zstd/common/pool.h |
|
34 | 33 | contrib/python-zstandard/zstd/common/threading.c |
|
35 | 34 | contrib/python-zstandard/zstd/common/threading.h |
|
36 | 35 | contrib/python-zstandard/zstd/common/xxhash.c |
|
37 | 36 | contrib/python-zstandard/zstd/common/xxhash.h |
|
38 | 37 | contrib/python-zstandard/zstd/common/zstd_common.c |
|
39 | 38 | contrib/python-zstandard/zstd/common/zstd_errors.h |
|
40 | 39 | contrib/python-zstandard/zstd/common/zstd_internal.h |
|
41 | 40 | contrib/python-zstandard/zstd/compress/fse_compress.c |
|
42 | 41 | contrib/python-zstandard/zstd/compress/huf_compress.c |
|
43 | 42 | contrib/python-zstandard/zstd/compress/zstd_compress.c |
|
44 | 43 | contrib/python-zstandard/zstd/compress/zstd_opt.h |
|
45 | 44 | contrib/python-zstandard/zstd/compress/zstdmt_compress.c |
|
46 | 45 | contrib/python-zstandard/zstd/compress/zstdmt_compress.h |
|
47 | 46 | contrib/python-zstandard/zstd/decompress/huf_decompress.c |
|
48 | 47 | contrib/python-zstandard/zstd/decompress/zstd_decompress.c |
|
49 | 48 | contrib/python-zstandard/zstd/dictBuilder/cover.c |
|
50 | 49 | contrib/python-zstandard/zstd/dictBuilder/divsufsort.c |
|
51 | 50 | contrib/python-zstandard/zstd/dictBuilder/divsufsort.h |
|
52 | 51 | contrib/python-zstandard/zstd/dictBuilder/zdict.c |
|
53 | 52 | contrib/python-zstandard/zstd/dictBuilder/zdict.h |
|
54 | 53 | contrib/python-zstandard/zstd/zstd.h |
|
55 | 54 | hgext/fsmonitor/pywatchman/bser.c |
@@ -1,201 +1,195 | |||
|
1 | 1 | /* |
|
2 | 2 | mpatch.c - efficient binary patching for Mercurial |
|
3 | 3 | |
|
4 | 4 | This implements a patch algorithm that's O(m + nlog n) where m is the |
|
5 | 5 | size of the output and n is the number of patches. |
|
6 | 6 | |
|
7 | 7 | Given a list of binary patches, it unpacks each into a hunk list, |
|
8 | 8 | then combines the hunk lists with a treewise recursion to form a |
|
9 | 9 | single hunk list. This hunk list is then applied to the original |
|
10 | 10 | text. |
|
11 | 11 | |
|
12 | 12 | The text (or binary) fragments are copied directly from their source |
|
13 | 13 | Python objects into a preallocated output string to avoid the |
|
14 | 14 | allocation of intermediate Python objects. Working memory is about 2x |
|
15 | 15 | the total number of hunks. |
|
16 | 16 | |
|
17 | 17 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
18 | 18 | |
|
19 | 19 | This software may be used and distributed according to the terms |
|
20 | 20 | of the GNU General Public License, incorporated herein by reference. |
|
21 | 21 | */ |
|
22 | 22 | |
|
23 | 23 | #define PY_SSIZE_T_CLEAN |
|
24 | 24 | #include <Python.h> |
|
25 | 25 | #include <stdlib.h> |
|
26 | 26 | #include <string.h> |
|
27 | 27 | |
|
28 | 28 | #include "bitmanipulation.h" |
|
29 | 29 | #include "compat.h" |
|
30 | 30 | #include "mpatch.h" |
|
31 | 31 | #include "util.h" |
|
32 | 32 | |
|
33 | 33 | static char mpatch_doc[] = "Efficient binary patching."; |
|
34 | 34 | static PyObject *mpatch_Error; |
|
35 | 35 | |
|
36 | 36 | static void setpyerr(int r) |
|
37 | 37 | { |
|
38 | 38 | switch (r) { |
|
39 | 39 | case MPATCH_ERR_NO_MEM: |
|
40 | 40 | PyErr_NoMemory(); |
|
41 | 41 | break; |
|
42 | 42 | case MPATCH_ERR_CANNOT_BE_DECODED: |
|
43 | 43 | PyErr_SetString(mpatch_Error, "patch cannot be decoded"); |
|
44 | 44 | break; |
|
45 | 45 | case MPATCH_ERR_INVALID_PATCH: |
|
46 | 46 | PyErr_SetString(mpatch_Error, "invalid patch"); |
|
47 | 47 | break; |
|
48 | 48 | } |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | struct mpatch_flist *cpygetitem(void *bins, ssize_t pos) |
|
52 | 52 | { |
|
53 | 53 | const char *buffer; |
|
54 | 54 | struct mpatch_flist *res; |
|
55 | 55 | ssize_t blen; |
|
56 | 56 | int r; |
|
57 | 57 | |
|
58 | PyObject *tmp = PyList_GetItem((PyObject*)bins, pos); | |
|
58 | PyObject *tmp = PyList_GetItem((PyObject *)bins, pos); | |
|
59 | 59 | if (!tmp) |
|
60 | 60 | return NULL; |
|
61 | if (PyObject_AsCharBuffer(tmp, &buffer, (Py_ssize_t*)&blen)) | |
|
61 | if (PyObject_AsCharBuffer(tmp, &buffer, (Py_ssize_t *)&blen)) | |
|
62 | 62 | return NULL; |
|
63 | 63 | if ((r = mpatch_decode(buffer, blen, &res)) < 0) { |
|
64 | 64 | if (!PyErr_Occurred()) |
|
65 | 65 | setpyerr(r); |
|
66 | 66 | return NULL; |
|
67 | 67 | } |
|
68 | 68 | return res; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | static PyObject * | |
|
72 | patches(PyObject *self, PyObject *args) | |
|
71 | static PyObject *patches(PyObject *self, PyObject *args) | |
|
73 | 72 | { |
|
74 | 73 | PyObject *text, *bins, *result; |
|
75 | 74 | struct mpatch_flist *patch; |
|
76 | 75 | const char *in; |
|
77 | 76 | int r = 0; |
|
78 | 77 | char *out; |
|
79 | 78 | Py_ssize_t len, outlen, inlen; |
|
80 | 79 | |
|
81 | 80 | if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins)) |
|
82 | 81 | return NULL; |
|
83 | 82 | |
|
84 | 83 | len = PyList_Size(bins); |
|
85 | 84 | if (!len) { |
|
86 | 85 | /* nothing to do */ |
|
87 | 86 | Py_INCREF(text); |
|
88 | 87 | return text; |
|
89 | 88 | } |
|
90 | 89 | |
|
91 | 90 | if (PyObject_AsCharBuffer(text, &in, &inlen)) |
|
92 | 91 | return NULL; |
|
93 | 92 | |
|
94 | 93 | patch = mpatch_fold(bins, cpygetitem, 0, len); |
|
95 | 94 | if (!patch) { /* error already set or memory error */ |
|
96 | 95 | if (!PyErr_Occurred()) |
|
97 | 96 | PyErr_NoMemory(); |
|
98 | 97 | return NULL; |
|
99 | 98 | } |
|
100 | 99 | |
|
101 | 100 | outlen = mpatch_calcsize(inlen, patch); |
|
102 | 101 | if (outlen < 0) { |
|
103 | 102 | r = (int)outlen; |
|
104 | 103 | result = NULL; |
|
105 | 104 | goto cleanup; |
|
106 | 105 | } |
|
107 | 106 | result = PyBytes_FromStringAndSize(NULL, outlen); |
|
108 | 107 | if (!result) { |
|
109 | 108 | result = NULL; |
|
110 | 109 | goto cleanup; |
|
111 | 110 | } |
|
112 | 111 | out = PyBytes_AsString(result); |
|
113 | 112 | r = mpatch_apply(out, in, inlen, patch); |
|
114 | 113 | if (r < 0) { |
|
115 | 114 | Py_DECREF(result); |
|
116 | 115 | result = NULL; |
|
117 | 116 | } |
|
118 | 117 | cleanup: |
|
119 | 118 | mpatch_lfree(patch); |
|
120 | 119 | if (!result && !PyErr_Occurred()) |
|
121 | 120 | setpyerr(r); |
|
122 | 121 | return result; |
|
123 | 122 | } |
|
124 | 123 | |
|
125 | 124 | /* calculate size of a patched file directly */ |
|
126 | static PyObject * | |
|
127 | patchedsize(PyObject *self, PyObject *args) | |
|
125 | static PyObject *patchedsize(PyObject *self, PyObject *args) | |
|
128 | 126 | { |
|
129 | 127 | long orig, start, end, len, outlen = 0, last = 0, pos = 0; |
|
130 | 128 | Py_ssize_t patchlen; |
|
131 | 129 | char *bin; |
|
132 | 130 | |
|
133 | 131 | if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen)) |
|
134 | 132 | return NULL; |
|
135 | 133 | |
|
136 | 134 | while (pos >= 0 && pos < patchlen) { |
|
137 | 135 | start = getbe32(bin + pos); |
|
138 | 136 | end = getbe32(bin + pos + 4); |
|
139 | 137 | len = getbe32(bin + pos + 8); |
|
140 | 138 | if (start > end) |
|
141 | 139 | break; /* sanity check */ |
|
142 | 140 | pos += 12 + len; |
|
143 | 141 | outlen += start - last; |
|
144 | 142 | last = end; |
|
145 | 143 | outlen += len; |
|
146 | 144 | } |
|
147 | 145 | |
|
148 | 146 | if (pos != patchlen) { |
|
149 | 147 | if (!PyErr_Occurred()) |
|
150 |
PyErr_SetString(mpatch_Error, |
|
|
148 | PyErr_SetString(mpatch_Error, | |
|
149 | "patch cannot be decoded"); | |
|
151 | 150 | return NULL; |
|
152 | 151 | } |
|
153 | 152 | |
|
154 | 153 | outlen += orig - last; |
|
155 | 154 | return Py_BuildValue("l", outlen); |
|
156 | 155 | } |
|
157 | 156 | |
|
158 | 157 | static PyMethodDef methods[] = { |
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|
158 | {"patches", patches, METH_VARARGS, "apply a series of patches\n"}, | |
|
159 | {"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"}, | |
|
160 | {NULL, NULL}, | |
|
162 | 161 | }; |
|
163 | 162 | |
|
164 | 163 | static const int version = 1; |
|
165 | 164 | |
|
166 | 165 | #ifdef IS_PY3K |
|
167 | 166 | static struct PyModuleDef mpatch_module = { |
|
168 | PyModuleDef_HEAD_INIT, | |
|
169 | "mpatch", | |
|
170 | mpatch_doc, | |
|
171 | -1, | |
|
172 | methods | |
|
167 | PyModuleDef_HEAD_INIT, "mpatch", mpatch_doc, -1, methods, | |
|
173 | 168 | }; |
|
174 | 169 | |
|
175 | 170 | PyMODINIT_FUNC PyInit_mpatch(void) |
|
176 | 171 | { |
|
177 | 172 | PyObject *m; |
|
178 | 173 | |
|
179 | 174 | m = PyModule_Create(&mpatch_module); |
|
180 | 175 | if (m == NULL) |
|
181 | 176 | return NULL; |
|
182 | 177 | |
|
183 | mpatch_Error = PyErr_NewException("mercurial.cext.mpatch.mpatchError", | |
|
184 | NULL, NULL); | |
|
178 | mpatch_Error = | |
|
179 | PyErr_NewException("mercurial.cext.mpatch.mpatchError", NULL, NULL); | |
|
185 | 180 | Py_INCREF(mpatch_Error); |
|
186 | 181 | PyModule_AddObject(m, "mpatchError", mpatch_Error); |
|
187 | 182 | PyModule_AddIntConstant(m, "version", version); |
|
188 | 183 | |
|
189 | 184 | return m; |
|
190 | 185 | } |
|
191 | 186 | #else |
|
192 | PyMODINIT_FUNC | |
|
193 | initmpatch(void) | |
|
187 | PyMODINIT_FUNC initmpatch(void) | |
|
194 | 188 | { |
|
195 | 189 | PyObject *m; |
|
196 | 190 | m = Py_InitModule3("mpatch", methods, mpatch_doc); |
|
197 | mpatch_Error = PyErr_NewException("mercurial.cext.mpatch.mpatchError", | |
|
198 | NULL, NULL); | |
|
191 | mpatch_Error = | |
|
192 | PyErr_NewException("mercurial.cext.mpatch.mpatchError", NULL, NULL); | |
|
199 | 193 | PyModule_AddIntConstant(m, "version", version); |
|
200 | 194 | } |
|
201 | 195 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now