VirtualBox

source: vbox/trunk/src/libs/liblzma-5.8.1/common/common.c

Last change on this file was 108913, checked in by vboxsync, 4 weeks ago

libs/liblzma: Liblzma ose fix. jiraref:VBP-1635

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette