
    di                         d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
mZmZmZ d dlT d dlT d dlmZ ddej        de j        d	efd
Z G d d          ZdS )    N)b2hHexstr)StructArraythisInt32ubInt16ubInt8ub)*)Optionalfooin_fileout_zippc                 6   g d}|                                  }t          |          rqt          j        d|dd                   \  }}|                    |dz   ||dz
           z   dz   |dd|z                       |d|z   d         }t          |          odS dS )	zConvert an ICJ (Interoperable Java Card) file [back] to a CAP file.
    example usage:
        with io.open(sys.argv[1],"rb") as f, zipfile.ZipFile(sys.argv[2], "wb") as z:
        ijc_to_cap(f, z)
    )Header	DirectoryAppletImportConstantPoolClassMethodStaticFieldRefLocationExport
DescriptorDebugz!BHr      z
/javacard/   .capN)readlenstructunpackwritestr)r   r   r   TAGSbtagsizes          </home/jenkins/workspace/simtester-sanitize/pySim/javacard.py
ijc_to_capr+      s    - - -DA
a&& M%1Q300	T<SU3F:Aa$hKHHHafggJ a&&         c                   D   e Zd Z edez  dez  dez  dez  dez  dez  d edez  dez  dez            z  d	 ee          z            Z	 edez  dez  d
ez  d e
ej         edez  dez                      z            ZdefdZdefdZdefdZddedefdZdS )CapFiler(   r)   magicminor_versionmajor_versionflagspackageAIDpackage_namecountappletsinstall_method_offsetfilenamec           
         i | _         t          j        |          }|                                }t	          |          D ]\  }}|                                                    d          r6|                                                    d          st          d          |                                                    d          rK|                    d          d         	                    d          }|
                    |          | j         |<   dddd	d
ddddd	}|D ]5}|| j                                         vrt          d||         z            6d S )Nz.capxz;incompatible .cap file, extended .cap format not supported!r    /COMPONENT_HeaderCOMPONENT_DirectoryCOMPONENT_ImportCOMPONENT_ConstantPoolCOMPONENT_ClassCOMPONENT_MethodCOMPONENT_StaticFieldCOMPONENT_ReferenceLocationCOMPONENT_Descriptor)	r   r   r   r   r   r   r   r   r   zinvalid cap file, %s missing!)_CapFile__componentzipfileZipFilenamelist	enumeratelowerendswith
ValueErrorsplitremovesuffixr!   keys)selfr9   capcap_namelistikeyrequired_components	components           r*   __init__zCapFile.__init__A   sy     oh''||~~$\22 	; 	;KAx~~((11 `(..:J:J:S:ST[:\:\ ` !^___~~((00 ;nnS))"-::6BB(+(:(: % +=-B*<0H):*</F/L.DF F - 	c 	cI 0 5 5 7 777 !@CVW`Ca!abbb 8	c 	cr,   returnc                    | j         d         }|| j         d         z  }|| j         d         z  }d| j                                         v r|| j         d         z  }|| j         d         z  }|| j         d         z  }|| j         d         z  }d| j                                         v r|| j         d         z  }|| j         d	         z  }|| j         d
         z  }d| j                                         v r|| j         d         z  }|S )z(Get the executable loadfile as hexstringr   r   r   r   r   r   r   r   r   r   r   )rF   rP   )rQ   loadfiles     r*   get_loadfilezCapFile.get_loadfilec   s    #H-D$[11D$X..t',,....(22HD$W--D$X..D$]33t',,....(22HD$^44D$]334+002222(66Hr,   c                     | j                             | j        d                   }|d         pd}|dk    rt          d|z            |d         d         S )z!Get the loadfile AID as hexstringr   r/   r   l   = zKinvalid cap file, COMPONENT_Header lacks magic number (0x%08X!=0xDECAFFED)!r3   r4   )"_CapFile__header_component_compactparserF   rM   )rQ   headerr/   s      r*   get_loadfile_aidzCapFile.get_loadfile_aidw   s^    066t7G7QRRw$1Jjmrrsssi ''r,   r   indexc                    d| j                                         vrt          d          | j                            | j         d                   }||d         k    rt          d||d         fz            |d         |         d         S )zGet the applet AID as hexstringr   zOcan't get the AID, this cap file lacks the optional COMPONENT_Applet component!r6   zOcan't get the AID for applet with index=%u, this .cap file only has %u applets!r7   r4   )rF   rP   rM   "_CapFile__applet_component_compactr_   )rQ   rb   applets      r*   get_applet_aidzCapFile.get_applet_aid   s     4+002222nooo066t7G7QRR6'?""n#VG_56 7 7 7 i '..r,   N)r   )__name__
__module____qualname__r   r
   r	   r   LV	COptionalr^   r   r   r6   rd   strrX   bytesr\   r   ra   intrf    r,   r*   r.   r.   +   s{        "(f(.w(/(7(>(7(>(/(1&&9O9H9O9>r3C 3C )C )7yy}}(D	"F 	"F "(f(.w(/(1%%
FF5QS8KbcjKjEl El 3m 3m )m") ") c  c  c  c  cDe    ((& ( ( ( (/ /3 /v / / / / / /r,   r.   )r   )rG   r#   sysioosmocom.utilsr   r   	constructr   r   r   r   r	   r
   osmocom.constructosmocom.tlvr   rk   IOBaserH   rl   r+   r.   ro   r,   r*   <module>rw      s  &   



 				 % % % % % % % % C C C C C C C C C C C C C C C C         + + + + + + 	 GO     b/ b/ b/ b/ b/ b/ b/ b/ b/ b/r,   