|
3 | 3 | #include "common.h"
|
4 | 4 | #include "berval.h"
|
5 | 5 |
|
6 |
| -/* |
7 |
| - * Converts a Python object into a data for a berval structure. |
8 |
| - * |
9 |
| - * New memory is allocated, and the content of the object is copied into it. |
10 |
| - * Then the (pre-existing) berval structure's field are filled in with pointer |
11 |
| - * and length data. |
12 |
| - * |
13 |
| - * The source object must implement the buffer interface, or be None. |
14 |
| - * If the source object is None, bv->bv_val will be set to NULL and bv_len to 0. |
15 |
| - * Otherwise, bv->bv_val will be non-NULL (even for zero-length data). |
16 |
| - * This allows the caller to distinguish a None argument as something special. |
17 |
| - * |
18 |
| - * Returns 0 on failure, leaving *bv unchanged, and setting an error. |
19 |
| - * Returns 1 on success: the berval must be freed with LDAPberval_release(). |
20 |
| - */ |
21 |
| -int |
22 |
| -LDAPberval_from_object(PyObject *obj, struct berval *bv) |
23 |
| -{ |
24 |
| - const void *data; |
25 |
| - char *datacp; |
26 |
| - Py_ssize_t len; |
27 |
| - |
28 |
| - if (PyNone_Check(obj)) { |
29 |
| - bv->bv_len = 0; |
30 |
| - bv->bv_val = NULL; |
31 |
| - return 1; |
32 |
| - } |
33 |
| - |
34 |
| - if (!PyObject_AsReadBuffer(obj, &data, &len)) |
35 |
| - return 0; |
36 |
| - |
37 |
| - datacp = PyMem_MALLOC(len ? len : 1); |
38 |
| - if (!datacp) { |
39 |
| - PyErr_NoMemory(); |
40 |
| - return 0; |
41 |
| - } |
42 |
| - memcpy(datacp, data, len); |
43 |
| - |
44 |
| - bv->bv_len = len; |
45 |
| - bv->bv_val = datacp; |
46 |
| - return 1; |
47 |
| -} |
48 |
| - |
49 |
| -/* |
50 |
| - * Returns true if the object could be used to initialize a berval structure |
51 |
| - * with LDAPberval_from_object() |
52 |
| - */ |
53 |
| -int |
54 |
| -LDAPberval_from_object_check(PyObject *obj) |
55 |
| -{ |
56 |
| - return PyNone_Check(obj) || |
57 |
| - PyObject_CheckReadBuffer(obj); |
58 |
| -} |
59 |
| - |
60 |
| -/* |
61 |
| - * Releases memory allocated by LDAPberval_from_object(). |
62 |
| - * Has no effect if the berval pointer is NULL or the berval data is NULL. |
63 |
| - */ |
64 |
| -void |
65 |
| -LDAPberval_release(struct berval *bv) { |
66 |
| - if (bv && bv->bv_val) { |
67 |
| - PyMem_FREE(bv->bv_val); |
68 |
| - bv->bv_len = 0; |
69 |
| - bv->bv_val = NULL; |
70 |
| - } |
71 |
| -} |
72 |
| - |
73 | 6 | /*
|
74 | 7 | * Copies out the data from a berval, and returns it as a new Python object,
|
75 | 8 | * Returns None if the berval pointer is NULL.
|
|
0 commit comments