code: def ctypes2buffer(cptr, length):
      
      if not isinstance(cptr, ctypes.POINTER(ctypes.c_char)):
          raise RuntimeError('expected char pointer')
      res = bytearray(length)
      rptr = (ctypes.c_char * length).from_buffer(res)
      if not ctypes.memmove(rptr, cptr, length):
          raise RuntimeError('memmove failed')
      return res
docs: Convert ctypes pointer to buffer type.