]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Modules/md5module.c
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 2/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Modules / md5module.c
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Modules/md5module.c b/AppPkg/Applications/Python/Python-2.7.10/Modules/md5module.c
new file mode 100644 (file)
index 0000000..04c969d
--- /dev/null
@@ -0,0 +1,339 @@
+\r
+/* MD5 module */\r
+\r
+/* This module provides an interface to the RSA Data Security,\r
+   Inc. MD5 Message-Digest Algorithm, described in RFC 1321.\r
+   It requires the files md5c.c and md5.h (which are slightly changed\r
+   from the versions in the RFC to avoid the "global.h" file.) */\r
+\r
+\r
+/* MD5 objects */\r
+\r
+#include "Python.h"\r
+#include "structmember.h"\r
+#include "md5.h"\r
+\r
+typedef struct {\r
+    PyObject_HEAD\r
+    md5_state_t         md5;            /* the context holder */\r
+} md5object;\r
+\r
+static PyTypeObject MD5type;\r
+\r
+#define is_md5object(v)         ((v)->ob_type == &MD5type)\r
+\r
+static md5object *\r
+newmd5object(void)\r
+{\r
+    md5object *md5p;\r
+\r
+    md5p = PyObject_New(md5object, &MD5type);\r
+    if (md5p == NULL)\r
+        return NULL;\r
+\r
+    md5_init(&md5p->md5);       /* actual initialisation */\r
+    return md5p;\r
+}\r
+\r
+\r
+/* MD5 methods */\r
+\r
+static void\r
+md5_dealloc(md5object *md5p)\r
+{\r
+    PyObject_Del(md5p);\r
+}\r
+\r
+\r
+/* MD5 methods-as-attributes */\r
+\r
+static PyObject *\r
+md5_update(md5object *self, PyObject *args)\r
+{\r
+    Py_buffer view;\r
+    Py_ssize_t n;\r
+    unsigned char *buf;\r
+\r
+    if (!PyArg_ParseTuple(args, "s*:update", &view))\r
+        return NULL;\r
+\r
+    n = view.len;\r
+    buf = (unsigned char *) view.buf;\r
+    while (n > 0) {\r
+        Py_ssize_t nbytes;\r
+        if (n > INT_MAX)\r
+            nbytes = INT_MAX;\r
+        else\r
+            nbytes = n;\r
+        md5_append(&self->md5, buf,\r
+                   Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));\r
+        buf += nbytes;\r
+        n -= nbytes;\r
+    }\r
+\r
+    PyBuffer_Release(&view);\r
+    Py_RETURN_NONE;\r
+}\r
+\r
+PyDoc_STRVAR(update_doc,\r
+"update (arg)\n\\r
+\n\\r
+Update the md5 object with the string arg. Repeated calls are\n\\r
+equivalent to a single call with the concatenation of all the\n\\r
+arguments.");\r
+\r
+\r
+static PyObject *\r
+md5_digest(md5object *self)\r
+{\r
+    md5_state_t mdContext;\r
+    unsigned char aDigest[16];\r
+\r
+    /* make a temporary copy, and perform the final */\r
+    mdContext = self->md5;\r
+    md5_finish(&mdContext, aDigest);\r
+\r
+    return PyString_FromStringAndSize((char *)aDigest, 16);\r
+}\r
+\r
+PyDoc_STRVAR(digest_doc,\r
+"digest() -> string\n\\r
+\n\\r
+Return the digest of the strings passed to the update() method so\n\\r
+far. This is a 16-byte string which may contain non-ASCII characters,\n\\r
+including null bytes.");\r
+\r
+\r
+static PyObject *\r
+md5_hexdigest(md5object *self)\r
+{\r
+    md5_state_t mdContext;\r
+    unsigned char digest[16];\r
+    unsigned char hexdigest[32];\r
+    int i, j;\r
+\r
+    /* make a temporary copy, and perform the final */\r
+    mdContext = self->md5;\r
+    md5_finish(&mdContext, digest);\r
+\r
+    /* Make hex version of the digest */\r
+    for(i=j=0; i<16; i++) {\r
+        char c;\r
+        c = (digest[i] >> 4) & 0xf;\r
+        c = (c>9) ? c+'a'-10 : c + '0';\r
+        hexdigest[j++] = c;\r
+        c = (digest[i] & 0xf);\r
+        c = (c>9) ? c+'a'-10 : c + '0';\r
+        hexdigest[j++] = c;\r
+    }\r
+    return PyString_FromStringAndSize((char*)hexdigest, 32);\r
+}\r
+\r
+\r
+PyDoc_STRVAR(hexdigest_doc,\r
+"hexdigest() -> string\n\\r
+\n\\r
+Like digest(), but returns the digest as a string of hexadecimal digits.");\r
+\r
+\r
+static PyObject *\r
+md5_copy(md5object *self)\r
+{\r
+    md5object *md5p;\r
+\r
+    if ((md5p = newmd5object()) == NULL)\r
+        return NULL;\r
+\r
+    md5p->md5 = self->md5;\r
+\r
+    return (PyObject *)md5p;\r
+}\r
+\r
+PyDoc_STRVAR(copy_doc,\r
+"copy() -> md5 object\n\\r
+\n\\r
+Return a copy (``clone'') of the md5 object.");\r
+\r
+\r
+static PyMethodDef md5_methods[] = {\r
+    {"update",    (PyCFunction)md5_update,    METH_VARARGS, update_doc},\r
+    {"digest",    (PyCFunction)md5_digest,    METH_NOARGS,  digest_doc},\r
+    {"hexdigest", (PyCFunction)md5_hexdigest, METH_NOARGS,  hexdigest_doc},\r
+    {"copy",      (PyCFunction)md5_copy,      METH_NOARGS,  copy_doc},\r
+    {NULL, NULL}                             /* sentinel */\r
+};\r
+\r
+static PyObject *\r
+md5_get_block_size(PyObject *self, void *closure)\r
+{\r
+    return PyInt_FromLong(64);\r
+}\r
+\r
+static PyObject *\r
+md5_get_digest_size(PyObject *self, void *closure)\r
+{\r
+    return PyInt_FromLong(16);\r
+}\r
+\r
+static PyObject *\r
+md5_get_name(PyObject *self, void *closure)\r
+{\r
+    return PyString_FromStringAndSize("MD5", 3);\r
+}\r
+\r
+static PyGetSetDef md5_getseters[] = {\r
+    {"digest_size",\r
+     (getter)md5_get_digest_size, NULL,\r
+     NULL,\r
+     NULL},\r
+    {"block_size",\r
+     (getter)md5_get_block_size, NULL,\r
+     NULL,\r
+     NULL},\r
+    {"name",\r
+     (getter)md5_get_name, NULL,\r
+     NULL,\r
+     NULL},\r
+    /* the old md5 and sha modules support 'digest_size' as in PEP 247.\r
+     * the old sha module also supported 'digestsize'.  ugh. */\r
+    {"digestsize",\r
+     (getter)md5_get_digest_size, NULL,\r
+     NULL,\r
+     NULL},\r
+    {NULL}  /* Sentinel */\r
+};\r
+\r
+\r
+PyDoc_STRVAR(module_doc,\r
+"This module implements the interface to RSA's MD5 message digest\n\\r
+algorithm (see also Internet RFC 1321). Its use is quite\n\\r
+straightforward: use the new() to create an md5 object. You can now\n\\r
+feed this object with arbitrary strings using the update() method, and\n\\r
+at any point you can ask it for the digest (a strong kind of 128-bit\n\\r
+checksum, a.k.a. ``fingerprint'') of the concatenation of the strings\n\\r
+fed to it so far using the digest() method.\n\\r
+\n\\r
+Functions:\n\\r
+\n\\r
+new([arg]) -- return a new md5 object, initialized with arg if provided\n\\r
+md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\\r
+\n\\r
+Special Objects:\n\\r
+\n\\r
+MD5Type -- type object for md5 objects");\r
+\r
+PyDoc_STRVAR(md5type_doc,\r
+"An md5 represents the object used to calculate the MD5 checksum of a\n\\r
+string of information.\n\\r
+\n\\r
+Methods:\n\\r
+\n\\r
+update() -- updates the current digest with an additional string\n\\r
+digest() -- return the current digest value\n\\r
+hexdigest() -- return the current digest as a string of hexadecimal digits\n\\r
+copy() -- return a copy of the current md5 object");\r
+\r
+static PyTypeObject MD5type = {\r
+    PyVarObject_HEAD_INIT(NULL, 0)\r
+    "_md5.md5",                   /*tp_name*/\r
+    sizeof(md5object),            /*tp_size*/\r
+    0,                            /*tp_itemsize*/\r
+    /* methods */\r
+    (destructor)md5_dealloc,  /*tp_dealloc*/\r
+    0,                            /*tp_print*/\r
+    0,                        /*tp_getattr*/\r
+    0,                            /*tp_setattr*/\r
+    0,                            /*tp_compare*/\r
+    0,                            /*tp_repr*/\r
+    0,                            /*tp_as_number*/\r
+    0,                        /*tp_as_sequence*/\r
+    0,                            /*tp_as_mapping*/\r
+    0,                            /*tp_hash*/\r
+    0,                            /*tp_call*/\r
+    0,                            /*tp_str*/\r
+    0,                            /*tp_getattro*/\r
+    0,                            /*tp_setattro*/\r
+    0,                            /*tp_as_buffer*/\r
+    Py_TPFLAGS_DEFAULT,           /*tp_flags*/\r
+    md5type_doc,                  /*tp_doc*/\r
+    0,                        /*tp_traverse*/\r
+    0,                            /*tp_clear*/\r
+    0,                            /*tp_richcompare*/\r
+    0,                            /*tp_weaklistoffset*/\r
+    0,                            /*tp_iter*/\r
+    0,                            /*tp_iternext*/\r
+    md5_methods,                  /*tp_methods*/\r
+    0,                            /*tp_members*/\r
+    md5_getseters,            /*tp_getset*/\r
+};\r
+\r
+\r
+/* MD5 functions */\r
+\r
+static PyObject *\r
+MD5_new(PyObject *self, PyObject *args)\r
+{\r
+    md5object *md5p;\r
+    Py_buffer view = { 0 };\r
+    Py_ssize_t n;\r
+    unsigned char *buf;\r
+\r
+    if (!PyArg_ParseTuple(args, "|s*:new", &view))\r
+        return NULL;\r
+\r
+    if ((md5p = newmd5object()) == NULL) {\r
+        PyBuffer_Release(&view);\r
+        return NULL;\r
+    }\r
+\r
+    n = view.len;\r
+    buf = (unsigned char *) view.buf;\r
+    while (n > 0) {\r
+        Py_ssize_t nbytes;\r
+        if (n > INT_MAX)\r
+            nbytes = INT_MAX;\r
+        else\r
+            nbytes = n;\r
+        md5_append(&md5p->md5, buf,\r
+                   Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));\r
+        buf += nbytes;\r
+        n -= nbytes;\r
+    }\r
+    PyBuffer_Release(&view);\r
+\r
+    return (PyObject *)md5p;\r
+}\r
+\r
+PyDoc_STRVAR(new_doc,\r
+"new([arg]) -> md5 object\n\\r
+\n\\r
+Return a new md5 object. If arg is present, the method call update(arg)\n\\r
+is made.");\r
+\r
+\r
+/* List of functions exported by this module */\r
+\r
+static PyMethodDef md5_functions[] = {\r
+    {"new",             (PyCFunction)MD5_new, METH_VARARGS, new_doc},\r
+    {NULL,              NULL}   /* Sentinel */\r
+};\r
+\r
+\r
+/* Initialize this module. */\r
+\r
+PyMODINIT_FUNC\r
+init_md5(void)\r
+{\r
+    PyObject *m, *d;\r
+\r
+    Py_TYPE(&MD5type) = &PyType_Type;\r
+    if (PyType_Ready(&MD5type) < 0)\r
+        return;\r
+    m = Py_InitModule3("_md5", md5_functions, module_doc);\r
+    if (m == NULL)\r
+        return;\r
+    d = PyModule_GetDict(m);\r
+    PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type);\r
+    PyModule_AddIntConstant(m, "digest_size", 16);\r
+    /* No need to check the error here, the caller will do that */\r
+}\r