21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

generator. The code presented here uses the spc_rand_range( ) function from Recipe<br />

11.11. Refer to Chapter 11 for possible implementations of random number generators.<br />

The function SpcMakeTempFile( ) repeatedly generates a random temporary filename<br />

using a cryptographically strong pseudo-random number generator and attempts to<br />

create the file. The generated filename contains an absolute path specification to the<br />

user’s temporary files directory. If successful, the file is created, inheriting access permissions<br />

from that directory, which ordinarily will prevent users other than the<br />

Administrator and the owner from gaining access to it. If SpcMakeTempFile( ) is<br />

unable to create the file, the process begins anew. SpcMakeTempFile( ) will not return<br />

until a file can be successfully created or some kind of fatal error occurs.<br />

As arguments, SpcMakeTempFile( ) requires a preallocated writable buffer and the size<br />

of that buffer in characters. The buffer will contain the filename used to successfully<br />

create the temporary file, and the return value from the function will be a handle to<br />

the open file. If an error occurs, the return value will be INVALID_HANDLE_VALUE, and<br />

GetLastError( ) can be used to obtain more detailed error information.<br />

#include <br />

static LPTSTR lpszFilenameCharacters = TEXT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");<br />

static BOOL MakeTempFilename(LPTSTR lpszBuffer, DWORD dwBuffer) {<br />

int i;<br />

DWORD dwCharacterRange, dwTempPathLength;<br />

TCHAR cCharacter;<br />

dwTempPathLength = GetTempPath(dwBuffer, lpszBuffer);<br />

if (!dwTempPathLength) return FALSE;<br />

if (++dwTempPathLength > dwBuffer || dwBuffer - dwTempPathLength < 12) {<br />

SetLastError(ERROR_INSUFFICIENT_BUFFER);<br />

return FALSE;<br />

}<br />

dwCharacterRange = lstrlen(lpszFilenameCharacters) - 1;<br />

for (i = 0; i < 8; i++) {<br />

cCharacter = lpszFilenameCharacters[spc_rand_range(0, dwCharacterRange)];<br />

lpszBuffer[dwTempPathLength++ - 1] = cCharacter;<br />

}<br />

lpszBuffer[dwTempPathLength++ - 1] = '.';<br />

lpszBuffer[dwTempPathLength++ - 1] = 'T';<br />

lpszBuffer[dwTempPathLength++ - 1] = 'M';<br />

lpszBuffer[dwTempPathLength++ - 1] = 'P';<br />

lpszBuffer[dwTempPathLength++ - 1] = 0;<br />

return TRUE;<br />

}<br />

HANDLE SpcMakeTempFile(LPTSTR lpszBuffer, DWORD dwBuffer) {<br />

HANDLE hFile;<br />

do {<br />

Creating Files for Temporary Use | 67<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!