_pack.c 7.3 KB

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