common.c 11 KB

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