_pack.c 5.9 KB

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