
    *pj&                    >   % S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	J
r
Jr  SSKJr  SSKJrJr  S	S
S.rS\S'   Sr\R(                  " S\R*                  5      r\R(                  " S\R*                  5      r\R(                  " S\R*                  5      r\R(                  " S\R2                  5      r\R(                  " S\R*                  5      r\R(                  " S\R*                  5      r\" \" SS5      5      rSr SS jr!SS jr"SS jr#        SS jr$S S jr%g)!z<Stage 1b: charset declaration extraction (HTML/XML/PEP 263).    )annotationsN)decodes_without_error)EncodingEra)DETERMINISTIC_CONFIDENCEDetectionResultPipelineContext)compute_structural_score)REGISTRYlookup_encoding)	shift_jiscp932)euc_krcp949)shift_jis_2004r   zdict[str, tuple[str, str]]_MARKUP_SUPERSET_PROMOTIONSi   s*   <\?xml[^>]+encoding\s*=\s*['"]([^'"]+)['"]s,   <meta[^>]+charset\s*=\s*['"]?\s*([^\s'">;]+)s6   <meta[^>]+content\s*=\s*['"][^'"]*charset=([^\s'">;]+)s&   ^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)z<(?:meta|\?xml)[^>]*zI(?:charset|encoding)\s*=\s*[^\sA-Za-z0-9._-]?\s*([A-Za-z][A-Za-z0-9._-]+)      g      ?c           	     .   [        U 5      [        U R                  S[        5      5      -
  nU[        U 5      [        -  :  a  gU R	                  SSS9n[
        R                  U5       H  n[        R                  UR                  S5      5       H  n[        UR                  S5      R                  5       5      nUc  M0  [        U   R                  [        R                  -  (       d  M[  [        X5      (       d  Mm  [!        U["        SSS9s  s  $    M     g)	u  Look for a charset declaration in EBCDIC-encoded markup.

The ASCII regexes cannot see declarations in EBCDIC bytes, so when the
head looks like EBCDIC text (dominated by high bytes), decode it through
cp037 — the EBCDIC page whose letter and digit positions are shared by
all variants — and scan the decoded text.  Only declarations naming a
MAINFRAME-era encoding are honoured, and the declared encoding must
actually decode the head.
Ncp037replace)errorsr      	text/htmlencoding
confidencelanguage	mime_type)len	translate_MARKUP_HIGH_BYTES_EBCDIC_SCAN_MIN_HIGH_FRACTIONdecode_EBCDIC_TAG_REfinditer_EBCDIC_DECL_REgroupr   stripr
   erar   	MAINFRAMEr   r   r   )head
high_countdecodedtagmatchr   s         S/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/chardet/pipeline/markup.py_detect_ebcdic_declarationr1   I   s     TS6H!IJJJCI >>>kk')k4G
 &&w/$--ciil;E&u{{1~';';'=>H$X&**[-B-BBB)$99&%7!)	  < 0     c                ~   SU SS ;  a  gSR                  U R                  SS5      SS 5      n[        R                  U5      nU(       a]   UR	                  S5      R                  S5      R                  5       n[        U5      nUb   [        X5      (       a  [        U[        SSS	9$ g! [        [        4 a     gf = f)
a.  Check the first two lines of *data* for a PEP 263 encoding declaration.

PEP 263 declarations (e.g. ``# -*- coding: utf-8 -*-``) are only valid
on line 1 or line 2 of a Python source file.

:param data: The raw byte data to scan.
:returns: A :class:`DetectionResult` with confidence 0.95, or ``None``.
   #N      
   r   asciiztext/x-pythonr   )joinsplit
_PEP263_REsearchr'   r#   r(   UnicodeDecodeError
ValueErrorr   _validate_bytesr   r   )datafirst_two_linesr/   raw_namer   s        r0   _detect_pep263rC   l   s     4:jjE1!5bq!9:Oo.E	{{1~,,W5;;=H #8,OD$C$C"!3)	   #J/ 		s   .B) )B<;B<c                   U (       d  gU S[          n[        [        [        4 H  nUR	                  U5      nU(       d  M   UR                  S5      R                  S5      R                  5       n[        U5      nUc  M\  [        X5      (       d  Mn  U[        L a  SOSn[        U[        SUS9s  $    [        U5      nUb  U$ [        U 5      $ ! [        [        4 a     M  f = f)av  Scan the first bytes of *data* for a charset declaration.

Checks for:

1. ``<?xml ... encoding="..."?>``
2. ``<meta charset="...">``
3. ``<meta http-equiv="Content-Type" content="...; charset=...">``
4. PEP 263 ``# -*- coding: ... -*-`` (first two lines only)

:param data: The raw byte data to scan.
:returns: A :class:`DetectionResult` with confidence 0.95, or ``None``.
Nr   r8   ztext/xmlr   r   )_SCAN_LIMIT_XML_ENCODING_RE_HTML5_CHARSET_RE_HTML4_CONTENT_TYPE_REr<   r'   r#   r(   r=   r>   r   r?   r   r   r1   rC   )r@   r+   patternr/   rB   r   r   ebcdic_results           r0   detect_markup_charsetrK      s     D$&79OPt$5 ;;q>009??A 'x0H#(G(G*15E*EJ;	&%7!'	  Q" /t4M $! '
3 s   .CC! C!c                   UR                   c  U$ [        R                  UR                   5      nUc  U$ Uu  pEXR;  a  U$ [        U   n[	        X5      (       d  U$ [	        X5      (       d,  [        UUR                  UR                  UR                  5      $ [        5       n[        U [        UR                      U5      n[        XU5      n	X:  a,  [        UUR                  UR                  UR                  5      $ U$ )al  Promote a markup-declared encoding to its superset when structural evidence supports it.

If the declared encoding has a known superset (per
:data:`_MARKUP_SUPERSET_PROMOTIONS`), the superset validates the data,
and the superset's structural score is materially better, return a new
result using the superset encoding.  Otherwise return *markup_result*
unchanged.
)r   r   getr
   r   r   r   r   r   r   r	   )
r@   markup_resultallowed	promotionreported_codecsuperset_namesuperset_infoctx
base_scoresuperset_scores
             r0   promote_markup_supersetrW      s    %+//0F0FGI$-!N#]+M 55
 !66$$""##	
 	
 
C)$9O9O0PRUVJ-d3GN"$$""##	
 	
 r2   c                (    [        U S[         U5      $ )zCheck that *data* can be decoded under *encoding* without errors.

Only validates the first ``_SCAN_LIMIT`` bytes to avoid decoding a
full 200 kB input just to verify a charset declaration found in the
header.
N)r   rE   )r@   r   s     r0   r?   r?      s     !l{!3X>>r2   )r+   bytesreturnDetectionResult | None)r@   rY   rZ   r[   )r@   rY   rN   r   rO   zfrozenset[str]rZ   r   )r@   rY   r   strrZ   bool)&__doc__
__future__r   rechardet._utilsr   chardet.enumsr   chardet.pipeliner   r   r   chardet.pipeline.structuralr	   chardet.registryr
   r   r   __annotations__rE   compile
IGNORECASErF   rG   rH   	MULTILINEr;   r$   r&   rY   ranger!   r"   r1   rC   rK   rW   r?    r2   r0   <module>rl      s-   B " 	 0 % W W @ 6 -!; 7 
 ::6  JJ8"--  BBMM  ZZBBLLQ
 3R]]C**PMM 5u-.  "&  F>' T/
/"/ / 	/d?r2   