pyldb: added binary_encode() and binary_decode() methods
authorAndrew Tridgell <tridge@samba.org>
Thu, 28 Jul 2011 07:03:06 +0000 (17:03 +1000)
committerAndrew Tridgell <tridge@samba.org>
Fri, 29 Jul 2011 08:17:44 +0000 (18:17 +1000)
this gives access to RFC2254 encoding from python

Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>
Pair-Programmed-With: Amitay Isaacs <amitay@gmail.com>

lib/ldb/pyldb.c
lib/ldb/tests/python/api.py

index adec424aa1e215f7cfb39d3c3083b8799b8d8ae6..c92d64dd12273f05e36a8fa80f5ec323beb7886c 100644 (file)
@@ -3170,6 +3170,53 @@ static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
        return PyBool_FromLong(ldb_valid_attr_name(name));
 }
 
+/*
+  encode a string using RFC2254 rules
+ */
+static PyObject *py_binary_encode(PyObject *self, PyObject *args)
+{
+       char *str, *encoded;
+       Py_ssize_t size;
+       struct ldb_val val;
+       PyObject *ret;
+
+       if (!PyArg_ParseTuple(args, "s#", &str, &size))
+               return NULL;
+       val.data = (uint8_t *)str;
+       val.length = size;
+
+       encoded = ldb_binary_encode(NULL, val);
+       if (encoded == NULL) {
+               PyErr_SetString(PyExc_TypeError, "unable to encode binary string");
+               return NULL;
+       }
+       ret = PyString_FromString(encoded);
+       talloc_free(encoded);
+       return ret;
+}
+
+/*
+  decode a string using RFC2254 rules
+ */
+static PyObject *py_binary_decode(PyObject *self, PyObject *args)
+{
+       char *str;
+       struct ldb_val val;
+       PyObject *ret;
+
+       if (!PyArg_ParseTuple(args, "s", &str))
+               return NULL;
+
+       val = ldb_binary_decode(NULL, str);
+       if (val.data == NULL) {
+               PyErr_SetString(PyExc_TypeError, "unable to decode binary string");
+               return NULL;
+       }
+       ret = Py_BuildValue("s#", val.data, val.length);
+       talloc_free(val.data);
+       return ret;
+}
+
 static PyMethodDef py_ldb_global_methods[] = {
        { "register_module", py_register_module, METH_VARARGS, 
                "S.register_module(module) -> None\n"
@@ -3185,6 +3232,12 @@ static PyMethodDef py_ldb_global_methods[] = {
                "Check whether the supplied name is a valid attribute name." },
        { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
                NULL },
+       { "binary_encode", py_binary_encode, METH_VARARGS,
+               "S.binary_encode(string) -> string\n"
+               "Perform a RFC2254 binary encoding on a string" },
+       { "binary_decode", py_binary_decode, METH_VARARGS,
+               "S.binary_decode(string) -> string\n"
+               "Perform a RFC2254 binary decode on a string" },
        { NULL }
 };
 
index bd10b0b293f7783179e7cac9b1d8476ae5e87965..6a8df25b7bbc1b404418c36ad20a5ab63b96c93a 100755 (executable)
@@ -30,6 +30,10 @@ class NoContextTests(unittest.TestCase):
         self.assertEquals(0, ldb.string_to_time("19700101000000.0Z"))
         self.assertEquals(1195499412, ldb.string_to_time("20071119191012.0Z"))
 
+    def test_binary_encode(self):
+        encoded = self.binary_encode('test\\x')
+        decoded = self.binary_decode(encoded)
+        self.assertEquals(decoded, 'test\\x')
 
 class SimpleLdb(unittest.TestCase):