| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "checksums.h" |
| 7 |
|
| 8 |
#include "aws/checksums/crc.h" |
| 9 |
#include "aws/common/byte_buf.h" |
| 10 |
PyObject *checksums_crc32_common(PyObject *args, uint32_t (*checksum_fn)(const uint8_t *, size_t, uint32_t)) { |
| 11 |
Py_buffer input; |
| 12 |
PyObject *py_previousCrc; |
| 13 |
PyObject *py_result = NULL; |
| 14 |
|
| 15 |
if (!PyArg_ParseTuple(args, "s*O", &input, &py_previousCrc)) { |
| 16 |
return NULL; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
uint32_t previousCrc = PyLong_AsUnsignedLong(py_previousCrc); |
| 22 |
|
| 23 |
if (previousCrc == (uint32_t)-1 && PyErr_Occurred()) { |
| 24 |
goto done; |
| 25 |
} |
| 26 |
|
| 27 |
if (!PyBuffer_IsContiguous(&input, 'C')) { |
| 28 |
PyErr_SetString(PyExc_ValueError, "input must be contiguous buffer"); |
| 29 |
goto done; |
| 30 |
} |
| 31 |
|
| 32 |
uint32_t val = previousCrc; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
if (input.len > 1024 * 5) { |
| 37 |
unsigned char *buf = input.buf; |
| 38 |
Py_ssize_t len = input.len; |
| 39 |
|
| 40 |
|
| 41 |
Py_BEGIN_ALLOW_THREADS |
| 42 |
val = checksum_fn(buf, (size_t)len, val); |
| 43 |
Py_END_ALLOW_THREADS |
| 44 |
|
| 45 |
} else { |
| 46 |
val = checksum_fn(input.buf, (size_t)input.len, val); |
| 47 |
} |
| 48 |
py_result = PyLong_FromUnsignedLong(val); |
| 49 |
done: |
| 50 |
if (input.obj) { |
| 51 |
PyBuffer_Release(&input); |
| 52 |
} |
| 53 |
return py_result; |
| 54 |
} |
| 55 |
|
| 56 |
PyObject *aws_py_checksums_crc32(PyObject *self, PyObject *args) { |
| 57 |
(void)self; |
| 58 |
return checksums_crc32_common(args, aws_checksums_crc32_ex); |
| 59 |
} |
| 60 |
|
| 61 |
PyObject *aws_py_checksums_crc32c(PyObject *self, PyObject *args) { |
| 62 |
(void)self; |
| 63 |
return checksums_crc32_common(args, aws_checksums_crc32c_ex); |
| 64 |
} |
| 65 |
|
| 66 |
PyObject *aws_py_checksums_crc64nvme(PyObject *self, PyObject *args) { |
| 67 |
(void)self; |
| 68 |
Py_buffer input; |
| 69 |
PyObject *py_previousCrc64; |
| 70 |
PyObject *py_result = NULL; |
| 71 |
|
| 72 |
if (!PyArg_ParseTuple(args, "s*O", &input, &py_previousCrc64)) { |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
uint64_t previousCrc = PyLong_AsUnsignedLongLong(py_previousCrc64); |
| 79 |
|
| 80 |
if (previousCrc == (uint64_t)-1 && PyErr_Occurred()) { |
| 81 |
goto done; |
| 82 |
} |
| 83 |
|
| 84 |
if (!PyBuffer_IsContiguous(&input, 'C')) { |
| 85 |
PyErr_SetString(PyExc_ValueError, "input must be contiguous buffer"); |
| 86 |
goto done; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
if (input.len > 1024 * 5) { |
| 92 |
|
| 93 |
Py_BEGIN_ALLOW_THREADS |
| 94 |
previousCrc = aws_checksums_crc64nvme_ex(input.buf, (size_t)input.len, previousCrc); |
| 95 |
Py_END_ALLOW_THREADS |
| 96 |
|
| 97 |
} else { |
| 98 |
previousCrc = aws_checksums_crc64nvme_ex(input.buf, (size_t)input.len, previousCrc); |
| 99 |
} |
| 100 |
py_result = PyLong_FromUnsignedLongLong(previousCrc); |
| 101 |
done: |
| 102 |
if (input.obj) { |
| 103 |
PyBuffer_Release(&input); |
| 104 |
} |
| 105 |
return py_result; |
| 106 |
} |
| 107 |
|
| 108 |
PyObject *aws_py_checksums_crc32_combine(PyObject *self, PyObject *args) { |
| 109 |
(void)self; |
| 110 |
PyObject *py_crc1; |
| 111 |
PyObject *py_crc2; |
| 112 |
PyObject *py_len2; |
| 113 |
|
| 114 |
if (!PyArg_ParseTuple(args, "OOO", &py_crc1, &py_crc2, &py_len2)) { |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
|
| 118 |
uint32_t crc1 = PyLong_AsUnsignedLong(py_crc1); |
| 119 |
if (crc1 == (uint32_t)-1 && PyErr_Occurred()) { |
| 120 |
PyErr_Clear(); |
| 121 |
PyErr_SetString(PyExc_ValueError, "crc32_result1 is not a valid unsigned 32-bit integer"); |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
uint32_t crc2 = PyLong_AsUnsignedLong(py_crc2); |
| 126 |
if (crc2 == (uint32_t)-1 && PyErr_Occurred()) { |
| 127 |
PyErr_Clear(); |
| 128 |
PyErr_SetString(PyExc_ValueError, "crc32_result2 is not a valid unsigned 32-bit integer"); |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
uint64_t len2 = PyLong_AsUnsignedLongLong(py_len2); |
| 133 |
if (len2 == (uint64_t)-1 && PyErr_Occurred()) { |
| 134 |
PyErr_Clear(); |
| 135 |
PyErr_SetString(PyExc_ValueError, "data_length2 is not a valid unsigned 64-bit integer"); |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
uint32_t result = aws_checksums_crc32_combine(crc1, crc2, len2); |
| 140 |
return PyLong_FromUnsignedLong(result); |
| 141 |
} |
| 142 |
|
| 143 |
PyObject *aws_py_checksums_crc32c_combine(PyObject *self, PyObject *args) { |
| 144 |
(void)self; |
| 145 |
PyObject *py_crc1; |
| 146 |
PyObject *py_crc2; |
| 147 |
PyObject *py_len2; |
| 148 |
|
| 149 |
if (!PyArg_ParseTuple(args, "OOO", &py_crc1, &py_crc2, &py_len2)) { |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
uint32_t crc1 = PyLong_AsUnsignedLong(py_crc1); |
| 154 |
if (crc1 == (uint32_t)-1 && PyErr_Occurred()) { |
| 155 |
PyErr_Clear(); |
| 156 |
PyErr_SetString(PyExc_ValueError, "crc32c_result1 is not a valid unsigned 32-bit integer"); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
uint32_t crc2 = PyLong_AsUnsignedLong(py_crc2); |
| 161 |
if (crc2 == (uint32_t)-1 && PyErr_Occurred()) { |
| 162 |
PyErr_Clear(); |
| 163 |
PyErr_SetString(PyExc_ValueError, "crc32c_result2 is not a valid unsigned 32-bit integer"); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
uint64_t len2 = PyLong_AsUnsignedLongLong(py_len2); |
| 168 |
if (len2 == (uint64_t)-1 && PyErr_Occurred()) { |
| 169 |
PyErr_Clear(); |
| 170 |
PyErr_SetString(PyExc_ValueError, "data_length2 is not a valid unsigned 64-bit integer"); |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
uint32_t result = aws_checksums_crc32c_combine(crc1, crc2, len2); |
| 175 |
return PyLong_FromUnsignedLong(result); |
| 176 |
} |
| 177 |
|
| 178 |
PyObject *aws_py_checksums_crc64nvme_combine(PyObject *self, PyObject *args) { |
| 179 |
(void)self; |
| 180 |
PyObject *py_crc1; |
| 181 |
PyObject *py_crc2; |
| 182 |
PyObject *py_len2; |
| 183 |
|
| 184 |
if (!PyArg_ParseTuple(args, "OOO", &py_crc1, &py_crc2, &py_len2)) { |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
|
| 188 |
uint64_t crc1 = PyLong_AsUnsignedLongLong(py_crc1); |
| 189 |
if (crc1 == (uint64_t)-1 && PyErr_Occurred()) { |
| 190 |
PyErr_Clear(); |
| 191 |
PyErr_SetString(PyExc_ValueError, "crc64nvme_result1 is not a valid unsigned 64-bit integer"); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
uint64_t crc2 = PyLong_AsUnsignedLongLong(py_crc2); |
| 196 |
if (crc2 == (uint64_t)-1 && PyErr_Occurred()) { |
| 197 |
PyErr_Clear(); |
| 198 |
PyErr_SetString(PyExc_ValueError, "crc64nvme_result2 is not a valid unsigned 64-bit integer"); |
| 199 |
return NULL; |
| 200 |
} |
| 201 |
|
| 202 |
uint64_t len2 = PyLong_AsUnsignedLongLong(py_len2); |
| 203 |
if (len2 == (uint64_t)-1 && PyErr_Occurred()) { |
| 204 |
PyErr_Clear(); |
| 205 |
PyErr_SetString(PyExc_ValueError, "data_length2 is not a valid unsigned 64-bit integer"); |
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
uint64_t result = aws_checksums_crc64nvme_combine(crc1, crc2, len2); |
| 210 |
return PyLong_FromUnsignedLongLong(result); |
| 211 |
} |