HEX
Server: Apache
System: Linux a16-asgard6.hospedagemuolhost.com.br 5.14.0-570.52.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Oct 15 06:39:08 EDT 2025 x86_64
User: maoristu4c3dbd03 (1436)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/lib64/python3.9/site-packages/rhsm/__pycache__/pathtree.cpython-39.opt-1.pyc
a

X�Zh�.�@sVddlZddlZddlmZmZmZddlmZddlm	Z	dZ
dZGdd�d�ZdS)	�N)�Tuple�List�Any)�GhettoBitStream��HuffmanNodezPATH ENDZlistingc@s�eZdZdZedd�dd�Zeed�dd�Zed	�d
d�Z	de
eedd�d
d�Ze
ee
ed�dd��Zeeee
efd�dd��Zeeed�dd��Ze
eeed�dd��Zeeeed�dd��Ze
eeeeeed�dd��ZdS) �PathTreeat
    This builds and makes available a tree that represents matchable paths. A
    path must be matched starting from its root and the root of the tree,
    matching one segment at a time.

    There are three trees involved in the process, and that can get confusing.

    1)  Word Tree: This is a Huffman tree made from the word list provided at
        the beginning of the data stream.
    2)  Huffman Path Tree: This is a Huffman tree made of nodes whose values
        will become nodes in the Path Tree. This tree exists so there can be
        a Huffman code associated with each node in the Path Tree. However,
        the Path Tree itself will arrange this data much differently.
    3)  Path Tree: This is the tree used to match paths. Each node is a
        dict where keys are path segments (the middle part of /.../) and each
        value is a list of other nodes.
    N)�data�returncCsn|�|�\}}t�|�tdd�|D��}t|�}|�|�}t�|�tdd�|D��}|�||||�|_dS)a�
        Uncompresses data into a tree that can be traversed for matching paths
        :param data:    binary data as read from a file or pulled directly out
                        of a certificate extension. Data should be compressed
                        with huffman coding as described for v3 entitlement
                        certificates
        :type  data:    binary string
        css|]}|j|jfVqdS�N)�code�value��.0�node�r�3/usr/lib64/python3.9/site-packages/rhsm/pathtree.py�	<genexpr>9�z$PathTree.__init__.<locals>.<genexpr>css|]}|j|fVqdSr)rrrrrr=rN)�_unpack_datarZ
build_tree�dictr�_generate_path_leaves�_generate_path_tree�	path_tree)�selfr	Zword_leavesZunused_bits�	word_dict�	bitstream�path_leaves�	path_dictrrr�__init__.s	


zPathTree.__init__)�pathr
cCs,|�d�std��|�|j|�d��d��S)a�
        Given an absolute path, determines if the path tree contains any
        complete paths that exactly equal the beginning of this path. Thus,
        The tree is traversed from its root, and as long as the provided path
        does not hit its end before hitting the end of a tree path, this will
        return True
        :param path:    absolute path to match against the tree
        :return:        True iff there is a match, else False
        �/zpath must start with "/")�
startswith�
ValueError�_traverse_treer�strip�split)rr rrr�
match_path@s

zPathTree.match_path)r
cCsg}|�|�d�t|��S)N�
)�build_path_list�join�sorted)r�pathsrrr�__str__Ns
zPathTree.__str__)�acc�tree�	curr_pathr
cCsv|durd}|dur|j}t|t�r*|g}|D]B}|��D]4\}}|tkrV|�|�q:|j||d||fd�q:q.dS)aW
        Expand the Huffman tree into a list of paths
        :param acc:       an accumulator that stores the expanded paths. Callers should provide an empty list
        :param tree:      A dict representing a node in the greater path tree
        :param curr_path: A string representing a path that is added to as nodes are visited
        N�z%s/%s)r/r0)r�
isinstancer�items�PATH_END�appendr))rr.r/r0�branch�k�vrrrr)Ss
zPathTree.build_path_list)r/�wordsr
cCs�t|vrdS|r�g}|d|vr.|�|d�|dtkrJt|�dkrJdSt|���D]}|�d�rV|�|�qV|D].}||D] }|�||dd��r�dSq�qtdS)a�
        Helper method for match_path that does recursive matching
        :param tree:    A dict representing a node in the greater path tree
        :param words:   list of words to match, the result of spliting a path
                        by the "/" separator. Words must be sorted with the
                        next word to match being at words[0]
        :return:        True iff there is a match, else False
        Tr��$NF)r4r5�LISTING�len�list�keysr"r$)�clsr/r9Zwords_to_try�word�childrrrr$is 

zPathTree._traverse_treecCsVt��}|�|�}|�d�}dd�|D�}ttt�d�|��}dd�|D�}||jfS)a�
        :param data:    binary data as read from a file or pulled directly out
                        of a certificate extension. Data should be compressed
                        with huffman coding as described for v3 entitlement
                        certificates
        :return:        tuple: (list of HuffmanNode instances not yet in a
                        tree, binary string of leftover bits that were not
                        part of the zlib-compressed word list
        �cSsg|]}|�d��qS)zutf-8)�decode)rrArrr�
<listcomp>�rz)PathTree._unpack_data.<locals>.<listcomp>r:cSsg|]\}}t||��qSrr)r�weightr
rrrrE�r)	�zlib�
decompressobj�
decompressr&r>�zip�	itertools�count�unused_data)r	rIZdecompressed_datar9Zweighted_words�nodesrrrr�s

zPathTree._unpack_data)rr
csD���}|dkr|S|d}�fdd�t|�D�}��|�}|SdS)a>
        Determine the total number of nodes in the uncompressed tree. The
        algorithm for doing so is described in the v3 entitlement cert
        format documentation.

        :param bitstream:   the full bit stream following the zlib-compressed
                            word list. As defined in the v3 entitlement cert
                            format, the beginning of this stream defines how
                            many total nodes exist. This method retrieves that
                            value
        :return:            number of nodes
        �csg|]}����qSr)�pop_byte)r�x�rrrrE�rz,PathTree._get_node_count.<locals>.<listcomp>N)rP�rangeZ
combine_bytes)rZ
first_byteZ	num_bytesZcount_bytes�
node_countrrRr�_get_node_count�s
zPathTree._get_node_countcCs6|�|�}g}td|�D]}t|i�}|�|�q|S)a�
        Given the remaining bits after decompressing the word list, this
        generates HuffmanNode objects to represent each node (besides root)
        that will end up in the path tree

        :param bitstream:   stream of bits remaining after decompressing the
                            word list
        :return:            list of HuffmanNode objects that can be used to
                            build a path tree
        r:)rUrSrr5)r@rrTrNrFrrrrr�s

zPathTree._generate_path_leaves)�	code_dictrr
cCs.d}|D] }||7}||vr||SqdS)a�
        Given a bit stream and dictionary where keys are huffman codes, return
        the value from that dictionary that corresponds to the next huffman
        code in the stream. This is a substitute for actually traversing the
        tree, and this likely performs better in large data sets
        :param code_dict:   any dictionary where keys are huffman codes
        :param bitstream:   bit stream with a huffman code as the next value
        :return:            value from the dict
        r1Nr)rVrr�bitrrr�_get_leaf_from_dict�s
zPathTree._get_leaf_from_dict)rrrrr
c
Csvdd�|D�}i}|�d|�|D]8}|�||�}|s8q"|�||�}	|�|g��|	j�q&q"|D]}|s`d|t<q`|S)a�
        Once huffman trees have been generated for the words and for the path
        nodes, this method uses them and the bit stream to create the path tree
        that can be traversed to match potentially authorized paths
        :param path_dict:   dictionary where keys are huffman codes and values
                            are path nodes
        :param path_leaves: leaf nodes from the huffman tree of path nodes. the
                            values will be constructed into a new tree that can
                            be traversed to match actual paths
        :param word_dict:   dict where keys are huffman codes and values are
                            words from the zlib-compressed word list
        :param bitstream:   bit stream where the rest of the bits describe
                            how to use words as references between nodes in
                            the path tree. This format is described in detail
                            in the v3 entitlement certificate docs
        cSsg|]
}|j�qSr)r
)rZleafrrrrE�rz0PathTree._generate_path_tree.<locals>.<listcomp>rN)�insertrX�
setdefaultr5r
r4)
r@rrrr�values�rootr
rAZ	path_noderrrr�s
zPathTree._generate_path_tree)NN)�__name__�
__module__�__qualname__�__doc__�bytesr�str�boolr'r-r>rr)�classmethodr$�staticmethodrrr�intrUrrrrrXrrrrrrs&!�r)
rKrG�typingrrrZrhsm.bitstreamrZrhsm.huffmanrr4r<rrrrr�<module>s