_pack.c 5.6 KB

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