common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
  30. lzma_alloc(size_t size, const lzma_allocator *allocator)
  31. {
  32. // Some malloc() variants return NULL if called with size == 0.
  33. if (size == 0)
  34. size = 1;
  35. void *ptr;
  36. if (allocator != NULL && allocator->alloc != NULL)
  37. ptr = allocator->alloc(allocator->opaque, 1, size);
  38. else
  39. ptr = malloc(size);
  40. return ptr;
  41. }
  42. extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
  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. strm->next_in += in_pos;
  227. strm->avail_in -= in_pos;
  228. strm->total_in += in_pos;
  229. strm->next_out += out_pos;
  230. strm->avail_out -= out_pos;
  231. strm->total_out += out_pos;
  232. strm->internal->avail_in = strm->avail_in;
  233. switch (ret) {
  234. case LZMA_OK:
  235. // Don't return LZMA_BUF_ERROR when it happens the first time.
  236. // This is to avoid returning LZMA_BUF_ERROR when avail_out
  237. // was zero but still there was no more data left to written
  238. // to next_out.
  239. if (out_pos == 0 && in_pos == 0) {
  240. if (strm->internal->allow_buf_error)
  241. ret = LZMA_BUF_ERROR;
  242. else
  243. strm->internal->allow_buf_error = true;
  244. } else {
  245. strm->internal->allow_buf_error = false;
  246. }
  247. break;
  248. case LZMA_TIMED_OUT:
  249. strm->internal->allow_buf_error = false;
  250. ret = LZMA_OK;
  251. break;
  252. case LZMA_SEEK_NEEDED:
  253. strm->internal->allow_buf_error = false;
  254. // If LZMA_FINISH was used, reset it back to the
  255. // LZMA_RUN-based state so that new input can be supplied
  256. // by the application.
  257. if (strm->internal->sequence == ISEQ_FINISH)
  258. strm->internal->sequence = ISEQ_RUN;
  259. break;
  260. case LZMA_STREAM_END:
  261. if (strm->internal->sequence == ISEQ_SYNC_FLUSH
  262. || strm->internal->sequence == ISEQ_FULL_FLUSH
  263. || strm->internal->sequence
  264. == ISEQ_FULL_BARRIER)
  265. strm->internal->sequence = ISEQ_RUN;
  266. else
  267. strm->internal->sequence = ISEQ_END;
  268. // Fall through
  269. case LZMA_NO_CHECK:
  270. case LZMA_UNSUPPORTED_CHECK:
  271. case LZMA_GET_CHECK:
  272. case LZMA_MEMLIMIT_ERROR:
  273. // Something else than LZMA_OK, but not a fatal error,
  274. // that is, coding may be continued (except if ISEQ_END).
  275. strm->internal->allow_buf_error = false;
  276. break;
  277. default:
  278. // All the other errors are fatal; coding cannot be continued.
  279. assert(ret != LZMA_BUF_ERROR);
  280. strm->internal->sequence = ISEQ_ERROR;
  281. break;
  282. }
  283. return ret;
  284. }
  285. extern LZMA_API(void)
  286. lzma_end(lzma_stream *strm)
  287. {
  288. if (strm != NULL && strm->internal != NULL) {
  289. lzma_next_end(&strm->internal->next, strm->allocator);
  290. lzma_free(strm->internal, strm->allocator);
  291. strm->internal = NULL;
  292. }
  293. return;
  294. }
  295. #ifdef HAVE_SYMBOL_VERSIONS_LINUX
  296. // This is for compatibility with binaries linked against liblzma that
  297. // has been patched with xz-5.2.2-compat-libs.patch from RHEL/CentOS 7.
  298. LZMA_SYMVER_API("lzma_get_progress@XZ_5.2.2",
  299. void, lzma_get_progress_522)(lzma_stream *strm,
  300. uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow
  301. __attribute__((__alias__("lzma_get_progress_52")));
  302. LZMA_SYMVER_API("lzma_get_progress@@XZ_5.2",
  303. void, lzma_get_progress_52)(lzma_stream *strm,
  304. uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow;
  305. #define lzma_get_progress lzma_get_progress_52
  306. #endif
  307. extern LZMA_API(void)
  308. lzma_get_progress(lzma_stream *strm,
  309. uint64_t *progress_in, uint64_t *progress_out)
  310. {
  311. if (strm->internal->next.get_progress != NULL) {
  312. strm->internal->next.get_progress(strm->internal->next.coder,
  313. progress_in, progress_out);
  314. } else {
  315. *progress_in = strm->total_in;
  316. *progress_out = strm->total_out;
  317. }
  318. return;
  319. }
  320. extern LZMA_API(lzma_check)
  321. lzma_get_check(const lzma_stream *strm)
  322. {
  323. // Return LZMA_CHECK_NONE if we cannot know the check type.
  324. // It's a bug in the application if this happens.
  325. if (strm->internal->next.get_check == NULL)
  326. return LZMA_CHECK_NONE;
  327. return strm->internal->next.get_check(strm->internal->next.coder);
  328. }
  329. extern LZMA_API(uint64_t)
  330. lzma_memusage(const lzma_stream *strm)
  331. {
  332. uint64_t memusage;
  333. uint64_t old_memlimit;
  334. if (strm == NULL || strm->internal == NULL
  335. || strm->internal->next.memconfig == NULL
  336. || strm->internal->next.memconfig(
  337. strm->internal->next.coder,
  338. &memusage, &old_memlimit, 0) != LZMA_OK)
  339. return 0;
  340. return memusage;
  341. }
  342. extern LZMA_API(uint64_t)
  343. lzma_memlimit_get(const lzma_stream *strm)
  344. {
  345. uint64_t old_memlimit;
  346. uint64_t memusage;
  347. if (strm == NULL || strm->internal == NULL
  348. || strm->internal->next.memconfig == NULL
  349. || strm->internal->next.memconfig(
  350. strm->internal->next.coder,
  351. &memusage, &old_memlimit, 0) != LZMA_OK)
  352. return 0;
  353. return old_memlimit;
  354. }
  355. extern LZMA_API(lzma_ret)
  356. lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
  357. {
  358. // Dummy variables to simplify memconfig functions
  359. uint64_t old_memlimit;
  360. uint64_t memusage;
  361. if (strm == NULL || strm->internal == NULL
  362. || strm->internal->next.memconfig == NULL)
  363. return LZMA_PROG_ERROR;
  364. // Zero is a special value that cannot be used as an actual limit.
  365. // If 0 was specified, use 1 instead.
  366. if (new_memlimit == 0)
  367. new_memlimit = 1;
  368. return strm->internal->next.memconfig(strm->internal->next.coder,
  369. &memusage, &old_memlimit, new_memlimit);
  370. }