<commit_before>﻿#define _CRT_SECURE_NO_WARNINGS
#define WIN32_NO_STATUS
#include <Windows.h>
#undef WIN32_NO_STATUS
#include <winternl.h>
#include <ntstatus.h>
#include <climits>

struct FILE_GET_EA_INFORMATION {
	ULONG NextEntryOffset;
	UCHAR EaNameLength;
	CHAR EaName[1];
};

struct FILE_FULL_EA_INFORMATION {
	ULONG NextEntryOffset;
	UCHAR Flags;
	UCHAR EaNameLength;
	USHORT EaValueLength;
	CHAR EaName[1];
};

extern "C" NTSYSAPI NTSTATUS NTAPI NtQueryEaFile(
	_In_ HANDLE FileHandle,
	_Out_ PIO_STATUS_BLOCK IoStatusBlock,
	_Out_ PVOID Buffer,
	_In_ ULONG Length,
	_In_ BOOLEAN ReturnSingleEntry,
	_In_opt_ PVOID EaList,
	_In_ ULONG EaListLength,
	_In_opt_ PULONG EaIndex,
	_In_ BOOLEAN RestartScan
);

extern "C" NTSYSAPI NTSTATUS NTAPI NtSetEaFile(
	_In_ HANDLE FileHandle,
	_Out_ PIO_STATUS_BLOCK IoStatusBlock,
	_In_ PVOID EaBuffer,
	_In_ ULONG EaBufferSize
);

extern "C" NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(
	_In_ PWSTR DosFileName,
	_Out_ PUNICODE_STRING NtFileName,
	_Out_opt_ PWSTR* FilePart,
	_Out_opt_ PVOID RelativeName
);

extern "C" __declspec(dllexport) HANDLE GetFileHandle(LPWSTR path, bool directory, bool create, bool write) {
	UNICODE_STRING ntPath;
	if (!RtlDosPathNameToNtPathName_U(path, &ntPath, nullptr, nullptr))
		return INVALID_HANDLE_VALUE;

	OBJECT_ATTRIBUTES objAttrs;
	InitializeObjectAttributes(&objAttrs, &ntPath, 0, 0, nullptr);

	HANDLE hFile;
	IO_STATUS_BLOCK status;
	auto res = NtCreateFile(&hFile,
		FILE_GENERIC_READ | (write ? FILE_GENERIC_WRITE : 0),
		&objAttrs, &status, nullptr, 0, 0,
		create ? FILE_CREATE : FILE_OPEN,
		FILE_SYNCHRONOUS_IO_ALERT | (directory ? FILE_DIRECTORY_FILE : FILE_NON_DIRECTORY_FILE),
		nullptr, 0
	);

	return res == STATUS_SUCCESS ? hFile : INVALID_HANDLE_VALUE;
}

const char *LxssEaName = "LXATTRB";
const int LxssEaNameLength = 7;

extern "C" __declspec(dllexport) bool CopyLxssEa(HANDLE hFrom, HANDLE hTo) {
	const int getEaInfoSize = (int)(sizeof(FILE_GET_EA_INFORMATION) + LxssEaNameLength);
	const int eaInfoSize = (int)(sizeof(FILE_FULL_EA_INFORMATION) + LxssEaNameLength + USHRT_MAX);

	char getEaBuf[getEaInfoSize];
	auto getEaInfo = (FILE_GET_EA_INFORMATION *)getEaBuf;
	getEaInfo->NextEntryOffset = 0;
	getEaInfo->EaNameLength = LxssEaNameLength;
	strcpy(getEaInfo->EaName, LxssEaName);

	char eaBuf[eaInfoSize];
	auto eaInfo = (FILE_FULL_EA_INFORMATION *)eaBuf;
	IO_STATUS_BLOCK status;
	auto res = NtQueryEaFile(hFrom, &status, eaInfo, eaInfoSize, true, getEaInfo, getEaInfoSize, nullptr, true);
	if (res != STATUS_SUCCESS) return false;
	res = NtSetEaFile(hTo, &status, eaInfo, eaInfoSize);
	if (res != STATUS_SUCCESS) return false;

	return true;
}
<commit_msg>Add RtlFreeUnicodeString() call.<commit_after>﻿#define _CRT_SECURE_NO_WARNINGS
#define WIN32_NO_STATUS
#include <Windows.h>
#undef WIN32_NO_STATUS
#include <winternl.h>
#include <ntstatus.h>
#include <climits>

struct FILE_GET_EA_INFORMATION {
	ULONG NextEntryOffset;
	UCHAR EaNameLength;
	CHAR EaName[1];
};

struct FILE_FULL_EA_INFORMATION {
	ULONG NextEntryOffset;
	UCHAR Flags;
	UCHAR EaNameLength;
	USHORT EaValueLength;
	CHAR EaName[1];
};

extern "C" NTSYSAPI NTSTATUS NTAPI NtQueryEaFile(
	_In_ HANDLE FileHandle,
	_Out_ PIO_STATUS_BLOCK IoStatusBlock,
	_Out_ PVOID Buffer,
	_In_ ULONG Length,
	_In_ BOOLEAN ReturnSingleEntry,
	_In_opt_ PVOID EaList,
	_In_ ULONG EaListLength,
	_In_opt_ PULONG EaIndex,
	_In_ BOOLEAN RestartScan
);

extern "C" NTSYSAPI NTSTATUS NTAPI NtSetEaFile(
	_In_ HANDLE FileHandle,
	_Out_ PIO_STATUS_BLOCK IoStatusBlock,
	_In_ PVOID EaBuffer,
	_In_ ULONG EaBufferSize
);

extern "C" NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(
	_In_ PWSTR DosFileName,
	_Out_ PUNICODE_STRING NtFileName,
	_Out_opt_ PWSTR* FilePart,
	_Out_opt_ PVOID RelativeName
);

extern "C" __declspec(dllexport) HANDLE GetFileHandle(LPWSTR path, bool directory, bool create, bool write) {
	UNICODE_STRING ntPath;
	if (!RtlDosPathNameToNtPathName_U(path, &ntPath, nullptr, nullptr))
		return INVALID_HANDLE_VALUE;

	OBJECT_ATTRIBUTES objAttrs;
	InitializeObjectAttributes(&objAttrs, &ntPath, 0, 0, nullptr);

	HANDLE hFile;
	IO_STATUS_BLOCK status;
	auto res = NtCreateFile(&hFile,
		FILE_GENERIC_READ | (write ? FILE_GENERIC_WRITE : 0),
		&objAttrs, &status, nullptr, 0, 0,
		create ? FILE_CREATE : FILE_OPEN,
		FILE_SYNCHRONOUS_IO_ALERT | (directory ? FILE_DIRECTORY_FILE : FILE_NON_DIRECTORY_FILE),
		nullptr, 0
	);

	RtlFreeUnicodeString(&ntPath);
	return res == STATUS_SUCCESS ? hFile : INVALID_HANDLE_VALUE;
}

const char *LxssEaName = "LXATTRB";
const int LxssEaNameLength = 7;

extern "C" __declspec(dllexport) bool CopyLxssEa(HANDLE hFrom, HANDLE hTo) {
	const int getEaInfoSize = (int)(sizeof(FILE_GET_EA_INFORMATION) + LxssEaNameLength);
	const int eaInfoSize = (int)(sizeof(FILE_FULL_EA_INFORMATION) + LxssEaNameLength + USHRT_MAX);

	char getEaBuf[getEaInfoSize];
	auto getEaInfo = (FILE_GET_EA_INFORMATION *)getEaBuf;
	getEaInfo->NextEntryOffset = 0;
	getEaInfo->EaNameLength = LxssEaNameLength;
	strcpy(getEaInfo->EaName, LxssEaName);

	char eaBuf[eaInfoSize];
	auto eaInfo = (FILE_FULL_EA_INFORMATION *)eaBuf;
	IO_STATUS_BLOCK status;
	auto res = NtQueryEaFile(hFrom, &status, eaInfo, eaInfoSize, true, getEaInfo, getEaInfoSize, nullptr, true);
	if (res != STATUS_SUCCESS) return false;
	res = NtSetEaFile(hTo, &status, eaInfo, eaInfoSize);
	if (res != STATUS_SUCCESS) return false;

	return true;
}

{'fraction_non_alphanumeric': 0.08575008950948801, 'fraction_numerical': 0.005012531328320802, 'mean_word_length': 5.187153931339978, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '6671790', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2207, 'n_words': 475}