Commit 4ad8217a authored by Tim Peters's avatar Tim Peters

win32_urandom(): Rewrite to Python C standards (hard tabs, function name

in first column, no parens around return value).
parent 38330fe5
......@@ -7236,15 +7236,16 @@ typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
static CRYPTGENRANDOM pCryptGenRandom = NULL;
static HCRYPTPROV hCryptProv = 0;
static PyObject* win32_urandom(PyObject *self, PyObject *args)
static PyObject*
win32_urandom(PyObject *self, PyObject *args)
{
int howMany = 0;
unsigned char* bytes = NULL;
PyObject* returnVal = NULL;
/* Read arguments */
if (!PyArg_ParseTuple(args, "i", &howMany))
return(NULL);
if (! PyArg_ParseTuple(args, "i", &howMany))
return NULL;
if (hCryptProv == 0) {
HINSTANCE hAdvAPI32 = NULL;
......@@ -7252,32 +7253,38 @@ static PyObject* win32_urandom(PyObject *self, PyObject *args)
/* Obtain handle to the DLL containing CryptoAPI
This should not fail */
if( (hAdvAPI32 = GetModuleHandle("advapi32.dll")) == NULL)
hAdvAPI32 = GetModuleHandle("advapi32.dll");
if(hAdvAPI32 == NULL)
return win32_error("GetModuleHandle", NULL);
/* Obtain pointers to the CryptoAPI functions
This will fail on some early versions of Win95 */
pCryptAcquireContext=(CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32,\
pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
hAdvAPI32,
"CryptAcquireContextA");
pCryptGenRandom=(CRYPTGENRANDOM)GetProcAddress(hAdvAPI32,\
"CryptGenRandom");
if (pCryptAcquireContext == NULL || pCryptGenRandom == NULL)
return PyErr_Format(PyExc_NotImplementedError,\
if (pCryptAcquireContext == NULL)
return PyErr_Format(PyExc_NotImplementedError,
"CryptAcquireContextA not found");
pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
hAdvAPI32, "CryptGenRandom");
if (pCryptAcquireContext == NULL)
return PyErr_Format(PyExc_NotImplementedError,
"CryptGenRandom not found");
/* Acquire context */
if(!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return win32_error("CryptAcquireContext", NULL);
}
/* Allocate bytes */
if ((bytes = (unsigned char*)PyMem_Malloc(howMany)) == NULL)
bytes = (unsigned char*)PyMem_Malloc(howMany);
if (bytes == NULL)
return PyErr_NoMemory();
/* Get random data */
if (!pCryptGenRandom(hCryptProv, howMany, bytes)) {
if (! pCryptGenRandom(hCryptProv, howMany, bytes)) {
PyMem_Free(bytes);
return win32_error("CryptGenRandom", NULL);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment