1 | #include "Python.h" |
2 | #include "../_ssl.h" |
3 | |
4 | #include "openssl/bio.h" |
5 | |
6 | /* BIO_s_mem() to PyBytes |
7 | */ |
8 | static PyObject * |
9 | _PySSL_BytesFromBIO(_sslmodulestate *state, BIO *bio) |
10 | { |
11 | long size; |
12 | char *data = NULL; |
13 | size = BIO_get_mem_data(bio, &data); |
14 | if (data == NULL || size < 0) { |
15 | PyErr_SetString(PyExc_ValueError, "Not a memory BIO" ); |
16 | return NULL; |
17 | } |
18 | return PyBytes_FromStringAndSize(data, size); |
19 | } |
20 | |
21 | /* BIO_s_mem() to PyUnicode |
22 | */ |
23 | static PyObject * |
24 | _PySSL_UnicodeFromBIO(_sslmodulestate *state, BIO *bio, const char *error) |
25 | { |
26 | long size; |
27 | char *data = NULL; |
28 | size = BIO_get_mem_data(bio, &data); |
29 | if (data == NULL || size < 0) { |
30 | PyErr_SetString(PyExc_ValueError, "Not a memory BIO" ); |
31 | return NULL; |
32 | } |
33 | return PyUnicode_DecodeUTF8(data, size, error); |
34 | } |
35 | |