
    u%j%                         d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z
 d dlmZ  ej                  d      Z G d d      Zy)    N)
AXMLParser)format_value)etreezpyaxmlparser.axmlprinterc                   V    e Zd ZdZdZdZd Zd ZddZd Z	d Z
d Zd	 Zd
 Zd Zd Zy)AXMLPrinterz
    Converter for AXML Files into a lxml ElementTree, which can easily be
    converted into XML.

    A Reference Implementation can be found at http://androidxref.com/9.0.0_r3/
    xref/frameworks/base/tools/aapt/XMLNode.cpp
    Nc           
      	   t        |      | _        d | _        d| _        g }| j                  j	                         rt        | j                        }|t        j                  k(  r| j                  | j                  j                        }| j                  | j                  j                        }dj                  ||      }| j                  j                  }|rX| j                  %t        j                  dj                  |             n'|d   j!                  t#        j$                  |             t        j'                  dj                  || j                  j(                               t#        j*                  || j                  j,                        }t/        | j                  j1                               D ]  }	| j                  | j                  j3                  |	            }| j                  | j                  j5                  |	            }| j7                  | j9                  |	            }
t        j'                  dj                  |||
j;                  d                   dj                  ||      |j<                  v r%t        j                  d	j                  ||             |j?                  dj                  ||      |
        | j                  || _        n,|st        jA                  d
       y |d   j!                  |       |j!                  |       |t        jB                  k(  r|st        j                  d       | j                  j                  }| j                  | j                  j                        }dj                  ||      }|d   jD                  |k7  rMt        j                  dj                  | j                  j                  | j                  j(                               |jG                          |t        jH                  k(  rEt        j'                  dj                  |d                | j                  jJ                  |d   _%        |t        jL                  k(  r8tO        | j                  jP                        dkD  rt        j                  d       y | j                  j	                         ry y )NFz{}{}z6Can not attach comment with content '{}' without root!zSTART_TAG: {} (line={}))nsmapzfound an attribute: {}{}='{}'utf-8z+Duplicate attribute '{}{}'! Will overwrite!z>No more elements available to attach to! Is the XML malformed?z:Too many END_TAG! No more elements available to attach to!zXClosing tag '{}' does not match current stack! At line number: {}. Is the XML malformed?zTEXT for {}r   z7Not all namespace mappings were closed! Malformed AXML?))r   axmlrootpackerwarningis_validnextconst	START_TAG	_fix_namename_print_namespace	namespaceformatcommentlogwarningappendr   Commentdebugm_lineNumberElementr
   rangegetAttributeCountgetAttributeNamespacegetAttributeName
_fix_value_get_attribute_valueencodeattribseterrorEND_TAGtagpopTEXTtextEND_DOCUMENTlen
namespaces)selfraw_buffcur_typer   urir+   r   elemivalues              f/home/ubuntu/.local/share/pipx/venvs/gplaycli/lib/python3.12/site-packages/pyaxmlparser/axmlprinter.py__init__zAXMLPrinter.__init__*   sm   x(		"ii  "OE'~~diinn5++DII,?,?@mmC.))++yy($\$c$cdk$lmBu}}W'=>		3::3		@V@VWX}}S		@tyy::<= >A//		0O0OPQ0RSC>>$))*D*DQ*GHD OOD,E,Ea,HIEII=DDS$PUP\P\]dPefg}}S$/4;;>$Q$X$XY\^b$cdHHV]]35u=> 99$ $DI		"bcGNN4(

4 %KK \]yy~~++DII,?,?@mmC.r7;;#%KKDDJF IINNDII,B,BE 	

"		-..s2w78#yy~~B***tyy++,q0KKQS{ ii  ""    c                 &    | j                  d      S )zu
        Returns the raw XML file without prettification applied.

        :returns: bytes, encoded as UTF-8
        F)pretty)get_xmlr2   s    r:   get_buffzAXMLPrinter.get_buffp   s     ||5|))r<   c                 F    t        j                  | j                  d|      S )zZ
        Get the XML as an UTF-8 string

        :returns: bytes encoded as UTF-8
        r   )encodingpretty_print)r   tostringr   )r2   r>   s     r:   r?   zAXMLPrinter.get_xmlx   s     ~~dii'OOr<   c                     | j                   S )ze
        Get the XML as an ElementTree object

        :returns: :class:`lxml.etree.Element`
        )r   r@   s    r:   get_xml_objzAXMLPrinter.get_xml_obj   s     yyr<   c                 6    | j                   j                         S )z
        Return the state of the AXMLParser.
        If this flag is set to False, the parsing has failed, thus
        the resulting XML will not work or will even be empty.
        )r   r   r@   s    r:   r   zAXMLPrinter.is_valid   s     yy!!##r<   c                     | j                   S )aw  
        Returns True if the AXML is likely to be packed

        Packers do some weird stuff and we try to detect it.
        Sometimes the files are not packed but simply broken or compiled with
        some broken version of a tool.
        Some file corruption might also be appear to be a packed file.

        :returns: True if packer detected, False otherwise
        )r   r@   s    r:   	is_packedzAXMLPrinter.is_packed   s     !!!r<   c                       j                   j                        } j                   j                        }t        || fd      S )z
        Wrapper function for format_value
        to resolve the actual value of an attribute in a tag
        :param index: index of the current attribute
        :return: formatted value
        c                 :    j                   j                        S )N)r   getAttributeValue)_indexr2   s    r:   <lambda>z2AXMLPrinter._get_attribute_value.<locals>.<lambda>   s    DII4O4OPU4V r<   )r   getAttributeValueTypegetAttributeValueDatar   )r2   rO   r5   _datas   ``  r:   r%   z AXMLPrinter._get_attribute_value   s=     		//6		//6E5*VWWr<   c                 H   |d   j                         sD|d   dk7  r<t        j                  dj                  |             d| _        dj                  |      }|j                  d      r9t        j                  dj                  |             d| _        |t        d      d }d	|v r$t        j                  d
j                  |             t        j                  d|      sBt        j                  dj                  |             d| _        t        j                  dd|      }|S )a  
        Apply some fixes to element named and attribute names.
        Try to get conform to:
        > Like element names, attribute names are case-sensitive and must start with a letter or underscore.
        > The rest of the name can contain letters, digits, hyphens, underscores, and periods.
        See: https://msdn.microsoft.com/en-us/library/ms256152(v=vs.110).aspx

        :param name: Name of the attribute
        :return: a fixed version of the name
        r   rN   zInvalid start for name '{}'Tz_{}zandroid:zZName '{}' starts with 'android:' prefix! The Manifest seems to be broken? Removing prefix.N:z.Name seems to contain a namespace prefix: '{}'z^[a-zA-Z0-9._-]*$z&Name '{}' contains invalid characters!z[^a-zA-Z0-9._-])
isalphar   r   r   r   
startswithr0   rematchsub)r2   r   s     r:   r   zAXMLPrinter._fix_name   s     Aw T!W^KK5<<TBC!%D<<%D??:& KKDDJFE "&DJ()D$;KKHOOPTUVxx,d3KK@GGMN!%D66,c48Dr<   c           
         | j                   r| j                  s|t        j                  dk(  r5t	        j
                  d      | _         t	        j
                  d      | _        n4t	        j
                  d      | _         t	        j
                  d      | _        d|v rqd| _        t        j                  dj                  |j                  d      t        j                  |j                  d	                         |d
|j                  d       }| j                   j                  |      s8t        j                  d       d| _        | j                  j                  d|      }|S )a9  
        Return a cleaned version of a value
        according to the specification:
        > Char	   ::=   	#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

        See https://www.w3.org/TR/xml/#charsets

        :param value: a value to clean
        :return: the cleaned value
        i  u)   ^([ -퟿	
-�]|[-][-])*$u   [^ -	
-�-]u   ^[ -퟿	
-�𐀀-􏿿]*$u   [^ -퟿	
-�𐀀-􏿿] TzCNull byte found in attribute value at position {}: Value(hex): '{}'r   Nz5Invalid character in value found. Replacing with '_'.rN   )_AXMLPrinter__charrange_AXMLPrinter__replacementsys
maxunicoderX   compiler   r   r   r   findbinasciihexlifyr&   rY   rZ   )r2   r9   s     r:   r$   zAXMLPrinter._fix_value   s    t'9'9~~'#%::i$k  &(ZZ0o%p"#%::.w#x %'ZZ0w%x" U?!%DKK##)6JJv&$$U\\'%:;$ -5::f-.E%%e,KKOP!%D&&**36Er<   c                 2    |dk7  rdj                  |      }|S )N z{{{}}})r   )r2   r6   s     r:   r   zAXMLPrinter._print_namespace   s    "9//#&C
r<   )T)__name__
__module____qualname____doc__r]   r^   r;   rA   r?   rG   r   rJ   r%   r   r$   r    r<   r:   r   r      sK     KMDL*P$"
X#J&Pr<   r   )rX   r_   rc   loggingpyaxmlparser.axmlparserr   pyaxmlparser.utilsr   pyaxmlparser.constants	constantsr   lxmlr   	getLoggerr   r   rk   r<   r:   <module>rs      s>   $ 
 
   . + & g23Z Zr<   