1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Common functions needed in many places in liblzma
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
20 extern LZMA_API(uint32_t)
21 lzma_version_number(void)
27 extern LZMA_API(const char *)
28 lzma_version_string(void)
30 return LZMA_VERSION_STRING;
34 ///////////////////////
35 // Memory allocation //
36 ///////////////////////
38 extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
39 lzma_alloc(size_t size, const lzma_allocator *allocator)
41 // Some malloc() variants return NULL if called with size == 0.
47 if (allocator != NULL && allocator->alloc != NULL)
48 ptr = allocator->alloc(allocator->opaque, 1, size);
56 extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
57 lzma_alloc_zero(size_t size, const lzma_allocator *allocator)
59 // Some calloc() variants return NULL if called with size == 0.
65 if (allocator != NULL && allocator->alloc != NULL) {
66 ptr = allocator->alloc(allocator->opaque, 1, size);
70 ptr = calloc(1, size);
78 lzma_free(void *ptr, const lzma_allocator *allocator)
80 if (allocator != NULL && allocator->free != NULL)
81 allocator->free(allocator->opaque, ptr);
94 lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
95 size_t in_size, uint8_t *restrict out,
96 size_t *restrict out_pos, size_t out_size)
98 const size_t in_avail = in_size - *in_pos;
99 const size_t out_avail = out_size - *out_pos;
100 const size_t copy_size = my_min(in_avail, out_avail);
102 // Call memcpy() only if there is something to copy. If there is
103 // nothing to copy, in or out might be NULL and then the memcpy()
104 // call would trigger undefined behavior.
106 memcpy(out + *out_pos, in + *in_pos, copy_size);
108 *in_pos += copy_size;
109 *out_pos += copy_size;
116 lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator,
117 const lzma_filter_info *filters)
119 lzma_next_coder_init(filters[0].init, next, allocator);
120 next->id = filters[0].id;
121 return filters[0].init == NULL
122 ? LZMA_OK : filters[0].init(next, allocator, filters);
127 lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator,
128 const lzma_filter *reversed_filters)
130 // Check that the application isn't trying to change the Filter ID.
131 // End of filters is indicated with LZMA_VLI_UNKNOWN in both
132 // reversed_filters[0].id and next->id.
133 if (reversed_filters[0].id != next->id)
134 return LZMA_PROG_ERROR;
136 if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
139 assert(next->update != NULL);
140 return next->update(next->coder, allocator, NULL, reversed_filters);
145 lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
147 if (next->init != (uintptr_t)(NULL)) {
148 // To avoid tiny end functions that simply call
149 // lzma_free(coder, allocator), we allow leaving next->end
150 // NULL and call lzma_free() here.
151 if (next->end != NULL)
152 next->end(next->coder, allocator);
154 lzma_free(next->coder, allocator);
156 // Reset the variables so the we don't accidentally think
157 // that it is an already initialized coder.
158 *next = LZMA_NEXT_CODER_INIT;
165 //////////////////////////////////////
166 // External to internal API wrapper //
167 //////////////////////////////////////
170 lzma_strm_init(lzma_stream *strm)
173 return LZMA_PROG_ERROR;
175 if (strm->internal == NULL) {
176 strm->internal = lzma_alloc(sizeof(lzma_internal),
178 if (strm->internal == NULL)
179 return LZMA_MEM_ERROR;
181 strm->internal->next = LZMA_NEXT_CODER_INIT;
184 memzero(strm->internal->supported_actions,
185 sizeof(strm->internal->supported_actions));
186 strm->internal->sequence = ISEQ_RUN;
187 strm->internal->allow_buf_error = false;
196 extern LZMA_API(lzma_ret)
197 lzma_code(lzma_stream *strm, lzma_action action)
200 if ((strm->next_in == NULL && strm->avail_in != 0)
201 || (strm->next_out == NULL && strm->avail_out != 0)
202 || strm->internal == NULL
203 || strm->internal->next.code == NULL
204 || (unsigned int)(action) > LZMA_ACTION_MAX
205 || !strm->internal->supported_actions[action])
206 return LZMA_PROG_ERROR;
208 // Check if unsupported members have been set to non-zero or non-NULL,
209 // which would indicate that some new feature is wanted.
210 if (strm->reserved_ptr1 != NULL
211 || strm->reserved_ptr2 != NULL
212 || strm->reserved_ptr3 != NULL
213 || strm->reserved_ptr4 != NULL
214 || strm->reserved_int1 != 0
215 || strm->reserved_int2 != 0
216 || strm->reserved_int3 != 0
217 || strm->reserved_int4 != 0
218 || strm->reserved_enum1 != LZMA_RESERVED_ENUM
219 || strm->reserved_enum2 != LZMA_RESERVED_ENUM)
220 return LZMA_OPTIONS_ERROR;
222 switch (strm->internal->sequence) {
228 case LZMA_SYNC_FLUSH:
229 strm->internal->sequence = ISEQ_SYNC_FLUSH;
232 case LZMA_FULL_FLUSH:
233 strm->internal->sequence = ISEQ_FULL_FLUSH;
237 strm->internal->sequence = ISEQ_FINISH;
240 case LZMA_FULL_BARRIER:
241 strm->internal->sequence = ISEQ_FULL_BARRIER;
247 case ISEQ_SYNC_FLUSH:
248 // The same action must be used until we return
249 // LZMA_STREAM_END, and the amount of input must not change.
250 if (action != LZMA_SYNC_FLUSH
251 || strm->internal->avail_in != strm->avail_in)
252 return LZMA_PROG_ERROR;
256 case ISEQ_FULL_FLUSH:
257 if (action != LZMA_FULL_FLUSH
258 || strm->internal->avail_in != strm->avail_in)
259 return LZMA_PROG_ERROR;
264 if (action != LZMA_FINISH
265 || strm->internal->avail_in != strm->avail_in)
266 return LZMA_PROG_ERROR;
270 case ISEQ_FULL_BARRIER:
271 if (action != LZMA_FULL_BARRIER
272 || strm->internal->avail_in != strm->avail_in)
273 return LZMA_PROG_ERROR;
278 return LZMA_STREAM_END;
282 return LZMA_PROG_ERROR;
287 lzma_ret ret = strm->internal->next.code(
288 strm->internal->next.coder, strm->allocator,
289 strm->next_in, &in_pos, strm->avail_in,
290 strm->next_out, &out_pos, strm->avail_out, action);
292 strm->next_in += in_pos;
293 strm->avail_in -= in_pos;
294 strm->total_in += in_pos;
296 strm->next_out += out_pos;
297 strm->avail_out -= out_pos;
298 strm->total_out += out_pos;
300 strm->internal->avail_in = strm->avail_in;
302 // Cast is needed to silence a warning about LZMA_TIMED_OUT, which
303 // isn't part of lzma_ret enumeration.
304 switch ((unsigned int)(ret)) {
306 // Don't return LZMA_BUF_ERROR when it happens the first time.
307 // This is to avoid returning LZMA_BUF_ERROR when avail_out
308 // was zero but still there was no more data left to written
310 if (out_pos == 0 && in_pos == 0) {
311 if (strm->internal->allow_buf_error)
312 ret = LZMA_BUF_ERROR;
314 strm->internal->allow_buf_error = true;
316 strm->internal->allow_buf_error = false;
321 strm->internal->allow_buf_error = false;
325 case LZMA_STREAM_END:
326 if (strm->internal->sequence == ISEQ_SYNC_FLUSH
327 || strm->internal->sequence == ISEQ_FULL_FLUSH
328 || strm->internal->sequence
329 == ISEQ_FULL_BARRIER)
330 strm->internal->sequence = ISEQ_RUN;
332 strm->internal->sequence = ISEQ_END;
337 case LZMA_UNSUPPORTED_CHECK:
339 case LZMA_MEMLIMIT_ERROR:
340 // Something else than LZMA_OK, but not a fatal error,
341 // that is, coding may be continued (except if ISEQ_END).
342 strm->internal->allow_buf_error = false;
346 // All the other errors are fatal; coding cannot be continued.
347 assert(ret != LZMA_BUF_ERROR);
348 strm->internal->sequence = ISEQ_ERROR;
356 extern LZMA_API(void)
357 lzma_end(lzma_stream *strm)
359 if (strm != NULL && strm->internal != NULL) {
360 lzma_next_end(&strm->internal->next, strm->allocator);
361 lzma_free(strm->internal, strm->allocator);
362 strm->internal = NULL;
369 extern LZMA_API(void)
370 lzma_get_progress(lzma_stream *strm,
371 uint64_t *progress_in, uint64_t *progress_out)
373 if (strm->internal->next.get_progress != NULL) {
374 strm->internal->next.get_progress(strm->internal->next.coder,
375 progress_in, progress_out);
377 *progress_in = strm->total_in;
378 *progress_out = strm->total_out;
385 extern LZMA_API(lzma_check)
386 lzma_get_check(const lzma_stream *strm)
388 // Return LZMA_CHECK_NONE if we cannot know the check type.
389 // It's a bug in the application if this happens.
390 if (strm->internal->next.get_check == NULL)
391 return LZMA_CHECK_NONE;
393 return strm->internal->next.get_check(strm->internal->next.coder);
397 extern LZMA_API(uint64_t)
398 lzma_memusage(const lzma_stream *strm)
401 uint64_t old_memlimit;
403 if (strm == NULL || strm->internal == NULL
404 || strm->internal->next.memconfig == NULL
405 || strm->internal->next.memconfig(
406 strm->internal->next.coder,
407 &memusage, &old_memlimit, 0) != LZMA_OK)
414 extern LZMA_API(uint64_t)
415 lzma_memlimit_get(const lzma_stream *strm)
417 uint64_t old_memlimit;
420 if (strm == NULL || strm->internal == NULL
421 || strm->internal->next.memconfig == NULL
422 || strm->internal->next.memconfig(
423 strm->internal->next.coder,
424 &memusage, &old_memlimit, 0) != LZMA_OK)
431 extern LZMA_API(lzma_ret)
432 lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
434 // Dummy variables to simplify memconfig functions
435 uint64_t old_memlimit;
438 if (strm == NULL || strm->internal == NULL
439 || strm->internal->next.memconfig == NULL)
440 return LZMA_PROG_ERROR;
442 // Zero is a special value that cannot be used as an actual limit.
443 // If 0 was specified, use 1 instead.
444 if (new_memlimit == 0)
447 return strm->internal->next.memconfig(strm->internal->next.coder,
448 &memusage, &old_memlimit, new_memlimit);