| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "checksums.h" |
| 7 |
|
| 8 |
#include "aws/checksums/xxhash.h" |
| 9 |
|
| 10 |
const char *s_capsule_name_xxhash = "aws_xxhash"; |
| 11 |
|
| 12 |
static void s_xxhash_destructor(PyObject *xxhash_capsule) { |
| 13 |
struct aws_xxhash *hash = PyCapsule_GetPointer(xxhash_capsule, s_capsule_name_xxhash); |
| 14 |
assert(hash); |
| 15 |
|
| 16 |
aws_xxhash_destroy(hash); |
| 17 |
} |
| 18 |
|
| 19 |
PyObject *aws_py_xxhash64_new(PyObject *self, PyObject *args) { |
| 20 |
(void)self; |
| 21 |
PyObject *py_seed; |
| 22 |
|
| 23 |
if (!PyArg_ParseTuple(args, "O", &py_seed)) { |
| 24 |
return NULL; |
| 25 |
} |
| 26 |
|
| 27 |
uint64_t seed = PyLong_AsUnsignedLongLong(py_seed); |
| 28 |
|
| 29 |
if (seed == (uint64_t)-1 && PyErr_Occurred()) { |
| 30 |
return NULL; |
| 31 |
} |
| 32 |
|
| 33 |
PyObject *capsule = NULL; |
| 34 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 35 |
|
| 36 |
struct aws_xxhash *hash = aws_xxhash64_new(allocator, seed); |
| 37 |
|
| 38 |
if (hash == NULL) { |
| 39 |
return PyErr_AwsLastError(); |
| 40 |
} |
| 41 |
|
| 42 |
capsule = PyCapsule_New(hash, s_capsule_name_xxhash, s_xxhash_destructor); |
| 43 |
|
| 44 |
if (capsule == NULL) { |
| 45 |
aws_xxhash_destroy(hash); |
| 46 |
} |
| 47 |
|
| 48 |
return capsule; |
| 49 |
} |
| 50 |
|
| 51 |
PyObject *aws_py_xxhash3_64_new(PyObject *self, PyObject *args) { |
| 52 |
(void)self; |
| 53 |
PyObject *py_seed; |
| 54 |
|
| 55 |
if (!PyArg_ParseTuple(args, "O", &py_seed)) { |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
uint64_t seed = PyLong_AsUnsignedLongLong(py_seed); |
| 60 |
|
| 61 |
if (seed == (uint64_t)-1 && PyErr_Occurred()) { |
| 62 |
return NULL; |
| 63 |
} |
| 64 |
|
| 65 |
PyObject *capsule = NULL; |
| 66 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 67 |
|
| 68 |
struct aws_xxhash *hash = aws_xxhash3_64_new(allocator, seed); |
| 69 |
|
| 70 |
if (hash == NULL) { |
| 71 |
return PyErr_AwsLastError(); |
| 72 |
} |
| 73 |
|
| 74 |
capsule = PyCapsule_New(hash, s_capsule_name_xxhash, s_xxhash_destructor); |
| 75 |
|
| 76 |
if (capsule == NULL) { |
| 77 |
aws_xxhash_destroy(hash); |
| 78 |
} |
| 79 |
|
| 80 |
return capsule; |
| 81 |
} |
| 82 |
|
| 83 |
PyObject *aws_py_xxhash3_128_new(PyObject *self, PyObject *args) { |
| 84 |
(void)self; |
| 85 |
PyObject *py_seed; |
| 86 |
|
| 87 |
if (!PyArg_ParseTuple(args, "O", &py_seed)) { |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
uint64_t seed = PyLong_AsUnsignedLongLong(py_seed); |
| 92 |
|
| 93 |
if (seed == (uint64_t)-1 && PyErr_Occurred()) { |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
PyObject *capsule = NULL; |
| 98 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 99 |
|
| 100 |
struct aws_xxhash *hash = aws_xxhash3_128_new(allocator, seed); |
| 101 |
|
| 102 |
if (hash == NULL) { |
| 103 |
return PyErr_AwsLastError(); |
| 104 |
} |
| 105 |
|
| 106 |
capsule = PyCapsule_New(hash, s_capsule_name_xxhash, s_xxhash_destructor); |
| 107 |
|
| 108 |
if (capsule == NULL) { |
| 109 |
aws_xxhash_destroy(hash); |
| 110 |
} |
| 111 |
|
| 112 |
return capsule; |
| 113 |
} |
| 114 |
|
| 115 |
PyObject *aws_py_xxhash64_compute(PyObject *self, PyObject *args) { |
| 116 |
(void)self; |
| 117 |
struct aws_byte_cursor input; |
| 118 |
PyObject *py_seed; |
| 119 |
if (!PyArg_ParseTuple(args, "y#O", &input.ptr, &input.len, &py_seed)) { |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
uint64_t seed = PyLong_AsUnsignedLongLong(py_seed); |
| 124 |
|
| 125 |
if (seed == (uint64_t)-1 && PyErr_Occurred()) { |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
|
| 129 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 130 |
struct aws_byte_buf buf; |
| 131 |
aws_byte_buf_init(&buf, allocator, 8); |
| 132 |
|
| 133 |
if (aws_xxhash64_compute(seed, input, &buf)) { |
| 134 |
aws_byte_buf_clean_up_secure(&buf); |
| 135 |
return PyErr_AwsLastError(); |
| 136 |
} |
| 137 |
|
| 138 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)buf.buffer, buf.len); |
| 139 |
aws_byte_buf_clean_up_secure(&buf); |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
PyObject *aws_py_xxhash3_64_compute(PyObject *self, PyObject *args) { |
| 144 |
(void)self; |
| 145 |
struct aws_byte_cursor input; |
| 146 |
PyObject *py_seed; |
| 147 |
if (!PyArg_ParseTuple(args, "y#O", &input.ptr, &input.len, &py_seed)) { |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
|
| 151 |
uint64_t seed = PyLong_AsUnsignedLongLong(py_seed); |
| 152 |
|
| 153 |
if (seed == (uint64_t)-1 && PyErr_Occurred()) { |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
|
| 157 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 158 |
struct aws_byte_buf buf; |
| 159 |
aws_byte_buf_init(&buf, allocator, 8); |
| 160 |
|
| 161 |
if (aws_xxhash3_64_compute(seed, input, &buf)) { |
| 162 |
aws_byte_buf_clean_up_secure(&buf); |
| 163 |
return PyErr_AwsLastError(); |
| 164 |
} |
| 165 |
|
| 166 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)buf.buffer, buf.len); |
| 167 |
aws_byte_buf_clean_up_secure(&buf); |
| 168 |
return ret; |
| 169 |
} |
| 170 |
|
| 171 |
PyObject *aws_py_xxhash3_128_compute(PyObject *self, PyObject *args) { |
| 172 |
(void)self; |
| 173 |
struct aws_byte_cursor input; |
| 174 |
PyObject *py_seed; |
| 175 |
if (!PyArg_ParseTuple(args, "y#O", &input.ptr, &input.len, &py_seed)) { |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
|
| 179 |
uint64_t seed = PyLong_AsUnsignedLongLong(py_seed); |
| 180 |
|
| 181 |
if (seed == (uint64_t)-1 && PyErr_Occurred()) { |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 186 |
struct aws_byte_buf buf; |
| 187 |
aws_byte_buf_init(&buf, allocator, 16); |
| 188 |
|
| 189 |
if (aws_xxhash3_128_compute(seed, input, &buf)) { |
| 190 |
aws_byte_buf_clean_up_secure(&buf); |
| 191 |
return PyErr_AwsLastError(); |
| 192 |
} |
| 193 |
|
| 194 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)buf.buffer, buf.len); |
| 195 |
aws_byte_buf_clean_up_secure(&buf); |
| 196 |
return ret; |
| 197 |
} |
| 198 |
|
| 199 |
PyObject *aws_py_xxhash_update(PyObject *self, PyObject *args) { |
| 200 |
(void)self; |
| 201 |
struct aws_byte_cursor input; |
| 202 |
PyObject *xxhash_capsule = NULL; |
| 203 |
if (!PyArg_ParseTuple(args, "Oy#", &xxhash_capsule, &input.ptr, &input.len)) { |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
|
| 207 |
struct aws_xxhash *hash = PyCapsule_GetPointer(xxhash_capsule, s_capsule_name_xxhash); |
| 208 |
if (hash == NULL) { |
| 209 |
return NULL; |
| 210 |
} |
| 211 |
|
| 212 |
if (aws_xxhash_update(hash, input)) { |
| 213 |
return PyErr_AwsLastError(); |
| 214 |
} |
| 215 |
|
| 216 |
Py_RETURN_NONE; |
| 217 |
} |
| 218 |
|
| 219 |
PyObject *aws_py_xxhash_finalize(PyObject *self, PyObject *args) { |
| 220 |
(void)self; |
| 221 |
PyObject *xxhash_capsule = NULL; |
| 222 |
if (!PyArg_ParseTuple(args, "O", &xxhash_capsule)) { |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
|
| 226 |
struct aws_xxhash *hash = PyCapsule_GetPointer(xxhash_capsule, s_capsule_name_xxhash); |
| 227 |
if (hash == NULL) { |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
|
| 231 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 232 |
struct aws_byte_buf buf; |
| 233 |
aws_byte_buf_init(&buf, allocator, 16); |
| 234 |
|
| 235 |
if (aws_xxhash_finalize(hash, &buf)) { |
| 236 |
aws_byte_buf_clean_up_secure(&buf); |
| 237 |
return PyErr_AwsLastError(); |
| 238 |
} |
| 239 |
|
| 240 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)buf.buffer, buf.len); |
| 241 |
aws_byte_buf_clean_up_secure(&buf); |
| 242 |
return ret; |
| 243 |
} |