_pack.c 7.1 KB

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