o
    �õ±j¿-  ã                   @   s`   d dl Z d dlZddlT ddlmZ G dd„ dƒZG dd„ dƒZG d	d
„ d
ƒZG dd„ dƒZdS )é    Né   )Ú*)ÚPROCESSOR_ARCHITECTUREc                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚVirtualSegmentc                 C   s   || _ || _|| _d | _d S ©N)ÚstartÚendÚstart_file_addressÚdata)Úselfr   r   r	   © r   úŠ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/minidump/minidumpreader.pyÚ__init__   s   
zVirtualSegment.__init__c                 C   s   | j |ko	|| jkS r   )r   r   )r   r   r   r   r   r   Úinrange   s   zVirtualSegment.inrangeN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      s    	r   c                   @   s6   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚMinidumpBufferedMemorySegmenté (  c                 C   s6   |j | _|j| _|j|j  | _|j| _|| _g | _d S r   )Ústart_virtual_addressÚstart_addressÚend_virtual_addressÚend_addressÚ
total_sizer	   Ú	chunksizeÚchunks)r   Úmemory_segmentÚfile_handler   r   r   r   r      s   
z&MinidumpBufferedMemorySegment.__init__c                 C   s   | j |  ko| jk S   S r   )r   r   ©r   Úpositionr   r   r   r   !   s   z%MinidumpBufferedMemorySegment.inrangec                 C   s   |   |¡sd S | j| S r   )r   r   r   r   r   r   Úremaining_len$   s   

z+MinidumpBufferedMemorySegment.remaining_lenc                 C   s   |   |dd¡}| ||¡S )Nr   éÿÿÿÿ)ÚreadÚfind)r   r   ÚpatternÚstartposr
   r   r   r   r#   )   s   z"MinidumpBufferedMemorySegment.findc                 C   sB  |d u r|  | j| ¡ | | j| j|  ¡S | jD ]}| ||¡r1|j||j ||j …   S q| jd| j	 krc| j}t
d|| jƒ}|  | j¡ | |¡|_| j |¡ |j||j ||j … S t|| | j	ƒ}|| | jkrw| j| }t
||| | j| ƒ}|  |j¡ | |¡|_| j |¡ |j||j ||j … S )Né   r   )Úseekr	   r"   r   r   r   r
   r   r   r   r   ÚappendÚmax)r   r   r   r   Úchunkr   Úvsr   r   r   r"   -   s,   
ÿ
z"MinidumpBufferedMemorySegment.readN©r   )r   r   r   r   r   r    r#   r"   r   r   r   r   r      s    
r   c                   @   s¦   e Zd Zd*dd„Zdd„ Zdd„ Zd+d	d
„Zdd„ Zd,dd„Zdd„ Z	dd„ Z
d-d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d.d(d)„ZdS )/ÚMinidumpBufferedReaderr   c                 C   s"   || _ || _g | _d | _d | _d S r   )ÚreaderÚsegment_chunk_sizeÚmemory_segmentsÚcurrent_segmentÚcurrent_position)r   r.   r/   r   r   r   r   L   s
   
zMinidumpBufferedReader.__init__c                 C   s~   | j D ]}| |¡r|| _|| _ dS q| jj D ] }| |¡r8t|| jj| jd�}| j  |¡ || _|| _ dS qt	d| ƒ‚)z

		N)r   z4Memory address 0x%08x is not in process memory space)
r0   r   r1   r2   r.   r   r   r/   r(   Ú	Exception)r   Úrequested_positionr   Ú
newsegmentr   r   r   Ú_select_segmentT   s   

ý
ûz&MinidumpBufferedReader._select_segmentc                 C   ó   | j S r   )r.   ©r   r   r   r   Ú
get_readerj   ó   z!MinidumpBufferedReader.get_readerr   c                 C   sf   |dkr| j j| }n|dkr| j| }n|dkr | j j| }ntdƒ‚| j  |¡s.tdƒ‚|| _dS )aD  
		Changes the current address to an offset of offset. The whence parameter controls from which position should we count the offsets.
		0: beginning of the current memory segment
		1: from current position
		2: from the end of the current memory segment
		If you wish to move out from the segment, use the 'move' function
		r   r   r&   z.Seek function whence value must be between 0-2z5Seek would cross memory segment boundaries (use move)N)r1   r   r2   r   r3   r   )r   ÚoffsetÚwhenceÚtr   r   r   r'   m   s   zMinidumpBufferedReader.seekc                 C   s   |   |¡ dS )z@
		Moves the buffer to a virtual address specified by address
		N)r6   )r   Úaddressr   r   r   Úmove„   s   
zMinidumpBufferedReader.moveNc                 C   sT   |du r| j jjtjkrd}nd}| j| }|dkrdS || | }|  |d¡ dS )zD
		Repositions the current reader to match architecture alignment
		Né   é   r   r   )r.   ÚsysinfoÚProcessorArchitecturer   ÚAMD64r2   r'   )r   Ú	alignmentr;   Úoffset_to_alignedr   r   r   Úalign‹   s   
zMinidumpBufferedReader.alignc                 C   r7   )z)
		Returns the current virtual address
		)r2   r8   r   r   r   Útell›   s   zMinidumpBufferedReader.tellc                 C   sH   | j | }| j |d ¡stdƒ‚| j | jj| j | jj || jj ¡S )z@
		Returns up to length bytes from the current memory segment
		r   ú#Would read over segment boundaries!)r2   r1   r   r3   r"   r.   r   r   )r   Úlengthr=   r   r   r   Úpeek¡   s   
&zMinidumpBufferedReader.peekr!   c                 C   s¬   |dk rt dƒ‚|dkr-| j | j¡}|sdS | j}| jj| _| j | jj|| jj d¡S | j| }| j 	|d ¡s>t dƒ‚| j}|| _| j | jj|| jj || jj ¡S )z‰
		Returns data bytes of size size from the current segment. If size is -1 it returns all the remaining data bytes from memory segment
		r!   zYou shouldnt be doing thisNr   rI   )
r3   r1   r    r2   r   r"   r.   r   r   r   )r   Úsizer=   Úold_new_posr   r   r   r"   ª   s   

$zMinidumpBufferedReader.readc                 C   ó<   | j jjtjkrtj|  d¡ddd�S tj|  d¡ddd�S )z®
		Reads an integer. The size depends on the architecture.
		Reads a 4 byte small-endian singed int on 32 bit arch
		Reads an 8 byte small-endian singed int on 64 bit arch
		r@   ÚlittleT©Ú	byteorderÚsignedrA   ©r.   rB   rC   r   rD   ÚintÚ
from_bytesr"   r8   r   r   r   Úread_intÁ   ó   zMinidumpBufferedReader.read_intc                 C   rN   )z²
		Reads an integer. The size depends on the architecture.
		Reads a 4 byte small-endian unsinged int on 32 bit arch
		Reads an 8 byte small-endian unsinged int on 64 bit arch
		r@   rO   FrP   rA   rS   r8   r   r   r   Ú	read_uintÌ   rW   z MinidumpBufferedReader.read_uintc                 C   s(   | j  | jj|¡}|dkrdS || j S )z:
		Searches for a pattern in the current memory segment
		r!   )r1   r#   r.   r   r2   )r   r$   Úposr   r   r   r#   ×   s   
zMinidumpBufferedReader.findc                 C   sD   g }d}	 | j  | jj||d ¡}|dkr	 |S | || j j ¡ q)zp
		Searches for all occurrences of a pattern in the current memory segment, returns all occurrences as a list
		r!   Tr   )r1   r#   r.   r   r(   r   )r   r$   rY   Ú
last_foundr   r   r   Úfind_allà   s   þüzMinidumpBufferedReader.find_allc                 C   s$   | j  |¡}t|ƒdkrdS |d S )zx
		Searches for the pattern in the whole process memory space and returns the first occurrence.
		This is exhaustive!
		r   r!   )r.   ÚsearchÚlen)r   r$   Úpos_sr   r   r   Úfind_globalî   s   z"MinidumpBufferedReader.find_globalc                 C   s   | j  |¡S )z�
		Searches for the pattern in the whole process memory space and returns a list of addresses where the pattern begins.
		This is exhaustive!
		)r.   r\   )r   r$   r   r   r   Úfind_all_globalù   s   z&MinidumpBufferedReader.find_all_globalc                 C   s   |   |¡ |  ¡ S r   )r?   rX   )r   rY   r   r   r   Úget_ptr   s   
zMinidumpBufferedReader.get_ptrc                 C   sN   | j jjtjkr|  |¡ tj|  d¡ddd�}|d | S |  |¡ |  	¡ S )NrA   rO   TrP   )
r.   rB   rC   r   rD   r?   rT   rU   r"   rX   )r   rY   Úptrr   r   r   Úget_ptr_with_offset  s   

z*MinidumpBufferedReader.get_ptr_with_offsetFc                 C   s   | j j||||| jd�}|S )N)Ú
find_firstÚreverse_orderr   )r.   Úsearch_moduler/   )r   Úmodule_namer$   rd   re   r=   r   r   r   Úfind_in_module  s   z%MinidumpBufferedReader.find_in_moduler,   )r   r   )r!   )FF)r   r   r   r   r6   r9   r'   r?   rG   rH   rK   r"   rV   rX   r#   r[   r_   r`   ra   rc   rh   r   r   r   r   r-   K   s&    



			r-   c                   @   sZ   e Zd Zdd„ Zdd„ Zdd„ Zddd	„Zd
d„ Zdd„ Zddd„Z	ddd„Z
dd„ ZdS )ÚMinidumpFileReaderc                 C   sÈ   |j j | _ g | _|jd ur|jj | _|j| _|jr"|jj| _d| _n|jj| _d| _|j| _|j| _d| _d| _	|jj
tjtjfv rJd| _d| _d S | jj
tjtjfv r\d| _d| _d S td| jj
 ƒ‚)NTFrA   z<Lr@   z<Qz<Unknown processor architecture %s! Please fix and submit PR!)ÚmodulesÚunloaded_modulesrB   Úmemory_segments_64r0   Úis_fulldumpÚfilenamer   Úsizeof_longÚunpack_longrC   r   rD   ÚAARCH64Ú
sizeof_ptrÚ
unpack_ptrÚINTELÚARMr3   )r   Úminidumpfiler   r   r   r     s.   






ÿ
zMinidumpFileReader.__init__c                 C   r7   r   )r   r8   r   r   r   Úget_handler4  r:   zMinidumpFileReader.get_handlerc                 C   r7   r   )r0   r8   r   r   r   Ú
get_memory7  r:   zMinidumpFileReader.get_memoryr   c                 C   s   t | |d�S )N)r/   )r-   )r   r/   r   r   r   Úget_buffered_reader:  s   z&MinidumpFileReader.get_buffered_readerc                 C   s6   | j D ]}t |j¡ ¡  | ¡ ¡dkr|  S qd S ©Nr!   )rj   ÚntpathÚbasenameÚnameÚlowerr#   ©r   rg   Úmodr   r   r   Úget_module_by_name=  s
   
ÿz%MinidumpFileReader.get_module_by_namec                 C   s.   | j D ]}t |j¡ |¡dkr|  S qd S rz   )rk   r{   r|   r}   r#   r   r   r   r   Úget_unloaded_by_nameC  s
   
ÿz'MinidumpFileReader.get_unloaded_by_nameFc           	      C   s–   |   |¡}|d u r|  |¡}|d u rtd| ƒ‚g }| jD ]+}|j|j  kr,|jk rHn q||j|| j||d�7 }t	|ƒdkrH|du rH|  S q|S )NzCould not find module! %s©rd   r   r   T)
r�   r‚   r3   r0   Úbaseaddressr   Ú
endaddressr\   r   r]   )	r   rg   r$   rd   re   r   r€   ÚneedlesÚmsr   r   r   rf   I  s   


€z MinidumpFileReader.search_modulec                 C   s,   g }| j D ]}||j|| j||d�7 }q|S )Nrƒ   )r0   r\   r   )r   r$   rd   r   r=   r‡   r   r   r   r\   Z  s   
zMinidumpFileReader.searchc                 C   s:   | j D ]}| |¡r| ||| j¡  S qtdt|ƒ ƒ‚)NzAddress not in memory range! %s)r0   r   r"   r   r3   Úhex)r   Ú	virt_addrrL   Úsegmentr   r   r   r"   a  s
   

ÿzMinidumpFileReader.readNr,   )FFr   )Fr   )r   r   r   r   rw   rx   ry   r�   r‚   rf   r\   r"   r   r   r   r   ri     s     


ri   )	Ústructr{   Úcommon_structsÚstreams.SystemInfoStreamr   r   r   r-   ri   r   r   r   r   Ú<module>   s   3 I