2 * Copyright (c) 2018, Cygwin
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
14 #include "compress_zstd.h"
23 * Predicate: the stream is open for read.
25 compress_zstd::compress_zstd (io_stream
* parent
)
32 if (!parent
|| parent
->error())
42 compress_zstd::create (void)
44 state
= (struct private_data
*)calloc(sizeof(*state
), 1);
52 state
->stream
= ZSTD_createDStream();
53 if (state
->stream
== NULL
)
59 ZSTD_initDStream(state
->stream
);
60 state
->out_block
.size
= state
->out_block
.pos
= state
->out_pos
= state
->out_bsz
= ZSTD_DStreamOutSize();
61 state
->out_block
.dst
= (unsigned char *)malloc(state
->out_bsz
);
62 if (state
->out_block
.dst
== NULL
)
64 free(state
->out_block
.dst
);
69 state
->in_block
.size
= state
->in_block
.pos
= state
->in_bsz
= ZSTD_DStreamInSize();
70 state
->in_block
.src
= (unsigned char *)malloc(state
->in_bsz
);
72 if (state
->in_block
.src
== NULL
)
74 free(state
->out_block
.dst
);
75 free((void*)state
->in_block
.src
);
83 compress_zstd::read (void *buffer
, size_t len
)
85 /* there is no recovery from a busted stream */
95 size_t lenRemaining
= len
;
96 size_t lenBuffered
= 0;
99 if (state
->in_block
.size
> 0 && state
->in_block
.pos
>= state
->in_block
.size
)
101 /* no compressed data ready; read some more input */
102 state
->in_block
.size
= state
->in_bsz
;
103 ssize_t got
= this->original
->read((void *)state
->in_block
.src
, state
->in_bsz
);
106 state
->in_block
.size
= got
;
107 state
->in_block
.pos
= 0;
117 if (state
->out_pos
< state
->out_block
.pos
)
119 /* output buffer has unused data */
120 ssize_t tmplen
= std::min (state
->out_block
.pos
- state
->out_pos
, lenRemaining
);
121 memcpy (&((char *)buffer
)[lenBuffered
], &((char *)state
->out_block
.dst
)[state
->out_pos
], tmplen
);
122 state
->out_pos
+= tmplen
;
123 lenBuffered
+= tmplen
;
124 lenRemaining
-= tmplen
;
136 /* output buffer is empty; decompress more data */
137 state
->out_block
.size
= state
->out_bsz
;
138 state
->out_pos
= state
->out_block
.pos
= 0;
139 size_t ret
= ZSTD_decompressStream (state
->stream
, &state
->out_block
, &state
->in_block
);
140 if (ZSTD_isError(ret
))
142 // TODO return/print error
145 state
->eof
= (ret
== 0);
148 while (lenRemaining
!= 0);
150 return (len
- lenRemaining
);
154 compress_zstd::write (const void *buffer
, size_t len
)
156 throw new std::logic_error("compress_zstd::write is not implemented");
160 compress_zstd::peek (void *buffer
, size_t len
)
162 /* can only peek 512 bytes */
166 // we only peek at the beginning of a file, so no buffer tearing can happen
167 // do a real read first…
168 ssize_t got
= read (buffer
, len
);
171 // …then rewind read position for the next read()
172 state
->out_pos
-= got
;
179 compress_zstd::tell ()
181 throw new std::logic_error("compress_zstd::tell is not implemented");
185 compress_zstd::seek (off_t where
, io_stream_seek_t whence
)
187 if ((whence
== IO_SEEK_SET
) && (where
== 0))
189 off_t result
= original
->seek(where
, whence
);
195 throw new std::logic_error("compress_zstd::seek is not implemented");
199 compress_zstd::error ()
205 compress_zstd::set_mtime (time_t mtime
)
208 return original
->set_mtime (mtime
);
213 compress_zstd::get_mtime ()
216 return original
->get_mtime ();
221 compress_zstd::get_mode ()
224 return original
->get_mode ();
229 compress_zstd::release_original ()
231 owns_original
= false;
235 compress_zstd::destroy ()
239 ZSTD_freeDStream(state
->stream
);
241 if (state
->out_block
.dst
)
243 free (state
->out_block
.dst
);
244 state
->out_block
.dst
= NULL
;
247 if (state
->in_block
.src
)
249 free ((void*)state
->in_block
.src
);
250 state
->in_block
.src
= NULL
;
258 compress_zstd::~compress_zstd ()
262 if (original
&& owns_original
)
267 compress_zstd::is_zstd (void * buffer
, size_t len
)
269 return (ZSTD_getFrameContentSize(buffer
, len
) != ZSTD_CONTENTSIZE_ERROR
);