⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fastio.cpp

📁 一个过滤层文件系统驱动的完整代码,实现了文件的加密,操作截获等
💻 CPP
📖 第 1 页 / 共 5 页
字号:
FsTPMFastIoLock (
				   IN PFILE_OBJECT FileObject,
				   IN PLARGE_INTEGER FileOffset,
				   IN PLARGE_INTEGER Length,
				   PEPROCESS ProcessId,
				   ULONG Key,
				   BOOLEAN FailImmediately,
				   BOOLEAN ExclusiveLock,
				   OUT PIO_STATUS_BLOCK IoStatus,
				   IN PDEVICE_OBJECT DeviceObject
				   )

				   /*++

				   Routine Description:

				   This routine is the fast I/O "pass through" routine for locking a byte
				   range within a file.

				   This function simply invokes the file system's cooresponding routine, or
				   returns FALSE if the file system does not implement the function.

				   Arguments:

				   FileObject - Pointer to the file object to be locked.

				   FileOffset - Starting byte offset from the base of the file to be locked.

				   Length - Length of the byte range to be locked.

				   ProcessId - ID of the process requesting the file lock.

				   Key - Lock key to associate with the file lock.

				   FailImmediately - Indicates whether or not the lock request is to fail
				   if it cannot be immediately be granted.

				   ExclusiveLock - Indicates whether the lock to be taken is exclusive (TRUE)
				   or shared.

				   IoStatus - Pointer to a variable to receive the I/O status of the
				   operation.

				   DeviceObject - Pointer to this driver's device object, the device on
				   which the operation is to occur.

				   Return Value:

				   The function value is TRUE or FALSE based on whether or not fast I/O
				   is possible for this file.

				   --*/

{
	// 暂不予支持
	return FALSE;

	FsTPM_DbgPrint(("->FsTPMFastIoLock \n"));

	PDEVICE_OBJECT deviceObject;
	PFAST_IO_DISPATCH fastIoDispatch;

	PAGED_CODE();
	ASSERT(IS_MY_DEVICE_EXTENSION( DeviceObject->DeviceExtension ));

	//
	//  Pass through logic for this type of Fast I/O
	//

	deviceObject = ((PHOOK_EXTENSION) (DeviceObject->DeviceExtension))->Vcb.NextLowerDevice;
	if (deviceObject) {

		fastIoDispatch = deviceObject->DriverObject->FastIoDispatch;

		if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoLock )) {

			return (fastIoDispatch->FastIoLock)(
				FileObject,
				FileOffset,
				Length,
				ProcessId,
				Key,
				FailImmediately,
				ExclusiveLock,
				IoStatus,
				deviceObject );
		}
	}
	return FALSE;
}


BOOLEAN
FsTPMFastIoUnlockSingle (
						   IN PFILE_OBJECT FileObject,
						   IN PLARGE_INTEGER FileOffset,
						   IN PLARGE_INTEGER Length,
						   PEPROCESS ProcessId,
						   ULONG Key,
						   OUT PIO_STATUS_BLOCK IoStatus,
						   IN PDEVICE_OBJECT DeviceObject
						   )

						   /*++

						   Routine Description:

						   This routine is the fast I/O "pass through" routine for unlocking a byte
						   range within a file.

						   This function simply invokes the file system's cooresponding routine, or
						   returns FALSE if the file system does not implement the function.

						   Arguments:

						   FileObject - Pointer to the file object to be unlocked.

						   FileOffset - Starting byte offset from the base of the file to be
						   unlocked.

						   Length - Length of the byte range to be unlocked.

						   ProcessId - ID of the process requesting the unlock operation.

						   Key - Lock key associated with the file lock.

						   IoStatus - Pointer to a variable to receive the I/O status of the
						   operation.

						   DeviceObject - Pointer to this driver's device object, the device on
						   which the operation is to occur.

						   Return Value:

						   The function value is TRUE or FALSE based on whether or not fast I/O
						   is possible for this file.

						   --*/

{
	// 暂不予支持
	return FALSE;

	FsTPM_DbgPrint(("->FsTPMFastIoUnlockSingle \n"));

	PDEVICE_OBJECT deviceObject;
	PFAST_IO_DISPATCH fastIoDispatch;

	PAGED_CODE();
	ASSERT(IS_MY_DEVICE_EXTENSION( DeviceObject->DeviceExtension ));

	//
	//  Pass through logic for this type of Fast I/O
	//

	deviceObject = ((PHOOK_EXTENSION) (DeviceObject->DeviceExtension))->Vcb.NextLowerDevice;

	if (deviceObject) {

		fastIoDispatch = deviceObject->DriverObject->FastIoDispatch;

		if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoUnlockSingle )) {

			return (fastIoDispatch->FastIoUnlockSingle)(
				FileObject,
				FileOffset,
				Length,
				ProcessId,
				Key,
				IoStatus,
				deviceObject );
		}
	}
	return FALSE;
}


BOOLEAN
FsTPMFastIoUnlockAll (
						IN PFILE_OBJECT FileObject,
						PEPROCESS ProcessId,
						OUT PIO_STATUS_BLOCK IoStatus,
						IN PDEVICE_OBJECT DeviceObject
						)

						/*++

						Routine Description:

						This routine is the fast I/O "pass through" routine for unlocking all
						locks within a file.

						This function simply invokes the file system's cooresponding routine, or
						returns FALSE if the file system does not implement the function.

						Arguments:

						FileObject - Pointer to the file object to be unlocked.

						ProcessId - ID of the process requesting the unlock operation.

						IoStatus - Pointer to a variable to receive the I/O status of the
						operation.

						DeviceObject - Pointer to this driver's device object, the device on
						which the operation is to occur.

						Return Value:

						The function value is TRUE or FALSE based on whether or not fast I/O
						is possible for this file.

						--*/

{
	// 暂不予支持
	return FALSE;

	FsTPM_DbgPrint(("->FsTPMFastIoUnlockAll \n"));


	PDEVICE_OBJECT deviceObject;
	PFAST_IO_DISPATCH fastIoDispatch;

	PAGED_CODE();
	ASSERT(IS_MY_DEVICE_EXTENSION( DeviceObject->DeviceExtension ));

	//
	//  Pass through logic for this type of Fast I/O
	//

	deviceObject = ((PHOOK_EXTENSION) (DeviceObject->DeviceExtension))->Vcb.NextLowerDevice;
	if (deviceObject) {

		fastIoDispatch = deviceObject->DriverObject->FastIoDispatch;

		if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoUnlockAll )) {

			return (fastIoDispatch->FastIoUnlockAll)(
				FileObject,
				ProcessId,
				IoStatus,
				deviceObject );
		}
	}
	return FALSE;
}


BOOLEAN
FsTPMFastIoUnlockAllByKey (
							 IN PFILE_OBJECT FileObject,
							 PVOID ProcessId,
							 ULONG Key,
							 OUT PIO_STATUS_BLOCK IoStatus,
							 IN PDEVICE_OBJECT DeviceObject
							 )

							 /*++

							 Routine Description:

							 This routine is the fast I/O "pass through" routine for unlocking all
							 locks within a file based on a specified key.

							 This function simply invokes the file system's cooresponding routine, or
							 returns FALSE if the file system does not implement the function.

							 Arguments:

							 FileObject - Pointer to the file object to be unlocked.

							 ProcessId - ID of the process requesting the unlock operation.

							 Key - Lock key associated with the locks on the file to be released.

							 IoStatus - Pointer to a variable to receive the I/O status of the
							 operation.

							 DeviceObject - Pointer to this driver's device object, the device on
							 which the operation is to occur.

							 Return Value:

							 The function value is TRUE or FALSE based on whether or not fast I/O
							 is possible for this file.

							 --*/

{
	// 暂不予支持
	return FALSE;

	FsTPM_DbgPrint(("->FsTPMFastIoUnlockAllByKey \n"));

	PDEVICE_OBJECT deviceObject;
	PFAST_IO_DISPATCH fastIoDispatch;

	PAGED_CODE();
	ASSERT(IS_MY_DEVICE_EXTENSION( DeviceObject->DeviceExtension ));

	//
	//  Pass through logic for this type of Fast I/O
	//

	deviceObject = ((PHOOK_EXTENSION) (DeviceObject->DeviceExtension))->Vcb.NextLowerDevice;

	if (deviceObject) {

		fastIoDispatch = deviceObject->DriverObject->FastIoDispatch;

		if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoUnlockAllByKey )) {

			return (fastIoDispatch->FastIoUnlockAllByKey)(
				FileObject,
				ProcessId,
				Key,
				IoStatus,
				deviceObject );
		}
	}
	return FALSE;
}


BOOLEAN
FsTPMFastIoDeviceControl (
							IN PFILE_OBJECT FileObject,
							IN BOOLEAN Wait,
							IN PVOID InputBuffer OPTIONAL,
							IN ULONG InputBufferLength,
							OUT PVOID OutputBuffer OPTIONAL,
							IN ULONG OutputBufferLength,
							IN ULONG IoControlCode,
							OUT PIO_STATUS_BLOCK IoStatus,
							IN PDEVICE_OBJECT DeviceObject
							)

							/*++

							Routine Description:

							This routine is the fast I/O "pass through" routine for device I/O control
							operations on a file.

							This function simply invokes the file system's cooresponding routine, or
							returns FALSE if the file system does not implement the function.

							Arguments:

							FileObject - Pointer to the file object representing the device to be
							serviced.

							Wait - Indicates whether or not the caller is willing to wait if the
							appropriate locks, etc. cannot be acquired

							InputBuffer - Optional pointer to a buffer to be passed into the driver.

							InputBufferLength - Length of the optional InputBuffer, if one was
							specified.

							OutputBuffer - Optional pointer to a buffer to receive data from the
							driver.

							OutputBufferLength - Length of the optional OutputBuffer, if one was
							specified.

							IoControlCode - I/O control code indicating the operation to be performed
							on the device.

							IoStatus - Pointer to a variable to receive the I/O status of the
							operation.

							DeviceObject - Pointer to this driver's device object, the device on
							which the operation is to occur.

							Return Value:

							The function value is TRUE or FALSE based on whether or not fast I/O
							is possible for this file.

							--*/
{

	FsTPM_DbgPrint(("->FsTPMFastIoDeviceControl \n"));


	PDEVICE_OBJECT deviceObject;
	PFAST_IO_DISPATCH fastIoDispatch;

	PAGED_CODE();

	if (((PHOOK_EXTENSION)DeviceObject->DeviceExtension)->Type==GUIINTERFACE)
	{
		IoStatus->Information = 0;
		IoStatus->Status = STATUS_SUCCESS;

		switch(IoControlCode) 
		{
		case IOCTL_FSTPM_ADD_PROTECT_FILE:
			if (InputBuffer!=NULL && InputBufferLength==sizeof(FILE_PROTECT_LIST_ITEM))
			{
				DbgPrint("===>Add Protect File ****************\n");
				PFILE_PROTECT_LIST_ITEM pItem = (PFILE_PROTECT_LIST_ITEM)ExAllocatePoolWithTag(NonPagedPool, sizeof(FILE_PROTECT_LIST_ITEM), TAGS);
	
				pItem->ProtectedFileName[MAXPATHLEN-1] = 0; // 确保字符串正确 

				RtlCopyMemory( pItem, InputBuffer, InputBufferLength);
				UpperWordW(pItem->ProtectedFileName);
				
				DbgPrint("===>FileName is: %S\n", pItem->ProtectedFileName);

				ListInsert(&ProtectControlBlock.FileProtectList,pItem);

				IoStatus->Information = sizeof(FILE_PROTECT_LIST_ITEM);
			}
			else
			{
				IoStatus->Information = 0;
				IoStatus->Status = STATUS_INVALID_PARAMETER;
			}
			break;
		case IOCTL_FSTPM_DELETE_PROTECT_FILE:
			if (InputBuffer!=NULL && InputBufferLength==sizeof(FILE_PROTECT_LIST_ITEM))
			{
				PFILE_PROTECT_LIST_ITEM pItem = (PFILE_PROTECT_LIST_ITEM)InputBuffer;
				pItem->ProtectedFileName[MAXPATHLEN-1] = 0;

				ListDelete(&ProtectControlBlock.FileProtectList, pItem->ProtectedFileName);
				IoStatus->Information = sizeof(FILE_PROTECT_LIST_ITEM);
			}
			else
			{
				IoStatus->Information = 0;
				IoStatus->Status = STATUS_INVALID_PARAMETER;
			}
			break;
		case IOCTL_FSTPM_SET_CHECK_PROTECT_STATUS:
			if (InputBufferLength == 1 && InputBuffer!=NULL)
			{
				ProtectControlBlock.EnableCheckProtect = (unsigned char)InputBuffer ? TRUE:FALSE;
				IoStatus->Information = 1;
			}
			else
			{
				IoStatus->Information = 0;
				IoStatus->Status = STATUS_INVALID_PARAMETER;
			}
			break;
		case IOCTL_FSTPM_SET_STATIC_PROTECT_STATUS:
			if (InputBufferLength == 1 && InputBuffer!=NULL)
			{
				ProtectControlBlock.EnableStaticProtect = (unsigned char)InputBuffer ? TRUE:FALSE;
				IoStatus->Information = 1;
			}
			else
			{
				IoStatus->Information = 0;
				IoStatus->Status = STATUS_INVALID_PARAMETER;
			}
			break;
		case IOCTL_FSTPM_SET_ENCRYPT_PROTECT:    
			if (InputBufferLength == 1 && InputBuffer!=NULL)
			{
				ProtectControlBlock.EnableEncryptProtect = (unsigned char)InputBuffer ? TRUE:FALSE;
				IoStatus->Information = 1;
			}
			else
			{
				IoStatus->Information = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -