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