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 if (action != LZMA_SYNC_FLUSH)
196 return LZMA_PROG_ERROR;
198 // Check that application doesn't change avail_in once
199 // LZMA_SYNC_FLUSH has been used.
200 if (strm->internal->avail_in != strm->avail_in)
201 return LZMA_DATA_ERROR;
205 case ISEQ_FULL_FLUSH:
206 if (action != LZMA_FULL_FLUSH)
207 return LZMA_PROG_ERROR;
209 // Check that application doesn't change avail_in once
210 // LZMA_FULL_FLUSH has been used.
211 if (strm->internal->avail_in != strm->avail_in)
212 return LZMA_DATA_ERROR;
217 if (action != LZMA_FINISH)
218 return LZMA_PROG_ERROR;
220 if (strm->internal->avail_in != strm->avail_in)
221 return LZMA_DATA_ERROR;
226 return LZMA_STREAM_END;
230 return LZMA_PROG_ERROR;
235 lzma_ret ret = strm->internal->next.code(
236 strm->internal->next.coder, strm->allocator,
237 strm->next_in, &in_pos, strm->avail_in,
238 strm->next_out, &out_pos, strm->avail_out, action);
240 strm->next_in += in_pos;
241 strm->avail_in -= in_pos;
242 strm->total_in += in_pos;
244 strm->next_out += out_pos;
245 strm->avail_out -= out_pos;
246 strm->total_out += out_pos;
248 strm->internal->avail_in = strm->avail_in;
252 // Don't return LZMA_BUF_ERROR when it happens the first time.
253 // This is to avoid returning LZMA_BUF_ERROR when avail_out
254 // was zero but still there was no more data left to written
256 if (out_pos == 0 && in_pos == 0) {
257 if (strm->internal->allow_buf_error)
258 ret = LZMA_BUF_ERROR;
260 strm->internal->allow_buf_error = true;
262 strm->internal->allow_buf_error = false;
266 case LZMA_STREAM_END:
267 if (strm->internal->sequence == ISEQ_SYNC_FLUSH
268 || strm->internal->sequence == ISEQ_FULL_FLUSH)
269 strm->internal->sequence = ISEQ_RUN;
271 strm->internal->sequence = ISEQ_END;
274 case LZMA_UNSUPPORTED_CHECK:
275 strm->internal->allow_buf_error = false;
279 // All the other errors are fatal; coding cannot be continued.
280 strm->internal->sequence = ISEQ_ERROR;
289 lzma_end(lzma_stream *strm)
291 if (strm != NULL && strm->internal != NULL) {
292 lzma_next_end(&strm->internal->next, strm->allocator);
293 lzma_free(strm->internal, strm->allocator);
294 strm->internal = NULL;
301 extern LZMA_API lzma_check
302 lzma_get_check(const lzma_stream *strm)
304 // Return LZMA_CHECK_NONE if we cannot know the check type.
305 // It's a bug in the application if this happens.
306 if (strm->internal->next.get_check == NULL)
307 return LZMA_CHECK_NONE;
309 return strm->internal->next.get_check(strm->internal->next.coder);
313 extern LZMA_API uint64_t
314 lzma_memusage(const lzma_stream *strm)
317 uint64_t old_memlimit;
319 if (strm == NULL || strm->internal == NULL
320 || strm->internal->next.memconfig == NULL
321 || strm->internal->next.memconfig(
322 strm->internal->next.coder,
323 &memusage, &old_memlimit, 0) != LZMA_OK)
330 extern LZMA_API uint64_t
331 lzma_memlimit_get(const lzma_stream *strm)
333 uint64_t old_memlimit;
336 if (strm == NULL || strm->internal == NULL
337 || strm->internal->next.memconfig == NULL
338 || strm->internal->next.memconfig(
339 strm->internal->next.coder,
340 &memusage, &old_memlimit, 0) != LZMA_OK)
347 extern LZMA_API lzma_ret
348 lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
350 // Dummy variables to simplify memconfig functions
351 uint64_t old_memlimit;
354 if (strm == NULL || strm->internal == NULL
355 || strm->internal->next.memconfig == NULL)
356 return LZMA_PROG_ERROR;
358 if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
359 return LZMA_MEMLIMIT_ERROR;
361 return strm->internal->next.memconfig(strm->internal->next.coder,
362 &memusage, &old_memlimit, new_memlimit);