Port bzzdec plugin to build on python2/python3

This commit is contained in:
Eli Schwartz 2019-01-10 02:40:15 -05:00
parent 1a16a0e545
commit dcf58e9a15

View File

@ -23,7 +23,7 @@
typedef uint8_t bool; typedef uint8_t bool;
typedef struct Table { typedef struct Table {
uint16_t p; uint16_t p;
uint16_t m; uint16_t m;
uint8_t up; uint8_t up;
@ -315,11 +315,11 @@ static Table default_ztable[256] = // {{{
#define MIN(x, y) ((x < y) ? x : y) #define MIN(x, y) ((x < y) ? x : y)
#define ffz(ffzt, x) ((x>=0xff00) ? (ffzt[x&0xff]+8) : (ffzt[(x>>8)&0xff])) #define ffz(ffzt, x) ((x>=0xff00) ? (ffzt[x&0xff]+8) : (ffzt[(x>>8)&0xff]))
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0
#define MAXBLOCK 4096 #define MAXBLOCK 4096
#define FREQMAX 4 #define FREQMAX 4
#define CTXIDS 3 #define CTXIDS 3
@ -397,7 +397,7 @@ static inline int32_t decode_sub_simple(State *state, int32_t mps, uint32_t z) {
} }
state->fence = MIN(state->code, 0x7fff); state->fence = MIN(state->code, 0x7fff);
return mps ^ 1; return mps ^ 1;
} }
/* MPS renormalization */ /* MPS renormalization */
state->scount -= 1; state->scount -= 1;
@ -543,10 +543,10 @@ static bool decode(State *state, uint8_t *ctx) {
ctxid = 2 * CTXIDS; ctxid = 2 * CTXIDS;
for (j = 1; j < 8; j++) { for (j = 1; j < 8; j++) {
if (zpcodec_decode(state, ctx, ctxid)) { if (zpcodec_decode(state, ctx, ctxid)) {
mtfno = (1 << j) + decode_binary(state, ctx, ctxid, j); mtfno = (1 << j) + decode_binary(state, ctx, ctxid, j);
state->buf[i] = mtf[mtfno]; state->buf[i] = mtf[mtfno];
goto rotate; goto rotate;
} }
ctxid += 1 << j; ctxid += 1 << j;
} }
@ -571,7 +571,7 @@ static bool decode(State *state, uint8_t *ctx) {
// Relocate new char according to new freq // Relocate new char according to new freq
fc = fadd; fc = fadd;
if (mtfno < FREQMAX) fc += freq[mtfno]; if (mtfno < FREQMAX) fc += freq[mtfno];
for (k=mtfno; k>=FREQMAX; k--) for (k=mtfno; k>=FREQMAX; k--)
mtf[k] = mtf[k-1]; mtf[k] = mtf[k-1];
for (; k>0 && fc>=freq[k-1]; k--) { for (; k>0 && fc>=freq[k-1]; k--) {
mtf[k] = mtf[k-1]; mtf[k] = mtf[k-1];
@ -585,7 +585,7 @@ static bool decode(State *state, uint8_t *ctx) {
if (markerpos<1 || (uint32_t)markerpos>=state->xsize) { CORRUPT; goto end; } if (markerpos<1 || (uint32_t)markerpos>=state->xsize) { CORRUPT; goto end; }
// Allocate pointers // Allocate pointers
// Fill count buffer // Fill count buffer
for (i=0; i<(uint32_t)markerpos; i++) for (i=0; i<(uint32_t)markerpos; i++)
{ {
c = state->buf[i]; c = state->buf[i];
posn[i] = (c<<24) | (count[c] & 0xffffff); posn[i] = (c<<24) | (count[c] & 0xffffff);
@ -682,8 +682,10 @@ end:
if (PyErr_Occurred()) return NULL; if (PyErr_Occurred()) return NULL;
return ans; return ans;
} }
static PyMethodDef bzzdecmethods[] = { static char bzzdec_doc[] = "Decompress BZZ encoded strings (used in DJVU)";
static PyMethodDef bzzdec_methods[] = {
{"decompress", bzz_decompress, METH_VARARGS, {"decompress", bzz_decompress, METH_VARARGS,
"decompress(bytestring) -> decompressed bytestring\n\n" "decompress(bytestring) -> decompressed bytestring\n\n"
"Decompress a BZZ compressed byte string. " "Decompress a BZZ compressed byte string. "
@ -692,15 +694,32 @@ static PyMethodDef bzzdecmethods[] = {
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
#if PY_MAJOR_VERSION >= 3
#define INITERROR return NULL
#define INITMODULE PyModule_Create(&bzzdec_module)
static struct PyModuleDef bzzdec_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "bzzdec",
/* m_doc */ bzzdec_doc,
/* m_size */ -1,
/* m_methods */ bzzdec_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_bzzdec(void) {
#else
#define INITERROR return
#define INITMODULE Py_InitModule3("bzzdec", bzzdec_methods, bzzdec_doc)
CALIBRE_MODINIT_FUNC initbzzdec(void) {
#endif
PyObject *m = INITMODULE;
CALIBRE_MODINIT_FUNC if (m == NULL) {
initbzzdec(void) { INITERROR;
PyObject *m; }
m = Py_InitModule3("bzzdec", bzzdecmethods, #if PY_MAJOR_VERSION >= 3
"Decompress BZZ encoded strings (used in DJVU)" return m;
); #endif
if (m == NULL) return;
} }