misc.c 816 B

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