Incorrect function porting in Python to C using the Python C API

I have a checksum function in Python:

def checksum(data):
    a = b = 0
    l = len(data)
    for i in range(l):
        a += ord(data[i])
        b += (l - i)*ord(data[i])

    return (b << 16) | a, a, b

that I'm trying to connect to module C for speed. Here's the C function:

static PyObject *
checksum(PyObject *self, PyObject *args)
{
    int i, length;
    unsigned long long a = 0, b = 0;
    unsigned long long checksum = 0;
    char *data;

    if (!PyArg_ParseTuple(args, "s#", &data, &length)) {
        return NULL;
    }

    for (i = 0; i < length; i++) {
        a += (int)data[i];
        b += (length - i) * (int)data[i];
    }

    checksum = (b << 16) | a;
    return Py_BuildValue("(Kii)", checksum, (int)a, (int)b);
}

I use it by opening a file and giving it a 4096 data block. They both return the same values ​​for small lines, but when I transfer binary data directly from the file, version C returns completely different values. Any help would be appreciated.

+3
source share
2 answers

, . , . , , . , . psyco? . , python C, .

+1

, "". ( , ). , 64 260 , b - 4096 . , , -.

C, b c Python Python. , :

  • C long long Python . n, a n * 255, b - len(data) * n * 255. 2**63-1 C long long.
  • long long unsigned long long RuntimeError , .

Python 64 a & 0xffffffffffffffff b & 0xffffffffffffffff.

, binascii.crc32.

+1

Source: https://habr.com/ru/post/1785627/


All Articles