diff options
author | Andrew Tridgell <tridge@samba.org> | 2011-07-28 17:03:06 +1000 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2011-07-29 18:17:44 +1000 |
commit | 7ce4aca029473b219d053221ced1e3686ce2d3ad (patch) | |
tree | 90b75e6de4c32f9e27039d818c0ed8cfff8594a3 /lib | |
parent | d815ce094e7a495fb2ddf0668015c5a1b9bb1dc4 (diff) | |
download | samba-7ce4aca029473b219d053221ced1e3686ce2d3ad.tar.gz samba-7ce4aca029473b219d053221ced1e3686ce2d3ad.tar.bz2 samba-7ce4aca029473b219d053221ced1e3686ce2d3ad.zip |
pyldb: added binary_encode() and binary_decode() methods
this gives access to RFC2254 encoding from python
Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>
Pair-Programmed-With: Amitay Isaacs <amitay@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ldb/pyldb.c | 53 | ||||
-rwxr-xr-x | lib/ldb/tests/python/api.py | 4 |
2 files changed, 57 insertions, 0 deletions
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): |