blob: e935502a5d9b81e5c73110269e997e07a2a0cde0 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001///////////////////////////////////////////////////////////////////////////////
2//
3/// \file common.c
4/// \brief Common functions needed in many places in liblzma
5//
6// Author: Lasse Collin
7//
8// This file has been put into the public domain.
9// You can do whatever you want with this file.
10//
11///////////////////////////////////////////////////////////////////////////////
12
13#include "common.h"
14
15
16/////////////
17// Version //
18/////////////
19
20extern LZMA_API(uint32_t)
21lzma_version_number(void)
22{
23 return LZMA_VERSION;
24}
25
26
27extern LZMA_API(const char *)
28lzma_version_string(void)
29{
30 return LZMA_VERSION_STRING;
31}
32
33
34///////////////////////
35// Memory allocation //
36///////////////////////
37
38extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
39lzma_alloc(size_t size, const lzma_allocator *allocator)
40{
41 // Some malloc() variants return NULL if called with size == 0.
42 if (size == 0)
43 size = 1;
44
45 void *ptr;
46
47 if (allocator != NULL && allocator->alloc != NULL)
48 ptr = allocator->alloc(allocator->opaque, 1, size);
49 else
50 ptr = malloc(size);
51
52 return ptr;
53}
54
55
56extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
57lzma_alloc_zero(size_t size, const lzma_allocator *allocator)
58{
59 // Some calloc() variants return NULL if called with size == 0.
60 if (size == 0)
61 size = 1;
62
63 void *ptr;
64
65 if (allocator != NULL && allocator->alloc != NULL) {
66 ptr = allocator->alloc(allocator->opaque, 1, size);
67 if (ptr != NULL)
68 memzero(ptr, size);
69 } else {
70 ptr = calloc(1, size);
71 }
72
73 return ptr;
74}
75
76
77extern void
78lzma_free(void *ptr, const lzma_allocator *allocator)
79{
80 if (allocator != NULL && allocator->free != NULL)
81 allocator->free(allocator->opaque, ptr);
82 else
83 free(ptr);
84
85 return;
86}
87
88
89//////////
90// Misc //
91//////////
92
93extern size_t
94lzma_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)
97{
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);
101
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.
105 if (copy_size > 0)
106 memcpy(out + *out_pos, in + *in_pos, copy_size);
107
108 *in_pos += copy_size;
109 *out_pos += copy_size;
110
111 return copy_size;
112}
113
114
115extern lzma_ret
116lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator,
117 const lzma_filter_info *filters)
118{
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);
123}
124
125
126extern lzma_ret
127lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator,
128 const lzma_filter *reversed_filters)
129{
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;
135
136 if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
137 return LZMA_OK;
138
139 assert(next->update != NULL);
140 return next->update(next->coder, allocator, NULL, reversed_filters);
141}
142
143
144extern void
145lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
146{
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);
153 else
154 lzma_free(next->coder, allocator);
155
156 // Reset the variables so the we don't accidentally think
157 // that it is an already initialized coder.
158 lzma_next_coder next_coder = LZMA_NEXT_CODER_INIT;
159 *next = next_coder;
160 }
161
162 return;
163}
164
165
166//////////////////////////////////////
167// External to internal API wrapper //
168//////////////////////////////////////
169
170extern lzma_ret
171lzma_strm_init(lzma_stream *strm)
172{
173 if (strm == NULL)
174 return LZMA_PROG_ERROR;
175
176 if (strm->internal == NULL) {
177 strm->internal = (lzma_internal *) lzma_alloc(sizeof(lzma_internal),
178 strm->allocator);
179 if (strm->internal == NULL)
180 return LZMA_MEM_ERROR;
181
182 lzma_next_coder next_coder = LZMA_NEXT_CODER_INIT;
183 strm->internal->next = next_coder;
184 }
185
186 memzero(strm->internal->supported_actions,
187 sizeof(strm->internal->supported_actions));
188 strm->internal->sequence = ISEQ_RUN;
189 strm->internal->allow_buf_error = false;
190
191 strm->total_in = 0;
192 strm->total_out = 0;
193
194 return LZMA_OK;
195}
196
197
198extern LZMA_API(lzma_ret)
199lzma_code(lzma_stream *strm, lzma_action action)
200{
201 // Sanity checks
202 if ((strm->next_in == NULL && strm->avail_in != 0)
203 || (strm->next_out == NULL && strm->avail_out != 0)
204 || strm->internal == NULL
205 || strm->internal->next.code == NULL
206 || (unsigned int)(action) > LZMA_ACTION_MAX
207 || !strm->internal->supported_actions[action])
208 return LZMA_PROG_ERROR;
209
210 // Check if unsupported members have been set to non-zero or non-NULL,
211 // which would indicate that some new feature is wanted.
212 if (strm->reserved_ptr1 != NULL
213 || strm->reserved_ptr2 != NULL
214 || strm->reserved_ptr3 != NULL
215 || strm->reserved_ptr4 != NULL
216 || strm->reserved_int1 != 0
217 || strm->reserved_int2 != 0
218 || strm->reserved_int3 != 0
219 || strm->reserved_int4 != 0
220 || strm->reserved_enum1 != LZMA_RESERVED_ENUM
221 || strm->reserved_enum2 != LZMA_RESERVED_ENUM)
222 return LZMA_OPTIONS_ERROR;
223
224 switch (strm->internal->sequence) {
225 case ISEQ_RUN:
226 switch (action) {
227 case LZMA_RUN:
228 break;
229
230 case LZMA_SYNC_FLUSH:
231 strm->internal->sequence = ISEQ_SYNC_FLUSH;
232 break;
233
234 case LZMA_FULL_FLUSH:
235 strm->internal->sequence = ISEQ_FULL_FLUSH;
236 break;
237
238 case LZMA_FINISH:
239 strm->internal->sequence = ISEQ_FINISH;
240 break;
241
242 case LZMA_FULL_BARRIER:
243 strm->internal->sequence = ISEQ_FULL_BARRIER;
244 break;
245 }
246
247 break;
248
249 case ISEQ_SYNC_FLUSH:
250 // The same action must be used until we return
251 // LZMA_STREAM_END, and the amount of input must not change.
252 if (action != LZMA_SYNC_FLUSH
253 || strm->internal->avail_in != strm->avail_in)
254 return LZMA_PROG_ERROR;
255
256 break;
257
258 case ISEQ_FULL_FLUSH:
259 if (action != LZMA_FULL_FLUSH
260 || strm->internal->avail_in != strm->avail_in)
261 return LZMA_PROG_ERROR;
262
263 break;
264
265 case ISEQ_FINISH:
266 if (action != LZMA_FINISH
267 || strm->internal->avail_in != strm->avail_in)
268 return LZMA_PROG_ERROR;
269
270 break;
271
272 case ISEQ_FULL_BARRIER:
273 if (action != LZMA_FULL_BARRIER
274 || strm->internal->avail_in != strm->avail_in)
275 return LZMA_PROG_ERROR;
276
277 break;
278
279 case ISEQ_END:
280 return LZMA_STREAM_END;
281
282 case ISEQ_ERROR:
283 default:
284 return LZMA_PROG_ERROR;
285 }
286
287 size_t in_pos = 0;
288 size_t out_pos = 0;
289 lzma_ret ret = strm->internal->next.code(
290 strm->internal->next.coder, strm->allocator,
291 strm->next_in, &in_pos, strm->avail_in,
292 strm->next_out, &out_pos, strm->avail_out, action);
293
294 strm->next_in += in_pos;
295 strm->avail_in -= in_pos;
296 strm->total_in += in_pos;
297
298 strm->next_out += out_pos;
299 strm->avail_out -= out_pos;
300 strm->total_out += out_pos;
301
302 strm->internal->avail_in = strm->avail_in;
303
304 // Cast is needed to silence a warning about LZMA_TIMED_OUT, which
305 // isn't part of lzma_ret enumeration.
306 switch ((unsigned int)(ret)) {
307 case LZMA_OK:
308 // Don't return LZMA_BUF_ERROR when it happens the first time.
309 // This is to avoid returning LZMA_BUF_ERROR when avail_out
310 // was zero but still there was no more data left to written
311 // to next_out.
312 if (out_pos == 0 && in_pos == 0) {
313 if (strm->internal->allow_buf_error)
314 ret = LZMA_BUF_ERROR;
315 else
316 strm->internal->allow_buf_error = true;
317 } else {
318 strm->internal->allow_buf_error = false;
319 }
320 break;
321
322 case LZMA_TIMED_OUT:
323 strm->internal->allow_buf_error = false;
324 ret = LZMA_OK;
325 break;
326
327 case LZMA_STREAM_END:
328 if (strm->internal->sequence == ISEQ_SYNC_FLUSH
329 || strm->internal->sequence == ISEQ_FULL_FLUSH
330 || strm->internal->sequence
331 == ISEQ_FULL_BARRIER)
332 strm->internal->sequence = ISEQ_RUN;
333 else
334 strm->internal->sequence = ISEQ_END;
335
336 // Fall through
337
338 case LZMA_NO_CHECK:
339 case LZMA_UNSUPPORTED_CHECK:
340 case LZMA_GET_CHECK:
341 case LZMA_MEMLIMIT_ERROR:
342 // Something else than LZMA_OK, but not a fatal error,
343 // that is, coding may be continued (except if ISEQ_END).
344 strm->internal->allow_buf_error = false;
345 break;
346
347 default:
348 // All the other errors are fatal; coding cannot be continued.
349 assert(ret != LZMA_BUF_ERROR);
350 strm->internal->sequence = ISEQ_ERROR;
351 break;
352 }
353
354 return ret;
355}
356
357
358extern LZMA_API(void)
359lzma_end(lzma_stream *strm)
360{
361 if (strm != NULL && strm->internal != NULL) {
362 lzma_next_end(&strm->internal->next, strm->allocator);
363 lzma_free(strm->internal, strm->allocator);
364 strm->internal = NULL;
365 }
366
367 return;
368}
369
370
371#ifdef HAVE_SYMBOL_VERSIONS_LINUX
372// This is for compatibility with binaries linked against liblzma that
373// has been patched with xz-5.2.2-compat-libs.patch from RHEL/CentOS 7.
374LZMA_SYMVER_API("lzma_get_progress@XZ_5.2.2",
375 void, lzma_get_progress_522)(lzma_stream *strm,
376 uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow
377 __attribute__((__alias__("lzma_get_progress_52")));
378
379LZMA_SYMVER_API("lzma_get_progress@@XZ_5.2",
380 void, lzma_get_progress_52)(lzma_stream *strm,
381 uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow;
382
383#define lzma_get_progress lzma_get_progress_52
384#endif
385extern LZMA_API(void)
386lzma_get_progress(lzma_stream *strm,
387 uint64_t *progress_in, uint64_t *progress_out)
388{
389 if (strm->internal->next.get_progress != NULL) {
390 strm->internal->next.get_progress(strm->internal->next.coder,
391 progress_in, progress_out);
392 } else {
393 *progress_in = strm->total_in;
394 *progress_out = strm->total_out;
395 }
396
397 return;
398}
399
400
401extern LZMA_API(lzma_check)
402lzma_get_check(const lzma_stream *strm)
403{
404 // Return LZMA_CHECK_NONE if we cannot know the check type.
405 // It's a bug in the application if this happens.
406 if (strm->internal->next.get_check == NULL)
407 return LZMA_CHECK_NONE;
408
409 return strm->internal->next.get_check(strm->internal->next.coder);
410}
411
412
413extern LZMA_API(uint64_t)
414lzma_memusage(const lzma_stream *strm)
415{
416 uint64_t memusage;
417 uint64_t old_memlimit;
418
419 if (strm == NULL || strm->internal == NULL
420 || strm->internal->next.memconfig == NULL
421 || strm->internal->next.memconfig(
422 strm->internal->next.coder,
423 &memusage, &old_memlimit, 0) != LZMA_OK)
424 return 0;
425
426 return memusage;
427}
428
429
430extern LZMA_API(uint64_t)
431lzma_memlimit_get(const lzma_stream *strm)
432{
433 uint64_t old_memlimit;
434 uint64_t memusage;
435
436 if (strm == NULL || strm->internal == NULL
437 || strm->internal->next.memconfig == NULL
438 || strm->internal->next.memconfig(
439 strm->internal->next.coder,
440 &memusage, &old_memlimit, 0) != LZMA_OK)
441 return 0;
442
443 return old_memlimit;
444}
445
446
447extern LZMA_API(lzma_ret)
448lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
449{
450 // Dummy variables to simplify memconfig functions
451 uint64_t old_memlimit;
452 uint64_t memusage;
453
454 if (strm == NULL || strm->internal == NULL
455 || strm->internal->next.memconfig == NULL)
456 return LZMA_PROG_ERROR;
457
458 // Zero is a special value that cannot be used as an actual limit.
459 // If 0 was specified, use 1 instead.
460 if (new_memlimit == 0)
461 new_memlimit = 1;
462
463 return strm->internal->next.memconfig(strm->internal->next.coder,
464 &memusage, &old_memlimit, new_memlimit);
465}