summaryrefslogtreecommitdiff
path: root/src/c/pyeclib_c/pyeclib_c.c
blob: 42e110200707a15031c6aa0e3b3ca0e04ec92145 (plain)
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
/* 
 * Copyright (c) 2013-2014, Kevin Greenan (kmgreen2@gmail.com)
 * Copyright (c) 2014, Tushar Gohad (tusharsg@gmail.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.  THIS SOFTWARE IS PROVIDED BY
 * THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <Python.h>

/* Compat layer for python <= 2.6 */
#include "capsulethunk.h"

#include <xor_code.h>
#include <reed_sol.h>
#include <alg_sig.h>
#include <cauchy.h>
#include <jerasure.h>
#include <liberation.h>
#include <galois.h>
#include <math.h>
#include <pyeclib_c.h>
#include <bytesobject.h>

/* Python 3 compatibility macros */
#if PY_MAJOR_VERSION >= 3
  #define MOD_ERROR_VAL NULL
  #define MOD_SUCCESS_VAL(val) val
  #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
  #define MOD_DEF(ob, name, doc, methods) \
          static struct PyModuleDef moduledef = { \
              PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
          ob = PyModule_Create(&moduledef);
  #define PY_BUILDVALUE_OBJ_LEN(obj, objlen) \
          Py_BuildValue("y#", obj, objlen)
  #define PyInt_FromLong PyLong_FromLong
  #define PyString_FromString PyUnicode_FromString
  #define ENCODE_ARGS "Oy#"
  #define GET_METADATA_ARGS "Oy#"
#else
  #define MOD_ERROR_VAL
  #define MOD_SUCCESS_VAL(val)
  #define MOD_INIT(name) void init##name(void)
  #define MOD_DEF(ob, name, doc, methods) \
          ob = Py_InitModule3(name, methods, doc);
  #define PY_BUILDVALUE_OBJ_LEN(obj, objlen) \
          Py_BuildValue("s#", obj, objlen)
  #define ENCODE_ARGS "Os#"
  #define GET_METADATA_ARGS "Os#"
#endif


/*
 * TODO (kmg): Cauchy restriction (k*w*PACKETSIZE)  < data_len / k, otherwise you could
 * end up with full-blocks of padding
 *
 */

/*
 * TODO (kmg): Do a big cleanup: standardize naming, types (typedef stuff), comments, standard error
 * handling and refactor where needed.
 */

/*
 * TODO (kmg): Need to clean-up all of the reference counting related stuff.  That is, INCREF before 
 * calling a function that will "steal" a reference and create functions to DECREF stuff.
 */

/*
 * Make the buffer alignment code in 'encode' generic and simple...  It sucks right now.
 */

static PyObject *PyECLibError;


/**
 * Prototypes for Python/C API methods
 */
static PyObject * pyeclib_c_init(PyObject *self, PyObject *args);
static void pyeclib_c_destructor(PyObject *obj);
static PyObject * pyeclib_c_get_segment_info(PyObject *self, PyObject *args);
static PyObject * pyeclib_c_encode(PyObject *self, PyObject *args);
static PyObject * pyeclib_c_fragments_to_string(PyObject *self, PyObject *args);
static PyObject * pyeclib_c_get_fragment_partition(PyObject *self, PyObject *args);
static PyObject * pyeclib_c_reconstruct(PyObject *self, PyObject *args);
static PyObject * pyeclib_c_decode(PyObject *self, PyObject *args);
static PyObject * pyeclib_c_get_metadata(PyObject *self, PyObject *args);
static PyObject * pyeclib_c_check_metadata(PyObject *self, PyObject *args);


/*
 * Determine if an address is aligned to a particular boundary
 */
static int is_addr_aligned(unsigned long addr, int align)
{
  return (addr & (align-1)) == 0;
}

/*
 * Validate the basic erasure code parameters 
 */
static int validate_args(int k, int m, int w, pyeclib_type_t type)
{

  switch (type) {
    case PYECC_RS_CAUCHY_ORIG:
      {
        if (w < 31 && (k+m) > (1 << w)) {
          return 0;
        }
      }
    case PYECC_XOR_HD_4:
    case PYECC_XOR_HD_3:
      return 1; 
    case PYECC_RS_VAND:
    default:
      {
        long long max_symbols;
      
        if (w != 8 && w != 16 && w != 32) {
          return 0;
        }
        max_symbols = 1LL << w;
        if ((k+m) > max_symbols) {
          return 0;
        }
      }
  }
  return 1;
}

/*
 * Convert an int list into a bitmap
 * Assume the list is '-1' terminated.
 */
static unsigned long long convert_list_to_bitmap(int *list)
{
  int i = 0;
  unsigned long long bm = 0;

  while (list[i] > -1) {
    /*
     * TODO: Assert list[i] < 64
     */
    bm |= (1 << list[i]);  
    i++;
  } 

  return bm;
}

static
void init_fragment_header(char *buf)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  header->magic = PYECC_HEADER_MAGIC;
}


/**
 * Memory Management Methods
 * 
 * The following methods provide wrappers for allocating and deallocating
 * memory.  
 * 
 * Future discussions may want to consider moving closer to the recommended
 * guidelines in the Python\C API reference manual.  One potential issue,
 * however, may be how we enforce memory alignment in the Python heap.
 *
 * 2.7: https://docs.python.org/2.7/c-api/memory.html
 * 3.4: https://docs.python.org/3.4/c-api/memory.html
 */
 
/**
 * Allocate a zero-ed buffer of a specific size.  This method allocates from
 * the Python stack in order to comply with the recommendations of the
 * Python\C API.  On error, return NULL and call PyErr_NoMemory.
 *
 * @param size integer size in bytes of buffer to allocate
 * @return pointer to start of allocated buffer or NULL on error
 */
static
void * alloc_zeroed_buffer(int size)
{
  void * buf = NULL;  /* buffer to allocate and return */
  
  /* Allocate and zero the buffer, or set the appropriate error */
  buf = PyMem_Malloc((size_t) size);
  if (buf) {
    buf = memset(buf, 0, (size_t) size);
  } else {
    buf = (void *) PyErr_NoMemory();
  }

  return buf;
}


/**
 * Deallocate memory buffer if it's not NULL.  This methods returns NULL so 
 * that you can free and reset a buffer using a single line as follows:
 *
 * my_ptr = check_and_free_buffer(my_ptr);
 *
 * @return NULL
 */
static
void * check_and_free_buffer(void * buf)
{
  if (buf) PyMem_Free(buf);
  
  return NULL;
}


/**
 * Allocates the resources required for the algebraic signatures.  On error,
 * PyErr_NoMemory is called to set the appropriate error response and NULL
 * is returned.
 *
 * NOTE: It is the caller's responsibility to free the memory allocated here!
 *
 * @param sig_len integer signature length
 * @param gf_w integer galois field size
 * @return ptr to allocated and initialized algebraic signature tables
 */
static
alg_sig_t *alloc_alg_sig(int sig_len, int gf_w)
{
  alg_sig_t *buf = NULL;  /* algebraic signature handle to allocate */
  
  buf = init_alg_sig(sig_len, gf_w);
  if (NULL == buf) {
    PyErr_NoMemory();
  }

  return buf;
}


/**
 * Deallocate the resources used for algebraic signatures.
 *
 * @return NULL
 */
static
void * check_and_free_alg_sig(alg_sig_t *buf)
{
  if (buf) destroy_alg_sig(buf);
  
  return NULL;
}


/**
 * Allocate and zero out a buffer aligned to a 16-byte boundary in order
 * to support 128-bit operations.  On error, call PyErr_NoMemory to set
 * the appropriate error string and return NULL.
 * 
 * @param size integer size in bytes of buffer to allocate
 * @return pointer to start of allocated buffer, or NULL on error
 */
static
void* alloc_aligned_buffer16(int size)
{
  void *buf = NULL;
  
  /*
   * Ensure all memory is aligned to
   * 16-byte boundaries to support 
   * 128-bit operations
   */
  if (posix_memalign(&buf, 16, size) < 0) {
    return PyErr_NoMemory();
  } else {
    memset(buf, 0, size);
  }
  
  return buf;
}


/**
 * Allocate an initialized fragment buffer.  On error, return NULL and 
 * preserve call to PyErr_NoMemory.  Note, all allocated memory is aligned 
 * to 16-bytes boundaries in order to support 128-bit operations.
 *
 * @param size integer size in bytes of buffer to allocate
 * @return pointer to start of allocated fragment or NULL on error
 */
static
char *alloc_fragment_buffer(int size)
{
  char *buf = NULL;
  fragment_header_t *header = NULL;

  /* Calculate size needed for fragment header + data */
  size += sizeof(fragment_header_t);

  /* Allocate and init the aligned buffer, or set the appropriate error */
  buf = alloc_aligned_buffer16(size);
  if (buf) {
    init_fragment_header(buf);
  }

  return buf;
}


/**
 * Deallocate a fragment buffer.  This method confirms via magic number that
 * the passed in buffer is a proper fragment buffer.
 *
 * @param buf pointer to fragment buffer
 * @return 0 on successful free, -1 on error
 */
static
int free_fragment_buffer(char *buf)
{
  fragment_header_t *header;
  if (buf == NULL) {
    return -1;
  }

  buf -= sizeof(fragment_header_t);

  header = (fragment_header_t*)buf;
  if (header->magic != PYECC_HEADER_MAGIC) {
    PyErr_SetString(PyECLibError, "Invalid fragment header (free fragment)!"); 
    return -1; 
  }
  free(buf);

  return 0;
}


/**
 * Allocate a zero-ed Python string of a specific size. On error, call
 * PyErr_NoMemory and return NULL.
 *
 * @param size integer size in bytes of zero string to create
 * @return pointer to start of allocated zero string or NULL on error
 */
static
PyObject * alloc_zero_string(int size)
{
  char *tmp_data = NULL;         /* tmp buffer used in PyObject creation */
  PyObject *zero_string = NULL;  /* zero string to return */
  
  /* Create the zero-out c-string buffer */
  tmp_data = (char *) alloc_zeroed_buffer(size);
  if (NULL == tmp_data) {
    return PyErr_NoMemory();
  }
  
  /* Create the python value to return */
  zero_string = PY_BUILDVALUE_OBJ_LEN(tmp_data, size);
  check_and_free_buffer(tmp_data);
  
  return zero_string;
}


static
char* get_data_ptr_from_fragment(char *buf)
{
  char * data_ptr = NULL;
  
  if (NULL != buf) {
    data_ptr = buf + sizeof(fragment_header_t);
  }
  
  return data_ptr;
}

static
char* get_fragment_ptr_from_data_novalidate(char *buf)
{
  buf -= sizeof(fragment_header_t);

  return buf;
}

static 
int use_inline_chksum(pyeclib_t *pyeclib_handle)
{
  return pyeclib_handle->inline_chksum;
}

static
int supports_alg_sig(pyeclib_t *pyeclib_handle)
{
  if (!pyeclib_handle->algsig_chksum) return 0;
  if (pyeclib_handle->alg_sig_desc != NULL) return 1;
  return 0;
}

static
char* get_fragment_ptr_from_data(char *buf)
{
  fragment_header_t *header;

  buf -= sizeof(fragment_header_t);

  header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    fprintf(stderr, "Invalid fragment header (get header ptr)!\n");
    return NULL; 
  }

  return buf;
}

static
int set_chksum(char *buf, int chksum)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    fprintf(stderr, "Invalid fragment header (set chksum)!\n");
    return -1; 
  }

  header->chksum = chksum;
  
  return 0;
}

static
int get_chksum(char *buf)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    PyErr_SetString(PyECLibError, "Invalid fragment header (get chksum)!");
    return -1;
  }

  return header->chksum;
}

static
int set_fragment_idx(char *buf, int idx)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    fprintf(stderr, "Invalid fragment header (idx check)!\n");
    return -1; 
  }

  header->idx = idx;
  
  return 0;
}

static
int get_fragment_idx(char *buf)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    PyErr_SetString(PyECLibError, "Invalid fragment header (get idx)!");
    return -1;
  }

  return header->idx;
}

static
int set_fragment_size(char *buf, int size)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    PyErr_SetString(PyECLibError, "Invalid fragment header (size check)!");
    return -1; 
  }

  header->size = size;
    
  return 0;
}

static
int get_fragment_size(char *buf)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    PyErr_SetString(PyECLibError, "Invalid fragment header (get size)!");
    return -1;
  }

  return header->size;
}

static
int set_orig_data_size(char *buf, int orig_data_size)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    PyErr_SetString(PyECLibError, "Invalid fragment header (set orig data check)!");
    return -1; 
  }

  header->orig_data_size = orig_data_size;

  return 0;
}

static
int get_orig_data_size(char *buf)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    PyErr_SetString(PyECLibError, "Invalid fragment header (get orig data check)!");
    return -1;
  }

  return header->orig_data_size;
}

static
int validate_fragment(char *buf)
{
  fragment_header_t* header = (fragment_header_t*)buf;

  if (header->magic != PYECC_HEADER_MAGIC) {
    return -1;
  }

  return 0;
}


/*
 * Prepares the fragments and helper data structures for decoding.  Note, 
 * buffers for data, parity and missing_idxs must be allocated by the caller.
 *
 * @param *pyeclib_handle
 * @param *data_list python list of data fragments
 * @param *parity_list python list of parity fragments
 * @param *missing_idx_list python list of indexes of missing fragments
 * @param **data allocated k-length array of data buffers
 * @param **parity allocated m-length array of parity buffers
 * @param *missing_idxs array of missing indexes with extra space for -1 terminator
 * @param *orig_size original size of object from the fragment header
 * @param fragment_size integer size in bytes of fragment
 * @return 0 on success, -1 on error
 */
static int get_decoding_info(pyeclib_t *pyeclib_handle,
                             PyObject  *data_list, 
                             PyObject  *parity_list, 
                             PyObject  *missing_idx_list,
                             char      **data,
                             char      **parity,
                             int       *missing_idxs,
                             int       *orig_size,
                             int       fragment_size,
                             unsigned long long *realloc_bm)
{
  int i;                          /* a counter */
  int data_size = 0;              /* number of data fragments provided */
  int parity_size = 0;            /* number of parity fragments provided */
  int missing_size = 0;           /* number of missing index entries */
  int orig_data_size = -1;        /* data size (B) from fragment header */
  unsigned long long missing_bm;  /* bitmap form of missing indexes list */
  int needs_addr_alignment = PYECLIB_NEEDS_ADDR_ALIGNMENT(pyeclib_handle->type);
  
  /* Validate the list sizes */
  data_size = (int)PyList_Size(data_list);
  parity_size = (int)PyList_Size(parity_list);
  missing_size = (int)PyList_Size(missing_idx_list);  
  if (data_size != pyeclib_handle->k) {
    return -1;
  }
  if (parity_size != pyeclib_handle->m) {
    return -1;
  }
  
  /* Record missing indexes in a -1 terminated array and bitmap */
  for (i = 0; i < missing_size; i++) {
    PyObject *obj_idx = PyList_GetItem(missing_idx_list, i);
    
    missing_idxs[i] = (int) PyLong_AsLong(obj_idx);;
  }
  missing_idxs[i] = -1; 
  missing_bm = convert_list_to_bitmap(missing_idxs);

  /*
   * Determine if each data fragment is:
   * 1.) Alloc'd: if not, alloc new buffer (for missing fragments)
   * 2.) Aligned to 16-byte boundaries: if not, alloc a new buffer
   *     memcpy the contents and free the old buffer 
   */
  for (i = 0; i < pyeclib_handle->k; i++) {
    PyObject *tmp_data = PyList_GetItem(data_list, i);
    Py_ssize_t len = 0;
    PyBytes_AsStringAndSize(tmp_data, &(data[i]), &len);
    
    /*
     * Allocate or replace with aligned buffer if the buffer was not aligned.
     * DO NOT FREE: the python GC should free the original when cleaning up 'data_list'
     */
    if (len == 0 || !data[i]) {
      data[i] = alloc_fragment_buffer(fragment_size - sizeof(fragment_header_t));
      if (NULL == data[i]) {
        return -1;
      }
      *realloc_bm = *realloc_bm | (1 << i);
    } else if ((needs_addr_alignment && !is_addr_aligned((unsigned long)data[i], 16))) {
      char *tmp_buf = alloc_fragment_buffer(fragment_size - sizeof(fragment_header_t));
      memcpy(tmp_buf, data[i], fragment_size);
      data[i] = tmp_buf;
      *realloc_bm = *realloc_bm | (1 << i);
    }

    /* Need to determine the size of the original data */
    if (((missing_bm & (1 << i)) == 0) && orig_data_size < 0) {
      orig_data_size = get_orig_data_size(data[i]);
      if (orig_data_size < 0) {
        return -1;
      }
    }

    /* Set the data element to the fragment payload */
    data[i] = get_data_ptr_from_fragment(data[i]);
    if (data[i] == NULL) {
      return -1;
    }
  }

  /* Perform the same allocation, alignment checks on the parity fragments */
  for (i=0; i < pyeclib_handle->m; i++) {
    PyObject *tmp_parity = PyList_GetItem(parity_list, i);
    Py_ssize_t len = 0;
    PyBytes_AsStringAndSize(tmp_parity, &(parity[i]), &len);
    
    /*
     * Allocate or replace with aligned buffer, if the buffer was not aligned.
     * DO NOT FREE: the python GC should free the original when cleaning up 'data_list'
     */
    if (len == 0 || !parity[i]) {
      parity[i] = alloc_fragment_buffer(fragment_size-sizeof(fragment_header_t));
      if (NULL == parity[i]) {
        return -1;
      }
      *realloc_bm = *realloc_bm | (1 << (pyeclib_handle->k + i));
    } else if (needs_addr_alignment && !is_addr_aligned((unsigned long)parity[i], 16)) {
      char *tmp_buf = alloc_fragment_buffer(fragment_size-sizeof(fragment_header_t));
      memcpy(tmp_buf, parity[i], fragment_size);
      parity[i] = tmp_buf;
      *realloc_bm = *realloc_bm | (1 << (pyeclib_handle->k + i));
    } 
    
    /* Set the parity element to the fragment payload */
    parity[i] = get_data_ptr_from_fragment(parity[i]);
    if (parity[i] == NULL) {
      return -1;
    }
  }

  *orig_size = orig_data_size;
  return 0; 
}


/**
 * Compute a size aligned to the number of data and the underlying wordsize 
 * of the EC algorithm.
 * 
 * @param pyeclib_handle eclib object with EC configurations
 * @param data_len integer length of data in bytes
 * @return integer data length aligned with wordsize of EC algorithm
 */
static int
get_aligned_data_size(pyeclib_t* pyeclib_handle, int data_len)
{
  int word_size = pyeclib_handle->w / 8;
  int alignment_multiple;
  int aligned_size = 0;

  /*
   * For Cauchy reed-solomon align to k*word_size*packet_size
   * For Vandermonde reed-solomon and flat-XOR, align to k*word_size
   */
  if (pyeclib_handle->type == PYECC_RS_CAUCHY_ORIG) {
    alignment_multiple = pyeclib_handle->k * pyeclib_handle->w * PYECC_CAUCHY_PACKETSIZE;
  } else {
    alignment_multiple = pyeclib_handle->k * word_size;
  }

  aligned_size = (int)ceill((double)data_len / alignment_multiple) * alignment_multiple;

  return aligned_size;
}


static
int get_minimum_encode_size(pyeclib_t *pyeclib_handle)
{
  return get_aligned_data_size(pyeclib_handle, 1);
}


static
int get_fragment_metadata(pyeclib_t *pyeclib_handle, char *fragment_buf, fragment_metadata_t *fragment_metadata)
{
  char *fragment_data = get_data_ptr_from_fragment(fragment_buf);
  int fragment_size = get_fragment_size(fragment_buf);
  int fragment_idx = get_fragment_idx(fragment_buf);

  memset(fragment_metadata, 0, sizeof(fragment_metadata_t));

  fragment_metadata->size = fragment_size;
  fragment_metadata->idx = fragment_idx;

  /*
   * If w \in [8, 16] and using RS_VAND or XOR_CODE, then use Alg_sig
   * Else use CRC32
   */
  if (supports_alg_sig(pyeclib_handle)) {
    // Compute algebraic signature
    compute_alg_sig(pyeclib_handle->alg_sig_desc, fragment_data, fragment_size, fragment_metadata->signature);
  } else if (use_inline_chksum(pyeclib_handle)) {
    int stored_chksum = get_chksum(fragment_buf);
    int computed_chksum = crc32(0, fragment_data, fragment_size); 

    if (stored_chksum != computed_chksum) {
      fragment_metadata->chksum_mismatch = 1;
    }
  }

  return 0;
}


/**
 * Free the memory allocated for the Cauchy Reed Solomon encoder.
 */
static void
check_and_free_rs_cauchy_orig(pyeclib_t *handle)
{
  if (NULL != handle->matrix) {
    free(handle->matrix);
    handle->matrix = NULL;
  }
  if (NULL != handle->bitmatrix) {
    free(handle->bitmatrix);
    handle->bitmatrix = NULL;
  }
  if (NULL != handle->schedule) {
    free(handle->schedule);
    handle->schedule = NULL;
  }
}


/**
 * Allocate memory and configuration the pyeclib handle for the Cauchy Reed Solomon 
 * encoder.  Note, this method allocates memory which must be freed by the caller
 * by using the check_and_free_rs_cauchy_orig method.
 *
 * @param handle: allocated pyeclib handle to configure
 * @returns: 0 on success, non-zero on error
 */
static int
init_rs_cauchy_orig(pyeclib_t *handle)
{ 
  int k = handle->k;  /* number of data elements param from handle */
  int m = handle->m;  /* number of checksum elements param from handle */
  int w = handle->w;  /* word size param from handle */
  int rc = 0;         /* return code, 0 on success */
  
  /* Zero out the exiting values, alloc and initialize the pyeclib handle*/
  handle->matrix = NULL;
  handle->bitmatrix = NULL;
  handle->schedule = NULL;
  handle->matrix = cauchy_original_coding_matrix(k, m, w);
  if (NULL == handle->matrix) goto error;
  handle->bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, handle->matrix);
  if (NULL == handle->bitmatrix) goto error;
  handle->schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, handle->bitmatrix);
  if (NULL == handle->schedule) goto error;
  goto exit;
  
error:
  check_and_free_rs_cauchy_orig(handle);
  PyErr_NoMemory();
  rc = 1;

exit:
  return rc;
}


/**
 * Constructor method for creating a new pyeclib object using the given parameters.
 *
 * @param k integer number of data elements
 * @param m integer number of checksum elements
 * @param w integer word size in bytes
 * @param type_str string name of erasure coding algorithm
 * @return pointer to PyObject or NULL on error
 */
static PyObject *
pyeclib_c_init(PyObject *self, PyObject *args)
{
  pyeclib_t *pyeclib_handle = NULL;
  PyObject *pyeclib_obj_handle = NULL;
  int k, m, w;
  int use_inline_chksum = 0, use_algsig_chksum = 0;
  const char *type_str;
  pyeclib_type_t type;

  /* Obtain and validate the method parameters */
  if (!PyArg_ParseTuple(args, "iis|ii", &k, &m, &type_str, &use_inline_chksum, &use_algsig_chksum)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.init");
    return NULL;
  }
  type = get_ecc_type(type_str);
  if (type == PYECC_NOT_FOUND) {
    PyErr_SetString(PyECLibError, "Invalid type passed to pyeclib.init");
    return NULL;
  }

  w = get_best_w_for_ecc_type(type);
  if (w < 0) {
    /* this should not happen */
    PyErr_SetString(PyECLibError, "Invalid w value. pyeclib internal error");
    return NULL;
  }

  if (!validate_args(k, m, w, type)) {
    PyErr_SetString(PyECLibError, "Invalid args passed to pyeclib.init");
    return NULL;
  }

  /* Allocate and initialize the pyeclib object */
  pyeclib_handle = (pyeclib_t *) alloc_zeroed_buffer(sizeof(pyeclib_t));
  if (NULL == pyeclib_handle) {
    goto error;
  }
  pyeclib_handle->k = k;
  pyeclib_handle->m = m;
  pyeclib_handle->w = w;
  pyeclib_handle->type = type;
  pyeclib_handle->inline_chksum = use_inline_chksum;
  pyeclib_handle->algsig_chksum = use_algsig_chksum;

  /* Allocate and initialize additional resources */
  switch (type) {
    case PYECC_RS_CAUCHY_ORIG:
      if (0 != init_rs_cauchy_orig(pyeclib_handle)) goto error;
      break;
    case PYECC_XOR_HD_3:
      pyeclib_handle->xor_code_desc = init_xor_hd_code(k, m, 3);
      if (pyeclib_handle->algsig_chksum) {
        pyeclib_handle->alg_sig_desc = alloc_alg_sig(32, 16);
        if (NULL == pyeclib_handle->alg_sig_desc) goto error;
      }
      break;
    case PYECC_XOR_HD_4:
      pyeclib_handle->xor_code_desc = init_xor_hd_code(k, m, 4);
      if (pyeclib_handle->algsig_chksum) {
        pyeclib_handle->alg_sig_desc = alloc_alg_sig(32, 16);
        if (NULL == pyeclib_handle->alg_sig_desc) goto error;
      }
      break;
    case PYECC_RS_VAND:
    default:
      pyeclib_handle->matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
      if (pyeclib_handle->algsig_chksum && w == 8) {
        pyeclib_handle->alg_sig_desc = alloc_alg_sig(32, 8);
        if (NULL == pyeclib_handle->alg_sig_desc) goto error;
      } else if (pyeclib_handle->algsig_chksum && w == 16) {
        pyeclib_handle->alg_sig_desc = alloc_alg_sig(32, 16);
        if (NULL == pyeclib_handle->alg_sig_desc) goto error;
      }
  }

  /* Prepare the python object to return */
#ifdef Py_CAPSULE_H
  pyeclib_obj_handle = PyCapsule_New(pyeclib_handle, PYECC_HANDLE_NAME,
                                     pyeclib_c_destructor);
#else
  pyeclib_obj_handle = PyCObject_FromVoidPtrAndDesc(pyeclib_handle,
                         (void *) PYECC_HANDLE_NAME,
             pyeclib_c_destructor);
#endif /* Py_CAPSULE_H */

  /* Clean up the allocated memory on error, or update the ref count */
  if (pyeclib_obj_handle == NULL) {
    PyErr_SetString(PyECLibError, "Could not encapsulate pyeclib_handle into Python object in pyeclib.init");
    goto error;
  } else {
    Py_INCREF(pyeclib_obj_handle);
  }
  
  goto exit;

error:
  if (NULL != pyeclib_handle) {
    check_and_free_alg_sig(pyeclib_handle->alg_sig_desc);
    check_and_free_rs_cauchy_orig(pyeclib_handle);
  }
  check_and_free_buffer(pyeclib_handle);
  pyeclib_obj_handle = NULL;

exit:  
  return pyeclib_obj_handle;
}


/**
 * Destructor method for cleaning up pyeclib object.
 */
static void 
pyeclib_c_destructor(PyObject *obj)
{
  pyeclib_t *pyeclib_handle = NULL;  /* pyeclib object to destroy */

  if (!PyCapsule_CheckExact(obj)) {
    PyErr_SetString(PyECLibError, "Attempted to free a non-Capsule object in pyeclib");
    return;
  }

  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(obj, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Attempted to free an invalid reference to pyeclib_handle");
  } else {
    check_and_free_alg_sig(pyeclib_handle->alg_sig_desc);
    check_and_free_rs_cauchy_orig(pyeclib_handle);
    check_and_free_buffer(pyeclib_handle);
  }
  
  return;
}


/**
 * This function takes data length and a segment size and returns an object 
 * containing:
 *
 * segment_size: size of the payload to give to encode()
 * last_segment_size: size of the payload to give to encode()
 * fragment_size: the fragment size returned by encode()
 * last_fragment_size: the fragment size returned by encode()
 * num_segments: number of segments 
 *
 * This allows the caller to prepare requests when segmenting a data stream 
 * to be EC'd.
 * 
 * Since the data length will rarely be aligned to the segment size, the last 
 * segment will be a different size than the others.  
 * 
 * There are restrictions on the length given to encode(), so calling this 
 * before encode is highly recommended when segmenting a data stream.
 *
 * Minimum segment size depends on the underlying EC type (if it is less 
 * than this, then the last segment will be slightly larger than the others, 
 * otherwise it will be smaller).
 *
 * @param pyeclib_obj_handle
 * @param data_len integer length of data in bytes
 * @param segment_size integer length of segment in bytes
 * @return a python dictionary with segment information
 * 
 */
static PyObject *
pyeclib_c_get_segment_info(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t *pyeclib_handle = NULL;
  PyObject *ret_dict = NULL;               /* python dictionary to return */
  int data_len;                            /* data length from user in bytes */
  int segment_size, last_segment_size;     /* segment sizes in bytes */
  int num_segments;                        /* total number of segments */
  int fragment_size, last_fragment_size;   /* fragment sizes in bytes */
  int min_segment_size;                    /* EC algorithm's min. size (B) */
  int aligned_segment_size;                /* size (B) adjusted for addr alignment */
  int aligned_data_len;                    /* size (B) adjusted for addr alignment */
  
  /* Obtain and validate the method parameters */
  if (!PyArg_ParseTuple(args, "Oii", &pyeclib_obj_handle, &data_len, &segment_size)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.encode");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.encode");
    return NULL;
  }

  /* The minimum segment size depends on the EC algorithm */
  min_segment_size = get_minimum_encode_size(pyeclib_handle);

  /* Get the number of segments */
  num_segments = (int)ceill((double)data_len / segment_size);

  /*
   * If there are two segments and the last is smaller than the 
   * minimum size, then combine into a single segment
   */
  if (num_segments == 2 && data_len < (segment_size + min_segment_size)) {
    num_segments--;
  }

  /* Compute the fragment size from the segment size */
  if (num_segments == 1) {
    /*
     * There is one fragment, or two fragments, where the second is 
     * smaller than the min_segment_size, just create one segment
     */

    /*
     * This will copmpute a size algined to the number of data
     * and the underlying wordsize of the EC algorithm.
     */
    aligned_data_len = get_aligned_data_size(pyeclib_handle, data_len);

    /* aligned_data_len is guaranteed to be divisible by k */
    fragment_size = aligned_data_len / pyeclib_handle->k;

    /* Segment size is the user-provided segment size */
    segment_size = data_len;
    last_fragment_size = fragment_size;
    last_segment_size = segment_size;
  } else {
    /*
     * There will be at least 2 segments, where the last exceeeds
     * the minimum segment size.
     */

    aligned_segment_size = get_aligned_data_size(pyeclib_handle, segment_size);

    /* aligned_data_len is guaranteed to be divisible by k */
    fragment_size = aligned_segment_size / pyeclib_handle->k;

    last_segment_size = data_len - (segment_size * (num_segments - 1)); 

    /*
     * The last segment is lower than the minimum size, so combine it 
     * with the previous fragment
     */
    if (last_segment_size < min_segment_size) {
      // assert(num_segments > 2)?

      /* Add current "last segment" to second to last segment */
      num_segments--;
      last_segment_size = last_segment_size + segment_size;
    } 
    
    aligned_segment_size = get_aligned_data_size(pyeclib_handle, last_segment_size);
     
    /* Compute the last fragment size from the last segment size */
    last_fragment_size = aligned_segment_size / pyeclib_handle->k;
  }
  
  /* Add header to fragment sizes */
  last_fragment_size += sizeof(fragment_header_t);
  fragment_size += sizeof(fragment_header_t);

  /* Create and return the python dictionary of segment info */
  ret_dict = PyDict_New();
  if (NULL == ret_dict) {
    PyErr_SetString(PyECLibError, "Error allocating python dict in get_segment_info");
  } else {
    PyDict_SetItem(ret_dict, PyString_FromString("segment_size\0"), PyInt_FromLong(segment_size));
    PyDict_SetItem(ret_dict, PyString_FromString("last_segment_size\0"), PyInt_FromLong(last_segment_size));
    PyDict_SetItem(ret_dict, PyString_FromString("fragment_size\0"), PyInt_FromLong(fragment_size));
    PyDict_SetItem(ret_dict, PyString_FromString("last_fragment_size\0"), PyInt_FromLong(last_fragment_size));
    PyDict_SetItem(ret_dict, PyString_FromString("num_segments\0"), PyInt_FromLong(num_segments));
  }
  
  return ret_dict;
}


/**
 * Erasure encode a data buffer.
 *
 * @param pyeclib_obj_handle
 * @param data to encode
 * @return python list of encoded data and parity elements
 */
static PyObject *
pyeclib_c_encode(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t *pyeclib_handle= NULL; 
  char **data_to_encode = NULL;     /* array of k data buffers */
  char **encoded_parity = NULL;     /* array of m parity buffers */
  PyObject *list_of_strips = NULL;  /* list of encoded strips to return */
  char *data;                       /* param, data buffer to encode */
  int data_len;                     /* param, length of data buffer */
  int aligned_data_len;             /* EC algorithm compatible data length */
  int orig_data_size;               /* data length to write to headers */
  int blocksize;                    /* length of each of k data elements */
  int i;                            /* a counter */

  /* Assume binary data (force "byte array" input) */
  if (!PyArg_ParseTuple(args, ENCODE_ARGS, &pyeclib_obj_handle, &data, &data_len)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.encode");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.encode");
    return NULL;
  }
  
  /* Calculate data sizes, aligned_data_len guaranteed to be divisible by k*/
  orig_data_size = data_len;
  aligned_data_len = get_aligned_data_size(pyeclib_handle, data_len);
  blocksize = aligned_data_len / pyeclib_handle->k;

  /* Allocate and initialize an array of zero'd out data buffers */
  data_to_encode = (char**) alloc_zeroed_buffer(sizeof(char*) * pyeclib_handle->k);
  if (NULL == data_to_encode) {
    goto error;
  }
  for (i = 0; i < pyeclib_handle->k; i++) {
    int payload_size = data_len > blocksize ? blocksize : data_len;
    char *fragment = alloc_fragment_buffer(blocksize);    
    if (NULL == fragment) {
      goto error;
    }
    
    /* Copy existing data into clean, zero'd out buffer */
    data_to_encode[i] = get_data_ptr_from_fragment(fragment);
    if (data_len > 0) {
      memcpy(data_to_encode[i], data, payload_size);
    }

    /* Fragment size will always be the same (may be able to get rid of this) */
    set_fragment_size(fragment, blocksize);

    data += payload_size;
    data_len -= payload_size;
  }

  /* Allocate and initialize an array of zero'd out parity buffers */
  encoded_parity = (char**) alloc_zeroed_buffer(sizeof(char*) * pyeclib_handle->m);
  if (NULL == encoded_parity) {
    goto error;
  }
  for (i = 0; i < pyeclib_handle->m; i++) {
    char *fragment = alloc_fragment_buffer(blocksize);
    if (NULL == fragment) {
      goto error;
    }
    
    encoded_parity[i] = get_data_ptr_from_fragment(fragment);
    set_fragment_size(fragment, blocksize);
  }

  /* Run the erasure coding algorithm to generate the parity fragments */
  switch (pyeclib_handle->type) {
    case PYECC_RS_CAUCHY_ORIG:
      jerasure_bitmatrix_encode(pyeclib_handle->k, pyeclib_handle->m, 
                                pyeclib_handle->w, pyeclib_handle->bitmatrix, 
                                data_to_encode, encoded_parity, blocksize, 
                                PYECC_CAUCHY_PACKETSIZE);
      break;
    case PYECC_XOR_HD_3:
    case PYECC_XOR_HD_4:
      pyeclib_handle->xor_code_desc->encode(pyeclib_handle->xor_code_desc, 
                                            data_to_encode, encoded_parity, 
                                            blocksize);
      break;
    case PYECC_RS_VAND:
    default:
      jerasure_matrix_encode(pyeclib_handle->k, pyeclib_handle->m, 
                             pyeclib_handle->w, pyeclib_handle->matrix, 
                             data_to_encode, encoded_parity, blocksize);
      break;
  }

  /* Create the python list of fragments to return */
  list_of_strips = PyList_New(pyeclib_handle->k + pyeclib_handle->m);
  if (NULL == list_of_strips) {
    PyErr_SetString(PyECLibError, "Error allocating python list in encode");
    goto error;
  }
  
  /* Finalize data fragments and add them to the python list to return */
  for (i = 0; i < pyeclib_handle->k; i++) {
    char *fragment_ptr = get_fragment_ptr_from_data(data_to_encode[i]);
    int fragment_size = blocksize + sizeof(fragment_header_t);
    set_fragment_idx(fragment_ptr, i);
    set_orig_data_size(fragment_ptr, orig_data_size);
    if (use_inline_chksum(pyeclib_handle)) {
      int chksum = crc32(0, data_to_encode[i], blocksize);
      set_chksum(fragment_ptr, chksum);
    }
    PyList_SET_ITEM(list_of_strips, i, 
                    PY_BUILDVALUE_OBJ_LEN(fragment_ptr, fragment_size));
  }
  
  /* Finalize parity fragments and add them to the python list to return */
  for (i = 0; i < pyeclib_handle->m; i++) {
    char *fragment_ptr = get_fragment_ptr_from_data(encoded_parity[i]);
    int fragment_size = blocksize + sizeof(fragment_header_t);
    set_fragment_idx(fragment_ptr, pyeclib_handle->k+i);
    set_orig_data_size(fragment_ptr, orig_data_size);
    if (use_inline_chksum(pyeclib_handle)) {
      int chksum = crc32(0, encoded_parity[i], blocksize);
      set_chksum(fragment_ptr, chksum);
    }
    PyList_SET_ITEM(list_of_strips, pyeclib_handle->k + i, 
                    PY_BUILDVALUE_OBJ_LEN(fragment_ptr, fragment_size));
  }
  
  goto exit;

error:
  list_of_strips = NULL;
  
exit:
  if (data_to_encode) {
    for (i = 0; i < pyeclib_handle->k; i++) {
      if (data_to_encode[i]) free_fragment_buffer(data_to_encode[i]);
    }
    check_and_free_buffer(data_to_encode);
  }
  if (encoded_parity) {
    for (i = 0; i < pyeclib_handle->m; i++) {
      if (encoded_parity[i]) free_fragment_buffer(encoded_parity[i]);
    }
    check_and_free_buffer(encoded_parity);
  }
  
  return list_of_strips;
}


/*
 * Convert a set of fragments into a string.  If, less than k data fragments 
 * are present, return None.  Return NULL on error.
 *
 * @param pyeclib_obj_handle
 * @param list of fragments
 * @param python string or None, NULL on error
 */
static PyObject *
pyeclib_c_fragments_to_string(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t *pyeclib_handle = NULL;
  PyObject *fragment_list = NULL;  /* param, python list of fragments */
  PyObject *ret_string = NULL;     /* python string to return */
  char *ret_cstring = NULL;        /* c string to build return string from */
  int ret_data_size;               /* size of return string in bytes */
  char **data = NULL;              /* array of data buffers */
  int string_off = 0;              /* offset into cstring */
  int num_fragments = 0;           /* num of fragments provided by caller */
  int num_data = 0;                /* num of fragments that are data */
  int orig_data_size = -1;         /* data size from fragment header */
  int i = 0;                       /* a counter */

  /* Collect and validate the method arguments */
  if (!PyArg_ParseTuple(args, "OO", &pyeclib_obj_handle, &fragment_list)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.fragments_to_string");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.fragments_to_string");
    return NULL;
  }
  if (!PyList_Check(fragment_list)) {
    PyErr_SetString(PyECLibError, "Invalid structure passed in for fragment list in pyeclib.fragments_to_string");
    return NULL;
  }
  
  /* Return None if there's insufficient fragments */
  num_fragments = (int) PyList_Size(fragment_list);
  if (pyeclib_handle->k > num_fragments) {
    return Py_BuildValue("");
  }
    
  /*
   * NOTE: Update to only copy original size out of the buffers
   */

  /*
   * Iterate over the fragments.  If we have all k data fragments, then we can
   * concatenate them into a string and return it; otherwise, we return NULL
   */
  data = (char **) alloc_zeroed_buffer(sizeof(char *) * pyeclib_handle->k);
  if (NULL == data) {
    return NULL;
  }
  for (i = 0; i < num_fragments && num_data < pyeclib_handle->k; i++) {
    PyObject *tmp_data = PyList_GetItem(fragment_list, i);
    char *tmp_buf;
    int index;
    int data_size;
    Py_ssize_t len;

    /* Get and validate the fragment index and size */
    PyBytes_AsStringAndSize(tmp_data, &tmp_buf, &len);
    index = get_fragment_idx(tmp_buf);
    data_size = get_fragment_size(tmp_buf);
    if ((index < 0) || (data_size < 0)) {
      ret_string = NULL;
      goto exit;
    }

    /* Validate the original data size */
    if (orig_data_size < 0) {
      orig_data_size = get_orig_data_size(tmp_buf);
    } else {
      if (get_orig_data_size(tmp_buf) != orig_data_size) {
        PyErr_SetString(PyECLibError, "Inconsistent orig data sizes found in headers");
        ret_string = NULL;
        goto exit;
      }
    }
    
    /* Skip parity fragments, put data fragments in index order */
    if (index >= pyeclib_handle->k) {
      continue;
    } else {
      data[index] = tmp_buf;
      num_data++;
    }
  }

  /* Return None if there still isn't insufficient fragments */
  if (num_data != pyeclib_handle->k) {
    ret_string = Py_BuildValue("");
    goto exit;
  }
  
  /* Create the c-string to return */
  ret_cstring = (char *) alloc_zeroed_buffer(orig_data_size);
  if (NULL == ret_cstring) {
    ret_string = NULL;
    goto exit;
  }
  ret_data_size = orig_data_size;

  /* Copy fragment data into cstring (fragments should be in index order) */
  for (i = 0; i < num_data && orig_data_size > 0; i++) {
    char* fragment_data = get_data_ptr_from_fragment(data[i]);
    int fragment_size = get_fragment_size(data[i]);
    int payload_size = orig_data_size > fragment_size ? fragment_size : orig_data_size;

    memcpy(ret_cstring + string_off, fragment_data, payload_size);
    orig_data_size -= payload_size;
    string_off += payload_size;
  }
  ret_string = PY_BUILDVALUE_OBJ_LEN(ret_cstring, ret_data_size);

exit:
  check_and_free_buffer(data);
  check_and_free_buffer(ret_cstring);

  return ret_string;
}


/*
 * Return a tuple containing a reference to a data fragment list, a parity 
 * fragment list and a missing fragment index list, including all '0' 
 * fragment for missing fragments.  Copy references to the existing 
 * fragments and create new string for missing fragments.
 *
 * @param pyeclib_obj_handle
 * @param list of fragments
 * @return (data fragments, parity fragments, missing indexes) tuple
 */
static PyObject *
pyeclib_c_get_fragment_partition(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t* pyeclib_handle = NULL;
  PyObject *fragment_list = NULL;     /* param, list of fragments */
  PyObject *data_list = NULL;         /* data fragments for return tuple */
  PyObject *parity_list = NULL;       /* parity fragments for return tuple */
  PyObject *fragment_string = NULL;   /* fragment string for lists */
  PyObject *missing_list = NULL;      /* missing indexes for return tuple */
  PyObject *return_lists = NULL;      /* Python tuple to return */
  PyObject **data = NULL;             /* array of k data fragment buffers*/
  PyObject **parity = NULL;           /* array of m parity fragment buffers */
  int *missing = NULL;                /* indicies of missing fragments */
  int num_missing = 0;                /* num of missing fragments */
  int num_fragments;                  /* num of frags provided by caller */
  int fragment_size = 0;              /* size in bytes of fragments */
  int i = 0;                          /* a counter */

  /* Collect and validate the method arguments */
  if (!PyArg_ParseTuple(args, "OO", &pyeclib_obj_handle, &fragment_list)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.get_fragment_partition");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.get_fragment_partition");
    return NULL;
  }
  if (!PyList_Check(fragment_list)) {
    PyErr_SetString(PyECLibError, "Invalid structure passed in for fragment list in pyeclib.get_fragment_partition");
    return NULL;
  }
  
  /* Create the arrays need to hold the data and parity fragments */
  data = (PyObject **) alloc_zeroed_buffer(pyeclib_handle->k * sizeof(PyObject*));
  if (NULL == data) {
    goto error;
  }
  parity = (PyObject **) alloc_zeroed_buffer(pyeclib_handle->m * sizeof(PyObject*));
  if (NULL == parity) {
    goto error;
  }
      
  /* Fill in the data and parity pointers to reveal missing fragments */
  num_fragments = (int) PyList_Size(fragment_list);
  for (i = 0; i < num_fragments; i++) {
    PyObject *tmp_data = PyList_GetItem(fragment_list, i);
    char *c_buf;
    int index;
    Py_ssize_t len;
    
    /* ASSUMPTION: The fragment_size is the max of the fragments we read */
    PyBytes_AsStringAndSize(tmp_data, &c_buf, &len);
    if (len > fragment_size) {
      fragment_size = (int) len;
    }
    
    /* Get fragment index and set the index in data or parity */
    index = get_fragment_idx(c_buf);
    if (index < pyeclib_handle->k) {
      data[index] = tmp_data;
    } else {
      parity[index - pyeclib_handle->k] = tmp_data;
    }
  }
  
  /* If there are missing bufs, figure out which indexes are missing
   *
   * ASSUMPTION: We will never try to do anything when we have more 
   * than k missing fragments
   */
  missing = (int *) alloc_zeroed_buffer(pyeclib_handle->k * sizeof(int));
  if (NULL == missing) {
    goto error;
  } else {
    num_missing = 0;
    for (i = 0; i < pyeclib_handle->k; i++) {
      if (data[i] == NULL) {
        missing[num_missing] = i;
        num_missing++;
      }
    }
    for (i = 0; i < pyeclib_handle->m; i++) {
      if (parity[i] == NULL) {
        missing[num_missing] = i + pyeclib_handle->k;
        num_missing++;
      }
    }
  }
  
  /* Create the python objects to return */
  data_list = PyList_New(pyeclib_handle->k);
  parity_list = PyList_New(pyeclib_handle->m);
  missing_list = PyList_New(num_missing);
  return_lists = PyTuple_New(3);
  if (!data_list || !parity_list || !missing_list || !return_lists) {
    return_lists = PyErr_NoMemory();
    goto exit;
  }
  
  /* Fill in the data fragments, create zero fragment if missing */
  for (i = 0; i < pyeclib_handle->k; i++) {
    if (data[i] != NULL) {
      /* BORROWED REF: increment ref count before inserting into list */
      Py_INCREF(data[i]);
      fragment_string = data[i];
    } else {
      fragment_string = alloc_zero_string(fragment_size);
      if (NULL == fragment_string) {
        goto error;
      }
    }
    PyList_SET_ITEM(data_list, i, fragment_string);
  }

  /* Fill in the parity fragments, create zero fragment if missing */
  for (i = 0; i < pyeclib_handle->m; i++) {
    if (parity[i] != NULL) {
      /* BORROWED REF: increment ref count before inserting into list */
      Py_INCREF(parity[i]);
      fragment_string = parity[i];
    } else {
      fragment_string = alloc_zero_string(fragment_size);
      if (NULL == fragment_string) {
        goto error;
      }
    }
    PyList_SET_ITEM(parity_list, i, fragment_string);
  }

  /* Fill in the list of missing indexes */
  for (i = 0;i < num_missing; i++) {
    PyList_SET_ITEM(missing_list, i, Py_BuildValue("i", missing[i]));
  }
  
  Py_INCREF(data_list);
  Py_INCREF(parity_list);
  Py_INCREF(missing_list);
  
  PyTuple_SetItem(return_lists, 0, data_list);
  PyTuple_SetItem(return_lists, 1, parity_list);
  PyTuple_SetItem(return_lists, 2, missing_list);
  //Py_INCREF(return_lists);

  goto exit;

error:
  return_lists = NULL;

exit:
  check_and_free_buffer(data);
  check_and_free_buffer(parity);
  check_and_free_buffer(missing);
  
  return return_lists;
}


/**
 * Return a list of lists with valid rebuild indexes given an EC algorithm
 * and a list of missing indexes.
 *
 * @param pyeclib_obj_handle
 * @param missing_list indexes of missing fragments
 * @return a list of lists of indexes to rebuild data from
 */
static PyObject *
pyeclib_c_get_required_fragments(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t *pyeclib_handle = NULL;
  PyObject *missing_list = NULL;        /* param, */
  PyObject *fragment_idx_list = NULL;   /* list of req'd indexes to return */
  int *c_missing_list = NULL;           /* c-array of missing indexes */
  int num_missing;                      /* size of passed in missing list */
  int i = 0, j = 0;                     /* counters */
  int k, m;                             /* EC algorithm parameters */
  unsigned long missing_bm = 0;         /* bitmap of missing indexes */
  int *fragments_needed = NULL;         /* indexes of xor code fragments */
  int ret;                              /* return value for xor code */

  /* Obtain and validate the method parameters */
  if (!PyArg_ParseTuple(args, "OO", &pyeclib_obj_handle, &missing_list)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.get_required_fragments");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.get_required_fragments");
    return NULL;
  }
  k = pyeclib_handle->k;
  m = pyeclib_handle->m;

  /* Generate -1 terminated c-array and bitmap of missing indexes */
  num_missing = (int) PyList_Size(missing_list);
  c_missing_list = (int *) alloc_zeroed_buffer((num_missing + 1) * sizeof(int));
  if (NULL == c_missing_list) {
    return NULL;
  }
  c_missing_list[num_missing] = -1;
  for (i = 0; i < num_missing; i++) {
    PyObject *obj_idx = PyList_GetItem(missing_list, i);
    long idx = PyLong_AsLong(obj_idx);
    c_missing_list[i] = (int) idx;
  }
  missing_bm = convert_list_to_bitmap(c_missing_list);
   
  /* Generate the python list of lists of rebuild indexes to return */   
  fragment_idx_list = PyList_New(0);
  if (NULL == fragment_idx_list) {
    goto exit;
  }
  j = 0;
  switch(pyeclib_handle->type) {
    case PYECC_RS_CAUCHY_ORIG:
    case PYECC_RS_VAND:
      for (i = 0; i < (k + m); i++) {
        if (!(missing_bm & (1 << i))) {
          PyList_Append(fragment_idx_list, Py_BuildValue("i", i));
          j++;
        }
        if (j == k) {
          break;
        }
      }

      if (j != k) {
        /* Let the garbage collector clean this up */
        Py_DECREF(fragment_idx_list);
        PyErr_Format(PyECLibError, "Not enough fragments for pyeclib.get_required_fragments (need at least %d, %d are given)", k, j);
        fragment_idx_list = NULL;
      }
      break;
    case PYECC_XOR_HD_3:
    case PYECC_XOR_HD_4:
    {
      fragments_needed = alloc_zeroed_buffer(sizeof(int) * (k + m));
      if (NULL == fragments_needed) {
        fragment_idx_list = NULL;
        goto exit;
      }
      ret = pyeclib_handle->xor_code_desc->fragments_needed(pyeclib_handle->xor_code_desc,
                                                            c_missing_list, 
                                                            fragments_needed);

      if (ret < 0) {
        Py_DECREF(fragment_idx_list);
        PyErr_Format(PyECLibError, "Not enough fragments for pyeclib.get_required_fragments!");
        fragment_idx_list = NULL;
        break;
      }

      while (fragments_needed[j] > -1) {
        PyList_Append(fragment_idx_list, Py_BuildValue("i", fragments_needed[j]));
        j++;
      }
      break;
    }
    default:
      PyErr_SetString(PyECLibError, "Invalid EC type used in pyeclib.get_required_fragments");
      break;
  }

exit:
  check_and_free_buffer(c_missing_list);
  check_and_free_buffer(fragments_needed);

  return fragment_idx_list;
}


/**
 * Reconstruct a missing fragment from the the remaining fragments.
 *
 * TODO: If we are reconstructing a parity element, ensure that all of the 
 * data elements are available!
 *
 * @param pyeclib_obj_handle
 * @param data_list k length list of data elements
 * @param parity_list m length list of parity elements
 * @param missing_idx_list list of the indexes of missing elements
 * @param destination_idx index of fragment to reconstruct
 * @param fragment_size size in bytes of the fragments
 * @return reconstructed destination fragment or NULL on error
 */
static PyObject *
pyeclib_c_reconstruct(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t *pyeclib_handle = NULL;
  PyObject *data_list = NULL;           /* param, list of data fragments */
  PyObject *parity_list = NULL;         /* param, list of parity fragments */
  PyObject *missing_idx_list = NULL;    /* param, list of missing indexes */
  PyObject *reconstructed = NULL;       /* reconstructed object to return */
  int *erased = NULL;                   /* jerasure notation of erased devs */
  int fragment_size;                    /* param, size in bytes of fragment */
  int blocksize;                        /* size in bytes, fragment - header */
  char **data = NULL;                   /* k length array of data buffers */
  char **parity = NULL;                 /* m length array of parity buffers */
  int *missing_idxs = NULL;             /* array of missing indexes */
  int destination_idx;                  /* param, index to reconstruct */
  unsigned long long realloc_bm = 0;    /* bitmap, which fragments were realloc'ed */
  int orig_data_size = -1;              /* data size (B),from fragment hdr */
  int missing_size;                     /* number of missing indexes */
  int *decoding_matrix = NULL;          /* reconstruct specific decode matrix */
  int *decoding_row = NULL;             /* required row from decode matrix */
  int *dm_ids = NULL;                   /* k length array of surviving indexes */
  int k, m, w;                          /* EC algorithm parameters */
  int ret;                              /* decode matrix creation return val */
  int i = 0;                            /* a counter */

  /* Obtain and validate the method parameters */
  if (!PyArg_ParseTuple(args, "OOOOii", &pyeclib_obj_handle, &data_list, 
                        &parity_list, &missing_idx_list, &destination_idx, 
                        &fragment_size)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.encode");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.encode");
    return NULL;
  }
  k = pyeclib_handle->k;
  m = pyeclib_handle->m;
  w = pyeclib_handle->w;
  if (!PyList_Check(data_list) || !PyList_Check(parity_list) || !PyList_Check(missing_idx_list)) {
    PyErr_SetString(PyECLibError, "Invalid structure passed in for data, parity and/or missing_idx list");
    return NULL;
  }
  if (k != PyList_Size(data_list)) {
    PyErr_SetString(PyECLibError, "The data list does not have the correct number of entries");
    return NULL;
  }
  if (m != PyList_Size(parity_list)) {
    PyErr_SetString(PyECLibError, "The parity list does not have the correct number of entries");
    return NULL;
  }

  /* Allocate data structures needed for reconstruction */
  blocksize = FRAGSIZE_2_BLOCKSIZE(fragment_size);
  missing_size = (int) PyList_Size(missing_idx_list);
  missing_idxs = (int *) alloc_zeroed_buffer(sizeof(int) * (missing_size + 1));
  data = (char **) alloc_zeroed_buffer(sizeof(char *) * k);
  parity = (char **) alloc_zeroed_buffer(sizeof(char *) * m);
  if (NULL == missing_idxs || NULL == data || NULL == parity) {
    goto error;
  }
  
  /* Prepare for decoding, no need to go further on error */
  if (get_decoding_info(pyeclib_handle, data_list, parity_list, 
                        missing_idx_list, data, parity, missing_idxs, 
                        &orig_data_size, fragment_size, &realloc_bm)) {
    PyErr_SetString(PyECLibError, "Could not extract adequate decoding info from data, parity and missing lists");
    goto error;
  }

  /* Create the decoding matrix, and attempt reconstruction */
  erased = jerasure_erasures_to_erased(k, m, missing_idxs);
  switch (pyeclib_handle->type) {
    case PYECC_RS_CAUCHY_ORIG:
      if (destination_idx < k) {
        decoding_matrix = (int *) alloc_zeroed_buffer(sizeof(int*) * k * k * w * w);
        dm_ids = (int *) alloc_zeroed_buffer(sizeof(int) * k);
        if (NULL == decoding_matrix || NULL == dm_ids) {
          goto error;
        }
        ret = jerasure_make_decoding_bitmatrix(k, m, w, pyeclib_handle->bitmatrix, 
                                               erased, decoding_matrix, dm_ids);
        decoding_row = decoding_matrix + (destination_idx * k * w * w);
      } else {
        ret = 0;
        decoding_row = pyeclib_handle->bitmatrix + ((destination_idx - k) * k * w * w);    
      }
      
      if (ret == 0) {
        jerasure_bitmatrix_dotprod(k, w, decoding_row, dm_ids, destination_idx, 
                                   data, parity, blocksize, 
                                   PYECC_CAUCHY_PACKETSIZE);
      }
      break;
    case PYECC_RS_VAND:
      if (destination_idx < k) {
        decoding_matrix = (int *) alloc_zeroed_buffer(sizeof(int*) * k * k);
        dm_ids = (int *) alloc_zeroed_buffer(sizeof(int) * k);
        if (NULL == decoding_matrix || NULL == dm_ids) {
          goto error;
        }
        ret = jerasure_make_decoding_matrix(k, m, w, pyeclib_handle->matrix, 
                                            erased, decoding_matrix, dm_ids);
        decoding_row = decoding_matrix + (destination_idx * k);
      } else {
        ret = 0;
        decoding_row = pyeclib_handle->matrix + ((destination_idx - k) * k);
      }
      
      if (ret == 0) {
        jerasure_matrix_dotprod(k, w, decoding_row, dm_ids, destination_idx, 
                                data, parity, blocksize);
      }
      break;
    case PYECC_XOR_HD_3:
    case PYECC_XOR_HD_4:
      ret = 0;
      xor_reconstruct_one(pyeclib_handle->xor_code_desc, data, parity, 
                          missing_idxs, destination_idx, blocksize);
      break;
    default:
      ret = -1;
      break;
  }

  /* Set the metadata on the reconstructed fragment */
  if (ret == 0) {
    char *fragment_ptr = NULL;
    if (destination_idx < k) {
      fragment_ptr = get_fragment_ptr_from_data_novalidate(data[destination_idx]);
      init_fragment_header(fragment_ptr);
      set_fragment_idx(fragment_ptr, destination_idx);
      set_orig_data_size(fragment_ptr, orig_data_size);
      set_fragment_size(fragment_ptr, blocksize);
      if (use_inline_chksum(pyeclib_handle)) {
        int chksum = crc32(0, data[destination_idx], blocksize);
        set_chksum(fragment_ptr, chksum);
      }
    } else {
      fragment_ptr = get_fragment_ptr_from_data_novalidate(parity[destination_idx - k]);
      init_fragment_header(fragment_ptr);
      set_fragment_idx(fragment_ptr, destination_idx);
      set_orig_data_size(fragment_ptr, orig_data_size);
      set_fragment_size(fragment_ptr, blocksize);
      if (use_inline_chksum(pyeclib_handle)) {
        int chksum = crc32(0, parity[destination_idx - k], blocksize);
        set_chksum(fragment_ptr, chksum);
      }
    }

    reconstructed = PY_BUILDVALUE_OBJ_LEN(fragment_ptr, fragment_size);
  } else {
    reconstructed = NULL;
  }
  
  goto out;

error:
  reconstructed = NULL;
  
out:
  /* Free fragment buffers that needed to be reallocated for alignment */
  for (i = 0; i < k; i++) {
    if (realloc_bm & (1 << i)) {
      free(get_fragment_ptr_from_data_novalidate(data[i]));
    }
  }
  for (i = 0; i < m; i++) {
    if (realloc_bm & (1 << (i + k))) {
      free(get_fragment_ptr_from_data_novalidate(parity[i]));
    }
  }

  check_and_free_buffer(missing_idxs);
  check_and_free_buffer(data);
  check_and_free_buffer(parity);
  check_and_free_buffer(decoding_matrix);
  check_and_free_buffer(dm_ids);

  return reconstructed;
}


/**
 * Reconstruct all of the missing fragments from a set of fragments.
 *
 * TODO: There's a lot of duplicated code between this and the 
 * reconstruct method.  Consider refactoring these methods.
 *
 * @param pyeclib_obj_handle
 * @param data_list k length list of data elements
 * @param parity_list m length list of parity elements
 * @param missing_idx_list list of the indexes of missing elements
 * @param fragment_size size in bytes of the fragments
 * @return list of fragments
 */
static PyObject *
pyeclib_c_decode(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t *pyeclib_handle = NULL;
  PyObject *list_of_strips = NULL;    /* list of strips to return */
  PyObject *data_list = NULL;         /* param, list of data fragments */
  PyObject *parity_list = NULL;       /* param, list of data fragments */
  PyObject *missing_idx_list = NULL;  /* param, list of missing indexes */
  int fragment_size;                  /* param, size in bytes of fragment */
  int blocksize;                      /* size in bytes, fragment - header */
  unsigned long long realloc_bm = 0;  /* bitmap, which fragments were realloc'ed */
  char **data = NULL;                 /* k length array of data buffers */
  char **parity = NULL;               /* m length array of parity buffers */
  int *missing_idxs = NULL;           /* array of missing indexes */
  int missing_size;                   /* number of missing indexes */
  int orig_data_size = -1;            /* data size in bytes ,from fragment hdr */
  int k, m, w;                        /* EC algorithm parameters */
  int i = 0, j = 0;                   /* counters */

  /* Obtain and validate the method parameters */
  if (!PyArg_ParseTuple(args, "OOOOi", &pyeclib_obj_handle, &data_list, &parity_list, &missing_idx_list, &fragment_size)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.encode");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.encode");
    return NULL;
  }
  k = pyeclib_handle->k;
  m = pyeclib_handle->m;
  w = pyeclib_handle->w;
  if (!PyList_Check(data_list) || !PyList_Check(parity_list) || !PyList_Check(missing_idx_list)) {
    PyErr_SetString(PyECLibError, "Invalid structure passed in for data, parity and/or missing_idx list");
    return NULL;
  }
  if (k != PyList_Size(data_list)) {
    PyErr_SetString(PyECLibError, "The data list does not have the correct number of entries");
    return NULL;
  }
  if (m != PyList_Size(parity_list)) {
    PyErr_SetString(PyECLibError, "The parity list does not have the correct number of entries");
    return NULL;
  }

  /* Allocate data structures needed for reconstruction */
  blocksize = FRAGSIZE_2_BLOCKSIZE(fragment_size);
  missing_size = (int) PyList_Size(missing_idx_list);
  missing_idxs = (int *) alloc_zeroed_buffer(sizeof(int) * (missing_size + 1));
  data = (char **) alloc_zeroed_buffer(sizeof(char *) * k);
  parity = (char **) alloc_zeroed_buffer(sizeof(char *) * m);
  if (NULL == missing_idxs || NULL == data || NULL == parity) {
    goto error;
  }

  /* Prepare for decoding, no need to go further on error */
  if (get_decoding_info(pyeclib_handle, data_list, parity_list, missing_idx_list, 
                        data, parity, missing_idxs, &orig_data_size, 
                        fragment_size, &realloc_bm)) {
    PyErr_SetString(PyECLibError, "Could not extract adequate decoding info from data, parity and missing lists");
    return NULL;
  }

  /* Reconstruct the missing fragments */
  switch (pyeclib_handle->type) {
    case PYECC_RS_CAUCHY_ORIG:
      jerasure_bitmatrix_decode(k, m, w, pyeclib_handle->bitmatrix, 0, missing_idxs, 
                                data, parity, blocksize, PYECC_CAUCHY_PACKETSIZE);
      break;
    case PYECC_XOR_HD_3:
    case PYECC_XOR_HD_4:
      pyeclib_handle->xor_code_desc->decode(pyeclib_handle->xor_code_desc, data, 
                                            parity, missing_idxs, blocksize, 1);
      break;
    case PYECC_RS_VAND:
    default:
      jerasure_matrix_decode(k, m, w, pyeclib_handle->matrix, 1, missing_idxs, 
                            data, parity, blocksize);
      break;
  }

  /* Create the python list to return */
  list_of_strips = PyList_New(k + m);
  if (NULL == list_of_strips) {
    goto error;
  }
  
  /* Create headers for the newly decoded elements */
  while (missing_idxs[j] >= 0) {
    int missing_idx = missing_idxs[j];
    if (missing_idx < k) {
      char *fragment_ptr = get_fragment_ptr_from_data_novalidate(data[missing_idx]);
      init_fragment_header(fragment_ptr);
      set_fragment_idx(fragment_ptr, missing_idx);
      set_orig_data_size(fragment_ptr, orig_data_size);
      set_fragment_size(fragment_ptr, blocksize);
      if (use_inline_chksum(pyeclib_handle)) {
        int chksum = crc32(0, data[missing_idx], blocksize);
        set_chksum(fragment_ptr, chksum);
      }
    } else if (missing_idx >= k) {
      int parity_idx = missing_idx - k;
      char *fragment_ptr = get_fragment_ptr_from_data_novalidate(parity[parity_idx]);
      init_fragment_header(fragment_ptr);
      set_fragment_idx(fragment_ptr, missing_idx);
      set_orig_data_size(fragment_ptr, orig_data_size);
      set_fragment_size(fragment_ptr, blocksize);
      if (use_inline_chksum(pyeclib_handle)) {
        int chksum = crc32(0, parity[parity_idx], blocksize);
        set_chksum(fragment_ptr, chksum);
      }
    } 
    j++;
  }

  /* Fill in the data fragments */
  for (i = 0; i < k; i++) {
    char *fragment_ptr = get_fragment_ptr_from_data(data[i]);
    PyList_SET_ITEM(list_of_strips, i, PY_BUILDVALUE_OBJ_LEN(fragment_ptr, fragment_size));
  }
  /* Fill in the parity fragments */
  for (i = 0; i < m; i++) {
    char *fragment_ptr = get_fragment_ptr_from_data(parity[i]);
    PyList_SET_ITEM(list_of_strips, k + i, PY_BUILDVALUE_OBJ_LEN(fragment_ptr, fragment_size));
  }
  
  goto exit;

error:
  list_of_strips = NULL;

exit:
  /* Free fragment buffers that needed to be reallocated for alignment */
  for (i = 0; i < k; i++) {
    if (realloc_bm & (1 << i)) {
      free(get_fragment_ptr_from_data_novalidate(data[i]));
    }
  }
  for (i = 0; i < m; i++) {
    if (realloc_bm & (1 << (i + k))) {
      free(get_fragment_ptr_from_data_novalidate(parity[i]));
    }
  }
  
  check_and_free_buffer(missing_idxs);
  check_and_free_buffer(data);
  check_and_free_buffer(parity);

  return list_of_strips;
}


/**
 * Obtain the metadata from a fragment.
 * 
 * @param pyeclib_obj_handle
 * @param data fragment from user to extract metadata from
 * @param data_len size in bytes of the data fragment
 * @return fragment metadata or NULL on error
 */
static PyObject *
pyeclib_c_get_metadata(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t* pyeclib_handle = NULL;
  char *data = NULL;                              /* param, fragment from caller */
  int data_len;                                   /* param, data len (B) */
  int metadata_len;                               /* metadata header size (B) */
  fragment_metadata_t *fragment_metadata = NULL;  /* buffer to hold metadata */
  PyObject *ret_fragment_metadata = NULL;         /* metadata object to return */

  /* Obtain and validate the method parameters */
  if (!PyArg_ParseTuple(args, GET_METADATA_ARGS, &pyeclib_obj_handle, &data, &data_len)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.get_metadata");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.get_required_fragments");
    return NULL;
  }

  /* Obtain the metadata from the data and build a object to return */
  metadata_len = sizeof(fragment_metadata_t);
  fragment_metadata = (fragment_metadata_t *) alloc_zeroed_buffer(metadata_len);
  if (NULL == fragment_metadata) {
    ret_fragment_metadata = NULL;
  } else {
    get_fragment_metadata(pyeclib_handle, data, fragment_metadata);
    ret_fragment_metadata = PY_BUILDVALUE_OBJ_LEN((char*)fragment_metadata, 
                                                   metadata_len);                                                  
    check_and_free_buffer(fragment_metadata);
  }

  return ret_fragment_metadata;
}


/**
 * Confirm the health of the fragment metadata.
 *
 * TODO: Return a list containing tuples (index, problem).  An empty list means
 * everything is OK.
 * 
 * @param pyeclib_obj_handle
 * @param fragment_metadata_list list of fragment metadata headers
 * @return -1 if no errors, or the index of the first problem checksum
 */
static PyObject*
pyeclib_c_check_metadata(PyObject *self, PyObject *args)
{
  PyObject *pyeclib_obj_handle = NULL;
  pyeclib_t *pyeclib_handle = NULL;
  PyObject *fragment_metadata_list = NULL;                /* param, fragment metadata */
  fragment_metadata_t **c_fragment_metadata_list = NULL;  /* c version of metadata */
  int num_fragments;                                      /* k + m from EC algorithm */
  char **c_fragment_signatures = NULL;                    /* array of alg. signatures */
  int k, m, w;                                            /* EC algorithm params */
  int size;                                               /* size for buf allocation */
  int ret = -1;                                           /* c return value */
  PyObject *ret_obj = NULL;                               /* python long to return */
  int i = 0;                                              /* a counter */

  /* Obtain and validate the method parameters */
  if (!PyArg_ParseTuple(args, "OO", &pyeclib_obj_handle, &fragment_metadata_list)) {
    PyErr_SetString(PyECLibError, "Invalid arguments passed to pyeclib.encode");
    return NULL;
  }
  pyeclib_handle = (pyeclib_t*)PyCapsule_GetPointer(pyeclib_obj_handle, PYECC_HANDLE_NAME);
  if (pyeclib_handle == NULL) {
    PyErr_SetString(PyECLibError, "Invalid handle passed to pyeclib.encode");
    return NULL;
  }
  k = pyeclib_handle->k;
  m = pyeclib_handle->m;
  w = pyeclib_handle->w;
  num_fragments = k + m;
  if (num_fragments != PyList_Size(fragment_metadata_list)) {
    PyErr_SetString(PyECLibError, "Not enough fragment metadata to perform integrity check");
    return NULL;
  }
  
  /* Allocate space for fragment signatures */
  size = sizeof(fragment_metadata_t * ) * num_fragments;
  c_fragment_metadata_list = (fragment_metadata_t**) alloc_zeroed_buffer(size);
  size = sizeof(char *) * num_fragments;
  c_fragment_signatures = (char **) alloc_zeroed_buffer(size);
  if (NULL == c_fragment_metadata_list || NULL == c_fragment_signatures) {
    goto error;
  }

  /* Populate and order the metadata */
  for (i = 0; i < num_fragments; i++) {
    PyObject *tmp_data = PyList_GetItem(fragment_metadata_list, i);
    Py_ssize_t len = 0;
    char *c_buf = NULL;
    fragment_metadata_t *fragment_metadata;
    PyBytes_AsStringAndSize(tmp_data, &(c_buf), &len);

    fragment_metadata = (fragment_metadata_t*)c_buf;
    c_fragment_metadata_list[fragment_metadata->idx] = fragment_metadata;

    if (supports_alg_sig(pyeclib_handle)) {
      c_fragment_signatures[fragment_metadata->idx] = (char*)alloc_aligned_buffer16(PYCC_MAX_SIG_LEN);
      memcpy(c_fragment_signatures[fragment_metadata->idx], fragment_metadata->signature, PYCC_MAX_SIG_LEN);
    } else {
      c_fragment_signatures[fragment_metadata->idx] = fragment_metadata->signature;
    }
  }

  /* Ensure all fragments are here and check integrity using alg signatures */
  if (supports_alg_sig(pyeclib_handle)) {
    char **parity_sigs = (char **) alloc_zeroed_buffer(sizeof(char **) * m);
    if (NULL == parity_sigs) {
      goto error;
    }
    for (i = 0; i < m; i++) {
      parity_sigs[i] = (char *) alloc_aligned_buffer16(PYCC_MAX_SIG_LEN);
      if (NULL == parity_sigs[i]) {
        int j;
        for (j = 0; j < i; j++) free(parity_sigs[j]);
        goto error;
      } else {
        memset(parity_sigs[i], 0, PYCC_MAX_SIG_LEN);
      }
    }

    /* Calculate the parity of the signatures */
    if (pyeclib_handle->type == PYECC_RS_VAND) {
      jerasure_matrix_encode(k, m, w, pyeclib_handle->matrix, 
                             c_fragment_signatures, parity_sigs, PYCC_MAX_SIG_LEN);
    } else {
      pyeclib_handle->xor_code_desc->encode(pyeclib_handle->xor_code_desc, 
                                            c_fragment_signatures,
                                            parity_sigs, 
                                            PYCC_MAX_SIG_LEN);
    }
    
    /* Compare the parity of the signatures, and the signature of the parity */
    for (i = 0; i < m; i++) {
      if (memcmp(parity_sigs[i], c_fragment_signatures[k + i], PYCC_MAX_SIG_LEN) != 0) {
        ret = i;
        break;
      }
    }

    /* Clean up memory used in algebraic signature checking */
    for (i = 0; i < m; i++) {
      free(parity_sigs[i]);
    }
    check_and_free_buffer(parity_sigs);
    for (i = 0; i < k; i++) {
      free(c_fragment_signatures[i]);
    }
  } else if (use_inline_chksum(pyeclib_handle)) {
    for (i = 0; i < num_fragments; i++) {
      if (c_fragment_metadata_list[i]->chksum_mismatch == 1) {
        ret = i;
        break;
      }
    }
  }

  /* Return index of first checksum signature error */  
  ret_obj = PyLong_FromLong((long)ret);
  goto exit;

error:
  ret_obj = NULL;

exit:
  free(c_fragment_signatures);
  free(c_fragment_metadata_list);
  
  return ret_obj;
}


static PyMethodDef PyECLibMethods[] = {
    {"init",  pyeclib_c_init, METH_VARARGS, "Initialize a new erasure encoder/decoder"},
    {"encode",  pyeclib_c_encode, METH_VARARGS, "Create parity using source data"},
    {"decode",  pyeclib_c_decode, METH_VARARGS, "Recover all lost data/parity"},
    {"reconstruct",  pyeclib_c_reconstruct, METH_VARARGS, "Recover selective data/parity"},
    {"fragments_to_string", pyeclib_c_fragments_to_string, METH_VARARGS, "Try to transform a set of fragments into original string without calling the decoder"},
    {"get_fragment_partition", pyeclib_c_get_fragment_partition, METH_VARARGS, "Parition fragments into data and parity, also returns a list of missing indexes"},
    {"get_required_fragments", pyeclib_c_get_required_fragments, METH_VARARGS, "Return the fragments required to reconstruct a set of missing fragments"},
    {"get_segment_info", pyeclib_c_get_segment_info, METH_VARARGS, "Return segment and fragment size information needed when encoding a segmented stream"},
    {"get_metadata", pyeclib_c_get_metadata, METH_VARARGS, "Get the integrity checking metadata for a fragment"},
    {"check_metadata", pyeclib_c_check_metadata, METH_VARARGS, "Check the integrity checking metadata for a set of fragments"},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

MOD_INIT(pyeclib_c)
{
    PyObject *m;

    MOD_DEF(m, "pyeclib_c", NULL, PyECLibMethods);

    if (m == NULL)
      return MOD_ERROR_VAL;

    PyECLibError = PyErr_NewException("pyeclib.Error", NULL, NULL);
    if (PyECLibError == NULL) {
      fprintf(stderr, "Could not create default PyECLib exception object!\n");
      exit(2);
    }
    Py_INCREF(PyECLibError);
    PyModule_AddObject(m, "error", PyECLibError);

    return MOD_SUCCESS_VAL(m);
}