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/aminidumpreader.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 )ÚAMinidumpBufferedMemorySegmenté (  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_segmentr   r   r   r   r      s   
z'AMinidumpBufferedMemorySegment.__init__c                 C   s   | j |  ko| jk S   S r   )r   r   ©r   Úpositionr   r   r   r       s   z&AMinidumpBufferedMemorySegment.inrangec                 C   s   |   |¡sd S | j| S r   )r   r   r   r   r   r   Úremaining_len#   s   

z,AMinidumpBufferedMemorySegment.remaining_lenc                 Ã   s"   �|   |dd¡I d H }| ||¡S )Nr   éÿÿÿÿ)ÚreadÚfind)r   Úfile_handleÚpatternÚstartposr
   r   r   r   r"   (   s   €z#AMinidumpBufferedMemorySegment.findc                 Ã   sh  �|d u r|  | j| ¡I d H  | | j| j|  ¡I d H S | jD ]}| ||¡r8|j||j ||j …   S q!| jd| j	 krp| j}t
d|| jƒ}|  | j¡I d H  | |¡I d H |_| j |¡ |j||j ||j … S t|| | j	ƒ}|| | jkr„| j| }t
||| | j| ƒ}|  |j¡I d H  | |¡I d H |_| 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#AMinidumpBufferedMemorySegment.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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 )-ÚAMinidumpBufferedReaderr   c                 C   s"   || _ g | _|| _d | _d | _d S r   )ÚreaderÚmemory_segmentsÚsegment_chunk_sizeÚcurrent_segmentÚcurrent_position)r   r.   r0   r   r   r   r   I   s
   
z AMinidumpBufferedReader.__init__c                 Ã   sz   �| j D ]}| |¡r|| _|| _ dS q| jj D ]}| |¡r6t|| jd�}| j  |¡ || _|| _ dS qtd| ƒ‚)z

		N)r   z4Memory address 0x%08x is not in process memory space)	r/   r   r1   r2   r.   r   r0   r(   Ú	Exception)r   Úrequested_positionr   Ú
newsegmentr   r   r   Ú_select_segmentQ   s    €

ý
ûz'AMinidumpBufferedReader._select_segmentr   c                 Ã   sh   �|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'   g   s   €zAMinidumpBufferedReader.seekc                 Ã   s   �|   |¡I dH  dS )z@
		Moves the buffer to a virtual address specified by address
		N)r6   )r   Úaddressr   r   r   Úmove~   s   €zAMinidumpBufferedReader.moveNc                 Ã   s\   �|du r| j jjtjkrd}nd}| j| }|dkrdS || | }|  |d¡I dH  dS )zD
		Repositions the current reader to match architecture alignment
		Né   é   r   r   )r.   ÚsysinfoÚProcessorArchitecturer   ÚAMD64r2   r'   )r   Ú	alignmentr7   Úoffset_to_alignedr   r   r   Úalign…   s   €
zAMinidumpBufferedReader.alignc                 C   s   | j S )z)
		Returns the current virtual address
		)r2   )r   r   r   r   Útell•   s   zAMinidumpBufferedReader.tellc                 Ã   sP   �| j | }| j |d ¡stdƒ‚| j | jj| j | jj || jj ¡I dH S )z@
		Returns up to length bytes from the current memory segment
		r   ú#Would read over segment boundaries!N)r2   r1   r   r3   r!   r.   r#   r   )r   Úlengthr9   r   r   r   Úpeek›   s
   €
,zAMinidumpBufferedReader.peekr    c                 Ã   sº   �|dk r	t dƒ‚|dkr1| j | j¡}|sdS | j}| jj| _| j | jj|| jj d¡I dH S | j| }| j 	|d ¡sBt dƒ‚| j}|| _| j | jj|| jj || jj ¡I dH 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   rE   )
r3   r1   r   r2   r   r!   r.   r#   r   r   )r   Úsizer9   Úold_new_posr   r   r   r!   ¤   s    €
"
*zAMinidumpBufferedReader.readc                 Ã   sV   �| j jjtjkr|  d¡I dH }tj|ddd�S |  d¡I dH  }}tj|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<   NÚlittleT©Ú	byteorderÚsignedr=   ©r.   r>   r?   r   r@   r!   ÚintÚ
from_bytes©r   r9   r   r   r   Úread_int»   s   €z AMinidumpBufferedReader.read_intc                 Ã   sR   �| j jjtjkr|  d¡I dH }tj|ddd�S |  d¡I dH }tj|ddd�S )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<   NrJ   FrK   r=   rN   rQ   r   r   r   Ú	read_uintÈ   s   €z!AMinidumpBufferedReader.read_uintc                 Ã   s0   �| j  | jj|¡I dH }|dkrdS || j S )z:
		Searches for a pattern in the current memory segment
		Nr    )r1   r"   r.   r#   r2   )r   r$   Úposr   r   r   r"   Õ   s
   €
zAMinidumpBufferedReader.findc                 Ã   sL   �g }d}	 | j  | jj||d ¡I dH }|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   N)r1   r"   r.   r#   r(   r   )r   r$   rT   Ú
last_foundr   r   r   Úfind_allÞ   s   €þüz AMinidumpBufferedReader.find_allc                 Ã   s,   �| j  |¡I dH }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!
		Nr   r    )r.   ÚsearchÚlen)r   r$   Úpos_sr   r   r   Úfind_globalì   s
   €z#AMinidumpBufferedReader.find_globalc                 Ã   s   �| j  |¡I dH 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!
		N)r.   rW   )r   r$   r   r   r   Úfind_all_global÷   s   €z'AMinidumpBufferedReader.find_all_globalc                 Ã   s    �|   |¡I d H  |  ¡ I d H S r   )r;   rS   )r   rT   r   r   r   Úget_ptrþ   s   €zAMinidumpBufferedReader.get_ptrc                 Ã   sl   �| j jjtjkr'|  |¡I d H  |  d¡I d H }tj|ddd�}|d | S |  |¡I d H  |  	¡ I d H S )Nr=   rJ   TrK   )
r.   r>   r?   r   r@   r;   r!   rO   rP   rS   )r   rT   r9   Úptrr   r   r   Úget_ptr_with_offset  s   €z+AMinidumpBufferedReader.get_ptr_with_offsetFc                 Ã   s$   �| j j||||| jd�I d H }|S )N)Ú
find_firstÚreverse_orderr   )r.   Úsearch_moduler0   )r   Úmodule_namer$   r_   r`   r9   r   r   r   Úfind_in_module  s   €z&AMinidumpBufferedReader.find_in_moduler,   )r   r   )r    )FF)r   r   r   r   r6   r'   r;   rC   rD   rG   r!   rR   rS   r"   rV   rZ   r[   r\   r^   rc   r   r   r   r   r-   H   s$    



		
r-   c                   @   sB   e Zd Zdd„ Zddd„Zdd„ Zdd	d
„Zddd„Zdd„ ZdS )ÚAMinidumpFileReaderc                 C   s®   |j j | _ |j| _|jr|jj| _d| _n|jj| _d| _|j| _|j| _d| _d| _|jj	t
jt
jfv r=d| _d| _d S | jj	t
jt
jfv rOd| _d| _d S td| jj	 ƒ‚)NTFr=   z<Lr<   z<Qz<Unknown processor architecture %s! Please fix and submit PR!)Úmodulesr>   Úmemory_segments_64r/   Úis_fulldumpÚfilenamer#   Úsizeof_longÚunpack_longr?   r   r@   ÚAARCH64Ú
sizeof_ptrÚ
unpack_ptrÚINTELÚARMr3   )r   Úminidumpfiler   r   r   r     s(   




ÿ
zAMinidumpFileReader.__init__r   c                 C   s   t | |d�S )N)r0   )r-   )r   r0   r   r   r   Úget_buffered_reader2  s   z'AMinidumpFileReader.get_buffered_readerc                 C   s6   | j D ]}t |j¡ ¡  | ¡ ¡dkr|  S qd S )Nr    )re   ÚntpathÚbasenameÚnameÚlowerr"   )r   rb   Úmodr   r   r   Úget_module_by_name5  s
   
ÿz&AMinidumpFileReader.get_module_by_nameFc           	      Ã   sŒ   �|   |¡}|d u rtd| ƒ‚g }| jD ].}|j|j  kr$|jk rCn q||j|| j||d�I d H 7 }t|ƒdkrC|du rC|  S q|S )NzCould not find module! %s©r_   r   r   T)	rw   r3   r/   Úbaseaddressr   Ú
endaddressÚasearchr#   rX   )	r   rb   r$   r_   r`   r   rv   ÚneedlesÚmsr   r   r   ra   ;  s   €

€z!AMinidumpFileReader.search_modulec                 Ã   s4   �g }| j D ]}||j|| j||d�I d H 7 }q|S )Nrx   )r/   r{   r#   )r   r$   r_   r   r9   r}   r   r   r   rW   H  s
   €
 zAMinidumpFileReader.searchc                 Ã   sB   �| j D ]}| |¡r| ||| j¡I d H   S qtdt|ƒ ƒ‚)NzAddress not in memory range! %s)r/   r   Úareadr#   r3   Úhex)r   Ú	virt_addrrH   Úsegmentr   r   r   r!   O  s   €

ÿzAMinidumpFileReader.readNr,   )FFr   )Fr   )	r   r   r   r   rq   rw   ra   rW   r!   r   r   r   r   rd     s    


rd   )	Ústructrr   Úcommon_structsÚstreams.SystemInfoStreamr   r   r   r-   rd   r   r   r   r   Ú<module>   s   1 N