<commit_before>﻿using LibUsbDotNet.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibUsbDotNet.Main;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.IO;

namespace LibUsbDotNet.WinHid
{
    internal class WinHidApi : UsbApiBase
    {
        public override bool AbortPipe(SafeHandle interfaceHandle, byte pipeID)
        {
            throw new NotImplementedException();
        }

        public override bool ControlTransfer(SafeHandle interfaceHandle, UsbSetupPacket setupPacket, IntPtr buffer, int bufferLength, out int lengthTransferred)
        {
            throw new NotImplementedException();
        }

        public override bool FlushPipe(SafeHandle interfaceHandle, byte pipeID)
        {
            throw new NotImplementedException();
        }

        public override bool GetDescriptor(SafeHandle interfaceHandle, byte descriptorType, byte index, ushort languageID, IntPtr buffer, int bufferLength, out int lengthTransferred)
        {
            throw new NotImplementedException();
        }

        public override bool GetOverlappedResult(SafeHandle interfaceHandle, IntPtr pOverlapped, out int numberOfBytesTransferred, bool wait)
        {
            return Kernel32.GetOverlappedResult(interfaceHandle, pOverlapped, out numberOfBytesTransferred, wait);
        }

        public override bool ReadPipe(UsbEndpointBase endPointBase, IntPtr pBuffer, int bufferLength, out int lengthTransferred, int isoPacketSize, IntPtr pOverlapped)
        {
            var res = Kernel32.ReadFile(endPointBase.Device.Handle, pBuffer, bufferLength, out lengthTransferred, pOverlapped);
            return res > 0;
        }

        public override bool ResetPipe(SafeHandle interfaceHandle, byte pipeID)
        {
            throw new NotImplementedException();
        }

        public override bool WritePipe(UsbEndpointBase endPointBase, IntPtr pBuffer, int bufferLength, out int lengthTransferred, int isoPacketSize, IntPtr pOverlapped)
        {
            //var fileHandle = (SafeFileHandle)endPointBase.Device.Handle;
            //var file = new FileStream(handle, FileAccess.ReadWrite, 65, true);
            //    file.Write(output, 0, bufferLength+1);
           var res = Kernel32.WriteFile(endPointBase.Device.Handle, pBuffer, bufferLength, out lengthTransferred, pOverlapped);
            
            return res > 0;
                //return Hid.NativeMethods.HidD_SetOutputReport(, pBuffer, bufferLength);
        }
        static SafeFileHandle handle;

        internal static bool OpenDevice(out SafeFileHandle sfhDevice, string DevicePath)
        {
            sfhDevice =
            Kernel32.CreateFile(DevicePath,
                        NativeFileAccess.FILE_GENERIC_WRITE | NativeFileAccess.FILE_GENERIC_READ,
                        NativeFileShare.FILE_SHARE_WRITE | NativeFileShare.FILE_SHARE_READ,
                        IntPtr.Zero,
                        NativeFileMode.OPEN_EXISTING,
                        NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED,
                        IntPtr.Zero);
            //sfhDevice =
            //    Kernel32.CreateFile(DevicePath,
            //                        0,
            //                        NativeFileShare.FILE_SHARE_WRITE | NativeFileShare.FILE_SHARE_READ,
            //                        IntPtr.Zero,
            //                        NativeFileMode.OPEN_EXISTING,
            //                        0, //NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED,
            //                        IntPtr.Zero);

            //sfhDevice = FileIo.CreateFile(DevicePath, FileIo.GenericRead | FileIo.GenericWrite, FileIo.FileShareRead | FileIo.FileShareWrite, IntPtr.Zero, FileIo.OpenExisting, NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED, IntPtr.Zero);

            handle = sfhDevice;

            return (!sfhDevice.IsInvalid && !sfhDevice.IsClosed);
        }
    }
}
<commit_msg>Silently fail for unimplemented WinHid API functions<commit_after>﻿using LibUsbDotNet.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibUsbDotNet.Main;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.IO;
using System.Diagnostics;

namespace LibUsbDotNet.WinHid
{
    internal class WinHidApi : UsbApiBase
    {
        public override bool AbortPipe(SafeHandle interfaceHandle, byte pipeID)
        {
            Debug.WriteLine("Unimplemented: WinHidApi: AbortPipe");
            return false;
        }

        public override bool ControlTransfer(SafeHandle interfaceHandle, UsbSetupPacket setupPacket, IntPtr buffer, int bufferLength, out int lengthTransferred)
        {
            lengthTransferred = 0;
            Debug.WriteLine("Unimplemented: WinHidApi: ControlTransfer");
            return false;
        }

        public override bool FlushPipe(SafeHandle interfaceHandle, byte pipeID)
        {
            Debug.WriteLine("Unimplemented: WinHidApi: FlushPipe");
            return false;
        }

        public override bool GetDescriptor(SafeHandle interfaceHandle, byte descriptorType, byte index, ushort languageID, IntPtr buffer, int bufferLength, out int lengthTransferred)
        {
            lengthTransferred = 0;
            Debug.WriteLine("Unimplemented: WinHidApi: GetDescriptor");
            return false;
        }

        public override bool GetOverlappedResult(SafeHandle interfaceHandle, IntPtr pOverlapped, out int numberOfBytesTransferred, bool wait)
        {
            return Kernel32.GetOverlappedResult(interfaceHandle, pOverlapped, out numberOfBytesTransferred, wait);
        }

        public override bool ReadPipe(UsbEndpointBase endPointBase, IntPtr pBuffer, int bufferLength, out int lengthTransferred, int isoPacketSize, IntPtr pOverlapped)
        {
            var res = Kernel32.ReadFile(endPointBase.Device.Handle, pBuffer, bufferLength, out lengthTransferred, pOverlapped);
            return res > 0;
        }

        public override bool ResetPipe(SafeHandle interfaceHandle, byte pipeID)
        {
            Debug.WriteLine("Unimplemented: WinHidApi: ResetPipe");
            return false;
        }

        public override bool WritePipe(UsbEndpointBase endPointBase, IntPtr pBuffer, int bufferLength, out int lengthTransferred, int isoPacketSize, IntPtr pOverlapped)
        {
            //var fileHandle = (SafeFileHandle)endPointBase.Device.Handle;
            //var file = new FileStream(handle, FileAccess.ReadWrite, 65, true);
            //    file.Write(output, 0, bufferLength+1);
           var res = Kernel32.WriteFile(endPointBase.Device.Handle, pBuffer, bufferLength, out lengthTransferred, pOverlapped);
            
            return res > 0;
                //return Hid.NativeMethods.HidD_SetOutputReport(, pBuffer, bufferLength);
        }
        static SafeFileHandle handle;

        internal static bool OpenDevice(out SafeFileHandle sfhDevice, string DevicePath)
        {
            sfhDevice =
            Kernel32.CreateFile(DevicePath,
                        NativeFileAccess.FILE_GENERIC_WRITE | NativeFileAccess.FILE_GENERIC_READ,
                        NativeFileShare.FILE_SHARE_WRITE | NativeFileShare.FILE_SHARE_READ,
                        IntPtr.Zero,
                        NativeFileMode.OPEN_EXISTING,
                        NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED,
                        IntPtr.Zero);
            //sfhDevice =
            //    Kernel32.CreateFile(DevicePath,
            //                        0,
            //                        NativeFileShare.FILE_SHARE_WRITE | NativeFileShare.FILE_SHARE_READ,
            //                        IntPtr.Zero,
            //                        NativeFileMode.OPEN_EXISTING,
            //                        0, //NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED,
            //                        IntPtr.Zero);

            //sfhDevice = FileIo.CreateFile(DevicePath, FileIo.GenericRead | FileIo.GenericWrite, FileIo.FileShareRead | FileIo.FileShareWrite, IntPtr.Zero, FileIo.OpenExisting, NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED, IntPtr.Zero);

            handle = sfhDevice;

            return (!sfhDevice.IsInvalid && !sfhDevice.IsClosed);
        }
    }
}

{'fraction_non_alphanumeric': 0.06911798929485688, 'fraction_numerical': 0.004887130556202001, 'mean_word_length': 2.3044982698961936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6644989', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2216, 'n_words': 486}