mercurial/thirdparty/sha1dc/cext.c
author Pierre-Yves David <pierre-yves.david@octobus.net>
Thu, 01 Oct 2020 09:29:49 +0200
changeset 45671 2d6aea053153
parent 44087 dc9b53482689
permissions -rw-r--r--
copies: add a HASCOPIESINFO flag to highlight rev with useful data If some files changes that may impact copy tracing are detected, we set this flag. This helps the copy tracing algorithm to skip fetching possibly expensive data when unnecessary. Differential Revision: https://phab.mercurial-scm.org/D9139
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
{
44087
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    28
	Py_buffer data;
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    29
	data.obj = NULL;
44058
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);
44087
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    37
	if (!PyArg_ParseTuple(args, PY23("|s*", "|y*"), &data)) {
44058
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
	}
44087
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    40
	if (data.obj) {
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    41
		if (!PyBuffer_IsContiguous(&data, 'C') || data.ndim > 1) {
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    42
			PyErr_SetString(PyExc_BufferError,
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    43
			                "buffer must be contiguous and single dimension");
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    44
			PyBuffer_Release(&data);
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    45
			return -1;
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    46
		}
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    47
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    48
		SHA1DCUpdate(&(self->ctx), data.buf, data.len);
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    49
		PyBuffer_Release(&data);
44058
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
	return 0;
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
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
static void pysha1ctx_dealloc(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
	PyObject_Del(self);
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
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
static PyObject *pysha1ctx_update(pysha1ctx *self, PyObject *args)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
{
44087
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    61
	Py_buffer data;
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    62
	if (!PyArg_ParseTuple(args, PY23("s*", "y*"), &data)) {
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
		return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
	}
44087
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    65
	if (!PyBuffer_IsContiguous(&data, 'C') || data.ndim > 1) {
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    66
		PyErr_SetString(PyExc_BufferError,
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    67
		                "buffer must be contiguous and single dimension");
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    68
		PyBuffer_Release(&data);
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    69
		return NULL;
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    70
	}
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    71
	SHA1DCUpdate(&(self->ctx), data.buf, data.len);
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44083
diff changeset
    72
	PyBuffer_Release(&data);
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
	Py_RETURN_NONE;
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
/* 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
    77
   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
    78
   with padding.
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
*/
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    80
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
    81
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    82
	if (SHA1DCFinal(hash_out, &ctx)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    83
		PyErr_SetString(PyExc_OverflowError,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    84
		                "sha1 collision attack detected");
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    85
		return 0;
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
	return 1;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    88
}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    89
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    90
static PyObject *pysha1ctx_digest(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    91
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    92
	unsigned char hash[20];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    93
	if (!finalize(self->ctx, hash)) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    94
		return NULL;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    95
	}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    96
	return PyBytes_FromStringAndSize((char *)hash, 20);
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
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    99
static PyObject *pysha1ctx_hexdigest(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   100
{
44082
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
   101
	static const char hexdigit[] = "0123456789abcdef";
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   102
	unsigned char hash[20];
44082
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
   103
	char hexhash[40];
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
   104
	int i;
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   105
	if (!finalize(self->ctx, hash)) {
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
	}
44082
c3f741bb2f33 sha1dc: declare all variables at begininng of block
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
   108
	for (i = 0; i < 20; ++i) {
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
		hexhash[i * 2] = hexdigit[hash[i] >> 4];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   110
		hexhash[i * 2 + 1] = hexdigit[hash[i] & 15];
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   111
	}
44083
29a110e2776e sha1dc: use proper string functions on Python 2/3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44082
diff changeset
   112
	return PY23(PyString_FromStringAndSize, PyUnicode_FromStringAndSize)(hexhash, 40);
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
static PyTypeObject sha1ctxType;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
static PyObject *pysha1ctx_copy(pysha1ctx *self)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   119
	pysha1ctx *clone = (pysha1ctx *)PyObject_New(pysha1ctx, &sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
	if (!clone) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
		return 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
	clone->ctx = self->ctx;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   124
	return (PyObject *)clone;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
static PyMethodDef pysha1ctx_methods[] = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
    {"update", (PyCFunction)pysha1ctx_update, METH_VARARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   129
     "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
   130
    {"digest", (PyCFunction)pysha1ctx_digest, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   131
     "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
   132
    {"hexdigest", (PyCFunction)pysha1ctx_hexdigest, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
     "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
   134
    {"copy", (PyCFunction)pysha1ctx_copy, METH_NOARGS,
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   135
     "Return a copy of the hash object."},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   136
    {NULL},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   137
};
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   138
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   139
/* clang-format off */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   140
static PyTypeObject sha1ctxType = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   141
	PyVarObject_HEAD_INIT(NULL, 0)                    /* header */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   142
	"sha1dc.sha1",                                    /* tp_name */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   143
	sizeof(pysha1ctx),                                /* tp_basicsize */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   144
	0,                                                /* tp_itemsize */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   145
	(destructor)pysha1ctx_dealloc,                    /* tp_dealloc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   146
	0,                                                /* tp_print */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   147
	0,                                                /* tp_getattr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   148
	0,                                                /* tp_setattr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   149
	0,                                                /* tp_compare */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   150
	0,                                                /* tp_repr */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   151
	0,                                                /* tp_as_number */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   152
	0,                                                /* tp_as_sequence */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   153
	0,                                                /* tp_as_mapping */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   154
	0,                                                /* tp_hash */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   155
	0,                                                /* tp_call */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   156
	0,                                                /* tp_str */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   157
	0,                                                /* tp_getattro */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   158
	0,                                                /* tp_setattro */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   159
	0,                                                /* tp_as_buffer */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   160
	Py_TPFLAGS_DEFAULT,                               /* tp_flags */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   161
	"sha1 implementation that looks for collisions",  /* tp_doc */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   162
	0,                                                /* tp_traverse */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   163
	0,                                                /* tp_clear */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   164
	0,                                                /* tp_richcompare */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   165
	0,                                                /* tp_weaklistoffset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   166
	0,                                                /* tp_iter */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   167
	0,                                                /* tp_iternext */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   168
	pysha1ctx_methods,                                /* tp_methods */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   169
	0,                                                /* tp_members */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   170
	0,                                                /* tp_getset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   171
	0,                                                /* tp_base */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   172
	0,                                                /* tp_dict */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   173
	0,                                                /* tp_descr_get */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   174
	0,                                                /* tp_descr_set */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   175
	0,                                                /* tp_dictoffset */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   176
	(initproc)pysha1ctx_init,                         /* tp_init */
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   177
	0,                                                /* tp_alloc */
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
/* clang-format on */
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
static PyMethodDef methods[] = {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   182
    {NULL, NULL},
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   183
};
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
static void module_init(PyObject *mod)
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
	sha1ctxType.tp_new = PyType_GenericNew;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   188
	if (PyType_Ready(&sha1ctxType) < 0) {
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   189
		return;
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
	Py_INCREF(&sha1ctxType);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   192
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   193
	PyModule_AddObject(mod, "sha1", (PyObject *)&sha1ctxType);
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
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   196
#ifdef IS_PY3K
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   197
static struct PyModuleDef sha1dc_module = {PyModuleDef_HEAD_INIT, "sha1dc",
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   198
                                           sha1dc_doc, -1, methods};
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   199
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   200
PyMODINIT_FUNC PyInit_sha1dc(void)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   201
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   202
	PyObject *mod = PyModule_Create(&sha1dc_module);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   203
	module_init(mod);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   204
	return mod;
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   205
}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   206
#else
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   207
PyMODINIT_FUNC initsha1dc(void)
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   208
{
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   209
	PyObject *mod = Py_InitModule3("sha1dc", methods, sha1dc_doc);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   210
	module_init(mod);
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   211
}
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
   212
#endif