|
| 1 | +/* See http://www.python-ldap.org/ for details. |
| 2 | + * $Id: berval.c,v 1.1 2009/08/17 01:49:47 leonard Exp $ */ |
| 3 | + |
| 4 | +#include "common.h" |
| 5 | +#include "berval.h" |
| 6 | + |
| 7 | +/* |
| 8 | + * Converts a Python object into a data for a berval structure. |
| 9 | + * |
| 10 | + * New memory is allocated, and the content of the object is copied into it. |
| 11 | + * Then the (pre-existing) berval structure's field are filled in with pointer |
| 12 | + * and length data. |
| 13 | + * |
| 14 | + * The source object must implement the buffer interface, or be None. |
| 15 | + * If the source object is None, bv->bv_val will be set to NULL and bv_len to 0. |
| 16 | + * Otherwise, bv->bv_val will be non-NULL (even for zero-length data). |
| 17 | + * This allows the caller to distinguish a None argument as something special. |
| 18 | + * |
| 19 | + * Returns 0 on failure, leaving *bv unchanged, and setting an error. |
| 20 | + * Returns 1 on success: the berval must be freed with LDAPberval_release(). |
| 21 | + */ |
| 22 | +int |
| 23 | +LDAPberval_from_object(PyObject *obj, struct berval *bv) |
| 24 | +{ |
| 25 | + const void *data; |
| 26 | + char *datacp; |
| 27 | + Py_ssize_t len; |
| 28 | + |
| 29 | + if (PyNone_Check(obj)) { |
| 30 | + bv->bv_len = 0; |
| 31 | + bv->bv_val = NULL; |
| 32 | + return 1; |
| 33 | + } |
| 34 | + |
| 35 | + if (!PyObject_AsReadBuffer(obj, &data, &len)) |
| 36 | + return 0; |
| 37 | + |
| 38 | + datacp = PyMem_MALLOC(len ? len : 1); |
| 39 | + if (!datacp) { |
| 40 | + PyErr_NoMemory(); |
| 41 | + return 0; |
| 42 | + } |
| 43 | + memcpy(datacp, data, len); |
| 44 | + |
| 45 | + bv->bv_len = len; |
| 46 | + bv->bv_val = datacp; |
| 47 | + return 1; |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * Returns true if the object could be used to initialize a berval structure |
| 52 | + * with LDAPberval_from_object() |
| 53 | + */ |
| 54 | +int |
| 55 | +LDAPberval_from_object_check(PyObject *obj) |
| 56 | +{ |
| 57 | + return PyNone_Check(obj) || |
| 58 | + PyObject_CheckReadBuffer(obj); |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * Releases memory allocated by LDAPberval_from_object(). |
| 63 | + * Has no effect if the berval pointer is NULL or the berval data is NULL. |
| 64 | + */ |
| 65 | +void |
| 66 | +LDAPberval_release(struct berval *bv) { |
| 67 | + if (bv && bv->bv_val) { |
| 68 | + PyMem_FREE(bv->bv_val); |
| 69 | + bv->bv_len = 0; |
| 70 | + bv->bv_val = NULL; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/* |
| 75 | + * Copies out the data from a berval, and returns it as a new Python object, |
| 76 | + * Returns None if the berval pointer is NULL. |
| 77 | + * |
| 78 | + * Note that this function is not the exact inverse of LDAPberval_from_object |
| 79 | + * with regards to the NULL/None conversion. |
| 80 | + * |
| 81 | + * Returns a new Python object on success, or NULL on failure. |
| 82 | + */ |
| 83 | +PyObject * |
| 84 | +LDAPberval_to_object(const struct berval *bv) |
| 85 | +{ |
| 86 | + PyObject *ret = NULL; |
| 87 | + |
| 88 | + if (!bv) { |
| 89 | + ret = Py_None; |
| 90 | + Py_INCREF(ret); |
| 91 | + } |
| 92 | + else { |
| 93 | + ret = PyString_FromStringAndSize(bv->bv_val, bv->bv_len); |
| 94 | + } |
| 95 | + |
| 96 | + return ret; |
| 97 | +} |
0 commit comments