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
...@@ -903,7 +903,7 @@ _pystat_fromstructstat(STRUCT_STAT st) ...@@ -903,7 +903,7 @@ _pystat_fromstructstat(STRUCT_STAT st)
#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
static BOOL static BOOL
IsUNCRootA(char *path, int pathlen) IsUNCRootA(char *path, int pathlen)
{ {
#define ISSLASH ISSLASHA #define ISSLASH ISSLASHA
...@@ -927,7 +927,7 @@ IsUNCRootA(char *path, int pathlen) ...@@ -927,7 +927,7 @@ IsUNCRootA(char *path, int pathlen)
} }
#ifdef Py_WIN_WIDE_FILENAMES #ifdef Py_WIN_WIDE_FILENAMES
static BOOL static BOOL
IsUNCRootW(Py_UNICODE *path, int pathlen) IsUNCRootW(Py_UNICODE *path, int pathlen)
{ {
#define ISSLASH ISSLASHW #define ISSLASH ISSLASHW
...@@ -994,7 +994,7 @@ posix_do_stat(PyObject *self, PyObject *args, ...@@ -994,7 +994,7 @@ posix_do_stat(PyObject *self, PyObject *args,
if (pathlen > 0) { if (pathlen > 0) {
if (ISSLASHW(wpath[pathlen-1])) { if (ISSLASHW(wpath[pathlen-1])) {
/* It does end with a slash -- exempt the root drive cases. */ /* It does end with a slash -- exempt the root drive cases. */
if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') || if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':') ||
IsUNCRootW(wpath, pathlen)) IsUNCRootW(wpath, pathlen))
/* leave it alone */; /* leave it alone */;
else { else {
...@@ -1002,7 +1002,7 @@ posix_do_stat(PyObject *self, PyObject *args, ...@@ -1002,7 +1002,7 @@ posix_do_stat(PyObject *self, PyObject *args,
wpath[pathlen-1] = L'\0'; wpath[pathlen-1] = L'\0';
} }
} }
else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 && else if (ISSLASHW(wpath[1]) && pathlen < ARRAYSIZE(wpath)-1 &&
IsUNCRootW(wpath, pathlen)) { IsUNCRootW(wpath, pathlen)) {
/* UNC root w/o trailing slash: add one when there's room */ /* UNC root w/o trailing slash: add one when there's room */
wpath[pathlen++] = L'\\'; wpath[pathlen++] = L'\\';
...@@ -1044,7 +1044,7 @@ posix_do_stat(PyObject *self, PyObject *args, ...@@ -1044,7 +1044,7 @@ posix_do_stat(PyObject *self, PyObject *args,
if (pathlen > 0) { if (pathlen > 0) {
if (ISSLASHA(path[pathlen-1])) { if (ISSLASHA(path[pathlen-1])) {
/* It does end with a slash -- exempt the root drive cases. */ /* It does end with a slash -- exempt the root drive cases. */
if (pathlen == 1 || (pathlen == 3 && path[1] == ':') || if (pathlen == 1 || (pathlen == 3 && path[1] == ':') ||
IsUNCRootA(path, pathlen)) IsUNCRootA(path, pathlen))
/* leave it alone */; /* leave it alone */;
else { else {
...@@ -1054,7 +1054,7 @@ posix_do_stat(PyObject *self, PyObject *args, ...@@ -1054,7 +1054,7 @@ posix_do_stat(PyObject *self, PyObject *args,
path = pathcopy; path = pathcopy;
} }
} }
else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 && else if (ISSLASHA(path[1]) && pathlen < ARRAYSIZE(pathcopy)-1 &&
IsUNCRootA(path, pathlen)) { IsUNCRootA(path, pathlen)) {
/* UNC root w/o trailing slash: add one when there's room */ /* UNC root w/o trailing slash: add one when there's room */
strncpy(pathcopy, path, pathlen); strncpy(pathcopy, path, pathlen);
...@@ -2079,8 +2079,8 @@ posix_utime(PyObject *self, PyObject *args) ...@@ -2079,8 +2079,8 @@ posix_utime(PyObject *self, PyObject *args)
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
#ifdef Py_WIN_WIDE_FILENAMES #ifdef Py_WIN_WIDE_FILENAMES
if (have_unicode_filename) if (have_unicode_filename)
/* utime is OK with utimbuf, but _wutime insists /* utime is OK with utimbuf, but _wutime insists
on _utimbuf (the msvc headers assert the on _utimbuf (the msvc headers assert the
underscore version is ansi) */ underscore version is ansi) */
res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
else else
...@@ -7236,57 +7236,64 @@ typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ ...@@ -7236,57 +7236,64 @@ 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 */
if (!PyArg_ParseTuple(args, "i", &howMany))
return(NULL);
if (hCryptProv == 0) {
HINSTANCE hAdvAPI32 = NULL;
CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
/* Obtain handle to the DLL containing CryptoAPI
This should not fail */
if( (hAdvAPI32 = GetModuleHandle("advapi32.dll")) == 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,\
"CryptAcquireContextA");
pCryptGenRandom=(CRYPTGENRANDOM)GetProcAddress(hAdvAPI32,\
"CryptGenRandom");
if (pCryptAcquireContext == NULL || pCryptGenRandom == NULL) /* Read arguments */
return PyErr_Format(PyExc_NotImplementedError,\ if (! PyArg_ParseTuple(args, "i", &howMany))
"CryptGenRandom not found"); return NULL;
/* Acquire context */ if (hCryptProv == 0) {
if(!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, HINSTANCE hAdvAPI32 = NULL;
CRYPT_VERIFYCONTEXT)) CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
return win32_error("CryptAcquireContext", NULL);
} /* Obtain handle to the DLL containing CryptoAPI
This should not fail */
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,
"CryptAcquireContextA");
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))
return win32_error("CryptAcquireContext", NULL);
}
/* Allocate bytes */ /* Allocate bytes */
if ((bytes = (unsigned char*)PyMem_Malloc(howMany)) == NULL) bytes = (unsigned char*)PyMem_Malloc(howMany);
return PyErr_NoMemory(); if (bytes == NULL)
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);
} }
/* Build return value */ /* Build return value */
returnVal = PyString_FromStringAndSize(bytes, howMany); returnVal = PyString_FromStringAndSize(bytes, howMany);
PyMem_Free(bytes); PyMem_Free(bytes);
return returnVal; return returnVal;
} }
#endif #endif
......
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