| 1 |
import sys |
| 2 |
from _pypy_openssl import ffi |
| 3 |
from _pypy_openssl import lib |
| 4 |
|
| 5 |
def _string_from_asn1(asn1): |
| 6 |
data = lib.ASN1_STRING_get0_data(asn1) |
| 7 |
length = lib.ASN1_STRING_length(asn1) |
| 8 |
return _str_with_len(ffi.cast("const char*",data), length) |
| 9 |
|
| 10 |
def _str_with_len(char_ptr, length): |
| 11 |
return ffi.buffer(char_ptr, length)[:].decode('utf-8').strip('\n') |
| 12 |
|
| 13 |
def _bytes_with_len(char_ptr, length): |
| 14 |
return ffi.buffer(char_ptr, length)[:] |
| 15 |
|
| 16 |
def _str_to_ffi_buffer(view): |
| 17 |
if isinstance(view, str): |
| 18 |
return ffi.from_buffer(view.encode()) |
| 19 |
elif isinstance(view, memoryview): |
| 20 |
|
| 21 |
|
| 22 |
view = view.tobytes() |
| 23 |
|
| 24 |
|
| 25 |
return ffi.from_buffer(view) |
| 26 |
|
| 27 |
def _str_from_buf(buf): |
| 28 |
return ffi.string(buf).decode('utf-8') |
| 29 |
|
| 30 |
def _cstr_decode_fs(buf): |
| 31 |
if buf == ffi.NULL: |
| 32 |
return None |
| 33 |
return ffi.string(buf).decode(sys.getfilesystemencoding()) |
| 34 |
|