| 1 |
|
| 2 |
import os |
| 3 |
import sys |
| 4 |
|
| 5 |
from cffi import FFI |
| 6 |
ffi = FFI() |
| 7 |
|
| 8 |
USE_SHARED_BROTLI = os.environ.get("USE_SHARED_BROTLI") |
| 9 |
if USE_SHARED_BROTLI != "1": |
| 10 |
libraries = ['libbrotli'] |
| 11 |
else: |
| 12 |
libraries = ['brotlienc', 'brotlidec'] |
| 13 |
|
| 14 |
if 'win32' not in str(sys.platform).lower(): |
| 15 |
libraries.append('stdc++') |
| 16 |
|
| 17 |
|
| 18 |
ffi.set_source( |
| 19 |
"_brotlicffi", |
| 20 |
"""#include <brotli/decode.h> |
| 21 |
#include <brotli/encode.h> |
| 22 |
""", |
| 23 |
libraries=libraries, |
| 24 |
include_dirs=["libbrotli/c", "libbrotli/c/include", "libbrotli/c/common"] |
| 25 |
) |
| 26 |
|
| 27 |
ffi.cdef(""" |
| 28 |
/* common/types.h */ |
| 29 |
typedef bool BROTLI_BOOL; |
| 30 |
#define BROTLI_TRUE ... |
| 31 |
#define BROTLI_FALSE ... |
| 32 |
|
| 33 |
/* dec/state.h */ |
| 34 |
/* Allocating function pointer. Function MUST return 0 in the case of |
| 35 |
failure. Otherwise it MUST return a valid pointer to a memory region of |
| 36 |
at least size length. Neither items nor size are allowed to be 0. |
| 37 |
opaque argument is a pointer provided by client and could be used to |
| 38 |
bind function to specific object (memory pool). */ |
| 39 |
typedef void* (*brotli_alloc_func)(void* opaque, size_t size); |
| 40 |
|
| 41 |
/* Deallocating function pointer. Function SHOULD be no-op in the case the |
| 42 |
address is 0. */ |
| 43 |
typedef void (*brotli_free_func)(void* opaque, void* address); |
| 44 |
|
| 45 |
/* dec/decode.h */ |
| 46 |
|
| 47 |
typedef enum { |
| 48 |
/* Decoding error, e.g. corrupt input or memory allocation problem */ |
| 49 |
BROTLI_DECODER_RESULT_ERROR = 0, |
| 50 |
/* Decoding successfully completed */ |
| 51 |
BROTLI_DECODER_RESULT_SUCCESS = 1, |
| 52 |
/* Partially done; should be called again with more input */ |
| 53 |
BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT = 2, |
| 54 |
/* Partially done; should be called again with more output */ |
| 55 |
BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT = 3 |
| 56 |
} BrotliDecoderResult; |
| 57 |
|
| 58 |
typedef enum {...} BrotliDecoderErrorCode; |
| 59 |
typedef ... BrotliDecoderState; |
| 60 |
|
| 61 |
/* Creates the instance of BrotliDecoderState and initializes it. |
| 62 |
|alloc_func| and |free_func| MUST be both zero or both non-zero. In the |
| 63 |
case they are both zero, default memory allocators are used. |opaque| is |
| 64 |
passed to |alloc_func| and |free_func| when they are called. */ |
| 65 |
BrotliDecoderState* BrotliDecoderCreateInstance(brotli_alloc_func, |
| 66 |
brotli_free_func, |
| 67 |
void *); |
| 68 |
|
| 69 |
/* Deinitializes and frees BrotliDecoderState instance. */ |
| 70 |
void BrotliDecoderDestroyInstance(BrotliDecoderState* state); |
| 71 |
|
| 72 |
/* Decompresses the data. Supports partial input and output. |
| 73 |
|
| 74 |
Must be called with an allocated input buffer in |*next_in| and an |
| 75 |
allocated output buffer in |*next_out|. The values |*available_in| and |
| 76 |
|*available_out| must specify the allocated size in |*next_in| and |
| 77 |
|*next_out| respectively. |
| 78 |
|
| 79 |
After each call, |*available_in| will be decremented by the amount of |
| 80 |
input bytes consumed, and the |*next_in| pointer will be incremented by |
| 81 |
that amount. Similarly, |*available_out| will be decremented by the |
| 82 |
amount of output bytes written, and the |*next_out| pointer will be |
| 83 |
incremented by that amount. |total_out|, if it is not a null-pointer, |
| 84 |
will be set to the number of bytes decompressed since the last state |
| 85 |
initialization. |
| 86 |
|
| 87 |
Input is never overconsumed, so |next_in| and |available_in| could be |
| 88 |
passed to the next consumer after decoding is complete. */ |
| 89 |
BrotliDecoderResult BrotliDecoderDecompressStream(BrotliDecoderState* s, |
| 90 |
size_t* available_in, |
| 91 |
const uint8_t** next_in, |
| 92 |
size_t* available_out, |
| 93 |
uint8_t** next_out, |
| 94 |
size_t* total_out); |
| 95 |
|
| 96 |
/* Returns true, if decoder has some unconsumed output. |
| 97 |
Otherwise returns false. */ |
| 98 |
BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s); |
| 99 |
|
| 100 |
/* Returns true, if decoder has already received some input bytes. |
| 101 |
Otherwise returns false. */ |
| 102 |
BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s); |
| 103 |
|
| 104 |
/* Returns true, if decoder is in a state where we reached the end of the |
| 105 |
input and produced all of the output; returns false otherwise. */ |
| 106 |
BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s); |
| 107 |
|
| 108 |
/* Returns detailed error code after BrotliDecompressStream returns |
| 109 |
BROTLI_DECODER_RESULT_ERROR. */ |
| 110 |
BrotliDecoderErrorCode BrotliDecoderGetErrorCode( |
| 111 |
const BrotliDecoderState* s); |
| 112 |
|
| 113 |
const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c); |
| 114 |
|
| 115 |
/* enc/encode.h */ |
| 116 |
typedef ... BrotliEncoderState; |
| 117 |
|
| 118 |
typedef enum BrotliEncoderParameter { |
| 119 |
BROTLI_PARAM_MODE = 0, |
| 120 |
/* Controls the compression-speed vs compression-density tradeoffs. The |
| 121 |
higher the quality, the slower the compression. Range is 0 to 11. */ |
| 122 |
BROTLI_PARAM_QUALITY = 1, |
| 123 |
/* Base 2 logarithm of the sliding window size. Range is 10 to 24. */ |
| 124 |
BROTLI_PARAM_LGWIN = 2, |
| 125 |
/* Base 2 logarithm of the maximum input block size. Range is 16 to 24. |
| 126 |
If set to 0, the value will be set based on the quality. */ |
| 127 |
BROTLI_PARAM_LGBLOCK = 3 |
| 128 |
} BrotliEncoderParameter; |
| 129 |
|
| 130 |
typedef enum BrotliEncoderMode { |
| 131 |
/* Default compression mode. The compressor does not know anything in |
| 132 |
advance about the properties of the input. */ |
| 133 |
BROTLI_MODE_GENERIC = 0, |
| 134 |
/* Compression mode for UTF-8 format text input. */ |
| 135 |
BROTLI_MODE_TEXT = 1, |
| 136 |
/* Compression mode used in WOFF 2.0. */ |
| 137 |
BROTLI_MODE_FONT = 2 |
| 138 |
} BrotliEncoderMode; |
| 139 |
|
| 140 |
int BROTLI_DEFAULT_QUALITY = 11; |
| 141 |
int BROTLI_DEFAULT_WINDOW = 22; |
| 142 |
#define BROTLI_DEFAULT_MODE ... |
| 143 |
|
| 144 |
typedef enum BrotliEncoderOperation { |
| 145 |
BROTLI_OPERATION_PROCESS = 0, |
| 146 |
/* Request output stream to flush. Performed when input stream is |
| 147 |
depleted and there is enough space in output stream. */ |
| 148 |
BROTLI_OPERATION_FLUSH = 1, |
| 149 |
/* Request output stream to finish. Performed when input stream is |
| 150 |
depleted and there is enough space in output stream. */ |
| 151 |
BROTLI_OPERATION_FINISH = 2 |
| 152 |
} BrotliEncoderOperation; |
| 153 |
|
| 154 |
/* Creates the instance of BrotliEncoderState and initializes it. |
| 155 |
|alloc_func| and |free_func| MUST be both zero or both non-zero. In the |
| 156 |
case they are both zero, default memory allocators are used. |opaque| is |
| 157 |
passed to |alloc_func| and |free_func| when they are called. */ |
| 158 |
BrotliEncoderState* BrotliEncoderCreateInstance(brotli_alloc_func, |
| 159 |
brotli_free_func, |
| 160 |
void *); |
| 161 |
|
| 162 |
/* Deinitializes and frees BrotliEncoderState instance. */ |
| 163 |
void BrotliEncoderDestroyInstance(BrotliEncoderState* state); |
| 164 |
|
| 165 |
/* Compresses the data in |input_buffer| into |encoded_buffer|, and sets |
| 166 |
|*encoded_size| to the compressed length. |
| 167 |
BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW and BROTLI_DEFAULT_MODE |
| 168 |
should be used as |quality|, |lgwin| and |mode| if there are no specific |
| 169 |
requirements to encoder speed and compression ratio. |
| 170 |
If compression fails, |*encoded_size| is set to 0. |
| 171 |
If BrotliEncoderMaxCompressedSize(|input_size|) is not zero, then |
| 172 |
|*encoded_size| is never set to the bigger value. |
| 173 |
Returns false if there was an error and true otherwise. */ |
| 174 |
BROTLI_BOOL BrotliEncoderCompress(int quality, |
| 175 |
int lgwin, |
| 176 |
BrotliEncoderMode mode, |
| 177 |
size_t input_size, |
| 178 |
const uint8_t* input_buffer, |
| 179 |
size_t* encoded_size, |
| 180 |
uint8_t* encoded_buffer); |
| 181 |
|
| 182 |
BROTLI_BOOL BrotliEncoderCompressStream(BrotliEncoderState* s, |
| 183 |
BrotliEncoderOperation op, |
| 184 |
size_t* available_in, |
| 185 |
const uint8_t** next_in, |
| 186 |
size_t* available_out, |
| 187 |
uint8_t** next_out, |
| 188 |
size_t* total_out); |
| 189 |
|
| 190 |
BROTLI_BOOL BrotliEncoderSetParameter(BrotliEncoderState* state, |
| 191 |
BrotliEncoderParameter p, |
| 192 |
uint32_t value); |
| 193 |
|
| 194 |
/* Check if encoder is in "finished" state, i.e. no more input is |
| 195 |
acceptable and no more output will be produced. |
| 196 |
Works only with BrotliEncoderCompressStream workflow. |
| 197 |
Returns 1 if stream is finished and 0 otherwise. */ |
| 198 |
BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* s); |
| 199 |
|
| 200 |
/* Check if encoder has more output bytes in internal buffer. |
| 201 |
Works only with BrotliEncoderCompressStream workflow. |
| 202 |
Returns 1 if has more output (in internal buffer) and 0 otherwise. */ |
| 203 |
BROTLI_BOOL BrotliEncoderHasMoreOutput(BrotliEncoderState* s); |
| 204 |
""") |
| 205 |
|
| 206 |
if __name__ == '__main__': |
| 207 |
ffi.compile() |