common.c 11 KB

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