File: | _counter.c |
Warning: | line 154, column 13 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | ||||
2 | * _counter.c: Fast counter for use with CTR-mode ciphers | ||||
3 | * | ||||
4 | * Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net> | ||||
5 | * | ||||
6 | * =================================================================== | ||||
7 | * The contents of this file are dedicated to the public domain. To | ||||
8 | * the extent that dedication to the public domain is not available, | ||||
9 | * everyone is granted a worldwide, perpetual, royalty-free, | ||||
10 | * non-exclusive license to exercise all rights associated with the | ||||
11 | * contents of this file for any purpose whatsoever. | ||||
12 | * No rights are reserved. | ||||
13 | * | ||||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||
18 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||
19 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||
20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
21 | * SOFTWARE. | ||||
22 | * =================================================================== | ||||
23 | */ | ||||
24 | |||||
25 | #include "pycrypto_common.h" | ||||
26 | #include <assert.h> | ||||
27 | #include <stddef.h> | ||||
28 | #include <string.h> | ||||
29 | #include "_counter.h" | ||||
30 | |||||
31 | /* NB: This can be called multiple times for a given object, via the __init__ method. Be careful. */ | ||||
32 | static int | ||||
33 | CounterObject_init(PCT_CounterObject *self, PyObject *args, PyObject *kwargs) | ||||
34 | { | ||||
35 | PyBytesObject *prefix=NULL((void*)0), *suffix=NULL((void*)0), *initval=NULL((void*)0); | ||||
36 | int allow_wraparound = 0; | ||||
37 | Py_ssize_t size; | ||||
38 | |||||
39 | static char *kwlist[] = {"prefix", "suffix", "initval", "allow_wraparound", NULL((void*)0)}; | ||||
40 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "SSS|i", kwlist, &prefix, &suffix, &initval, &allow_wraparound)) | ||||
41 | return -1; | ||||
42 | |||||
43 | /* Check string size and set nbytes */ | ||||
44 | size = PyBytes_GET_SIZE(initval)((((PyVarObject*)(initval))->ob_size)); | ||||
45 | if (size < 1) { | ||||
46 | PyErr_SetString(PyExc_ValueError, "initval length too small (must be >= 1 byte)"); | ||||
47 | return -1; | ||||
48 | } else if (size > 0xffff) { | ||||
49 | PyErr_SetString(PyExc_ValueError, "initval length too large (must be <= 65535 bytes)"); | ||||
50 | return -1; | ||||
51 | } | ||||
52 | self->nbytes = (uint16_t) size; | ||||
53 | |||||
54 | /* Check prefix length */ | ||||
55 | size = PyBytes_GET_SIZE(prefix)((((PyVarObject*)(prefix))->ob_size)); | ||||
56 | assert(size >= 0)((void) sizeof ((size >= 0) ? 1 : 0), __extension__ ({ if ( size >= 0) ; else __assert_fail ("size >= 0", "src/_counter.c" , 56, __extension__ __PRETTY_FUNCTION__); })); | ||||
57 | if (size > 0xffff) { | ||||
58 | PyErr_SetString(PyExc_ValueError, "prefix length too large (must be <= 65535 bytes)"); | ||||
59 | return -1; | ||||
60 | } | ||||
61 | |||||
62 | /* Check suffix length */ | ||||
63 | size = PyBytes_GET_SIZE(suffix)((((PyVarObject*)(suffix))->ob_size)); | ||||
64 | assert(size >= 0)((void) sizeof ((size >= 0) ? 1 : 0), __extension__ ({ if ( size >= 0) ; else __assert_fail ("size >= 0", "src/_counter.c" , 64, __extension__ __PRETTY_FUNCTION__); })); | ||||
65 | if (size > 0xffff) { | ||||
66 | PyErr_SetString(PyExc_ValueError, "suffix length too large (must be <= 65535 bytes)"); | ||||
67 | return -1; | ||||
68 | } | ||||
69 | |||||
70 | /* Set prefix, being careful to properly discard any old reference */ | ||||
71 | Py_CLEAR(self->prefix)do { PyObject *_py_tmp = ((PyObject*)(self->prefix)); if ( _py_tmp != ((void*)0)) { (self->prefix) = ((void*)0); _Py_DECREF (((PyObject*)(_py_tmp))); } } while (0); | ||||
72 | Py_INCREF(prefix)_Py_INCREF(((PyObject*)(prefix))); | ||||
73 | self->prefix = prefix; | ||||
74 | |||||
75 | /* Set prefix, being careful to properly discard any old reference */ | ||||
76 | Py_CLEAR(self->suffix)do { PyObject *_py_tmp = ((PyObject*)(self->suffix)); if ( _py_tmp != ((void*)0)) { (self->suffix) = ((void*)0); _Py_DECREF (((PyObject*)(_py_tmp))); } } while (0); | ||||
77 | Py_INCREF(suffix)_Py_INCREF(((PyObject*)(suffix))); | ||||
78 | self->suffix = suffix; | ||||
79 | |||||
80 | /* Free old buffer (if any) */ | ||||
81 | if (self->val) { | ||||
82 | PyMem_Free(self->val); | ||||
83 | self->val = self->p = NULL((void*)0); | ||||
84 | self->buf_size = 0; | ||||
85 | } | ||||
86 | |||||
87 | /* Allocate new buffer */ | ||||
88 | /* buf_size won't overflow because the length of each string will always be <= 0xffff */ | ||||
89 | self->buf_size = PyBytes_GET_SIZE(prefix)((((PyVarObject*)(prefix))->ob_size)) + PyBytes_GET_SIZE(suffix)((((PyVarObject*)(suffix))->ob_size)) + self->nbytes; | ||||
90 | self->val = self->p = PyMem_Malloc(self->buf_size); | ||||
91 | if (self->val == NULL((void*)0)) { | ||||
92 | self->buf_size = 0; | ||||
93 | return -1; | ||||
94 | } | ||||
95 | self->p = self->val + PyBytes_GET_SIZE(prefix)((((PyVarObject*)(prefix))->ob_size)); | ||||
96 | |||||
97 | /* Sanity-check pointers */ | ||||
98 | assert(self->val <= self->p)((void) sizeof ((self->val <= self->p) ? 1 : 0), __extension__ ({ if (self->val <= self->p) ; else __assert_fail ( "self->val <= self->p", "src/_counter.c", 98, __extension__ __PRETTY_FUNCTION__); })); | ||||
99 | assert(self->buf_size >= 0)((void) sizeof ((self->buf_size >= 0) ? 1 : 0), __extension__ ({ if (self->buf_size >= 0) ; else __assert_fail ("self->buf_size >= 0" , "src/_counter.c", 99, __extension__ __PRETTY_FUNCTION__); } )); | ||||
100 | assert(self->p + self->nbytes <= self->val + self->buf_size)((void) sizeof ((self->p + self->nbytes <= self-> val + self->buf_size) ? 1 : 0), __extension__ ({ if (self-> p + self->nbytes <= self->val + self->buf_size) ; else __assert_fail ("self->p + self->nbytes <= self->val + self->buf_size" , "src/_counter.c", 100, __extension__ __PRETTY_FUNCTION__); } )); | ||||
101 | assert(self->val + PyBytes_GET_SIZE(self->prefix) == self->p)((void) sizeof ((self->val + ((((PyVarObject*)(self->prefix ))->ob_size)) == self->p) ? 1 : 0), __extension__ ({ if (self->val + ((((PyVarObject*)(self->prefix))->ob_size )) == self->p) ; else __assert_fail ("self->val + PyBytes_GET_SIZE(self->prefix) == self->p" , "src/_counter.c", 101, __extension__ __PRETTY_FUNCTION__); } )); | ||||
102 | assert(PyBytes_GET_SIZE(self->prefix) + self->nbytes + PyBytes_GET_SIZE(self->suffix) == self->buf_size)((void) sizeof ((((((PyVarObject*)(self->prefix))->ob_size )) + self->nbytes + ((((PyVarObject*)(self->suffix))-> ob_size)) == self->buf_size) ? 1 : 0), __extension__ ({ if (((((PyVarObject*)(self->prefix))->ob_size)) + self-> nbytes + ((((PyVarObject*)(self->suffix))->ob_size)) == self->buf_size) ; else __assert_fail ("PyBytes_GET_SIZE(self->prefix) + self->nbytes + PyBytes_GET_SIZE(self->suffix) == self->buf_size" , "src/_counter.c", 102, __extension__ __PRETTY_FUNCTION__); } )); | ||||
103 | |||||
104 | /* Copy the prefix, suffix, and initial value into the buffer. */ | ||||
105 | memcpy(self->val, PyBytes_AS_STRING(prefix)((((PyBytesObject *)(prefix))->ob_sval)), PyBytes_GET_SIZE(prefix)((((PyVarObject*)(prefix))->ob_size))); | ||||
106 | memcpy(self->p, PyBytes_AS_STRING(initval)((((PyBytesObject *)(initval))->ob_sval)), self->nbytes); | ||||
107 | memcpy(self->p + self->nbytes, PyBytes_AS_STRING(suffix)((((PyBytesObject *)(suffix))->ob_sval)), PyBytes_GET_SIZE(suffix)((((PyVarObject*)(suffix))->ob_size))); | ||||
108 | |||||
109 | /* Set allow_wraparound */ | ||||
110 | self->allow_wraparound = allow_wraparound; | ||||
111 | |||||
112 | /* Clear the carry flag */ | ||||
113 | self->carry = 0; | ||||
114 | |||||
115 | return 0; | ||||
116 | } | ||||
117 | |||||
118 | static void | ||||
119 | CounterObject_dealloc(PCT_CounterObject *self) | ||||
120 | { | ||||
121 | /* Free the buffer */ | ||||
122 | if (self->val) { | ||||
123 | memset(self->val, 0, self->buf_size); /* wipe the buffer before freeing it */ | ||||
124 | PyMem_Free(self->val); | ||||
125 | self->val = self->p = NULL((void*)0); | ||||
126 | self->buf_size = 0; | ||||
127 | } | ||||
128 | |||||
129 | /* Deallocate the prefix and suffix, if they are present. */ | ||||
130 | Py_CLEAR(self->prefix)do { PyObject *_py_tmp = ((PyObject*)(self->prefix)); if ( _py_tmp != ((void*)0)) { (self->prefix) = ((void*)0); _Py_DECREF (((PyObject*)(_py_tmp))); } } while (0); | ||||
131 | Py_CLEAR(self->suffix)do { PyObject *_py_tmp = ((PyObject*)(self->suffix)); if ( _py_tmp != ((void*)0)) { (self->suffix) = ((void*)0); _Py_DECREF (((PyObject*)(_py_tmp))); } } while (0); | ||||
132 | |||||
133 | /* Free this object */ | ||||
134 | PyObject_DelPyObject_Free(self); | ||||
135 | } | ||||
136 | |||||
137 | static inline PyObject * | ||||
138 | _CounterObject_next_value(PCT_CounterObject *self, int little_endian) | ||||
139 | { | ||||
140 | unsigned int i; | ||||
141 | int increment; | ||||
142 | uint8_t *p; | ||||
143 | PyObject *eight = NULL((void*)0); | ||||
144 | PyObject *ch = NULL((void*)0); | ||||
145 | PyObject *y = NULL((void*)0); | ||||
146 | PyObject *x = NULL((void*)0); | ||||
147 | |||||
148 | if (self->carry && !self->allow_wraparound) { | ||||
149 | PyErr_SetString(PyExc_OverflowError, | ||||
150 | "counter wrapped without allow_wraparound"); | ||||
151 | goto err_out; | ||||
152 | } | ||||
153 | |||||
154 | eight = PyInt_FromLongPyLong_FromLong(8); | ||||
| |||||
155 | if (!eight) | ||||
156 | goto err_out; | ||||
157 | |||||
158 | /* Make a new Python long integer */ | ||||
159 | x = PyLong_FromUnsignedLong(0); | ||||
160 | if (!x) | ||||
161 | goto err_out; | ||||
162 | |||||
163 | if (little_endian
| ||||
164 | /* little endian */ | ||||
165 | p = self->p + self->nbytes - 1; | ||||
166 | increment = -1; | ||||
167 | } else { | ||||
168 | /* big endian */ | ||||
169 | p = self->p; | ||||
170 | increment = 1; | ||||
171 | } | ||||
172 | for (i = 0; i < self->nbytes; i++, p += increment) { | ||||
173 | /* Sanity check pointer */ | ||||
174 | assert(self->p <= p)((void) sizeof ((self->p <= p) ? 1 : 0), __extension__ ( { if (self->p <= p) ; else __assert_fail ("self->p <= p" , "src/_counter.c", 174, __extension__ __PRETTY_FUNCTION__); } )); | ||||
175 | assert(p < self->p + self->nbytes)((void) sizeof ((p < self->p + self->nbytes) ? 1 : 0 ), __extension__ ({ if (p < self->p + self->nbytes) ; else __assert_fail ("p < self->p + self->nbytes", "src/_counter.c" , 175, __extension__ __PRETTY_FUNCTION__); })); | ||||
176 | |||||
177 | /* ch = ord(p) */ | ||||
178 | Py_CLEAR(ch)do { PyObject *_py_tmp = ((PyObject*)(ch)); if (_py_tmp != (( void*)0)) { (ch) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); /* delete old ch */ | ||||
179 | ch = PyInt_FromLongPyLong_FromLong((long) *p); | ||||
180 | if (!ch) | ||||
181 | goto err_out; | ||||
182 | |||||
183 | /* y = x << 8 */ | ||||
184 | Py_CLEAR(y)do { PyObject *_py_tmp = ((PyObject*)(y)); if (_py_tmp != ((void *)0)) { (y) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp))); } } while (0); /* delete old y */ | ||||
185 | y = PyNumber_Lshift(x, eight); | ||||
186 | if (!y) | ||||
187 | goto err_out; | ||||
188 | |||||
189 | /* x = y | ch */ | ||||
190 | Py_CLEAR(x)do { PyObject *_py_tmp = ((PyObject*)(x)); if (_py_tmp != ((void *)0)) { (x) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp))); } } while (0); /* delete old x */ | ||||
191 | x = PyNumber_Or(y, ch); | ||||
192 | } | ||||
193 | |||||
194 | Py_CLEAR(eight)do { PyObject *_py_tmp = ((PyObject*)(eight)); if (_py_tmp != ((void*)0)) { (eight) = ((void*)0); _Py_DECREF(((PyObject*)( _py_tmp))); } } while (0); | ||||
195 | Py_CLEAR(ch)do { PyObject *_py_tmp = ((PyObject*)(ch)); if (_py_tmp != (( void*)0)) { (ch) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | ||||
196 | Py_CLEAR(y)do { PyObject *_py_tmp = ((PyObject*)(y)); if (_py_tmp != ((void *)0)) { (y) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp))); } } while (0); | ||||
197 | return x; | ||||
198 | |||||
199 | err_out: | ||||
200 | Py_CLEAR(eight)do { PyObject *_py_tmp = ((PyObject*)(eight)); if (_py_tmp != ((void*)0)) { (eight) = ((void*)0); _Py_DECREF(((PyObject*)( _py_tmp))); } } while (0); | ||||
201 | Py_CLEAR(ch)do { PyObject *_py_tmp = ((PyObject*)(ch)); if (_py_tmp != (( void*)0)) { (ch) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | ||||
202 | Py_CLEAR(y)do { PyObject *_py_tmp = ((PyObject*)(y)); if (_py_tmp != ((void *)0)) { (y) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp))); } } while (0); | ||||
203 | Py_CLEAR(x)do { PyObject *_py_tmp = ((PyObject*)(x)); if (_py_tmp != ((void *)0)) { (x) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp))); } } while (0); | ||||
204 | return NULL((void*)0); | ||||
205 | } | ||||
206 | |||||
207 | static PyObject * | ||||
208 | CounterLEObject_next_value(PCT_CounterObject *self, PyObject *args) | ||||
209 | { | ||||
210 | return _CounterObject_next_value(self, 1); | ||||
211 | } | ||||
212 | |||||
213 | static PyObject * | ||||
214 | CounterBEObject_next_value(PCT_CounterObject *self, PyObject *args) | ||||
215 | { | ||||
216 | return _CounterObject_next_value(self, 0); | ||||
| |||||
217 | } | ||||
218 | |||||
219 | static void | ||||
220 | CounterLEObject_increment(PCT_CounterObject *self) | ||||
221 | { | ||||
222 | unsigned int i, tmp, carry; | ||||
223 | uint8_t *p; | ||||
224 | |||||
225 | assert(sizeof(i) >= sizeof(self->nbytes))((void) sizeof ((sizeof(i) >= sizeof(self->nbytes)) ? 1 : 0), __extension__ ({ if (sizeof(i) >= sizeof(self->nbytes )) ; else __assert_fail ("sizeof(i) >= sizeof(self->nbytes)" , "src/_counter.c", 225, __extension__ __PRETTY_FUNCTION__); } )); | ||||
226 | |||||
227 | carry = 1; | ||||
228 | p = self->p; | ||||
229 | for (i = 0; i < self->nbytes; i++, p++) { | ||||
230 | /* Sanity check pointer */ | ||||
231 | assert(self->p <= p)((void) sizeof ((self->p <= p) ? 1 : 0), __extension__ ( { if (self->p <= p) ; else __assert_fail ("self->p <= p" , "src/_counter.c", 231, __extension__ __PRETTY_FUNCTION__); } )); | ||||
232 | assert(p < self->p + self->nbytes)((void) sizeof ((p < self->p + self->nbytes) ? 1 : 0 ), __extension__ ({ if (p < self->p + self->nbytes) ; else __assert_fail ("p < self->p + self->nbytes", "src/_counter.c" , 232, __extension__ __PRETTY_FUNCTION__); })); | ||||
233 | |||||
234 | tmp = *p + carry; | ||||
235 | carry = tmp >> 8; /* This will only ever be 0 or 1 */ | ||||
236 | *p = tmp & 0xff; | ||||
237 | } | ||||
238 | self->carry = carry; | ||||
239 | } | ||||
240 | |||||
241 | static void | ||||
242 | CounterBEObject_increment(PCT_CounterObject *self) | ||||
243 | { | ||||
244 | unsigned int i, tmp, carry; | ||||
245 | uint8_t *p; | ||||
246 | |||||
247 | assert(sizeof(i) >= sizeof(self->nbytes))((void) sizeof ((sizeof(i) >= sizeof(self->nbytes)) ? 1 : 0), __extension__ ({ if (sizeof(i) >= sizeof(self->nbytes )) ; else __assert_fail ("sizeof(i) >= sizeof(self->nbytes)" , "src/_counter.c", 247, __extension__ __PRETTY_FUNCTION__); } )); | ||||
248 | |||||
249 | carry = 1; | ||||
250 | p = self->p + self->nbytes-1; | ||||
251 | for (i = 0; i < self->nbytes; i++, p--) { | ||||
252 | /* Sanity check pointer */ | ||||
253 | assert(self->p <= p)((void) sizeof ((self->p <= p) ? 1 : 0), __extension__ ( { if (self->p <= p) ; else __assert_fail ("self->p <= p" , "src/_counter.c", 253, __extension__ __PRETTY_FUNCTION__); } )); | ||||
254 | assert(p < self->p + self->nbytes)((void) sizeof ((p < self->p + self->nbytes) ? 1 : 0 ), __extension__ ({ if (p < self->p + self->nbytes) ; else __assert_fail ("p < self->p + self->nbytes", "src/_counter.c" , 254, __extension__ __PRETTY_FUNCTION__); })); | ||||
255 | |||||
256 | tmp = *p + carry; | ||||
257 | carry = tmp >> 8; /* This will only ever be 0 or 1 */ | ||||
258 | *p = tmp & 0xff; | ||||
259 | } | ||||
260 | self->carry = carry; | ||||
261 | } | ||||
262 | |||||
263 | static PyObject * | ||||
264 | CounterObject_call(PCT_CounterObject *self, PyObject *args, PyObject *kwargs) | ||||
265 | { | ||||
266 | PyObject *retval; | ||||
267 | |||||
268 | if (self->carry && !self->allow_wraparound) { | ||||
269 | PyErr_SetString(PyExc_OverflowError, | ||||
270 | "counter wrapped without allow_wraparound"); | ||||
271 | return NULL((void*)0); | ||||
272 | } | ||||
273 | |||||
274 | retval = (PyObject *)PyBytes_FromStringAndSize((const char *)self->val, self->buf_size); | ||||
275 | |||||
276 | self->inc_func(self); | ||||
277 | |||||
278 | return retval; | ||||
279 | } | ||||
280 | |||||
281 | static PyMethodDef CounterLEObject_methods[] = { | ||||
282 | {"next_value", (PyCFunction)CounterLEObject_next_value, METH_VARARGS0x0001, | ||||
283 | "Get the numerical value of next value of the counter."}, | ||||
284 | |||||
285 | {NULL((void*)0)} /* sentinel */ | ||||
286 | }; | ||||
287 | |||||
288 | static PyMethodDef CounterBEObject_methods[] = { | ||||
289 | {"next_value", (PyCFunction)CounterBEObject_next_value, METH_VARARGS0x0001, | ||||
290 | "Get the numerical value of next value of the counter."}, | ||||
291 | |||||
292 | {NULL((void*)0)} /* sentinel */ | ||||
293 | }; | ||||
294 | |||||
295 | /* Python 2.1 doesn't allow us to assign methods or attributes to an object, | ||||
296 | * so we hack it here. */ | ||||
297 | |||||
298 | static PyObject * | ||||
299 | CounterLEObject_getattro(PyObject *s, PyObject *attr) | ||||
300 | { | ||||
301 | PCT_CounterObject *self = (PCT_CounterObject *)s; | ||||
302 | if (!PyString_Check(attr)((((((PyObject*)(attr))->ob_type))->tp_flags & ((1UL << 28))) != 0)) | ||||
303 | goto generic; | ||||
304 | |||||
305 | if (PyString_CompareWithASCIIStringPyUnicode_CompareWithASCIIString(attr, "carry") == 0) { | ||||
306 | return PyInt_FromLongPyLong_FromLong((long)self->carry); | ||||
307 | } | ||||
308 | generic: | ||||
309 | #if PYTHON_API_VERSION1013 >= 1011 /* Python 2.2 and later */ | ||||
310 | return PyObject_GenericGetAttr(s, attr); | ||||
311 | #else | ||||
312 | if (PyString_Check(attr)((((((PyObject*)(attr))->ob_type))->tp_flags & ((1UL << 28))) != 0) < 0) { | ||||
313 | PyErr_SetObject(PyExc_AttributeError, attr); | ||||
314 | return NULL((void*)0); | ||||
315 | } | ||||
316 | return Py_FindMethod(CounterLEObject_methods, (PyObject *)self, PyString_AsString(attr)); | ||||
317 | #endif | ||||
318 | } | ||||
319 | |||||
320 | static PyObject * | ||||
321 | CounterBEObject_getattro(PyObject *s, PyObject *attr) | ||||
322 | { | ||||
323 | PCT_CounterObject *self = (PCT_CounterObject *)s; | ||||
324 | if (!PyString_Check(attr)((((((PyObject*)(attr))->ob_type))->tp_flags & ((1UL << 28))) != 0)) | ||||
325 | goto generic; | ||||
326 | |||||
327 | if (PyString_CompareWithASCIIStringPyUnicode_CompareWithASCIIString(attr, "carry") == 0) { | ||||
328 | return PyInt_FromLongPyLong_FromLong((long)self->carry); | ||||
329 | } | ||||
330 | generic: | ||||
331 | #if PYTHON_API_VERSION1013 >= 1011 /* Python 2.2 and later */ | ||||
332 | return PyObject_GenericGetAttr(s, attr); | ||||
333 | #else | ||||
334 | if (PyString_Check(attr)((((((PyObject*)(attr))->ob_type))->tp_flags & ((1UL << 28))) != 0) < 0) { | ||||
335 | PyErr_SetObject(PyExc_AttributeError, attr); | ||||
336 | return NULL((void*)0); | ||||
337 | } | ||||
338 | return Py_FindMethod(CounterBEObject_methods, (PyObject *)self, PyString_AsString(attr)); | ||||
339 | #endif | ||||
340 | } | ||||
341 | |||||
342 | static PyTypeObject | ||||
343 | PCT_CounterLEType = { | ||||
344 | PyVarObject_HEAD_INIT(NULL, 0){ { 1, ((void*)0) }, 0 }, /* deferred type init for compilation on Windows, type will be filled in at runtime */ | ||||
345 | "_counter.CounterLE", /* tp_name */ | ||||
346 | sizeof(PCT_CounterObject), /* tp_basicsize */ | ||||
347 | 0, /* tp_itemsize */ | ||||
348 | /* methods */ | ||||
349 | (destructor)CounterObject_dealloc, /* tp_dealloc */ | ||||
350 | 0, /* tp_print */ | ||||
351 | 0, /* tp_getattr */ | ||||
352 | 0, /* tp_setattr */ | ||||
353 | 0, /* tp_compare */ | ||||
354 | 0, /* tp_repr */ | ||||
355 | 0, /* tp_as_number */ | ||||
356 | 0, /* tp_as_sequence */ | ||||
357 | 0, /* tp_as_mapping */ | ||||
358 | 0, /* tp_hash */ | ||||
359 | (ternaryfunc)CounterObject_call, /* tp_call */ | ||||
360 | 0, /* tp_str */ | ||||
361 | CounterLEObject_getattro, /* tp_getattro */ | ||||
362 | 0, /* tp_setattro */ | ||||
363 | 0, /* tp_as_buffer */ | ||||
364 | Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0), /* tp_flags */ | ||||
365 | "Counter (little endian)", /* tp_doc */ | ||||
366 | 0, /*tp_traverse*/ | ||||
367 | 0, /*tp_clear*/ | ||||
368 | 0, /*tp_richcompare*/ | ||||
369 | 0, /*tp_weaklistoffset*/ | ||||
370 | #if PYTHON_API_VERSION1013 >= 1011 /* Python 2.2 and later */ | ||||
371 | 0, /*tp_iter*/ | ||||
372 | 0, /*tp_iternext*/ | ||||
373 | CounterLEObject_methods, /*tp_methods*/ | ||||
374 | #endif | ||||
375 | }; | ||||
376 | |||||
377 | static PyTypeObject | ||||
378 | PCT_CounterBEType = { | ||||
379 | PyVarObject_HEAD_INIT(NULL, 0){ { 1, ((void*)0) }, 0 }, /* deferred type init for compilation on Windows, type will be filled in at runtime */ | ||||
380 | "_counter.CounterBE", /* tp_name */ | ||||
381 | sizeof(PCT_CounterObject), /* tp_basicsize */ | ||||
382 | 0, /* tp_itemsize */ | ||||
383 | (destructor)CounterObject_dealloc, /* tp_dealloc */ | ||||
384 | 0, /* tp_print */ | ||||
385 | 0, /* tp_getattr */ | ||||
386 | 0, /* tp_setattr */ | ||||
387 | 0, /* tp_compare */ | ||||
388 | 0, /* tp_repr */ | ||||
389 | 0, /* tp_as_number */ | ||||
390 | 0, /* tp_as_sequence */ | ||||
391 | 0, /* tp_as_mapping */ | ||||
392 | 0, /* tp_hash */ | ||||
393 | (ternaryfunc)CounterObject_call, /* tp_call */ | ||||
394 | 0, /* tp_str */ | ||||
395 | CounterBEObject_getattro, /* tp_getattro */ | ||||
396 | 0, /* tp_setattro */ | ||||
397 | 0, /* tp_as_buffer */ | ||||
398 | Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0), /* tp_flags */ | ||||
399 | "Counter (big endian)", /* tp_doc */ | ||||
400 | 0, /*tp_traverse*/ | ||||
401 | 0, /*tp_clear*/ | ||||
402 | 0, /*tp_richcompare*/ | ||||
403 | 0, /*tp_weaklistoffset*/ | ||||
404 | #if PYTHON_API_VERSION1013 >= 1011 /* Python 2.2 and later */ | ||||
405 | 0, /*tp_iter*/ | ||||
406 | 0, /*tp_iternext*/ | ||||
407 | CounterBEObject_methods, /*tp_methods*/ | ||||
408 | #endif | ||||
409 | }; | ||||
410 | |||||
411 | /* | ||||
412 | * Python 2.1 doesn't seem to allow a C equivalent of the __init__ method, so | ||||
413 | * we use the module-level functions newLE and newBE here. | ||||
414 | */ | ||||
415 | static PyObject * | ||||
416 | CounterLE_new(PyObject *self, PyObject *args, PyObject *kwargs) | ||||
417 | { | ||||
418 | PCT_CounterObject *obj = NULL((void*)0); | ||||
419 | |||||
420 | /* Create the new object */ | ||||
421 | obj = PyObject_New(PCT_CounterObject, &PCT_CounterLEType)( (PCT_CounterObject *) _PyObject_New(&PCT_CounterLEType) ); | ||||
422 | if (obj == NULL((void*)0)) { | ||||
423 | return NULL((void*)0); | ||||
424 | } | ||||
425 | |||||
426 | /* Zero the custom portion of the structure */ | ||||
427 | memset(&obj->prefix, 0, sizeof(PCT_CounterObject) - offsetof(PCT_CounterObject, prefix)__builtin_offsetof(PCT_CounterObject, prefix)); | ||||
428 | |||||
429 | /* Call the object's initializer. Delete the object if this fails. */ | ||||
430 | if (CounterObject_init(obj, args, kwargs) != 0) { | ||||
431 | return NULL((void*)0); | ||||
432 | } | ||||
433 | |||||
434 | /* Set the inc_func pointer */ | ||||
435 | obj->inc_func = (void (*)(void *))CounterLEObject_increment; | ||||
436 | |||||
437 | /* Return the object */ | ||||
438 | return (PyObject *)obj; | ||||
439 | } | ||||
440 | |||||
441 | static PyObject * | ||||
442 | CounterBE_new(PyObject *self, PyObject *args, PyObject *kwargs) | ||||
443 | { | ||||
444 | PCT_CounterObject *obj = NULL((void*)0); | ||||
445 | |||||
446 | /* Create the new object */ | ||||
447 | obj = PyObject_New(PCT_CounterObject, &PCT_CounterBEType)( (PCT_CounterObject *) _PyObject_New(&PCT_CounterBEType) ); | ||||
448 | if (obj == NULL((void*)0)) { | ||||
449 | return NULL((void*)0); | ||||
450 | } | ||||
451 | |||||
452 | /* Zero the custom portion of the structure */ | ||||
453 | memset(&obj->prefix, 0, sizeof(PCT_CounterObject) - offsetof(PCT_CounterObject, prefix)__builtin_offsetof(PCT_CounterObject, prefix)); | ||||
454 | |||||
455 | /* Call the object's initializer. Delete the object if this fails. */ | ||||
456 | if (CounterObject_init(obj, args, kwargs) != 0) { | ||||
457 | return NULL((void*)0); | ||||
458 | } | ||||
459 | |||||
460 | /* Set the inc_func pointer */ | ||||
461 | obj->inc_func = (void (*)(void *))CounterBEObject_increment; | ||||
462 | |||||
463 | /* Return the object */ | ||||
464 | return (PyObject *)obj; | ||||
465 | } | ||||
466 | |||||
467 | /* | ||||
468 | * Module-level method table and module initialization function | ||||
469 | */ | ||||
470 | |||||
471 | static PyMethodDef module_methods[] = { | ||||
472 | {"_newLE", (PyCFunction) CounterLE_new, METH_VARARGS0x0001|METH_KEYWORDS0x0002, NULL((void*)0)}, | ||||
473 | {"_newBE", (PyCFunction) CounterBE_new, METH_VARARGS0x0001|METH_KEYWORDS0x0002, NULL((void*)0)}, | ||||
474 | {NULL((void*)0), NULL((void*)0), 0, NULL((void*)0)} /* end-of-list sentinel value */ | ||||
475 | }; | ||||
476 | |||||
477 | #ifdef IS_PY3K | ||||
478 | static struct PyModuleDef moduledef = { | ||||
479 | PyModuleDef_HEAD_INIT{ { 1, ((void*)0) }, ((void*)0), 0, ((void*)0), }, | ||||
480 | "_counter", | ||||
481 | NULL((void*)0), | ||||
482 | -1, | ||||
483 | module_methods, | ||||
484 | NULL((void*)0), | ||||
485 | NULL((void*)0), | ||||
486 | NULL((void*)0), | ||||
487 | NULL((void*)0) | ||||
488 | }; | ||||
489 | #endif | ||||
490 | |||||
491 | PyMODINIT_FUNCPyObject* | ||||
492 | #ifdef IS_PY3K | ||||
493 | PyInit__counter(void) | ||||
494 | #else | ||||
495 | init_counter(void) | ||||
496 | #endif | ||||
497 | { | ||||
498 | PyObject *m = NULL((void*)0); | ||||
499 | |||||
500 | if (PyType_Ready(&PCT_CounterLEType) < 0) | ||||
501 | goto errout; | ||||
502 | if (PyType_Ready(&PCT_CounterBEType) < 0) | ||||
503 | goto errout; | ||||
504 | |||||
505 | /* Initialize the module */ | ||||
506 | #ifdef IS_PY3K | ||||
507 | m = PyModule_Create(&moduledef)PyModule_Create2(&moduledef, 1013); | ||||
508 | #else | ||||
509 | m = Py_InitModule("_counter", module_methods); | ||||
510 | #endif | ||||
511 | if (m == NULL((void*)0)) | ||||
512 | goto errout; | ||||
513 | |||||
514 | /* Add the counter types to the module so that epydoc can see them, and so | ||||
515 | * that we can access them in the block cipher modules. */ | ||||
516 | PyObject_SetAttrString(m, "CounterBE", (PyObject *)&PCT_CounterBEType); | ||||
517 | PyObject_SetAttrString(m, "CounterLE", (PyObject *)&PCT_CounterLEType); | ||||
518 | |||||
519 | /* Allow block_template.c to do an ABI check */ | ||||
520 | PyModule_AddIntConstant(m, "_PCT_CTR_ABI_VERSION", PCT_CTR_ABI_VERSION1); | ||||
521 | |||||
522 | |||||
523 | out: | ||||
524 | /* Final error check */ | ||||
525 | if (m == NULL((void*)0) && !PyErr_Occurred()) { | ||||
526 | PyErr_SetString(PyExc_ImportError, "can't initialize module"); | ||||
527 | goto errout; | ||||
528 | } | ||||
529 | |||||
530 | /* Free local objects here */ | ||||
531 | |||||
532 | /* Return */ | ||||
533 | #ifdef IS_PY3K | ||||
534 | return m; | ||||
535 | #else | ||||
536 | return; | ||||
537 | #endif | ||||
538 | |||||
539 | errout: | ||||
540 | /* Free the module and other global objects here */ | ||||
541 | Py_CLEAR(m)do { PyObject *_py_tmp = ((PyObject*)(m)); if (_py_tmp != ((void *)0)) { (m) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp))); } } while (0); | ||||
542 | goto out; | ||||
543 | } | ||||
544 | |||||
545 | /* vim:set ts=4 sw=4 sts=4 expandtab: */ |
1 | #ifndef PyLong_FromLong |
2 | struct _object; |
3 | typedef struct _object PyObject; |
4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
5 | PyObject* PyLong_FromLong(long v) { |
6 | return clang_analyzer_PyObject_New_Reference(); |
7 | } |
8 | #else |
9 | #warning "API PyLong_FromLong is defined as a macro." |
10 | #endif |