Kaydet (Commit) 4ad8217a authored tarafından 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).
üst 38330fe5
...@@ -7236,15 +7236,16 @@ typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ ...@@ -7236,15 +7236,16 @@ typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
static CRYPTGENRANDOM pCryptGenRandom = NULL; static CRYPTGENRANDOM pCryptGenRandom = NULL;
static HCRYPTPROV hCryptProv = 0; static HCRYPTPROV hCryptProv = 0;
static PyObject* win32_urandom(PyObject *self, PyObject *args) static PyObject*
win32_urandom(PyObject *self, PyObject *args)
{ {
int howMany = 0; int howMany = 0;
unsigned char* bytes = NULL; unsigned char* bytes = NULL;
PyObject* returnVal = NULL; PyObject* returnVal = NULL;
/* Read arguments */ /* Read arguments */
if (!PyArg_ParseTuple(args, "i", &howMany)) if (! PyArg_ParseTuple(args, "i", &howMany))
return(NULL); return NULL;
if (hCryptProv == 0) { if (hCryptProv == 0) {
HINSTANCE hAdvAPI32 = NULL; HINSTANCE hAdvAPI32 = NULL;
...@@ -7252,32 +7253,38 @@ static PyObject* win32_urandom(PyObject *self, PyObject *args) ...@@ -7252,32 +7253,38 @@ static PyObject* win32_urandom(PyObject *self, PyObject *args)
/* Obtain handle to the DLL containing CryptoAPI /* Obtain handle to the DLL containing CryptoAPI
This should not fail */ This should not fail */
if( (hAdvAPI32 = GetModuleHandle("advapi32.dll")) == NULL) hAdvAPI32 = GetModuleHandle("advapi32.dll");
if(hAdvAPI32 == NULL)
return win32_error("GetModuleHandle", NULL); return win32_error("GetModuleHandle", NULL);
/* Obtain pointers to the CryptoAPI functions /* Obtain pointers to the CryptoAPI functions
This will fail on some early versions of Win95 */ This will fail on some early versions of Win95 */
pCryptAcquireContext=(CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32,\ pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
hAdvAPI32,
"CryptAcquireContextA"); "CryptAcquireContextA");
pCryptGenRandom=(CRYPTGENRANDOM)GetProcAddress(hAdvAPI32,\ if (pCryptAcquireContext == NULL)
"CryptGenRandom"); return PyErr_Format(PyExc_NotImplementedError,
"CryptAcquireContextA not found");
if (pCryptAcquireContext == NULL || pCryptGenRandom == NULL)
return PyErr_Format(PyExc_NotImplementedError,\ pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
hAdvAPI32, "CryptGenRandom");
if (pCryptAcquireContext == NULL)
return PyErr_Format(PyExc_NotImplementedError,
"CryptGenRandom not found"); "CryptGenRandom not found");
/* Acquire context */ /* Acquire context */
if(!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
CRYPT_VERIFYCONTEXT)) PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return win32_error("CryptAcquireContext", NULL); return win32_error("CryptAcquireContext", NULL);
} }
/* Allocate bytes */ /* Allocate bytes */
if ((bytes = (unsigned char*)PyMem_Malloc(howMany)) == NULL) bytes = (unsigned char*)PyMem_Malloc(howMany);
if (bytes == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
/* Get random data */ /* Get random data */
if (!pCryptGenRandom(hCryptProv, howMany, bytes)) { if (! pCryptGenRandom(hCryptProv, howMany, bytes)) {
PyMem_Free(bytes); PyMem_Free(bytes);
return win32_error("CryptGenRandom", NULL); 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