
    LLjq]                    *   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ  e
e      j                         j                  d   Zedz  Zed	z  Zed
z  Zej(                  j+                  d eedz               ddlmZmZmZ ddlmZmZmZmZm Z  dZ!e!dz   Z"dZ#d*dZ$d+dZ%d,dZ&d,dZ'd,dZ(d-dZ)d.dZ*d/dZ+d0dZ,d1d2dZ-d3dZ.d4dZ/ddd	 	 	 	 	 	 	 	 	 	 	 d5d Z0d6d!Z1d6d"Z2d7d#Z3d7d$Z4d7d%Z5d8d&Z6d9d'Z7d:d(Z8e9d)k(  r e8        yy);a]  Review generic VM opcode 0x2a parent-stream evidence.

Opcode 0x2a is grounded as a linked-object script fanout helper.  The remaining
question is narrower: do any currently grounded active-object scripts actually
use it, and do raw 0x2a-looking bytes in data sections line up with real command
boundaries instead of operands inside other commands?
    )annotationsN)Counter)Path)Any   outwebzHwanse2.exetools)offset_to_varead_sectionsva_to_offset)decode_commanddecode_streamhex32is_vasection_name_fori8D    W@ c                V    t        j                  | dd      S t        |       d      S )N T)quote)htmlescapestrvalues    7tools/build_generic_vm_opcode2a_parent_stream_review.pyhr   )   s%    ;;U]r$GGE
$GG    c                    | d S d| dS )N0x08x r   s    r   hxr$   -   s    =465+&66r   c                X    t        ||      }|t        dt        |       d      | |   S )NVA  is outside raw sections)r   
ValueErrorr$   exesectionsvaoffsets       r   u8r.   1   s6    (B'F~3r"vh&>?@@v;r   c                    t        ||      }|t        dt        |       d      t        j                  d| |      d   S )Nr&   r'   z<Hr   r   r(   r$   structunpack_fromr)   s       r   u16r3   8   F    (B'F~3r"vh&>?@@dC033r   c                    t        ||      }|t        dt        |       d      t        j                  d| |      d   S )Nr&   r'   <Ir   r0   r)   s       r   u32r7   ?   r4   r   c                0    t        ||      }|y| |||z    S )Nr   )r   )r*   r+   r,   sizer-   s        r   read_atr:   F   s'    (B'F~v&&r   c                r    | D ]2  }t        |d         }|t        |d         z   }||cxk  r	|k  s.|c S  4 y )Nr,   raw_sizeint)r+   r,   sectionstartends        r   section_for_varB   M   sO     GDM"c'*-..BN 
 r   c                r    | D ]2  }t        |d         }|t        |d         z   }||cxk  r	|k  s.|c S  4 y )Nrawr<   r=   )r+   r-   r?   r@   rA   s        r   section_for_offsetrE   V   sO     GEN#c'*-..F S N !
 r   c                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)pathfallbacks     r   	load_jsonrP   _   s*    ;;=::dnngn677r   c                L   t        j                  d|      }g }| j                  |      }|dk7  rut        ||      }t	        ||      }|@|>|j                  |t        |      |j                  d      d       t        |      |k\  r	 |S | j                  ||dz         }|dk7  ru|S )Nr6   name)refVarefVaHexr?   r   )	r1   packfindr   rE   appendr$   getlen)	r*   r+   	target_valimitneedlerowsposref_var?   s	            r   dword_refs_tora   e   s    [[y)F!#D
((6
C
)h,$Xs3'"5KK# "6
&{{62 4yE!K hhvsQw' ) Kr   c                    t        j                  d|      }d}| j                  |      }|dk7  r |dz  }| j                  ||dz         }|dk7  r |S )Nr6   r   rR   r   )r1   rV   rW   )r*   r[   r]   countr_   s        r   
refs_countrd   z   sY    [[y)FE
((6
C
)
hhvsQw' ) Lr   c                f    	 t        | ||      S # t        t        j                  t        f$ r Y y w xY w)N)r   r(   r1   error
IndexError)r*   r+   r,   s      r   safe_decode_commandrh      s3    c8R00j1 s    00   i`  max_commands	max_bytesc                   	 t        | ||||      }|j                  dg       }t        |t              r|S g S # t        t        j                  t        f$ r g cY S w xY w)Nrj   commands)r   r(   r1   rf   rg   rY   
isinstancelist)r*   r+   r,   rk   rl   decodedrn   s          r   safe_decode_streamrr      sa    XrXab {{:r*H!(D189r9 j1 	s   8  AAc                   t        ||      }|g S t        |d         }t        ||dz
        }|| dz  z  }g }t        ||d      D ]  }|}g }	g }
t        d      D ]  }||k(  r<|j	                  |t        |      t        | |      t        |	      |	dd  |
dd  d        X||kD  s|dz   |dz   kD  r jt        | ||      }|s {t        |j                  d	      xs d
      }|d
k  r |	j	                  t        |j                  dd                   |
j	                  t        |j                  dd                   ||z  }  |j                  d        |d d S )Nr,         0   i)startVa
startVaHexstartRefCountcommandCountBefore	opsBeforecommandStartsBeforer       lengthr   	opcodeHexr   vaHexc                <    t        | d          t        | d         fS )Nry   rz   r=   items    r   <lambda>z'parent_boundary_paths.<locals>.<lambda>   s#    #d?&;"<!<c$G[B\>] ^ r   key
   )rB   r>   maxrangerX   r$   rd   rZ   rh   rY   r   sort)r*   r+   r,   r?   start_valowerpathsr@   cursoropsstarts_rowr~   s                 r   parent_boundary_pathsr      s}   Xr*G	74=!H"t)$E	uf\E"$Eub!$ r 	A|#(&(i)3C)?.1#h%(Y/5cd|	 {fqj294%c8V<C*/a0F{JJs377;345MM#cgggr234fF/		8 
JJ^J_":r   c                   t        ||      }|g S t        t        |d         |dz
        }|| dz  z  }g }t        ||d      D ]  }t	        | ||      }|st        |j                  d      xs d      }||cxk  r	||z   k  s@n C|j                  |t        |      |j                  d      |j                  d      ||j                  d      |j                  d	      d
        |j                  d        |d d S )Nr,   (   ru   r~   r   r   
opcodeNamerawHexsummary)coverStartVacoverStartVaHexr   r   r~   r   r   c                :    t        | d         t        | d         fS )Nr   r~   r=   r   s    r   r   z)interior_operand_covers.<locals>.<lambda>   s    D$8 93tH~;NO r   r      )	rB   r   r>   r   rh   rY   rX   r$   r   )	r*   r+   r,   r?   r   r^   r@   r   r~   s	            r   interior_operand_coversr      s   Xr*G	GDM"BI.E	uf\E!#Dub!$ !#x7SWWX&+!,2&&KK$)')%y!$!5"%'',"7$!ggh/"wwy1
" 	IIOIP8Or   c                   dt         dt        j                  dt              j	                  d      ddddd	dd
ddddddddddddddg}g }|D ]  }t
        j                  t        |d               }t        | |t        |d         t        |            }|j                  |d   t        t        |d               |d   ||k(  |d   |j	                  d      d        |S )Nzhandler-table-entryz<generic handler table opcode 0x2a entry points at 0x004057ebr6    )idr,   meaningexpectedHexzfanout-list-selectr   zlstream+1 active-object list slot; high bit selects restore-child-cursor branch; stream+2 is object+0x14 maskzM55 8b ec 83 ec 0c 53 56 57 8b 45 08 8b 40 40 33 c9 8a 48 01 83 e1 7f 89 4d f4zfanout-restore-child-cursoriX@ zbhigh-bit branch saves target object+0x40, runs stream+4 through 0x00402321, restores target cursorz8b 45 f8 8b 40 40 89 45 fc 8b 45 08 8b 40 40 8b 40 04 8b 4d f8 89 41 40 8b 45 f8 50 e8 6d ca ff ff 83 c4 04 8b 45 fc 8b 4d f8 89 41 40zfanout-persistent-child-cursoriX@ zVnormal branch writes stream+4 into target object+0x40 and keeps advanced target cursorzP8b 45 08 8b 40 40 8b 40 04 8b 4d f8 89 41 40 8b 45 f8 50 e8 44 ca ff ff 83 c4 04zparent-stream-advanceiX@ z=parent object+0x40 advances by +0x08 after opcode 0x2a fanoutz28b 45 08 83 40 40 08 c7 05 b8 a1 55 00 00 00 00 00r   r,   r   r   )r   r   r   matchesr   	actualHex) OPCODE_2A_HANDLER_TABLE_ENTRY_VAr1   rV   OPCODE_2A_HANDLER_VAhexbytesfromhexr   r:   r>   rZ   rX   r$   )r*   r+   snippetsr^   r   expectedactuals          r   verify_snippetsr      s!    (2U!;;t-ABFFsK		
 ' Fj		
 0{ d		
 3om		
 *VO		
3H@ "$D 
==T-%8!9:hDJXG4jCT
O,	?!X-#M2#ZZ_		

 Kr   c                   t        t        dz  i       }t        |t              r|j	                  dg       ng }g }|D ]  }|j	                  d      }t        |t
              rt        ||      s1t        | ||dd      }|D ]e  }|j	                  d      dk(  s|j                  |t        |      |j	                  d	      |j	                  d
      |j	                  d      |d       g  |S )Nz#active_object_script_inventory.jsonscriptsscriptVax   i   rj   opcode*   r,   r   classification)r   scriptVaHex	commandVacommandVaHexr   command)
rP   OUTro   dictrY   r>   r   rr   rX   r$   )	r*   r+   	inventoryr   hitsscript	script_varn   r   s	            r   active_object_hitsr     s    # EErJI.8D.IimmIr*rG!#D JJz*	)S)x1K%c8YS\ab 	G{{8$,$-'))}%,[[%6(/G(<*0**5E*F#*		" Kr   c                |   g }|D ]  }|j                  d      dvrt        |d         }|t        |d         z   }| || }|j                  d      }|dk7  sS||z   }t        ||      }	|	|	dz   t        |d         t        |d         z   k  rpt	        | ||	d	z         }
t        ||
      rSt        | ||	      }|rC|j                  d
      dk(  r.t        |j                  dd            }t        |j                  dd            }t        | ||
dd      }t        | ||	      }t        | ||	      }t        | |	      }t        | |
      }|r|d   nd }t        |      xr | }d}|r|dz  }|r|dz  }|r t        |j                  dd            r|dz  }|dk  r|dz  }|dk(  r|dz  }|r|dz  }|rd}n|rd}nd}|j                  i d|	dt        |	      d|j                  d      d|d|dt        | ||	d      j                  d       d|d!|j                  d!      d"|j                  d"      d|d#|j                  d#      d$|
d%t        |
      d&t!        ||
      d'|d(|d)t#        |      |t#        |      ||d d* D cg c]  }|j                  d+       c}|d d, D cg c]E  }|j                  d-      |j                  d+      |j                  d.      |j                  d/      d0G c}d1d2       |j                  d|dz         }|dk7  rĐ |j%                  d3 4       |S c c}w c c}w )5NrS   >   .data.rdatarD   r<      *rR   r   r,   ru   r   r   slotr   
filterMask      rj         ry   r      review-boundary-candidatelikely-operand-false-positiveraw-va-shaped-candidatecandidateVacandidateVaHexr?   r   scorer   r   rawSlotrestoreCursorfilterMaskHexchildScriptVachildScriptVaHexchildSectioncommandRefCountchildRefCountparentBoundaryPathCountr   r      r   r   r   )r   r   r   r   F)bestParentBoundaryinteriorCoverCountinteriorCoverschildOpschildCommandPreviewpromotedProducerc                J    | d   dk7  t        | d          t        | d         fS )Nr   r   r   r   r=   r   s    r   r   z%scan_raw_candidates.<locals>.<lambda>{  s4    !"&AAg]#$
 r   r   )rY   r>   rW   r   r7   r   rh   rr   r   r   rd   boolrX   r$   r:   r   r   rZ   r   )r*   r+   r^   r?   	raw_startraw_enddataindexr-   r,   childr   r   maskchild_commandsparent_pathscoverscommand_ref_countchild_ref_count	best_pathlikely_interiorr   r   r   s                           r   scan_raw_candidatesr   )  s   !#D N2;;v&99'	c'*"5669W%		'"rk&Fh/B~"q&C,>WZEXAY,Y"YC2625)1#xDG7;;x#8D#@"7;;vq#9:"7;;|Q#?@);C5_amq)r'<S(B'O!8h!K,6sB,?)*4S%*@7CLO	*.v,*K|;K !,!QJE'!QJE$Y]]?A-N)O!QJE4<!QJE19!QJE*!QJE'-HN,-LN-FN  -r  0"R&  !*7;;v+>  !1.	 
 !(  !)'#xR*H*L*LS*Q  !'  !*7;;y+A  !0_1M  !-d  !0_1M  !0  !3BuI  !/0@50Q  !23D   !0! " !:3|;L# $ 7@69&k28M[\_]_M`,acSWW[-A,a 0>bq/A8" ), 251A58WW[5I69ggl6K367793E	%&8" 5:? "F IIguqy1EO rkN2^ 	II
   K+ -b8"s   
L4+A
L9c                 l   t         j                         } t        |       }t        | |      }t	        | |      }t        | |      }t        d |D              }|D cg c]  }|d   dk(  rt        |d         dk\  r| }}t        t              t        t              t        d |D              t        |      t        |      t        |      t        |      |j                  dd      |j                  dd      |j                  d	d      t        |      d|r|d   d
   nd dd}ddd|||||d d g dd	S c c}w )Nc              3  &   K   | ]	  }|d      yw)r   Nr#   .0r   s     r   	<genexpr>zbuild_report.<locals>.<genexpr>  s     Fc*+Fs   r   r   r   r   c              3  ,   K   | ]  }|d    s	d  yw)r   r   Nr#   r   s     r   r   zbuild_report.<locals>.<genexpr>  s     #L#S^A#Ls   
r   r   r   r   aZ  Opcode 0x2a is a grounded linked-object script fanout primitive, but it is not present in the current strict active-object +0xec script inventory. Raw data-section bytes that look like 0x2a commands are mostly operand/data hits; review-boundary candidates remain route-neutral because no parent stream is linked to a scene/map/encounter producer.)handlerTableEntryVaHexhandlerVaHexverifiedSnippetCountverifiedSnippetTotalactiveObjectEcOpcode2aCountrawCandidateCountclassificationCountsboundaryCandidateCountinteriorOperandLikelyCountrawVaShapedCandidateCountstrongBoundaryCandidateCountpromotedParentProducerCountmostReviewableCandidateVaHexdecisionz/hwanse-generic-vm-opcode2a-parent-stream-reviewz7tools/build_generic_vm_opcode2a_parent_stream_review.pyz8opcode-0x2a-grounded-fanout-no-active-ec-parent-producerri   )z[A raw byte 0x2a plus a VA-shaped dword at +4 is not enough to claim an opcode 0x2a command.z|A command-boundary candidate is not a route producer unless its parent/root is linked to gameplay scene/map/encounter state.ziThe active object +0xec inventory remains a negative result for opcode 0x2a after the decoder correction.)	kindsourcestatusr   verifiedSnippetsactiveObjectEcHitsrawCandidatestopReviewCandidates	nonClaims)EXE
read_bytesr   r   r   r   r   r>   r$   r   r   sumrZ   r   rY   )	r*   r+   r   active_hitsraw_candidatesclassesr   strong_candidatesr   s	            r   build_reportr    s]   
..
CS!HsH-H$S(3K(h7NF~FFG " $??CGDUYZDZ 	  #%%E"F/0 ##L#L L #H'*;'7 0 $W")++.I1"M&-kk2QST&U%,[[1JA%N(+,=(>'(Rc(9!(<=M(NimIG, BKL$)'-cr2
 5s   "D1c                   | d   }d| d   fd|d   fd|d   fd|d   fd	|d
   fd|d   fd|d   fg}dj                  d |D              }dj                  d | d   D              }dj                  d | d   D              xs d}dj                  d | d   D              xs d}dj                  d | d   D              }t        j                  | d      }dt        |d          d| d | d!| d"| d#| d$t        |       d%S )&Nr   r  zactive +ec 0x2ar   zraw candidatesr   zboundary candidatesr   zlikely operandsr   zstrong boundaryr  zpromoted producersr  r   c              3  X   K   | ]"  \  }}d t        |       dt        |       d $ yw)z<div class='card'><b>z
</b><span>z</span></div>Nr   )r   kvs      r   r   zrender_html.<locals>.<genexpr>  s/     fX\XY[\/!vZ!v]Sfs   (*c              3     K   | ]R  }d t        |d          dt        |d          dt        |d          dt        |d          dt        |d	          d
 T yw)z<tr><td>r   z
<br><code>r   </code></td><td>r   	</td><td>r   </td><td><code>r   </code></td></tr>Nr  r   s     r   r   zrender_html.<locals>.<genexpr>  s}       	T|nJqW&7 8Y ! "Y ! "s;'() *		s   AAr  c               3    K   | ]  }d t        |d          dt        |d          dt        |d          dt        |d          dt        |d	          d
t        |d          dt        |d          dt        |d          dt        dj                  |d                dt        |d          dt        |d          dt        |d          dt        |d          dt        |d          d  yw)<tr><td><code>r   z</code><br>r   r  r   z</td><td>slot r   z<br>restore r   z<br>mask <code>r   </code></td><td><code>r   r   z<br>ops z, r   z</td><td>cmd refs r   z<br>child refs r   z<br>boundary r   z<br>interior r   r  r   r  N)r   joinr   s     r   r   zrender_html.<locals>.<genexpr>  so     
 	s+,-.k!C@P<Q:R9S TW  c&kN#<#o2F0G/HXYZ]^mZnXoWp qs-./0Ac.>Q<R;SS[\]^b^g^ghklvhw^x\y[z {#/012/!CDXBYAZZghijm  oH  kI  iJ  hK  KX  YZ  [^  _s  [t  Yu  Xv vs8}%& '	
s   C.C0r  z"<tr><td colspan='6'>none</td></tr>c              3     K   | ]b  }d t        |d          dt        |d          dt        |d          dt        |j                  d      xs i j                  d             d		 d y
w)r   r   r!  r   r  r   r  r   r   z
</td></tr>N)r   rY   r   s     r   r   zrender_html.<locals>.<genexpr>  s       	s=)*+ ,s>*+, -%&'( )#'')$*//	:;< =		s   A(A*r	  z"<tr><td colspan='4'>none</td></tr>c              3  :   K   | ]  }d t        |       d  yw)z<li>z</li>Nr  )r   r   s     r   r   zrender_html.<locals>.<genexpr>  s     O44$y.Os   r  F)ensure_asciiaI  <!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Generic VM Opcode 0x2A Parent Stream Review</title>
  <style>
    body { margin:0; background:#101318; color:#edf1f7; font-family:system-ui,sans-serif; }
    main { max-width:1320px; margin:0 auto; padding:24px; }
    a { color:#8ecbff; }
    .nav { display:flex; flex-wrap:wrap; gap:8px; margin-bottom:16px; }
    .chip { border:1px solid #334155; border-radius:999px; padding:6px 10px; text-decoration:none; background:#161b22; }
    .summary { display:grid; grid-template-columns:repeat(auto-fit,minmax(170px,1fr)); gap:12px; margin:16px 0; }
    .card { border:1px solid #2b3544; border-radius:8px; padding:12px; background:#161b22; }
    .card b { display:block; color:#9fb1c9; font-size:12px; text-transform:uppercase; }
    .card span { display:block; margin-top:8px; font-size:18px; overflow-wrap:anywhere; }
    section { border:1px solid #273244; border-radius:10px; padding:16px; margin:16px 0; background:#141922; overflow:auto; }
    table { border-collapse:collapse; width:100%; min-width:980px; font-size:13px; }
    th,td { border-bottom:1px solid #283342; padding:8px; text-align:left; vertical-align:top; }
    th { color:#b9c7dc; background:#111722; }
    code { color:#dbeafe; overflow-wrap:anywhere; }
    pre { white-space:pre-wrap; background:#0b0f14; border:1px solid #253044; border-radius:8px; padding:12px; max-height:360px; overflow:auto; }
  </style>
</head>
<body>
<main>
  <div class="nav">
    <a class="chip" href="index.html">index</a>
    <a class="chip" href="static_analysis_remaining_work.html">remaining work</a>
    <a class="chip" href="generic_vm_stream_producer_frontier_review.html">generic VM frontier</a>
  </div>
  <h1>Generic VM Opcode 0x2A Parent Stream Review</h1>
  <p>r  z</p>
  <div class="summary">z</div>
  <section>
    <h2>Verified Handler Evidence</h2>
    <table><thead><tr><th>snippet</th><th>matches</th><th>meaning</th><th>actual bytes</th></tr></thead><tbody>z</tbody></table>
  </section>
  <section>
    <h2>Strict Active Object +0xEC Hits</h2>
    <table><thead><tr><th>script</th><th>command</th><th>classification</th><th>summary</th></tr></thead><tbody>z</tbody></table>
  </section>
  <section>
    <h2>Top Raw Candidates</h2>
    <table><thead><tr><th>candidate</th><th>score</th><th>operands</th><th>child</th><th>evidence</th><th>raw</th></tr></thead><tbody>zJ</tbody></table>
  </section>
  <section>
    <h2>Non-Claims</h2>
    <ul>z</ul>
  </section>
  <section>
    <h2>Raw JSON</h2>
    <pre id="payload"></pre>
  </section>
</main>
<script type="application/json" id="report-json">a  </script>
<script>
  const data = JSON.parse(document.getElementById("report-json").textContent);
  document.getElementById("payload").textContent = JSON.stringify(data, null, 2);
  window.HWANSE_GENERIC_VM_OPCODE2A_PARENT_STREAM_REVIEW = data;
</script>
</body>
</html>
)r"  rK   dumpsr   )	reportr   cards	card_htmlsnippet_rowscandidate_rowsactive_rows
non_claimspayloads	            r   render_htmlr/    s   YG	6(#$	G$ABC	7#678	(@ AB	G$@AB	G$BCD	w'DEFE f`effI77  ,- L WW 
 /0
 
 
. 
.  ''  ./  . 
.  O6+;NOOJjje4G @ 
 !{ #p q}o} ~q r}p} ~G HV  GW W	 
 2 34G* >o? ?r   c                 X   t               } t        j                  d       t        j                  d       t        dz  j	                  t        j                  | dd      dz   d	       t        d
z  j	                  t        |       d	       t        d| d   d    d| d   d    d       y )NT)exist_okz-generic_vm_opcode2a_parent_stream_review.jsonFr   )r%  indent
rG   rH   z-generic_vm_opcode2a_parent_stream_review.htmlz(wrote opcode 0x2a parent stream review (r   r   z active +ec hits, r  z  strong raw boundary candidates))	r  r   mkdirWEB
write_textrK   r&  r/  print)r'  s    r   mainr8  #  s    ^FIItIIItI::FF

6a84? G  	::FFF G  
	9;<==O);<
==]	_r   __main__)r   r   returnr   )r   z
int | Noner:  z
str | None)r*   r   r+   list[dict[str, Any]]r,   r>   r:  r>   )
r*   r   r+   r;  r,   r>   r9   r>   r:  r   )r+   r;  r,   r>   r:  dict[str, Any] | None)r+   r;  r-   r>   r:  r<  )rN   r   rO   r   r:  r   )r   )
r*   r   r+   r;  r[   r>   r\   r>   r:  r;  )r*   r   r[   r>   r:  r>   )r*   r   r+   r;  r,   r>   r:  r<  )r*   r   r+   r;  r,   r>   rk   r>   rl   r>   r:  r;  )r*   r   r+   r;  r,   r>   r:  r;  )r*   r   r+   r;  r:  r;  )r:  dict[str, Any])r'  r=  r:  r   )r:  None):__doc__
__future__r   r   rK   r1   syscollectionsr   pathlibr   typingr   __file__resolveparentsROOTr   r5  r  rN   insertr   probe_exe_scene_tablesr   r   r   'summarize_object_payload_442c75_callersr   r   r   r   r   GENERIC_HANDLER_TABLE_VAr   r   r   r$   r.   r3   r7   r:   rB   rE   rP   ra   rd   rh   rr   r   r   r   r   r   r  r/  r8  __name__r#   r   r   <module>rN     sh   #    
    H~''*
Ul
Ul
] 3tg~& ' L L  & #;h#F  ! H744'8* :	:": 	:
 : : : %P8/d0Xv/djZ& zF r   