common.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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, lzma_allocator *allocator)
  31. {
  32. void *ptr;
  33. // Some malloc() variants return NULL if called with size == 0.
  34. if (size == 0)
  35. size = 1;
  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
  43. lzma_free(void *ptr, lzma_allocator *allocator)
  44. {
  45. if (allocator != NULL && allocator->free != NULL)
  46. allocator->free(allocator->opaque, ptr);
  47. else
  48. free(ptr);
  49. return;
  50. }
  51. //////////
  52. // Misc //
  53. //////////
  54. extern size_t
  55. lzma_bufcpy(const uint8_t *LZMA_RESTRICT in, size_t *LZMA_RESTRICT in_pos,
  56. size_t in_size, uint8_t *LZMA_RESTRICT out,
  57. size_t *LZMA_RESTRICT out_pos, size_t out_size)
  58. {
  59. const size_t in_avail = in_size - *in_pos;
  60. const size_t out_avail = out_size - *out_pos;
  61. const size_t copy_size = my_min(in_avail, out_avail);
  62. memcpy(out + *out_pos, in + *in_pos, copy_size);
  63. *in_pos += copy_size;
  64. *out_pos += copy_size;
  65. return copy_size;
  66. }
  67. extern lzma_ret
  68. lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
  69. const lzma_filter_info *filters)
  70. {
  71. lzma_next_coder_init(filters[0].init, next, allocator);
  72. next->id = filters[0].id;
  73. return filters[0].init == NULL
  74. ? LZMA_OK : filters[0].init(next, allocator, filters);
  75. }
  76. extern lzma_ret
  77. lzma_next_filter_update(lzma_next_coder *next, lzma_allocator *allocator,
  78. const lzma_filter *reversed_filters)
  79. {
  80. // Check that the application isn't trying to change the Filter ID.
  81. // End of filters is indicated with LZMA_VLI_UNKNOWN in both
  82. // reversed_filters[0].id and next->id.
  83. if (reversed_filters[0].id != next->id)
  84. return LZMA_PROG_ERROR;
  85. if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
  86. return LZMA_OK;
  87. assert(next->update != NULL);
  88. return next->update(next->coder, allocator, NULL, reversed_filters);
  89. }
  90. extern void
  91. lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
  92. {
  93. if (next->init != (uintptr_t)(NULL)) {
  94. // To avoid tiny end functions that simply call
  95. // lzma_free(coder, allocator), we allow leaving next->end
  96. // NULL and call lzma_free() here.
  97. if (next->end != NULL)
  98. next->end(next->coder, allocator);
  99. else
  100. lzma_free(next->coder, allocator);
  101. // Reset the variables so the we don't accidentally think
  102. // that it is an already initialized coder.
  103. *next = LZMA_NEXT_CODER_INIT;
  104. }
  105. return;
  106. }
  107. //////////////////////////////////////
  108. // External to internal API wrapper //
  109. //////////////////////////////////////
  110. extern lzma_ret
  111. lzma_strm_init(lzma_stream *strm)
  112. {
  113. if (strm == NULL)
  114. return LZMA_PROG_ERROR;
  115. if (strm->internal == NULL) {
  116. strm->internal = lzma_alloc(sizeof(lzma_internal),
  117. strm->allocator);
  118. if (strm->internal == NULL)
  119. return LZMA_MEM_ERROR;
  120. strm->internal->next = LZMA_NEXT_CODER_INIT;
  121. }
  122. strm->internal->supported_actions[LZMA_RUN] = false;
  123. strm->internal->supported_actions[LZMA_SYNC_FLUSH] = false;
  124. strm->internal->supported_actions[LZMA_FULL_FLUSH] = false;
  125. strm->internal->supported_actions[LZMA_FINISH] = false;
  126. strm->internal->sequence = ISEQ_RUN;
  127. strm->internal->allow_buf_error = false;
  128. strm->total_in = 0;
  129. strm->total_out = 0;
  130. return LZMA_OK;
  131. }
  132. extern LZMA_API(lzma_ret)
  133. lzma_code(lzma_stream *strm, lzma_action action)
  134. {
  135. size_t in_pos = 0;
  136. size_t out_pos = 0;
  137. lzma_ret ret;
  138. // Sanity checks
  139. if ((strm->next_in == NULL && strm->avail_in != 0)
  140. || (strm->next_out == NULL && strm->avail_out != 0)
  141. || strm->internal == NULL
  142. || strm->internal->next.code == NULL
  143. || (unsigned int)(action) > LZMA_FINISH
  144. || !strm->internal->supported_actions[action])
  145. return LZMA_PROG_ERROR;
  146. // Check if unsupported members have been set to non-zero or non-NULL,
  147. // which would indicate that some new feature is wanted.
  148. if (strm->reserved_ptr1 != NULL
  149. || strm->reserved_ptr2 != NULL
  150. || strm->reserved_ptr3 != NULL
  151. || strm->reserved_ptr4 != NULL
  152. || strm->reserved_int1 != 0
  153. || strm->reserved_int2 != 0
  154. || strm->reserved_int3 != 0
  155. || strm->reserved_int4 != 0
  156. || strm->reserved_enum1 != LZMA_RESERVED_ENUM
  157. || strm->reserved_enum2 != LZMA_RESERVED_ENUM)
  158. return LZMA_OPTIONS_ERROR;
  159. switch (strm->internal->sequence) {
  160. case ISEQ_RUN:
  161. switch (action) {
  162. case LZMA_RUN:
  163. break;
  164. case LZMA_SYNC_FLUSH:
  165. strm->internal->sequence = ISEQ_SYNC_FLUSH;
  166. break;
  167. case LZMA_FULL_FLUSH:
  168. strm->internal->sequence = ISEQ_FULL_FLUSH;
  169. break;
  170. case LZMA_FINISH:
  171. strm->internal->sequence = ISEQ_FINISH;
  172. break;
  173. }
  174. break;
  175. case ISEQ_SYNC_FLUSH:
  176. // The same action must be used until we return
  177. // LZMA_STREAM_END, and the amount of input must not change.
  178. if (action != LZMA_SYNC_FLUSH
  179. || strm->internal->avail_in != strm->avail_in)
  180. return LZMA_PROG_ERROR;
  181. break;
  182. case ISEQ_FULL_FLUSH:
  183. if (action != LZMA_FULL_FLUSH
  184. || strm->internal->avail_in != strm->avail_in)
  185. return LZMA_PROG_ERROR;
  186. break;
  187. case ISEQ_FINISH:
  188. if (action != LZMA_FINISH
  189. || strm->internal->avail_in != strm->avail_in)
  190. return LZMA_PROG_ERROR;
  191. break;
  192. case ISEQ_END:
  193. return LZMA_STREAM_END;
  194. case ISEQ_ERROR:
  195. default:
  196. return LZMA_PROG_ERROR;
  197. }
  198. ret = strm->internal->next.code(
  199. strm->internal->next.coder, strm->allocator,
  200. strm->next_in, &in_pos, strm->avail_in,
  201. strm->next_out, &out_pos, strm->avail_out, action);
  202. strm->next_in += in_pos;
  203. strm->avail_in -= in_pos;
  204. strm->total_in += in_pos;
  205. strm->next_out += out_pos;
  206. strm->avail_out -= out_pos;
  207. strm->total_out += out_pos;
  208. strm->internal->avail_in = strm->avail_in;
  209. switch (ret) {
  210. case LZMA_OK:
  211. // Don't return LZMA_BUF_ERROR when it happens the first time.
  212. // This is to avoid returning LZMA_BUF_ERROR when avail_out
  213. // was zero but still there was no more data left to written
  214. // to next_out.
  215. if (out_pos == 0 && in_pos == 0) {
  216. if (strm->internal->allow_buf_error)
  217. ret = LZMA_BUF_ERROR;
  218. else
  219. strm->internal->allow_buf_error = true;
  220. } else {
  221. strm->internal->allow_buf_error = false;
  222. }
  223. break;
  224. case LZMA_STREAM_END:
  225. if (strm->internal->sequence == ISEQ_SYNC_FLUSH
  226. || strm->internal->sequence == ISEQ_FULL_FLUSH)
  227. strm->internal->sequence = ISEQ_RUN;
  228. else
  229. strm->internal->sequence = ISEQ_END;
  230. // Fall through
  231. case LZMA_NO_CHECK:
  232. case LZMA_UNSUPPORTED_CHECK:
  233. case LZMA_GET_CHECK:
  234. case LZMA_MEMLIMIT_ERROR:
  235. // Something else than LZMA_OK, but not a fatal error,
  236. // that is, coding may be continued (except if ISEQ_END).
  237. strm->internal->allow_buf_error = false;
  238. break;
  239. default:
  240. // All the other errors are fatal; coding cannot be continued.
  241. assert(ret != LZMA_BUF_ERROR);
  242. strm->internal->sequence = ISEQ_ERROR;
  243. break;
  244. }
  245. return ret;
  246. }
  247. extern LZMA_API(void)
  248. lzma_end(lzma_stream *strm)
  249. {
  250. if (strm != NULL && strm->internal != NULL) {
  251. lzma_next_end(&strm->internal->next, strm->allocator);
  252. lzma_free(strm->internal, strm->allocator);
  253. strm->internal = NULL;
  254. }
  255. return;
  256. }
  257. extern LZMA_API(lzma_check)
  258. lzma_get_check(const lzma_stream *strm)
  259. {
  260. // Return LZMA_CHECK_NONE if we cannot know the check type.
  261. // It's a bug in the application if this happens.
  262. if (strm->internal->next.get_check == NULL)
  263. return LZMA_CHECK_NONE;
  264. return strm->internal->next.get_check(strm->internal->next.coder);
  265. }
  266. extern LZMA_API(uint64_t)
  267. lzma_memusage(const lzma_stream *strm)
  268. {
  269. uint64_t memusage;
  270. uint64_t old_memlimit;
  271. if (strm == NULL || strm->internal == NULL
  272. || strm->internal->next.memconfig == NULL
  273. || strm->internal->next.memconfig(
  274. strm->internal->next.coder,
  275. &memusage, &old_memlimit, 0) != LZMA_OK)
  276. return 0;
  277. return memusage;
  278. }
  279. extern LZMA_API(uint64_t)
  280. lzma_memlimit_get(const lzma_stream *strm)
  281. {
  282. uint64_t old_memlimit;
  283. uint64_t memusage;
  284. if (strm == NULL || strm->internal == NULL
  285. || strm->internal->next.memconfig == NULL
  286. || strm->internal->next.memconfig(
  287. strm->internal->next.coder,
  288. &memusage, &old_memlimit, 0) != LZMA_OK)
  289. return 0;
  290. return old_memlimit;
  291. }
  292. extern LZMA_API(lzma_ret)
  293. lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
  294. {
  295. // Dummy variables to simplify memconfig functions
  296. uint64_t old_memlimit;
  297. uint64_t memusage;
  298. if (strm == NULL || strm->internal == NULL
  299. || strm->internal->next.memconfig == NULL)
  300. return LZMA_PROG_ERROR;
  301. if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
  302. return LZMA_MEMLIMIT_ERROR;
  303. return strm->internal->next.memconfig(strm->internal->next.coder,
  304. &memusage, &old_memlimit, new_memlimit);
  305. }