common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file common.h
  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. memcpy(out + *out_pos, in + *in_pos, copy_size);
  79. *in_pos += copy_size;
  80. *out_pos += copy_size;
  81. return copy_size;
  82. }
  83. extern lzma_ret
  84. lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator,
  85. const lzma_filter_info *filters)
  86. {
  87. lzma_next_coder_init(filters[0].init, next, allocator);
  88. next->id = filters[0].id;
  89. return filters[0].init == NULL
  90. ? LZMA_OK : filters[0].init(next, allocator, filters);
  91. }
  92. extern lzma_ret
  93. lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator,
  94. const lzma_filter *reversed_filters)
  95. {
  96. // Check that the application isn't trying to change the Filter ID.
  97. // End of filters is indicated with LZMA_VLI_UNKNOWN in both
  98. // reversed_filters[0].id and next->id.
  99. if (reversed_filters[0].id != next->id)
  100. return LZMA_PROG_ERROR;
  101. if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
  102. return LZMA_OK;
  103. assert(next->update != NULL);
  104. return next->update(next->coder, allocator, NULL, reversed_filters);
  105. }
  106. extern void
  107. lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
  108. {
  109. if (next->init != (uintptr_t)(NULL)) {
  110. // To avoid tiny end functions that simply call
  111. // lzma_free(coder, allocator), we allow leaving next->end
  112. // NULL and call lzma_free() here.
  113. if (next->end != NULL)
  114. next->end(next->coder, allocator);
  115. else
  116. lzma_free(next->coder, allocator);
  117. // Reset the variables so the we don't accidentally think
  118. // that it is an already initialized coder.
  119. *next = LZMA_NEXT_CODER_INIT;
  120. }
  121. return;
  122. }
  123. //////////////////////////////////////
  124. // External to internal API wrapper //
  125. //////////////////////////////////////
  126. extern lzma_ret
  127. lzma_strm_init(lzma_stream *strm)
  128. {
  129. if (strm == NULL)
  130. return LZMA_PROG_ERROR;
  131. if (strm->internal == NULL) {
  132. strm->internal = lzma_alloc(sizeof(lzma_internal),
  133. strm->allocator);
  134. if (strm->internal == NULL)
  135. return LZMA_MEM_ERROR;
  136. strm->internal->next = LZMA_NEXT_CODER_INIT;
  137. }
  138. memzero(strm->internal->supported_actions,
  139. sizeof(strm->internal->supported_actions));
  140. strm->internal->sequence = ISEQ_RUN;
  141. strm->internal->allow_buf_error = false;
  142. strm->total_in = 0;
  143. strm->total_out = 0;
  144. return LZMA_OK;
  145. }
  146. extern LZMA_API(lzma_ret)
  147. lzma_code(lzma_stream *strm, lzma_action action)
  148. {
  149. // Sanity checks
  150. if ((strm->next_in == NULL && strm->avail_in != 0)
  151. || (strm->next_out == NULL && strm->avail_out != 0)
  152. || strm->internal == NULL
  153. || strm->internal->next.code == NULL
  154. || (unsigned int)(action) > LZMA_ACTION_MAX
  155. || !strm->internal->supported_actions[action])
  156. return LZMA_PROG_ERROR;
  157. // Check if unsupported members have been set to non-zero or non-NULL,
  158. // which would indicate that some new feature is wanted.
  159. if (strm->reserved_ptr1 != NULL
  160. || strm->reserved_ptr2 != NULL
  161. || strm->reserved_ptr3 != NULL
  162. || strm->reserved_ptr4 != NULL
  163. || strm->reserved_int1 != 0
  164. || strm->reserved_int2 != 0
  165. || strm->reserved_int3 != 0
  166. || strm->reserved_int4 != 0
  167. || strm->reserved_enum1 != LZMA_RESERVED_ENUM
  168. || strm->reserved_enum2 != LZMA_RESERVED_ENUM)
  169. return LZMA_OPTIONS_ERROR;
  170. switch (strm->internal->sequence) {
  171. case ISEQ_RUN:
  172. switch (action) {
  173. case LZMA_RUN:
  174. break;
  175. case LZMA_SYNC_FLUSH:
  176. strm->internal->sequence = ISEQ_SYNC_FLUSH;
  177. break;
  178. case LZMA_FULL_FLUSH:
  179. strm->internal->sequence = ISEQ_FULL_FLUSH;
  180. break;
  181. case LZMA_FINISH:
  182. strm->internal->sequence = ISEQ_FINISH;
  183. break;
  184. case LZMA_FULL_BARRIER:
  185. strm->internal->sequence = ISEQ_FULL_BARRIER;
  186. break;
  187. }
  188. break;
  189. case ISEQ_SYNC_FLUSH:
  190. // The same action must be used until we return
  191. // LZMA_STREAM_END, and the amount of input must not change.
  192. if (action != LZMA_SYNC_FLUSH
  193. || strm->internal->avail_in != strm->avail_in)
  194. return LZMA_PROG_ERROR;
  195. break;
  196. case ISEQ_FULL_FLUSH:
  197. if (action != LZMA_FULL_FLUSH
  198. || strm->internal->avail_in != strm->avail_in)
  199. return LZMA_PROG_ERROR;
  200. break;
  201. case ISEQ_FINISH:
  202. if (action != LZMA_FINISH
  203. || strm->internal->avail_in != strm->avail_in)
  204. return LZMA_PROG_ERROR;
  205. break;
  206. case ISEQ_FULL_BARRIER:
  207. if (action != LZMA_FULL_BARRIER
  208. || strm->internal->avail_in != strm->avail_in)
  209. return LZMA_PROG_ERROR;
  210. break;
  211. case ISEQ_END:
  212. return LZMA_STREAM_END;
  213. case ISEQ_ERROR:
  214. default:
  215. return LZMA_PROG_ERROR;
  216. }
  217. size_t in_pos = 0;
  218. size_t out_pos = 0;
  219. lzma_ret ret = strm->internal->next.code(
  220. strm->internal->next.coder, strm->allocator,
  221. strm->next_in, &in_pos, strm->avail_in,
  222. strm->next_out, &out_pos, strm->avail_out, action);
  223. strm->next_in += in_pos;
  224. strm->avail_in -= in_pos;
  225. strm->total_in += in_pos;
  226. strm->next_out += out_pos;
  227. strm->avail_out -= out_pos;
  228. strm->total_out += out_pos;
  229. strm->internal->avail_in = strm->avail_in;
  230. // Cast is needed to silence a warning about LZMA_TIMED_OUT, which
  231. // isn't part of lzma_ret enumeration.
  232. switch ((unsigned int)(ret)) {
  233. case LZMA_OK:
  234. // Don't return LZMA_BUF_ERROR when it happens the first time.
  235. // This is to avoid returning LZMA_BUF_ERROR when avail_out
  236. // was zero but still there was no more data left to written
  237. // to next_out.
  238. if (out_pos == 0 && in_pos == 0) {
  239. if (strm->internal->allow_buf_error)
  240. ret = LZMA_BUF_ERROR;
  241. else
  242. strm->internal->allow_buf_error = true;
  243. } else {
  244. strm->internal->allow_buf_error = false;
  245. }
  246. break;
  247. case LZMA_TIMED_OUT:
  248. strm->internal->allow_buf_error = false;
  249. ret = LZMA_OK;
  250. break;
  251. case LZMA_STREAM_END:
  252. if (strm->internal->sequence == ISEQ_SYNC_FLUSH
  253. || strm->internal->sequence == ISEQ_FULL_FLUSH
  254. || strm->internal->sequence
  255. == ISEQ_FULL_BARRIER)
  256. strm->internal->sequence = ISEQ_RUN;
  257. else
  258. strm->internal->sequence = ISEQ_END;
  259. // Fall through
  260. case LZMA_NO_CHECK:
  261. case LZMA_UNSUPPORTED_CHECK:
  262. case LZMA_GET_CHECK:
  263. case LZMA_MEMLIMIT_ERROR:
  264. // Something else than LZMA_OK, but not a fatal error,
  265. // that is, coding may be continued (except if ISEQ_END).
  266. strm->internal->allow_buf_error = false;
  267. break;
  268. default:
  269. // All the other errors are fatal; coding cannot be continued.
  270. assert(ret != LZMA_BUF_ERROR);
  271. strm->internal->sequence = ISEQ_ERROR;
  272. break;
  273. }
  274. return ret;
  275. }
  276. extern LZMA_API(void)
  277. lzma_end(lzma_stream *strm)
  278. {
  279. if (strm != NULL && strm->internal != NULL) {
  280. lzma_next_end(&strm->internal->next, strm->allocator);
  281. lzma_free(strm->internal, strm->allocator);
  282. strm->internal = NULL;
  283. }
  284. return;
  285. }
  286. extern LZMA_API(void)
  287. lzma_get_progress(lzma_stream *strm,
  288. uint64_t *progress_in, uint64_t *progress_out)
  289. {
  290. if (strm->internal->next.get_progress != NULL) {
  291. strm->internal->next.get_progress(strm->internal->next.coder,
  292. progress_in, progress_out);
  293. } else {
  294. *progress_in = strm->total_in;
  295. *progress_out = strm->total_out;
  296. }
  297. return;
  298. }
  299. extern LZMA_API(lzma_check)
  300. lzma_get_check(const lzma_stream *strm)
  301. {
  302. // Return LZMA_CHECK_NONE if we cannot know the check type.
  303. // It's a bug in the application if this happens.
  304. if (strm->internal->next.get_check == NULL)
  305. return LZMA_CHECK_NONE;
  306. return strm->internal->next.get_check(strm->internal->next.coder);
  307. }
  308. extern LZMA_API(uint64_t)
  309. lzma_memusage(const lzma_stream *strm)
  310. {
  311. uint64_t memusage;
  312. uint64_t old_memlimit;
  313. if (strm == NULL || strm->internal == NULL
  314. || strm->internal->next.memconfig == NULL
  315. || strm->internal->next.memconfig(
  316. strm->internal->next.coder,
  317. &memusage, &old_memlimit, 0) != LZMA_OK)
  318. return 0;
  319. return memusage;
  320. }
  321. extern LZMA_API(uint64_t)
  322. lzma_memlimit_get(const lzma_stream *strm)
  323. {
  324. uint64_t old_memlimit;
  325. uint64_t memusage;
  326. if (strm == NULL || strm->internal == NULL
  327. || strm->internal->next.memconfig == NULL
  328. || strm->internal->next.memconfig(
  329. strm->internal->next.coder,
  330. &memusage, &old_memlimit, 0) != LZMA_OK)
  331. return 0;
  332. return old_memlimit;
  333. }
  334. extern LZMA_API(lzma_ret)
  335. lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
  336. {
  337. // Dummy variables to simplify memconfig functions
  338. uint64_t old_memlimit;
  339. uint64_t memusage;
  340. if (strm == NULL || strm->internal == NULL
  341. || strm->internal->next.memconfig == NULL)
  342. return LZMA_PROG_ERROR;
  343. // Zero is a special value that cannot be used as an actual limit.
  344. // If 0 was specified, use 1 instead.
  345. if (new_memlimit == 0)
  346. new_memlimit = 1;
  347. return strm->internal->next.memconfig(strm->internal->next.coder,
  348. &memusage, &old_memlimit, new_memlimit);
  349. }