Generated by Cython 0.27

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

Raw output: cyxor.c

 01: # This file is part of Xpra.
 02: # Copyright (C) 2012-2017 Antoine Martin <antoine@devloop.org.uk>
 03: # Xpra is released under the terms of the GNU GPL v2, or, at your option, any
 04: # later version. See the file COPYING for details.
 05: 
 06: #cython: wraparound=False
 07: from __future__ import absolute_import
 08: 
 09: from libc.stdint cimport uint64_t
 10: from xpra.buffers.membuf cimport getbuf, object_as_buffer, MemBuf
 11: 
 12: 
+13: def xor_str(a, b):
/* Python wrapper */
static PyObject *__pyx_pw_4xpra_6codecs_3xor_5cyxor_1xor_str(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_4xpra_6codecs_3xor_5cyxor_1xor_str = {"xor_str", (PyCFunction)__pyx_pw_4xpra_6codecs_3xor_5cyxor_1xor_str, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_4xpra_6codecs_3xor_5cyxor_1xor_str(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_a = 0;
  PyObject *__pyx_v_b = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("xor_str (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_a,&__pyx_n_s_b,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        CYTHON_FALLTHROUGH;
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        CYTHON_FALLTHROUGH;
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_a)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        CYTHON_FALLTHROUGH;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_b)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("xor_str", 1, 2, 2, 1); __PYX_ERR(0, 13, __pyx_L3_error)
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "xor_str") < 0)) __PYX_ERR(0, 13, __pyx_L3_error)
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_a = values[0];
    __pyx_v_b = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("xor_str", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 13, __pyx_L3_error)
  __pyx_L3_error:;
  __Pyx_AddTraceback("xpra.codecs.xor.cyxor.xor_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4xpra_6codecs_3xor_5cyxor_xor_str(__pyx_self, __pyx_v_a, __pyx_v_b);

  /* function exit code */
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyObject *__pyx_pf_4xpra_6codecs_3xor_5cyxor_xor_str(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_b) {
  uint64_t *__pyx_v_abuf;
  uint64_t *__pyx_v_bbuf;
  Py_ssize_t __pyx_v_alen;
  Py_ssize_t __pyx_v_blen;
  struct __pyx_obj_4xpra_7buffers_6membuf_MemBuf *__pyx_v_out_buf = 0;
  uint64_t *__pyx_v_obuf;
  unsigned int __pyx_v_steps;
  unsigned int __pyx_v_i;
  unsigned int __pyx_v_j;
  unsigned int __pyx_v_char_steps;
  unsigned char *__pyx_v_acbuf;
  unsigned char *__pyx_v_bcbuf;
  unsigned char *__pyx_v_ocbuf;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("xor_str", 0);
/* … */
  /* function exit code */
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_6);
  __Pyx_AddTraceback("xpra.codecs.xor.cyxor.xor_str", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XDECREF((PyObject *)__pyx_v_out_buf);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
/* … */
  __pyx_tuple_ = PyTuple_Pack(15, __pyx_n_s_a, __pyx_n_s_b, __pyx_n_s_abuf, __pyx_n_s_bbuf, __pyx_n_s_alen, __pyx_n_s_blen, __pyx_n_s_out_buf, __pyx_n_s_obuf, __pyx_n_s_steps, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_char_steps, __pyx_n_s_acbuf, __pyx_n_s_bcbuf, __pyx_n_s_ocbuf); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 13, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_tuple_);
  __Pyx_GIVEREF(__pyx_tuple_);
/* … */
  __pyx_t_2 = PyCFunction_NewEx(&__pyx_mdef_4xpra_6codecs_3xor_5cyxor_1xor_str, NULL, __pyx_n_s_xpra_codecs_xor_cyxor); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 13, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_2);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_xor_str, __pyx_t_2) < 0) __PYX_ERR(0, 13, __pyx_L1_error)
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+14:     assert len(a)==len(b), "cyxor cannot xor strings of different lengths (%s:%s vs %s:%s)" % (type(a), len(a), type(b), len(b))
  #ifndef CYTHON_WITHOUT_ASSERTIONS
  if (unlikely(!Py_OptimizeFlag)) {
    __pyx_t_1 = PyObject_Length(__pyx_v_a); if (unlikely(__pyx_t_1 == -1)) __PYX_ERR(0, 14, __pyx_L1_error)
    __pyx_t_2 = PyObject_Length(__pyx_v_b); if (unlikely(__pyx_t_2 == -1)) __PYX_ERR(0, 14, __pyx_L1_error)
    if (unlikely(!((__pyx_t_1 == __pyx_t_2) != 0))) {
      __pyx_t_3 = PyObject_Length(__pyx_v_a); if (unlikely(__pyx_t_3 == -1)) __PYX_ERR(0, 14, __pyx_L1_error)
      __pyx_t_4 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 14, __pyx_L1_error)
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_3 = PyObject_Length(__pyx_v_b); if (unlikely(__pyx_t_3 == -1)) __PYX_ERR(0, 14, __pyx_L1_error)
      __pyx_t_5 = PyInt_FromSsize_t(__pyx_t_3); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 14, __pyx_L1_error)
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_6 = PyTuple_New(4); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 14, __pyx_L1_error)
      __Pyx_GOTREF(__pyx_t_6);
      __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_a)));
      __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_v_a)));
      PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)Py_TYPE(__pyx_v_a)));
      __Pyx_GIVEREF(__pyx_t_4);
      PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_t_4);
      __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_b)));
      __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_v_b)));
      PyTuple_SET_ITEM(__pyx_t_6, 2, ((PyObject *)Py_TYPE(__pyx_v_b)));
      __Pyx_GIVEREF(__pyx_t_5);
      PyTuple_SET_ITEM(__pyx_t_6, 3, __pyx_t_5);
      __pyx_t_4 = 0;
      __pyx_t_5 = 0;
      __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_cyxor_cannot_xor_strings_of_diff, __pyx_t_6); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 14, __pyx_L1_error)
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      PyErr_SetObject(PyExc_AssertionError, __pyx_t_5);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __PYX_ERR(0, 14, __pyx_L1_error)
    }
  }
  #endif
 15:     cdef uint64_t *abuf
 16:     cdef uint64_t *bbuf
+17:     cdef Py_ssize_t alen = 0, blen = 0
  __pyx_v_alen = 0;
  __pyx_v_blen = 0;
+18:     assert object_as_buffer(a, <const void**> &abuf, &alen)==0, "cannot get buffer pointer for %s" % type(a)
  #ifndef CYTHON_WITHOUT_ASSERTIONS
  if (unlikely(!Py_OptimizeFlag)) {
    if (unlikely(!((__pyx_f_4xpra_7buffers_6membuf_object_as_buffer(__pyx_v_a, ((void const **)(&__pyx_v_abuf)), (&__pyx_v_alen)) == 0) != 0))) {
      __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_cannot_get_buffer_pointer_for_s, ((PyObject *)Py_TYPE(__pyx_v_a))); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 18, __pyx_L1_error)
      __Pyx_GOTREF(__pyx_t_5);
      PyErr_SetObject(PyExc_AssertionError, __pyx_t_5);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __PYX_ERR(0, 18, __pyx_L1_error)
    }
  }
  #endif
+19:     assert object_as_buffer(b, <const void**> &bbuf, &blen)==0, "cannot get buffer pointer for %s" % type(b)
  #ifndef CYTHON_WITHOUT_ASSERTIONS
  if (unlikely(!Py_OptimizeFlag)) {
    if (unlikely(!((__pyx_f_4xpra_7buffers_6membuf_object_as_buffer(__pyx_v_b, ((void const **)(&__pyx_v_bbuf)), (&__pyx_v_blen)) == 0) != 0))) {
      __pyx_t_5 = __Pyx_PyString_Format(__pyx_kp_s_cannot_get_buffer_pointer_for_s, ((PyObject *)Py_TYPE(__pyx_v_b))); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 19, __pyx_L1_error)
      __Pyx_GOTREF(__pyx_t_5);
      PyErr_SetObject(PyExc_AssertionError, __pyx_t_5);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __PYX_ERR(0, 19, __pyx_L1_error)
    }
  }
  #endif
+20:     assert alen == blen, "python or cython bug? buffers don't have the same length?"
  #ifndef CYTHON_WITHOUT_ASSERTIONS
  if (unlikely(!Py_OptimizeFlag)) {
    if (unlikely(!((__pyx_v_alen == __pyx_v_blen) != 0))) {
      PyErr_SetObject(PyExc_AssertionError, __pyx_kp_s_python_or_cython_bug_buffers_don);
      __PYX_ERR(0, 20, __pyx_L1_error)
    }
  }
  #endif
+21:     cdef MemBuf out_buf = getbuf(alen)
  __pyx_t_5 = __pyx_f_4xpra_7buffers_6membuf_getbuf(__pyx_v_alen); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 21, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_5);
  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_4xpra_7buffers_6membuf_MemBuf))))) __PYX_ERR(0, 21, __pyx_L1_error)
  __pyx_v_out_buf = ((struct __pyx_obj_4xpra_7buffers_6membuf_MemBuf *)__pyx_t_5);
  __pyx_t_5 = 0;
+22:     cdef uint64_t *obuf = <uint64_t*> out_buf.get_mem()
  __pyx_v_obuf = ((uint64_t *)((struct __pyx_vtabstruct_4xpra_7buffers_6membuf_MemBuf *)__pyx_v_out_buf->__pyx_vtab)->get_mem(__pyx_v_out_buf));
 23:     #64 bits at a time (8 bytes):
+24:     cdef unsigned int steps = alen//8
  __pyx_v_steps = __Pyx_div_Py_ssize_t(__pyx_v_alen, 8);
 25:     cdef unsigned int i,j
+26:     for 0 <= i < steps:
  __pyx_t_7 = __pyx_v_steps;
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+27:         obuf[i] = abuf[i] ^ bbuf[i]
    (__pyx_v_obuf[__pyx_v_i]) = ((__pyx_v_abuf[__pyx_v_i]) ^ (__pyx_v_bbuf[__pyx_v_i]));
  }
 28:     #only used for the few remaining bytes at the end:
+29:     cdef unsigned int char_steps = alen % 8
  __pyx_v_char_steps = __Pyx_mod_Py_ssize_t(__pyx_v_alen, 8);
 30:     cdef unsigned char *acbuf
 31:     cdef unsigned char *bcbuf
 32:     cdef unsigned char *ocbuf
+33:     if char_steps>0:
  __pyx_t_8 = ((__pyx_v_char_steps > 0) != 0);
  if (__pyx_t_8) {
/* … */
  }
+34:         acbuf = <unsigned char *> abuf
    __pyx_v_acbuf = ((unsigned char *)__pyx_v_abuf);
+35:         bcbuf = <unsigned char *> bbuf
    __pyx_v_bcbuf = ((unsigned char *)__pyx_v_bbuf);
+36:         ocbuf = <unsigned char *> obuf
    __pyx_v_ocbuf = ((unsigned char *)__pyx_v_obuf);
+37:         for 0 <= i < char_steps:
    __pyx_t_7 = __pyx_v_char_steps;
    for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_7; __pyx_v_i++) {
+38:             j = steps*8 + i
      __pyx_v_j = ((__pyx_v_steps * 8) + __pyx_v_i);
+39:             ocbuf[j] = acbuf[j] ^ bcbuf[j]
      (__pyx_v_ocbuf[__pyx_v_j]) = ((__pyx_v_acbuf[__pyx_v_j]) ^ (__pyx_v_bcbuf[__pyx_v_j]));
    }
+40:     return memoryview(out_buf)
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 40, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_INCREF(((PyObject *)__pyx_v_out_buf));
  __Pyx_GIVEREF(((PyObject *)__pyx_v_out_buf));
  PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_out_buf));
  __pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_memoryview, __pyx_t_5, NULL); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 40, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_r = __pyx_t_6;
  __pyx_t_6 = 0;
  goto __pyx_L0;