def c_uint8_ptr(data): if isinstance(data, _buffer_type): # This only works for cffi >= 1.7 return ffi.cast(uint8_t_type, ffi.from_buffer(data)) elif byte_string(data) or isinstance(data, _Array): return data else: raise TypeError("Object type %s cannot be passed to C code" % type(data))
def c_uint8_ptr(data): if byte_string(data) or isinstance(data, _Array): return data elif isinstance(data, _buffer_type): obj = _py_object(data) buf = _Py_buffer() _PyObject_GetBuffer(obj, byref(buf), _PyBUF_SIMPLE) buffer_type = c_ubyte * buf.len return buffer_type.from_address(buf.buf) else: raise TypeError("Object type %s cannot be passed to C code" % type(data))
def encode(self): """Return this DER SEQUENCE, fully encoded as a binary string. Raises: ValueError: if some elements in the sequence are neither integers nor byte strings. """ self.payload = b'' for item in self._seq: if byte_string(item): self.payload += item elif _is_number(item): self.payload += DerInteger(item).encode() else: self.payload += item.encode() return DerObject.encode(self)
def decode(self, der_encoded, strict=False): """Decode a complete DER element, and re-initializes this object with it. Args: der_encoded (byte string): A complete DER element. Raises: ValueError: in case of parsing errors. """ if not byte_string(der_encoded): raise ValueError("Input is not a byte string") s = BytesIO_EOF(der_encoded) self._decodeFromStream(s, strict) # There shouldn't be other bytes left if s.remaining_data() > 0: raise ValueError("Unexpected extra data after the DER structure") return self