_pack.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. } else if (cmd != 0) {
  136. memcpy(out+outindex, delta+index, cmd);
  137. outindex += cmd;
  138. index += cmd;
  139. } else {
  140. PyErr_SetString(PyExc_ValueError, "Invalid opcode 0");
  141. Py_DECREF(ret);
  142. Py_DECREF(py_delta);
  143. Py_DECREF(py_src_buf);
  144. return NULL;
  145. }
  146. }
  147. Py_DECREF(py_src_buf);
  148. Py_DECREF(py_delta);
  149. if (index != delta_len) {
  150. PyErr_SetString(PyExc_ValueError, "delta not empty");
  151. Py_DECREF(ret);
  152. return NULL;
  153. }
  154. if (dest_size != outindex) {
  155. PyErr_SetString(PyExc_ValueError, "dest size incorrect");
  156. Py_DECREF(ret);
  157. return NULL;
  158. }
  159. ret_list = Py_BuildValue("[N]", ret);
  160. if (ret_list == NULL) {
  161. Py_DECREF(ret);
  162. return NULL;
  163. }
  164. return ret_list;
  165. }
  166. static PyObject *py_bisect_find_sha(PyObject *self, PyObject *args)
  167. {
  168. PyObject *unpack_name;
  169. char *sha;
  170. int sha_len;
  171. int start, end;
  172. if (!PyArg_ParseTuple(args, "iis#O", &start, &end,
  173. &sha, &sha_len, &unpack_name))
  174. return NULL;
  175. if (sha_len != 20) {
  176. PyErr_SetString(PyExc_ValueError, "Sha is not 20 bytes long");
  177. return NULL;
  178. }
  179. if (start > end) {
  180. PyErr_SetString(PyExc_AssertionError, "start > end");
  181. return NULL;
  182. }
  183. while (start <= end) {
  184. PyObject *file_sha;
  185. int i = (start + end)/2;
  186. int cmp;
  187. file_sha = PyObject_CallFunction(unpack_name, "i", i);
  188. if (file_sha == NULL) {
  189. return NULL;
  190. }
  191. if (!py_is_sha(file_sha)) {
  192. PyErr_SetString(PyExc_TypeError, "unpack_name returned non-sha object");
  193. Py_DECREF(file_sha);
  194. return NULL;
  195. }
  196. cmp = memcmp(PyString_AsString(file_sha), sha, 20);
  197. Py_DECREF(file_sha);
  198. if (cmp < 0)
  199. start = i + 1;
  200. else if (cmp > 0)
  201. end = i - 1;
  202. else {
  203. return PyInt_FromLong(i);
  204. }
  205. }
  206. Py_RETURN_NONE;
  207. }
  208. static PyMethodDef py_pack_methods[] = {
  209. { "apply_delta", (PyCFunction)py_apply_delta, METH_VARARGS, NULL },
  210. { "bisect_find_sha", (PyCFunction)py_bisect_find_sha, METH_VARARGS, NULL },
  211. { NULL, NULL, 0, NULL }
  212. };
  213. void init_pack(void)
  214. {
  215. PyObject *m;
  216. m = Py_InitModule3("_pack", py_pack_methods, NULL);
  217. if (m == NULL)
  218. return;
  219. }