freeotfe.c

来自「文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2」· C语言 代码 · 共 1,966 行 · 第 1/5 页

C
1,966
字号
            status = IOCTL_Std_DiskIsWritable(DeviceObject, Irp);
            break;
            }


        case IOCTL_DISK_GET_DRIVE_GEOMETRY:
        case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
            {
            status = IOCTL_Std_DiskGetDriveGeometry(DeviceObject, Irp);
            break;
            }

        // xxxop - implement IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
        // xxxop - implement IOCTL_CDROM_GET_DRIVE_GEOMETRY_EX

        case IOCTL_DISK_GET_DRIVE_LAYOUT:
            {
            status = IOCTL_Std_DiskGetDriveLayout(DeviceObject, Irp);
            break;
            }

#if (VER_PRODUCTBUILD >= 2600)
        // Windows XP and later only
        case IOCTL_DISK_GET_DRIVE_LAYOUT_EX:
            {
            status = IOCTL_Std_DiskGetDriveLayoutEx(DeviceObject, Irp);
            break;
            }
#endif

#if (VER_PRODUCTBUILD >= 2600)
        // Windows XP and later only
        case IOCTL_DISK_GET_LENGTH_INFO:
            {
            status = IOCTL_Std_DiskGetLengthInfo(DeviceObject, Irp);
            break;
            }
#endif
                        

        case IOCTL_DISK_GET_PARTITION_INFO:
            {
            status = IOCTL_Std_DiskGetPartitionInfo(DeviceObject, Irp);
            break;
            }
            
#if (VER_PRODUCTBUILD >= 2600)
        // Windows XP and later only
        case IOCTL_DISK_GET_PARTITION_INFO_EX:
            {
            status = IOCTL_Std_DiskGetPartitionInfoEx(DeviceObject, Irp);
            break;
            }
#endif
            
            
        case IOCTL_DISK_SET_PARTITION_INFO:
            {
            status = IOCTL_Std_DiskSetPartitionInfo(DeviceObject, Irp);
            break;
            }


#if (VER_PRODUCTBUILD >= 2600)
        // Windows XP and later only
        case IOCTL_DISK_SET_PARTITION_INFO_EX:
            {
            status = IOCTL_Std_DiskSetPartitionInfoEx(DeviceObject, Irp);
            break;
            }
#endif
            

        case IOCTL_DISK_VERIFY:
            {
            status = IOCTL_Std_DiskVerify(DeviceObject, Irp);
            break;
            }
            

        case IOCTL_DISK_CHECK_VERIFY:
        case IOCTL_CDROM_CHECK_VERIFY:
        case IOCTL_STORAGE_CHECK_VERIFY:
        case IOCTL_STORAGE_CHECK_VERIFY2:
            {                       
            status = IOCTL_Std_CheckVerify(DeviceObject, Irp);
            break;
            }
            

        // These two are only really valid for removable drives
        // They set/clear the media's avility to eject
        case IOCTL_DISK_MEDIA_REMOVAL:
        case IOCTL_STORAGE_MEDIA_REMOVAL:
            {
            status = IOCTL_Std_MediaRemoval(DeviceObject, Irp);
            break;
            }


        // These two are only really valid for removable drives
        // Equivilent of dismount
        case IOCTL_STORAGE_EJECT_MEDIA:
            {
            status = IOCTL_Std_StorageEjectMedia(
                                                 DeviceObject,
                                                 Irp,
                                                 &queueDeviceObject
                                                );
            break;
            }

        
        case IOCTL_CDROM_READ_TOC:
            {
            status = IOCTL_Std_CDROMReadTOC(DeviceObject, Irp);
            break;
            }

        // TODOzzz: Any more standard calls which can be implemented?


        default:
            {
            DEBUGOUTMAINDRV(DEBUGLEV_WARN, ("IoControl code is UNRECOGNISED (0x%x).\n",
                        irpSp->Parameters.DeviceIoControl.IoControlCode));
            status = STATUS_NOT_IMPLEMENTED;
            }
        }

        
        
    // If we want to queue the IRP, this should have been flagged by setting
    // "queueDeviceObject" to be the object which receives the queued IRP.
    // Otherwise we complete the request.
    if (queueDeviceObject != NULL)
        {
        DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Queuing IRP...\n"));
        status = QueueIRPToThread(queueDeviceObject, Irp);    

        // Check if the IRP was queued successfully...
        if (status == STATUS_PENDING)
            {
            // Note: IoCsqInsertIrp in QueueIRPToThread marks the IRP pending.
            DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Queued IRP.\n"));
            }
        else
            {
            Irp->IoStatus.Status = status;
            IoCompleteRequest(Irp, IO_NO_INCREMENT);
            DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("Failed to queue IRP.\n"));
            }

        }
    else
        {
        DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Completing IRP...\n"));
        // If there was a failure, return nothing; this will be set as appropriate if there
        // is no problem
        if (!(NT_SUCCESS(status)))
            {
            Irp->IoStatus.Information = 0;
            }                
        Irp->IoStatus.Status = status;
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
        DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("OK.\n"));
        }


    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("FreeOTFE_MF_DispatchDeviceControl\n"));
    
    return status;
}


// =========================================================================
// 
NTSTATUS
IOCTL_FreeOTFEIOCTL_Version(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
)
{
    NTSTATUS status;
    PIO_STACK_LOCATION irpSp;
    PDIOC_VERSION DIOCBuffer;
    
    DEBUGOUTMAINDRV(DEBUGLEV_ENTER, ("IOCTL_FreeOTFEIOCTL_Version\n"));

    irpSp = IoGetCurrentIrpStackLocation(Irp);

    // Check size of OUTPUT buffer
    if (irpSp->Parameters.DeviceIoControl.OutputBufferLength <
            sizeof(DIOC_VERSION))
        {
        DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("outBuffer size wrong size (expect: %d; got: %d)\n",
            sizeof(DIOC_VERSION),
            irpSp->Parameters.DeviceIoControl.OutputBufferLength
            ));
        status = STATUS_INVALID_BUFFER_SIZE;
        return status;            
        }
        
    // Request valid, process...
    DIOCBuffer = (PDIOC_VERSION)Irp->AssociatedIrp.SystemBuffer;

    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Reporting driver as version: 0x%08x (v%02d.%02d.%04d)\n",
                                     DRIVER_VERSION, 
                                     (DRIVER_VERSION & 0xFF000000) / 0x00FF0000,
                                     (DRIVER_VERSION & 0x00FF0000) / 0x0000FF00,
                                     (DRIVER_VERSION & 0x0000FFFF)
                                    ));
    DIOCBuffer->VersionID = DRIVER_VERSION;

    Irp->IoStatus.Information = sizeof(DIOC_VERSION);
    status = STATUS_SUCCESS;

    // Uncomment to execute test code
    // TestCode();

    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("IOCTL_FreeOTFEIOCTL_Version\n"));
    return status;
}


// =========================================================================
// 
NTSTATUS
IOCTL_FreeOTFEIOCTL_Create(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
)
{
    NTSTATUS status;
    PIO_STACK_LOCATION irpSp;
    PDIOC_DISK_DEVICE_CREATE DIOCBufferIn;
    PDIOC_DEVICE_NAME DIOCBufferOut;
    PDEVICE_OBJECT tgtDeviceObj;
    ANSI_STRING tmpANSIDeviceName;
    PDEVICE_EXTENSION tgtDevExt;
    ULONG createDeviceType;

    DEBUGOUTMAINDRV(DEBUGLEV_ENTER, ("IOCTL_FreeOTFEIOCTL_Create\n"));

    irpSp = IoGetCurrentIrpStackLocation(Irp);
    

    // Check size of INPUT buffer
    if (irpSp->Parameters.DeviceIoControl.InputBufferLength <
            sizeof(DIOC_DISK_DEVICE_CREATE))
        {
        DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("inBuffer size wrong size (expect: %d; got: %d)\n",
            sizeof(DIOC_DISK_DEVICE_CREATE),
            irpSp->Parameters.DeviceIoControl.InputBufferLength
            ));
        status = STATUS_INVALID_BUFFER_SIZE;
        return status;            
        }

    // Check size of OUTPUT buffer
    if (irpSp->Parameters.DeviceIoControl.OutputBufferLength <
            sizeof(DIOC_DEVICE_NAME))
        {
        DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("outBuffer size wrong size (expect: %d; got: %d)\n",
            sizeof(DIOC_DEVICE_NAME),
            irpSp->Parameters.DeviceIoControl.OutputBufferLength
            ));
        status = STATUS_INVALID_BUFFER_SIZE;
        return status;            
        }

    // Request valid, process...
    DIOCBufferIn = (PDIOC_DISK_DEVICE_CREATE)Irp->AssociatedIrp.SystemBuffer;
    DIOCBufferOut = (PDIOC_DEVICE_NAME)Irp->AssociatedIrp.SystemBuffer;

    // Store the input, as when we write to the output we'll overwrite it
    createDeviceType = DIOCBufferIn->DeviceType;


    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Creating disk device...\n"));
    status = CreateDiskDevice(
                              DeviceObject->DriverObject,
                              createDeviceType,
                              &tgtDeviceObj
                             );
    if (!(NT_SUCCESS(status)))
        {
        DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("Call to CreateDiskDevice FAILED.\n")); 
        return status;
        }
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Disk device created OK.\n"));
    
    
    // Set the output buffer to contain the devicename of the device just
    // created
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Copying device name to output buffer.\n"));
    tgtDevExt = (PDEVICE_EXTENSION)tgtDeviceObj->DeviceExtension;

    RtlUnicodeStringToAnsiString(
                                    &tmpANSIDeviceName,
                                    &tgtDevExt->zzDeviceName,
                                    TRUE
                                ); 
    // Copy the resulting ANSI string into the buffer
    RtlZeroMemory(
                    DIOCBufferOut->DeviceName,
                    sizeof(DIOCBufferOut->DeviceName)
                    );
    RtlCopyMemory(            
                    &DIOCBufferOut->DeviceName,
                    tmpANSIDeviceName.Buffer,
                    tmpANSIDeviceName.Length
                    );
    RtlFreeAnsiString(&tmpANSIDeviceName);

    Irp->IoStatus.Information = sizeof(DIOC_DEVICE_NAME);
    status = STATUS_SUCCESS;


    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("IOCTL_FreeOTFEIOCTL_Create\n"));
    return status;
}

    
// =========================================================================
// 
NTSTATUS
IOCTL_FreeOTFEIOCTL_Destroy(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
)
{
    NTSTATUS status;
    PIO_STACK_LOCATION irpSp;
    PDIOC_DEVICE_NAME DIOCBuffer;
    PDEVICE_OBJECT tgtDeviceObj;
    ANSI_STRING tmpANSIDeviceName;
    PDEVICE_EXTENSION tgtDevExt;
    
    DEBUGOUTMAINDRV(DEBUGLEV_ENTER, ("IOCTL_FreeOTFEIOCTL_Destroy\n"));

    irpSp = IoGetCurrentIrpStackLocation(Irp);
    

    // Check size of INPUT buffer
    if (irpSp->Parameters.DeviceIoControl.InputBufferLength <
            sizeof(DIOC_DEVICE_NAME))
        {
        DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("inBuffer size wrong size (expect: %d; got: %d)\n",
            sizeof(DIOC_DEVICE_NAME),
            irpSp->Parameters.DeviceIoControl.InputBufferLength
            ));
        status = STATUS_INVALID_BUFFER_SIZE;
        return status;            
        }

    // Request valid, process...
    DIOCBuffer = (PDIOC_DEVICE_NAME)Irp->AssociatedIrp.SystemBuffer;

    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Locating disk device...\n"));

    status = GetNamedCharDevice(
                                DeviceObject->DriverObject,
                                (PCHAR)&DIOCBuffer->DeviceName,
                                &tgtDeviceObj
                                );
                            
    if (!(NT_SUCCESS(status)))
        {
        DEBUGOUTMAINDRV(DEBUGLEV_ERROR, ("Unable to locate required disk device.\n"));
        status = STATUS_INVALID_PARAMETER;
        return status;
        }
        
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("About to destroy device...\n"));
    DestroyDevice(tgtDeviceObj);
    DEBUGOUTMAINDRV(DEBUGLEV_INFO, ("Device destroyed.\n"));            
    
    Irp->IoStatus.Information = 0;
    status = STATUS_SUCCESS;


    DEBUGOUTMAINDRV(DEBUGLEV_EXIT, ("IOCTL_FreeOTFEIOCTL_Destroy\n"));
    return status;
}


// =========================================================================
// 
NTSTATUS
IOCTL_FreeOTFEIOCTL_Mount(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PDEVICE_OBJECT *QueueDeviceObject

⌨️ 快捷键说明

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