_lzma_build.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # This file is based on lzmaffi/_lzmamodule2.py from lzmaffi version 0.3.0.
  2. # PyPy changes:
  3. # - added __getstate__() methods that raise TypeError on pickling.
  4. # - ported to CFFI 1.0
  5. from cffi import FFI
  6. ffi = FFI()
  7. ffi.cdef("""
  8. #define UINT64_MAX ...
  9. #define LZMA_CONCATENATED ...
  10. #define LZMA_CHECK_NONE ...
  11. #define LZMA_CHECK_CRC32 ...
  12. #define LZMA_CHECK_CRC64 ...
  13. #define LZMA_CHECK_SHA256 ...
  14. #define LZMA_CHECK_ID_MAX ...
  15. #define LZMA_DELTA_TYPE_BYTE ...
  16. #define LZMA_TELL_ANY_CHECK ...
  17. #define LZMA_TELL_NO_CHECK ...
  18. #define LZMA_VLI_UNKNOWN ...
  19. #define LZMA_FILTER_LZMA1 ...
  20. #define LZMA_FILTER_LZMA2 ...
  21. #define LZMA_FILTER_DELTA ...
  22. #define LZMA_FILTER_X86 ...
  23. #define LZMA_FILTER_IA64 ...
  24. #define LZMA_FILTER_ARM ...
  25. #define LZMA_FILTER_ARMTHUMB ...
  26. #define LZMA_FILTER_SPARC ...
  27. #define LZMA_FILTER_POWERPC ...
  28. #define LZMA_FILTERS_MAX ...
  29. #define LZMA_STREAM_HEADER_SIZE ...
  30. #define LZMA_MF_HC3 ...
  31. #define LZMA_MF_HC4 ...
  32. #define LZMA_MF_BT2 ...
  33. #define LZMA_MF_BT3 ...
  34. #define LZMA_MF_BT4 ...
  35. #define LZMA_MODE_FAST ...
  36. #define LZMA_MODE_NORMAL ...
  37. #define LZMA_PRESET_DEFAULT ...
  38. #define LZMA_PRESET_EXTREME ...
  39. typedef enum { LZMA_OK, LZMA_STREAM_END, LZMA_NO_CHECK,
  40. LZMA_UNSUPPORTED_CHECK, LZMA_GET_CHECK,
  41. LZMA_MEM_ERROR, LZMA_MEMLIMIT_ERROR,
  42. LZMA_FORMAT_ERROR, LZMA_OPTIONS_ERROR,
  43. LZMA_DATA_ERROR, LZMA_BUF_ERROR,
  44. LZMA_PROG_ERROR, ...
  45. } lzma_ret;
  46. typedef enum { LZMA_RUN, LZMA_FINISH, ...} lzma_action;
  47. typedef enum { ... } lzma_check;
  48. typedef uint64_t lzma_vli;
  49. typedef struct {
  50. void* (*alloc)(void*, size_t, size_t);
  51. void (*free)(void*, void*);
  52. void* opaque;
  53. ...;
  54. } lzma_allocator;
  55. typedef struct {
  56. const uint8_t *next_in;
  57. size_t avail_in;
  58. uint64_t total_in;
  59. uint8_t *next_out;
  60. size_t avail_out;
  61. uint64_t total_out;
  62. lzma_allocator *allocator;
  63. ...;
  64. } lzma_stream;
  65. typedef struct {
  66. int type;
  67. uint32_t dist;
  68. ...;
  69. } lzma_options_delta;
  70. typedef struct {
  71. uint32_t start_offset;
  72. ...;
  73. } lzma_options_bcj;
  74. typedef struct {
  75. uint32_t dict_size;
  76. uint32_t lc;
  77. uint32_t lp;
  78. uint32_t pb;
  79. int mode;
  80. uint32_t nice_len;
  81. int mf;
  82. uint32_t depth;
  83. ...;
  84. } lzma_options_lzma;
  85. typedef struct {
  86. lzma_vli id;
  87. void *options;
  88. ...;
  89. } lzma_filter;
  90. typedef struct {
  91. uint32_t version;
  92. lzma_vli backward_size;
  93. int check;
  94. ...;
  95. } lzma_stream_flags;
  96. typedef ... lzma_index;
  97. typedef struct {
  98. uint32_t version;
  99. uint32_t header_size;
  100. int check;
  101. lzma_vli compressed_size;
  102. lzma_filter* filters;
  103. ...;
  104. } lzma_block;
  105. bool lzma_check_is_supported(int check);
  106. // Encoder/Decoder
  107. int lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags);
  108. int lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags);
  109. int lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit);
  110. int lzma_raw_decoder(lzma_stream *strm, const lzma_filter *filters);
  111. int lzma_block_decoder(lzma_stream *strm, lzma_block *block);
  112. int lzma_easy_encoder(lzma_stream *strm, uint32_t preset, int check);
  113. int lzma_alone_encoder(lzma_stream *strm, lzma_options_lzma* options);
  114. int lzma_raw_encoder(lzma_stream *strm, const lzma_filter *filters);
  115. int lzma_get_check(const lzma_stream *strm);
  116. int lzma_code(lzma_stream *strm, int action);
  117. void lzma_end(lzma_stream *strm);
  118. // Extras
  119. int lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in);
  120. int lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in);
  121. int lzma_stream_flags_compare(const lzma_stream_flags *a,
  122. const lzma_stream_flags *b);
  123. typedef enum {
  124. LZMA_INDEX_ITER_ANY, LZMA_INDEX_ITER_STREAM, LZMA_INDEX_ITER_BLOCK,
  125. LZMA_INDEX_ITER_NONEMPTY_BLOCK, ...
  126. } lzma_index_iter_mode;
  127. // Indexes
  128. lzma_index* lzma_index_init(lzma_allocator *al);
  129. void lzma_index_end(lzma_index *i, lzma_allocator *al);
  130. int lzma_index_stream_padding(lzma_index *i, lzma_vli stream_padding);
  131. lzma_index* lzma_index_dup(const lzma_index *i, lzma_allocator *al);
  132. int lzma_index_cat(lzma_index *dest, lzma_index *src, lzma_allocator *al);
  133. int lzma_index_buffer_decode(lzma_index **i, uint64_t *memlimit,
  134. lzma_allocator *allocator, const uint8_t *in, size_t *in_pos,
  135. size_t in_size);
  136. lzma_vli lzma_index_block_count(const lzma_index *i);
  137. lzma_vli lzma_index_stream_size(const lzma_index *i);
  138. lzma_vli lzma_index_uncompressed_size(const lzma_index *i);
  139. lzma_vli lzma_index_size(const lzma_index *i);
  140. lzma_vli lzma_index_total_size(const lzma_index *i);
  141. // Blocks
  142. int lzma_block_header_decode(lzma_block *block, lzma_allocator *al,
  143. const uint8_t *in);
  144. int lzma_block_compressed_size(lzma_block *block, lzma_vli unpadded_size);
  145. typedef struct {
  146. // cffi doesn't support partial anonymous structs
  147. // so we write the definition in full
  148. struct {
  149. const lzma_stream_flags *flags;
  150. const void *reserved_ptr1;
  151. const void *reserved_ptr2;
  152. const void *reserved_ptr3;
  153. lzma_vli number;
  154. lzma_vli block_count;
  155. lzma_vli compressed_offset;
  156. lzma_vli uncompressed_offset;
  157. lzma_vli compressed_size;
  158. lzma_vli uncompressed_size;
  159. lzma_vli padding;
  160. lzma_vli reserved_vli1;
  161. lzma_vli reserved_vli2;
  162. lzma_vli reserved_vli3;
  163. lzma_vli reserved_vli4;
  164. } stream;
  165. struct {
  166. lzma_vli number_in_file;
  167. lzma_vli compressed_file_offset;
  168. lzma_vli uncompressed_file_offset;
  169. lzma_vli number_in_stream;
  170. lzma_vli compressed_stream_offset;
  171. lzma_vli uncompressed_stream_offset;
  172. lzma_vli uncompressed_size;
  173. lzma_vli unpadded_size;
  174. lzma_vli total_size;
  175. lzma_vli reserved_vli1;
  176. lzma_vli reserved_vli2;
  177. lzma_vli reserved_vli3;
  178. lzma_vli reserved_vli4;
  179. const void *reserved_ptr1;
  180. const void *reserved_ptr2;
  181. const void *reserved_ptr3;
  182. const void *reserved_ptr4;
  183. } block;
  184. ...;
  185. } lzma_index_iter;
  186. void lzma_index_iter_init(lzma_index_iter *iter, const lzma_index *i);
  187. int lzma_index_iter_next(lzma_index_iter *iter, int mode);
  188. int lzma_index_iter_locate(lzma_index_iter *iter, lzma_vli target);
  189. // Properties
  190. int lzma_properties_size(uint32_t *size, const lzma_filter *filter);
  191. int lzma_properties_encode(const lzma_filter *filter, uint8_t *props);
  192. int lzma_properties_decode(lzma_filter *filter, lzma_allocator *allocator,
  193. const uint8_t *props, size_t props_size);
  194. int lzma_lzma_preset(lzma_options_lzma* options, uint32_t preset);
  195. // Special functions
  196. void _pylzma_stream_init(lzma_stream *strm);
  197. void _pylzma_block_header_size_decode(uint32_t b);
  198. void *malloc(size_t size);
  199. void free(void *ptr);
  200. void *realloc(void *ptr, size_t size);
  201. """)
  202. ffi.set_source('_lzma_cffi', """
  203. #ifdef _MSC_VER
  204. #define LZMA_API_STATIC
  205. #endif
  206. #include <lzma.h>
  207. #include <stdlib.h>
  208. void _pylzma_stream_init(lzma_stream *strm) {
  209. lzma_stream tmp = LZMA_STREAM_INIT; // macro from lzma.h
  210. *strm = tmp;
  211. }
  212. uint32_t _pylzma_block_header_size_decode(uint32_t b) {
  213. return lzma_block_header_size_decode(b); // macro from lzma.h
  214. }
  215. """,
  216. libraries=['lzma'])
  217. if __name__ == '__main__':
  218. ffi.compile()