
    u%ja$                     ^    d dl Z d dlmZ d dlmZ  e j                  d      Z G d de      Z	y)    N)unpackzpyaxmlparser.stringblockc                   \    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d Zd Zy)StringBlockz
    StringBlock is a CHUNK inside an AXML File
    It contains all strings, which are used by referecing to ID's

    See http://androidxref.com/9.0.0_r3/xref/frameworks/base/libs/androidfw/include/androidfw/ResourceTypes.h#436
    c           	         i | _         || _        t        d|j                  d            d   | _        t        d|j                  d            d   | _        t        d|j                  d            d   | _        | j                  t        j                  z  dk7  | _	        t        d|j                  d            d   | _
        t        d|j                  d            d   | _        | j
                  dk(  r$| j                  dkD  rt        j                  d       g | _        g | _        d| _        g | _        t%        | j                        D ]9  }| j                  j'                  t        d|j                  d            d          ; t%        | j
                        D ]9  }| j                  j'                  t        d|j                  d            d          ; | j                  j(                  | j                  z
  }| j                  dk7  r(| j
                  dk7  r| j                  | j                  z
  }|dz  dk7  rt        j+                  d       |j                  |      | _        | j                  dk7  r| j
                  dk7  r| j                  j(                  | j                  z
  }|dz  dk7  rt        j+                  d       t%        d|dz        D ]9  }| j"                  j'                  t        d|j                  d            d          ; yyy)	z|
        :param buff: buffer which holds the string block
        :param header: a instance of :class:`~ARSCHeader`
        z<I   r   z^Styles Offset given, but styleCount is zero. This is not a problem but could indicate packers. z-Size of strings is not aligned by four bytes.z,Size of styles is not aligned by four bytes.N)_cacheheaderr   readstringCount
styleCountflagsconst	UTF8_FLAGm_isUTF8stringsOffsetstylesOffsetloginfom_stringOffsetsm_styleOffsets
m_charbuffm_stylesrangeappendsizewarning)selfbuffr
   ir   s        f/home/ubuntu/.local/share/pipx/venvs/gplaycli/lib/python3.12/site-packages/pyaxmlparser/stringblock.py__init__zStringBlock.__init__"   s   
  "$		!5a8 tyy|4Q7 D$))A,/2
**u61< $D$))A,7: #416q9 ??aD$5$5$9HH I J  "  t''( 	GA  ''tTYYq\(B1(EF	G
 t' 	FA&&vdDIIaL'A!'DE	F {{$"4"44 !doo&:$$t'9'99D1H?KKGH))D/!doo&:;;##d&7&77DqQJK1dai( D$$VD$))A,%?%BCD ';!    c                 $    | j                  |      S )zE
        Returns the string at the index in the string table
        )	getStringr   idxs     r!   __getitem__zStringBlock.__getitem__d   s     ~~c""r#   c                     | j                   S )z@
        Get the number of strings stored in this table
        )r   )r   s    r!   __len__zStringBlock.__len__j   s     r#   c              #   f   K   t        | j                        D ]  }| j                  |        yw)z+
        Iterable over all strings
        N)r   r   r%   )r   r    s     r!   __iter__zStringBlock.__iter__p   s1      t''( 	$A..##	$s   /1c                 L   || j                   v r| j                   |   S |dk  s| j                  r|| j                  kD  ry| j                  |   }| j                  r| j	                  |      | j                   |<   n| j                  |      | j                   |<   | j                   |   S )z
        Return the string at the index in the string table

        :param idx: index in the string table
        :return: str
        r   r   )r	   r   r   r   _decode8	_decode16)r   r'   offsets      r!   r%   zStringBlock.getStringw   s     $++;;s##7$..#8H8H2H%%c*==#}}V4DKK#~~f5DKK{{3r#   c                      | j                   |   S )zu
        Return the style associated with the index

        :param idx: index of the style
        :return:
        )r   r&   s     r!   getStylezStringBlock.getStyle   s     }}S!!r#   c                 
   | j                  |d      \  }}||z  }| j                  |d      \  }}||z  }| j                  |||z    }| j                  ||z      dk(  sJ dj                  |             | j                  |d|      S )z
        Decode an UTF-8 String at the given offset

        :param offset: offset of the string inside the data
        :return: str
           r   z1UTF-8 String is not null terminated! At offset={}zutf-8_decode_lengthr   format_decode_bytesr   r0   str_lenskipencoded_bytesdatas         r!   r.   zStringBlock._decode8   s     ++FA6$ #11&!<t$vv'=>v56!; 	O?FFvN	O; !!$99r#   c                     | j                  |d      \  }}||z  }|dz  }| j                  |||z    }| j                  ||z   ||z   dz    dk(  sJ dj                  |             | j                  |d|      S )z
        Decode an UTF-16 String at the given offset

        :param offset: offset of the string inside the data
        :return: str
           s     z2UTF-16 String is not null terminated! At offset={}zutf-16r5   r9   s         r!   r/   zStringBlock._decode16   s     ++FA6$  !vv'=>v5f}6Lq6PQU`` 	P@GGO	P` !!$'::r#   c                 p    | j                  |d      }t        |      |k7  rt        j                  d       |S )a  
        Generic decoding with length check.
        The string is decoded from bytes with the given encoding, then the length
        of the string is checked.
        The string is decoded using the "replace" method.

        :param data: bytes
        :param encoding: encoding name ("utf-8" or "utf-16")
        :param str_len: length of the decoded string
        :return: str
        replacezinvalid decoded string length)decodelenr   r   )r=   encodingr:   strings       r!   r8   zStringBlock._decode_bytes   s2     Xy1v;'!KK78r#   c                 R   |dz  }dj                  |dk(  rdnd      }dd|dz
  z  z  }t        || j                  |||z          \  }}||z  dk7  r|| z  d|z  z  |z  }|}	n|}|}	|dk(  r|dk  sJ d	j                  |             ||	fS |d
k  sJ dj                  |             ||	fS )a  
        Generic Length Decoding at offset of string

        The method works for both 8 and 16 bit Strings.
        Length checks are enforced:
        * 8 bit strings: maximum of 0x7FFF bytes (See
        http://androidxref.com/9.0.0_r3/xref/frameworks/base/libs/androidfw/ResourceTypes.cpp#692)
        * 16 bit strings: maximum of 0x7FFFFFF bytes (See
        http://androidxref.com/9.0.0_r3/xref/frameworks/base/libs/androidfw/ResourceTypes.cpp#670)

        :param offset: offset into the string data section of the beginning of
        the string
        :param sizeof_char: number of bytes per char (1 = 8bit, 2 = 16bit)
        :returns: tuple of (length, read bytes)
        r4   z<2{}BH      r   i  z1length of UTF-8 string is too large! At offset={}iz3length of UTF-16 string is too large!  At offset={})r7   r   r   )
r   r0   sizeof_charsizeof_2charsfmthighbitlength1length2lengthr   s
             r!   r6   zStringBlock._decode_length   s      $q(mm;!#3C=1a01!#tvv?U'WXg!#')q;?7JF DFD!V#g%X%_%_`f%gg# t| Z'm)^)e)efl)mm't|r#   c                 (   t        d| j                  | j                  | j                  | j                  | j
                  fz         | j                  dkD  rLt                t        d       t        |       D ])  \  }}t        dj                  |t        |                   + | j                  dkD  rct                t        d       t        | j                        D ]5  }t        dj                  |t        | j                  |                         7 yy)zI
        Print some information on stdout about the string table
        zcStringBlock(stringsCount=0x%x, stringsOffset=0x%x, stylesCount=0x%x, stylesOffset=0x%x, flags=0x%x)r   zString Table: z	{:08d} {}zStyles Table: N)printr   r   r   r   r   	enumerater7   reprr   r2   )r   r    ss      r!   showzStringBlock.show   s     	 
 %%''__&&ZZ	!	! 		" aG"#!$ 61k((DG456 ??QG"#4??+ Ek((Dq1A,BCDE r#   N)__name__
__module____qualname____doc__r"   r(   r*   r,   r%   r2   r.   r/   staticmethodr8   r6   rW    r#   r!   r   r      sR    @DD# $ ,":.;(  ""HEr#   r   )
loggingstructr   pyaxmlparser.constants	constantsr   	getLoggerr   objectr   r]   r#   r!   <module>rd      s5   $   & g23uE& uEr#   