1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Common functions needed in many places in liblzma
6 // Copyright (C) 2007-2008 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
27 extern LZMA_API(uint32_t)
28 lzma_version_number(void)
34 extern LZMA_API(const char *)
35 lzma_version_string(void)
37 return PACKAGE_VERSION;
41 ///////////////////////
42 // Memory allocation //
43 ///////////////////////
45 extern void * lzma_attribute((malloc))
46 lzma_alloc(size_t size, lzma_allocator *allocator)
48 // Some malloc() variants return NULL if called with size == 0.
54 if (allocator != NULL && allocator->alloc != NULL)
55 ptr = allocator->alloc(allocator->opaque, 1, size);
64 lzma_free(void *ptr, lzma_allocator *allocator)
66 if (allocator != NULL && allocator->free != NULL)
67 allocator->free(allocator->opaque, ptr);
80 lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
81 size_t in_size, uint8_t *restrict out,
82 size_t *restrict out_pos, size_t out_size)
84 const size_t in_avail = in_size - *in_pos;
85 const size_t out_avail = out_size - *out_pos;
86 const size_t copy_size = MIN(in_avail, out_avail);
88 memcpy(out + *out_pos, in + *in_pos, copy_size);
91 *out_pos += copy_size;
98 lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
99 const lzma_filter_info *filters)
101 lzma_next_coder_init(filters[0].init, next, allocator);
103 return filters[0].init == NULL
104 ? LZMA_OK : filters[0].init(next, allocator, filters);
109 lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
111 if (next->init != (uintptr_t)(NULL)) {
112 // To avoid tiny end functions that simply call
113 // lzma_free(coder, allocator), we allow leaving next->end
114 // NULL and call lzma_free() here.
115 if (next->end != NULL)
116 next->end(next->coder, allocator);
118 lzma_free(next->coder, allocator);
120 // Reset the variables so the we don't accidentally think
121 // that it is an already initialized coder.
122 *next = LZMA_NEXT_CODER_INIT;
129 //////////////////////////////////////
130 // External to internal API wrapper //
131 //////////////////////////////////////
134 lzma_strm_init(lzma_stream *strm)
137 return LZMA_PROG_ERROR;
139 if (strm->internal == NULL) {
140 strm->internal = lzma_alloc(sizeof(lzma_internal),
142 if (strm->internal == NULL)
143 return LZMA_MEM_ERROR;
145 strm->internal->next = LZMA_NEXT_CODER_INIT;
148 strm->internal->supported_actions[LZMA_RUN] = false;
149 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = false;
150 strm->internal->supported_actions[LZMA_FULL_FLUSH] = false;
151 strm->internal->supported_actions[LZMA_FINISH] = false;
152 strm->internal->sequence = ISEQ_RUN;
161 extern LZMA_API(lzma_ret)
162 lzma_code(lzma_stream *strm, lzma_action action)
165 if ((strm->next_in == NULL && strm->avail_in != 0)
166 || (strm->next_out == NULL && strm->avail_out != 0)
167 || strm->internal == NULL
168 || strm->internal->next.code == NULL
169 || (unsigned int)(action) > LZMA_FINISH
170 || !strm->internal->supported_actions[action])
171 return LZMA_PROG_ERROR;
173 switch (strm->internal->sequence) {
179 case LZMA_SYNC_FLUSH:
180 strm->internal->sequence = ISEQ_SYNC_FLUSH;
183 case LZMA_FULL_FLUSH:
184 strm->internal->sequence = ISEQ_FULL_FLUSH;
188 strm->internal->sequence = ISEQ_FINISH;
194 case ISEQ_SYNC_FLUSH:
195 // The same action must be used until we return
196 // LZMA_STREAM_END, and the amount of input must not change.
197 if (action != LZMA_SYNC_FLUSH
198 || strm->internal->avail_in != strm->avail_in)
199 return LZMA_PROG_ERROR;
203 case ISEQ_FULL_FLUSH:
204 if (action != LZMA_FULL_FLUSH
205 || strm->internal->avail_in != strm->avail_in)
206 return LZMA_PROG_ERROR;
211 if (action != LZMA_FINISH
212 || strm->internal->avail_in != strm->avail_in)
213 return LZMA_PROG_ERROR;
218 return LZMA_STREAM_END;
222 return LZMA_PROG_ERROR;
227 lzma_ret ret = strm->internal->next.code(
228 strm->internal->next.coder, strm->allocator,
229 strm->next_in, &in_pos, strm->avail_in,
230 strm->next_out, &out_pos, strm->avail_out, action);
232 strm->next_in += in_pos;
233 strm->avail_in -= in_pos;
234 strm->total_in += in_pos;
236 strm->next_out += out_pos;
237 strm->avail_out -= out_pos;
238 strm->total_out += out_pos;
240 strm->internal->avail_in = strm->avail_in;
244 // Don't return LZMA_BUF_ERROR when it happens the first time.
245 // This is to avoid returning LZMA_BUF_ERROR when avail_out
246 // was zero but still there was no more data left to written
248 if (out_pos == 0 && in_pos == 0) {
249 if (strm->internal->allow_buf_error)
250 ret = LZMA_BUF_ERROR;
252 strm->internal->allow_buf_error = true;
254 strm->internal->allow_buf_error = false;
258 case LZMA_STREAM_END:
259 if (strm->internal->sequence == ISEQ_SYNC_FLUSH
260 || strm->internal->sequence == ISEQ_FULL_FLUSH)
261 strm->internal->sequence = ISEQ_RUN;
263 strm->internal->sequence = ISEQ_END;
268 case LZMA_UNSUPPORTED_CHECK:
270 case LZMA_MEMLIMIT_ERROR:
271 // Something else than LZMA_OK, but not a fatal error,
272 // that is, coding may be continued (except if ISEQ_END).
273 strm->internal->allow_buf_error = false;
277 // All the other errors are fatal; coding cannot be continued.
278 assert(ret != LZMA_BUF_ERROR);
279 strm->internal->sequence = ISEQ_ERROR;
287 extern LZMA_API(void)
288 lzma_end(lzma_stream *strm)
290 if (strm != NULL && strm->internal != NULL) {
291 lzma_next_end(&strm->internal->next, strm->allocator);
292 lzma_free(strm->internal, strm->allocator);
293 strm->internal = NULL;
300 extern LZMA_API(lzma_check)
301 lzma_get_check(const lzma_stream *strm)
303 // Return LZMA_CHECK_NONE if we cannot know the check type.
304 // It's a bug in the application if this happens.
305 if (strm->internal->next.get_check == NULL)
306 return LZMA_CHECK_NONE;
308 return strm->internal->next.get_check(strm->internal->next.coder);
312 extern LZMA_API(uint64_t)
313 lzma_memusage(const lzma_stream *strm)
316 uint64_t old_memlimit;
318 if (strm == NULL || strm->internal == NULL
319 || strm->internal->next.memconfig == NULL
320 || strm->internal->next.memconfig(
321 strm->internal->next.coder,
322 &memusage, &old_memlimit, 0) != LZMA_OK)
329 extern LZMA_API(uint64_t)
330 lzma_memlimit_get(const lzma_stream *strm)
332 uint64_t old_memlimit;
335 if (strm == NULL || strm->internal == NULL
336 || strm->internal->next.memconfig == NULL
337 || strm->internal->next.memconfig(
338 strm->internal->next.coder,
339 &memusage, &old_memlimit, 0) != LZMA_OK)
346 extern LZMA_API(lzma_ret)
347 lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
349 // Dummy variables to simplify memconfig functions
350 uint64_t old_memlimit;
353 if (strm == NULL || strm->internal == NULL
354 || strm->internal->next.memconfig == NULL)
355 return LZMA_PROG_ERROR;
357 if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
358 return LZMA_MEMLIMIT_ERROR;
360 return strm->internal->next.memconfig(strm->internal->next.coder,
361 &memusage, &old_memlimit, new_memlimit);