U
    -^R.                     @   sH  d Z ddlZddlZdZdZdZdZdZdZd	Z	d
Z
dZdZdejfdejfdejfdejfdejfdejfdejfgZedejfg ZdejfdejfgZdejfdejfdejfdejfdejfgZdejfdejfdejfdejfdejfdejfgZdejfdejfgZdejfdejfdejd fdejfdejfdejfd ejfd!ejfd"ejfd#ejfdejfgZdejfdejfdejd fdejfdejfdejfd ejfd!ejfd"ejfd#ejfdejfgZed$ejfd%ejfg Zed&ejfd'ejfd%ejfd(ejfg Zd)d* Zd5d+d,Zd-d. Zd/d0 Zd6d1d2Zd3d4 Z dS )7a	  
This module contains function to analyse dynamic library
headers to extract system information

Currently only for MacOSX

Library file on macosx system starts with Mach-O or Fat field.
This can be distinguish by first 32 bites and it is called magic number.
Proper value of magic number is with suffix _MAGIC. Suffix _CIGAM means
reversed bytes order.
Both fields can occur in two types: 32 and 64 bytes.

FAT field inform that this library contains few version of library
(typically for different types version). It contains
information where Mach-O headers starts.

Each section started with Mach-O header contains one library
(So if file starts with this field it contains only one version).

After filed Mach-O there are section fields.
Each of them starts with two fields:
cmd - magic number for this command
cmdsize - total size occupied by this section information.

In this case only sections LC_VERSION_MIN_MACOSX (for macosx 10.13 and earlier)
and LC_BUILD_VERSION (for macosx 10.14 and newer) are interesting,
because them contains information about minimal system version.

Important remarks:
- For fat files this implementation looks for maximum number version.
  It not check if it is 32 or 64 and do not compare it with currently builded package.
  So it is possible to false report higher version that needed.
- All structures signatures are taken form macosx header files.
- I think that binary format will be more stable than `otool` output.
  and if apple introduce some changes both implementation will need to be updated.
    Nl   : l   ~u} l   : l   ~u l   z} l   m l   z} l   m $   2   magicZcputypeZ
cpusubtypeZfiletypencmdsZ
sizeofcmdsflagsZreserved	nfat_archoffsetsizeZaligncmdcmdsizeZsegname   ZvmaddrZvmsizeZfileoffZfilesizeZmaxprotZinitprotZnsectsversionZsdkplatformminosZntoolsc                 C   s0   | d> d@ | d> d@ B | d? d@ B | d? d@ B S )N   l      ~    i          )xr   r   6/usr/lib/python3/dist-packages/wheel/macosx_libfile.pyswap32   s    



r   c                 C   s   |d kr|   }n
| | tj| ttjj}|tt	t
tfkrjtjdkrZtj}ntj}t|}ntj}| | ||fS )Nlittle)tellseekctypesc_uint32from_buffer_copyreadsizeofvalue	FAT_CIGAMFAT_CIGAM_64MH_CIGAMMH_CIGAM_64sys	byteorderZBigEndianStructureZLittleEndianStructurer   Z	Structure)lib_filer   magic_number	BaseClassr   r   r   get_base_class_and_magic_number   s    




r*   c                 C   s   |  |t| S )N)r   r   r   r   )Zstruct_classr'   r   r   r   	read_data   s    r+   c           	   
      sh  t | dRtd\}}|ttttfkr:W 5 Q R  d S |ttfkrG dd d|}t|}|tkr|G dd d| nG dd d|  fdd	t|j	D }g }|D ]<}z"t
|j}|d k	r|| W q tk
r   Y qX qt|dkrt|W  5 Q R  S W 5 Q R  d S n<zt
dW W  5 Q R  S  tk
rX   Y W 5 Q R  d S X W 5 Q R X d S )
Nrbr   c                   @   s   e Zd ZeZdS )z4extract_macosx_min_system_version.<locals>.FatHeaderN)__name__
__module____qualname__fat_header_fields_fields_r   r   r   r   	FatHeader   s   r2   c                   @   s   e Zd ZeZdS z2extract_macosx_min_system_version.<locals>.FatArchN)r-   r.   r/   fat_arch_fieldsr1   r   r   r   r   FatArch  s   r5   c                   @   s   e Zd ZeZdS r3   )r-   r.   r/   fat_arch_64_fieldsr1   r   r   r   r   r5     s   c                    s   g | ]}t  qS r   )r+   ).0_r5   r'   r   r   
<listcomp>
  s     z5extract_macosx_min_system_version.<locals>.<listcomp>)openr*   	FAT_MAGICFAT_MAGIC_64MH_MAGICMH_MAGIC_64r"   r+   ranger   read_mach_headerr   append
ValueErrorlenmax)	Zpath_to_libr)   r(   r2   Z
fat_headerZfat_arch_listZversions_listZelr   r   r9   r   !extract_macosx_min_system_version   s4    
rF   c                 C   s  |dk	r|  | t| \}}|tkr*dnd}G dd d|}|dkrXG dd d|}nG dd d|}t|| }t|jD ]}|  }	t|| }
|  |	 |
jtkrG d	d
 d
|}t|| }t	|j
  S |
jtkrG dd d|}t|| }t	|j  S |  |	|
j  q|q|dS )z
    This funcition parse mach-O header and extract
    information about minimal system version

    :param lib_file: reference to opened library file with pointer
    NZ32Z64c                   @   s   e Zd ZeZdS )z%read_mach_header.<locals>.SegmentBaseN)r-   r.   r/   segment_base_fieldsr1   r   r   r   r   SegmentBase.  s   rH   c                   @   s   e Zd ZeZdS z$read_mach_header.<locals>.MachHeaderN)r-   r.   r/   mach_header_fieldsr1   r   r   r   r   
MachHeader3  s   rK   c                   @   s   e Zd ZeZdS rI   )r-   r.   r/   mach_header_fields_64r1   r   r   r   r   rK   8  s   c                   @   s   e Zd ZeZdS )z+read_mach_header.<locals>.VersionMinCommandN)r-   r.   r/   version_min_command_fieldsr1   r   r   r   r   VersionMinCommandA  s   rN   c                   @   s   e Zd ZeZdS )z&read_mach_header.<locals>.VersionBuildN)r-   r.   r/   build_version_command_fieldsr1   r   r   r   r   VersionBuildG  s   rP   )r   r*   r>   r+   r@   r   r   r
   LC_VERSION_MIN_MACOSXparse_versionr   LC_BUILD_VERSIONr   r   )r'   r   Z
base_classr(   ZarchrH   rK   Zmach_headerZ_iposZsegment_baserN   version_inforP   r   r   r   rA   "  s.    






rA   c                 C   s*   | d@ d? }| d@ d? }| d@ }|||fS )Nl      r   r   r   r   r   )r   r   yzr   r   r   rR   Q  s    rR   )N)N)!__doc__r   r%   r<   r!   r=   r"   r>   r#   r?   r$   rQ   rS   r   Zc_intrJ   rL   r0   r4   Zc_uint64r6   rG   Zc_charZsegment_command_fieldsZsegment_command_fields_64rM   rO   r   r*   r+   rF   rA   rR   r   r   r   r   <module>   s   %   	                
,
/