/src/xz/src/liblzma/common/common.c
Line | Count | Source (jump to first uncovered line) |
1 | | /////////////////////////////////////////////////////////////////////////////// |
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 | | |
20 | | extern LZMA_API(uint32_t) |
21 | | lzma_version_number(void) |
22 | 0 | { |
23 | 0 | return LZMA_VERSION; |
24 | 0 | } |
25 | | |
26 | | |
27 | | extern LZMA_API(const char *) |
28 | | lzma_version_string(void) |
29 | 0 | { |
30 | 0 | return LZMA_VERSION_STRING; |
31 | 0 | } |
32 | | |
33 | | |
34 | | /////////////////////// |
35 | | // Memory allocation // |
36 | | /////////////////////// |
37 | | |
38 | | lzma_attr_alloc_size(1) |
39 | | extern void * |
40 | | lzma_alloc(size_t size, const lzma_allocator *allocator) |
41 | 1.13M | { |
42 | | // Some malloc() variants return NULL if called with size == 0. |
43 | 1.13M | if (size == 0) |
44 | 0 | size = 1; |
45 | | |
46 | 1.13M | void *ptr; |
47 | | |
48 | 1.13M | if (allocator != NULL && allocator->alloc != NULL) |
49 | 0 | ptr = allocator->alloc(allocator->opaque, 1, size); |
50 | 1.13M | else |
51 | 1.13M | ptr = malloc(size); |
52 | | |
53 | 1.13M | return ptr; |
54 | 1.13M | } |
55 | | |
56 | | |
57 | | lzma_attr_alloc_size(1) |
58 | | extern void * |
59 | | lzma_alloc_zero(size_t size, const lzma_allocator *allocator) |
60 | 0 | { |
61 | | // Some calloc() variants return NULL if called with size == 0. |
62 | 0 | if (size == 0) |
63 | 0 | size = 1; |
64 | |
|
65 | 0 | void *ptr; |
66 | |
|
67 | 0 | if (allocator != NULL && allocator->alloc != NULL) { |
68 | 0 | ptr = allocator->alloc(allocator->opaque, 1, size); |
69 | 0 | if (ptr != NULL) |
70 | 0 | memzero(ptr, size); |
71 | 0 | } else { |
72 | 0 | ptr = calloc(1, size); |
73 | 0 | } |
74 | |
|
75 | 0 | return ptr; |
76 | 0 | } |
77 | | |
78 | | |
79 | | extern void |
80 | | lzma_free(void *ptr, const lzma_allocator *allocator) |
81 | 1.74M | { |
82 | 1.74M | if (allocator != NULL && allocator->free != NULL) |
83 | 0 | allocator->free(allocator->opaque, ptr); |
84 | 1.74M | else |
85 | 1.74M | free(ptr); |
86 | | |
87 | 1.74M | return; |
88 | 1.74M | } |
89 | | |
90 | | |
91 | | ////////// |
92 | | // Misc // |
93 | | ////////// |
94 | | |
95 | | extern size_t |
96 | | lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos, |
97 | | size_t in_size, uint8_t *restrict out, |
98 | | size_t *restrict out_pos, size_t out_size) |
99 | 531k | { |
100 | 531k | const size_t in_avail = in_size - *in_pos; |
101 | 531k | const size_t out_avail = out_size - *out_pos; |
102 | 531k | const size_t copy_size = my_min(in_avail, out_avail); |
103 | | |
104 | | // Call memcpy() only if there is something to copy. If there is |
105 | | // nothing to copy, in or out might be NULL and then the memcpy() |
106 | | // call would trigger undefined behavior. |
107 | 531k | if (copy_size > 0) |
108 | 509k | memcpy(out + *out_pos, in + *in_pos, copy_size); |
109 | | |
110 | 531k | *in_pos += copy_size; |
111 | 531k | *out_pos += copy_size; |
112 | | |
113 | 531k | return copy_size; |
114 | 531k | } |
115 | | |
116 | | |
117 | | extern lzma_ret |
118 | | lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator, |
119 | | const lzma_filter_info *filters) |
120 | 847k | { |
121 | 847k | lzma_next_coder_init(filters[0].init, next, allocator); |
122 | 847k | next->id = filters[0].id; |
123 | 847k | return filters[0].init == NULL |
124 | 847k | ? LZMA_OK : filters[0].init(next, allocator, filters); |
125 | 847k | } |
126 | | |
127 | | |
128 | | extern lzma_ret |
129 | | lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator, |
130 | | const lzma_filter *reversed_filters) |
131 | 0 | { |
132 | | // Check that the application isn't trying to change the Filter ID. |
133 | | // End of filters is indicated with LZMA_VLI_UNKNOWN in both |
134 | | // reversed_filters[0].id and next->id. |
135 | 0 | if (reversed_filters[0].id != next->id) |
136 | 0 | return LZMA_PROG_ERROR; |
137 | | |
138 | 0 | if (reversed_filters[0].id == LZMA_VLI_UNKNOWN) |
139 | 0 | return LZMA_OK; |
140 | | |
141 | 0 | assert(next->update != NULL); |
142 | 0 | return next->update(next->coder, allocator, NULL, reversed_filters); |
143 | 0 | } |
144 | | |
145 | | |
146 | | extern void |
147 | | lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator) |
148 | 699k | { |
149 | 699k | if (next->init != (uintptr_t)(NULL)) { |
150 | | // To avoid tiny end functions that simply call |
151 | | // lzma_free(coder, allocator), we allow leaving next->end |
152 | | // NULL and call lzma_free() here. |
153 | 344k | if (next->end != NULL) |
154 | 344k | next->end(next->coder, allocator); |
155 | 0 | else |
156 | 0 | lzma_free(next->coder, allocator); |
157 | | |
158 | | // Reset the variables so the we don't accidentally think |
159 | | // that it is an already initialized coder. |
160 | 344k | *next = LZMA_NEXT_CODER_INIT; |
161 | 344k | } |
162 | | |
163 | 699k | return; |
164 | 699k | } |
165 | | |
166 | | |
167 | | ////////////////////////////////////// |
168 | | // External to internal API wrapper // |
169 | | ////////////////////////////////////// |
170 | | |
171 | | extern lzma_ret |
172 | | lzma_strm_init(lzma_stream *strm) |
173 | 10.3k | { |
174 | 10.3k | if (strm == NULL) |
175 | 0 | return LZMA_PROG_ERROR; |
176 | | |
177 | 10.3k | if (strm->internal == NULL) { |
178 | 10.3k | strm->internal = lzma_alloc(sizeof(lzma_internal), |
179 | 10.3k | strm->allocator); |
180 | 10.3k | if (strm->internal == NULL) |
181 | 0 | return LZMA_MEM_ERROR; |
182 | | |
183 | 10.3k | strm->internal->next = LZMA_NEXT_CODER_INIT; |
184 | 10.3k | } |
185 | | |
186 | 10.3k | memzero(strm->internal->supported_actions, |
187 | 10.3k | sizeof(strm->internal->supported_actions)); |
188 | 10.3k | strm->internal->sequence = ISEQ_RUN; |
189 | 10.3k | strm->internal->allow_buf_error = false; |
190 | | |
191 | 10.3k | strm->total_in = 0; |
192 | 10.3k | strm->total_out = 0; |
193 | | |
194 | 10.3k | return LZMA_OK; |
195 | 10.3k | } |
196 | | |
197 | | |
198 | | extern LZMA_API(lzma_ret) |
199 | | lzma_code(lzma_stream *strm, lzma_action action) |
200 | 49.5k | { |
201 | | // Sanity checks |
202 | 49.5k | if ((strm->next_in == NULL && strm->avail_in != 0) |
203 | 49.5k | || (strm->next_out == NULL && strm->avail_out != 0) |
204 | 49.5k | || strm->internal == NULL |
205 | 49.5k | || strm->internal->next.code == NULL |
206 | 49.5k | || (unsigned int)(action) > LZMA_ACTION_MAX |
207 | 49.5k | || !strm->internal->supported_actions[action]) |
208 | 0 | 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 | 49.5k | if (strm->reserved_ptr1 != NULL |
213 | 49.5k | || strm->reserved_ptr2 != NULL |
214 | 49.5k | || strm->reserved_ptr3 != NULL |
215 | 49.5k | || strm->reserved_ptr4 != NULL |
216 | 49.5k | || strm->reserved_int2 != 0 |
217 | 49.5k | || strm->reserved_int3 != 0 |
218 | 49.5k | || strm->reserved_int4 != 0 |
219 | 49.5k | || strm->reserved_enum1 != LZMA_RESERVED_ENUM |
220 | 49.5k | || strm->reserved_enum2 != LZMA_RESERVED_ENUM) |
221 | 0 | return LZMA_OPTIONS_ERROR; |
222 | | |
223 | 49.5k | switch (strm->internal->sequence) { |
224 | 10.3k | case ISEQ_RUN: |
225 | 10.3k | switch (action) { |
226 | 0 | case LZMA_RUN: |
227 | 0 | break; |
228 | | |
229 | 0 | case LZMA_SYNC_FLUSH: |
230 | 0 | strm->internal->sequence = ISEQ_SYNC_FLUSH; |
231 | 0 | break; |
232 | | |
233 | 0 | case LZMA_FULL_FLUSH: |
234 | 0 | strm->internal->sequence = ISEQ_FULL_FLUSH; |
235 | 0 | break; |
236 | | |
237 | 10.3k | case LZMA_FINISH: |
238 | 10.3k | strm->internal->sequence = ISEQ_FINISH; |
239 | 10.3k | break; |
240 | | |
241 | 0 | case LZMA_FULL_BARRIER: |
242 | 0 | strm->internal->sequence = ISEQ_FULL_BARRIER; |
243 | 0 | break; |
244 | 10.3k | } |
245 | | |
246 | 10.3k | break; |
247 | | |
248 | 10.3k | case ISEQ_SYNC_FLUSH: |
249 | | // The same action must be used until we return |
250 | | // LZMA_STREAM_END, and the amount of input must not change. |
251 | 0 | if (action != LZMA_SYNC_FLUSH |
252 | 0 | || strm->internal->avail_in != strm->avail_in) |
253 | 0 | return LZMA_PROG_ERROR; |
254 | | |
255 | 0 | break; |
256 | | |
257 | 0 | case ISEQ_FULL_FLUSH: |
258 | 0 | if (action != LZMA_FULL_FLUSH |
259 | 0 | || strm->internal->avail_in != strm->avail_in) |
260 | 0 | return LZMA_PROG_ERROR; |
261 | | |
262 | 0 | break; |
263 | | |
264 | 39.2k | case ISEQ_FINISH: |
265 | 39.2k | if (action != LZMA_FINISH |
266 | 39.2k | || strm->internal->avail_in != strm->avail_in) |
267 | 0 | return LZMA_PROG_ERROR; |
268 | | |
269 | 39.2k | break; |
270 | | |
271 | 39.2k | case ISEQ_FULL_BARRIER: |
272 | 0 | if (action != LZMA_FULL_BARRIER |
273 | 0 | || strm->internal->avail_in != strm->avail_in) |
274 | 0 | return LZMA_PROG_ERROR; |
275 | | |
276 | 0 | break; |
277 | | |
278 | 0 | case ISEQ_END: |
279 | 0 | return LZMA_STREAM_END; |
280 | | |
281 | 0 | case ISEQ_ERROR: |
282 | 0 | default: |
283 | 0 | return LZMA_PROG_ERROR; |
284 | 49.5k | } |
285 | | |
286 | 49.5k | size_t in_pos = 0; |
287 | 49.5k | size_t out_pos = 0; |
288 | 49.5k | lzma_ret ret = strm->internal->next.code( |
289 | 49.5k | strm->internal->next.coder, strm->allocator, |
290 | 49.5k | strm->next_in, &in_pos, strm->avail_in, |
291 | 49.5k | strm->next_out, &out_pos, strm->avail_out, action); |
292 | | |
293 | | // Updating next_in and next_out has to be skipped when they are NULL |
294 | | // to avoid null pointer + 0 (undefined behavior). Do this by checking |
295 | | // in_pos > 0 and out_pos > 0 because this way NULL + non-zero (a bug) |
296 | | // will get caught one way or other. |
297 | 49.5k | if (in_pos > 0) { |
298 | 29.1k | strm->next_in += in_pos; |
299 | 29.1k | strm->avail_in -= in_pos; |
300 | 29.1k | strm->total_in += in_pos; |
301 | 29.1k | } |
302 | | |
303 | 49.5k | if (out_pos > 0) { |
304 | 30.8k | strm->next_out += out_pos; |
305 | 30.8k | strm->avail_out -= out_pos; |
306 | 30.8k | strm->total_out += out_pos; |
307 | 30.8k | } |
308 | | |
309 | 49.5k | strm->internal->avail_in = strm->avail_in; |
310 | | |
311 | 49.5k | switch (ret) { |
312 | 46.3k | case LZMA_OK: |
313 | | // Don't return LZMA_BUF_ERROR when it happens the first time. |
314 | | // This is to avoid returning LZMA_BUF_ERROR when avail_out |
315 | | // was zero but still there was no more data left to written |
316 | | // to next_out. |
317 | 46.3k | if (out_pos == 0 && in_pos == 0) { |
318 | 14.1k | if (strm->internal->allow_buf_error) |
319 | 7.08k | ret = LZMA_BUF_ERROR; |
320 | 7.08k | else |
321 | 7.08k | strm->internal->allow_buf_error = true; |
322 | 32.1k | } else { |
323 | 32.1k | strm->internal->allow_buf_error = false; |
324 | 32.1k | } |
325 | 46.3k | break; |
326 | | |
327 | 0 | case LZMA_TIMED_OUT: |
328 | 0 | strm->internal->allow_buf_error = false; |
329 | 0 | ret = LZMA_OK; |
330 | 0 | break; |
331 | | |
332 | 0 | case LZMA_SEEK_NEEDED: |
333 | 0 | strm->internal->allow_buf_error = false; |
334 | | |
335 | | // If LZMA_FINISH was used, reset it back to the |
336 | | // LZMA_RUN-based state so that new input can be supplied |
337 | | // by the application. |
338 | 0 | if (strm->internal->sequence == ISEQ_FINISH) |
339 | 0 | strm->internal->sequence = ISEQ_RUN; |
340 | |
|
341 | 0 | break; |
342 | | |
343 | 9 | case LZMA_STREAM_END: |
344 | 9 | if (strm->internal->sequence == ISEQ_SYNC_FLUSH |
345 | 9 | || strm->internal->sequence == ISEQ_FULL_FLUSH |
346 | 9 | || strm->internal->sequence |
347 | 9 | == ISEQ_FULL_BARRIER) |
348 | 0 | strm->internal->sequence = ISEQ_RUN; |
349 | 9 | else |
350 | 9 | strm->internal->sequence = ISEQ_END; |
351 | | |
352 | | // Fall through |
353 | | |
354 | 9 | case LZMA_NO_CHECK: |
355 | 9 | case LZMA_UNSUPPORTED_CHECK: |
356 | 9 | case LZMA_GET_CHECK: |
357 | 24 | case LZMA_MEMLIMIT_ERROR: |
358 | | // Something else than LZMA_OK, but not a fatal error, |
359 | | // that is, coding may be continued (except if ISEQ_END). |
360 | 24 | strm->internal->allow_buf_error = false; |
361 | 24 | break; |
362 | | |
363 | 3.19k | default: |
364 | | // All the other errors are fatal; coding cannot be continued. |
365 | 3.19k | assert(ret != LZMA_BUF_ERROR); |
366 | 3.19k | strm->internal->sequence = ISEQ_ERROR; |
367 | 3.19k | break; |
368 | 49.5k | } |
369 | | |
370 | 49.5k | return ret; |
371 | 49.5k | } |
372 | | |
373 | | |
374 | | extern LZMA_API(void) |
375 | | lzma_end(lzma_stream *strm) |
376 | 10.3k | { |
377 | 10.3k | if (strm != NULL && strm->internal != NULL) { |
378 | 10.3k | lzma_next_end(&strm->internal->next, strm->allocator); |
379 | 10.3k | lzma_free(strm->internal, strm->allocator); |
380 | 10.3k | strm->internal = NULL; |
381 | 10.3k | } |
382 | | |
383 | 10.3k | return; |
384 | 10.3k | } |
385 | | |
386 | | |
387 | | #ifdef HAVE_SYMBOL_VERSIONS_LINUX |
388 | | // This is for compatibility with binaries linked against liblzma that |
389 | | // has been patched with xz-5.2.2-compat-libs.patch from RHEL/CentOS 7. |
390 | | LZMA_SYMVER_API("lzma_get_progress@XZ_5.2.2", |
391 | | void, lzma_get_progress_522)(lzma_stream *strm, |
392 | | uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow |
393 | | __attribute__((__alias__("lzma_get_progress_52"))); |
394 | | |
395 | | LZMA_SYMVER_API("lzma_get_progress@@XZ_5.2", |
396 | | void, lzma_get_progress_52)(lzma_stream *strm, |
397 | | uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow; |
398 | | |
399 | | #define lzma_get_progress lzma_get_progress_52 |
400 | | #endif |
401 | | extern LZMA_API(void) |
402 | | lzma_get_progress(lzma_stream *strm, |
403 | | uint64_t *progress_in, uint64_t *progress_out) |
404 | 0 | { |
405 | 0 | if (strm->internal->next.get_progress != NULL) { |
406 | 0 | strm->internal->next.get_progress(strm->internal->next.coder, |
407 | 0 | progress_in, progress_out); |
408 | 0 | } else { |
409 | 0 | *progress_in = strm->total_in; |
410 | 0 | *progress_out = strm->total_out; |
411 | 0 | } |
412 | |
|
413 | 0 | return; |
414 | 0 | } |
415 | | |
416 | | |
417 | | extern LZMA_API(lzma_check) |
418 | | lzma_get_check(const lzma_stream *strm) |
419 | 0 | { |
420 | | // Return LZMA_CHECK_NONE if we cannot know the check type. |
421 | | // It's a bug in the application if this happens. |
422 | 0 | if (strm->internal->next.get_check == NULL) |
423 | 0 | return LZMA_CHECK_NONE; |
424 | | |
425 | 0 | return strm->internal->next.get_check(strm->internal->next.coder); |
426 | 0 | } |
427 | | |
428 | | |
429 | | extern LZMA_API(uint64_t) |
430 | | lzma_memusage(const lzma_stream *strm) |
431 | 0 | { |
432 | 0 | uint64_t memusage; |
433 | 0 | uint64_t old_memlimit; |
434 | |
|
435 | 0 | if (strm == NULL || strm->internal == NULL |
436 | 0 | || strm->internal->next.memconfig == NULL |
437 | 0 | || strm->internal->next.memconfig( |
438 | 0 | strm->internal->next.coder, |
439 | 0 | &memusage, &old_memlimit, 0) != LZMA_OK) |
440 | 0 | return 0; |
441 | | |
442 | 0 | return memusage; |
443 | 0 | } |
444 | | |
445 | | |
446 | | extern LZMA_API(uint64_t) |
447 | | lzma_memlimit_get(const lzma_stream *strm) |
448 | 0 | { |
449 | 0 | uint64_t old_memlimit; |
450 | 0 | uint64_t memusage; |
451 | |
|
452 | 0 | if (strm == NULL || strm->internal == NULL |
453 | 0 | || strm->internal->next.memconfig == NULL |
454 | 0 | || strm->internal->next.memconfig( |
455 | 0 | strm->internal->next.coder, |
456 | 0 | &memusage, &old_memlimit, 0) != LZMA_OK) |
457 | 0 | return 0; |
458 | | |
459 | 0 | return old_memlimit; |
460 | 0 | } |
461 | | |
462 | | |
463 | | extern LZMA_API(lzma_ret) |
464 | | lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit) |
465 | 0 | { |
466 | | // Dummy variables to simplify memconfig functions |
467 | 0 | uint64_t old_memlimit; |
468 | 0 | uint64_t memusage; |
469 | |
|
470 | 0 | if (strm == NULL || strm->internal == NULL |
471 | 0 | || strm->internal->next.memconfig == NULL) |
472 | 0 | return LZMA_PROG_ERROR; |
473 | | |
474 | | // Zero is a special value that cannot be used as an actual limit. |
475 | | // If 0 was specified, use 1 instead. |
476 | 0 | if (new_memlimit == 0) |
477 | 0 | new_memlimit = 1; |
478 | |
|
479 | 0 | return strm->internal->next.memconfig(strm->internal->next.coder, |
480 | 0 | &memusage, &old_memlimit, new_memlimit); |
481 | 0 | } |