mercurial/thirdparty/sha1dc/cext.c
author Augie Fackler <augie@google.com>
Wed, 08 Jan 2020 15:59:52 -0500
changeset 44058 bde1cd4c99d9
child 44082 c3f741bb2f33
permissions -rw-r--r--
sha1dc: initial implementation of Python extension A future change will use this when available to avoid sha1 collision issues until we can get moved to something else. Differential Revision: https://phab.mercurial-scm.org/D7815
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    87
	unsigned char hash[20];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    88
	if (!finalize(self->ctx, hash)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    89
		return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    90
	}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    91
	char hexhash[40];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    92
	static const char hexdigit[] = "0123456789abcdef";
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    93
	for (int i = 0; i < 20; ++i) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    94
		hexhash[i * 2] = hexdigit[hash[i] >> 4];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    95
		hexhash[i * 2 + 1] = hexdigit[hash[i] & 15];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    96
	}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    97
	return PyString_FromStringAndSize(hexhash, 40);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    98
}
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
static PyTypeObject sha1ctxType;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   101
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   102
static PyObject *pysha1ctx_copy(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   103
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   104
	pysha1ctx *clone = (pysha1ctx *)PyObject_New(pysha1ctx, &sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   105
	if (!clone) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   106
		return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   107
	}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   108
	clone->ctx = self->ctx;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
	return (PyObject *)clone;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   110
}
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
static PyMethodDef pysha1ctx_methods[] = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
    {"update", (PyCFunction)pysha1ctx_update, METH_VARARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
     "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
   115
    {"digest", (PyCFunction)pysha1ctx_digest, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
     "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
   117
    {"hexdigest", (PyCFunction)pysha1ctx_hexdigest, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
     "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
   119
    {"copy", (PyCFunction)pysha1ctx_copy, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
     "Return a copy of the hash object."},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
    {NULL},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   122
};
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
/* clang-format off */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
static PyTypeObject sha1ctxType = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
	PyVarObject_HEAD_INIT(NULL, 0)                    /* header */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
	"sha1dc.sha1",                                    /* tp_name */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
	sizeof(pysha1ctx),                                /* tp_basicsize */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   129
	0,                                                /* tp_itemsize */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   130
	(destructor)pysha1ctx_dealloc,                    /* tp_dealloc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   131
	0,                                                /* tp_print */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   132
	0,                                                /* tp_getattr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
	0,                                                /* tp_setattr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   134
	0,                                                /* tp_compare */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   135
	0,                                                /* tp_repr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   136
	0,                                                /* tp_as_number */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   137
	0,                                                /* tp_as_sequence */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   138
	0,                                                /* tp_as_mapping */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   139
	0,                                                /* tp_hash */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   140
	0,                                                /* tp_call */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   141
	0,                                                /* tp_str */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   142
	0,                                                /* tp_getattro */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   143
	0,                                                /* tp_setattro */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   144
	0,                                                /* tp_as_buffer */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   145
	Py_TPFLAGS_DEFAULT,                               /* tp_flags */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   146
	"sha1 implementation that looks for collisions",  /* tp_doc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   147
	0,                                                /* tp_traverse */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   148
	0,                                                /* tp_clear */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   149
	0,                                                /* tp_richcompare */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   150
	0,                                                /* tp_weaklistoffset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   151
	0,                                                /* tp_iter */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   152
	0,                                                /* tp_iternext */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   153
	pysha1ctx_methods,                                /* tp_methods */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   154
	0,                                                /* tp_members */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   155
	0,                                                /* tp_getset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   156
	0,                                                /* tp_base */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   157
	0,                                                /* tp_dict */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   158
	0,                                                /* tp_descr_get */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   159
	0,                                                /* tp_descr_set */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   160
	0,                                                /* tp_dictoffset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   161
	(initproc)pysha1ctx_init,                         /* tp_init */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   162
	0,                                                /* tp_alloc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   163
};
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   164
/* clang-format on */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   165
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   166
static PyMethodDef methods[] = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   167
    {NULL, NULL},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   168
};
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
static void module_init(PyObject *mod)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   171
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   172
	sha1ctxType.tp_new = PyType_GenericNew;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   173
	if (PyType_Ready(&sha1ctxType) < 0) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   174
		return;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   175
	}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   176
	Py_INCREF(&sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   177
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   178
	PyModule_AddObject(mod, "sha1", (PyObject *)&sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   179
}
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
#ifdef IS_PY3K
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   182
static struct PyModuleDef sha1dc_module = {PyModuleDef_HEAD_INIT, "sha1dc",
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   183
                                           sha1dc_doc, -1, methods};
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   184
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   185
PyMODINIT_FUNC PyInit_sha1dc(void)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   186
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   187
	PyObject *mod = PyModule_Create(&sha1dc_module);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   188
	module_init(mod);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   189
	return mod;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   190
}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   191
#else
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   192
PyMODINIT_FUNC initsha1dc(void)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   193
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   194
	PyObject *mod = Py_InitModule3("sha1dc", methods, sha1dc_doc);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   195
	module_init(mod);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   196
}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   197
#endif