From 7ce4aca029473b219d053221ced1e3686ce2d3ad Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 28 Jul 2011 17:03:06 +1000 Subject: pyldb: added binary_encode() and binary_decode() methods this gives access to RFC2254 encoding from python Pair-Programmed-With: Andrew Bartlett Pair-Programmed-With: Amitay Isaacs --- lib/ldb/pyldb.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ lib/ldb/tests/python/api.py | 4 ++++ 2 files changed, 57 insertions(+) (limited to 'lib') diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index adec424aa1..c92d64dd12 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -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 } }; diff --git a/lib/ldb/tests/python/api.py b/lib/ldb/tests/python/api.py index bd10b0b293..6a8df25b7b 100755 --- a/lib/ldb/tests/python/api.py +++ b/lib/ldb/tests/python/api.py @@ -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): -- cgit