<commit_before>extern "C"
{
#include "device.h"
}
#include "../../Interface/File.h"
#include <string>

namespace
{
	class DiskDevice
	{
	public:
		DiskDevice() {}

		void Init(IFile* p_img, ULONGLONG DiskLength, ULONG DiskNumber)
		{
			img = p_img;
			disk_length = DiskLength;
		}

		NTSTATUS InternalDeviceControl(PIRP Irp)
		{
			switch (Irp->StackLocation.Parameters.DeviceIoControl.IoControlCode)
			{
			case IOCTL_DISK_UPDATE_PROPERTIES:
				return STATUS_SUCCESS;
			case IOCTL_DISK_GET_DRIVE_LAYOUT_EX:
			{
				PDRIVE_LAYOUT_INFORMATION_EX layout_info = reinterpret_cast<PDRIVE_LAYOUT_INFORMATION_EX>(Irp->AssociatedIrp.SystemBuffer);
				layout_info->PartitionCount = 0;
				Irp->IoStatus.Information = sizeof(DRIVE_LAYOUT_INFORMATION_EX);
				return STATUS_SUCCESS;
			}
			case IOCTL_DISK_GET_LENGTH_INFO:
			{
				PGET_LENGTH_INFORMATION length_info = reinterpret_cast<PGET_LENGTH_INFORMATION>(Irp->AssociatedIrp.SystemBuffer);
				length_info->Length.QuadPart = disk_length;
				Irp->IoStatus.Information = sizeof(GET_LENGTH_INFORMATION);
				return STATUS_SUCCESS;
			}
			case IOCTL_STORAGE_GET_DEVICE_NUMBER:
			{
				PSTORAGE_DEVICE_NUMBER device_number = reinterpret_cast<PSTORAGE_DEVICE_NUMBER>(Irp->AssociatedIrp.SystemBuffer);
				device_number->DeviceNumber = disk_number;
				device_number->PartitionNumber = 0;
				device_number->DeviceType = 0;
				Irp->IoStatus.Information = sizeof(STORAGE_DEVICE_NUMBER);
				return STATUS_SUCCESS;
			}
			case IOCTL_DISK_GET_DRIVE_GEOMETRY:
			{
				PDISK_GEOMETRY disk_geom = reinterpret_cast<PDISK_GEOMETRY>(Irp->AssociatedIrp.SystemBuffer);
				memset(disk_geom, 0, sizeof(DISK_GEOMETRY));
				disk_geom->BytesPerSector = 4096;
				Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
				return STATUS_SUCCESS;
			}
			case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME:
			{
				PMOUNTDEV_NAME mountdev_name = reinterpret_cast<PMOUNTDEV_NAME>(Irp->AssociatedIrp.SystemBuffer);
				std::wstring name = L"\\Device\\Btrfs{foobar}";
				mountdev_name->NameLength = static_cast<ULONG>((name.size() + 1)*sizeof(wchar_t));

				if (Irp->StackLocation.Parameters.DeviceIoControl.OutputBufferLength >= sizeof(ULONG) + (name.size() + 1) * sizeof(wchar_t))
				{
					memcpy(mountdev_name->Name, name.data(), sizeof(wchar_t) * (name.size() + 1));
					return STATUS_SUCCESS;
				}
				else
				{
					return STATUS_BUFFER_OVERFLOW;
				}
			}
			}

			return STATUS_NOT_IMPLEMENTED;
		}

		NTSTATUS Read(PIRP Irp, PDEVICE_OBJECT DeviceObject)
		{
			bool has_read_err = false;
			_u32 rc = img->Read(static_cast<int64>(Irp->StackLocation.Parameters.Read.ByteOffset.QuadPart),
				reinterpret_cast<char*>(Irp->UserBuffer), Irp->StackLocation.Parameters.Read.Length,
				&has_read_err);

			if (has_read_err)
			{
				return STATUS_CRC_ERROR;
			}

			Irp->IoStatus.Status = STATUS_SUCCESS;
			Irp->IoStatus.Information = rc;

			return STATUS_SUCCESS;
		}

		NTSTATUS Write(PIRP Irp, PDEVICE_OBJECT DeviceObject)
		{
			bool has_read_err = false;
			_u32 rc = img->Write(static_cast<int64>(Irp->StackLocation.Parameters.Write.ByteOffset.QuadPart),
				reinterpret_cast<char*>(Irp->UserBuffer), Irp->StackLocation.Parameters.Write.Length,
				&has_read_err);

			if (has_read_err)
			{
				return STATUS_CRC_ERROR;
			}

			Irp->IoStatus.Status = STATUS_SUCCESS;
			Irp->IoStatus.Information = rc;

			return STATUS_SUCCESS;
		}

		NTSTATUS DeviceControl(PIRP Irp, PDEVICE_OBJECT DeviceObject)
		{
			if (Irp->StackLocation.Parameters.DeviceIoControl.IoControlCode != IOCTL_ATA_PASS_THROUGH)<commit_msg>Call completion routine<commit_after>extern "C"
{
#include "device.h"
}
#include "../../Interface/File.h"
#include <string>

namespace
{
	class DiskDevice
	{
	public:
		DiskDevice() {}

		void Init(IFile* p_img, ULONGLONG DiskLength, ULONG DiskNumber)
		{
			img = p_img;
			disk_length = DiskLength;
		}

		NTSTATUS InternalDeviceControl(PIRP Irp)
		{
			switch (Irp->StackLocation.Parameters.DeviceIoControl.IoControlCode)
			{
			case IOCTL_DISK_UPDATE_PROPERTIES:
				return STATUS_SUCCESS;
			case IOCTL_DISK_GET_DRIVE_LAYOUT_EX:
			{
				PDRIVE_LAYOUT_INFORMATION_EX layout_info = reinterpret_cast<PDRIVE_LAYOUT_INFORMATION_EX>(Irp->AssociatedIrp.SystemBuffer);
				layout_info->PartitionCount = 0;
				Irp->IoStatus.Information = sizeof(DRIVE_LAYOUT_INFORMATION_EX);
				return STATUS_SUCCESS;
			}
			case IOCTL_DISK_GET_LENGTH_INFO:
			{
				PGET_LENGTH_INFORMATION length_info = reinterpret_cast<PGET_LENGTH_INFORMATION>(Irp->AssociatedIrp.SystemBuffer);
				length_info->Length.QuadPart = disk_length;
				Irp->IoStatus.Information = sizeof(GET_LENGTH_INFORMATION);
				return STATUS_SUCCESS;
			}
			case IOCTL_STORAGE_GET_DEVICE_NUMBER:
			{
				PSTORAGE_DEVICE_NUMBER device_number = reinterpret_cast<PSTORAGE_DEVICE_NUMBER>(Irp->AssociatedIrp.SystemBuffer);
				device_number->DeviceNumber = disk_number;
				device_number->PartitionNumber = 0;
				device_number->DeviceType = 0;
				Irp->IoStatus.Information = sizeof(STORAGE_DEVICE_NUMBER);
				return STATUS_SUCCESS;
			}
			case IOCTL_DISK_GET_DRIVE_GEOMETRY:
			{
				PDISK_GEOMETRY disk_geom = reinterpret_cast<PDISK_GEOMETRY>(Irp->AssociatedIrp.SystemBuffer);
				memset(disk_geom, 0, sizeof(DISK_GEOMETRY));
				disk_geom->BytesPerSector = 4096;
				Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
				return STATUS_SUCCESS;
			}
			case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME:
			{
				PMOUNTDEV_NAME mountdev_name = reinterpret_cast<PMOUNTDEV_NAME>(Irp->AssociatedIrp.SystemBuffer);
				std::wstring name = L"\\Device\\Btrfs{foobar}";
				mountdev_name->NameLength = static_cast<ULONG>((name.size() + 1)*sizeof(wchar_t));

				if (Irp->StackLocation.Parameters.DeviceIoControl.OutputBufferLength >= sizeof(ULONG) + (name.size() + 1) * sizeof(wchar_t))
				{
					memcpy(mountdev_name->Name, name.data(), sizeof(wchar_t) * (name.size() + 1));
					return STATUS_SUCCESS;
				}
				else
				{
					return STATUS_BUFFER_OVERFLOW;
				}
			}
			}

			return STATUS_NOT_IMPLEMENTED;
		}

		NTSTATUS Read(PIRP Irp, PDEVICE_OBJECT DeviceObject)
		{
			bool has_read_err = false;
			_u32 rc = img->Read(static_cast<int64>(Irp->StackLocation.Parameters.Read.ByteOffset.QuadPart),
				reinterpret_cast<char*>(Irp->UserBuffer), Irp->StackLocation.Parameters.Read.Length,
				&has_read_err);

			if (has_read_err)
			{
				return STATUS_CRC_ERROR;
			}

			Irp->IoStatus.Status = STATUS_SUCCESS;
			Irp->IoStatus.Information = rc;

			Irp->CompletionRoutine(DeviceObject, Irp, Irp->CompletionRoutineContext);

			return STATUS_SUCCESS;
		}

		NTSTATUS Write(PIRP Irp, PDEVICE_OBJECT DeviceObject)
		{
			bool has_read_err = false;
			_u32 rc = img->Write(static_cast<int64>(Irp->StackLocation.Parameters.Write.ByteOffset.QuadPart),
				reinterpret_cast<char*>(Irp->UserBuffer), Irp->StackLocation.Parameters.Write.Length,
				&has_read_err);

			if (has_read_err)
			{
				return STATUS_CRC_ERROR;
			}

			Irp->IoStatus.Status = STATUS_SUCCESS;
			Irp->IoStatus.Information = rc;

			Irp->CompletionRoutine(DeviceObject, Irp, Irp->CompletionRoutineContext);

			return STATUS_SUCCESS;
		}

		NTSTATUS DeviceControl(PIRP Irp, PDEVICE_OBJECT DeviceObject)
		{
			if (Irp->StackLocation.Parameters.DeviceIoControl.IoControlCode != IOCTL_ATA_PASS_THROUGH)
{'fraction_non_alphanumeric': 0.1074676219344172, 'fraction_numerical': 0.005235602094240838, 'mean_word_length': 4.562452107279693, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6403462', 'n_tokens_mistral': 3087, 'n_tokens_neox': 2583, 'n_words': 345}