_pack.c 4.8 KB

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