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