_objects.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 of the License.
  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. #define hexbyte(x) (isdigit(x)?(x)-'0':(x)-'a'+0xa)
  21. #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
  22. static PyObject *py_hex_to_sha(PyObject *self, PyObject *py_hexsha)
  23. {
  24. char *hexsha;
  25. char sha[20];
  26. int i;
  27. if (!PyString_Check(py_hexsha)) {
  28. PyErr_SetString(PyExc_TypeError, "hex sha is not a string");
  29. return NULL;
  30. }
  31. if (PyString_Size(py_hexsha) != 40) {
  32. PyErr_SetString(PyExc_ValueError, "hex sha is not 40 bytes long");
  33. return NULL;
  34. }
  35. hexsha = PyString_AsString(py_hexsha);
  36. for (i = 0; i < 20; i++) {
  37. sha[i] = (hexbyte(hexsha[i*2]) << 4) + hexbyte(hexsha[i*2+1]);
  38. }
  39. return PyString_FromStringAndSize(sha, 20);
  40. }
  41. static PyObject *sha_to_pyhex(const unsigned char *sha)
  42. {
  43. char hexsha[41];
  44. int i;
  45. for (i = 0; i < 20; i++) {
  46. hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
  47. hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
  48. }
  49. return PyString_FromStringAndSize(hexsha, 40);
  50. }
  51. static PyObject *py_sha_to_hex(PyObject *self, PyObject *py_sha)
  52. {
  53. if (!PyString_Check(py_sha)) {
  54. PyErr_SetString(PyExc_TypeError, "sha is not a string");
  55. return NULL;
  56. }
  57. if (PyString_Size(py_sha) != 20) {
  58. PyErr_SetString(PyExc_ValueError, "sha is not 20 bytes long");
  59. return NULL;
  60. }
  61. return sha_to_pyhex((unsigned char *)PyString_AsString(py_sha));
  62. }
  63. static PyObject *py_parse_tree(PyObject *self, PyObject *args)
  64. {
  65. char *text, *end;
  66. int len, namelen;
  67. PyObject *ret, *item, *name;
  68. if (!PyArg_ParseTuple(args, "s#", &text, &len))
  69. return NULL;
  70. ret = PyDict_New();
  71. if (ret == NULL) {
  72. return NULL;
  73. }
  74. end = text + len;
  75. while (text < end) {
  76. long mode;
  77. mode = strtol(text, &text, 8);
  78. if (*text != ' ') {
  79. PyErr_SetString(PyExc_RuntimeError, "Expected space");
  80. Py_DECREF(ret);
  81. return NULL;
  82. }
  83. text++;
  84. namelen = strlen(text);
  85. name = PyString_FromStringAndSize(text, namelen);
  86. if (name == NULL) {
  87. Py_DECREF(ret);
  88. return NULL;
  89. }
  90. item = Py_BuildValue("(lN)", mode, sha_to_pyhex((unsigned char *)text+namelen+1));
  91. if (item == NULL) {
  92. Py_DECREF(ret);
  93. Py_DECREF(name);
  94. return NULL;
  95. }
  96. PyDict_SetItem(ret, name, item);
  97. Py_DECREF(name);
  98. Py_DECREF(item);
  99. text += namelen+21;
  100. }
  101. return ret;
  102. }
  103. static PyMethodDef py_objects_methods[] = {
  104. { "hex_to_sha", (PyCFunction)py_hex_to_sha, METH_O, NULL },
  105. { "sha_to_hex", (PyCFunction)py_sha_to_hex, METH_O, NULL },
  106. { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL, },
  107. };
  108. void init_objects(void)
  109. {
  110. PyObject *m;
  111. m = Py_InitModule3("_objects", py_objects_methods, NULL);
  112. if (m == NULL)
  113. return;
  114. }