All pastes #487282 Raw Edit

Unnamed

public cpp v1 · immutable
·published 2007-05-14 11:15 UTC
rendered paste body
#include "CGuidGen.h"int CGuidGen::ReadPhysicalDriveInNTWithAdminRights(){	int done = FALSE;	int drive = 0;	for(drive=0; drive<MAX_IDE_DRIVES; drive++)	{		if(done)			return done;		HANDLE hPhysicalDriveIOCTL = 0;		// Try to get a handle to PhysicalDrive IOCTL, report failure and exit if can't		char driveName[256];		sprintf_s(driveName, 256, "\\\\.\\PhysicalDrive%d", drive);		// Windows NT, Windows 2000, must have admin rights		hPhysicalDriveIOCTL = CreateFile (driveName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);		if(hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)		{			GETVERSIONOUTPARAMS VersionParams;			DWORD cbBytesReturned = 0;			// Get the version, etc of PhysicalDrive IOCTL			memset((void*)&VersionParams, 0, sizeof(VersionParams));			if(!DeviceIoControl(hPhysicalDriveIOCTL, DFP_GET_VERSION, NULL, 0, &VersionParams, sizeof(VersionParams), &cbBytesReturned, NULL) )			{         				// printf ("DFP_GET_VERSION failed for drive %d\n", i);				// continue;			}			// If there is a IDE device at number "i" issue commands			// to the device			if(VersionParams.bIDEDeviceMap > 0)			{				BYTE bIDCmd = 0;   // IDE or ATAPI IDENTIFY cmd				SENDCMDINPARAMS scip;				// Now, get the ID sector for all IDE devices in the system.				// If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,				// otherwise use the IDE_ATA_IDENTIFY command				bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;				memset(&scip, 0, sizeof(scip));				memset(IdOutCmd, 0, sizeof(IdOutCmd));				if(DoIDENTIFY(hPhysicalDriveIOCTL, &scip, (PSENDCMDOUTPARAMS)&IdOutCmd, (BYTE)bIDCmd, (BYTE)drive, &cbBytesReturned))				{					DWORD diskdata [256];					int ijk = 0;					USHORT *pIdSector = (USHORT *)((PSENDCMDOUTPARAMS)IdOutCmd)->bBuffer;					for(ijk = 0; ijk<256; ijk++)						diskdata[ijk] = pIdSector[ijk];					GetHardDriveSerial(diskdata);					done = TRUE;				}			}			CloseHandle(hPhysicalDriveIOCTL);		}	}   return done;}// DoIDENTIFY: Send an IDENTIFY command to the drive// bDriveNum = 0-3, bIDCmd = IDE_ATA_IDENTIFY or IDE_ATAPI_IDENTIFYBOOL CGuidGen::DoIDENTIFY(HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP, PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum, PDWORD lpcbBytesReturned){	// Set up data structures for IDENTIFY command.	pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;	pSCIP->irDriveRegs.bFeaturesReg = 0;	pSCIP->irDriveRegs.bSectorCountReg = 1;	//pSCIP -> irDriveRegs.bSectorNumberReg = 1;	pSCIP->irDriveRegs.bCylLowReg = 0;	pSCIP->irDriveRegs.bCylHighReg = 0;	// Compute the drive number.	pSCIP->irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);	// The command can either be IDE identify or ATAPI identify.	pSCIP->irDriveRegs.bCommandReg = bIDCmd;	pSCIP->bDriveNumber = bDriveNum;	pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;   return (DeviceIoControl(hPhysicalDriveIOCTL, DFP_RECEIVE_DRIVE_DATA,               (LPVOID)pSCIP,               sizeof(SENDCMDINPARAMS) - 1,               (LPVOID)pSCOP,               sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,               lpcbBytesReturned, NULL));}int CGuidGen::ReadIdeDriveAsScsiDriveInNT(){   int done = FALSE;   int controller = 0;   for(controller=0; controller<16; controller++)   {		HANDLE hScsiDriveIOCTL = 0;		char driveName [256];		// Try to get a handle to PhysicalDrive IOCTL, report failure and exit if can't.		sprintf_s(driveName, 256, "\\\\.\\Scsi%d:", controller);		// Windows NT, Windows 2000, any rights should do		hScsiDriveIOCTL = CreateFile (driveName,						   GENERIC_READ | GENERIC_WRITE, 						   FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,						   OPEN_EXISTING, 0, NULL);		if(hScsiDriveIOCTL != INVALID_HANDLE_VALUE)		{			if(done)				return done;			int drive = 0;			for(drive = 0; drive<2; drive++)			{				char buffer [sizeof (SRB_IO_CONTROL) + SENDIDLENGTH];				SRB_IO_CONTROL *p = (SRB_IO_CONTROL *) buffer;				SENDCMDINPARAMS *pin = (SENDCMDINPARAMS *)(buffer + sizeof(SRB_IO_CONTROL));				DWORD dummy;				memset(buffer, 0, sizeof (buffer));				p->HeaderLength = sizeof (SRB_IO_CONTROL);				p->Timeout = 10000;				p->Length = SENDIDLENGTH;				p->ControlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;				strncpy_s((char*)p->Signature, 8, "SCSIDISK", 8);				pin->irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;				pin->bDriveNumber = drive;				if(DeviceIoControl (hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT, 									 buffer,									 sizeof (SRB_IO_CONTROL) + sizeof (SENDCMDINPARAMS) - 1,									 buffer,									 sizeof (SRB_IO_CONTROL) + SENDIDLENGTH,									 &dummy, NULL))				{					SENDCMDOUTPARAMS *pOut = (SENDCMDOUTPARAMS *) (buffer + sizeof (SRB_IO_CONTROL));					IDSECTOR *pId = (IDSECTOR *)(pOut->bBuffer);					if(pId->sModelNumber[0])					{						DWORD diskdata [256];						int ijk = 0;						USHORT *pIdSector = (USHORT *)pId;          						for(ijk = 0; ijk < 256; ijk++)							diskdata[ijk] = pIdSector[ijk];						GetHardDriveSerial(diskdata);						done = TRUE;					}				}			}			CloseHandle(hScsiDriveIOCTL);		}	}	return done;}BOOL CGuidGen::GetHardDriveSerial(DWORD diskdata[256]){   char string[1024];	//  copy the hard drive serial number to the buffer	strcpy_s(string, 1024, ConvertToString(diskdata, 10, 19));	if(isalnum(string[0]) || isalnum(string[19])) 	{		char *s = string;		while(isspace(*s)) ++s;		strcpy_s(HDDSerial, 128, s);		return TRUE;	}	return FALSE;}char *CGuidGen::ConvertToString(DWORD diskdata[256], int firstIndex, int lastIndex){	static char string [1024];	int index = 0;	int position = 0;	// each integer has two characters stored in it backwards	for (index = firstIndex; index <= lastIndex; index++)	{		// get high byte for 1st character		string [position] = (char)(diskdata [index] / 256);		position++;		// get low byte for 2nd character		string [position] = (char)(diskdata [index] % 256);		position++;   }	// end the string 	string [position] = '\0';	// cut off the trailing blanks	for(index = position-1; index>0 && ' ' == string[index]; index--)		string[index] = '\0';   return string;}int CGuidGen::GetHDDInfo(){   OSVERSIONINFO version;	memset(&version, 0, sizeof(version));	version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);	GetVersionEx(&version);	if(version.dwPlatformId != VER_PLATFORM_WIN32_NT)		return ERROR_PLATFORM;	if(ReadPhysicalDriveInNTWithAdminRights() || ReadIdeDriveAsScsiDriveInNT())	{		// generate an md5		MD5_CTX ctx;		MD5Init(&ctx);		MD5Update(&ctx, (BYTE*)HDDSerial, (UINT)strlen(HDDSerial));		MD5Final(&ctx);		// copy hex to md5 buf		memcpy(HDDSerialMD5, ctx.digest, 16);		// copy hex to a string		for(int i=0; i<16; i++)			sprintf_s(&HDDSerialMD5str[i*2], 32, "%02X", ctx.digest[i]);			return ERROR_SUCCESS;	}	return ERROR_IO;}