Here it is
#include <windows.h>
#include <stdio.h>
// fEqual returns TRUE if pStr1 is equal to pStr2 over a distance of
// dwLength bytes, otherwise FALSE
// Input parameter : fUpperIsLower --> if TRUE "ABC" == "abc"
// if FALSE "ABC" != "abc"
BOOL fEqual ( char *pStr1,
char *pStr2,
DWORD dwLength,
BOOL fUpperIsLower )
{
if ( !dwLength ) return TRUE; // Length 0 --> TRUE
if ( pStr1 == pStr2 ) return TRUE; // Same pointer --> TRUE
if ( pStr1 == NULL || pStr2 == NULL ) return FALSE;
if ( fUpperIsLower )
{ for ( DWORD i1 = 0; i1 < dwLength; i1++ )
{ if ( toupper ( *(pStr1 + i1 )) != toupper ( *(pStr2 + i1 )) )
{ return FALSE; } }
} else
{ if ( memcmp ( pStr1, pStr2, dwLength ) != 0 )
{ return FALSE; } }
return TRUE; }
// FindStringInFile searches for the presence of a string in a file.
// Return values 0xffffffff means the string is not found or an error
// occurred.
// Otherwise the return value is the offset of the first occurrence of
// the string in the file.
// Input parameter : fUpperIsLower --> if TRUE "ABC" == "abc"
// if FALSE "ABC" != "abc"
DWORD FindStringInFile ( char *pFileName,
char *pSearchString,
DWORD cbSearchString,
BOOL fUpperIsLower )
{
if ( pFileName == NULL ) return 0xffffffff;
if ( pSearchString == NULL ) return 0xffffffff;
if ( !cbSearchString ) return 0xffffffff;
DWORD dwReturnValue = 0xffffffff;
char *Buffer = NULL;
HANDLE fh = INVALID_HANDLE_VALUE;
__try
{
// Open file for reading it
fh = CreateFile ( pFileName, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL );
// Is the file open?
if ( fh == INVALID_HANDLE_VALUE ) __leave;
// Get the file size
DWORD dwFileSize = GetFileSize ( fh, NULL );
// Did GetGileSize succeed?
if ( dwFileSize == 0xffffffff ) __leave;
// Is the filesize less than the size of the searchstring?
if ( dwFileSize < cbSearchString ) __leave;
// Allocate memory to read the entire file at once
Buffer = (char *)( malloc ( dwFileSize ));
// Memory allocation succesful?
if ( Buffer == NULL ) __leave;
// Read file in buffer
DWORD dwBytesRead;
if ( !ReadFile ( fh, Buffer, dwFileSize, &dwBytesRead, NULL )) __leave;
// Close file
CloseHandle ( fh );
fh = INVALID_HANDLE_VALUE;
// Is the searchstring greter than the number of bytes read ?
if ( cbSearchString > dwBytesRead ) __leave;
// calculate the last offset to be compared
DWORD dwOffsetLast = dwBytesRead + 1 - cbSearchString;
// Search for the string
for ( DWORD dwOffset = 0; dwOffset < dwOffsetLast; dwOffset++ )
{ if ( fEqual ( Buffer + dwOffset, pSearchString,
cbSearchString, fUpperIsLower ))
{ dwReturnValue = dwOffset;
__leave; } }
}
__finally
{ if ( Buffer != NULL )
{ free ( Buffer );
Buffer = NULL; }
if ( fh != INVALID_HANDLE_VALUE )
{ CloseHandle ( fh );
fh = INVALID_HANDLE_VALUE; }
return dwReturnValue; }
// the next line should never be executed but is there to prevent a
// compiler warning "Not all control paths return a value"
return dwReturnValue; }
void main ( )
{
char FileName[MAX_PATH];
char UpIsLow[10];
char SearchString[MAX_PATH];
printf ( "File name : " ); scanf ( "%s", &FileName );
printf ( "Search for : " ); scanf ( "%s", &SearchString );
printf ( "A == a (Y/N) : " ); scanf ( "%s", &UpIsLow );
BOOL fUpIsLow = toupper ( UpIsLow[0] ) == 'Y' ? TRUE : FALSE;
DWORD dwOffset = FindStringInFile ( FileName, SearchString,
strlen ( SearchString ),
fUpIsLow );
if ( dwOffset == 0xffffffff )
{ printf ( "String %s not found in file %s\n",
SearchString, FileName ); } else
{ printf ( "String %s found at offset %d in file %s\n",
SearchString, dwOffset, FileName ); } }
Marcel