+
     hh  c                   s    ^ RI H t  ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
HtHtHtHtHtHt Rt ! R	 R]4      tR
tRt ! R R4      t ! R R]
4      t ! R R]4      tR# )    )array)bisect_right)isfile)Struct)warn)ZstdDecompressReaderZstdFile_MODE_CLOSED
_MODE_READ_MODE_WRITEPathLikeioSeekableFormatErrorSeekableZstdFilec                   s6   a a ] tR t^t oRtV 3R ltRtVtV ;t# )r   z.An error related to Zstandard Seekable Format.c                s4   < \         SV `  R V,           4       R# )z!Zstandard Seekable Format error: N)super__init__)selfmsg	__class__s   &&?/usr/lib64/python3.14/site-packages/pyzstd/seekable_zstdfile.pyr   ZSeekableFormatError.__init__   s    <sBC     )	__name__
__module____qualname____firstlineno____doc__r   __static_attributes____classdictcell____classcell__r   __classdict__   @@r   r   r      s     4D Dr   a  Zstandard Seekable Format (Ver 0.1.0, Apr 2017)
Square brackets are used to indicate optional fields.
All numeric fields are little-endian unless specified otherwise.
A. Seek table is a skippable frame at the end of file:
     Magic_Number  Frame_Size  [Seek_Table_Entries]  Seek_Table_Footer
     4 bytes       4 bytes     8-12 bytes each       9 bytes
     Magic_Number must be 0x184D2A5E.
B. Seek_Table_Entries:
     Compressed_Size  Decompressed_Size  [Checksum]
     4 bytes          4 bytes            4 bytes
     Checksum is optional.
C. Seek_Table_Footer:
     Number_Of_Frames  Seek_Table_Descriptor  Seekable_Magic_Number
     4 bytes           1 byte                 4 bytes
     Seekable_Magic_Number must be 0x8F92EAB1.
D. Seek_Table_Descriptor:
     Bit_number  Field_name
     7           Checksum_Flag
     6-2         Reserved_Bits  (should ensure they are set to 0)
     1-0         Unused_Bits    (should not interpret these bits)z0.1.0c                   s   a  ] tR t^)t o ]! R4      t]! R4      t]! R4      tR tR t	R t
R tR tR	 tR
 tR tR tR t]R 4       t]R 4       tR tR tRtV tR# )	SeekTablez<IIz<IIIz<IBIc                s2    Wn         V P                  4        R # N)
_read_mode_clear_seek_table)r   	read_mode   &&r   r   ZSeekTable.__init__/   s    # r   c                s    R V n         ^ V n        ^ V n        ^ V n        ^ V n        ^ V n        V P                  '       d'   \        R^ .4      V n        \        R^ .4      V n	        R# \        R4      V n
        R# )FZqZIN)_has_checksum_seek_frame_size
_file_size_frames_count_full_c_size_full_d_sizer$   r   _cumulated_c_size_cumulated_d_size_framesr      &r   r%   ZSeekTable._clear_seek_table3   si    " !???
 &+3_D"%*3_D" !:DLr   c                s   V^ 8X  d   V^ 8X  d   R# \         hV ;P                  ^,          un        V ;P                  V,          un        V ;P                  V,          un        V P                  '       dM   V P
                  P                  V P                  4       V P                  P                  V P                  4       R# V P                  P                  V4       V P                  P                  V4       R# r    N)	
ValueErrorr+   r,   r-   r$   r.   Zappendr/   r0   )r   compressed_sizedecompressed_sizes   &&&r   append_entryZSeekTable.append_entryN   s    a A% ! a_,..???""))$*;*;<""))$*;*;<LL0LL 12r   c                s   VP                  ^ ^4      pV^ 8X  d   R# V^8  d   Rp\        V4      hVP                  R^4       VP                  ^	4      pV P                  P	                  V4      w  rgpVR8w  d   Rp\        V4      hV^,          V n        V^|,          '       d   R\        ,          p\        V4      hV P
                  '       d   ^M^p	^Wi,          ,           p
W:8  d   \        R4      hVP                  V
) ^4       VP                  V
4      pV P                  P                  V^ 4      w  rVR8w  d   Rp\        V4      hW^,
          8w  d   R	p\        V4      hV'       d   VP                  ^ 4       ^p\        V4       F  pV P
                  '       d*   V P                  P                  W4      w  pppV^,          pM'V P                  P                  W4      w  ppV^,          pV^ 8X  d   V^ 8w  d   R
V,          p\        V4      hV P                  VV4       V P                  W:,
          8  g   K  RV,          p\        V4      h	  V P                  W:,
          8w  d   \        R4      hWn        W0n        R# )r    NzPFile size is less than the minimal size (17 bytes) of Zstandard Seekable Format.   j% a  The last 4 bytes of the file is not Zstandard Seekable Format Magic Number (b"\xb1\xea\x92\x8f)". SeekableZstdFile class only supports Zstandard Seekable Format file or 0-size file. To read a zstd file that is not in Zstandard Seekable Format, use ZstdFile class.z^In Zstandard Seekable Format version %s, the Reserved_Bits in Seek_Table_Descriptor must be 0.z=File size is less than expected size of the seek table frame.^*Mz)Seek table frame's Magic_Number is wrong.z'Seek table frame's Frame_Size is wrong.zpWrong seek table. The index %d frame (0-based) is 0 size, but decompressed size is non-zero, this is impossible.zjWrong seek table. Since index %d frame (0-based), the cumulated compressed size is greater than file size.z&The cumulated compressed size is wrongi)seekr   Zread	_s_footerZunpackr(   __format_version__
_s_2uint32Zunpack_fromrange
_s_3uint32r7   r,   r)   r*   )r   fp	seek_to_0Zfsizer   ZfooterZframes_numberZ
descriptorZmagic_numberZ
entry_sizeZskippable_frame_sizeZskippable_frameZskippable_magic_numberZcontent_sizeoffsetZidxr5   r6   Zchecksums   &&&                r   load_seek_tableZSeekTable.load_seek_tabled   s   1A:RZ>C%c** 	A26..2G2G2O/<:%KC
 &c** 
" 	
""G()C &c**  ---R1
!M$>>'% (G I I 	%%q)''"67++OQ? 	- "Z/=C%c**!33;C%c** GGAJ 'C!!!OO//H =!2H" OO//H 3!2! !#(9Q(>-034 *#.. o/@A   5#??$'*+ *#..3 (8  <<%&NOO !5r   c                sv    V^ 8  d   ^ p\        V P                  V4      pW P                  ^,           8w  d   V# R# r3   )r   r/   r+   )r   posis   && r   index_by_dposZSeekTable.index_by_dpos   s;    7C//5""Q&&H r   c                sj    V P                   V^,
          ,          V P                  V^,
          ,          3# )i   )r.   r/   )r   rE   r'   r   get_frame_sizesZSeekTable.get_frame_sizes   s0    &&qs+&&qs+- 	-r   c                    V P                   # r#   )r,   r1   r2   r   get_full_c_sizeZSeekTable.get_full_c_size          r   c                rH   r#   )r-   r1   r2   r   get_full_d_sizeZSeekTable.get_full_d_size   rJ   r   c                s   V P                   V8:  d   R # V P                  p\        V P                   V4      w  r4V P                  4        ^ p\	        V4       Fw  pY6V8  d   ^M^ ,           ^,          p^ p^ p	\	        WUV,           ^4       F(  p
WV
,          ,          pWV
^,           ,          ,          p	K*  	  V P                  W4       WW,          pKy  	  R # r#   )r+   r0   Zdivmodr%   r>   r7   )r   Z
max_framesZarraZbrD   rE   ZlengthZc_sizeZd_sizeZjs   &&         r   _merge_framesZSeekTable._merge_frames   s    + lld((*5  z"AA11-2F FF3F
A.a& ac(" / f-MC #r   c           	     s   V P                   R 8  d4   \        RV P                   ,          \        ^4       V P                  R 4       ^ p^^V P                   ,          ,           p\	        V4      pV P
                  P                  WBRV^,
          4       V^,          p\        ^ \        V P                  4      ^4       FR  pV P
                  P                  WBV P                  V,          V P                  V^,           ,          4       V^,          pKT  	  V P                  P                  WBV P                   ^ R4       VP                  V4       R# )l    zSeekableZstdFile's seek table has %d entries, which exceeds the maximal value allowed by Zstandard Seekable Format (0xFFFFFFFF). The entries will be merged into 0xFFFFFFFF entries, this may reduce seeking performance.r9   r8   N)r+   r   RuntimeWarningrM   Z	bytearrayr=   Z	pack_intor>   Zlenr0   r;   write)r   r@   rB   sizeZbarE   s   &&    r   write_seek_tableZSeekTable.write_seek_table   s   
* 9 =A<N<N	O
  $ z* A****t_ 	!!"j$q&A!q#dll+Q/AOO%%b&*ll1o&*ll1Q3&79 aKF	 0 	  !%!3!3Q
	D 	r   c                rH   r#   )r)   r1   r2   r   seek_frame_sizeZSeekTable.seek_frame_size  s    $$$r   c                rH   r#   )r*   r1   r2   r   	file_sizeZSeekTable.file_size  s    r   c                rH   r#   )r+   r1   r2   r   __len__ZSeekTable.__len__   s    !!!r   c                sH    V P                   V P                  V P                  3# r#   )r+   r,   r-   r1   r2   r   get_infoZSeekTable.get_info#  s'    ""!!!!# 	#r   )
r.   r/   r*   r0   r+   r,   r-   r(   r$   r)   N)r   r   r   r   r   r=   r?   r;   r   r%   r7   rC   rF   rG   rI   rK   rM   rQ   propertyrR   rS   rT   rU   r   r   )r    s   @r   r"   r"   )   s     JJI!&63,\ ~
-!!2!F % %  "# #r   r"   c                   sH   a a ] tR tRt oV 3R ltR tRR ltR tRtVt	V ;t
# )SeekableDecompressReaderi(  c                s  < \        VR 4      '       d   \        VR4      '       g   \        R4      hVP                  4       '       g   \        R4      hVP                  4       '       g   \        R4      h\	        RR7      V n        V P
                  P                  VRR7       \        SV `!  WW44       V P
                  P                  4       V P                  n        R# )	readableseekablez`In SeekableZstdFile's reading mode, the file object should have .readable()/.seekable() methods.zGIn SeekableZstdFile's reading mode, the file object should be readable.zIn SeekableZstdFile's reading mode, the file object should be seekable. If the file object is not seekable, it can be read sequentially using ZstdFile class.Tr&   rA   N)hasattr	TypeErrorrX   rY   r"   _seek_tablerC   r   r   rK   _decomprP   )r   r@   	zstd_dictZoption	read_sizer   s   &&&&&r   r   Z!SeekableDecompressReader.__init__)  s    r:&&gb*.E.E9; ; {{}} " " {{}};= = %t4((t(< 	: ,,<<>r   c                s    R # )Tr   r1   r2   r   rY   Z!SeekableDecompressReader.seekableF  s    r   c                sl   V^ 8X  d   MdV^8X  d   V P                   P                  V,           pM?V^8X  d   V P                   P                  V,           pM\        RP	                  V4      4      hV P
                  P                  V4      pVf|   RV P                   n        V P                   P                  V P                   n        V P                  P                  V P
                  P                  4       V P                   P                  # V P
                  P                  V P                   P                  4      pV P
                  P                  V4      w  rVW48X  d;   WP                   P                  8  d!   V P                  P                  4       V8  d   MVRV P                   n        W`P                   n        V P                   P                  4        V P                  P                  V4       WP                   P                  ,          pV P                   P                  V4       V P                   P                  # )r    zInvalid value for whence: {}TF)r_   rD   rP   r4   Zformatr^   rF   Zeof_fpr:   rS   rG   tellZreset_sessionZforward)r   rB   ZwhenceZ	new_frameZ	old_frameZc_posZd_poss   &&&    r   r:   ZSeekableDecompressReader.seekK  s   Q;q[\\%%.Fq[\\&&/F;BB6JKK $$226:	#DLL#||00DLLHHMM$**445<<### $$224<<3C3CD	''77	B !\\%%%88==?e#  %DLL$LLLL&&(HHMM%  	,,"""V$||r   c                s6    V P                   P                  4       # r#   )r^   rU   r1   r2   r   get_seek_table_infoZ,SeekableDecompressReader.get_seek_table_infoz  s    ((**r   )r^   )r    )r   r   r   r   r   rY   r:   rd   r   r   r   r   r!   r   rW   rW   (  s     ?:
- ^+ +r   rW   c                   s   a a ] tR tRt oRtRtRt]tRRRRRRRRR	R
R/V 3R lllt	R t
V 3R ltR t]P                  3R lt]R 4       t]R 4       tRtVtV ;t# )r   i  zThis class can only create/write/read Zstandard Seekable Format file,
or read 0-size file.
It provides relatively fast seeking ability in read mode.
level_or_optionNr`   ra   i  
write_sizei max_frame_content_sizec          	     s  < RV n         RV n        RV n        \        V n        VR9   d   VR8w  d   \        R4      hEM$VR9   Ed   ^ Tu;8  d   V P                  8:  g!   M \        RV P                  V3,          4      hWpn        V P                  4        \        RR7      V n
        VR9   d   \        V\        \        \        34      '       g   \        R4      h\!        V4      '       dy   \"        P$                  ! VR4      ;_uu_ 4       p\'        VR4      '       d   VP)                  4       '       g   \        R	4      hV P                  P+                  VRR
7       RRR4       \,        S	V `]  WVVVVR7       VR9   d   V P                  P)                  4       '       dO   V P                  P1                  V P                  P3                  4       4       V P                  P5                  4        M\V P                  P7                  V P                  P8                  ^ 4       \;        RV P                  P8                  ,          \<        ^4       V P                  \>        8H  V n         R#   + '       g   i     EL; i)a-  Open a Zstandard Seekable Format file in binary mode. In read mode,
the file can be 0-size file.

filename can be either an actual file name (given as a str, bytes, or
PathLike object), in which case the named file is opened, or it can be
an existing file object to read from or write to.

mode can be "r" for reading (default), "w" for (over)writing, "x" for
creating exclusively, or "a" for appending. These can equivalently be
given as "rb", "wb", "xb" and "ab" respectively.

In append mode ("a" or "ab"), filename argument can't be a file object,
please use file path.

Parameters
level_or_option: When it's an int object, it represents compression
    level. When it's a dict object, it contains advanced compression
    parameters. Note, in read mode (decompression), it can only be a
    dict object, that represents decompression option. It doesn't
    support int type compression level in this case.
zstd_dict: A ZstdDict object, pre-trained dictionary for compression /
    decompression.
read_size: In reading mode, this is bytes number that read from the
    underlying file object each time, default value is zstd's
    recommended value. If use with Network File System, increasing
    it may get better performance.
write_size: In writing modes, this is output buffer's size, default
    value is zstd's recommended value. If use with Network File
    System, increasing it may get better performance.
max_frame_content_size: In write/append modes (compression), when
    the uncompressed data size reaches max_frame_content_size, a frame
    is generated automatically. If the size is small, it will increase
    seeking speed, but reduce compression ratio. If the size is large,
    it will reduce seeking speed, but increase compression ratio. You
    can also manually generate a frame using f.flush(f.FLUSH_FRAME).
FNrbzKmax_frame_content_size argument is only valid in write modes (compression).zPmax_frame_content_size argument should be 0 < value <= %d, provided value is %d.rZ   zIn append mode ('a', 'ab'), SeekableZstdFile.__init__() method can't accept file object as filename argument. Please use file path (str/bytes/PathLike).rY   z^In SeekableZstdFile's append mode ('a', 'ab'), the opened 'rb' file object should be seekable.r[   )re   r`   ra   rf   a0  SeekableZstdFile is opened in append mode ('a', 'ab'), but the underlying file object is not seekable. Therefore the seek table (a zstd skippable frame) at the end of the file can't be overwritten. Each time open such file in append mode, it will waste some storage space. %d bytes were wasted this time.)rrh      @)ZwZwbrL   abZxZxb)rL   rk   ) _write_in_closerb   _closefpr   _moder4   FRAME_MAX_D_SIZE_max_frame_content_size_reset_frame_sizesr"   r^   
isinstancestrbytesr   r]   r   r   openr\   rY   rC   r   r   r:   rI   Ztruncater7   rR   r   rN   r
   )
r   filenamemodere   r`   ra   rf   rg   Zfr   s
   &&&$$$$$ r   r   ZSeekableZstdFile.__init__  s6   R  %!
;%7  #H J J 8 66.G$2G2GG >**,BCDE E ,B(##%(59D {"!(S%,BCC#JL L (##400A&q*55QZZ\\"+">#@ @ ((88e8L 1 	)8#,#,$.	 	 	0 ;xx  ""d..>>@A!!#   --((88!=  ? ((889 $Q( !%

k 9I 100s   (I 1(I  I1	c                sD    ^ V n         ^ V n        V P                  V n        R# r3   )_current_c_size_current_d_sizerp   _left_d_sizer1   r2   r   rq   Z#SeekableZstdFile._reset_frame_sizes  s        88r   c                s4  <  V P                   '       dI    V P                  V P                  4       V P                  P	                  V P
                  4       RV n         RV n        \        SV `  4        R#   RT n         i ; i  RT n        \        ST `  4        i ; i)zFlush and close the file.

May be called more than once without error. Once the file is
closed, any other operation on it will raise a ValueError.
FN)rl   flushFLUSH_FRAMEr^   rQ   rb   r   close)r   r   s   &r   r}   ZSeekableZstdFile.close  s    	###1JJt//0$$55dhh? ,1D(  $DGMO ,1D(  $DGMOs#   B  A A4 B  4	A==B   Bc           
     s\   V P                   \        8w  d   V P                  \        4       \        V4      ;_uu_ 4       q"P	                  R4      ;_uu_ 4       pVP
                  p^ pV^ 8  d   \        W@P                  4      pV P                  P                  W5WV,            4      w  rxV ;P                  V,          un
        WV,          pWF,          pV ;P                  V,          un        V ;P                  V,          un        V ;P                  V,          un        V P                  ^ 8X  g   V P                  V P                  8  g   K  V P                  V P                  4       EK  VuuRRR4       uuRRR4       #   + '       g   i     M; iRRR4       R#   + '       g   i     R# ; i)zWrite a bytes-like object to the file.

Returns the number of uncompressed bytes written, which is
always the length of data in bytes. Note that due to buffering,
the file on disk may not reflect the data written until .flush()
or .close() is called.
ZBN)rn   r
   _check_modeZ
memoryviewZcastnbytesZminrz   _writerrO   Z_posrx   ry   FRAME_MAX_C_SIZEr{   r|   )	r   ZdataZviewZ	byte_viewr   rD   rf   _output_sizes	   &&       r   rO   ZSeekableZstdFile.write  s1    ::$[) yy~~%%FC1* ):):;
 "&!3!3(1cn(E"G		Z'	
 !$ $$3$$$
2$!!Z/! $$)''4+@+@@JJt//0E (6~~~s*   FC-E=		E=	(
F=FFF+	c                s   V P                   \        8w  d(   V P                   \        8X  d   R# V P                  4        V P                  P                  V4      w  r#V ;P                  V,          un        WP                  8X  dV   V P                  ^ 8w  dC   V P                  P                  V P                  V P                  4       V P                  4        R# R# R# )ao  Flush remaining data to the underlying stream.

The mode argument can be ZstdFile.FLUSH_BLOCK, ZstdFile.FLUSH_FRAME.
Abuse of this method will reduce compression ratio, use it only when
necessary.

If the program is interrupted afterwards, all data can be recovered.
To ensure saving to disk, also need to use os.fsync(fd).

This method does nothing in reading mode.
N)rn   r
   r	   r~   r   r{   rx   r|   r^   r7   ry   rq   )r   rw   r   r   s   &&  r   r{   ZSeekableZstdFile.flushI  s     ::$ zzZ' ++D1 	+ ###1$))$*>*>*.*>*>@##%	 % $r   c                s    V P                   \        8X  d   V P                  P                  4       # V P                   \        8X  d%   V P
                  P                  P                  4       # R# )aJ  A tuple: (frames_number, compressed_size, decompressed_size)
1, Frames_number and compressed_size don't count the seek table
   frame (a zstd skippable frame at the end of the file).
2, In write modes, the part of data that has not been flushed to
   frames is not counted.
3, If the SeekableZstdFile object is closed, it's None.
N)rn   r
   r^   rU   r	   Z_bufferZrawrd   r1   r2   r   seek_table_infoZ SeekableZstdFile.seek_table_infot  sO     ::$##,,..ZZ:%<<##7799 r   c                s|   \        V \        \        \        34      '       d   \        P
                  ! V R4      pRpMp\        V R4      '       dT   V P                  4       '       d>   \        V R4      '       d,   V P                  4       '       d   T pRpVP                  4       pM\        R4      h\        RR7      p VP                  VRR7       RpV'       d   VP                  4        V# VP                  X4        V#   \         d    Rp L=i ; i  T'       d   TP                  4        i TP                  X4       i ; i)	a  Check if a file is Zstandard Seekable Format file or 0-size file.

It parses the seek table at the end of the file, returns True if no
format error.

filename can be either a file path (str/bytes/PathLike), or can be an
existing file object in reading mode.
rh   TrX   rY   Fzhfilename argument should be a str/bytes/PathLike object, or a file object that is readable and seekable.rZ   r[   )rr   rs   rt   r   r   ru   r\   rX   rY   rc   r]   r"   rC   r   r}   r:   )rv   r@   Zis_file_pathZorig_posZtableZrets   &     r   is_seekable_format_fileZ(SeekableZstdFile.is_seekable_format_file  s    heX 6774(BLXz**x/@/@/B/BXz**x/@/@/B/BB LwwyHCE E
 E*	"!!"!6 C
 
 !
 # 	C	
 
!s*   9C< D <D	D DD ,D;)	rm   rx   ry   rb   rz   rp   rn   r^   rl   l        rj   )ri   )r   r   r   r   r   r   ro   rW   Z_READER_CLASSr   rq   r}   rO   r   ZFLUSH_BLOCKr{   rV   r   Zstaticmethodr   r   r   r   r   r!   r   r   r     s      (',Mo;!%o;15o;!o;.4o; )7o; o;b9
(/b "-- )&V    & & &r   N)r   r   )r   Zbisectr   Zos.pathr   Zstructr   Zwarningsr   Zpyzstd.zstdfiler   r   r   r	   r
   r   r   Z__all__Z	Exceptionr   r   r<   r"   rW   r   r   r   r   <module>r      ss        ) ) ) 6D) D
E*  }# }#~S+3 S+rkx kr   