ó
^ö&Rc           @   sÅ  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Td d d d d d d	 d
 g Z d d d d d g Z d d g Z	 d d d g Z
 d d d d d d d d g Z d f  d „  ƒ  YZ d f  d  „  ƒ  YZ d! f  d" „  ƒ  YZ d# f  d$ „  ƒ  YZ d% e f d& „  ƒ  YZ d' e f d( „  ƒ  YZ d) e f d* „  ƒ  YZ d+ e f d, „  ƒ  YZ d- f  d. „  ƒ  YZ d/ e f d0 „  ƒ  YZ d1 e f d2 „  ƒ  YZ d3 e f d4 „  ƒ  YZ d5 e f d6 „  ƒ  YZ d7 e f d8 „  ƒ  YZ d9 e f d: „  ƒ  YZ d; e f d< „  ƒ  YZ d= e f d> „  ƒ  YZ e d? k rÁe e j ƒ d@ k r\e e j dA ƒ Z  n  e e j ƒ dB k r¾xG e  j! e j d@ ƒ D]/ Z" dC j# e" ƒ GHy e e" ƒ Wn
 qˆn XqˆWn  n  d S(D   iÿÿÿÿN(   t   *t   cmapt   headt   hheat   hmtxt   maxpt   namet   posts   OS/2s   cvt t   fpgmt   glyft   locat   preps   CFF t   VORGt   EBDTt   EBLCt   EBSCt   gaspt   hdmxt   kernt   LTSHt   PCLTt   VDMXt   vheat   vmtxt   TTFontc           B   s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s  t  | d ƒ } | |  _ t | ƒ |  _ i  |  _ xE t d t |  j j ƒ ƒ D]( } t | |  j ƒ } | |  j | j	 <qL Wxv t
 |  j j ƒ  ƒ D]_ } d j |  j | j	 ƒ GH|  j | j	 d k rÙ |  j | j | ƒ qŽ |  j | j | ƒ qŽ Wd GHd GH| j |  j d j ƒ t | ƒ |  j d _ d GH| j |  j d j ƒ t | ƒ |  j d _ d	 GHt | |  j d
 ƒ |  j d
 _ d |  j k rÏd GH|  j d j j } |  j d j j d } | r×|  j d j d n |  j d j d } | | k st d ƒ ‚ d j | ƒ GH| j |  j d j ƒ t | | | ƒ |  j d _ d GHg  |  j d _ |  j d j }	 |  j d j |	 }
 | j |	 ƒ |  j d j j } t | ƒ |  j d j j d k st d ƒ ‚ nP d |  j k rd GH| j |  j d j ƒ t | ƒ |  j d _ n t d ƒ ‚ d GHd GHxÎt
 |  j j ƒ  ƒ D]·} | d k r—d GH| j |  j d j ƒ t | |  j d j d ƒ |  j d _ n  | d k råd GH| j |  j d j ƒ t | |  j d j ƒ |  j d _ n  | d k r&d GH| j |  j d j ƒ t  | ƒ |  j d _ n  | d  k rgd! GH| j |  j d  j ƒ t! | ƒ |  j d  _ n  | d" k r¨d# GH| j |  j d" j ƒ t" | ƒ |  j d" _ n  | d$ k r?d% GH| j |  j d$ j ƒ t# | |  j d$ j ƒ |  j d$ _ q?q?Wd& GHd  S('   Nt   rbi    s   [*] Table {}R   s!   [*] >> Parsing required tables <<s   [*] Parsing table heads   [*] Parsing table maxpR   s   [*] Parsing table cmapR   R	   s   [*] Parsing loca tablei   R
   i   i   s   weird number of Glyphss   [D] 	 Number of glyphs {}s   [*] Parsing table glyfs   Weird number of glyph offsetss   CFF s   [*] Parsing table CFFs   Can't find glyf nor CFF tables*   [*] >> Finished parsing required tables <<s!   [*] >> Parsing optional tables <<s   cvt s   [*] Parsing table CVT R   s   [*] Parsing table prepR   s   [*] Parsing table EBDTR   s   [*] Parsing table EBLCR   s   [*] Parsing table EBSCR   s   [*] Parsing table fpgms*   [*] >> Finished parsing optional tables <<($   t   opent   filenamet   FontOffsetTablet   fontOffsetTablet   fontTableDirectoriest   ranget   intt	   numTablest   FontTableDirectoryt   tagt   sortedt   keyst   formatt   doHeadChecksumt
   doChecksumt   seekt   offsett	   HeadTablet   tablet	   MaxpTablet	   CmapTablet   indexToLocFormatt	   numGlyphst   lengtht   AssertionErrort	   LocaTablet   offsetst   lent   CFFTablet	   Exceptiont   CVTTablet	   PrepTablet	   EBDTTablet	   EBLCTablet	   EBSCTablet	   FpgmTable(   t   selfR   t   font_handlet   iR,   t   tt   isLongt   numberOfGlyphst   nort   startOffsett	   endOffsetR4   (    (    s   parsers/TTF.pyt   __init__!   sŒ    		" .2*&*c         C   sB   |  j  j ƒ  x. t |  j j ƒ  ƒ D] } |  j | j ƒ  q# Wd  S(   N(   R   t   dumpR$   R   R%   (   R>   R@   (    (    s   parsers/TTF.pyRH   Ã   s    c         C   s   t  |  j d ƒ j ƒ  } | } xz t |  j j ƒ  ƒ D]c } |  j | } | j d k r4 d j | j ƒ GH| j | ƒ } | j	 | ƒ } | j
 | ƒ } q4 q4 Wd t j j |  j ƒ j d ƒ d d d j d	 „  t d
 ƒ Dƒ ƒ d } t  | d ƒ j | ƒ d  S(   NR   R	   s   [*] Fuzzing directory {}s   testcases//s   .ttfi    t   -t    c         s   s(   |  ] } t  j t j t j ƒ Vq d  S(   N(   t   randomt   choicet   stringt   ascii_lowercaset   digits(   t   .0t   x(    (    s   parsers/TTF.pys	   <genexpr>è   s    i   t   wb(   R   R   t   readR$   R   R%   R#   R&   t   fuzzDirectoryt   doFileBufferChecksumt   doFileBufferHeadChecksumt   ost   patht   basenamet   splitt   joinR   t   write(   R>   t   fileInMemoryt   fileModR@   R,   t   fname(    (    s   parsers/TTF.pyt   fuzzDirectoriesRadamsaÎ   s    	Dc         C   s  t  |  j d ƒ j ƒ  } | } t } xâ t |  j j ƒ  ƒ D]Ë } |  j | } | j d k r: d j | j ƒ GHd } xh | j	 D]] } | d 7} t
 | j	 ƒ | d k  r¬ q} n  | j d  k rÁ q} n  d GH| j | ƒ } d GHq} W| j | ƒ } | j | ƒ } t } q: q: W| | f S(	   NR   R	   s*   [*] Fuzzing bytecode found in directory {}iÿÿÿÿi   i   t   AAAt   BBB(   R   R   RS   t   FalseR$   R   R%   R#   R&   R,   R5   t   glypht   Nonet   fuzzBytecodeRU   RV   t   True(   R>   R]   R^   t   isFontFuzzedR@   R,   RA   (    (    s   parsers/TTF.pyt   fuzzFontBytecodeð   s*    
	c   	      C   sQ  d |  j  k s t d ƒ ‚ t t |  j d ƒ j ƒ  ƒ } |  j  d j } | |  j  d j } d j | | ƒ GHt	 j	 ƒ  t	 j	 ƒ  d } t	 j
 t j t | | ƒ | ƒ ƒ d } d j | | | ƒ GHxH t | ƒ D]: } t	 j
 d ƒ } t	 j
 | | ƒ } d	 | | | | <qÔ Wd
 j | ƒ } |  j  d j | ƒ } |  j  d j | ƒ } | S(   Ns   CFF s   [E] No CFF table foundR   s   [D] CFF range is {} <-> {}iô  i   s   Changing {} bytes out of {}i   s   %cRJ   (   R   R2   t   listR   R   RS   R*   R1   R&   RK   t	   randranget   matht   ceilt   floatR   R[   RU   RV   (	   R>   R]   t   startt   endt
   FuzzFactort	   numwritest   jt   rbytet   rn(    (    s   parsers/TTF.pyt   fuzzCffTableBitFlipping)  s     *
c   
      C   sƒ  t  t |  j d ƒ j ƒ  ƒ } t j ƒ  t j ƒ  d } t j t j t t	 | ƒ ƒ | ƒ ƒ d } d j
 | t	 | ƒ ƒ GHxF t | ƒ D]8 } t j d ƒ } t j t	 | ƒ ƒ } d | | | <qˆ Wd j | ƒ } x9 t |  j j ƒ  ƒ D]" } |  j | } | j | ƒ } qé W| j | ƒ } d t j j |  j ƒ j d	 ƒ d
 d d j d „  t d ƒ Dƒ ƒ d	 }	 t |	 d ƒ j | ƒ d  S(   NR   iô  i   s   Changing {} bytes out of {}i   s   %cRJ   s   testcases//s   .ttfi    RI   c         s   s(   |  ] } t  j t j t j ƒ Vq d  S(   N(   RK   RL   RM   RN   RO   (   RP   RQ   (    (    s   parsers/TTF.pys	   <genexpr>t  s    i   RR   (   Rj   R   R   RS   RK   Rk   Rl   Rm   Rn   R5   R&   R   R[   R$   R   R%   RU   RV   RW   RX   RY   RZ   R\   (
   R>   R]   Rq   Rr   Rs   Rt   Ru   R@   R,   R_   (    (    s   parsers/TTF.pyt   fuzzDirectoriesBitFlippingU  s     ,D(   t   __name__t
   __module__RG   RH   R`   Ri   Rv   Rw   (    (    (    s   parsers/TTF.pyR      s   	¢		"	9	,R   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   sk   | j  d ƒ |  _ t d | j  d ƒ ƒ d |  _ | j  d ƒ |  _ | j  d ƒ |  _ | j  d ƒ |  _ d  S(   Ni   s   >Hi   i    (   RS   t   SFNT_Versiont   unpackR!   t   searchRanget   entrySelectort
   rangeShift(   R>   t   handle(    (    s   parsers/TTF.pyRG   €  s
    c         C   s’   d GHd j  t d |  j ƒ d ƒ GHd j  |  j ƒ GHd j  t d |  j ƒ d ƒ GHd j  t d |  j ƒ d ƒ GHd	 j  t d |  j ƒ d ƒ GHd  S(
   Ns   FontOffsetTable:s   	SFNT_Version	 {}s   >ii    s   	numTables	 {}s   	searchRange	 {}s   >Hs   	entrySelector	 {}s   	rangeShift	 {}(   R&   R{   Rz   R!   R|   R}   R~   (   R>   (    (    s   parsers/TTF.pyRH   ‡  s    (   Rx   Ry   RG   RH   (    (    (    s   parsers/TTF.pyR   ~  s   	R"   c           B   sJ   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z RS(   c         C   s”   | j  ƒ  |  _ | j d ƒ |  _ t d | j d ƒ ƒ d |  _ t d | j d ƒ ƒ d |  _ t d | j d ƒ ƒ d |  _ d  |  _	 | |  _
 d  S(   Ni   s   >Li    (   t   tellt    fontDirectoryOffsetWithinTheFileRS   R#   R{   t   checksumR*   R1   Re   R,   R   (   R>   R   R   (    (    s   parsers/TTF.pyRG   “  s    	c         C   s  |  j  d  k s t d ƒ ‚ d } t | d ƒ } | |  j |  j |  j !} t | ƒ |  j k sk t d ƒ ‚ | j | ƒ | j ƒ  t	 j
 d | g ƒ t | ƒ  } x/ t | ƒ t | ƒ k  rÒ | t d d ƒ 7} q¤ W| d |  j !| | |  j |  j } t | ƒ t | ƒ k st ‚ | S(	   Ns   [E] No table for this directoryt	   tmp_tableRR   s   Couldn't read enough bytess   radamsa-0.3.exes   >bi   i    (   R,   Re   R2   R   R*   R1   R5   R\   t   closet
   subprocesst   check_outputt   pack(   R>   R]   t	   file_nameRA   t   datat   outputt
   final_data(    (    s   parsers/TTF.pyRT   ¤  s    !
&c         C   sÃ   d } | j  |  j ƒ |  j d d @} | j | ƒ } xH t d t | ƒ d ƒ D]. } t d | | | d !ƒ d } | | 7} qO Wd | @} | |  j k r± d j | |  j ƒ GHn  d j | ƒ GH| S(	   Ni    i   i   s   >Il   ÿÿ s+   
	Checksum error, calculated: {} - read: {}s   [*]	 checksum: {}iüÿÿÿ(	   R)   R*   R1   RS   R   R5   R{   R‚   R&   (   R>   R   t
   total_datat   checksumLengthR,   R@   R‰   R‹   (    (    s   parsers/TTF.pyR(   Â  s    
c   	      C   sã   d } |  j  d d	 @} | |  j |  j | !} xH t d t | ƒ d ƒ D]. } t d | | | d !ƒ d } | | 7} qG Wd | @} d j | ƒ GH| d |  j d !t d | ƒ | |  j d } t | ƒ t | ƒ k sß t ‚ | S(
   Ni    i   i   s   >Il   ÿÿ s   [*]	 New checksum {}s   >Li   iüÿÿÿ(	   R1   R*   R   R5   R{   R&   R   R‡   R2   (	   R>   R]   RŒ   R   R,   R@   R‰   t   new_checksumt   newFileInMemory(    (    s   parsers/TTF.pyRU   Ý  s    
c         C   s  |  j  d j } |  j  d j } d j | | ƒ GHd } |  j  d j d d @} | | d  t d d ƒ | | d } | | | | !} d j t | ƒ ƒ GHx{ t d t | ƒ d	 ƒ D]a } | d k rí d } t d | | | d	 !ƒ d }	 n t d | | | d	 !ƒ d } | | 7} q´ Wd
 | @}
 | d | d	 !t d |
 ƒ | | d } d j |
 | d	 ƒ GHd } xH t d t | ƒ d	 ƒ D]. } t d | | | d	 !ƒ d } | | 7} qW| d
 @} d | d
 @} d j | ƒ GH| | d  t d | ƒ | | d } d j | | d ƒ GH| S(   NR   sA   [D] Head headDirectoryOffsetWithinTheFile {} - headTableOffset {}i    i   i   s   >Ii   s   [D] table size {}i   l   ÿÿ s   >Ls   [D] head table checksum {} @ {}l   º/ac s"   [D] head newCheckSumAdjustement {}s+   [D] updating newCheckSumAdjustement {} @ {}iüÿÿÿ(	   R   R   R*   R&   R1   R‡   R5   R   R{   (   R>   R]   t    headDirectoryOffsetWithinTheFilet   headTableOffsetRŒ   R   R,   R@   R‰   t   checkSumAdjustementt   headTableChecksumR   t   newCheckSumAdjustement(    (    s   parsers/TTF.pyRV   ÷  s<    !

c         C   sä  d } | j  |  j ƒ |  j d d @} | j | ƒ } x{ t d t | ƒ d ƒ D]a } | d k rˆ d } t d | | | d !ƒ d } n t d | | | d !ƒ d } | | 7} qO Wd | @} | |  j k rä d j | |  j ƒ GHn  d j | ƒ GH| j  d d	 ƒ | j	 ƒ  }	 | j  d ƒ d } | j |	 ƒ }
 |
 |  j d  t
 d d ƒ |
 |  j d
 }
 xH t d t |
 ƒ d ƒ D]. } t d |
 | | d !ƒ d } | | 7} qvW| d @} d | d @} | | k ràd j | | ƒ GHn  | S(   s  
        To calculate the checkSum for the 'head' table which itself includes the checkSumAdjustment entry for the entire font, do the following:

        1] Set the checkSumAdjustment to 0.
        2] Calculate the checksum for all the tables including the 'head' table and enter that value into the table directory.
        3] Calculate the checksum for the entire font.
        4] Subtract that value from the hex value B1B0AFBA.
        5] Store the result in checkSumAdjustment.

        The checkSum for the head table which includes the checkSumAdjustment entry for the entire font is now incorrect.
        That is not a problem. Do not change it. An application attempting to verify that the 'head' table has not changed 
        should calculate the checkSum for that table by not including the checkSumAdjustment value, and compare the result 
        with the entry in the table directory. 
        i    i   i   i   s   >Il   ÿÿ s)   checksum error, calculated: {} - read: {}s   [*]	 checksum: {}i   i   l   º/ac s3   checkSumAdjustement error, calculated {} - read: {}iüÿÿÿ(   R)   R*   R1   RS   R   R5   R{   R‚   R&   R€   R‡   (   R>   R   RŒ   R   R,   R@   R‰   R’   R‹   t   end_of_filet   font(    (    s   parsers/TTF.pyR'   9  s8    !
-
RJ   c         C   sV   d j  | ƒ GHd j  |  j ƒ GHd j  |  j ƒ GHd j  |  j ƒ GHd j  |  j ƒ GHd  S(   Ns   FontTableDirectory {}s   	tag	 {}s   	checksum {}s   	offset	 {}s   	length	 {}(   R&   R#   R‚   R*   R1   (   R>   t   index(    (    s   parsers/TTF.pyRH   …  s
    (	   Rx   Ry   RG   RT   R(   RU   RV   R'   RH   (    (    (    s   parsers/TTF.pyR"   ‘  s   					B	Lt	   FontTablec           B   s   e  Z RS(    (   Rx   Ry   (    (    (    s   parsers/TTF.pyR˜   ‘  s   R6   c           B   s7   e  Z d  „  Z d d d „  ƒ  YZ d d d „  ƒ  YZ RS(   c         C   s   t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ |  j | ƒ |  _ x" |  j j D] } d j	 | ƒ GHq› W|  j | ƒ |  _
 |  j | ƒ |  _ x" |  j j D] } d j	 | ƒ GHqä Wd  S(   Ns   >Bi   i    s   [D] 	PS name: {}s   [D] 	String name: {}(   R{   RS   t   headerMajort   headerMinort   headerHdrSizet   headerOffSizet   IndexR   R‰   R&   t   topDictIndext   stringIndex(   R>   R   t   n(    (    s   parsers/TTF.pyRG   ˜  s    t   Dictc           B   s   e  Z d  „  Z RS(   c         C   s   d  S(   N(    (   R>   R   (    (    s   parsers/TTF.pyRG   Õ  s    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR¡   Ò  s   R   c           B   s   e  Z d  „  Z RS(   c         C   s  t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ g  |  _ xð t d |  j d ƒ D]Ø } |  j d k rœ |  j j t  d | j d ƒ ƒ d ƒ q^ |  j d k rÔ |  j j t  d | j d ƒ ƒ d ƒ q^ |  j d k rò t d ƒ ‚ q^ |  j d k r*|  j j t  d	 | j d ƒ ƒ d ƒ q^ t d
 ƒ ‚ q^ W| j ƒ  d |  _	 g  |  _
 xj t d |  j ƒ D]V } | j |  j	 |  j | ƒ |  j | d |  j | } |  j
 j | j | ƒ ƒ qiW| j ƒ  } | j |  j t |  j ƒ d |  j	 ƒ | j ƒ  | k st d ƒ ‚ d  S(   Ns   >Hi   i    s   >Bi   i   s   TODO: unpack 3 bytesi   s   >Is   Invalid offSizes   offsets screwed(   R{   RS   t   countt   offSizet   offsetArrayR   t   appendR7   R€   RE   R‰   R)   R5   R2   (   R>   R   R@   t   sizet   first(    (    s   parsers/TTF.pyRG   Û  s,    	)))	((   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR   Ù  s   (    (    (   Rx   Ry   RG   R¡   R   (    (    (    s   parsers/TTF.pyR6   –  s   	:R=   c           B   s   e  Z d  „  Z RS(   c         C   sM   g  |  _  x= t d | ƒ D], } |  j  j t d | j d ƒ ƒ d ƒ q Wd  S(   Ni    s   >Bi   (   t   instructionsR   R¥   R{   RS   (   R>   R   t   numberOfInstructionsR@   (    (    s   parsers/TTF.pyRG     s    	(   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR=     s   R:   c           B   s   e  Z d  „  Z RS(   c         C   sJ   t  d | j d ƒ ƒ d |  _ |  j d k sF t d j |  j ƒ ƒ ‚ d  S(   Ns   >li   i    i   s$   [E] Wrong version for EBDT table: {}(   R{   RS   t   versionR2   R&   (   R>   R   (    (    s   parsers/TTF.pyRG     s    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR:     s   R;   c           B   s$   e  Z d  „  Z d d d „  ƒ  YZ RS(   c         C   sr   t  d | j d ƒ ƒ d |  _ |  j d k sF t d j |  j ƒ ƒ ‚ t  d | j d ƒ ƒ d |  _ g  |  _ d  S(   Ns   >li   i    i   s$   [E] Wrong version for EBLC table: {}s   >L(   R{   RS   Rª   R2   R&   t   numSizest   bitmapSizeTables(   R>   R   (    (    s   parsers/TTF.pyRG   &  s    't   BitmapSizeTablec           B   s   e  Z d  „  Z RS(   c         C   sX  t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t | ƒ |  _ t | ƒ |  _ t  d | j d ƒ ƒ d |  _	 t  d | j d ƒ ƒ d |  _
 t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ d  S(   Ns   >Li   i    s   >Wi   s   >Bi   (   R{   RS   t   indexSubTableArrayOffsett   indexTablesSizet   numberOfIndexSubTablest   colorReft   SbitLineMetricst   horit   vertt   startGlyphIndext   endGlyphIndext   ppemXt   ppemYt   bitDeptht   flags(   R>   R   (    (    s   parsers/TTF.pyRG   0  s    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR­   .  s   (    (   Rx   Ry   RG   R­   (    (    (    s   parsers/TTF.pyR;   $  s   	R²   c           B   s   e  Z d  „  Z RS(   c         C   sx  t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _	 t  d t j d ƒ ƒ d |  _
 t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ t  d t j d ƒ ƒ d |  _ d  S(   Ns   >bi   i    s   >B(   R{   R   RS   t   ascendert	   descendert   widthMaxt   caretSlopeNumeratort   caretSlopeDenominatort   caretOffsett   minOriginSBt   minAdvancedSBt   maxBeforeBLt
   minAfterBLt   pad1t   pad2(   R>   (    (    s   parsers/TTF.pyRG   E  s    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR²   C  s   R<   c           B   s$   e  Z d  „  Z d d d „  ƒ  YZ RS(   c         C   s¥   t  d t j d ƒ ƒ d |  _ |  j d k sF t d j |  j ƒ ƒ ‚ t  d t j d ƒ ƒ d |  _ g  |  _ x0 t d |  j ƒ D] } |  j j	 t
 t ƒ ƒ q Wd  S(   Ns   >li   i    i   s$   [E] Wrong version for EBLC table: {}s   >L(   R{   R   RS   Rª   R2   R&   R«   t   bitmapScaleTableR   R¥   t   BitmapScaleTable(   R>   R@   (    (    s   parsers/TTF.pyRG   ^  s    '	RÈ   c           B   s   e  Z d  „  Z RS(   c         C   sÂ   t  | ƒ |  _ t  | ƒ |  _ t | j d | j d ƒ ƒ ƒ d |  _ t | j d | j d ƒ ƒ ƒ d |  _ t | j d | j d ƒ ƒ ƒ d |  _ t | j d | j d ƒ ƒ ƒ d |  _ d  S(   Ns   >Bi   i    (	   R²   R³   R´   R{   RS   R·   R¸   t   substitutePpemXt   substitutePpemY(   R>   R   (    (    s   parsers/TTF.pyRG   j  s    ((((   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyRÈ   h  s   (    (   Rx   Ry   RG   RÈ   (    (    (    s   parsers/TTF.pyR<   [  s   	
R3   c           B   s   e  Z d  Z d „  Z RS(   sá   
    Offsets are relative to the beginning of the glyphData.
    In order to compute the length of the last glyph element, 
    there is an extra entry after the last valid index.

    Index zero is the missing character
    c         C   s¦   | |  _  g  |  _ |  j  r^ x„ t d | ƒ D], } |  j j t d | j d ƒ ƒ d ƒ q+ WnD xA t d | ƒ D]0 } |  j j t d | j d ƒ ƒ d d ƒ qn Wd  S(   Ni    s   >Li   s   >Hi   (   RB   R4   R   R¥   R{   RS   (   R>   R   RB   t   numberOfRecordsR@   (    (    s   parsers/TTF.pyRG   „  s    			-(   Rx   Ry   t   __doc__RG   (    (    (    s   parsers/TTF.pyR3   {  s   R8   c           B   s   e  Z d  „  Z RS(   c         C   sI   g  |  _  x9 t d | ƒ D]( } |  j  j t d | j d ƒ ƒ ƒ q Wd  S(   Ni    s   >hi   (   t   valuesR   R¥   R{   RS   (   R>   R   t   numberOfElementsR@   (    (    s   parsers/TTF.pyRG   ™  s    	(   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR8   —  s   R9   c           B   s   e  Z d  „  Z RS(   c         C   sI   g  |  _  x9 t d | ƒ D]( } |  j  j t d | j d ƒ ƒ ƒ q Wd  S(   Ni    s   >Bi   (   R¨   R   R¥   R{   RS   (   R>   R   R©   R@   (    (    s   parsers/TTF.pyRG   ¨  s    	(   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR9   ¦  s   R-   c           B   s   e  Z d  „  Z RS(   c         C   sÕ  t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _	 t  d | j d ƒ ƒ d |  _
 t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ d  S(   Ns   >li   i    s   >Hi   (   R{   RS   t   tableVersionNumberR0   t	   maxPointst   maxContourst   maxCompositePointst   maxCompositeContourst   maxZonest   maxTwilightPointst
   maxStoraget   maxFunctionDefst   maxInstructionDefst   maxStackElementst   maxSizeOfInstructionst   maxComponentElementst   maxComponentDepth(   R>   R   (    (    s   parsers/TTF.pyRG   ²  s    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR-   °  s   R+   c           B   s   e  Z d  „  Z RS(   c         C   s  t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _	 t  d	 | j d ƒ ƒ d |  _
 t  d	 | j d ƒ ƒ d |  _ t  d	 | j d ƒ ƒ d |  _ t  d	 | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d	 | j d ƒ ƒ d |  _ t  d	 | j d ƒ ƒ d |  _ t  d	 | j d ƒ ƒ d |  _ d  S(
   Ns   >li   i    s   >Ls   >Hi   s   >Qi   s   >h(   R{   RS   RÏ   t   fontRevisionR’   t   magicNumberRº   t
   unitsPerEmt   createdt   modifiedt   xMint   yMint   xMaxt   yMaxt   macStylet   lowestRecPPEMt   fontDirectionHintR/   t   glyphDataFormat(   R>   R   (    (    s   parsers/TTF.pyRG   Æ  s"    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR+   Ä  s   t
   GlyphTablec           B   sf   e  Z d  „  Z d „  Z d d
 d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d d d	 „  ƒ  YZ RS(   c         C   sˆ   |  j  | ƒ |  _ |  j j d k r5 d GHd  |  _ nO |  j j d k  rg d GH|  j | |  j ƒ |  _ n d GH|  j | |  j ƒ |  _ d  S(   Ni    s   [*] 		 lasts   [*] 		 composites   [*] 		 simple(   t   GlyphTableHeadert   headert   numberOfCountoursRe   Rd   t   CompositeGlypht   SimpleGlyph(   R>   R   (    (    s   parsers/TTF.pyRG   á  s    c         C   sÉ  d GH|  j  j d  k s) |  j  j d  k r- | S|  j  j } |  j  j } d j | | ƒ GH| | | !} t | ƒ | | k s… t d ƒ ‚ t } | rd } t | d ƒ } | j	 | ƒ | j
 ƒ  t j d | g ƒ t | ƒ  } xªt | ƒ t | ƒ k  r| t d d ƒ 7} qß Wnxg  } t | ƒ }	 d	 }
 d	 } d	 } xÐ |
 |	 k  rt j d
 ƒ d d	 k rËt t j d ƒ ƒ rËt j |	 |
 ƒ } x, t d	 | ƒ D] } | j | |
 | ƒ q•W|
 | 7}
 | | 7} q8t d t j d
 ƒ ƒ } | j | ƒ |
 d 7}
 | d 7} q8Wt | ƒ | | k s$t ‚ d j t | ƒ | | ƒ GHt | ƒ t | ƒ k szt d j t | ƒ t | ƒ ƒ ƒ ‚ d j | ƒ } | d	 | !| | | } t | ƒ t | ƒ k sÀt ‚ d GH| S(   Ns   [DD] Start fuzz GLYPHs'   [D] 	 fuzzing between offsets {} and {}s   Couldn't read enough bytesRƒ   RR   s   radamsa-0.3.exes   >bi   i    i   i   i   s   >Bs,   [D] 	 bytecode size {} - random {} copied {}s   data: {} output: {}RJ   s   [DD] Return fuzz glyph(   Rd   t   bytecodeStartFileOffsetRe   t   bytecodeEndFileOffsetR&   R5   R2   Rc   R   R\   R„   R…   R†   R‡   RK   Rk   t   boolt   getrandbitsR   R¥   R[   (   R>   R]   RE   RF   R‰   t   radamsaRˆ   RA   RŠ   t   lR@   t
   byteCopiedt
   byteRandomRs   t   eR‹   (    (    s   parsers/TTF.pyRf   ô  sR    $"
.

<Rë   c           B   s   e  Z d  „  Z RS(   c         C   sŸ   t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ d  S(   Ns   >hi   i    (   R{   RS   Rí   Râ   Rã   Rä   Rå   (   R>   R   (    (    s   parsers/TTF.pyRG   L  s
    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyRë   J  s   Rî   c           B   s$   e  Z d  „  Z d d d „  ƒ  YZ RS(   c         C   sG  d  |  _ d  |  _ g  |  _ x%t rB|  j | ƒ } |  j j | ƒ | j ƒ  s | j ƒ  r?t	 d | j
 d ƒ ƒ d |  _ d j |  j ƒ GH|  j d k r¬ | j ƒ  |  _ n  g  |  _ yG x@ t d |  j ƒ D], } |  j j t	 d | j
 d ƒ ƒ d ƒ qË WWn t k
 r} d GHn X|  j d k r8| j ƒ  |  _ n  Pq?q q Wd  S(   Ns   >Hi   i    s   [D] 		 number of instruction {}s   >bi   s.   [E] 	 Probably read a wrong instruction number(   Re   Rð   Rñ   t
   componentsRg   t   CompositeGlyphComponentR¥   t   moreComponentst   weHaveInstructionsR{   RS   t   numInstrR&   R€   t   instrR   R7   (   R>   R   Rì   t	   componentR@   Rø   (    (    s   parsers/TTF.pyRG   ]  s*    					.	Rú   c           B   sq   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z e d
 „  ƒ Z RS(   c         C   s±  t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ |  j rˆ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ n t  d | j d ƒ ƒ d |  _ |  j ƒ  rÕ t  d | j d ƒ ƒ d |  _	 nØ |  j
 ƒ  r"t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ n‹ |  j ƒ  r­t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ n  d  S(   Ns   >Hi   i    s   >h(   R{   RS   Rº   t
   glyphIndext   arg1And2AreWordst	   argument1t	   argument2t   arg1and2t   weHaveAScalet   scalet   weHaveAnXAndYScalet   xscalet   yscalet   weHaveATwoByTwot   scale01t   scale10(   R>   R   (    (    s   parsers/TTF.pyRG   ˆ  s     	"""c         C   s   |  j  |  j d ƒ S(   Ni    (   t   testBitRº   (   R>   (    (    s   parsers/TTF.pyR  ¨  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyt   argsAreXYValues°  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyt   roundXYToGrid·  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyR  ¿  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyRû   Ç  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyR  Î  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyR
  Ö  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyRü   Þ  s    c         C   s   |  j  |  j d ƒ S(   Ni	   (   R  Rº   (   R>   (    (    s   parsers/TTF.pyt   useMyMetricsæ  s    c         C   s&   d | >} |  | @} | r d Sd Sd  S(   Ni   i    (    (   t   int_typeR*   t   maskt   res(    (    s   parsers/TTF.pyR  ê  s
    

(   Rx   Ry   RG   R  R  R  R  Rû   R  R
  Rü   R  t   staticmethodR  (    (    (    s   parsers/TTF.pyRú   †  s   	 									(    (   Rx   Ry   RG   Rú   (    (    (    s   parsers/TTF.pyRî   X  s   	)Rï   c           B   s   e  Z d  „  Z RS(   c         C   s¢  g  |  _  x@ t d | j ƒ D], } |  j  j t d | j d ƒ ƒ d ƒ q Wt d | j d ƒ ƒ d |  _ d  |  _ d  |  _	 |  j d k rž | j
 ƒ  |  _ n  g  |  _ x@ t d |  j ƒ D], } |  j j t d | j d ƒ ƒ d ƒ qº Wt |  j ƒ d k r| j
 ƒ  |  _	 n  d j t |  j  ƒ ƒ GHg  |  _ xN t d |  j  t |  j  ƒ d d ƒ D]% } t j | ƒ } |  j j | ƒ qYWg  |  _ x‚ t d t |  j ƒ ƒ D]h } |  j | j ƒ  ræ|  j j t d | j d ƒ ƒ d ƒ q¤|  j j t d | j d ƒ ƒ d ƒ q¤Wg  |  _ x‚ t d t |  j ƒ ƒ D]h } |  j | j ƒ  rt|  j j t d | j d ƒ ƒ d ƒ q2|  j j t d | j d ƒ ƒ d ƒ q2Wd  S(   Ni    s   >Hi   s   >Bi   s   [D] 	 endPtsOfCountour is {}s   >h(   t   endPtsOfCountourR   Rí   R¥   R{   RS   t   instructionLengthRe   Rð   Rñ   R€   R¨   R5   R&   t
   glyphFlagsRê   t
   GlyphFlagst   xCoordinatest   isXshortVectorSett   yCoordinatest   isYshortVectorSet(   R>   R   Rì   R@   Rº   (    (    s   parsers/TTF.pyRG   ø  s8    	*			*	.	)*	)(   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyRï   ö  s   R  c           B   s_   e  Z d  „  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   c         C   sT   t  d | j d ƒ ƒ d |  _ |  j d k rP t  d | j d ƒ ƒ d |  _ n  d  S(   Ns   >Bi   i    (   R{   RS   t
   flagAsBytet   isRepeatSett   numberOfRepetition(   R>   R   (    (    s   parsers/TTF.pyRG   /  s    c         C   s   |  j  S(   N(   R  (   R>   (    (    s   parsers/TTF.pyR  7  s    c         C   s&   d | >} |  | @} | r d Sd Sd  S(   Ni   i    (    (   R  R*   R  R  (    (    s   parsers/TTF.pyR  :  s
    

c         C   s   |  j  |  j d ƒ S(   Ni    (   R  R  (   R>   (    (    s   parsers/TTF.pyt   isOnCurveSetD  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  R  (   R>   (    (    s   parsers/TTF.pyR  K  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  R  (   R>   (    (    s   parsers/TTF.pyR  Q  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  R  (   R>   (    (    s   parsers/TTF.pyR  Z  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  R  (   R>   (    (    s   parsers/TTF.pyt   isPositiveXshortVectorSet^  s    c         C   s   |  j  |  j d ƒ S(   Ni   (   R  R  (   R>   (    (    s   parsers/TTF.pyt   isPositiveYshortVectorSeta  s    (   Rx   Ry   RG   R  R  R  R   R  R  R  R!  R"  (    (    (    s   parsers/TTF.pyR  -  s   		
						(    (    (    (    (   Rx   Ry   RG   Rf   Rë   Rî   Rï   R  (    (    (    s   parsers/TTF.pyRê   ß  s   		Vž7R.   c           B   s@   e  Z d  „  Z d d d „  ƒ  YZ d d d „  ƒ  YZ d „  Z RS(   c         C   sn   | j  | j ƒ | |  _ |  j | ƒ |  _ g  |  _ x3 t |  j j ƒ D] } |  j j |  j	 | ƒ ƒ qG Wd  S(   N(
   R)   R*   R,   t   CmapTableHeaderRì   t	   subTablesR   t   numberOfEncodingTablesR¥   t   CmapSubTable(   R>   R   R,   R@   (    (    s   parsers/TTF.pyRG   j  s    		R#  c           B   s   e  Z d  „  Z d „  Z RS(   c         C   sB   t  d | j d ƒ ƒ d |  _ t  d | j d ƒ ƒ d |  _ d  S(   Ns   >Hi   i    (   R{   RS   RÏ   R%  (   R>   R   (    (    s   parsers/TTF.pyRG   y  s    c         C   s    t  d |  j ƒ t  d |  j ƒ S(   Ns   >H(   R‡   RÏ   R%  (   R>   (    (    s   parsers/TTF.pyt
   getContent~  s    (   Rx   Ry   RG   R'  (    (    (    s   parsers/TTF.pyR#  w  s   	R&  c           B   s-   e  Z d  „  Z d „  Z d d d „  ƒ  YZ RS(   c         C   sp   | j  ƒ  |  _ t d | j d ƒ ƒ d |  _ t d | j d ƒ ƒ d |  _ t d | j d ƒ ƒ d |  _ d  S(   Ns   >Hi   i    s   >Li   (   R€   t   startOfTableOffsetR{   RS   t
   platformIdt   platformSpecificEncodingt
   byteOffset(   R>   R   (    (    s   parsers/TTF.pyRG   …  s    c         C   s0   t  d |  j ƒ t  d |  j ƒ t  d |  j ƒ S(   Ns   >H(   R‡   R)  R*  R+  (   R>   (    (    s   parsers/TTF.pyR'  ˜  s    t   Format0c           B   s   e  Z d  „  Z RS(   c         C   s   d  S(   N(    (   R>   R   t   subTable(    (    s   parsers/TTF.pyRG   ¨  s    (   Rx   Ry   RG   (    (    (    s   parsers/TTF.pyR,  ¦  s   (    (   Rx   Ry   RG   R'  R,  (    (    (    s   parsers/TTF.pyR&  ƒ  s   		c         C   sp   |  j  j ƒ  } x! |  j D] } | | j ƒ  7} q Wt | ƒ G|  j j GHt | ƒ |  j j k sl t d ƒ ‚ | S(   Ns*   [E] Length of content data != table length(   Rì   R'  R$  R5   R,   R1   R2   (   R>   R‰   RA   (    (    s   parsers/TTF.pyR'  ­  s    $(    (    (   Rx   Ry   RG   R#  R&  R'  (    (    (    s   parsers/TTF.pyR.   h  s   	*t   __main__i   i   i   s   % {}($   RW   t   sysRl   RK   RM   R…   t   structt   REQUIRED_TABLESt
   TTF_TABLESt   POSTSCRIPT_TABLESt   BITMAP_TABLESt   OPTIONAL_TABLESR   R   R"   R˜   R6   R=   R:   R;   R²   R<   R3   R8   R9   R-   R+   Rê   R.   Rx   R5   t   argvR–   t   listdirR@   R&   (    (    (    s   parsers/TTF.pyt   <module>   sV   
ÿ `ÿ w
 
ÿ ŠT