_websocket.pyx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from cpython cimport PyBytes_AsString
  2. #from cpython cimport PyByteArray_AsString # cython still not exports that
  3. cdef extern from "Python.h":
  4. char* PyByteArray_AsString(bytearray ba) except NULL
  5. from libc.stdint cimport uint32_t, uint64_t, uintmax_t
  6. def _websocket_mask_cython(object mask, object data):
  7. """Note, this function mutates its `data` argument
  8. """
  9. cdef:
  10. Py_ssize_t data_len, i
  11. # bit operations on signed integers are implementation-specific
  12. unsigned char * in_buf
  13. const unsigned char * mask_buf
  14. uint32_t uint32_msk
  15. uint64_t uint64_msk
  16. assert len(mask) == 4
  17. if not isinstance(mask, bytes):
  18. mask = bytes(mask)
  19. if isinstance(data, bytearray):
  20. data = <bytearray>data
  21. else:
  22. data = bytearray(data)
  23. data_len = len(data)
  24. in_buf = <unsigned char*>PyByteArray_AsString(data)
  25. mask_buf = <const unsigned char*>PyBytes_AsString(mask)
  26. uint32_msk = (<uint32_t*>mask_buf)[0]
  27. # TODO: align in_data ptr to achieve even faster speeds
  28. # does it need in python ?! malloc() always aligns to sizeof(long) bytes
  29. if sizeof(size_t) >= 8:
  30. uint64_msk = uint32_msk
  31. uint64_msk = (uint64_msk << 32) | uint32_msk
  32. while data_len >= 8:
  33. (<uint64_t*>in_buf)[0] ^= uint64_msk
  34. in_buf += 8
  35. data_len -= 8
  36. while data_len >= 4:
  37. (<uint32_t*>in_buf)[0] ^= uint32_msk
  38. in_buf += 4
  39. data_len -= 4
  40. for i in range(0, data_len):
  41. in_buf[i] ^= mask_buf[i]