�
ɺh�*���dZ ddlZn
#e$rdZYnwxYwddlZddlZddlmZmZddlmZddl m
Z
mZmZm
Z
mZmZddlmZmZmZmZmZddlmZddlmZdd lmZdd
lmZGd�d��ZGd
�d��ZdS)zo
maxminddb.reader
~~~~~~~~~~~~~~~~
This module contains the pure Python database reader and related classes.
�N)�IPv4Address�IPv6Address)�PathLike)�Any�AnyStr�IO�Optional�Tuple�Union)� MODE_AUTO� MODE_MMAP� MODE_FILE�MODE_MEMORY�MODE_FD)�Decoder)�InvalidDatabaseError)�
FileBuffer)�Recordc�^�eZdZUdZdZdZeeedfe d<dZ
eee d<e
fdeeeeefd ed
dfd�Zdd
�Zdeeeefd
eefd�Zdeeeefd
eeeeffd�Zded
eeeffd�Zded
efd�Zdeded
efd�Zded
efd�Zdd�Zdd�Z dd�Z!dS) �Readerz�
Instances of this class provide a reader for the MaxMind DB format. IP
addresses can be looked up using the ``get`` method.
�s���MaxMind.comz mmap.mmap�_bufferN�_ipv4_start�database�mode�returnc�n�|tkrts|tkr�t|d��5}tj|���dtj���|_|j���|_ddd��n#1swxYwY|}�n|ttfvr5t|��|_|j���|_|}n�|tkrZt|d��5}|���}||_t|��|_ddd��n#1swxYwY|}nX|tkr:|���|_t|j��|_|j}nt!d|�d����|j�|jt'd|jdz
����}|dkr'|���t+d |�d
����|t|j��z
}t-|j|��}|�|��\}} t1|t2��st+d|�d����t5di|��|_t-|j|jj|jz��|_d
|_dS)a�Reader for the MaxMind DB file format
Arguments:
database -- A path to a valid MaxMind DB file such as a GeoIP2 database
file, or a file descriptor in the case of MODE_FD.
mode -- mode to open the database with. Valid mode are:
* MODE_MMAP - read from memory map.
* MODE_FILE - read database as standard file.
* MODE_MEMORY - load database into memory.
* MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
* MODE_FD - the param passed via database is a file descriptor, not
a path. This mode implies MODE_MEMORY.
�rbr)�accessNzUnsupported open mode (z]). Only MODE_AUTO, MODE_FILE, MODE_MEMORY and MODE_FD are supported by the pure Python Readeri���zError opening database file (z#). Is this a valid MaxMind DB file?z)Error reading metadata in database file (z).F�) r�mmapr
�open�fileno�ACCESS_READr�size�_buffer_sizerrr�read�lenr�name�
ValueError�rfind�_METADATA_START_MARKER�max�closerr�decode�
isinstance�dict�Metadata� _metadata�search_tree_size�_DATA_SECTION_SEPARATOR_SIZE�_decoder�closed)
�selfrr�db_file�filename�buf�metadata_start�metadata_decoder�metadata�_s
�p/builddir/build/BUILD/imunify360-venv-2.5.2/opt/imunify360/venv/lib/python3.11/site-packages/maxminddb/reader.py�__init__zReader.__init__'s$��"
�I���$��4�9�+<�+<��h��%�%�
8��#�y����)9�)9�1�T�EU�V�V�V���$(�L�$5�$5�$7�$7��!�
8�
8�
8�
8�
8�
8�
8�
8�
8�
8�
8����
8�
8�
8�
8� �H�H�
�i��+�
+�
+�%�h�/�/�D�L� $�� 1� 1� 3� 3�D���H�H�
�[�
�
��h��%�%�
-���l�l�n�n��"���$'��H�H��!�
-�
-�
-�
-�
-�
-�
-�
-�
-�
-�
-����
-�
-�
-�
-� �H�H�
�W�_�_�#�=�=�?�?�D�L� #�D�L� 1� 1�D���}�H�H���$������
���+�+��'��Q��0A�J�0N�)O�)O�
�
���R����J�J�L�L�L�&�3��3�3�3���
�
�#�d�9�:�:�:��"�4�<��@�@��(�/�/��?�?�
��1��(�D�)�)� �&�H�H�H�H�H���
�"�-�-�H�-�-�����L��N�+�d�.O�O�
�
��
�����s$�AB�B�B�0D<�<E�Er3c��|jS)z7Return the metadata associated with the MaxMind DB file)r4�r9s rAr?zReader.metadatans
���~���
ip_addressc�6�|�|��\}}|S)z�Return the record for the ip_address in the MaxMind DB
Arguments:
ip_address -- an IP address in the standard string notation
)�get_with_prefix_len)r9rF�recordr@s rA�getz
Reader.getrs ���.�.�z�:�:�����
rEc��t|t��rtj|��}n|} t |j��}n"#t$r}td��|�d}~wwxYw|jdkr#|j j
dkrtd|�d����|�|��\}}|r|�
|��|fSd|fS)z�Return a tuple with the record and the associated prefix length
Arguments:
ip_address -- an IP address in the standard string notation
z/argument 1 must be a string or ipaddress objectN��zError looking up zD. You attempted to look up an IPv6 address in an IPv4-only database.)r1�str� ipaddressrF� bytearray�packed�AttributeError� TypeError�versionr4�
ip_versionr+�_find_address_in_tree�_resolve_data_pointer)r9rF�address�packed_address�ex�pointer�
prefix_lens rArHzReader.get_with_prefix_len|s���j�#�&�&� !��*�:�6�6�G�G� �G� W�&�w�~�6�6�N�N��� W� W� W��M�N�N�TV�V����� W�����?�a���D�N�$=��$B�$B��<�J�<�<�<���
�
!%� :� :�>� J� J���*�� C��-�-�g�6�6�
�B�B��Z��s�A�
A"�
A�A"rQc�F�t|��dz}|�|��}|jj}d}||krD||kr>d||dz d|dzz
z z}|�||��}|dz}||kr||k�>||krd|fS||kr||fStd���)N�r���zInvalid node in search tree)r)�_start_noder4�
node_count�
_read_noder)r9rQ� bit_count�noderc�i�bits rArVzReader._find_address_in_tree�s�����K�K�!�O� ���� �*�*���^�.�
�
���)�m�m��z� 1� 1��v�a�1�f�~��a�!�e��4�5�C��?�?�4��-�-�D��A��A��)�m�m��z� 1� 1�
�:����a�4�K��*�����7�N�"�#@�A�A�ArE�lengthc���|jjdks|dkrdS|jr|jSd}td��D]*}||jjkrn|�|d��}�+||_|S)NrL�r�`)r4rUr�rangercrd)r9rirfr@s rArbzReader._start_node�s����>�$��)�)�V�s�]�]��1��� $��#�#����r��� ,� ,�A��t�~�0�0�0����?�?�4��+�+�D�D�����rE�node_number�indexc��||jjz}|jj}|dkr||dzz}d|j||dz�z}n�|dkri|d|zz}t |j||dz���}|rd|dz|d<ndd|���zdz }|�d|��n3|d kr||dzz}|j||dz�}ntd
|�����tj d|��dS)N�r`��rM�r��� zUnknown record size: s!I)
r4�node_byte_size�record_sizerrP�pop�insertr�struct�unpack)r9rnro�base_offsetrx�offset�
node_bytes�middles rArdzReader._read_node�s2��!�D�N�$A�A���n�0���"��� �5�1�9�,�F� �4�<���!��0C�#D�D�J�J�
�B�
�
� �1�u�9�,�F�"�4�<���!��0C�#D�E�E�J��
-� $�z�!�}� 4�
�1�
�
�����!1�!1�1�a�7���!�!�!�V�,�,�,�,�
�B�
�
� �5�1�9�,�F���f�v��z�&9�:�J�J�&�'L�{�'L�'L�M�M�M��}�U�J�/�/��2�2rEr[c��||jjz
|jjz}||jkrt d���|j�|��\}}|S)Nz,The MaxMind DB file's search tree is corrupt)r4rcr5r'rr7r0)r9r[�resolved�datar@s rArWzReader._resolve_data_pointer�sV���T�^�6�6���9X�X���t�(�(�(�&�'U�V�V�V��M�(�(��2�2� ��q��rEc�j� |j���n#t$rYnwxYwd|_dS)zBCloses the MaxMind DB file and returns the resources to the systemTN)rr/rRr8rDs rAr/zReader.close�sI�� ��L��� � � � ��� � � ��D� ��������s��
)�)c�.�|���dS)N)r/�r9�argss rA�__exit__zReader.__exit__�s���
�
�����rEc�2�|jrtd���|S)Nz%Attempt to reopen a closed MaxMind DB)r8r+rDs rA� __enter__zReader.__enter__�s!���;� F��D�E�E�E��rE)rr3�rN)rr)"�__name__�
__module__�__qualname__�__doc__r6r-r�bytesr�__annotations__rr �intrrrrrBr?rNrrrrJr
rHrPrVrbrdrWr/r�r�r!rErArrs ���������
$&� �7��
�5�*�k�1�
2�2�2�2�!%�K��#��%�%�%�GP�E�E��f�c�8�R�7�8�E�@C�E�
�E�E�E�E�N�����e�C��k�$A�B��x�PV�GW����� ���[�+� =�>� � �x����$� %� � � � �>B�I�B�%��S��/�B�B�B�B�&�#��#�����"3�c�3�#�3�#�3�3�3�3�,�S��V������������������rErc�X�eZdZdZdd�Zedefd���Zedefd���Zd�Z dS) r3a�Metadata for the MaxMind DB reader
.. attribute:: binary_format_major_version
The major version number of the binary format used when creating the
database.
:type: int
.. attribute:: binary_format_minor_version
The minor version number of the binary format used when creating the
database.
:type: int
.. attribute:: build_epoch
The Unix epoch for the build time of the database.
:type: int
.. attribute:: database_type
A string identifying the database type, e.g., "GeoIP2-City".
:type: str
.. attribute:: description
A map from locales to text descriptions of the database.
:type: dict(str, str)
.. attribute:: ip_version
The IP version of the data in a database. A value of "4" means the
database only supports IPv4. A database with a value of "6" may support
both IPv4 and IPv6 lookups.
:type: int
.. attribute:: languages
A list of locale codes supported by the databse.
:type: list(str)
.. attribute:: node_count
The number of nodes in the database.
:type: int
.. attribute:: record_size
The bit size of a record in the search tree.
:type: int
rNc���|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d |_d
S)zACreates new Metadata object. kwargs are key/value pairs from specrcrxrU�
database_type� languages�binary_format_major_version�binary_format_minor_version�build_epoch�descriptionN) rcrxrUr�r�r�r�r�r�)r9�kwargss rArBzMetadata.__init__0s~��!��.���!�-�0��� ��.���#�O�4�����,���+1�2O�+P��(�+1�2O�+P��(�!�-�0���!�-�0����rEc��|jdzS)z8The size of a node in bytes
:type: int
rM)rxrDs rArwzMetadata.node_byte_size>s����1�$�$rEc� �|j|jzS)z8The size of the search tree
:type: int
)rcrwrDs rAr5zMetadata.search_tree_sizeFs�����!4�4�4rEc��d�d�|j���D����}|j�d|jj�d|�d�S)Nz, c3�*K�|]\}}|�d|��V��dS)�=Nr!)�.0�k�vs rA� <genexpr>z$Metadata.__repr__.<locals>.<genexpr>Os0����H�H�$�!�Q�A������H�H�H�H�H�HrE�.�(�))�join�__dict__�itemsr�� __class__r�r�s rA�__repr__zMetadata.__repr__NsV���y�y�H�H�$�-�2E�2E�2G�2G�H�H�H�H�H���/�E�E�D�N�$;�E�E�d�E�E�E�ErEr�)
r�r�r�r�rB�propertyr�rwr5r�r!rErAr3r3�s�������=�=�@1�1�1�1��%��%�%�%��X�%��5�#�5�5�5��X�5�F�F�F�F�FrEr3) r�r"�ImportErrorrOr{rr�osr�typingrrrr r
r�maxminddb.constrr
rrr�maxminddb.decoderr�maxminddb.errorsr�maxminddb.filer�maxminddb.typesrrr3r!rErA�<module>r�s�������K�K�K�K�������D�D�D���������
�
�
�
�.�.�.�.�.�.�.�.�������:�:�:�:�:�:�:�:�:�:�:�:�:�:�:�:�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�%�%�%�%�%�%�1�1�1�1�1�1�%�%�%�%�%�%�"�"�"�"�"�"�Q�Q�Q�Q�Q�Q�Q�Q�haF�aF�aF�aF�aF�aF�aF�aF�aF�aFs� �� |