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