_pack.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2009 Jelmer Vernooij <jelmer@jelmer.uk>
  3. *
  4. * Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. * General Public License as public by the Free Software Foundation; version 2.0
  6. * or (at your option) any later version. You can redistribute it and/or
  7. * modify it under the terms of either of these two licenses.
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * You should have received a copy of the licenses; if not, see
  16. * <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. * and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. * License, Version 2.0.
  19. */
  20. #define PY_SSIZE_T_CLEAN
  21. #include <Python.h>
  22. #include <stdint.h>
  23. #if PY_MAJOR_VERSION >= 3
  24. #define PyInt_FromLong PyLong_FromLong
  25. #define PyString_AS_STRING PyBytes_AS_STRING
  26. #define PyString_AS_STRING PyBytes_AS_STRING
  27. #define PyString_Check PyBytes_Check
  28. #define PyString_CheckExact PyBytes_CheckExact
  29. #define PyString_FromStringAndSize PyBytes_FromStringAndSize
  30. #define PyString_FromString PyBytes_FromString
  31. #define PyString_GET_SIZE PyBytes_GET_SIZE
  32. #define PyString_Size PyBytes_Size
  33. #define _PyString_Join _PyBytes_Join
  34. #endif
  35. static PyObject *PyExc_ApplyDeltaError = NULL;
  36. static int py_is_sha(PyObject *sha)
  37. {
  38. if (!PyString_CheckExact(sha))
  39. return 0;
  40. if (PyString_Size(sha) != 20)
  41. return 0;
  42. return 1;
  43. }
  44. static size_t get_delta_header_size(uint8_t *delta, size_t *index, size_t length)
  45. {
  46. size_t size = 0;
  47. size_t i = 0;
  48. while ((*index) < length) {
  49. uint8_t cmd = delta[*index];
  50. (*index)++;
  51. size |= (cmd & ~0x80) << i;
  52. i += 7;
  53. if (!(cmd & 0x80))
  54. break;
  55. }
  56. return size;
  57. }
  58. static PyObject *py_chunked_as_string(PyObject *py_buf)
  59. {
  60. if (PyList_Check(py_buf)) {
  61. PyObject *sep = PyString_FromString("");
  62. if (sep == NULL) {
  63. PyErr_NoMemory();
  64. return NULL;
  65. }
  66. py_buf = _PyString_Join(sep, py_buf);
  67. Py_DECREF(sep);
  68. if (py_buf == NULL) {
  69. PyErr_NoMemory();
  70. return NULL;
  71. }
  72. } else if (PyString_Check(py_buf)) {
  73. Py_INCREF(py_buf);
  74. } else {
  75. PyErr_SetString(PyExc_TypeError,
  76. "src_buf is not a string or a list of chunks");
  77. return NULL;
  78. }
  79. return py_buf;
  80. }
  81. static PyObject *py_apply_delta(PyObject *self, PyObject *args)
  82. {
  83. uint8_t *src_buf, *delta;
  84. size_t src_buf_len, delta_len;
  85. size_t src_size, dest_size;
  86. size_t outindex = 0;
  87. size_t index;
  88. uint8_t *out;
  89. PyObject *ret, *py_src_buf, *py_delta, *ret_list;
  90. if (!PyArg_ParseTuple(args, "OO", &py_src_buf, &py_delta))
  91. return NULL;
  92. py_src_buf = py_chunked_as_string(py_src_buf);
  93. if (py_src_buf == NULL)
  94. return NULL;
  95. py_delta = py_chunked_as_string(py_delta);
  96. if (py_delta == NULL) {
  97. Py_DECREF(py_src_buf);
  98. return NULL;
  99. }
  100. src_buf = (uint8_t *)PyString_AS_STRING(py_src_buf);
  101. src_buf_len = (size_t)PyString_GET_SIZE(py_src_buf);
  102. delta = (uint8_t *)PyString_AS_STRING(py_delta);
  103. delta_len = (size_t)PyString_GET_SIZE(py_delta);
  104. index = 0;
  105. src_size = get_delta_header_size(delta, &index, delta_len);
  106. if (src_size != src_buf_len) {
  107. PyErr_Format(PyExc_ApplyDeltaError,
  108. "Unexpected source buffer size: %lu vs %ld", src_size, src_buf_len);
  109. Py_DECREF(py_src_buf);
  110. Py_DECREF(py_delta);
  111. return NULL;
  112. }
  113. dest_size = get_delta_header_size(delta, &index, delta_len);
  114. ret = PyString_FromStringAndSize(NULL, dest_size);
  115. if (ret == NULL) {
  116. PyErr_NoMemory();
  117. Py_DECREF(py_src_buf);
  118. Py_DECREF(py_delta);
  119. return NULL;
  120. }
  121. out = (uint8_t *)PyString_AS_STRING(ret);
  122. while (index < delta_len) {
  123. uint8_t cmd = delta[index];
  124. index++;
  125. if (cmd & 0x80) {
  126. size_t cp_off = 0, cp_size = 0;
  127. int i;
  128. for (i = 0; i < 4; i++) {
  129. if (cmd & (1 << i)) {
  130. uint8_t x = delta[index];
  131. index++;
  132. cp_off |= x << (i * 8);
  133. }
  134. }
  135. for (i = 0; i < 3; i++) {
  136. if (cmd & (1 << (4+i))) {
  137. uint8_t x = delta[index];
  138. index++;
  139. cp_size |= x << (i * 8);
  140. }
  141. }
  142. if (cp_size == 0)
  143. cp_size = 0x10000;
  144. if (cp_off + cp_size < cp_size ||
  145. cp_off + cp_size > src_size ||
  146. cp_size > dest_size)
  147. break;
  148. memcpy(out+outindex, src_buf+cp_off, cp_size);
  149. outindex += cp_size;
  150. dest_size -= cp_size;
  151. } else if (cmd != 0) {
  152. if (cmd > dest_size)
  153. break;
  154. memcpy(out+outindex, delta+index, cmd);
  155. outindex += cmd;
  156. index += cmd;
  157. dest_size -= cmd;
  158. } else {
  159. PyErr_SetString(PyExc_ApplyDeltaError, "Invalid opcode 0");
  160. Py_DECREF(ret);
  161. Py_DECREF(py_delta);
  162. Py_DECREF(py_src_buf);
  163. return NULL;
  164. }
  165. }
  166. Py_DECREF(py_src_buf);
  167. Py_DECREF(py_delta);
  168. if (index != delta_len) {
  169. PyErr_SetString(PyExc_ApplyDeltaError, "delta not empty");
  170. Py_DECREF(ret);
  171. return NULL;
  172. }
  173. if (dest_size != 0) {
  174. PyErr_SetString(PyExc_ApplyDeltaError, "dest size incorrect");
  175. Py_DECREF(ret);
  176. return NULL;
  177. }
  178. ret_list = Py_BuildValue("[N]", ret);
  179. if (ret_list == NULL) {
  180. Py_DECREF(ret);
  181. return NULL;
  182. }
  183. return ret_list;
  184. }
  185. static PyObject *py_bisect_find_sha(PyObject *self, PyObject *args)
  186. {
  187. PyObject *unpack_name;
  188. char *sha;
  189. Py_ssize_t sha_len;
  190. int start, end;
  191. #if PY_MAJOR_VERSION >= 3
  192. if (!PyArg_ParseTuple(args, "iiy#O", &start, &end,
  193. &sha, &sha_len, &unpack_name))
  194. #else
  195. if (!PyArg_ParseTuple(args, "iis#O", &start, &end,
  196. &sha, &sha_len, &unpack_name))
  197. #endif
  198. return NULL;
  199. if (sha_len != 20) {
  200. PyErr_SetString(PyExc_ValueError, "Sha is not 20 bytes long");
  201. return NULL;
  202. }
  203. if (start > end) {
  204. PyErr_SetString(PyExc_AssertionError, "start > end");
  205. return NULL;
  206. }
  207. while (start <= end) {
  208. PyObject *file_sha;
  209. Py_ssize_t i = (start + end)/2;
  210. int cmp;
  211. file_sha = PyObject_CallFunction(unpack_name, "i", i);
  212. if (file_sha == NULL) {
  213. return NULL;
  214. }
  215. if (!py_is_sha(file_sha)) {
  216. PyErr_SetString(PyExc_TypeError, "unpack_name returned non-sha object");
  217. Py_DECREF(file_sha);
  218. return NULL;
  219. }
  220. cmp = memcmp(PyString_AS_STRING(file_sha), sha, 20);
  221. Py_DECREF(file_sha);
  222. if (cmp < 0)
  223. start = i + 1;
  224. else if (cmp > 0)
  225. end = i - 1;
  226. else {
  227. return PyInt_FromLong(i);
  228. }
  229. }
  230. Py_RETURN_NONE;
  231. }
  232. static PyMethodDef py_pack_methods[] = {
  233. { "apply_delta", (PyCFunction)py_apply_delta, METH_VARARGS, NULL },
  234. { "bisect_find_sha", (PyCFunction)py_bisect_find_sha, METH_VARARGS, NULL },
  235. { NULL, NULL, 0, NULL }
  236. };
  237. static PyObject *
  238. moduleinit(void)
  239. {
  240. PyObject *m;
  241. PyObject *errors_module;
  242. #if PY_MAJOR_VERSION >= 3
  243. static struct PyModuleDef moduledef = {
  244. PyModuleDef_HEAD_INIT,
  245. "_pack", /* m_name */
  246. NULL, /* m_doc */
  247. -1, /* m_size */
  248. py_pack_methods, /* m_methods */
  249. NULL, /* m_reload */
  250. NULL, /* m_traverse */
  251. NULL, /* m_clear*/
  252. NULL, /* m_free */
  253. };
  254. #endif
  255. errors_module = PyImport_ImportModule("dulwich.errors");
  256. if (errors_module == NULL)
  257. return NULL;
  258. PyExc_ApplyDeltaError = PyObject_GetAttrString(errors_module, "ApplyDeltaError");
  259. Py_DECREF(errors_module);
  260. if (PyExc_ApplyDeltaError == NULL)
  261. return NULL;
  262. #if PY_MAJOR_VERSION >= 3
  263. m = PyModule_Create(&moduledef);
  264. #else
  265. m = Py_InitModule3("_pack", py_pack_methods, NULL);
  266. #endif
  267. if (m == NULL)
  268. return NULL;
  269. return m;
  270. }
  271. #if PY_MAJOR_VERSION >= 3
  272. PyMODINIT_FUNC
  273. PyInit__pack(void)
  274. {
  275. return moduleinit();
  276. }
  277. #else
  278. PyMODINIT_FUNC
  279. init_pack(void)
  280. {
  281. moduleinit();
  282. }
  283. #endif