_pack.c 5.5 KB

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