annotate mercurial/thirdparty/sha1dc/cext.c @ 44082:c3f741bb2f33

sha1dc: declare all variables at begininng of block This is required to appease ancient C language standards, which msvc 2008 still requires for Python 2.7 on Windows. Differential Revision: https://phab.mercurial-scm.org/D7877
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 14 Jan 2020 17:39:12 -0800
parents bde1cd4c99d9
children 29a110e2776e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
1 #define PY_SSIZE_T_CLEAN
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
2 #include <Python.h>
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
3
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
4 #include "lib/sha1.h"
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
5
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
6 #if PY_MAJOR_VERSION >= 3
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
7 #define IS_PY3K
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
8 #endif
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
9
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
10 /* helper to switch things like string literal depending on Python version */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
11 #ifdef IS_PY3K
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
12 #define PY23(py2, py3) py3
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
13 #else
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
14 #define PY23(py2, py3) py2
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
15 #endif
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
16
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
17 static char sha1dc_doc[] = "Efficient detection of SHA1 collision constructs.";
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
18
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
19 /* clang-format off */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
20 typedef struct {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
21 PyObject_HEAD
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
22 SHA1_CTX ctx;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
23 } pysha1ctx;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
24 /* clang-format on */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
25
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
26 static int pysha1ctx_init(pysha1ctx *self, PyObject *args)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
27 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
28 const char *data = NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
29 Py_ssize_t len;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
30
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
31 SHA1DCInit(&(self->ctx));
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
32 /* We don't want "safe" sha1s, wherein sha1dc can give you a
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
33 different hash for something that's trying to give you a
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
34 collision. We just want to detect collisions.
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
35 */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
36 SHA1DCSetSafeHash(&(self->ctx), 0);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
37 if (!PyArg_ParseTuple(args, PY23("|s#", "|y#"), &data, &len)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
38 return -1;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
39 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
40 if (data) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
41 SHA1DCUpdate(&(self->ctx), data, len);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
42 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
43 return 0;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
44 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
45
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
46 static void pysha1ctx_dealloc(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
47 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
48 PyObject_Del(self);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
49 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
50
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
51 static PyObject *pysha1ctx_update(pysha1ctx *self, PyObject *args)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
52 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
53 const char *data;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
54 Py_ssize_t len;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
55 if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &data, &len)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
56 return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
57 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
58 SHA1DCUpdate(&(self->ctx), data, len);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
59 Py_RETURN_NONE;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
60 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
61
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
62 /* it is intentional that this take a ctx by value, as that clones the
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
63 context so we can keep using .update() without poisoning the state
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
64 with padding.
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
65 */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
66 static int finalize(SHA1_CTX ctx, unsigned char *hash_out)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
67 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
68 if (SHA1DCFinal(hash_out, &ctx)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
69 PyErr_SetString(PyExc_OverflowError,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
70 "sha1 collision attack detected");
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
71 return 0;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
72 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
73 return 1;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
74 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
75
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
76 static PyObject *pysha1ctx_digest(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
77 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
78 unsigned char hash[20];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
79 if (!finalize(self->ctx, hash)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
80 return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
81 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
82 return PyBytes_FromStringAndSize((char *)hash, 20);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
83 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
84
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
85 static PyObject *pysha1ctx_hexdigest(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
86 {
44082
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
87 static const char hexdigit[] = "0123456789abcdef";
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
88 unsigned char hash[20];
44082
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
89 char hexhash[40];
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
90 int i;
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
91 if (!finalize(self->ctx, hash)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
92 return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
93 }
44082
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
94 for (i = 0; i < 20; ++i) {
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
95 hexhash[i * 2] = hexdigit[hash[i] >> 4];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
96 hexhash[i * 2 + 1] = hexdigit[hash[i] & 15];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
97 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
98 return PyString_FromStringAndSize(hexhash, 40);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
99 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
100
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
101 static PyTypeObject sha1ctxType;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
102
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
103 static PyObject *pysha1ctx_copy(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
104 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
105 pysha1ctx *clone = (pysha1ctx *)PyObject_New(pysha1ctx, &sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
106 if (!clone) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
107 return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
108 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
109 clone->ctx = self->ctx;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
110 return (PyObject *)clone;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
111 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
112
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
113 static PyMethodDef pysha1ctx_methods[] = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
114 {"update", (PyCFunction)pysha1ctx_update, METH_VARARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
115 "Update this hash object's state with the provided bytes."},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
116 {"digest", (PyCFunction)pysha1ctx_digest, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
117 "Return the digest value as a string of binary data."},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
118 {"hexdigest", (PyCFunction)pysha1ctx_hexdigest, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
119 "Return the digest value as a string of hexadecimal digits."},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
120 {"copy", (PyCFunction)pysha1ctx_copy, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
121 "Return a copy of the hash object."},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
122 {NULL},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
123 };
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
124
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
125 /* clang-format off */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
126 static PyTypeObject sha1ctxType = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
127 PyVarObject_HEAD_INIT(NULL, 0) /* header */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
128 "sha1dc.sha1", /* tp_name */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
129 sizeof(pysha1ctx), /* tp_basicsize */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
130 0, /* tp_itemsize */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
131 (destructor)pysha1ctx_dealloc, /* tp_dealloc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
132 0, /* tp_print */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
133 0, /* tp_getattr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
134 0, /* tp_setattr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
135 0, /* tp_compare */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
136 0, /* tp_repr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
137 0, /* tp_as_number */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
138 0, /* tp_as_sequence */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
139 0, /* tp_as_mapping */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
140 0, /* tp_hash */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
141 0, /* tp_call */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
142 0, /* tp_str */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
143 0, /* tp_getattro */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
144 0, /* tp_setattro */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
145 0, /* tp_as_buffer */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
146 Py_TPFLAGS_DEFAULT, /* tp_flags */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
147 "sha1 implementation that looks for collisions", /* tp_doc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
148 0, /* tp_traverse */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
149 0, /* tp_clear */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
150 0, /* tp_richcompare */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
151 0, /* tp_weaklistoffset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
152 0, /* tp_iter */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
153 0, /* tp_iternext */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
154 pysha1ctx_methods, /* tp_methods */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
155 0, /* tp_members */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
156 0, /* tp_getset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
157 0, /* tp_base */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
158 0, /* tp_dict */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
159 0, /* tp_descr_get */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
160 0, /* tp_descr_set */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
161 0, /* tp_dictoffset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
162 (initproc)pysha1ctx_init, /* tp_init */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
163 0, /* tp_alloc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
164 };
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
165 /* clang-format on */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
166
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
167 static PyMethodDef methods[] = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
168 {NULL, NULL},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
169 };
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
170
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
171 static void module_init(PyObject *mod)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
172 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
173 sha1ctxType.tp_new = PyType_GenericNew;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
174 if (PyType_Ready(&sha1ctxType) < 0) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
175 return;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
176 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
177 Py_INCREF(&sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
178
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
179 PyModule_AddObject(mod, "sha1", (PyObject *)&sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
180 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
181
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
182 #ifdef IS_PY3K
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
183 static struct PyModuleDef sha1dc_module = {PyModuleDef_HEAD_INIT, "sha1dc",
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
184 sha1dc_doc, -1, methods};
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
185
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
186 PyMODINIT_FUNC PyInit_sha1dc(void)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
187 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
188 PyObject *mod = PyModule_Create(&sha1dc_module);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
189 module_init(mod);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
190 return mod;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
191 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
192 #else
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
193 PyMODINIT_FUNC initsha1dc(void)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
194 {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
195 PyObject *mod = Py_InitModule3("sha1dc", methods, sha1dc_doc);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
196 module_init(mod);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
197 }
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
198 #endif