
    1                     
   S r SSKrSSKJr  SSKrSSKJr  SSKJr  \R                  R                  r
\R                  R                  r\R                  R                  r " S S\R                  5      r\R"                  " 5       S   S:X  a  S	rOB\R"                  " 5       S   S
:X  a  SrO'\R&                  " S\R"                  " 5       S   -  5      e " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r\R0                  r\R0                  r\R0                  rSr\R:                  rSrSr Sr!Sr"Sr#Sr$Sr%S\
RL                  l'        \RP                  " \5      /\
RL                  l)        \RT                  \
RV                  l'        \\RP                  " \5      /\
RV                  l)        \RT                  \
RX                  l'        \\RP                  " \5      /\
RX                  l)        \RT                  \
RZ                  l'        \/\
RZ                  l)        \RT                  \
R\                  l'        \\R0                  \R^                  /\
R\                  l)        \\
R`                  l'        \\RP                  " \5      /\
R`                  l)        \RP                  " \5      \Rb                  \Rd                  \Rf                  /\Rh                  l)        \\Rh                  l'        \Rj                  \Rl                  l'        \\R0                  \RP                  " \5      \Rf                  \RP                  " \5      /\Rl                  l)        \Rj                  \Rn                  l'        \\RP                  " \5      \RP                  " \5      \Rf                  \RP                  " \Rf                  5      \R0                  /\Rn                  l)        \\Rp                  l'        \Rb                  \Rf                  \Rf                  \R0                  \Rf                  \Rf                  \/\Rp                  l)        \Rj                  \Rr                  l'        \/\Rr                  l)        \Rj                  \Rt                  l'        \\R0                  \Rf                  \RP                  " \Rf                  5      \R0                  /\Rt                  l)        \Rj                  \Rv                  l'        \\R0                  \Rf                  \RP                  " \Rf                  5      \R0                  /\Rv                  l)        S r<S r=S!S jr> " S S \R~                  5      r@g)"z3Implements raw HID device communication on Windows.    N)wintypes)errors)basec                       \ rS rSrS\R
                  4S\R                  4S\R                  4S\R                  S-  4/rSr	g)	GUID!   Data1Data2Data3Data4    N)
__name__
__module____qualname____firstlineno__ctypesc_ulongc_ushortc_ubyte_fields___static_attributes__r       $lib/third_party/pyu2f/hid/windows.pyr   r   !   s>    '((*+-(r   r   64bitr   32bit   zUnknown architecture: %sc                       \ rS rSrS\R
                  4S\4S\R
                  4S\R                  " \R                  5      4/r
\rSrg)DeviceInterfaceData4   cbSizeInterfaceClassGuidFlagsReservedr   N)r   r   r   r   r   DWORDr   r   POINTERr   r   SETUPAPI_PACK_pack_r   r   r   r   r   r   4   sE    (#T*'6>>&..9:<( &r   r   c                   V    \ rS rSrS\R
                  4S\R                  S-  4/r\	r
Srg)DeviceInterfaceDetailData<   r!   
DevicePathr   r   N)r   r   r   r   r   r%   r   c_byter   r'   r(   r   r   r   r   r*   r*   <   s+    (V]]Q./1(&r   r*   c                       \ rS rSrS\R
                  4S\R                  4S\R                  4S\R                  4/rSrg)HidAttributesB   SizeVendorID	ProductIDVersionNumberr   N)	r   r   r   r   r   r   r   r   r   r   r   r   r/   r/   B   s9    v~~&6??+FOO,02(r   r/   c            	           \ rS rSrS\R
                  4S\R
                  4S\R
                  4S\R
                  4S\R
                  4S\R
                  S-  4S	\R
                  S
-  4/rSrg)HidCapabilitiesI   Usage	UsagePageInputReportByteLengthOutputReportByteLengthFeatureReportByteLengthr$      NotUsed
   r   N)r   r   r   r   r   r   r   r   r   r   r   r6   r6   I   sg    (FOO,&8'9(&//:6??R/0&//B./1(r   r6   l    i            i   @l        c                    [        5       n[        R                  U [        R                  " U5      5      nU(       d  [        R
                  " 5       e[        R                  " S5      n[        R                  XS5      nU(       d  [        R
                  " 5       eUR                  Ul	        UR                  Ul        [        R                  " U5      Ul        g)aF  Fill out the attributes of the device.

Fills the devices HidAttributes and product string
into the descriptor.

Args:
  device: A handle to the open device
  descriptor: The DeviceDescriptor to populate with the
    attributes.

Returns:
  None

Raises:
  WindowsError when unable to obtain attributes or product
    string.
i   N)r/   hidHidD_GetAttributesr   byrefWinErrorcreate_string_bufferHidD_GetProductStringr2   	vendor_idr3   
product_id
wstring_atproduct_string)device
descriptor
attributesresultbufs        r   FillDeviceAttributesrS      s    $ *!!&&,,z*BC&	
//
##D)#$$V$7&	
//
#,,*$..*$//4*r   c                 4   [        S5      n[        R                  U [        R                  " U5      5      nU(       d  [        R
                  " 5       e [        5       n[        R                  U[        R                  " U5      5      nU[        :w  a  [        R
                  " 5       eUR                  Ul
        UR                  Ul        UR                  Ul        UR                  Ul        [        R#                  U5        g! [        R#                  U5        f = f)a  Fill out device capabilities.

Fills the HidCapabilitites of the device into descriptor.

Args:
  device: A handle to the open device
  descriptor: DeviceDescriptor to populate with the
    capabilities

Returns:
  none

Raises:
  WindowsError when unable to obtain capabilitites.
r   N)PHIDP_PREPARSED_DATArD   HidD_GetPreparsedDatar   rF   rG   r6   HidP_GetCapsHIDP_STATUS_SUCCESSr8   usager9   
usage_pager:   internal_max_in_report_lenr;   internal_max_out_report_lenHidD_FreePreparsedData)rN   rO   preparsed_dataretcapss        r   FillDeviceCapabilitiesra      s      (*.!!&&,,~*FG#	
//
/D


>6<<+=
>C
!!OOzzJ NNJ,0,F,FJ)-1-H-HJ* ~.C~.s   BD   Dc           	          [         [        -  n[        [        -  nU(       a  Sn[        R                  U UUS[        SS5      nU[        :X  a  [        R                  " 5       eU$ )z*Open the device and return a handle to it.r   N)
GENERIC_WRITEGENERIC_READFILE_SHARE_READFILE_SHARE_WRITEkernel32CreateFileAOPEN_EXISTINGINVALID_HANDLE_VALUEr   rG   )pathenumdesired_access
share_modehs        r   
OpenDevicerp      s]     </.!11*	N4)%49! 

//
	
(r   c                   L    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	 rS
rg)WindowsHidDevice   z/Implementation of raw HID interface on Windows.c            	         [        5       n [        R                  [        R                  " U 5      5        [
        R                  [        R                  " U 5      SSS5      nSn[        5       n[        R                  " [        5      Ul	        / n [
        R                  US[        R                  " U 5      U[        R                  " U5      5      nUS-  nU(       d   U$ [        R                  " 5       n[
        R                  U[        R                  " U5      SS[        R                  " U5      S5      nUR                  nUS:X  a  M  [        R                  " U5      n[         R#                  U5      n[        R                  " [         5      Ul	        [
        R                  U[        R                  " U5      [        R                  " U5      USS5      nU(       d  [        R$                  " 5       e[&        R(                  " 5       n	U[        R                  " [        R                  5      -
  n
[        R*                  " [        R,                  " UR.                  5      U
5      U	l        Sn [3        U	R0                  S5      n [;        X5        [=        X5        UR?                  U	RA                  5       5         [B        RE                  U5        GMA  ! [4         a"  nUR6                  [8        :X  a   SnAGMg  UeSnAff = f! [4         a!  n SnA[B        RE                  U5        GM  SnAff = f! [B        RE                  U5        f = f)See base class.N   r   Tr   )#r   rD   HidD_GetHidGuidr   rF   setupapiSetupDiGetClassDevsAr   sizeofr!   SetupDiEnumDeviceInterfacesr   r%    SetupDiGetDeviceInterfaceDetailAvaluerH   r*   from_bufferrG   r   DeviceDescriptor	string_at	addressofr,   rk   rp   WindowsErrorwinerrorERROR_ACCESS_DENIEDrS   ra   appendToPublicDictrg   CloseHandle)hid_guiddevicesindexinterface_infooutrQ   
detail_lenrR   interface_detailrO   path_lenrN   es                r   	EnumerateWindowsHidDevice.Enumerate   s    vHX./++XdD2GE(*N"MM*=>N
C
33
1fll8,e
,,~
&(f qjep Jm >>#j88
6<</q
,,z
"D*f ##j	q''
3c2>>sC &.G H88
6<</
,,'
(*dDBf oo((*j fmmHNN;;h((


+66
7Cjo fJOOT2%V0v2

:**,- 	V$y ^  ::,,
'	  V$ 	V$sB   *K 5K> 
K;K64K66K;>
L)L, $L))L, ,Mc                     [         R                  R                  X5        [        U5      U l        [         R
                  " 5       U l        [        U R                  U R                  5        g)ru   N)r   	HidDevice__init__rp   devr   descra   )selfrk   s     r   r   WindowsHidDevice.__init__A  sB    NND'$DH%%'DI488TYY/r   c                 4    U R                   R                  S-
  $ ru   r   )r   r[   r   s    r   GetInReportDataLength&WindowsHidDevice.GetInReportDataLengthH  s    99//!33r   c                 4    U R                   R                  S-
  $ r   )r   r\   r   s    r   GetOutReportDataLength'WindowsHidDevice.GetOutReportDataLengthL  s    9900144r   c                    [        U5      U R                  5       :w  a  [        R                  " S5      eS/U-   n[	        [        U5      5      n[        R                  " 5       n[        R                  U R                  U[        U5      [        R                  " U5      S5      nUR                  [        U5      :w  a1  [        R                  " SS[        U5      UR                  4-  -   5      eU(       d  [        R                  " 5       eg)ru   z,Packet length must match report data length.r   Nz"Failed to write complete packet.  zExpected %d, but got %d)lenr   r   HidErrorbytes	bytearrayr   r%   rg   	WriteFiler   r   rF   r}   rG   )r   packetpacket_datar   num_writtenr_   s         r   WriteWindowsHidDevice.WriteP  s    
6{d1133OOJKK#,K
	+&
'C.."KHHc3s8LL%t	-  CH$OO
.1Js8[&&
'2( () ) OO r   c                    [         R                  " U R                  R                  5      n[        R
                  " 5       n[        R                  U R                  U[        U5      [         R                  " U5      S5      nUR                  U R                  R                  :w  a  [        R                  " S5      eU(       d  [         R                  " 5       e[        [!        USS 5      5      $ )ru   Nz.Failed to read full length report from device.r   )r   rH   r   r[   r   r%   rg   ReadFiler   r   rF   r}   r   r   rG   listr   )r   rR   num_readr_   s       r   ReadWindowsHidDevice.Readc  s    

%
%dii&J&J
KC~~H


#s3xh!7?C ~~===OOLMMOO 	#ab'"##r   c                 f    [        U S5      (       a   [        R                  U R                  5        gg)z,Closes the file handle when object is GC-ed.r   N)hasattrrg   r   r   r   s    r   __del__WindowsHidDevice.__del__t  s&    tU488$ r   )r   r   N)r   r   r   r   __doc__staticmethodr   r   r   r   r   r   r   r   r   r   r   rr   rr      s8    7J JX045&$"%r   rr   )F)Ar   r   r   platformpyu2fr   	pyu2f.hidr   windllHidrD   SetupAPIrx   Kernel32rg   	Structurer   architecturer'   r   r   r*   r/   r6   c_void_pHDEVINFOHANDLErU   rj   c_longNTSTATUSrX   re   rf   ri   r   rc   rd   rw   restyper&   argtypesBOOLEANrE   rV   r]   rI   r   rW   c_char_pHWNDr%   ry   BOOLr{   r|   rh   r   r   r   rS   ra   rp   r   rr   r   r   r   <module>r      sl   :      mm==!!==!!-6 - 1(-Q7*-2X5J5J5LQ5OOPP&**  0 0 2F$$ 21f&& 1 ??	  "  ==      #    &t 45   !)!1!1   #)6>>-+H"I   $,$4$4   !&,&,nn5I&J&L   "%-%5%5   "';&<   #$,$4$4   !&,foov~~%N   "#   1#^^O<>    +1..*>*2--*I   &(0   %/7}} $ $ ,foov~~d3X^^
NN&'1) $ $ - 5=MM ) ) 1fnn01
NN,-x~~
NN8>>"FOO65 ) ) 2
  &   
OOX^^X^^V__NNHNNF!,     (}}   !'   $MM   
FOOX^^
NN8>>"FOO5    &]]   
FOOX^^
NN8>>"FOO5   
5D"/X F%t~~ F%r   