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))
39 lzma_alloc(size_t size, 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);
57 lzma_free(void *ptr, lzma_allocator *allocator)
59 if (allocator != NULL && allocator->free != NULL)
60 allocator->free(allocator->opaque, ptr);
73 lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
74 size_t in_size, uint8_t *restrict out,
75 size_t *restrict out_pos, size_t out_size)
77 const size_t in_avail = in_size - *in_pos;
78 const size_t out_avail = out_size - *out_pos;
79 const size_t copy_size = MIN(in_avail, out_avail);
81 memcpy(out + *out_pos, in + *in_pos, copy_size);
84 *out_pos += copy_size;
91 lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
92 const lzma_filter_info *filters)
94 lzma_next_coder_init(filters[0].init, next, allocator);
95 next->id = filters[0].id;
96 return filters[0].init == NULL
97 ? LZMA_OK : filters[0].init(next, allocator, filters);
102 lzma_next_filter_update(lzma_next_coder *next, lzma_allocator *allocator,
103 const lzma_filter *reversed_filters)
105 // Check that the application isn't trying to change the Filter ID.
106 // End of filters is indicated with LZMA_VLI_UNKNOWN in both
107 // reversed_filters[0].id and next->id.
108 if (reversed_filters[0].id != next->id)
109 return LZMA_PROG_ERROR;
111 if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
114 assert(next->update != NULL);
115 return next->update(next->coder, allocator, NULL, reversed_filters);
120 lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
122 if (next->init != (uintptr_t)(NULL)) {
123 // To avoid tiny end functions that simply call
124 // lzma_free(coder, allocator), we allow leaving next->end
125 // NULL and call lzma_free() here.
126 if (next->end != NULL)
127 next->end(next->coder, allocator);
129 lzma_free(next->coder, allocator);
131 // Reset the variables so the we don't accidentally think
132 // that it is an already initialized coder.
133 *next = LZMA_NEXT_CODER_INIT;
140 //////////////////////////////////////
141 // External to internal API wrapper //
142 //////////////////////////////////////
145 lzma_strm_init(lzma_stream *strm)
148 return LZMA_PROG_ERROR;
150 if (strm->internal == NULL) {
151 strm->internal = lzma_alloc(sizeof(lzma_internal),
153 if (strm->internal == NULL)
154 return LZMA_MEM_ERROR;
156 strm->internal->next = LZMA_NEXT_CODER_INIT;
159 strm->internal->supported_actions[LZMA_RUN] = false;
160 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = false;
161 strm->internal->supported_actions[LZMA_FULL_FLUSH] = false;
162 strm->internal->supported_actions[LZMA_FINISH] = false;
163 strm->internal->sequence = ISEQ_RUN;
164 strm->internal->allow_buf_error = false;
173 extern LZMA_API(lzma_ret)
174 lzma_code(lzma_stream *strm, lzma_action action)
177 if ((strm->next_in == NULL && strm->avail_in != 0)
178 || (strm->next_out == NULL && strm->avail_out != 0)
179 || strm->internal == NULL
180 || strm->internal->next.code == NULL
181 || (unsigned int)(action) > LZMA_FINISH
182 || !strm->internal->supported_actions[action])
183 return LZMA_PROG_ERROR;
185 switch (strm->internal->sequence) {
191 case LZMA_SYNC_FLUSH:
192 strm->internal->sequence = ISEQ_SYNC_FLUSH;
195 case LZMA_FULL_FLUSH:
196 strm->internal->sequence = ISEQ_FULL_FLUSH;
200 strm->internal->sequence = ISEQ_FINISH;
206 case ISEQ_SYNC_FLUSH:
207 // The same action must be used until we return
208 // LZMA_STREAM_END, and the amount of input must not change.
209 if (action != LZMA_SYNC_FLUSH
210 || strm->internal->avail_in != strm->avail_in)
211 return LZMA_PROG_ERROR;
215 case ISEQ_FULL_FLUSH:
216 if (action != LZMA_FULL_FLUSH
217 || strm->internal->avail_in != strm->avail_in)
218 return LZMA_PROG_ERROR;
223 if (action != LZMA_FINISH
224 || strm->internal->avail_in != strm->avail_in)
225 return LZMA_PROG_ERROR;
230 return LZMA_STREAM_END;
234 return LZMA_PROG_ERROR;
239 lzma_ret ret = strm->internal->next.code(
240 strm->internal->next.coder, strm->allocator,
241 strm->next_in, &in_pos, strm->avail_in,
242 strm->next_out, &out_pos, strm->avail_out, action);
244 strm->next_in += in_pos;
245 strm->avail_in -= in_pos;
246 strm->total_in += in_pos;
248 strm->next_out += out_pos;
249 strm->avail_out -= out_pos;
250 strm->total_out += out_pos;
252 strm->internal->avail_in = strm->avail_in;
256 // Don't return LZMA_BUF_ERROR when it happens the first time.
257 // This is to avoid returning LZMA_BUF_ERROR when avail_out
258 // was zero but still there was no more data left to written
260 if (out_pos == 0 && in_pos == 0) {
261 if (strm->internal->allow_buf_error)
262 ret = LZMA_BUF_ERROR;
264 strm->internal->allow_buf_error = true;
266 strm->internal->allow_buf_error = false;
270 case LZMA_STREAM_END:
271 if (strm->internal->sequence == ISEQ_SYNC_FLUSH
272 || strm->internal->sequence == ISEQ_FULL_FLUSH)
273 strm->internal->sequence = ISEQ_RUN;
275 strm->internal->sequence = ISEQ_END;
280 case LZMA_UNSUPPORTED_CHECK:
282 case LZMA_MEMLIMIT_ERROR:
283 // Something else than LZMA_OK, but not a fatal error,
284 // that is, coding may be continued (except if ISEQ_END).
285 strm->internal->allow_buf_error = false;
289 // All the other errors are fatal; coding cannot be continued.
290 assert(ret != LZMA_BUF_ERROR);
291 strm->internal->sequence = ISEQ_ERROR;
299 extern LZMA_API(void)
300 lzma_end(lzma_stream *strm)
302 if (strm != NULL && strm->internal != NULL) {
303 lzma_next_end(&strm->internal->next, strm->allocator);
304 lzma_free(strm->internal, strm->allocator);
305 strm->internal = NULL;
312 extern LZMA_API(lzma_check)
313 lzma_get_check(const lzma_stream *strm)
315 // Return LZMA_CHECK_NONE if we cannot know the check type.
316 // It's a bug in the application if this happens.
317 if (strm->internal->next.get_check == NULL)
318 return LZMA_CHECK_NONE;
320 return strm->internal->next.get_check(strm->internal->next.coder);
324 extern LZMA_API(uint64_t)
325 lzma_memusage(const lzma_stream *strm)
328 uint64_t old_memlimit;
330 if (strm == NULL || strm->internal == NULL
331 || strm->internal->next.memconfig == NULL
332 || strm->internal->next.memconfig(
333 strm->internal->next.coder,
334 &memusage, &old_memlimit, 0) != LZMA_OK)
341 extern LZMA_API(uint64_t)
342 lzma_memlimit_get(const lzma_stream *strm)
344 uint64_t old_memlimit;
347 if (strm == NULL || strm->internal == NULL
348 || strm->internal->next.memconfig == NULL
349 || strm->internal->next.memconfig(
350 strm->internal->next.coder,
351 &memusage, &old_memlimit, 0) != LZMA_OK)
358 extern LZMA_API(lzma_ret)
359 lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
361 // Dummy variables to simplify memconfig functions
362 uint64_t old_memlimit;
365 if (strm == NULL || strm->internal == NULL
366 || strm->internal->next.memconfig == NULL)
367 return LZMA_PROG_ERROR;
369 if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
370 return LZMA_MEMLIMIT_ERROR;
372 return strm->internal->next.memconfig(strm->internal->next.coder,
373 &memusage, &old_memlimit, new_memlimit);