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
|
/*
* Copyright (c) 2015, 2016 Jerry Ryle and Jonathan G. Underwood
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 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.
*
*/
#if defined(_WIN32) && defined(_MSC_VER)
#define inline __inline
#elif defined(__SUNPRO_C) || defined(__hpux) || defined(_AIX)
#define inline
#endif
#include <Python.h>
#include <stdlib.h>
#include <lz4.h> /* Needed for LZ4_VERSION_NUMBER only. */
#include <lz4frame.h>
static const char * compression_context_capsule_name = "_frame.LZ4F_cctx";
static const char * decompression_context_capsule_name = "_frame.LZ4F_dctx";
struct compression_context
{
LZ4F_cctx * context;
LZ4F_preferences_t preferences;
};
/*****************************
* create_compression_context *
******************************/
static void
destroy_compression_context (PyObject * py_context)
{
#ifndef PyCapsule_Type
struct compression_context *context =
PyCapsule_GetPointer (py_context, compression_context_capsule_name);
#else
/* Compatibility with 2.6 via capsulethunk. */
struct compression_context *context = py_context;
#endif
Py_BEGIN_ALLOW_THREADS
LZ4F_freeCompressionContext (context->context);
Py_END_ALLOW_THREADS
PyMem_Free (context);
}
static PyObject *
create_compression_context (PyObject * Py_UNUSED (self))
{
struct compression_context * context;
LZ4F_errorCode_t result;
context =
(struct compression_context *)
PyMem_Malloc (sizeof (struct compression_context));
if (!context)
{
return PyErr_NoMemory ();
}
Py_BEGIN_ALLOW_THREADS
result =
LZ4F_createCompressionContext (&context->context,
LZ4F_VERSION);
Py_END_ALLOW_THREADS
if (LZ4F_isError (result))
{
LZ4F_freeCompressionContext (context->context);
PyMem_Free (context);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_createCompressionContext failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
return PyCapsule_New (context, compression_context_capsule_name,
destroy_compression_context);
}
/************
* compress *
************/
static PyObject *
compress (PyObject * Py_UNUSED (self), PyObject * args,
PyObject * keywds)
{
Py_buffer source;
Py_ssize_t source_size;
int store_size = 1;
int return_bytearray = 0;
int content_checksum = 0;
int block_checksum = 0;
int block_linked = 1;
LZ4F_preferences_t preferences;
size_t destination_size;
size_t compressed_size;
PyObject *py_destination;
char *destination;
static char *kwlist[] = { "data",
"compression_level",
"block_size",
"content_checksum",
"block_checksum",
"block_linked",
"store_size",
"return_bytearray",
NULL
};
memset (&preferences, 0, sizeof preferences);
if (!PyArg_ParseTupleAndKeywords (args, keywds, "y*|iippppp", kwlist,
&source,
&preferences.compressionLevel,
&preferences.frameInfo.blockSizeID,
&content_checksum,
&block_checksum,
&block_linked,
&store_size,
&return_bytearray))
{
return NULL;
}
if (content_checksum)
{
preferences.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;
}
else
{
preferences.frameInfo.contentChecksumFlag = LZ4F_noContentChecksum;
}
if (block_linked)
{
preferences.frameInfo.blockMode = LZ4F_blockLinked;
}
else
{
preferences.frameInfo.blockMode = LZ4F_blockIndependent;
}
if (LZ4_versionNumber() >= 10800)
{
if (block_checksum)
{
preferences.frameInfo.blockChecksumFlag = LZ4F_blockChecksumEnabled;
}
else
{
preferences.frameInfo.blockChecksumFlag = LZ4F_noBlockChecksum;
}
}
else if (block_checksum)
{
PyErr_SetString (PyExc_RuntimeError,
"block_checksum specified but not supported by LZ4 library version");
return NULL;
}
source_size = source.len;
preferences.autoFlush = 0;
if (store_size)
{
preferences.frameInfo.contentSize = source_size;
}
else
{
preferences.frameInfo.contentSize = 0;
}
Py_BEGIN_ALLOW_THREADS
destination_size =
LZ4F_compressFrameBound (source_size, &preferences);
Py_END_ALLOW_THREADS
if (destination_size > PY_SSIZE_T_MAX)
{
PyBuffer_Release(&source);
PyErr_Format (PyExc_ValueError,
"Input data could require %zu bytes, which is larger than the maximum supported size of %zd bytes",
destination_size, PY_SSIZE_T_MAX);
return NULL;
}
destination = PyMem_Malloc (destination_size * sizeof * destination);
if (destination == NULL)
{
PyBuffer_Release(&source);
return PyErr_NoMemory();
}
Py_BEGIN_ALLOW_THREADS
compressed_size =
LZ4F_compressFrame (destination, destination_size, source.buf, source_size,
&preferences);
Py_END_ALLOW_THREADS
PyBuffer_Release(&source);
if (LZ4F_isError (compressed_size))
{
PyMem_Free (destination);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_compressFrame failed with code: %s",
LZ4F_getErrorName (compressed_size));
return NULL;
}
if (return_bytearray)
{
py_destination = PyByteArray_FromStringAndSize (destination, (Py_ssize_t) compressed_size);
}
else
{
py_destination = PyBytes_FromStringAndSize (destination, (Py_ssize_t) compressed_size);
}
PyMem_Free (destination);
if (py_destination == NULL)
{
return PyErr_NoMemory ();
}
return py_destination;
}
/******************
* compress_begin *
******************/
static PyObject *
compress_begin (PyObject * Py_UNUSED (self), PyObject * args,
PyObject * keywds)
{
PyObject *py_context = NULL;
Py_ssize_t source_size = (Py_ssize_t) 0;
int return_bytearray = 0;
int content_checksum = 0;
int block_checksum = 0;
int block_linked = 1;
LZ4F_preferences_t preferences;
PyObject *py_destination;
char * destination;
/* The destination buffer needs to be large enough for a header, which is 15
* bytes. Unfortunately, the lz4 library doesn't provide a #define for this.
* We over-allocate to allow for larger headers in the future. */
const size_t header_size = 32;
struct compression_context *context;
size_t result;
static char *kwlist[] = { "context",
"source_size",
"compression_level",
"block_size",
"content_checksum",
"block_checksum",
"block_linked",
"auto_flush",
"return_bytearray",
NULL
};
memset (&preferences, 0, sizeof preferences);
if (!PyArg_ParseTupleAndKeywords (args, keywds, "O|kiippppp", kwlist,
&py_context,
&source_size,
&preferences.compressionLevel,
&preferences.frameInfo.blockSizeID,
&content_checksum,
&block_checksum,
&block_linked,
&preferences.autoFlush,
&return_bytearray
))
{
return NULL;
}
if (content_checksum)
{
preferences.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;
}
else
{
preferences.frameInfo.contentChecksumFlag = LZ4F_noContentChecksum;
}
if (block_linked)
{
preferences.frameInfo.blockMode = LZ4F_blockLinked;
}
else
{
preferences.frameInfo.blockMode = LZ4F_blockIndependent;
}
if (LZ4_versionNumber() >= 10800)
{
if (block_checksum)
{
preferences.frameInfo.blockChecksumFlag = LZ4F_blockChecksumEnabled;
}
else
{
preferences.frameInfo.blockChecksumFlag = LZ4F_noBlockChecksum;
}
}
else if (block_checksum)
{
PyErr_SetString (PyExc_RuntimeError,
"block_checksum specified but not supported by LZ4 library version");
return NULL;
}
if (block_linked)
{
preferences.frameInfo.blockMode = LZ4F_blockLinked;
}
else
{
preferences.frameInfo.blockMode = LZ4F_blockIndependent;
}
preferences.frameInfo.contentSize = source_size;
context =
(struct compression_context *) PyCapsule_GetPointer (py_context, compression_context_capsule_name);
if (!context || !context->context)
{
PyErr_SetString (PyExc_ValueError, "No valid compression context supplied");
return NULL;
}
context->preferences = preferences;
destination = PyMem_Malloc (header_size * sizeof * destination);
if (destination == NULL)
{
return PyErr_NoMemory();
}
Py_BEGIN_ALLOW_THREADS
result = LZ4F_compressBegin (context->context,
destination,
header_size,
&context->preferences);
Py_END_ALLOW_THREADS
if (LZ4F_isError (result))
{
PyErr_Format (PyExc_RuntimeError,
"LZ4F_compressBegin failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
if (return_bytearray)
{
py_destination = PyByteArray_FromStringAndSize (destination, (Py_ssize_t) result);
}
else
{
py_destination = PyBytes_FromStringAndSize (destination, (Py_ssize_t) result);
}
PyMem_Free (destination);
if (py_destination == NULL)
{
return PyErr_NoMemory ();
}
return py_destination;
}
/******************
* compress_chunk *
******************/
static PyObject *
compress_chunk (PyObject * Py_UNUSED (self), PyObject * args,
PyObject * keywds)
{
PyObject *py_context = NULL;
Py_buffer source;
Py_ssize_t source_size;
struct compression_context *context;
size_t compressed_bound;
PyObject *py_destination;
char *destination;
LZ4F_compressOptions_t compress_options;
size_t result;
int return_bytearray = 0;
static char *kwlist[] = { "context",
"data",
"return_bytearray",
NULL
};
memset (&compress_options, 0, sizeof compress_options);
if (!PyArg_ParseTupleAndKeywords (args, keywds, "Oy*|p", kwlist,
&py_context,
&source,
&return_bytearray))
{
return NULL;
}
source_size = source.len;
context =
(struct compression_context *) PyCapsule_GetPointer (py_context, compression_context_capsule_name);
if (!context || !context->context)
{
PyBuffer_Release(&source);
PyErr_Format (PyExc_ValueError, "No compression context supplied");
return NULL;
}
/* If autoFlush is enabled, then the destination buffer only needs to be as
big as LZ4F_compressFrameBound specifies for this source size. However, if
autoFlush is disabled, previous calls may have resulted in buffered data,
and so we need instead to use LZ4F_compressBound to find the size required
for the destination buffer. This means that with autoFlush disabled we may
frequently allocate more memory than needed. */
Py_BEGIN_ALLOW_THREADS
if (context->preferences.autoFlush == 1)
{
compressed_bound =
LZ4F_compressFrameBound (source_size, &context->preferences);
}
else
{
compressed_bound =
LZ4F_compressBound (source_size, &context->preferences);
}
Py_END_ALLOW_THREADS
if (compressed_bound > PY_SSIZE_T_MAX)
{
PyBuffer_Release(&source);
PyErr_Format (PyExc_ValueError,
"input data could require %zu bytes, which is larger than the maximum supported size of %zd bytes",
compressed_bound, PY_SSIZE_T_MAX);
return NULL;
}
destination = PyMem_Malloc (compressed_bound * sizeof * destination);
if (destination == NULL)
{
PyBuffer_Release(&source);
return PyErr_NoMemory();
}
compress_options.stableSrc = 0;
Py_BEGIN_ALLOW_THREADS
result =
LZ4F_compressUpdate (context->context, destination,
compressed_bound, source.buf, source_size,
&compress_options);
Py_END_ALLOW_THREADS
PyBuffer_Release(&source);
if (LZ4F_isError (result))
{
PyMem_Free (destination);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_compressUpdate failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
if (return_bytearray)
{
py_destination = PyByteArray_FromStringAndSize (destination, (Py_ssize_t) result);
}
else
{
py_destination = PyBytes_FromStringAndSize (destination, (Py_ssize_t) result);
}
PyMem_Free (destination);
if (py_destination == NULL)
{
return PyErr_NoMemory ();
}
return py_destination;
}
/******************
* compress_flush *
******************/
static PyObject *
compress_flush (PyObject * Py_UNUSED (self), PyObject * args, PyObject * keywds)
{
PyObject *py_context = NULL;
LZ4F_compressOptions_t compress_options;
struct compression_context *context;
size_t destination_size;
int return_bytearray = 0;
int end_frame = 1;
PyObject *py_destination;
char * destination;
size_t result;
static char *kwlist[] = { "context",
"end_frame",
"return_bytearray",
NULL
};
memset (&compress_options, 0, sizeof compress_options);
if (!PyArg_ParseTupleAndKeywords (args, keywds, "O|pp", kwlist,
&py_context,
&end_frame,
&return_bytearray))
{
return NULL;
}
if (!end_frame && LZ4_versionNumber() < 10800)
{
PyErr_SetString (PyExc_RuntimeError,
"Flush without ending a frame is not supported with this version of the LZ4 library");
return NULL;
}
context =
(struct compression_context *) PyCapsule_GetPointer (py_context, compression_context_capsule_name);
if (!context || !context->context)
{
PyErr_SetString (PyExc_ValueError, "No compression context supplied");
return NULL;
}
compress_options.stableSrc = 0;
/* Calling LZ4F_compressBound with srcSize equal to 0 returns a size
sufficient to fit (i) any remaining buffered data (when autoFlush is
disabled) and the footer size, which is either 4 or 8 bytes depending on
whether checksums are enabled. See: https://github.com/lz4/lz4/issues/280
and https://github.com/lz4/lz4/issues/290. Prior to 1.7.5, it was necessary
to call LZ4F_compressBound with srcSize equal to 1. Since we now require a
minimum version to 1.7.5 we'll call this with srcSize equal to 0. */
Py_BEGIN_ALLOW_THREADS
destination_size = LZ4F_compressBound (0, &(context->preferences));
Py_END_ALLOW_THREADS
destination = PyMem_Malloc (destination_size * sizeof * destination);
if (destination == NULL)
{
return PyErr_NoMemory();
}
Py_BEGIN_ALLOW_THREADS
if (end_frame)
{
result =
LZ4F_compressEnd (context->context, destination,
destination_size, &compress_options);
}
else
{
result =
LZ4F_flush (context->context, destination,
destination_size, &compress_options);
}
Py_END_ALLOW_THREADS
if (LZ4F_isError (result))
{
PyMem_Free (destination);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_compressEnd failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
if (return_bytearray)
{
py_destination = PyByteArray_FromStringAndSize (destination, (Py_ssize_t) result);
}
else
{
py_destination = PyBytes_FromStringAndSize (destination, (Py_ssize_t) result);
}
PyMem_Free (destination);
if (py_destination == NULL)
{
return PyErr_NoMemory ();
}
return py_destination;
}
/******************
* get_frame_info *
******************/
static PyObject *
get_frame_info (PyObject * Py_UNUSED (self), PyObject * args,
PyObject * keywds)
{
Py_buffer py_source;
char *source;
size_t source_size;
LZ4F_decompressionContext_t context;
LZ4F_frameInfo_t frame_info;
size_t result;
unsigned int block_size;
unsigned int block_size_id;
int block_linked;
int content_checksum;
int block_checksum;
int skippable;
static char *kwlist[] = { "data",
NULL
};
if (!PyArg_ParseTupleAndKeywords (args, keywds, "y*", kwlist,
&py_source))
{
return NULL;
}
Py_BEGIN_ALLOW_THREADS
result = LZ4F_createDecompressionContext (&context, LZ4F_VERSION);
if (LZ4F_isError (result))
{
Py_BLOCK_THREADS
PyBuffer_Release (&py_source);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_createDecompressionContext failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
source = (char *) py_source.buf;
source_size = (size_t) py_source.len;
result =
LZ4F_getFrameInfo (context, &frame_info, source, &source_size);
if (LZ4F_isError (result))
{
LZ4F_freeDecompressionContext (context);
Py_BLOCK_THREADS
PyBuffer_Release (&py_source);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_getFrameInfo failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
result = LZ4F_freeDecompressionContext (context);
Py_END_ALLOW_THREADS
PyBuffer_Release (&py_source);
if (LZ4F_isError (result))
{
PyErr_Format (PyExc_RuntimeError,
"LZ4F_freeDecompressionContext failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
#define KB *(1<<10)
#define MB *(1<<20)
switch (frame_info.blockSizeID)
{
case LZ4F_default:
case LZ4F_max64KB:
block_size = 64 KB;
block_size_id = LZ4F_max64KB;
break;
case LZ4F_max256KB:
block_size = 256 KB;
block_size_id = LZ4F_max256KB;
break;
case LZ4F_max1MB:
block_size = 1 MB;
block_size_id = LZ4F_max1MB;
break;
case LZ4F_max4MB:
block_size = 4 MB;
block_size_id = LZ4F_max4MB;
break;
default:
PyErr_Format (PyExc_RuntimeError,
"Unrecognized blockSizeID in get_frame_info: %d",
frame_info.blockSizeID);
return NULL;
}
#undef KB
#undef MB
if (frame_info.blockMode == LZ4F_blockLinked)
{
block_linked = 1;
}
else if (frame_info.blockMode == LZ4F_blockIndependent)
{
block_linked = 0;
}
else
{
PyErr_Format (PyExc_RuntimeError,
"Unrecognized blockMode in get_frame_info: %d",
frame_info.blockMode);
return NULL;
}
if (frame_info.contentChecksumFlag == LZ4F_noContentChecksum)
{
content_checksum = 0;
}
else if (frame_info.contentChecksumFlag == LZ4F_contentChecksumEnabled)
{
content_checksum = 1;
}
else
{
PyErr_Format (PyExc_RuntimeError,
"Unrecognized contentChecksumFlag in get_frame_info: %d",
frame_info.contentChecksumFlag);
return NULL;
}
if (LZ4_versionNumber() >= 10800)
{
if (frame_info.blockChecksumFlag == LZ4F_noBlockChecksum)
{
block_checksum = 0;
}
else if (frame_info.blockChecksumFlag == LZ4F_blockChecksumEnabled)
{
block_checksum = 1;
}
else
{
PyErr_Format (PyExc_RuntimeError,
"Unrecognized blockChecksumFlag in get_frame_info: %d",
frame_info.blockChecksumFlag);
return NULL;
}
}
else
{
/* Prior to LZ4 1.8.0 the blockChecksum functionality wasn't exposed in the
frame API, and blocks weren't checksummed, so we'll always return 0
here. */
block_checksum = 0;
}
if (frame_info.frameType == LZ4F_frame)
{
skippable = 0;
}
else if (frame_info.frameType == LZ4F_skippableFrame)
{
skippable = 1;
}
else
{
PyErr_Format (PyExc_RuntimeError,
"Unrecognized frameType in get_frame_info: %d",
frame_info.frameType);
return NULL;
}
return Py_BuildValue ("{s:I,s:I,s:O,s:O,s:O,s:O,s:K}",
"block_size", block_size,
"block_size_id", block_size_id,
"block_linked", block_linked ? Py_True : Py_False,
"content_checksum", content_checksum ? Py_True : Py_False,
"block_checksum", block_checksum ? Py_True : Py_False,
"skippable", skippable ? Py_True : Py_False,
"content_size", frame_info.contentSize);
}
/********************************
* create_decompression_context *
********************************/
static void
destroy_decompression_context (PyObject * py_context)
{
#ifndef PyCapsule_Type
LZ4F_dctx * context =
PyCapsule_GetPointer (py_context, decompression_context_capsule_name);
#else
/* Compatibility with 2.6 via capsulethunk. */
LZ4F_dctx * context = py_context;
#endif
Py_BEGIN_ALLOW_THREADS
LZ4F_freeDecompressionContext (context);
Py_END_ALLOW_THREADS
}
static PyObject *
create_decompression_context (PyObject * Py_UNUSED (self))
{
LZ4F_dctx * context;
LZ4F_errorCode_t result;
Py_BEGIN_ALLOW_THREADS
result = LZ4F_createDecompressionContext (&context, LZ4F_VERSION);
if (LZ4F_isError (result))
{
Py_BLOCK_THREADS
LZ4F_freeDecompressionContext (context);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_createDecompressionContext failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
Py_END_ALLOW_THREADS
return PyCapsule_New (context, decompression_context_capsule_name,
destroy_decompression_context);
}
/*******************************
* reset_decompression_context *
*******************************/
static PyObject *
reset_decompression_context (PyObject * Py_UNUSED (self), PyObject * args,
PyObject * keywds)
{
LZ4F_dctx * context;
PyObject * py_context = NULL;
static char *kwlist[] = { "context",
NULL
};
if (!PyArg_ParseTupleAndKeywords (args, keywds, "O", kwlist,
&py_context
))
{
return NULL;
}
context = (LZ4F_dctx *)
PyCapsule_GetPointer (py_context, decompression_context_capsule_name);
if (!context)
{
PyErr_SetString (PyExc_ValueError,
"No valid decompression context supplied");
return NULL;
}
if (LZ4_versionNumber() >= 10800) /* LZ4 >= v1.8.0 has LZ4F_resetDecompressionContext */
{
/* No error checking possible here - this is always successful. */
Py_BEGIN_ALLOW_THREADS
LZ4F_resetDecompressionContext (context);
Py_END_ALLOW_THREADS
}
else
{
/* No resetDecompressionContext available, so we'll destroy the context
and create a new one. */
int result;
Py_BEGIN_ALLOW_THREADS
LZ4F_freeDecompressionContext (context);
result = LZ4F_createDecompressionContext (&context, LZ4F_VERSION);
if (LZ4F_isError (result))
{
LZ4F_freeDecompressionContext (context);
Py_BLOCK_THREADS
PyErr_Format (PyExc_RuntimeError,
"LZ4F_createDecompressionContext failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
Py_END_ALLOW_THREADS
result = PyCapsule_SetPointer(py_context, context);
if (result)
{
LZ4F_freeDecompressionContext (context);
PyErr_SetString (PyExc_RuntimeError,
"PyCapsule_SetPointer failed with code: %s");
return NULL;
}
}
Py_RETURN_NONE;
}
static inline PyObject *
__decompress(LZ4F_dctx * context, char * source, size_t source_size,
Py_ssize_t max_length, int full_frame,
int return_bytearray, int return_bytes_read)
{
size_t source_remain;
size_t source_read;
char * source_cursor;
char * source_end;
char * destination;
size_t destination_write;
char * destination_cursor;
size_t destination_written;
size_t destination_size;
PyObject * py_destination;
size_t result = 0;
LZ4F_frameInfo_t frame_info;
LZ4F_decompressOptions_t options;
int end_of_frame = 0;
int resize_factor = 1;
memset(&options, 0, sizeof options);
Py_BEGIN_ALLOW_THREADS
source_cursor = source;
source_end = source + source_size;
source_remain = source_size;
if (full_frame)
{
source_read = source_size;
result =
LZ4F_getFrameInfo (context, &frame_info,
source_cursor, &source_read);
if (LZ4F_isError (result))
{
Py_BLOCK_THREADS
PyErr_Format (PyExc_RuntimeError,
"LZ4F_getFrameInfo failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
/* Advance the source_cursor pointer past the header - the call to
getFrameInfo above replaces the passed source_read value with the
number of bytes read. Also reduce source_remain accordingly. */
source_cursor += source_read;
source_remain -= source_read;
/* If the uncompressed content size is available, we'll use that to size
the destination buffer. Otherwise, guess at twice the remaining source
source as a starting point, and adjust if needed. */
if (frame_info.contentSize > 0)
{
destination_size = frame_info.contentSize;
}
else
{
destination_size = 2 * source_remain;
}
}
else
{
if (max_length >= (Py_ssize_t) 0)
{
destination_size = (size_t) max_length;
}
else
{
/* Choose an initial destination size as twice the source size, and we'll
grow the allocation as needed. */
destination_size = 2 * source_remain;
}
}
Py_BLOCK_THREADS
destination = PyMem_Malloc (destination_size * sizeof * destination);
if (destination == NULL)
{
return PyErr_NoMemory();
}
Py_UNBLOCK_THREADS
/* Only set stableDst = 1 if we are sure no PyMem_Realloc will be called since
when stableDst = 1 the LZ4 library stores a pointer to the last compressed
data, which may be invalid after a PyMem_Realloc. */
if (full_frame && max_length >= (Py_ssize_t) 0)
{
options.stableDst = 1;
}
else
{
options.stableDst = 0;
}
source_read = source_remain;
destination_write = destination_size;
destination_cursor = destination;
destination_written = 0;
while (1)
{
/* Decompress from the source string and write to the destination
until there's no more source string to read, or until we've reached the
frame end.
On calling LZ4F_decompress, source_read is set to the remaining length
of source available to read. On return, source_read is set to the
actual number of bytes read from source, which may be less than
available. NB: LZ4F_decompress does not explicitly fail on empty input.
On calling LZ4F_decompress, destination_write is the number of bytes in
destination available for writing. On exit, destination_write is set to
the actual number of bytes written to destination. */
result = LZ4F_decompress (context,
destination_cursor,
&destination_write,
source_cursor,
&source_read,
&options);
if (LZ4F_isError (result))
{
Py_BLOCK_THREADS
PyErr_Format (PyExc_RuntimeError,
"LZ4F_decompress failed with code: %s",
LZ4F_getErrorName (result));
PyMem_Free (destination);
return NULL;
}
destination_written += destination_write;
source_cursor += source_read;
source_read = source_end - source_cursor;
if (result == 0)
{
/* We've reached the end of the frame. */
end_of_frame = 1;
break;
}
else if (source_cursor == source_end)
{
/* We've reached end of input. */
break;
}
else if (destination_written == destination_size)
{
/* Destination buffer is full. So, stop decompressing if
max_length is set. Otherwise expand the destination
buffer. */
if (max_length >= (Py_ssize_t) 0)
{
break;
}
else
{
/* Expand the destination buffer. We've tried various strategies
here to estimate the compression ratio so far and adjust the
buffer size accordingly. However, that grows the buffer too
slowly. The best choices found were to either double the buffer
size each time, or to grow faster by multiplying the buffer
size by 2^N, where N is the number of resizes. We take the
latter approach, though the former approach may actually be
good enough in practice. */
char * buff;
resize_factor *= 2;
destination_size *= resize_factor;
Py_BLOCK_THREADS
buff = PyMem_Realloc (destination, destination_size);
if (buff == NULL)
{
PyErr_SetString (PyExc_RuntimeError,
"Failed to resize buffer");
return NULL;
}
else
{
destination = buff;
}
Py_UNBLOCK_THREADS
}
}
/* Data still remaining to be decompressed, so increment the destination
cursor location, and reset destination_write ready for the next
iteration. Important to re-initialize destination_cursor here (as
opposed to simply incrementing it) so we're pointing to the realloc'd
memory location. */
destination_cursor = destination + destination_written;
destination_write = destination_size - destination_written;
}
Py_END_ALLOW_THREADS
if (result > 0 && full_frame)
{
PyErr_Format (PyExc_RuntimeError,
"Frame incomplete. LZ4F_decompress returned: %zu", result);
PyMem_Free (destination);
return NULL;
}
if (LZ4F_isError (result))
{
PyErr_Format (PyExc_RuntimeError,
"LZ4F_freeDecompressionContext failed with code: %s",
LZ4F_getErrorName (result));
PyMem_Free (destination);
return NULL;
}
if (return_bytearray)
{
py_destination = PyByteArray_FromStringAndSize (destination, (Py_ssize_t) destination_written);
}
else
{
py_destination = PyBytes_FromStringAndSize (destination, (Py_ssize_t) destination_written);
}
PyMem_Free (destination);
if (py_destination == NULL)
{
return PyErr_NoMemory ();
}
if (full_frame)
{
if (return_bytes_read)
{
return Py_BuildValue ("Ni",
py_destination,
source_cursor - source);
}
else
{
return py_destination;
}
}
else
{
return Py_BuildValue ("NiO",
py_destination,
source_cursor - source,
end_of_frame ? Py_True : Py_False);
}
}
/**************
* decompress *
**************/
static PyObject *
decompress (PyObject * Py_UNUSED (self), PyObject * args,
PyObject * keywds)
{
LZ4F_dctx * context;
LZ4F_errorCode_t result;
Py_buffer py_source;
char * source;
size_t source_size;
PyObject * ret;
int return_bytearray = 0;
int return_bytes_read = 0;
static char *kwlist[] = { "data",
"return_bytearray",
"return_bytes_read",
NULL
};
if (!PyArg_ParseTupleAndKeywords (args, keywds, "y*|pp", kwlist,
&py_source,
&return_bytearray,
&return_bytes_read
))
{
return NULL;
}
Py_BEGIN_ALLOW_THREADS
result = LZ4F_createDecompressionContext (&context, LZ4F_VERSION);
if (LZ4F_isError (result))
{
LZ4F_freeDecompressionContext (context);
Py_BLOCK_THREADS
PyBuffer_Release(&py_source);
PyErr_Format (PyExc_RuntimeError,
"LZ4F_createDecompressionContext failed with code: %s",
LZ4F_getErrorName (result));
return NULL;
}
Py_END_ALLOW_THREADS
/* MSVC can't do pointer arithmetic on void * pointers, so cast to char * */
source = (char *) py_source.buf;
source_size = py_source.len;
ret = __decompress (context,
source,
source_size,
-1,
1,
return_bytearray,
return_bytes_read);
PyBuffer_Release(&py_source);
Py_BEGIN_ALLOW_THREADS
LZ4F_freeDecompressionContext (context);
Py_END_ALLOW_THREADS
return ret;
}
/********************
* decompress_chunk *
********************/
static PyObject *
decompress_chunk (PyObject * Py_UNUSED (self), PyObject * args,
PyObject * keywds)
{
PyObject * py_context = NULL;
PyObject * ret;
LZ4F_dctx * context;
Py_buffer py_source;
char * source;
size_t source_size;
Py_ssize_t max_length = (Py_ssize_t) -1;
int return_bytearray = 0;
static char *kwlist[] = { "context",
"data",
"max_length",
"return_bytearray",
NULL
};
if (!PyArg_ParseTupleAndKeywords (args, keywds, "Oy*|np", kwlist,
&py_context,
&py_source,
&max_length,
&return_bytearray
))
{
return NULL;
}
context = (LZ4F_dctx *)
PyCapsule_GetPointer (py_context, decompression_context_capsule_name);
if (!context)
{
PyBuffer_Release(&py_source);
PyErr_SetString (PyExc_ValueError,
"No valid decompression context supplied");
return NULL;
}
/* MSVC can't do pointer arithmetic on void * pointers, so cast to char * */
source = (char *) py_source.buf;
source_size = py_source.len;
ret = __decompress (context,
source,
source_size,
max_length,
0,
return_bytearray,
0);
PyBuffer_Release(&py_source);
return ret;
}
PyDoc_STRVAR(
create_compression_context__doc,
"create_compression_context()\n" \
"\n" \
"Creates a compression context object.\n" \
"\n" \
"The compression object is required for compression operations.\n" \
"\n" \
"Returns:\n" \
" cCtx: A compression context\n"
);
#define COMPRESS_KWARGS_DOCSTRING \
" block_size (int): Sepcifies the maximum blocksize to use.\n" \
" Options:\n\n" \
" - `lz4.frame.BLOCKSIZE_DEFAULT`: the lz4 library default\n" \
" - `lz4.frame.BLOCKSIZE_MAX64KB`: 64 kB\n" \
" - `lz4.frame.BLOCKSIZE_MAX256KB`: 256 kB\n" \
" - `lz4.frame.BLOCKSIZE_MAX1MB`: 1 MB\n" \
" - `lz4.frame.BLOCKSIZE_MAX4MB`: 4 MB\n\n" \
" If unspecified, will default to `lz4.frame.BLOCKSIZE_DEFAULT`\n" \
" which is currently equal to `lz4.frame.BLOCKSIZE_MAX64KB`.\n" \
" block_linked (bool): Specifies whether to use block-linked\n" \
" compression. If ``True``, the compression ratio is improved,\n" \
" particularly for small block sizes. Default is ``True``.\n" \
" compression_level (int): Specifies the level of compression used.\n" \
" Values between 0-16 are valid, with 0 (default) being the\n" \
" lowest compression (0-2 are the same value), and 16 the highest.\n" \
" Values below 0 will enable \"fast acceleration\", proportional\n" \
" to the value. Values above 16 will be treated as 16.\n" \
" The following module constants are provided as a convenience:\n\n" \
" - `lz4.frame.COMPRESSIONLEVEL_MIN`: Minimum compression (0, the\n" \
" default)\n" \
" - `lz4.frame.COMPRESSIONLEVEL_MINHC`: Minimum high-compression\n" \
" mode (3)\n" \
" - `lz4.frame.COMPRESSIONLEVEL_MAX`: Maximum compression (16)\n\n" \
" content_checksum (bool): Specifies whether to enable checksumming\n" \
" of the uncompressed content. If True, a checksum is stored at the\n" \
" end of the frame, and checked during decompression. Default is\n" \
" ``False``.\n" \
" block_checksum (bool): Specifies whether to enable checksumming of\n" \
" the uncompressed content of each block. If `True` a checksum of\n" \
" the uncompressed data in each block in the frame is stored at\n\n" \
" the end of each block. If present, these checksums will be used\n\n" \
" to validate the data during decompression. The default is\n" \
" ``False`` meaning block checksums are not calculated and stored.\n" \
" This functionality is only supported if the underlying LZ4\n" \
" library has version >= 1.8.0. Attempting to set this value\n" \
" to ``True`` with a version of LZ4 < 1.8.0 will cause a\n" \
" ``RuntimeError`` to be raised.\n" \
" return_bytearray (bool): If ``True`` a ``bytearray`` object will be\n" \
" returned. If ``False``, a string of bytes is returned. The default\n" \
" is ``False``.\n" \
PyDoc_STRVAR(
compress__doc,
"compress(data, compression_level=0, block_size=0, content_checksum=0,\n" \
"block_linked=True, store_size=True, return_bytearray=False)\n" \
"\n" \
"Compresses ``data`` returning the compressed data as a complete frame.\n" \
"\n" \
"The returned data includes a header and endmark and so is suitable\n" \
"for writing to a file.\n" \
"\n" \
"Args:\n" \
" data (str, bytes or buffer-compatible object): data to compress\n" \
"\n" \
"Keyword Args:\n" \
COMPRESS_KWARGS_DOCSTRING \
" store_size (bool): If ``True`` then the frame will include an 8-byte\n" \
" header field that is the uncompressed size of data included\n" \
" within the frame. Default is ``True``.\n" \
"\n" \
"Returns:\n" \
" bytes or bytearray: Compressed data\n"
);
PyDoc_STRVAR
(
compress_begin__doc,
"compress_begin(context, source_size=0, compression_level=0, block_size=0,\n" \
"content_checksum=0, content_size=1, block_linked=0, frame_type=0,\n" \
"auto_flush=1)\n" \
"\n" \
"Creates a frame header from a compression context.\n\n" \
"Args:\n" \
" context (cCtx): A compression context.\n\n" \
"Keyword Args:\n" \
COMPRESS_KWARGS_DOCSTRING \
" auto_flush (bool): Enable or disable autoFlush. When autoFlush is disabled\n" \
" the LZ4 library may buffer data internally until a block is full.\n" \
" Default is ``False`` (autoFlush disabled).\n\n" \
" source_size (int): This optionally specifies the uncompressed size\n" \
" of the data to be compressed. If specified, the size will be stored\n" \
" in the frame header for use during decompression. Default is ``True``\n" \
" return_bytearray (bool): If ``True`` a bytearray object will be returned.\n" \
" If ``False``, a string of bytes is returned. Default is ``False``.\n\n" \
"Returns:\n" \
" bytes or bytearray: Frame header.\n"
);
#undef COMPRESS_KWARGS_DOCSTRING
PyDoc_STRVAR
(
compress_chunk__doc,
"compress_chunk(context, data)\n" \
"\n" \
"Compresses blocks of data and returns the compressed data.\n" \
"\n" \
"The returned data should be concatenated with the data returned from\n" \
"`lz4.frame.compress_begin` and any subsequent calls to\n" \
"`lz4.frame.compress_chunk`.\n" \
"\n" \
"Args:\n" \
" context (cCtx): compression context\n" \
" data (str, bytes or buffer-compatible object): data to compress\n" \
"\n" \
"Keyword Args:\n" \
" return_bytearray (bool): If ``True`` a bytearray object will be\n" \
" returned. If ``False``, a string of bytes is returned. The\n" \
" default is False.\n" \
"\n" \
"Returns:\n" \
" bytes or bytearray: Compressed data.\n\n" \
"Notes:\n" \
" If auto flush is disabled (``auto_flush=False`` when calling\n" \
" `lz4.frame.compress_begin`) this function may buffer and retain\n" \
" some or all of the compressed data for future calls to\n" \
" `lz4.frame.compress`.\n"
);
PyDoc_STRVAR
(
compress_flush__doc,
"compress_flush(context, end_frame=True, return_bytearray=False)\n" \
"\n" \
"Flushes any buffered data held in the compression context.\n" \
"\n" \
"This flushes any data buffed in the compression context, returning it as\n" \
"compressed data. The returned data should be appended to the output of\n" \
"previous calls to ``lz4.frame.compress_chunk``.\n" \
"\n" \
"The ``end_frame`` argument specifies whether or not the frame should be\n" \
"ended. If this is ``True`` and end of frame marker will be appended to\n" \
"the returned data. In this case, if ``content_checksum`` was ``True``\n" \
"when calling `lz4.frame.compress_begin`, then a checksum of the uncompressed\n" \
"data will also be included in the returned data.\n" \
"\n" \
"If the ``end_frame`` argument is ``True``, the compression context will be\n" \
"reset and can be re-used.\n" \
"\n" \
"Args:\n" \
" context (cCtx): Compression context\n" \
"\n" \
"Keyword Args:\n" \
" end_frame (bool): If ``True`` the frame will be ended. Default is\n" \
" ``True``.\n" \
" return_bytearray (bool): If ``True`` a ``bytearray`` object will\n" \
" be returned. If ``False``, a ``bytes`` object is returned.\n" \
" The default is ``False``.\n" \
"\n" \
"Returns:\n" \
" bytes or bytearray: compressed data.\n" \
"\n" \
"Notes:\n" \
" If ``end_frame`` is ``False`` but the underlying LZ4 library does not" \
" support flushing without ending the frame, a ``RuntimeError`` will be\n" \
" raised.\n"
);
PyDoc_STRVAR
(
get_frame_info__doc,
"get_frame_info(frame)\n\n" \
"Given a frame of compressed data, returns information about the frame.\n" \
"\n" \
"Args:\n" \
" frame (str, bytes or buffer-compatible object): LZ4 compressed frame\n" \
"\n" \
"Returns:\n" \
" dict: Dictionary with keys:\n" \
"\n" \
" - ``block_size`` (int): the maximum size (in bytes) of each block\n" \
" - ``block_size_id`` (int): identifier for maximum block size\n" \
" - ``content_checksum`` (bool): specifies whether the frame\n" \
" contains a checksum of the uncompressed content\n" \
" - ``content_size`` (int): uncompressed size in bytes of\n" \
" frame content\n" \
" - ``block_linked`` (bool): specifies whether the frame contains\n" \
" blocks which are independently compressed (``False``) or linked\n" \
" linked (``True``)\n" \
" - ``block_checksum`` (bool): specifies whether each block contains a\n" \
" checksum of its contents\n" \
" - ``skippable`` (bool): whether the block is skippable (``True``) or\n" \
" not (``False``)\n"
);
PyDoc_STRVAR
(
create_decompression_context__doc,
"create_decompression_context()\n" \
"\n" \
"Creates a decompression context object.\n" \
"\n" \
"A decompression context is needed for decompression operations.\n" \
"\n" \
"Returns:\n" \
" dCtx: A decompression context\n"
);
PyDoc_STRVAR
(
reset_decompression_context__doc,
"reset_decompression_context(context)\n" \
"\n" \
"Resets a decompression context object.\n" \
"\n" \
"This is useful for recovering from an error or for stopping an unfinished\n" \
"decompression and starting a new one with the same context\n" \
"\n" \
"Args:\n" \
" context (dCtx): A decompression context\n"
);
PyDoc_STRVAR
(
decompress__doc,
"decompress(data, return_bytearray=False, return_bytes_read=False)\n" \
"\n" \
"Decompresses a frame of data and returns it as a string of bytes.\n" \
"\n" \
"Args:\n" \
" data (str, bytes or buffer-compatible object): data to decompress.\n" \
" This should contain a complete LZ4 frame of compressed data.\n" \
"\n" \
"Keyword Args:\n" \
" return_bytearray (bool): If ``True`` a bytearray object will be\n" \
" returned. If ``False``, a string of bytes is returned. The\n" \
" default is ``False``.\n" \
" return_bytes_read (bool): If ``True`` then the number of bytes read\n" \
" from ``data`` will also be returned. Default is ``False``\n" \
"\n" \
"Returns:\n" \
" bytes/bytearray or tuple: Uncompressed data and optionally the number" \
" of bytes read\n" \
"\n" \
" If the ``return_bytes_read`` argument is ``True`` this function\n" \
" returns a tuple consisting of:\n" \
"\n" \
" - bytes or bytearray: Uncompressed data\n" \
" - int: Number of bytes consumed from ``data``\n"
);
PyDoc_STRVAR
(
decompress_chunk__doc,
"decompress_chunk(context, data, max_length=-1)\n" \
"\n" \
"Decompresses part of a frame of compressed data.\n" \
"\n" \
"The returned uncompressed data should be concatenated with the data\n" \
"returned from previous calls to `lz4.frame.decompress_chunk`\n" \
"\n" \
"Args:\n" \
" context (dCtx): decompression context\n" \
" data (str, bytes or buffer-compatible object): part of a LZ4\n" \
" frame of compressed data\n" \
"\n" \
"Keyword Args:\n" \
" max_length (int): if non-negative this specifies the maximum number\n" \
" of bytes of uncompressed data to return. Default is ``-1``.\n" \
" return_bytearray (bool): If ``True`` a bytearray object will be\n" \
" returned.If ``False``, a string of bytes is returned. The\n" \
" default is ``False``.\n" \
"\n" \
"Returns:\n" \
" tuple: uncompressed data, bytes read, end of frame indicator\n" \
"\n" \
" This function returns a tuple consisting of:\n" \
"\n" \
" - The uncompressed data as a ``bytes`` or ``bytearray`` object\n" \
" - The number of bytes consumed from input ``data`` as an ``int``\n" \
" - The end of frame indicator as a ``bool``.\n" \
"\n"
"The end of frame indicator is ``True`` if the end of the compressed\n" \
"frame has been reached, or ``False`` otherwise\n"
);
static PyMethodDef module_methods[] =
{
{
"create_compression_context", (PyCFunction) create_compression_context,
METH_NOARGS, create_compression_context__doc
},
{
"compress", (PyCFunction) compress,
METH_VARARGS | METH_KEYWORDS, compress__doc
},
{
"compress_begin", (PyCFunction) compress_begin,
METH_VARARGS | METH_KEYWORDS, compress_begin__doc
},
{
"compress_chunk", (PyCFunction) compress_chunk,
METH_VARARGS | METH_KEYWORDS, compress_chunk__doc
},
{
"compress_flush", (PyCFunction) compress_flush,
METH_VARARGS | METH_KEYWORDS, compress_flush__doc
},
{
"get_frame_info", (PyCFunction) get_frame_info,
METH_VARARGS | METH_KEYWORDS, get_frame_info__doc
},
{
"create_decompression_context", (PyCFunction) create_decompression_context,
METH_NOARGS, create_decompression_context__doc
},
{
"reset_decompression_context", (PyCFunction) reset_decompression_context,
METH_VARARGS | METH_KEYWORDS, reset_decompression_context__doc
},
{
"decompress", (PyCFunction) decompress,
METH_VARARGS | METH_KEYWORDS, decompress__doc
},
{
"decompress_chunk", (PyCFunction) decompress_chunk,
METH_VARARGS | METH_KEYWORDS, decompress_chunk__doc
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyDoc_STRVAR(lz4frame__doc,
"A Python wrapper for the LZ4 frame protocol"
);
static struct PyModuleDef moduledef =
{
PyModuleDef_HEAD_INIT,
"_frame",
lz4frame__doc,
-1,
module_methods
};
PyMODINIT_FUNC
PyInit__frame(void)
{
PyObject *module = PyModule_Create (&moduledef);
if (module == NULL)
return NULL;
PyModule_AddIntConstant (module, "BLOCKSIZE_DEFAULT", LZ4F_default);
PyModule_AddIntConstant (module, "BLOCKSIZE_MAX64KB", LZ4F_max64KB);
PyModule_AddIntConstant (module, "BLOCKSIZE_MAX256KB", LZ4F_max256KB);
PyModule_AddIntConstant (module, "BLOCKSIZE_MAX1MB", LZ4F_max1MB);
PyModule_AddIntConstant (module, "BLOCKSIZE_MAX4MB", LZ4F_max4MB);
return module;
}
|