File: _fecmodule.c

package info (click to toggle)
zfec 1.5.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 524 kB
  • sloc: python: 2,022; ansic: 892; haskell: 229; sh: 22; makefile: 4
file content (684 lines) | stat: -rw-r--r-- 25,312 bytes parent folder | download | duplicates (2)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/**
 * zfec -- fast forward error correction library with Python interface
 */

#include <Python.h>
#include <structmember.h>
#include <stddef.h>


#if (PY_VERSION_HEX < 0x02050000)
typedef int Py_ssize_t;
#endif

#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
#endif

#ifndef PyInt_Check
#define PyInt_Check     PyLong_Check
#define PyInt_FromLong  PyLong_FromLong
#define PyInt_AsLong    PyLong_AsLong
#define PyInt_Type      PyLong_Type
#endif

#ifndef PyString_FromStringAndSize
#define PyString_FromStringAndSize  PyBytes_FromStringAndSize
#define PyString_AsString           PyBytes_AsString
#endif

#include "fec.h"

#include "stdarg.h"

static PyObject *py_fec_error;

static char fec__doc__[] = "\
FEC - Forward Error Correction \n\
";

static char Encoder__doc__[] = "\
Hold static encoder state (an in-memory table for matrix multiplication), and k and m parameters, and provide {encode()} method.\n\n\
@param k: the number of packets required for reconstruction \n\
@param m: the number of packets generated \n\
";

typedef struct {
    PyObject_HEAD

    /* expose these */
    unsigned short kk;
    unsigned short mm;

    /* internal */
    fec_t* fec_matrix;
} Encoder;

static PyObject *
Encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwdict) {
    Encoder *self;

    self = (Encoder*)type->tp_alloc(type, 0);
    if (self != NULL) {
        self->kk = 0;
        self->mm = 0;
        self->fec_matrix = NULL;
    }

    return (PyObject *)self;
}

static int
Encoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
    static char *kwlist[] = {
        "k",
        "m",
        NULL
    };
    int ink, inm;
    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii:Encoder.__init__", kwlist, &ink, &inm))
        return -1;

    if (ink < 1) {
        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", ink);
        return -1;
    }
    if (inm < 1) {
        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", inm);
        return -1;
    }
    if (inm > 256) {
        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be less than or equal to 256, but it was %d", inm);
        return -1;
    }
    if (ink > inm) {
        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be less than or equal to the second argument, but they were %d and %d respectively", ink, inm);
        return -1;
    }
    self->kk = (unsigned short)ink;
    self->mm = (unsigned short)inm;
    self->fec_matrix = fec_new(self->kk, self->mm);

    return 0;
}

static char Encoder_encode__doc__[] = "\
Encode data into m packets.\n\
\n\
@param inblocks: a sequence of k buffers of data to encode -- these are the k primary blocks, i.e. the input data split into k pieces (for best performance, make it a tuple instead of a list);  All blocks are required to be the same length.\n\
@param desired_blocks_nums optional sequence of blocknums indicating which blocks to produce and return;  If None, all m blocks will be returned (in order).  (For best performance, make it a tuple instead of a list.)\n\
@returns: a list of buffers containing the requested blocks; Note that if any of the input blocks were 'primary blocks', i.e. their blocknum was < k, then the result sequence will contain a Python reference to the same Python object as was passed in.  As long as the Python object in question is immutable (i.e. a string) then you don't have to think about this detail, but if it is mutable (i.e. an array), then you have to be aware that if you subsequently mutate the contents of that object then that will also change the contents of the sequence that was returned from this call to encode().\n\
";

static PyObject *
Encoder_encode(Encoder *self, PyObject *args) {
    PyObject* inblocks;
    PyObject* desired_blocks_nums = NULL; /* The blocknums of the blocks that should be returned. */
    PyObject* result = NULL;

    gf** check_blocks_produced = (gf**)alloca((self->mm - self->kk) * sizeof(PyObject*)); /* This is an upper bound -- we will actually use only num_check_blocks_produced of these elements (see below). */
    PyObject** pystrs_produced = (PyObject**)alloca((self->mm - self->kk) * sizeof(PyObject*)); /* This is an upper bound -- we will actually use only num_check_blocks_produced of these elements (see below). */
    unsigned num_check_blocks_produced = 0; /* The first num_check_blocks_produced elements of the check_blocks_produced array and of the pystrs_produced array will be used. */
    const gf** incblocks = (const gf**)alloca(self->kk * sizeof(const gf*));
    size_t num_desired_blocks;
    PyObject* fast_desired_blocks_nums = NULL;
    PyObject** fast_desired_blocks_nums_items;
    size_t * c_desired_blocks_nums = (size_t*)alloca(self->mm * sizeof(size_t));
    unsigned* c_desired_checkblocks_ids = (unsigned*)alloca((self->mm - self->kk) * sizeof(unsigned));
    size_t i;
    PyObject* fastinblocks = NULL;
    PyObject** fastinblocksitems;
    Py_ssize_t sz, oldsz = 0;
    unsigned char check_block_index = 0; /* index into the check_blocks_produced and (parallel) pystrs_produced arrays */

    if (!PyArg_ParseTuple(args, "O|O:Encoder.encode", &inblocks, &desired_blocks_nums))
        return NULL;

    for (i = 0; i < self->mm - self->kk; i++)
        pystrs_produced[i] = NULL;

    if (desired_blocks_nums) {
        fast_desired_blocks_nums = PySequence_Fast(desired_blocks_nums, "Second argument (optional) was not a sequence.");

        if (!fast_desired_blocks_nums)
            goto err;

        num_desired_blocks = PySequence_Fast_GET_SIZE(fast_desired_blocks_nums);
        fast_desired_blocks_nums_items = PySequence_Fast_ITEMS(fast_desired_blocks_nums);

        for (i = 0; i < num_desired_blocks; i++) {
            if (!PyInt_Check(fast_desired_blocks_nums_items[i])) {
                PyErr_Format(py_fec_error, "Precondition violation: second argument is required to contain int.");
                goto err;
            }
            c_desired_blocks_nums[i] = PyInt_AsLong(fast_desired_blocks_nums_items[i]);
            if (c_desired_blocks_nums[i] >= self->kk)
                num_check_blocks_produced++;
        }
    } else {
        num_desired_blocks = self->mm;
        for (i = 0; i<num_desired_blocks; i++)
            c_desired_blocks_nums[i] = i;
        num_check_blocks_produced = self->mm - self->kk;
    }

    fastinblocks = PySequence_Fast(inblocks, "First argument was not a sequence.");
    if (!fastinblocks)
        goto err;

    if (PySequence_Fast_GET_SIZE(fastinblocks) != self->kk) {
        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- first argument (the sequence of input blocks) is required to contain exactly k blocks.  len(first): %zu, k: %d", PySequence_Fast_GET_SIZE(fastinblocks), self->kk);
        goto err;
    }

    /* Construct a C array of gf*'s of the input data. */
    fastinblocksitems = PySequence_Fast_ITEMS(fastinblocks);
    if (!fastinblocksitems)
        goto err;

    for (i = 0; i < self->kk; i++) {
        if (!PyObject_CheckReadBuffer(fastinblocksitems[i])) {
            PyErr_Format(py_fec_error, "Precondition violation: %zu'th item is required to offer the single-segment read character buffer protocol, but it does not.", i);
            goto err;
        }
        if (PyObject_AsReadBuffer(fastinblocksitems[i], (const void**)&(incblocks[i]), &sz))
            goto err;
        if (oldsz != 0 && oldsz != sz) {
            PyErr_Format(py_fec_error, "Precondition violation: Input blocks are required to be all the same length.  length of one block was: %zu, length of another block was: %zu", oldsz, sz);
            goto err;
        }
        oldsz = sz;
    }

    /* Allocate space for all of the check blocks. */

    for (i = 0; i < num_desired_blocks; i++) {
        if (c_desired_blocks_nums[i] >= self->kk) {
            c_desired_checkblocks_ids[check_block_index] = c_desired_blocks_nums[i];
            pystrs_produced[check_block_index] = PyString_FromStringAndSize(NULL, sz);
            if (pystrs_produced[check_block_index] == NULL)
                goto err;
            check_blocks_produced[check_block_index] = (gf*)PyString_AsString(pystrs_produced[check_block_index]);
            if (check_blocks_produced[check_block_index] == NULL)
                goto err;
            check_block_index++;
        }
    }
    assert (check_block_index == num_check_blocks_produced);

    /* Encode any check blocks that are needed. */
    fec_encode(self->fec_matrix, incblocks, check_blocks_produced, c_desired_checkblocks_ids, num_check_blocks_produced, sz);

    /* Wrap all requested blocks up into a Python list of Python strings. */
    result = PyList_New(num_desired_blocks);
    if (result == NULL)
        goto err;
    check_block_index = 0;
    for (i = 0; i < num_desired_blocks; i++) {
        if (c_desired_blocks_nums[i] < self->kk) {
            Py_INCREF(fastinblocksitems[c_desired_blocks_nums[i]]);
            if (PyList_SetItem(result, i, fastinblocksitems[c_desired_blocks_nums[i]]) == -1) {
                Py_DECREF(fastinblocksitems[c_desired_blocks_nums[i]]);
                goto err;
            }
        } else {
            if (PyList_SetItem(result, i, pystrs_produced[check_block_index]) == -1)
                goto err;
            pystrs_produced[check_block_index] = NULL;
            check_block_index++;
        }
    }

    goto cleanup;
  err:
    for (i = 0; i < num_check_blocks_produced; i++)
        Py_XDECREF(pystrs_produced[i]);
    Py_XDECREF(result); result = NULL;
  cleanup:
    Py_XDECREF(fastinblocks); fastinblocks=NULL;
    Py_XDECREF(fast_desired_blocks_nums); fast_desired_blocks_nums=NULL;
    return result;
}

static void
Encoder_dealloc(Encoder * self) {
    if (self->fec_matrix)
        fec_free(self->fec_matrix);
    Py_TYPE(self)->tp_free((PyObject*)self);
}

static PyMethodDef Encoder_methods[] = {
    {"encode", (PyCFunction)Encoder_encode, METH_VARARGS, Encoder_encode__doc__},
    {NULL},
};

static PyMemberDef Encoder_members[] = {
    {"k", T_SHORT, offsetof(Encoder, kk), READONLY, "k"},
    {"m", T_SHORT, offsetof(Encoder, mm), READONLY, "m"},
    {NULL} /* Sentinel */
};

static PyTypeObject Encoder_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "_fec.Encoder", /*tp_name*/
    sizeof(Encoder),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)Encoder_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    Encoder__doc__,           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    Encoder_methods,             /* tp_methods */
    Encoder_members,             /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)Encoder_init,      /* tp_init */
    0,                         /* tp_alloc */
    Encoder_new,                 /* tp_new */
};

static char Decoder__doc__[] = "\
Hold static decoder state (an in-memory table for matrix multiplication), and k and m parameters, and provide {decode()} method.\n\n\
@param k: the number of packets required for reconstruction \n\
@param m: the number of packets generated \n\
";

typedef struct {
    PyObject_HEAD

    /* expose these */
    unsigned short kk;
    unsigned short mm;

    /* internal */
    fec_t* fec_matrix;
} Decoder;

static PyObject *
Decoder_new(PyTypeObject *type, PyObject *args, PyObject *kwdict) {
    Decoder *self;

    self = (Decoder*)type->tp_alloc(type, 0);
    if (self != NULL) {
        self->kk = 0;
        self->mm = 0;
        self->fec_matrix = NULL;
    }

    return (PyObject *)self;
}

static int
Decoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
    static char *kwlist[] = {
        "k",
        "m",
        NULL
    };

    int ink, inm;
    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii:Decoder.__init__", kwlist, &ink, &inm))
        return -1;

    if (ink < 1) {
        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", ink);
	return -1;
    }
    if (inm < 1) {
        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", inm);
	return -1;
    }
    if (inm > 256) {
        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be less than or equal to 256, but it was %d", inm);
	return -1;
    }
    if (ink > inm) {
        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be less than or equal to the second argument, but they were %d and %d respectively", ink, inm);
	return -1;
    }
    self->kk = (unsigned short)ink;
    self->mm = (unsigned short)inm;
    self->fec_matrix = fec_new(self->kk, self->mm);

    return 0;
}

#define SWAP(a,b,t) {t tmp; tmp=a; a=b; b=tmp;}

static char Decoder_decode__doc__[] = "\
Decode a list blocks into a list of segments.\n\
@param blocks a sequence of buffers containing block data (for best performance, make it a tuple instead of a list)\n\
@param blocknums a sequence of integers of the blocknum for each block in blocks (for best performance, make it a tuple instead of a list)\n\
\n\
@return a list of strings containing the segment data (i.e. ''.join(retval) yields a string containing the decoded data)\n\
";

static PyObject *
Decoder_decode(Decoder *self, PyObject *args) {
    PyObject*restrict blocks;
    PyObject*restrict blocknums;
    PyObject* result = NULL;

    const gf**restrict cblocks = (const gf**restrict)alloca(self->kk * sizeof(const gf*));
    unsigned* cblocknums = (unsigned*)alloca(self->kk * sizeof(unsigned));
    gf**restrict recoveredcstrs = (gf**)alloca(self->kk * sizeof(gf*)); /* self->kk is actually an upper bound -- we probably won't need all of this space. */
    PyObject**restrict recoveredpystrs = (PyObject**restrict)alloca(self->kk * sizeof(PyObject*)); /* self->kk is actually an upper bound -- we probably won't need all of this space. */
    unsigned i;
    PyObject*restrict fastblocknums = NULL;
    PyObject*restrict fastblocks;
    unsigned needtorecover=0;
    PyObject** fastblocksitems;
    PyObject** fastblocknumsitems;
    Py_ssize_t sz, oldsz = 0;
    long tmpl;
    unsigned nextrecoveredix=0;

    if (!PyArg_ParseTuple(args, "OO:Decoder.decode", &blocks, &blocknums))
        return NULL;

    for (i=0; i<self->kk; i++)
        recoveredpystrs[i] = NULL;
    fastblocks = PySequence_Fast(blocks, "First argument was not a sequence.");
    if (!fastblocks)
        goto err;
    fastblocknums = PySequence_Fast(blocknums, "Second argument was not a sequence.");
    if (!fastblocknums)
        goto err;

    if (PySequence_Fast_GET_SIZE(fastblocks) != self->kk) {
        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- first argument is required to contain exactly k blocks.  len(first): %zu, k: %d", PySequence_Fast_GET_SIZE(fastblocks), self->kk);
        goto err;
    }
    if (PySequence_Fast_GET_SIZE(fastblocknums) != self->kk) {
        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- blocknums is required to contain exactly k blocks.  len(blocknums): %zu, k: %d", PySequence_Fast_GET_SIZE(fastblocknums), self->kk);
        goto err;
    }

    /* Construct a C array of gf*'s of the data and another of C ints of the blocknums. */
    fastblocknumsitems = PySequence_Fast_ITEMS(fastblocknums);
    if (!fastblocknumsitems)
        goto err;
    fastblocksitems = PySequence_Fast_ITEMS(fastblocks);
    if (!fastblocksitems)
        goto err;

    for (i=0; i<self->kk; i++) {
        if (!PyInt_Check(fastblocknumsitems[i])) {
            PyErr_Format(py_fec_error, "Precondition violation: second argument is required to contain int.");
            goto err;
        }
        tmpl = PyInt_AsLong(fastblocknumsitems[i]);
        if (tmpl < 0 || tmpl > 255) {
            PyErr_Format(py_fec_error, "Precondition violation: block nums can't be less than zero or greater than 255.  %ld\n", tmpl);
            goto err;
        }
        cblocknums[i] = (unsigned)tmpl;
        if (cblocknums[i] >= self->kk)
            needtorecover+=1;

        if (!PyObject_CheckReadBuffer(fastblocksitems[i])) {
            PyErr_Format(py_fec_error, "Precondition violation: %u'th item is required to offer the single-segment read character buffer protocol, but it does not.\n", i);
            goto err;
        }
        if (PyObject_AsReadBuffer(fastblocksitems[i], (const void**)&(cblocks[i]), &sz))
            goto err;
        if (oldsz != 0 && oldsz != sz) {
            PyErr_Format(py_fec_error, "Precondition violation: Input blocks are required to be all the same length.  length of one block was: %zu, length of another block was: %zu\n", oldsz, sz);
            goto err;
        }
        oldsz = sz;
    }

    /* Move src packets into position.  At the end of this loop we want the i'th
       element of the arrays to be the block with block number i, if that block
       is among our inputs. */
    for (i=0; i<self->kk;) {
        if (cblocknums[i] >= self->kk || cblocknums[i] == i)
            i++;
        else {
            /* put pkt in the right position. */
            unsigned c = cblocknums[i];

            SWAP (cblocknums[i], cblocknums[c], int);
            SWAP (cblocks[i], cblocks[c], const gf*);
            SWAP (fastblocksitems[i], fastblocksitems[c], PyObject*);
        }
    }

    /* Allocate space for all of the recovered blocks. */
    for (i=0; i<needtorecover; i++) {
        recoveredpystrs[i] = PyString_FromStringAndSize(NULL, sz);
        if (recoveredpystrs[i] == NULL)
            goto err;
        recoveredcstrs[i] = (gf*)PyString_AsString(recoveredpystrs[i]);
        if (recoveredcstrs[i] == NULL)
            goto err;
    }

    /* Decode any recovered blocks that are needed. */
    fec_decode(self->fec_matrix, cblocks, recoveredcstrs, cblocknums, sz);

    /* Wrap up both original primary blocks and decoded blocks into a Python list of Python strings. */
    result = PyList_New(self->kk);
    if (result == NULL)
        goto err;
    for (i=0; i<self->kk; i++) {
        if (cblocknums[i] == i) {
            /* Original primary block. */
            Py_INCREF(fastblocksitems[i]);
            if (PyList_SetItem(result, i, fastblocksitems[i]) == -1) {
                Py_DECREF(fastblocksitems[i]);
                goto err;
            }
        } else {
            /* Recovered block. */
            if (PyList_SetItem(result, i, recoveredpystrs[nextrecoveredix]) == -1)
                goto err;
            recoveredpystrs[nextrecoveredix] = NULL;
            nextrecoveredix++;
        }
    }

    goto cleanup;
  err:
    for (i=0; i<self->kk; i++)
        Py_XDECREF(recoveredpystrs[i]);
    Py_XDECREF(result); result = NULL;
  cleanup:
    Py_XDECREF(fastblocks); fastblocks=NULL;
    Py_XDECREF(fastblocknums); fastblocknums=NULL;
    return result;
}

static void
Decoder_dealloc(Decoder * self) {
    if (self->fec_matrix)
        fec_free(self->fec_matrix);
    Py_TYPE(self)->tp_free((PyObject*)self);
}

static PyMethodDef Decoder_methods[] = {
    {"decode", (PyCFunction)Decoder_decode, METH_VARARGS, Decoder_decode__doc__},
    {NULL},
};

static PyMemberDef Decoder_members[] = {
    {"k", T_SHORT, offsetof(Encoder, kk), READONLY, "k"},
    {"m", T_SHORT, offsetof(Encoder, mm), READONLY, "m"},
    {NULL} /* Sentinel */
};

static PyTypeObject Decoder_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "_fec.Decoder", /*tp_name*/
    sizeof(Decoder),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)Decoder_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    Decoder__doc__,           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    Decoder_methods,             /* tp_methods */
    Decoder_members,             /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)Decoder_init,      /* tp_init */
    0,                         /* tp_alloc */
    Decoder_new,                 /* tp_new */
};


void
_hexwrite(unsigned char*s, size_t l) {
  size_t i;
  for (i = 0; i < l; i++)
    printf("%.2x", s[i]);
}


PyObject*
test_from_agl(PyObject* self, PyObject* args) {
  unsigned char b0c[8], b1c[8];
  unsigned char b0[8], b1[8], b2[8], b3[8], b4[8];

  const unsigned char *blocks[3] = {b0, b1, b2};
  unsigned char *outblocks[2] = {b3, b4};
  unsigned block_nums[] = {3, 4};

  fec_t *const fec = fec_new(3, 5);

  const unsigned char *inpkts[] = {b3, b4, b2};
  unsigned char *outpkts[] = {b0, b1};
  unsigned indexes[] = {3, 4, 2};

  memset(b0, 1, 8);
  memset(b1, 2, 8);
  memset(b2, 3, 8);

  /*printf("_from_c before encoding:\n");
  printf("b0: "); _hexwrite(b0, 8); printf(", ");
  printf("b1: "); _hexwrite(b1, 8); printf(", ");
  printf("b2: "); _hexwrite(b2, 8); printf(", ");
  printf("\n");*/

  fec_encode(fec, blocks, outblocks, block_nums, 2, 8);

  /*printf("after encoding:\n");
  printf("b3: "); _hexwrite(b3, 8); printf(", ");
  printf("b4: "); _hexwrite(b4, 8); printf(", ");
  printf("\n");*/

  memcpy(b0c, b0, 8); memcpy(b1c, b1, 8);

  fec_decode(fec, inpkts, outpkts, indexes, 8);

  /*printf("after decoding:\n");
  printf("b0: "); _hexwrite(b0, 8); printf(", ");
  printf("b1: "); _hexwrite(b1, 8);
  printf("\n");*/

  if ((memcmp(b0, b0c,8) == 0) && (memcmp(b1, b1c,8) == 0))
    Py_RETURN_TRUE;
  else
    Py_RETURN_FALSE;
}

static PyMethodDef fec_functions[] = {
    {"test_from_agl", test_from_agl, METH_NOARGS, NULL},
    {NULL}
};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_fec", fec__doc__, -1, fec_functions, };
#endif

#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
#define MOD_ERROR_VAL NULL
PyInit__fec(void) {
#else
#define MOD_ERROR_VAL
init_fec(void) {
#endif
    PyObject *module;
    PyObject *module_dict;

    if (PyType_Ready(&Encoder_type) < 0)
        return MOD_ERROR_VAL;
    if (PyType_Ready(&Decoder_type) < 0)
        return MOD_ERROR_VAL;

#if PY_MAJOR_VERSION >= 3
    module = PyModule_Create(&moduledef);
#else
    module = Py_InitModule3("_fec", fec_functions, fec__doc__);
    if (module == NULL)
      return;
#endif

    Py_INCREF(&Encoder_type);
    Py_INCREF(&Decoder_type);

    PyModule_AddObject(module, "Encoder", (PyObject *)&Encoder_type);
    PyModule_AddObject(module, "Decoder", (PyObject *)&Decoder_type);

    module_dict = PyModule_GetDict(module);
    py_fec_error = PyErr_NewException("_fec.Error", NULL, NULL);
    PyDict_SetItemString(module_dict, "Error", py_fec_error);

#if PY_MAJOR_VERSION >= 3
    return module;
#endif
}

/**
 * originally inspired by fecmodule.c by the Mnet Project, especially Myers
 * Carpenter and Hauke Johannknecht
 */