Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Modules/shamodule.c

    r2 r388  
    2222/* Endianness testing and definitions */
    2323#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
    24         if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
     24        if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
    2525
    2626#define PCT_LITTLE_ENDIAN 1
     
    3232
    3333#if SIZEOF_INT == 4
    34 typedef unsigned int SHA_INT32; /* 32-bit integer */
     34typedef unsigned int SHA_INT32; /* 32-bit integer */
    3535#else
    3636/* not defined. compilation will die. */
     
    4646typedef struct {
    4747    PyObject_HEAD
    48     SHA_INT32 digest[5];                /* Message digest */
    49     SHA_INT32 count_lo, count_hi;       /* 64-bit bit count */
    50     SHA_BYTE data[SHA_BLOCKSIZE];       /* SHA data buffer */
     48    SHA_INT32 digest[5];                /* Message digest */
     49    SHA_INT32 count_lo, count_hi;       /* 64-bit bit count */
     50    SHA_BYTE data[SHA_BLOCKSIZE];       /* SHA data buffer */
    5151    int Endianness;
    52     int local;                          /* unprocessed amount in data */
     52    int local;                          /* unprocessed amount in data */
    5353} SHAobject;
    5454
     
    6161
    6262    if ( Endianness == PCT_BIG_ENDIAN )
    63         return;
     63        return;
    6464
    6565    byteCount /= sizeof(*buffer);
     
    112112   rcs@cs.arizona.edu for discovering this */
    113113
    114 /*#define f1(x,y,z)     ((x & y) | (~x & z))            // Rounds  0-19 */
    115 #define f1(x,y,z)       (z ^ (x & (y ^ z)))             /* Rounds  0-19 */
    116 #define f2(x,y,z)       (x ^ y ^ z)                     /* Rounds 20-39 */
    117 /*#define f3(x,y,z)     ((x & y) | (x & z) | (y & z))   // Rounds 40-59 */
    118 #define f3(x,y,z)       ((x & y) | (z & (x | y)))       /* Rounds 40-59 */
    119 #define f4(x,y,z)       (x ^ y ^ z)                     /* Rounds 60-79 */
     114/*#define f1(x,y,z)     ((x & y) | (~x & z))            // Rounds  0-19 */
     115#define f1(x,y,z)       (z ^ (x & (y ^ z)))             /* Rounds  0-19 */
     116#define f2(x,y,z)       (x ^ y ^ z)                     /* Rounds 20-39 */
     117/*#define f3(x,y,z)     ((x & y) | (x & z) | (y & z))   // Rounds 40-59 */
     118#define f3(x,y,z)       ((x & y) | (z & (x | y)))       /* Rounds 40-59 */
     119#define f4(x,y,z)       (x ^ y ^ z)                     /* Rounds 60-79 */
    120120
    121121/* SHA constants */
    122122
    123 #define CONST1          0x5a827999L                     /* Rounds  0-19 */
    124 #define CONST2          0x6ed9eba1L                     /* Rounds 20-39 */
    125 #define CONST3          0x8f1bbcdcL                     /* Rounds 40-59 */
    126 #define CONST4          0xca62c1d6L                     /* Rounds 60-79 */
     123#define CONST1          0x5a827999L                     /* Rounds  0-19 */
     124#define CONST2          0x6ed9eba1L                     /* Rounds 20-39 */
     125#define CONST3          0x8f1bbcdcL                     /* Rounds 40-59 */
     126#define CONST4          0xca62c1d6L                     /* Rounds 60-79 */
    127127
    128128/* 32-bit rotate */
    129129
    130 #define R32(x,n)        ((x << n) | (x >> (32 - n)))
     130#define R32(x,n)        ((x << n) | (x >> (32 - n)))
    131131
    132132/* the generic case, for when the overall rotation is not unraveled */
    133133
    134 #define FG(n)   \
    135     T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n;  \
     134#define FG(n)   \
     135    T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n;  \
    136136    E = D; D = C; C = R32(B,30); B = A; A = T
    137137
    138138/* specific cases, for when the overall rotation is unraveled */
    139139
    140 #define FA(n)   \
     140#define FA(n)   \
    141141    T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; B = R32(B,30)
    142142
    143 #define FB(n)   \
     143#define FB(n)   \
    144144    E = R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n; A = R32(A,30)
    145145
    146 #define FC(n)   \
     146#define FC(n)   \
    147147    D = R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n; T = R32(T,30)
    148148
    149 #define FD(n)   \
     149#define FD(n)   \
    150150    C = R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n; E = R32(E,30)
    151151
    152 #define FE(n)   \
     152#define FE(n)   \
    153153    B = R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n; D = R32(D,30)
    154154
    155 #define FT(n)   \
     155#define FT(n)   \
    156156    A = R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n; C = R32(C,30)
    157157
     
    168168
    169169    for (i = 16; i < 80; ++i) {
    170         W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
    171 
    172         /* extra rotation fix */
    173         W[i] = R32(W[i], 1);
     170        W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
     171
     172        /* extra rotation fix */
     173        W[i] = R32(W[i], 1);
    174174    }
    175175    A = sha_info->digest[0];
     
    237237
    238238static void
    239 sha_update(SHAobject *sha_info, SHA_BYTE *buffer, int count)
    240 {
    241     int i;
     239sha_update(SHAobject *sha_info, SHA_BYTE *buffer, unsigned int count)
     240{
     241    unsigned int i;
    242242    SHA_INT32 clo;
    243243
     
    287287    ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
    288288    if (count > SHA_BLOCKSIZE - 8) {
    289         memset(((SHA_BYTE *) sha_info->data) + count, 0,
    290                SHA_BLOCKSIZE - count);
    291         sha_transform(sha_info);
    292         memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
     289        memset(((SHA_BYTE *) sha_info->data) + count, 0,
     290               SHA_BLOCKSIZE - count);
     291        sha_transform(sha_info);
     292        memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
    293293    }
    294294    else {
    295         memset(((SHA_BYTE *) sha_info->data) + count, 0,
    296                SHA_BLOCKSIZE - 8 - count);
     295        memset(((SHA_BYTE *) sha_info->data) + count, 0,
     296               SHA_BLOCKSIZE - 8 - count);
    297297    }
    298298
     
    403403    retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
    404404    if (!retval)
    405             return NULL;
     405            return NULL;
    406406    hex_digest = PyString_AsString(retval);
    407407    if (!hex_digest) {
    408             Py_DECREF(retval);
    409             return NULL;
     408            Py_DECREF(retval);
     409            return NULL;
    410410    }
    411411
     
    414414        char c;
    415415        c = (digest[i] >> 4) & 0xf;
    416         c = (c>9) ? c+'a'-10 : c + '0';
     416        c = (c>9) ? c+'a'-10 : c + '0';
    417417        hex_digest[j++] = c;
    418418        c = (digest[i] & 0xf);
    419         c = (c>9) ? c+'a'-10 : c + '0';
     419        c = (c>9) ? c+'a'-10 : c + '0';
    420420        hex_digest[j++] = c;
    421421    }
     
    429429SHA_update(SHAobject *self, PyObject *args)
    430430{
    431     unsigned char *cp;
    432     int len;
    433 
    434     if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
     431    Py_buffer view;
     432    Py_ssize_t n;
     433    unsigned char *buf;
     434
     435    if (!PyArg_ParseTuple(args, "s*:update", &view))
    435436        return NULL;
    436437
    437     sha_update(self, cp, len);
    438 
    439     Py_INCREF(Py_None);
    440     return Py_None;
     438    n = view.len;
     439    buf = (unsigned char *) view.buf;
     440    while (n > 0) {
     441        Py_ssize_t nbytes;
     442        if (n > INT_MAX)
     443            nbytes = INT_MAX;
     444        else
     445            nbytes = n;
     446        sha_update(self, buf,
     447                   Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));
     448        buf += nbytes;
     449        n -= nbytes;
     450    }
     451
     452    PyBuffer_Release(&view);
     453    Py_RETURN_NONE;
    441454}
    442455
    443456static PyMethodDef SHA_methods[] = {
    444     {"copy",      (PyCFunction)SHA_copy,      METH_NOARGS,  SHA_copy__doc__},
    445     {"digest",    (PyCFunction)SHA_digest,    METH_NOARGS,  SHA_digest__doc__},
     457    {"copy",      (PyCFunction)SHA_copy,      METH_NOARGS,  SHA_copy__doc__},
     458    {"digest",    (PyCFunction)SHA_digest,    METH_NOARGS,  SHA_digest__doc__},
    446459    {"hexdigest", (PyCFunction)SHA_hexdigest, METH_NOARGS,  SHA_hexdigest__doc__},
    447     {"update",    (PyCFunction)SHA_update,    METH_VARARGS, SHA_update__doc__},
    448     {NULL,        NULL}         /* sentinel */
     460    {"update",    (PyCFunction)SHA_update,    METH_VARARGS, SHA_update__doc__},
     461    {NULL,        NULL}         /* sentinel */
    449462};
    450463
     
    491504static PyTypeObject SHAtype = {
    492505    PyVarObject_HEAD_INIT(NULL, 0)
    493     "_sha.sha",         /*tp_name*/
    494     sizeof(SHAobject),  /*tp_size*/
    495     0,                  /*tp_itemsize*/
     506    "_sha.sha",         /*tp_name*/
     507    sizeof(SHAobject),  /*tp_size*/
     508    0,                  /*tp_itemsize*/
    496509    /* methods */
    497     SHA_dealloc,        /*tp_dealloc*/
    498     0,                  /*tp_print*/
     510    SHA_dealloc,        /*tp_dealloc*/
     511    0,                  /*tp_print*/
    499512    0,                  /*tp_getattr*/
    500513    0,                  /*tp_setattr*/
     
    513526    0,                  /*tp_doc*/
    514527    0,                  /*tp_traverse*/
    515     0,                  /*tp_clear*/
    516     0,                  /*tp_richcompare*/
    517     0,                  /*tp_weaklistoffset*/
    518     0,                  /*tp_iter*/
    519     0,                  /*tp_iternext*/
    520     SHA_methods,        /* tp_methods */
     528    0,                  /*tp_clear*/
     529    0,                  /*tp_richcompare*/
     530    0,                  /*tp_weaklistoffset*/
     531    0,                  /*tp_iter*/
     532    0,                  /*tp_iternext*/
     533    SHA_methods,        /* tp_methods */
    521534    0,                  /* tp_members */
    522535    SHA_getseters,      /* tp_getset */
     
    536549    static char *kwlist[] = {"string", NULL};
    537550    SHAobject *new;
    538     unsigned char *cp = NULL;
    539     int len;
    540 
    541     if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist,
    542                                      &cp, &len)) {
     551    Py_buffer view = { 0 };
     552    Py_ssize_t n;
     553    unsigned char *buf;
     554
     555    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
     556                                     &view)) {
    543557        return NULL;
    544558    }
    545559
    546     if ((new = newSHAobject()) == NULL)
     560    if ((new = newSHAobject()) == NULL) {
     561        PyBuffer_Release(&view);
    547562        return NULL;
     563    }
    548564
    549565    sha_init(new);
     
    551567    if (PyErr_Occurred()) {
    552568        Py_DECREF(new);
     569        PyBuffer_Release(&view);
    553570        return NULL;
    554571    }
    555     if (cp)
    556         sha_update(new, cp, len);
     572
     573    n = view.len;
     574    buf = (unsigned char *) view.buf;
     575    while (n > 0) {
     576        Py_ssize_t nbytes;
     577        if (n > INT_MAX)
     578            nbytes = INT_MAX;
     579        else
     580            nbytes = n;
     581        sha_update(new, buf,
     582                   Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));
     583        buf += nbytes;
     584        n -= nbytes;
     585    }
     586
     587    PyBuffer_Release(&view);
    557588
    558589    return (PyObject *)new;
     
    564595static struct PyMethodDef SHA_functions[] = {
    565596    {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
    566     {NULL,      NULL}            /* Sentinel */
     597    {NULL,      NULL}            /* Sentinel */
    567598};
    568599
     
    582613    m = Py_InitModule("_sha", SHA_functions);
    583614    if (m == NULL)
    584         return;
     615        return;
    585616
    586617    /* Add some symbolic constants to the module */
    587618    insint("blocksize", 1);  /* For future use, in case some hash
    588619                                functions require an integral number of
    589                                 blocks */ 
     620                                blocks */
    590621    insint("digestsize", 20);
    591622    insint("digest_size", 20);
Note: See TracChangeset for help on using the changeset viewer.