
    !hiZP                         d dl Z d dlZd dlmZ d ZdZd Zd Zd Z G d d	      Z	dd
Z
d Z G d d      Z G d d      Z G d d      Z G d d      Zd Z G d d      Z G d d      Zd Zd Zy)    N)win32apic                     	 t        j                  | d      }t        |j                  j                  dz        S # t
        $ r Y yw xY w)ab  
    Checks if the specified PE file has CFG (Control Flow Guard) enabled.

    Parameters
    ----------
    filename : str
        Path to the PE file to inspect.

    Returns
    ----------
    bool
        True if file is a PE file with CFG enabled. False if CFG is not enabled or if file could not be processed using
        the pefile library.
    T	fast_loadi @  F)pefilePEboolOPTIONAL_HEADERDllCharacteristics	Exception)filenamepes     OC:\des-py\Monitor\venv\Lib\site-packages\PyInstaller/utils/win32/versioninfo.pypefile_check_control_flow_guardr      sG    YYx40 B&&99FBCC s   8; 	AA   c                 $    | j                  d      S )zL
    Encodes text as UTF-16LE (Microsoft 'Unicode') for use in structs.
    UTF-16LE)encode)texts    r   getRawr   /   s     ;;z""    c                 X   t        j                  | dt              }t        j                  |t        j
                  d         }t        |      syt        j                  |t        j
                  d   |d         }t               }|j                  |       t        j                  |       |S )z
    Read the version information structure from the given executable's resources, and return it as an instance of
    `VSVersionInfo` structure.
    r   
RT_VERSIONN)r   LoadLibraryExLOAD_LIBRARY_AS_DATAFILEEnumResourceNamesr   RESOURCE_TYPElenLoadResourceVSVersionInfofromRawFreeLibrary)exe_filenamehresdatainfos        r   !read_version_info_from_executabler(   6   s    
 	|Q0HIA

$
$Q(<(<\(J
KCs8  F$8$8$FAOD?DLLKr   c                     | dz   dz	  dz  S )z5
    Align `offset` to the next 4-byte boundary.
       r    )offsets    r   	nextDWordr-   F   s     aZA!##r   c                   8    e Zd ZdZd	dZd Zd Zd Zd
dZd Z	y)r    a  
    WORD  wLength;        // length of the VS_VERSION_INFO structure
    WORD  wValueLength;   // length of the Value member
    WORD  wType;          // 1 means text, 0 means binary
    WCHAR szKey[];        // Contains the Unicode string "VS_VERSION_INFO".
    WORD  Padding1[];
    VS_FIXEDFILEINFO Value;
    WORD  Padding2[];
    WORD  Children[];     // zero or more StringFileInfo or VarFileInfo
                          // structures (or both) that are children of the
                          // current version structure.
    Nc                 (    || _         |xs g | _        y Nffikids)selfr2   r3   s      r   __init__zVSVersionInfo.__init__Z   s    JB	r   c           	      &   t        |      \  }\  }}}}t        |      }t               | _        | j                  j	                  ||      }|}||k  r|}t        ||      \  }\  }}	}
}|j                         dk(  rAt               }|j	                  ||	|||||z         }| j                  j                  |       |}n@t               }|j	                  ||	|||||z         }| j                  j                  |       |}||z   }t        |      }||k  r|S NStringFileInfo)
parseCommonr-   FixedFileInfor2   r!   stripr8   r3   appendVarFileInfo)r4   r&   isublenvallenwTypenmjcsublencvallenctypsfikvfis                 r   r!   zVSVersionInfo.fromRaw^   s   )4T):&&FFE2aL ?HHT1%&jA.9$.B+A+$xxz--$&KK"dAq7{K		  %!mKK"dAq7{K		  %GA!A &j r   c                    t        d      }| j                  j                         }t        |      }d}dt        |      z   dz   }d}|dz  rd}|t        |      z   |z   }d}|dz  rd}dj	                  | j
                  D cg c]  }|j                          c}      }	|t        |      z   t        |	      z   }t        j                  d|||      |z   dz   |z   |z   |z   |	z   S c c}w )	NVS_VERSION_INFOr      r   r           HHH)r   r2   toRawr   joinr3   structpack)
r4   raw_namerawffir@   typr?   padpad2kidtmps
             r   rP   zVSVersionInfo.toRaww   s    +,!VS]"Q&A:C#c("V+A:Dhhtyy9		9:#d)#c#h.{{5&&#6AKORUUX^^aeehkkk :s    C"c                 (    | j                         |k(  S r0   rP   r4   others     r   __eq__zVSVersionInfo.__eq__       zz|u$$r   c                    |dz   }| j                   D cg c]  }|j                  |dz          }}dj                  |      }dj                  ddddd|d	| j                  j                  |       d
z   |dz   ||dz   dg
      S c c}w )N  , 

z# UTF-8#z3# For more details about fixed file info 'ffi' see:z7# http://msdn.microsoft.com/en-us/library/ms646997.aspxzVSVersionInfo(zffi=,zkids=[]))r3   __str__rQ   r2   )r4   indentrY   rZ   s       r   ri   zVSVersionInfo.__str__   s    $59YY?cs{{6D=)??kk#yyAEtDHH,,V45Q77XSL
  	 @s   Bc                 <    d| j                   d| j                  dS )Nzversioninfo.VSVersionInfo(ffi=z, kids=rh   r1   r4   s    r   __repr__zVSVersionInfo.__repr__   s    ?CxxSSr   NN 
__name__
__module____qualname____doc__r5   r!   rP   r_   ri   rm   r+   r   r   r    r    M   s'    2l"%"Tr   r    c                 |    |dz   }t        j                  d| ||       \  }}}t        | |||z         \  }}|||||ffS )NrL   3H)rR   unpackparseUString)r&   startr>   wLengthwValueLengthrA   r   s          r   r9   r9      sR    	A%+]]4eA%G"WlE4AK0GAtweT222r   c                 x    |}||k  r| ||dz    dk(  rn|dz  }||k  r| || j                  d      }|dz  }||fS )Nr   rN   r   )decode)r&   rz   limitr>   r   s        r   ry   ry      s_    A
e)!a%=K'	Q e) a=
+DFAd7Nr   c                   H    e Zd ZdZ	 	 	 	 	 	 	 	 d	dZd Zd Zd Zd
dZd Z	y)r:   a  
    DWORD dwSignature;        //Contains the value 0xFEEFO4BD
    DWORD dwStrucVersion;     // binary version number of this structure.
                              // The high-order word of this member contains
                              // the major version number, and the low-order
                              // word contains the minor version number.
    DWORD dwFileVersionMS;    // most significant 32 bits of the file's binary
                              // version number
    DWORD dwFileVersionLS;    //
    DWORD dwProductVersionMS; // most significant 32 bits of the binary version
                              // number of the product with which this file was
                              // distributed
    DWORD dwProductVersionLS; //
    DWORD dwFileFlagsMask;    // bitmask that specifies the valid bits in
                              // dwFileFlags. A bit is valid only if it was
                              // defined when the file was created.
    DWORD dwFileFlags;        // VS_FF_DEBUG, VS_FF_PATCHED etc.
    DWORD dwFileOS;           // VOS_NT, VOS_WINDOWS32 etc.
    DWORD dwFileType;         // VFT_APP etc.
    DWORD dwFileSubtype;      // 0 unless VFT_DRV or VFT_FONT or VFT_VXD
    DWORD dwFileDateMS;
    DWORD dwFileDateLS;
    c	                 >   d| _         d| _        |d   dz  |d   dz  z  | _        |d   dz  |d   dz  z  | _        |d   dz  |d   dz  z  | _        |d   dz  |d   dz  z  | _        || _        || _        || _        || _	        || _
        |d   | _        |d   | _        y )	Nl   } i   r           r   r*   )sigstrucVersionfileVersionMSfileVersionLSproductVersionMSproductVersionLSfileFlagsMask	fileFlagsfileOSfileTypefileSubtype
fileDateMS
fileDateLS)	r4   fileversprodversmaskflagsOSr   subtypedates	            r   r5   zFixedFileInfo.__init__   s     #&qkR/HQK&4HI&qkR/HQK&4HI!)!!2x{V7K L!)!!2x{V7K L! "q'q'r   c                     t        j                  d|||dz          \  | _        | _        | _        | _        | _        | _        | _        | _	        | _
        | _        | _        | _        | _        |dz   S )N13L4   )rR   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   )r4   r&   r>   s      r   r!   zFixedFileInfo.fromRaw   sr     MM%aB0	
H!!NKMOO2vr   c                 J   t        j                  d| j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S )Nr   )rR   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   rl   s    r   rP   zFixedFileInfo.toRaw   s    {{HH!!!!NNKKMMOOOO
 	
r   c                 (    | j                         |k(  S r0   r\   r]   s     r   r_   zFixedFileInfo.__eq__  r`   r   c                 d   | j                   dz	  | j                   dz  | j                  dz	  | j                  dz  f}| j                  dz	  | j                  dz  | j                  dz	  | j                  dz  f}| j                  | j
                  f}dddd|dd|dd	d
t        | j                        z  ddt        | j                        z  dddt        | j                        z  dddt        | j                        z  dddt        | j                        z  dd|dg}d| dj                  |      S )Nr   r   zFixedFileInfo(zN# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)z!# Set not needed items to zero 0.z	filevers=rf   z	prodvers=z;# Contains a bitmask that specifies the valid bits 'flags'rzmask=%s,zG# Contains a bitmask that specifies the Boolean attributes of the file.z	flags=%s,z8# The operating system for which this file was designed.z-# 0x4 - NT and there is no need to change it.zOS=%s,z# The general type of file.z## 0x1 - the file is an application.zfileType=%s,z# The function of the file.z5# 0x0 - the function is not defined for this fileTypezsubtype=%s,z# Creation date and time stamp.zdate=rh   rd   rb   )r   r   r   r   r   r   hexr   r   r   r   r   rQ   )r4   rj   fvpvfdrZ   s         r   ri   zFixedFileInfo.__str__
  sF   "$d&8&86&A"$d&8&86&A

 !!R')>)>)G!!R')>)>)G
 oot/\/ " "IT//00U#dnn--F;s4;;'')1S//)CC 0 011-+
. F82##C((r   c           	         | j                   dz	  | j                   dz  | j                  dz	  | j                  dz  f}| j                  dz	  | j                  dz  | j                  dz	  | j                  dz  f}| j                  | j
                  f}d||| j                  | j                  | j                  | j                  | j                  |fz  S )Nr   r   zwversioninfo.FixedFileInfo(filevers=%r, prodvers=%r, mask=0x%x, flags=0x%x, OS=0x%x, fileType=%r, subtype=0x%x, date=%r))r   r   r   r   r   r   r   r   r   r   r   )r4   r   r   r   s       r   rm   zFixedFileInfo.__repr__-  s    "$d&8&86&A"$d&8&86&A

 !!R')>)>)G!!R')>)>)G
 oot/2 T''dmmUYUeUegijk	
r   N)r   r   r   r   r   ?   r   i  r   r   )r   r   ro   rq   r+   r   r   r:   r:      s@    2 "2$
$%!)F
r   r:   c                   8    e Zd ZdZd	dZd Zd Zd Zd
dZd Z	y)r8   a  
    WORD        wLength;      // length of the version resource
    WORD        wValueLength; // length of the Value member in the current
                              // VS_VERSION_INFO structure
    WORD        wType;        // 1 means text, 0 means binary
    WCHAR       szKey[];      // Contains the Unicode string 'StringFileInfo'.
    WORD        Padding[];
    StringTable Children[];   // list of zero or more String structures
    Nc                 (    d| _         |xs g | _        y r7   namer3   r4   r3   s     r   r5   zStringFileInfo.__init__I  s    $	JB	r   c                     || _         ||k  r@t               }|j                  |||      }| j                  j	                  |       |}||k  r@|S r0   )r   StringTabler!   r3   r<   )	r4   r?   r@   r   r&   r>   r   strC   s	            r   r!   zStringFileInfo.fromRawM  sP    	%iB

4E*AIIR A	 %i
 r   c                 \   t        | j                        }d}d}dt        |      z   dz   }d}|dz  rd}dj                  | j                  D cg c]  }|j                          c}      }|t        |      z   t        |      z   }t        j                  d|||      |z   dz   |z   |z   S c c}w )	Nr   r   rL   r   r   rM   rN   rO   )r   r   r   rQ   r3   rP   rR   rS   )r4   rT   r@   rV   r?   rW   rY   rZ   s           r   rP   zStringFileInfo.toRawV  s    $))$S]"Q&A:Chhtyy9		9:#c("SX-{{5&&#6AKORUUX[[[ :s   B)c                 (    | j                         |k(  S r0   r\   r]   s     r   r_   zStringFileInfo.__eq__b  r`   r   c                 r    |dz   dj                  fd| j                  D              }| d d| d dS )Nrb   rc   c              3   @   K   | ]  }|j                          y wr0   )ri   ).0rY   
new_indents     r   	<genexpr>z)StringFileInfo.__str__.<locals>.<genexpr>g  s     Gc#++j1Gs   zStringFileInfo(
z[
rd   ])rQ   r3   )r4   rj   rZ   r   s      @r   ri   zStringFileInfo.__str__e  sD    d]
kkGTYYGG*:,c#bBOOr   c                      d| j                   z  S )Nzversioninfo.StringFileInfo(%r)r3   rl   s    r   rm   zStringFileInfo.__repr__j  s    /$));;r   r0   ro   rq   r+   r   r   r8   r8   ?  s'    
\%P
<r   r8   c                   8    e Zd ZdZd	dZd Zd Zd Zd
dZd Z	y)r   z
    WORD   wLength;
    WORD   wValueLength;
    WORD   wType;
    WCHAR  szKey[];
    String Children[];    // list of zero or more String structures.
    Nc                 0    |xs d| _         |xs g | _        y Nrp   r   r4   r   r3   s      r   r5   zStringTable.__init__v      JB	JB	r   c                     t        |||      \  }\  }}}| _        t        |      }||k  rKt               }|j	                  |||      }|}| j
                  j                  |       t        |      }||k  rK|S r0   )parseCodePager   r-   StringStructr!   r3   r<   )	r4   r&   r>   r   cpsublencpwValueLengthcpwTypessrC   s	            r   r!   zStringTable.fromRawz  s{    <I$PQSX<Y99HngtyaL%iB

4E*AAIIR !A %i r   c                 d   t        | j                        }d}d}dt        |      z   dz   }g }| j                  D ]6  }|j	                         }t        |      dz  r|dz   }|j                  |       8 dj                  |      }|t        |      z  }t        j                  d|||      |z   dz   |z   S )	Nr   r   rL   r   rM   rN   r   rO   )	r   r   r   r3   rP   r<   rQ   rR   rS   )r4   rT   r@   rV   r?   rZ   rY   raws           r   rP   zStringTable.toRaw  s    $))$S]"Q&99 	C))+C3x!|K'JJsO		
 hhsm#c({{5&&#6AKORUUUr   c                 (    | j                         |k(  S r0   r\   r]   s     r   r_   zStringTable.__eq__  r`   r   c           
          |dz   }d|z   j                  d | j                  D              }| d| d| j                   d| d| d
S )	Nrb   z,
c              3   2   K   | ]  }t        |        y wr0   strr   rY   s     r   r   z&StringTable.__str__.<locals>.<genexpr>  s     'FSC'F   zStringTable(
'z',
[r   )rQ   r3   r   )r4   rj   r   rZ   s       r   ri   zStringTable.__str__  sV    d]
z!'''FDII'FF
|1TYYKtJ<qQTPUUWXXr   c                 <    d| j                   d| j                  dS )Nzversioninfo.StringTable(, rh   r   rl   s    r   rm   zStringTable.__repr__  s    48IItyyIIr   rn   ro   rq   r+   r   r   r   r   n  s(    	V%Y
Jr   r   c                   8    e Zd ZdZd	dZd Zd Zd Zd
dZd Z	y)r   z
    WORD   wLength;
    WORD   wValueLength;
    WORD   wType;
    WCHAR  szKey[];
    WORD   Padding[];
    String Value[];
    Nc                 0    |xs d| _         |xs d| _        y r   r   val)r4   r   r   s      r   r5   zStringStruct.__init__  s    JB	9"r   c                     t        ||      \  }\  }}}| _        ||z   }t        |      }t        |||      \  }| _        |S r0   )r9   r   r-   ry   r   )r4   r&   r>   r   r?   r@   rV   s          r   r!   zStringStruct.fromRaw  sI    .9$.B++FFCF
aL"4E248r   c                 4   t        | j                        }t        | j                        }t        | j                        dz   }d}dt        |      z   dz   }d}|dz  rd}|t        |      z   |dz  z   }t	        j
                  d|||      |z   dz   |z   |z   dz   S )Nr   rL   r   r   rM   rN   rO   )r   r   r   r   rR   rS   )r4   rT   raw_valr@   rV   r?   rW   s          r   rP   zStringStruct.toRaw  s    $))$"TXX"S]"Q&A:C#c("fqj1{{5&&#6AKORUUX__bmmmr   c                 (    | j                         |k(  S r0   r\   r]   s     r   r_   zStringStruct.__eq__  r`   r   c                 <    d| j                   d| j                  dS )NzStringStruct(r   rh   r   r4   rj   s     r   ri   zStringStruct.__str__  s    )-DHH==r   c                 <    d| j                   d| j                  dS )Nzversioninfo.StringStruct(r   rh   r   rl   s    r   rm   zStringStruct.__repr__  s    59YYIIr   rn   ro   rq   r+   r   r   r   r     s'    n%>Jr   r   c                 :    t        | |      \  }\  }}}}|||||ffS r0   )r9   )r&   r>   r   r?   r|   rA   rB   s          r   r   r     s0    +6tQ+?(A(eRv|UB///r   c                   8    e Zd ZdZd	dZd Zd Zd Zd
dZd Z	y)r=   a  
    WORD  wLength;        // length of the version resource
    WORD  wValueLength;   // length of the Value member in the current
                          // VS_VERSION_INFO structure
    WORD  wType;          // 1 means text, 0 means binary
    WCHAR szKey[];        // Contains the Unicode string 'VarFileInfo'.
    WORD  Padding[];
    Var   Children[];     // list of zero or more Var structures
    Nc                     |xs g | _         y r0   r   r   s     r   r5   zVarFileInfo.__init__  s    JB	r   c                     || _         || _        || _        t        |      }||k  r@t	               }|j                  |||      }| j                  j                  |       |}||k  r@|S r0   )r?   r@   r   r-   	VarStructr!   r3   r<   )	r4   r?   r@   r   r&   r>   r   vsrC   s	            r   r!   zVarFileInfo.fromRaw  sg    	aL%iB

4E*AIIR A	 %i
 r   c                    d| _         d| _        d| _        t        | j                        }dt	        |      z   dz   }d}|dz  rd}dj                  | j                  D cg c]  }|j                          c}      }|t	        |      z   t	        |      z   | _        t        j                  d	| j                  | j                   | j                        |z   dz   |z   |z   S c c}w )
Nr   r   r=   rL   r   r   rM   rN   rO   )r@   rA   r   r   r   rQ   r3   rP   r?   rR   rS   r4   rT   r?   rW   rY   rZ   s         r   rP   zVarFileInfo.toRaw  s    
!	$))$S]"Q&A:Chhtyy9		9:s3x'#c(2{{5$++t{{DJJG(RU``cffilll :s   Cc                 (    | j                         |k(  S r0   r\   r]   s     r   r_   zVarFileInfo.__eq__  r`   r   c                 R    |ddj                  d | j                  D              z  z   S )NzVarFileInfo([%s])r   c              3   2   K   | ]  }t        |        y wr0   r   r   s     r   r   z&VarFileInfo.__str__.<locals>.<genexpr>  s     7VSC7Vr   r   r   s     r   ri   zVarFileInfo.__str__  s&    +dii7VDII7V.VVVVr   c                      d| j                   z  S )Nzversioninfo.VarFileInfo(%r)r   rl   s    r   rm   zVarFileInfo.__repr__  s    ,tyy88r   r0   ro   rq   r+   r   r   r=   r=     s'    
m%W9r   r=   c                   8    e Zd ZdZd	dZd Zd Zd Zd
dZd Z	y)r   a  
    WORD  wLength;        // length of the version resource
    WORD  wValueLength;   // length of the Value member in the current
                          // VS_VERSION_INFO structure
    WORD  wType;          // 1 means text, 0 means binary
    WCHAR szKey[];        // Contains the Unicode string 'Translation'
                          // or a user-defined key string value
    WORD  Padding[];      //
    WORD  Value[];        // list of one or more values that are language
                          // and code-page identifiers
    Nc                 0    |xs d| _         |xs g | _        y r   r   r   s      r   r5   zVarStruct.__init__  r   r   c                 "   t        ||      \  }\  | _        | _        | _        | _        t        |      }t        d| j                  d      D ]A  }t        j                  d|||dz          d   }| j                  j                  |       |dz  }C |S )Nr   r   H)r9   r?   r|   rA   r   r-   rangerR   rx   r3   r<   )r4   r&   r>   r   rC   rY   s         r   r!   zVarStruct.fromRaw  s    EPQUWXEYBBDK*DJ	aLq$++Q/ 	A--T!AE]3A6CIIS!FA	 r   c           	         t        | j                        dz  | _        d| _        t	        | j
                        }dt        |      z   dz   }d}|dz  rd}|t        |      z   | j                  z   | _        dj                  | j                  D cg c]  }t        j                  d|       c}      }t        j                  d| j                  | j                  | j                        |z   dz   |z   |z   S c c}w )	Nr   r   rL   r   rM   rN   r   rO   )
r   r3   r|   rA   r   r   r?   rQ   rR   rS   r   s         r   rP   zVarStruct.toRaw  s    		NQ.
$))$S]"Q&A:Cs3x'$*;*;;hhC#C-CD{{5$++t/@/@$**MPXX[ffillorrr Ds   C3c                 (    | j                         |k(  S r0   r\   r]   s     r   r_   zVarStruct.__eq__%  r`   r   c                 <    d| j                   d| j                  dS )NzVarStruct('z', rh   r   r   s     r   ri   zVarStruct.__str__(  s    (,		499==r   c                 <    d| j                   d| j                  dS )Nzversioninfo.VarStruct(r   rh   r   rl   s    r   rm   zVarStruct.__repr__+  s    26))TYYGGr   rn   ro   rq   r+   r   r   r   r      s'    

s%>Hr   r   c                 0   ddl mc m} t        | d      5 }|j	                  |j                               }ddd       	 t              }t        |t              sJ dt        |             |S # 1 sw Y   :xY w# t        $ r}t        d      |d}~ww xY w)z
    Load the `VSVersionInfo` structure from its string-based (`VSVersionInfo.__str__`) serialization by reading the
    text from the file and running it through `eval()`.
    r   NrbzCFailed to deserialize VSVersionInfo from text-based representation!zALoaded incompatible structure type! Expected VSVersionInfo, got: )PyInstaller.utils.miscutilsmiscopenr~   readevalr   
ValueError
isinstancer    type)r   	miscutilsfpr   r'   es         r    load_version_info_from_text_filer   /  s     /.	h	 +	*+gDz
 dM* [
KDQUJ>Z[* K+ +  g^_effgs"    A/A; /A8;	BBBc                 :   t        |t              sJ t        j                  | d      }|j	                         }|j                          t        j                  | d      }t        j                  |t        j                  d   d|j                                t        j                  |d       |rat        j                  | d      }|j	                         }|j                          |s't        | d      5 }|j                  |       d d d        y y y # 1 sw Y   y xY w)NTr   r   r   r   ab)r   r    r   r   get_overlaycloser   BeginUpdateResourceUpdateResourcer   rP   EndUpdateResourcer   write)r#   r'   r   overlay_beforehdstoverlay_afterexefs          r    write_version_info_to_executabler  G  s    dM*** 
<4	0B^^%NHHJ''a8DD&"6"6|"DaVtQ'YY|t4(

 lD) +T

>*+ +  + +s   4DD)r   )rR   r   PyInstaller.compatr   r   r   r   r(   r-   r    r9   ry   r:   r8   r   r   r   r=   r   r   r  r+   r   r   <module>r     s      '2  # $PT PTf3J
 J
Z,< ,<^/J /Jd(J (JV0
-9 -9`,H ,H^0+r   