_pack.c 6.6 KB

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