§
    |Øj�E  ã                   óÌ  — U d Z ddlmZmZmZ ddlmZmZ  e e	dd¦  «        ¦  «        Z
eed<   deded	ed
efd„ZdZeed<    ee¦  «        Zee         ed<    ed¦  «        Zee         ed<   d„  ee¦  «        D ¦   «         Zeeef         ed<   dZeed<   ded
efd„Zdeded
efd„Zded
efd„Zded
efd„Zdeded	ed
efd„Zded
eeef         dz  fd„Zded
edz  fd „ZdS )!a  Early detection of escape-sequence-based encodings (ISO-2022, HZ-GB-2312, UTF-7).

These encodings use ESC (0x1B), tilde (~), or plus (+) sequences to switch
character sets.  They must be detected before binary detection (ESC is a control
byte) and before ASCII detection (HZ-GB-2312 and UTF-7 use only printable ASCII
bytes plus their respective shift markers).

Note: ``from __future__ import annotations`` is intentionally omitted because
this module is compiled with mypyc, which does not support PEP 563 string
annotations.
é    )ÚEVIDENCE_CAP_BYTESÚcount_deletedÚdecodes_without_error)ÚDETERMINISTIC_CONFIDENCEÚDetectionResulté!   é   Ú_HZ_GB_BYTESÚdataÚ	max_startÚmax_endÚreturnc                 ó\  — d}	 |                       d|¦  «        }|dk    s||k    rdS |                       d|dz   |¦  «        }|dk    rdS | |dz   |…         }t          |¦  «        dk    r>t          |¦  «        dz  dk    r(t          |t          ¦  «        t          |¦  «        k    rdS |dz   }Œª)aF  Check that at least one ~{...~} region contains valid GB2312 byte pairs.

    In HZ-GB-2312 GB mode, characters are encoded as pairs of bytes in the
    0x21-0x7E range.  We require at least one region with a non-empty, even-
    length run of such bytes.

    *max_start* bounds where a region may open and *max_end* where it may
    close.  Keeping them separate is what lets a region that opens inside
    the evidence window close beyond it: bounding both at the same offset
    would cut a straddling region and hide the only escape evidence a
    document has (ADR-0006).
    r   Tó   ~{éÿÿÿÿFó   ~}é   )ÚfindÚlenr   r
   )r   r   r   ÚstartÚbeginÚendÚregions          úŒ/Users/tedpeters/Documents/Projects/Claude Projects/Misc Tools/notename-sheets/.venv/lib/python3.11/site-packages/chardet/pipeline/escape.pyÚ_has_valid_hz_regionsr      sÈ   € ð €EðØ—	’	˜% Ñ'Ô'ˆØ�BŠ;ˆ;˜% 9Ò,Ð,Ø�5Ø�iŠi˜˜u q™y¨'Ñ2Ô2ˆØ�"Š9ˆ9Ø�5Ø�e˜a‘i #�oÔ&ˆõ �‰KŒK˜1ÒÐÝ�F‘”˜a‘ 1Ò$Ð$Ý˜f¥lÑ3Ô3µs¸6±{´{ÒBÐBà�4Ø�a‘ˆð'ó    s@   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/Ú
_B64_CHARSÚ_UTF7_BASE64s   ABCDEFGHIJKLMNOPQRSTUVWXYZÚ_B64_UPPERCASEc                 ó   — i | ]\  }}||“Œ	S © r!   )Ú.0ÚiÚcs      r   ú
<dictcomp>r%   @   s   € ÐFÐFÐF©¨¨1˜q !ÐFÐFÐFr   Ú_B64_DECODEé   Ú_EMBEDDED_B64_RUNÚ	b64_bytesc                 óü  — t          | ¦  «        }|dz  }|dz  }|dk    r"t          | d                  }d|z  dz
  }||z  rdS |dz  }t          |¦  «        }d}d}	d}
| D ]5}|dz  t          |         z  }|	dz  }	|	dk    r|	dz  }	||	z	  dz  ||
<   |
dz  }
Œ6d}t          d|dz
  d	¦  «        D ]P}||         dz  ||dz            z  }d
|cxk    rdk    rn n|r dS d}Œ1d|cxk    rdk    rn n|s dS d}ŒI|r dS d}ŒQ| S )uØ  Check if base64 bytes decode to valid UTF-16BE with correct padding.

    A valid UTF-7 shifted sequence must:
    1. Contain at least 3 Base64 characters (18 bits, enough for one 16-bit
       UTF-16 code unit).
    2. Have zero-valued trailing padding bits (the unused low bits of the last
       Base64 sextet after the last complete 16-bit code unit).
    3. Decode to valid UTF-16BE â€” no lone surrogates.

    This rejects accidental ``+<alphanum>-`` patterns found in URLs, MIME
    boundaries, hex-encoded hashes (e.g. SHA-1 git refs), and other ASCII data.

    The caller (``_has_valid_utf7_sequences``) already checks ``b64_len >= 3``
    before calling this function, so *b64_bytes* is always at least 3 bytes.
    é   é   r   r   é   Fé   éÿ   r   i Ø  iÿÛ  Ti Ü  iÿß  )r   r&   Ú	bytearrayÚrange)r)   ÚnÚ
total_bitsÚpadding_bitsÚlast_valÚmaskÚ	num_bytesÚrawÚbit_bufÚ	bit_countÚout_idxr$   Ú	prev_highr#   Ú	code_units                  r   Ú_is_valid_utf7_b64r>   G   s¨  € õ  	ˆI‰Œ€AØ�Q‘€Jð  ‘?€LØ�aÒÐÝ˜y¨œ}Ô-ˆà�\Ñ! QÑ&ˆØ�d‰?ð 	Ø�5ð
 ˜a‘€IÝ
�IÑ
Ô
€CØ€GØ€IØ€GØð ð ˆØ˜a‘<¥;¨q¤>Ñ1ˆØ�Q‰ˆ	Ø˜Š>ˆ>Ø˜‰NˆIØ# yÑ0°DÑ8ˆC�‰LØ�q‰LˆGøØ€IÝ�1�i !‘m QÑ'Ô'ð ð ˆØ˜”V˜q‘[ C¨¨A©¤JÑ.ˆ	Ø�YÐ(Ð(Ò(Ð( &Ò(Ð(Ð(Ð(Ð(Øð Ø�u�uØˆIˆIØ�yÐ*Ð*Ò*Ð* FÒ*Ð*Ð*Ð*Ð*Øð Ø�u�uØˆIˆIàð Ø�u�uØˆIˆIØˆ=Ðr   Úposc                 ó¸   — t           t          d¦  «        z  }d}|dz
  }|dk    r5| |         }|dv r|dz  }Œ||v r|dz  }|t          k    rdS |dz  }nn|dk    °5dS )u  Return True if the ``+`` at *pos* is embedded in a base64 stream.

    Walks backward from *pos*, skipping CR/LF, and counts consecutive base64
    characters (including ``=`` for padding).  If 4 or more are found, the
    ``+`` is likely part of a PEM certificate, email attachment, or similar
    base64 blob rather than a real UTF-7 shift character.

    The walk stops at the fourth character rather than running to the start
    of the run.  Only the ``>= 4`` verdict is used, and newlines are
    skipped rather than ending the walk, so without the early exit a
    line-wrapped base64 blob â€” a PEM file, a MIME attachment, exactly what
    this guard exists to recognize â€” makes each ``+`` rescan everything
    before it, and the caller quadratic in the size of the blob.
    ó   =r   r-   >   é
   é   TF)r   Ú	frozensetr(   )r   r?   Úb64_with_padÚcountr#   Úbs         r   Ú_is_embedded_in_base64rH   „   s�   € õ $0µ)¸D±/´/Ñ#A€LØ€EØˆa‰€AØ
ˆqŠ&ˆ&Ø�ŒGˆØ�ÐÐØ�‰FˆAØØ�ÐÐØ�Q‰JˆEØÕ)Ò)Ð)Ø�tØ�‰FˆAˆAàð ˆqŠ&ˆ&ð ˆ5r   Úb64_datac                 óŠ   — t           | d                  dz  t           | d                  dz  z  t           | d                  z  dz	  S )z@Decode the first (only) UTF-16 code unit of a single-unit block.r   é   r-   r+   r   )r&   )rI   s    r   Ú_single_unitrL   ¥   sK   € õ 
�X˜a”[Ô	! RÑ	'Ý�x ”{Ô# qÑ(ñ	*å
�h˜q”kÔ
"ñ	#ð 
ñ	ð r   Úunitc                 óâ   — d| cxk    odk    nc p_d| cxk    odk    nc pOd| cxk    odk    nc p?d| cxk    odk    nc p/d	| cxk    od
k    nc pd| cxk    odk    nc pd| cxk    odk    nc S )z>Script ranges where a lone shifted character plausibly occurs.é€   iÿ  i   iÿ  i    iÿ+  i 0  iÿ0  i N  iÿŸ  i ¬  i£×  i ÿ  iïÿ  r!   )rM   s    r   Ú_plausible_lone_unitrP   ®   s  € ð 	�$Ð Ð Ò Ð ˜&Ò Ð Ð Ð ð 	$Ø�TÐ#Ð#Ò#Ð#˜VÒ#Ð#Ð#Ð#ð	$à�TÐ#Ð#Ò#Ð#˜VÒ#Ð#Ð#Ð#ð	$ð �TÐ#Ð#Ò#Ð#˜VÒ#Ð#Ð#Ð#ð	$ð �TÐ#Ð#Ò#Ð#˜VÒ#Ð#Ð#Ð#ð		$ð
 �TÐ#Ð#Ò#Ð#˜VÒ#Ð#Ð#Ð#ð	$ð �TÐ#Ð#Ò#Ð#˜VÒ#Ð#Ð#Ð#ðr   c                 ó  — d}t          t          | ¦  «        |¦  «        }	 |                      t          d¦  «        |¦  «        }|dk    s||k    rdS |dz   }|t          | ¦  «        k     r| |         t          d¦  «        k    r|dz   }Œi|t          | ¦  «        k     ry| |         t          d¦  «        k    r`|t          | ¦  «        k     rJ| |         t          d¦  «        k    r1|dz  }|t          | ¦  «        k     r| |         t          d¦  «        k    °1|}Œõt	          | |¦  «        r|}�Œ	|}||k     r)| |         t
          v r|dz  }||k     r| |         t
          v °||k    r&|t          | ¦  «        k     r| |         t
          v r|}�Œf||z
  }| ||…         }	|dk    rt          d	„ |	D ¦   «         ¦  «        s|}�Œ˜|dk    r?t          |	¦  «        r0|d
z  dz  dk    r"t          |	¦  «        }
t          |
¦  «        s|}�ŒÛdS t          ||¦  «        }�Œî)a&  Check that *data* contains at least one valid UTF-7 shifted sequence.

    A valid shifted sequence is ``+<base64 chars>`` terminated by either an
    explicit ``-`` or any non-Base64 character (per RFC 2152).  The base64
    portion must decode to valid UTF-16BE with correct zero-padding bits.
    The sequence ``+-`` is a literal plus sign and is **not** counted.

    *max_start* bounds where a shift may occur and *max_end* how far its
    base64 run may reach, so a run that begins inside the evidence window
    can finish beyond it (ADR-0006).  A run that outruns *max_end* is
    skipped rather than judged on the part that fits: the padding and
    surrogate checks read a cut run as a different run, and accepting on
    evidence we did not see is the one direction this must never fail in.
    r   Tú+r   Fr-   ú-é   c              3   ó(   K  — | ]}|t           v V — Œd S )N)r   )r"   rG   s     r   ú	<genexpr>z,_has_valid_utf7_sequences.<locals>.<genexpr>û   s'   è è € Ð#JÐ#J¸A A­Ð$7Ð#JÐ#JÐ#JÐ#JÐ#JÐ#Jr   r+   r,   )Úminr   r   ÚordrH   r   Úanyr>   rL   rP   Úmax)r   r   r   r   ÚlimitÚ	shift_posr?   r#   Úb64_lenrI   rM   s              r   Ú_has_valid_utf7_sequencesr^   »   sD  € ð €EÝ•�D‘	”	˜7Ñ#Ô#€EðGØ—I’I�c #™hœh¨Ñ.Ô.ˆ	Ø˜Š?ˆ?˜i¨9Ò4Ð4Ø�5Ø˜!‰mˆà•�T‘”Š?ˆ?˜t Cœy­C°©H¬HÒ4Ð4Ø˜!‘GˆEØð •�T‘”Š?ˆ?˜t Cœy­C°©H¬HÒ4Ð4Ø�˜D™	œ	’/�/ d¨3¤iµ3°s±8´8Ò&;Ð&;Ø�q‘�ð �˜D™	œ	’/�/ d¨3¤iµ3°s±8´8Ò&;Ð&;àˆEØõ " $¨	Ñ2Ô2ð 	ØˆEÙàˆØ�%Šiˆi˜D œG¥|Ð3Ð3Ø�‰FˆAð �%Šiˆi˜D œG¥|Ð3Ð3à�Š:ˆ:˜%¥# d¡)¤)Ò+Ð+°°Q´½<Ð0GÐ0GàˆEÙØ�c‘'ˆØ˜˜A˜”;ˆð �aŠ<ˆ<¥Ð#JÐ#JÀÐ#JÑ#JÔ#JÑ JÔ Jˆ<ØˆEÙð �aŠ<ˆ<Õ.¨xÑ8Ô8ˆ<ð ˜!‘ Ñ" aÒ'Ð'Ý# HÑ-Ô-�Ý+¨DÑ1Ô1ð Ø�EÙØ�4Ý�C˜‘”ˆñOGr   Nc                 ór   — d| v sd| v sd| v rdS d| v rdS d| v sd| v sd	| v sd
| v rd| v rd| v rdS dS d| v rdS dS )zŸName the ISO-2022 variant whose designations appear in *data*.

    :returns: ``(encoding, language)`` or ``None`` when no designation is
        present.
    s   $(Os   $(Ps   $(Q)Úiso2022_jp_2004Újas   (I)Úiso2022_jp_extra   s   $Bs   $@s   (Js   $(Dó   ó   )Úiso2022_jp_2ra   s   $)C)Ú
iso2022_krÚkoNr!   )r   s    r   Ú_iso2022_variantrh     s£   € ð �TÐÐ˜Z¨4Ð/Ð/°:ÀÐ3EÐ3EØ&Ð&ð �DÐÐØ%Ð%ð 	�TÐÐØ˜ÐÐØ˜ÐÐØ˜ÐÐð �dˆ?ˆ?˜w¨$˜˜Ø)Ð)ð $Ð#ð �TÐÐØ!Ð!àˆ4r   c                 ót  — d| v }d| v }d| v }|s|s|sdS |rJt          | ¦  «        }|�9t          | |d         ¦  «        r#t          |d         t          |d         ¬¦  «        S t	          t          | ¦  «        t          ¦  «        }t	          t          | ¦  «        dt          z  ¦  «        }|r@d	| v r<d
| v r8t          | ||¦  «        r't          | d¦  «        rt          dt          d¬¦  «        S |rL|                      ¦   «         r8t          | ||¦  «        r't          | d¦  «        rt          dt          d¬¦  «        S dS )aõ  Detect ISO-2022, HZ-GB-2312, and UTF-7 from escape/tilde/plus sequences.

    Every answer this stage returns decodes the whole window: a designation
    is three bytes that ordinary text can contain (``log: ESC $ B is the
    escape``), and a genuine ISO-2022 or HZ stream can still carry a bad
    byte past the header.  The gate costs one decode per hit only.

    :param data: The raw byte data to examine.
    :returns: A :class:`DetectionResult` if an escape encoding is found, or ``None``.
    ó   ó   ~ó   +Nr   r-   )ÚencodingÚ
confidenceÚlanguager   r   r   ÚhzÚzhzutf-7)
rh   r   r   r   rW   r   r   r   Úisasciir^   )r   Úhas_escÚ	has_tildeÚhas_plusÚvariantr   r   s          r   Údetect_escape_encodingrw   =  s•  € ð ˜ˆo€GØ˜�€IØ�tˆ|€Hàð ˜9ð ¨Xð Øˆtàð Ý" 4Ñ(Ô(ˆØÐÕ#8¸¸wÀq¼zÑ#JÔ#JÐÝ"Ø  œÝ3Ø  œðñ ô ð õ  •C˜‘I”IÕ1Ñ2Ô2€IÝ•#�d‘)”)˜QÕ!3Ñ3Ñ4Ô4€Gð 	ð
à�TˆMˆMØ�TˆMˆMÝ! $¨	°7Ñ;Ô;ð å! $¨Ñ-Ô-ð õ ØÝ/Øð
ñ 
ô 
ð 	
ð< 	ð

à�LŠL‰NŒNð

õ & d¨I°wÑ?Ô?ð

õ " $¨Ñ0Ô0ð	

õ ØÝ/Øð
ñ 
ô 
ð 	
ð ˆ4r   ) Ú__doc__Úchardet._utilsr   r   r   Úchardet.pipeliner   r   Úbytesr1   r
   Ú__annotations__ÚintÚboolr   r   rD   r   r   Ú	enumerater&   Údictr(   r>   rH   rL   rP   r^   ÚtupleÚstrrh   rw   r!   r   r   ú<module>rƒ      sq  ðð
ð 
ð 
ð TÐ SÐ SÐ SÐ SÐ SÐ SÐ SÐ SÐ SØ FÐ FÐ FÐ FÐ FÐ FÐ FÐ Fð �e˜E˜E $¨Ñ-Ô-Ñ.Ô.€ˆeÐ .Ð .Ñ .ð! ð !°#ð !Àð !Èð !ð !ð !ð !ðJ X€
ˆEÐ WÐ WÑ WØ(˜y¨Ñ4Ô4€ˆi˜ŒnÐ 4Ð 4Ñ 4ð "+ Ð+HÑ!IÔ!I€�	˜#”Ð IÐ IÑ Ið GÐF°	°	¸*Ñ0EÔ0EÐFÑFÔF€ˆT�#�s�(Œ^Ð FÐ FÑ Fð Ð �3Ð Ð Ñ ð: %ð :¨Dð :ð :ð :ð :ðz ð ¨Sð °Tð ð ð ð ðB˜5ð  Sð ð ð ð ð
˜sð 
 tð 
ð 
ð 
ð 
ðX Eð X°cð XÀCð XÈDð Xð Xð Xð Xðv$˜5ð $ U¨3°¨8¤_°tÑ%;ð $ð $ð $ð $ðN[ ð [¨?¸TÑ+Að [ð [ð [ð [ð [ð [r   