
    9cL                         d dl Z d dlZd dlZd dlZd dlZd dlZ G d de      Z G d de      Z	 G d de      Z
 G d d	ej                  j                  j                        Zy)
    Nc                       e Zd Zd Zd Zy)	PFSCommonc                     t         j                  j                  j                  |      }t	        j
                  d|z  |      d   S )zReturns a 2 byte integer.z%sHr   binwalkcorecompat	str2bytesstructunpackselfdata
endiannesss      7/usr/lib/python3/dist-packages/binwalk/plugins/unpfs.py_make_shortzPFSCommon._make_short
   7    ||"",,T2}}UZ/6q99    c                     t         j                  j                  j                  |      }t	        j
                  d|z  |      d   S )zReturns a 4 byte integer.z%sIr   r   r   s      r   	_make_intzPFSCommon._make_int   r   r   N)__name__
__module____qualname__r   r    r   r   r   r      s    :
:r   r   c                   B    e Zd ZdZdZddZddZd Zd Zd Z	d Z
d	 Zy
)PFSz"Class for accessing PFS meta-data.   c                 \   || _         t        j                  j                  j	                  |d      | _        | j
                  j                  | j                        }| j
                  j                         | _	        | j                  |dd  |      | _        | j                         dz   | _        y )Nrb   )r   r   r   common	BlockFilemetareadHEADER_SIZEtellfile_list_startr   	num_files_get_fname_len	node_size)r   fnamer   headers       r   __init__zPFS.__init__   s    $LL''11%>	 0 01#yy~~/))&+zB,,.3r   c                     | j                   j                  |      }|j                  d      }t        ||d       D ]  \  }}|dk7  s||z   c S  |S )z8Returns the number of bytes designated for the filename. N)r$   peekfind	enumerate)r   bufflenbuffstrlenibs         r   r*   zPFS._get_fname_len!   sV    yy~~g&4d67m, 	 DAqDyax	  r   c                 x    | j                   j                  | j                        }t        || j                        S )z;Reads a chunk of meta data from file and returns a PFSNode.)r$   r%   r+   PFSNoder   )r   r   s     r   	_get_nodezPFS._get_node*   s)    yy~~dnn-tT__--r   c                 N    | j                   | j                  | j                  z  z   S )z@Returns integer indicating the end of the file system meta data.)r&   r+   r)   r   s    r   get_end_of_meta_datazPFS.get_end_of_meta_data/   s     $..4>>"AAAr   c              #      K   | j                   j                  | j                         t        d| j                        D ]  }| j                           yw)z*Returns file meta-data entries one by one.r   N)r$   seekr(   ranger)   r;   )r   r7   s     r   entrieszPFS.entries3   sC     		t++,q$..) 	#A..""	#s   AAc                     | S Nr   r=   s    r   	__enter__zPFS.__enter__9   s    r   c                 8    | j                   j                          y rD   )r$   close)r   typevalue	tracebacks       r   __exit__zPFS.__exit__<   s    		r   N)<)   )r   r   r   __doc__r&   r.   r*   r;   r>   rB   rE   rK   r   r   r   r   r      s.    ,K4.
B#r   r   c                       e Zd ZdZd Zd Zy)r:   zFA node in the PFS Filesystem containing meta-data about a single file.c                     |d d |dd  c| _         }| j                          | j                  |d d |      | _        | j                  |dd |      | _        | j                  |dd  |      | _        y )Ni      )r,   _decode_fnamer   inode_nofoffsetfsizer   s      r   r.   zPFSNode.__init__B   sp    :tCDz
DtBQx<~~d1Qi<^^DHj9
r   c                     | j                   d| j                   j                  d       | _         | j                   j                  dd      | _         y)z4Extracts the actual string from the available bytes.Nr0   \/)r,   r2   replacer=   s    r   rS   zPFSNode._decode_fnameI   s:    ZZ 6!67
ZZ''c2
r   N)r   r   r   rN   r.   rS   r   r   r   r:   r:   ?   s    P:3r   r:   c                   (    e Zd ZdZdgZd Zd Zd Zy)PFSExtractorz:
    Extractor for known PFS/0.9 File System Formats.
    	Signaturec                     | j                   j                  j                  r3| j                   j                  j                  dd| j                         y y )Nz^pfs filesystempfs)regex	extensioncmd)module	extractorenabledadd_ruler=   s    r   initzPFSExtractor.initT   sD    ;;  ((KK!!**1B5:/3~~ + ? )r   c                     	 t        j                  t         j                  j                  |             y # t        $ r)}|j
                  t
        j                  k7  r|Y d }~y d }~ww xY wrD   )osmakedirspathdirnameOSErrorerrnoEEXIST)r   r,   es      r   _create_dir_from_fnamez#PFSExtractor._create_dir_from_fnameZ   sJ    	KK./ 	ww%,,& '	s   25 	A'A""A'c                 d   t         j                  j                  |      }t        j                  j
                  j                  t         j                  j                  t         j                  j                  |      d            }	 t        |      5 }t        j                  j
                  j                  |d      }|j                  |j                                |j                         D ]  }t         j                  j                  t         j                  j                  ||j                              }|j                  |      s-t        j                  j
                  j!                  d|z         | j#                  |       t        j                  j
                  j                  |d      }|j%                  |j'                  |j(                               |j+                           |j+                          d d d        y# 1 sw Y   yxY w# t,        $ r}|d }~wt.        $ r
}Y d }~yd }~ww xY w)Nzpfs-rootr   zYUnpfs extractor detected directory traversal attempt for file: '%s'. Refusing to extract.wbFT)ri   rk   abspathr   r   r"   unique_file_namejoinrl   r   r#   r@   r>   rB   r,   
startswithwarningrq   writer%   rV   rG   KeyboardInterrupt	Exception)	r   r,   out_dirfsr   entryoutfile_pathoutfilerp   s	            r   rd   zPFSExtractor.extractora   s   &,,%%66rww||BGGOOTYDZ\f7gh	U r||**44UDA		"1134ZZ\ (E#%77??277<<3U#VL'227;++33  5P  S_  5_  `33LA"),,"5"5"?"?d"Sdii&<=( 

& '&  ! 	G 		s=   H E)H9H HH H 	H/HH/*H/N)r   r   r   rN   MODULESrg   rq   rd   r   r   r   r\   r\   N   s     mG?r   r\   )ri   rn   r   binwalk.core.commonr   binwalk.core.compatbinwalk.core.pluginobjectr   r   r:   r   pluginPluginr\   r   r   r   <module>r      sX    	     
: 
:)) )V3i 3+7<<&&-- +r   