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
|
/**
* Copyright (c) 2016-present, Gregory Szorc
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
*/
#include "python-zstandard.h"
extern PyObject *ZstdError;
/**
* Ensure the ZSTD_DCtx on a decompressor is initiated and ready for a new
* operation.
*/
int ensure_dctx(ZstdDecompressor *decompressor, int loadDict) {
size_t zresult;
ZSTD_DCtx_reset(decompressor->dctx, ZSTD_reset_session_only);
if (decompressor->maxWindowSize) {
zresult = ZSTD_DCtx_setMaxWindowSize(decompressor->dctx,
decompressor->maxWindowSize);
if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "unable to set max window size: %s",
ZSTD_getErrorName(zresult));
return 1;
}
}
zresult = ZSTD_DCtx_setParameter(decompressor->dctx, ZSTD_d_format,
decompressor->format);
if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "unable to set decoding format: %s",
ZSTD_getErrorName(zresult));
return 1;
}
if (loadDict && decompressor->dict) {
if (ensure_ddict(decompressor->dict)) {
return 1;
}
zresult =
ZSTD_DCtx_refDDict(decompressor->dctx, decompressor->dict->ddict);
if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError,
"unable to reference prepared dictionary: %s",
ZSTD_getErrorName(zresult));
return 1;
}
}
return 0;
}
static int Decompressor_init(ZstdDecompressor *self, PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"dict_data", "max_window_size", "format", NULL};
PyObject *dict = NULL;
Py_ssize_t maxWindowSize = 0;
ZSTD_format_e format = ZSTD_f_zstd1;
self->dctx = NULL;
self->dict = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OnI:ZstdDecompressor",
kwlist, &dict, &maxWindowSize, &format)) {
return -1;
}
if (dict) {
if (dict == Py_None) {
dict = NULL;
}
else if (!PyObject_IsInstance(dict,
(PyObject *)ZstdCompressionDictType)) {
PyErr_Format(PyExc_TypeError,
"dict_data must be zstd.ZstdCompressionDict");
return -1;
}
}
self->dctx = ZSTD_createDCtx();
if (!self->dctx) {
PyErr_NoMemory();
goto except;
}
self->maxWindowSize = maxWindowSize;
self->format = format;
if (dict) {
self->dict = (ZstdCompressionDict *)dict;
Py_INCREF(dict);
}
if (ensure_dctx(self, 1)) {
goto except;
}
return 0;
except:
Py_CLEAR(self->dict);
if (self->dctx) {
ZSTD_freeDCtx(self->dctx);
self->dctx = NULL;
}
return -1;
}
static void Decompressor_dealloc(ZstdDecompressor *self) {
Py_CLEAR(self->dict);
if (self->dctx) {
ZSTD_freeDCtx(self->dctx);
self->dctx = NULL;
}
PyObject_Del(self);
}
static PyObject *Decompressor_memory_size(ZstdDecompressor *self) {
if (self->dctx) {
return PyLong_FromSize_t(ZSTD_sizeof_DCtx(self->dctx));
}
else {
PyErr_SetString(
ZstdError,
"no decompressor context found; this should never happen");
return NULL;
}
}
static PyObject *Decompressor_copy_stream(ZstdDecompressor *self,
PyObject *args, PyObject *kwargs) {
static char *kwlist[] = {"ifh", "ofh", "read_size", "write_size", NULL};
PyObject *source;
PyObject *dest;
size_t inSize = ZSTD_DStreamInSize();
size_t outSize = ZSTD_DStreamOutSize();
ZSTD_inBuffer input;
ZSTD_outBuffer output;
Py_ssize_t totalRead = 0;
Py_ssize_t totalWrite = 0;
char *readBuffer;
Py_ssize_t readSize;
PyObject *readResult = NULL;
PyObject *res = NULL;
size_t zresult = 0;
PyObject *writeResult;
PyObject *totalReadPy;
PyObject *totalWritePy;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|kk:copy_stream", kwlist,
&source, &dest, &inSize, &outSize)) {
return NULL;
}
if (!PyObject_HasAttrString(source, "read")) {
PyErr_SetString(PyExc_ValueError,
"first argument must have a read() method");
return NULL;
}
if (!PyObject_HasAttrString(dest, "write")) {
PyErr_SetString(PyExc_ValueError,
"second argument must have a write() method");
return NULL;
}
/* Prevent free on uninitialized memory in finally. */
output.dst = NULL;
if (ensure_dctx(self, 1)) {
res = NULL;
goto finally;
}
output.dst = PyMem_Malloc(outSize);
if (!output.dst) {
PyErr_NoMemory();
res = NULL;
goto finally;
}
output.size = outSize;
output.pos = 0;
/* Read source stream until EOF */
while (1) {
readResult = PyObject_CallMethod(source, "read", "n", inSize);
if (!readResult) {
goto finally;
}
PyBytes_AsStringAndSize(readResult, &readBuffer, &readSize);
/* If no data was read, we're at EOF. */
if (0 == readSize) {
break;
}
totalRead += readSize;
/* Send data to decompressor */
input.src = readBuffer;
input.size = readSize;
input.pos = 0;
while (input.pos < input.size) {
Py_BEGIN_ALLOW_THREADS zresult =
ZSTD_decompressStream(self->dctx, &output, &input);
Py_END_ALLOW_THREADS
if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "zstd decompressor error: %s",
ZSTD_getErrorName(zresult));
res = NULL;
goto finally;
}
if (output.pos) {
writeResult = PyObject_CallMethod(dest, "write", "y#",
output.dst, output.pos);
if (NULL == writeResult) {
res = NULL;
goto finally;
}
Py_XDECREF(writeResult);
totalWrite += output.pos;
output.pos = 0;
}
}
Py_CLEAR(readResult);
}
/* Source stream is exhausted. Finish up. */
totalReadPy = PyLong_FromSsize_t(totalRead);
totalWritePy = PyLong_FromSsize_t(totalWrite);
res = PyTuple_Pack(2, totalReadPy, totalWritePy);
Py_DECREF(totalReadPy);
Py_DECREF(totalWritePy);
finally:
if (output.dst) {
PyMem_Free(output.dst);
}
Py_XDECREF(readResult);
return res;
}
PyObject *Decompressor_decompress(ZstdDecompressor *self, PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {
"data",
"max_output_size",
"read_across_frames",
"allow_extra_data",
NULL
};
Py_buffer source;
Py_ssize_t maxOutputSize = 0;
unsigned long long decompressedSize;
PyObject *readAcrossFrames = NULL;
PyObject *allowExtraData = NULL;
size_t destCapacity;
PyObject *result = NULL;
size_t zresult;
ZSTD_outBuffer outBuffer;
ZSTD_inBuffer inBuffer;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|nOO:decompress", kwlist,
&source, &maxOutputSize, &readAcrossFrames,
&allowExtraData)) {
return NULL;
}
if (readAcrossFrames ? PyObject_IsTrue(readAcrossFrames) : 0) {
PyErr_SetString(ZstdError,
"ZstdDecompressor.read_across_frames=True is not yet implemented"
);
goto finally;
}
if (ensure_dctx(self, 1)) {
goto finally;
}
decompressedSize = ZSTD_getFrameContentSize(source.buf, source.len);
if (ZSTD_CONTENTSIZE_ERROR == decompressedSize) {
PyErr_SetString(ZstdError,
"error determining content size from frame header");
goto finally;
}
/* Special case of empty frame. */
else if (0 == decompressedSize) {
result = PyBytes_FromStringAndSize("", 0);
goto finally;
}
/* Missing content size in frame header. */
if (ZSTD_CONTENTSIZE_UNKNOWN == decompressedSize) {
if (0 == maxOutputSize) {
PyErr_SetString(ZstdError,
"could not determine content size in frame header");
goto finally;
}
result = PyBytes_FromStringAndSize(NULL, maxOutputSize);
destCapacity = maxOutputSize;
decompressedSize = 0;
}
/* Size is recorded in frame header. */
else {
assert(SIZE_MAX >= PY_SSIZE_T_MAX);
if (decompressedSize > PY_SSIZE_T_MAX) {
PyErr_SetString(
ZstdError, "frame is too large to decompress on this platform");
goto finally;
}
result = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)decompressedSize);
destCapacity = (size_t)decompressedSize;
}
if (!result) {
goto finally;
}
outBuffer.dst = PyBytes_AsString(result);
outBuffer.size = destCapacity;
outBuffer.pos = 0;
inBuffer.src = source.buf;
inBuffer.size = source.len;
inBuffer.pos = 0;
Py_BEGIN_ALLOW_THREADS zresult =
ZSTD_decompressStream(self->dctx, &outBuffer, &inBuffer);
Py_END_ALLOW_THREADS
if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "decompression error: %s",
ZSTD_getErrorName(zresult));
Py_CLEAR(result);
goto finally;
}
else if (zresult) {
PyErr_Format(ZstdError,
"decompression error: did not decompress full frame");
Py_CLEAR(result);
goto finally;
}
else if (decompressedSize && outBuffer.pos != decompressedSize) {
PyErr_Format(
ZstdError,
"decompression error: decompressed %zu bytes; expected %llu",
zresult, decompressedSize);
Py_CLEAR(result);
goto finally;
}
else if (outBuffer.pos < destCapacity) {
if (safe_pybytes_resize(&result, outBuffer.pos)) {
Py_CLEAR(result);
goto finally;
}
}
else if ((allowExtraData ? PyObject_IsTrue(allowExtraData) : 1) == 0
&& inBuffer.pos < inBuffer.size) {
PyErr_Format(
ZstdError,
"compressed input contains %zu bytes of unused data, which is disallowed",
inBuffer.size - inBuffer.pos
);
Py_CLEAR(result);
goto finally;
}
finally:
PyBuffer_Release(&source);
return result;
}
static ZstdDecompressionObj *Decompressor_decompressobj(ZstdDecompressor *self,
PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"write_size", "read_across_frames", NULL};
ZstdDecompressionObj *result = NULL;
size_t outSize = ZSTD_DStreamOutSize();
PyObject *readAcrossFrames = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|kO:decompressobj", kwlist,
&outSize, &readAcrossFrames)) {
return NULL;
}
if (!outSize) {
PyErr_SetString(PyExc_ValueError, "write_size must be positive");
return NULL;
}
result = (ZstdDecompressionObj *)PyObject_CallObject(
(PyObject *)ZstdDecompressionObjType, NULL);
if (!result) {
return NULL;
}
if (ensure_dctx(self, 1)) {
Py_DECREF(result);
return NULL;
}
result->decompressor = self;
Py_INCREF(result->decompressor);
result->outSize = outSize;
result->readAcrossFrames =
readAcrossFrames ? PyObject_IsTrue(readAcrossFrames) : 0;
return result;
}
static ZstdDecompressorIterator *
Decompressor_read_to_iter(ZstdDecompressor *self, PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"reader", "read_size", "write_size", "skip_bytes",
NULL};
PyObject *reader;
size_t inSize = ZSTD_DStreamInSize();
size_t outSize = ZSTD_DStreamOutSize();
ZstdDecompressorIterator *result;
size_t skipBytes = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|kkk:read_to_iter", kwlist,
&reader, &inSize, &outSize, &skipBytes)) {
return NULL;
}
if (skipBytes >= inSize) {
PyErr_SetString(PyExc_ValueError,
"skip_bytes must be smaller than read_size");
return NULL;
}
result = (ZstdDecompressorIterator *)PyObject_CallObject(
(PyObject *)ZstdDecompressorIteratorType, NULL);
if (!result) {
return NULL;
}
if (PyObject_HasAttrString(reader, "read")) {
result->reader = reader;
Py_INCREF(result->reader);
}
else if (1 == PyObject_CheckBuffer(reader)) {
/* Object claims it is a buffer. Try to get a handle to it. */
if (0 != PyObject_GetBuffer(reader, &result->buffer, PyBUF_CONTIG_RO)) {
goto except;
}
}
else {
PyErr_SetString(PyExc_ValueError,
"must pass an object with a read() method or conforms "
"to buffer protocol");
goto except;
}
result->decompressor = self;
Py_INCREF(result->decompressor);
result->inSize = inSize;
result->outSize = outSize;
result->skipBytes = skipBytes;
if (ensure_dctx(self, 1)) {
goto except;
}
result->input.src = PyMem_Malloc(inSize);
if (!result->input.src) {
PyErr_NoMemory();
goto except;
}
goto finally;
except:
Py_CLEAR(result);
finally:
return result;
}
static ZstdDecompressionReader *
Decompressor_stream_reader(ZstdDecompressor *self, PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"source", "read_size", "read_across_frames",
"closefd", NULL};
PyObject *source;
size_t readSize = ZSTD_DStreamInSize();
PyObject *readAcrossFrames = NULL;
PyObject *closefd = NULL;
ZstdDecompressionReader *result;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|kOO:stream_reader",
kwlist, &source, &readSize,
&readAcrossFrames, &closefd)) {
return NULL;
}
if (ensure_dctx(self, 1)) {
return NULL;
}
result = (ZstdDecompressionReader *)PyObject_CallObject(
(PyObject *)ZstdDecompressionReaderType, NULL);
if (NULL == result) {
return NULL;
}
result->entered = 0;
result->closed = 0;
if (PyObject_HasAttrString(source, "read")) {
result->reader = source;
Py_INCREF(source);
result->readSize = readSize;
}
else if (1 == PyObject_CheckBuffer(source)) {
if (0 != PyObject_GetBuffer(source, &result->buffer, PyBUF_CONTIG_RO)) {
Py_CLEAR(result);
return NULL;
}
}
else {
PyErr_SetString(PyExc_TypeError,
"must pass an object with a read() method or that "
"conforms to the buffer protocol");
Py_CLEAR(result);
return NULL;
}
result->decompressor = self;
Py_INCREF(self);
result->readAcrossFrames =
readAcrossFrames ? PyObject_IsTrue(readAcrossFrames) : 0;
result->closefd = closefd ? PyObject_IsTrue(closefd) : 1;
return result;
}
static ZstdDecompressionWriter *
Decompressor_stream_writer(ZstdDecompressor *self, PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"writer", "write_size", "write_return_read",
"closefd", NULL};
PyObject *writer;
size_t outSize = ZSTD_DStreamOutSize();
PyObject *writeReturnRead = NULL;
PyObject *closefd = NULL;
ZstdDecompressionWriter *result;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|kOO:stream_writer",
kwlist, &writer, &outSize,
&writeReturnRead, &closefd)) {
return NULL;
}
if (!PyObject_HasAttrString(writer, "write")) {
PyErr_SetString(PyExc_ValueError,
"must pass an object with a write() method");
return NULL;
}
if (ensure_dctx(self, 1)) {
return NULL;
}
result = (ZstdDecompressionWriter *)PyObject_CallObject(
(PyObject *)ZstdDecompressionWriterType, NULL);
if (!result) {
return NULL;
}
result->entered = 0;
result->closing = 0;
result->closed = 0;
result->decompressor = self;
Py_INCREF(result->decompressor);
result->writer = writer;
Py_INCREF(result->writer);
result->outSize = outSize;
result->writeReturnRead =
writeReturnRead ? PyObject_IsTrue(writeReturnRead) : 1;
result->closefd = closefd ? PyObject_IsTrue(closefd) : 1;
return result;
}
static PyObject *
Decompressor_decompress_content_dict_chain(ZstdDecompressor *self,
PyObject *args, PyObject *kwargs) {
static char *kwlist[] = {"frames", NULL};
PyObject *chunks;
Py_ssize_t chunksLen;
Py_ssize_t chunkIndex;
char parity = 0;
PyObject *chunk;
char *chunkData;
Py_ssize_t chunkSize;
size_t zresult;
ZSTD_frameHeader frameHeader;
void *buffer1 = NULL;
size_t buffer1Size = 0;
size_t buffer1ContentSize = 0;
void *buffer2 = NULL;
size_t buffer2Size = 0;
size_t buffer2ContentSize = 0;
void *destBuffer = NULL;
PyObject *result = NULL;
ZSTD_outBuffer outBuffer;
ZSTD_inBuffer inBuffer;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O!:decompress_content_dict_chain", kwlist,
&PyList_Type, &chunks)) {
return NULL;
}
chunksLen = PyList_Size(chunks);
if (!chunksLen) {
PyErr_SetString(PyExc_ValueError, "empty input chain");
return NULL;
}
/* The first chunk should not be using a dictionary. We handle it specially.
*/
chunk = PyList_GetItem(chunks, 0);
if (!PyBytes_Check(chunk)) {
PyErr_SetString(PyExc_ValueError, "chunk 0 must be bytes");
return NULL;
}
/* We require that all chunks be zstd frames and that they have content size
* set. */
PyBytes_AsStringAndSize(chunk, &chunkData, &chunkSize);
zresult = ZSTD_getFrameHeader(&frameHeader, (void *)chunkData, chunkSize);
if (ZSTD_isError(zresult)) {
PyErr_SetString(PyExc_ValueError, "chunk 0 is not a valid zstd frame");
return NULL;
}
else if (zresult) {
PyErr_SetString(PyExc_ValueError,
"chunk 0 is too small to contain a zstd frame");
return NULL;
}
if (ZSTD_CONTENTSIZE_UNKNOWN == frameHeader.frameContentSize) {
PyErr_SetString(PyExc_ValueError,
"chunk 0 missing content size in frame");
return NULL;
}
assert(ZSTD_CONTENTSIZE_ERROR != frameHeader.frameContentSize);
/* We check against PY_SSIZE_T_MAX here because we ultimately cast the
* result to a Python object and it's length can be no greater than
* Py_ssize_t. In theory, we could have an intermediate frame that is
* larger. But a) why would this API be used for frames that large b)
* it isn't worth the complexity to support. */
assert(SIZE_MAX >= PY_SSIZE_T_MAX);
if (frameHeader.frameContentSize > PY_SSIZE_T_MAX) {
PyErr_SetString(PyExc_ValueError,
"chunk 0 is too large to decompress on this platform");
return NULL;
}
if (ensure_dctx(self, 0)) {
goto finally;
}
buffer1Size = (size_t)frameHeader.frameContentSize;
buffer1 = PyMem_Malloc(buffer1Size);
if (!buffer1) {
goto finally;
}
outBuffer.dst = buffer1;
outBuffer.size = buffer1Size;
outBuffer.pos = 0;
inBuffer.src = chunkData;
inBuffer.size = chunkSize;
inBuffer.pos = 0;
Py_BEGIN_ALLOW_THREADS zresult =
ZSTD_decompressStream(self->dctx, &outBuffer, &inBuffer);
Py_END_ALLOW_THREADS if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "could not decompress chunk 0: %s",
ZSTD_getErrorName(zresult));
goto finally;
}
else if (zresult) {
PyErr_Format(ZstdError, "chunk 0 did not decompress full frame");
goto finally;
}
buffer1ContentSize = outBuffer.pos;
/* Special case of a simple chain. */
if (1 == chunksLen) {
result = PyBytes_FromStringAndSize(buffer1, buffer1Size);
goto finally;
}
/* This should ideally look at next chunk. But this is slightly simpler. */
buffer2Size = (size_t)frameHeader.frameContentSize;
buffer2 = PyMem_Malloc(buffer2Size);
if (!buffer2) {
goto finally;
}
/* For each subsequent chunk, use the previous fulltext as a content
dictionary. Our strategy is to have 2 buffers. One holds the previous
fulltext (to be used as a content dictionary) and the other holds the new
fulltext. The buffers grow when needed but never decrease in size. This
limits the memory allocator overhead.
*/
for (chunkIndex = 1; chunkIndex < chunksLen; chunkIndex++) {
chunk = PyList_GetItem(chunks, chunkIndex);
if (!PyBytes_Check(chunk)) {
PyErr_Format(PyExc_ValueError, "chunk %zd must be bytes",
chunkIndex);
goto finally;
}
PyBytes_AsStringAndSize(chunk, &chunkData, &chunkSize);
zresult =
ZSTD_getFrameHeader(&frameHeader, (void *)chunkData, chunkSize);
if (ZSTD_isError(zresult)) {
PyErr_Format(PyExc_ValueError,
"chunk %zd is not a valid zstd frame", chunkIndex);
goto finally;
}
else if (zresult) {
PyErr_Format(PyExc_ValueError,
"chunk %zd is too small to contain a zstd frame",
chunkIndex);
goto finally;
}
if (ZSTD_CONTENTSIZE_UNKNOWN == frameHeader.frameContentSize) {
PyErr_Format(PyExc_ValueError,
"chunk %zd missing content size in frame", chunkIndex);
goto finally;
}
assert(ZSTD_CONTENTSIZE_ERROR != frameHeader.frameContentSize);
if (frameHeader.frameContentSize > PY_SSIZE_T_MAX) {
PyErr_Format(
PyExc_ValueError,
"chunk %zd is too large to decompress on this platform",
chunkIndex);
goto finally;
}
inBuffer.src = chunkData;
inBuffer.size = chunkSize;
inBuffer.pos = 0;
parity = chunkIndex % 2;
/* This could definitely be abstracted to reduce code duplication. */
if (parity) {
/* Resize destination buffer to hold larger content. */
if (buffer2Size < frameHeader.frameContentSize) {
buffer2Size = (size_t)frameHeader.frameContentSize;
destBuffer = PyMem_Realloc(buffer2, buffer2Size);
if (!destBuffer) {
goto finally;
}
buffer2 = destBuffer;
}
Py_BEGIN_ALLOW_THREADS zresult = ZSTD_DCtx_refPrefix_advanced(
self->dctx, buffer1, buffer1ContentSize, ZSTD_dct_rawContent);
Py_END_ALLOW_THREADS if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError,
"failed to load prefix dictionary at chunk %zd",
chunkIndex);
goto finally;
}
outBuffer.dst = buffer2;
outBuffer.size = buffer2Size;
outBuffer.pos = 0;
Py_BEGIN_ALLOW_THREADS zresult =
ZSTD_decompressStream(self->dctx, &outBuffer, &inBuffer);
Py_END_ALLOW_THREADS if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "could not decompress chunk %zd: %s",
chunkIndex, ZSTD_getErrorName(zresult));
goto finally;
}
else if (zresult) {
PyErr_Format(ZstdError,
"chunk %zd did not decompress full frame",
chunkIndex);
goto finally;
}
buffer2ContentSize = outBuffer.pos;
}
else {
if (buffer1Size < frameHeader.frameContentSize) {
buffer1Size = (size_t)frameHeader.frameContentSize;
destBuffer = PyMem_Realloc(buffer1, buffer1Size);
if (!destBuffer) {
goto finally;
}
buffer1 = destBuffer;
}
Py_BEGIN_ALLOW_THREADS zresult = ZSTD_DCtx_refPrefix_advanced(
self->dctx, buffer2, buffer2ContentSize, ZSTD_dct_rawContent);
Py_END_ALLOW_THREADS if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError,
"failed to load prefix dictionary at chunk %zd",
chunkIndex);
goto finally;
}
outBuffer.dst = buffer1;
outBuffer.size = buffer1Size;
outBuffer.pos = 0;
Py_BEGIN_ALLOW_THREADS zresult =
ZSTD_decompressStream(self->dctx, &outBuffer, &inBuffer);
Py_END_ALLOW_THREADS if (ZSTD_isError(zresult)) {
PyErr_Format(ZstdError, "could not decompress chunk %zd: %s",
chunkIndex, ZSTD_getErrorName(zresult));
goto finally;
}
else if (zresult) {
PyErr_Format(ZstdError,
"chunk %zd did not decompress full frame",
chunkIndex);
goto finally;
}
buffer1ContentSize = outBuffer.pos;
}
}
result = PyBytes_FromStringAndSize(parity ? buffer2 : buffer1,
parity ? buffer2ContentSize
: buffer1ContentSize);
finally:
if (buffer2) {
PyMem_Free(buffer2);
}
if (buffer1) {
PyMem_Free(buffer1);
}
return result;
}
typedef struct {
void *sourceData;
size_t sourceSize;
size_t destSize;
} FramePointer;
typedef struct {
FramePointer *frames;
Py_ssize_t framesSize;
unsigned long long compressedSize;
} FrameSources;
typedef struct {
void *dest;
Py_ssize_t destSize;
BufferSegment *segments;
Py_ssize_t segmentsSize;
} DecompressorDestBuffer;
typedef enum {
DecompressorWorkerError_none = 0,
DecompressorWorkerError_zstd = 1,
DecompressorWorkerError_memory = 2,
DecompressorWorkerError_sizeMismatch = 3,
DecompressorWorkerError_unknownSize = 4,
} DecompressorWorkerError;
typedef struct {
/* Source records and length */
FramePointer *framePointers;
/* Which records to process. */
Py_ssize_t startOffset;
Py_ssize_t endOffset;
unsigned long long totalSourceSize;
/* Compression state and settings. */
ZSTD_DCtx *dctx;
int requireOutputSizes;
/* Output storage. */
DecompressorDestBuffer *destBuffers;
Py_ssize_t destCount;
/* Item that error occurred on. */
Py_ssize_t errorOffset;
/* If an error occurred. */
DecompressorWorkerError error;
/* result from zstd decompression operation */
size_t zresult;
} DecompressorWorkerState;
#ifdef HAVE_ZSTD_POOL_APIS
static void decompress_worker(DecompressorWorkerState *state) {
size_t allocationSize;
DecompressorDestBuffer *destBuffer;
Py_ssize_t frameIndex;
Py_ssize_t localOffset = 0;
Py_ssize_t currentBufferStartIndex = state->startOffset;
Py_ssize_t remainingItems = state->endOffset - state->startOffset + 1;
void *tmpBuf;
Py_ssize_t destOffset = 0;
FramePointer *framePointers = state->framePointers;
size_t zresult;
assert(NULL == state->destBuffers);
assert(0 == state->destCount);
assert(state->endOffset - state->startOffset >= 0);
/* We could get here due to the way work is allocated. Ideally we wouldn't
get here. But that would require a bit of a refactor in the caller. */
if (state->totalSourceSize > SIZE_MAX) {
state->error = DecompressorWorkerError_memory;
state->errorOffset = 0;
return;
}
/*
* We need to allocate a buffer to hold decompressed data. How we do this
* depends on what we know about the output. The following scenarios are
* possible:
*
* 1. All structs defining frames declare the output size.
* 2. The decompressed size is embedded within the zstd frame.
* 3. The decompressed size is not stored anywhere.
*
* For now, we only support #1 and #2.
*/
/* Resolve ouput segments. */
for (frameIndex = state->startOffset; frameIndex <= state->endOffset;
frameIndex++) {
FramePointer *fp = &framePointers[frameIndex];
unsigned long long decompressedSize;
if (0 == fp->destSize) {
decompressedSize =
ZSTD_getFrameContentSize(fp->sourceData, fp->sourceSize);
if (ZSTD_CONTENTSIZE_ERROR == decompressedSize) {
state->error = DecompressorWorkerError_unknownSize;
state->errorOffset = frameIndex;
return;
}
else if (ZSTD_CONTENTSIZE_UNKNOWN == decompressedSize) {
if (state->requireOutputSizes) {
state->error = DecompressorWorkerError_unknownSize;
state->errorOffset = frameIndex;
return;
}
/* This will fail the assert for .destSize > 0 below. */
decompressedSize = 0;
}
if (decompressedSize > SIZE_MAX) {
state->error = DecompressorWorkerError_memory;
state->errorOffset = frameIndex;
return;
}
fp->destSize = (size_t)decompressedSize;
}
}
state->destBuffers = calloc(1, sizeof(DecompressorDestBuffer));
if (NULL == state->destBuffers) {
state->error = DecompressorWorkerError_memory;
return;
}
state->destCount = 1;
destBuffer = &state->destBuffers[state->destCount - 1];
assert(framePointers[state->startOffset].destSize > 0); /* For now. */
allocationSize = roundpow2((size_t)state->totalSourceSize);
if (framePointers[state->startOffset].destSize > allocationSize) {
allocationSize = roundpow2(framePointers[state->startOffset].destSize);
}
destBuffer->dest = malloc(allocationSize);
if (NULL == destBuffer->dest) {
state->error = DecompressorWorkerError_memory;
return;
}
destBuffer->destSize = allocationSize;
destBuffer->segments = calloc(remainingItems, sizeof(BufferSegment));
if (NULL == destBuffer->segments) {
/* Caller will free state->dest as part of cleanup. */
state->error = DecompressorWorkerError_memory;
return;
}
destBuffer->segmentsSize = remainingItems;
for (frameIndex = state->startOffset; frameIndex <= state->endOffset;
frameIndex++) {
ZSTD_outBuffer outBuffer;
ZSTD_inBuffer inBuffer;
const void *source = framePointers[frameIndex].sourceData;
const size_t sourceSize = framePointers[frameIndex].sourceSize;
void *dest;
const size_t decompressedSize = framePointers[frameIndex].destSize;
size_t destAvailable = destBuffer->destSize - destOffset;
assert(decompressedSize > 0); /* For now. */
/*
* Not enough space in current buffer. Finish current before and
* allocate and switch to a new one.
*/
if (decompressedSize > destAvailable) {
/*
* Shrinking the destination buffer is optional. But it should be
* cheap, so we just do it.
*/
if (destAvailable) {
tmpBuf = realloc(destBuffer->dest, destOffset);
if (NULL == tmpBuf) {
state->error = DecompressorWorkerError_memory;
return;
}
destBuffer->dest = tmpBuf;
destBuffer->destSize = destOffset;
}
/* Truncate segments buffer. */
tmpBuf = realloc(destBuffer->segments,
(frameIndex - currentBufferStartIndex) *
sizeof(BufferSegment));
if (NULL == tmpBuf) {
state->error = DecompressorWorkerError_memory;
return;
}
destBuffer->segments = tmpBuf;
destBuffer->segmentsSize = frameIndex - currentBufferStartIndex;
/* Grow space for new DestBuffer. */
tmpBuf =
realloc(state->destBuffers, (state->destCount + 1) *
sizeof(DecompressorDestBuffer));
if (NULL == tmpBuf) {
state->error = DecompressorWorkerError_memory;
return;
}
state->destBuffers = tmpBuf;
state->destCount++;
destBuffer = &state->destBuffers[state->destCount - 1];
/* Don't take any chances will non-NULL pointers. */
memset(destBuffer, 0, sizeof(DecompressorDestBuffer));
allocationSize = roundpow2((size_t)state->totalSourceSize);
if (decompressedSize > allocationSize) {
allocationSize = roundpow2(decompressedSize);
}
destBuffer->dest = malloc(allocationSize);
if (NULL == destBuffer->dest) {
state->error = DecompressorWorkerError_memory;
return;
}
destBuffer->destSize = allocationSize;
destAvailable = allocationSize;
destOffset = 0;
localOffset = 0;
destBuffer->segments =
calloc(remainingItems, sizeof(BufferSegment));
if (NULL == destBuffer->segments) {
state->error = DecompressorWorkerError_memory;
return;
}
destBuffer->segmentsSize = remainingItems;
currentBufferStartIndex = frameIndex;
}
dest = (char *)destBuffer->dest + destOffset;
outBuffer.dst = dest;
outBuffer.size = decompressedSize;
outBuffer.pos = 0;
inBuffer.src = source;
inBuffer.size = sourceSize;
inBuffer.pos = 0;
zresult = ZSTD_decompressStream(state->dctx, &outBuffer, &inBuffer);
if (ZSTD_isError(zresult)) {
state->error = DecompressorWorkerError_zstd;
state->zresult = zresult;
state->errorOffset = frameIndex;
return;
}
else if (zresult || outBuffer.pos != decompressedSize) {
state->error = DecompressorWorkerError_sizeMismatch;
state->zresult = outBuffer.pos;
state->errorOffset = frameIndex;
return;
}
destBuffer->segments[localOffset].offset = destOffset;
destBuffer->segments[localOffset].length = outBuffer.pos;
destOffset += outBuffer.pos;
localOffset++;
remainingItems--;
}
if (destBuffer->destSize > destOffset) {
tmpBuf = realloc(destBuffer->dest, destOffset);
if (NULL == tmpBuf) {
state->error = DecompressorWorkerError_memory;
return;
}
destBuffer->dest = tmpBuf;
destBuffer->destSize = destOffset;
}
}
#endif
#ifdef HAVE_ZSTD_POOL_APIS
ZstdBufferWithSegmentsCollection *
decompress_from_framesources(ZstdDecompressor *decompressor,
FrameSources *frames, Py_ssize_t threadCount) {
Py_ssize_t i = 0;
int errored = 0;
Py_ssize_t segmentsCount;
ZstdBufferWithSegments *bws = NULL;
PyObject *resultArg = NULL;
Py_ssize_t resultIndex;
ZstdBufferWithSegmentsCollection *result = NULL;
FramePointer *framePointers = frames->frames;
unsigned long long workerBytes = 0;
Py_ssize_t currentThread = 0;
Py_ssize_t workerStartOffset = 0;
POOL_ctx *pool = NULL;
DecompressorWorkerState *workerStates = NULL;
unsigned long long bytesPerWorker;
/* Caller should normalize 0 and negative values to 1 or larger. */
assert(threadCount >= 1);
/* More threads than inputs makes no sense under any conditions. */
threadCount =
frames->framesSize < threadCount ? frames->framesSize : threadCount;
/* TODO lower thread count if input size is too small and threads would just
add overhead. */
if (decompressor->dict) {
if (ensure_ddict(decompressor->dict)) {
return NULL;
}
}
/* If threadCount==1, we don't start a thread pool. But we do leverage the
same API for dispatching work. */
workerStates = PyMem_Malloc(threadCount * sizeof(DecompressorWorkerState));
if (NULL == workerStates) {
PyErr_NoMemory();
goto finally;
}
memset(workerStates, 0, threadCount * sizeof(DecompressorWorkerState));
if (threadCount > 1) {
pool = POOL_create(threadCount, 1);
if (NULL == pool) {
PyErr_SetString(ZstdError, "could not initialize zstd thread pool");
goto finally;
}
}
bytesPerWorker = frames->compressedSize / threadCount;
if (bytesPerWorker > SIZE_MAX) {
PyErr_SetString(ZstdError,
"too much data per worker for this platform");
goto finally;
}
for (i = 0; i < threadCount; i++) {
size_t zresult;
workerStates[i].dctx = ZSTD_createDCtx();
if (NULL == workerStates[i].dctx) {
PyErr_NoMemory();
goto finally;
}
if (decompressor->dict) {
zresult = ZSTD_DCtx_refDDict(workerStates[i].dctx,
decompressor->dict->ddict);
if (zresult) {
PyErr_Format(ZstdError,
"unable to reference prepared dictionary: %s",
ZSTD_getErrorName(zresult));
goto finally;
}
}
workerStates[i].framePointers = framePointers;
workerStates[i].requireOutputSizes = 1;
}
Py_BEGIN_ALLOW_THREADS
/* There are many ways to split work among workers.
For now, we take a simple approach of splitting work so each worker
gets roughly the same number of input bytes. This will result in more
starvation than running N>threadCount jobs. But it avoids
complications around state tracking, which could involve extra
locking.
*/
for (i = 0; i < frames->framesSize; i++) {
workerBytes += frames->frames[i].sourceSize;
/*
* The last worker/thread needs to handle all remaining work. Don't
* trigger it prematurely. Defer to the block outside of the loop.
* (But still process this loop so workerBytes is correct.
*/
if (currentThread == threadCount - 1) {
continue;
}
if (workerBytes >= bytesPerWorker) {
workerStates[currentThread].startOffset = workerStartOffset;
workerStates[currentThread].endOffset = i;
workerStates[currentThread].totalSourceSize = workerBytes;
if (threadCount > 1) {
POOL_add(pool, (POOL_function)decompress_worker,
&workerStates[currentThread]);
}
else {
decompress_worker(&workerStates[currentThread]);
}
currentThread++;
workerStartOffset = i + 1;
workerBytes = 0;
}
}
if (workerBytes) {
workerStates[currentThread].startOffset = workerStartOffset;
workerStates[currentThread].endOffset = frames->framesSize - 1;
workerStates[currentThread].totalSourceSize = workerBytes;
if (threadCount > 1) {
POOL_add(pool, (POOL_function)decompress_worker,
&workerStates[currentThread]);
}
else {
decompress_worker(&workerStates[currentThread]);
}
}
if (threadCount > 1) {
POOL_free(pool);
pool = NULL;
}
Py_END_ALLOW_THREADS
for (i = 0; i < threadCount; i++) {
switch (workerStates[i].error) {
case DecompressorWorkerError_none:
break;
case DecompressorWorkerError_zstd:
PyErr_Format(ZstdError, "error decompressing item %zd: %s",
workerStates[i].errorOffset,
ZSTD_getErrorName(workerStates[i].zresult));
errored = 1;
break;
case DecompressorWorkerError_memory:
PyErr_NoMemory();
errored = 1;
break;
case DecompressorWorkerError_sizeMismatch:
PyErr_Format(ZstdError,
"error decompressing item %zd: decompressed %zu "
"bytes; expected %zu",
workerStates[i].errorOffset, workerStates[i].zresult,
framePointers[workerStates[i].errorOffset].destSize);
errored = 1;
break;
case DecompressorWorkerError_unknownSize:
PyErr_Format(PyExc_ValueError,
"could not determine decompressed size of item %zd",
workerStates[i].errorOffset);
errored = 1;
break;
default:
PyErr_Format(ZstdError, "unhandled error type: %d; this is a bug",
workerStates[i].error);
errored = 1;
break;
}
if (errored) {
break;
}
}
if (errored) {
goto finally;
}
segmentsCount = 0;
for (i = 0; i < threadCount; i++) {
segmentsCount += workerStates[i].destCount;
}
resultArg = PyTuple_New(segmentsCount);
if (NULL == resultArg) {
goto finally;
}
resultIndex = 0;
for (i = 0; i < threadCount; i++) {
Py_ssize_t bufferIndex;
DecompressorWorkerState *state = &workerStates[i];
for (bufferIndex = 0; bufferIndex < state->destCount; bufferIndex++) {
DecompressorDestBuffer *destBuffer =
&state->destBuffers[bufferIndex];
bws = BufferWithSegments_FromMemory(
destBuffer->dest, destBuffer->destSize, destBuffer->segments,
destBuffer->segmentsSize);
if (NULL == bws) {
goto finally;
}
/*
* Memory for buffer and segments was allocated using malloc() in
* worker and the memory is transferred to the BufferWithSegments
* instance. So tell instance to use free() and NULL the reference
* in the state struct so it isn't freed below.
*/
bws->useFree = 1;
destBuffer->dest = NULL;
destBuffer->segments = NULL;
PyTuple_SET_ITEM(resultArg, resultIndex++, (PyObject *)bws);
}
}
result = (ZstdBufferWithSegmentsCollection *)PyObject_CallObject(
(PyObject *)ZstdBufferWithSegmentsCollectionType, resultArg);
finally:
Py_CLEAR(resultArg);
if (workerStates) {
for (i = 0; i < threadCount; i++) {
Py_ssize_t bufferIndex;
DecompressorWorkerState *state = &workerStates[i];
if (state->dctx) {
ZSTD_freeDCtx(state->dctx);
}
for (bufferIndex = 0; bufferIndex < state->destCount;
bufferIndex++) {
if (state->destBuffers) {
/*
* Will be NULL if memory transfered to a
* BufferWithSegments. Otherwise it is left over after an
* error occurred.
*/
free(state->destBuffers[bufferIndex].dest);
free(state->destBuffers[bufferIndex].segments);
}
}
free(state->destBuffers);
}
PyMem_Free(workerStates);
}
POOL_free(pool);
return result;
}
#endif
#ifdef HAVE_ZSTD_POOL_APIS
static ZstdBufferWithSegmentsCollection *
Decompressor_multi_decompress_to_buffer(ZstdDecompressor *self, PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"frames", "decompressed_sizes", "threads", NULL};
PyObject *frames;
Py_buffer frameSizes;
int threads = 0;
Py_ssize_t frameCount;
Py_buffer *frameBuffers = NULL;
FramePointer *framePointers = NULL;
unsigned long long *frameSizesP = NULL;
unsigned long long totalInputSize = 0;
FrameSources frameSources;
ZstdBufferWithSegmentsCollection *result = NULL;
Py_ssize_t i;
memset(&frameSizes, 0, sizeof(frameSizes));
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O|y*i:multi_decompress_to_buffer", kwlist,
&frames, &frameSizes, &threads)) {
return NULL;
}
if (frameSizes.buf) {
frameSizesP = (unsigned long long *)frameSizes.buf;
}
if (threads < 0) {
threads = cpu_count();
}
if (threads < 2) {
threads = 1;
}
if (PyObject_TypeCheck(frames, ZstdBufferWithSegmentsType)) {
ZstdBufferWithSegments *buffer = (ZstdBufferWithSegments *)frames;
frameCount = buffer->segmentCount;
if (frameSizes.buf &&
frameSizes.len !=
frameCount * (Py_ssize_t)sizeof(unsigned long long)) {
PyErr_Format(
PyExc_ValueError,
"decompressed_sizes size mismatch; expected %zd, got %zd",
frameCount * sizeof(unsigned long long), frameSizes.len);
goto finally;
}
framePointers = PyMem_Malloc(frameCount * sizeof(FramePointer));
if (!framePointers) {
PyErr_NoMemory();
goto finally;
}
for (i = 0; i < frameCount; i++) {
void *sourceData;
unsigned long long sourceSize;
unsigned long long decompressedSize = 0;
if (buffer->segments[i].offset + buffer->segments[i].length >
buffer->dataSize) {
PyErr_Format(PyExc_ValueError,
"item %zd has offset outside memory area", i);
goto finally;
}
sourceData = (char *)buffer->data + buffer->segments[i].offset;
sourceSize = buffer->segments[i].length;
totalInputSize += sourceSize;
if (frameSizesP) {
decompressedSize = frameSizesP[i];
}
if (sourceSize > SIZE_MAX) {
PyErr_Format(PyExc_ValueError,
"item %zd is too large for this platform", i);
goto finally;
}
if (decompressedSize > SIZE_MAX) {
PyErr_Format(PyExc_ValueError,
"decompressed size of item %zd is too large for "
"this platform",
i);
goto finally;
}
framePointers[i].sourceData = sourceData;
framePointers[i].sourceSize = (size_t)sourceSize;
framePointers[i].destSize = (size_t)decompressedSize;
}
}
else if (PyObject_TypeCheck(frames, ZstdBufferWithSegmentsCollectionType)) {
Py_ssize_t offset = 0;
ZstdBufferWithSegments *buffer;
ZstdBufferWithSegmentsCollection *collection =
(ZstdBufferWithSegmentsCollection *)frames;
frameCount = BufferWithSegmentsCollection_length(collection);
if (frameSizes.buf && frameSizes.len != frameCount) {
PyErr_Format(
PyExc_ValueError,
"decompressed_sizes size mismatch; expected %zd; got %zd",
frameCount * sizeof(unsigned long long), frameSizes.len);
goto finally;
}
framePointers = PyMem_Malloc(frameCount * sizeof(FramePointer));
if (NULL == framePointers) {
PyErr_NoMemory();
goto finally;
}
/* Iterate the data structure directly because it is faster. */
for (i = 0; i < collection->bufferCount; i++) {
Py_ssize_t segmentIndex;
buffer = collection->buffers[i];
for (segmentIndex = 0; segmentIndex < buffer->segmentCount;
segmentIndex++) {
unsigned long long decompressedSize =
frameSizesP ? frameSizesP[offset] : 0;
if (buffer->segments[segmentIndex].offset +
buffer->segments[segmentIndex].length >
buffer->dataSize) {
PyErr_Format(PyExc_ValueError,
"item %zd has offset outside memory area",
offset);
goto finally;
}
if (buffer->segments[segmentIndex].length > SIZE_MAX) {
PyErr_Format(
PyExc_ValueError,
"item %zd in buffer %zd is too large for this platform",
segmentIndex, i);
goto finally;
}
if (decompressedSize > SIZE_MAX) {
PyErr_Format(PyExc_ValueError,
"decompressed size of item %zd in buffer %zd "
"is too large for this platform",
segmentIndex, i);
goto finally;
}
totalInputSize += buffer->segments[segmentIndex].length;
framePointers[offset].sourceData =
(char *)buffer->data +
buffer->segments[segmentIndex].offset;
framePointers[offset].sourceSize =
(size_t)buffer->segments[segmentIndex].length;
framePointers[offset].destSize = (size_t)decompressedSize;
offset++;
}
}
}
else if (PyList_Check(frames)) {
frameCount = PyList_GET_SIZE(frames);
if (frameSizes.buf &&
frameSizes.len !=
frameCount * (Py_ssize_t)sizeof(unsigned long long)) {
PyErr_Format(
PyExc_ValueError,
"decompressed_sizes size mismatch; expected %zd, got %zd",
frameCount * sizeof(unsigned long long), frameSizes.len);
goto finally;
}
framePointers = PyMem_Malloc(frameCount * sizeof(FramePointer));
if (!framePointers) {
PyErr_NoMemory();
goto finally;
}
frameBuffers = PyMem_Malloc(frameCount * sizeof(Py_buffer));
if (NULL == frameBuffers) {
PyErr_NoMemory();
goto finally;
}
memset(frameBuffers, 0, frameCount * sizeof(Py_buffer));
/* Do a pass to assemble info about our input buffers and output sizes.
*/
for (i = 0; i < frameCount; i++) {
unsigned long long decompressedSize =
frameSizesP ? frameSizesP[i] : 0;
if (0 != PyObject_GetBuffer(PyList_GET_ITEM(frames, i),
&frameBuffers[i], PyBUF_CONTIG_RO)) {
PyErr_Clear();
PyErr_Format(PyExc_TypeError,
"item %zd not a bytes like object", i);
goto finally;
}
if (decompressedSize > SIZE_MAX) {
PyErr_Format(PyExc_ValueError,
"decompressed size of item %zd is too large for "
"this platform",
i);
goto finally;
}
totalInputSize += frameBuffers[i].len;
framePointers[i].sourceData = frameBuffers[i].buf;
framePointers[i].sourceSize = frameBuffers[i].len;
framePointers[i].destSize = (size_t)decompressedSize;
}
}
else {
PyErr_SetString(PyExc_TypeError,
"argument must be list or BufferWithSegments");
goto finally;
}
/* We now have an array with info about our inputs and outputs. Feed it into
our generic decompression function. */
frameSources.frames = framePointers;
frameSources.framesSize = frameCount;
frameSources.compressedSize = totalInputSize;
result = decompress_from_framesources(self, &frameSources, threads);
finally:
if (frameSizes.buf) {
PyBuffer_Release(&frameSizes);
}
PyMem_Free(framePointers);
if (frameBuffers) {
for (i = 0; i < frameCount; i++) {
PyBuffer_Release(&frameBuffers[i]);
}
PyMem_Free(frameBuffers);
}
return result;
}
#endif
static PyMethodDef Decompressor_methods[] = {
{"copy_stream", (PyCFunction)Decompressor_copy_stream,
METH_VARARGS | METH_KEYWORDS, NULL},
{"decompress", (PyCFunction)Decompressor_decompress,
METH_VARARGS | METH_KEYWORDS, NULL},
{"decompressobj", (PyCFunction)Decompressor_decompressobj,
METH_VARARGS | METH_KEYWORDS, NULL},
{"read_to_iter", (PyCFunction)Decompressor_read_to_iter,
METH_VARARGS | METH_KEYWORDS, NULL},
{"stream_reader", (PyCFunction)Decompressor_stream_reader,
METH_VARARGS | METH_KEYWORDS, NULL},
{"stream_writer", (PyCFunction)Decompressor_stream_writer,
METH_VARARGS | METH_KEYWORDS, NULL},
{"decompress_content_dict_chain",
(PyCFunction)Decompressor_decompress_content_dict_chain,
METH_VARARGS | METH_KEYWORDS, NULL},
#ifdef HAVE_ZSTD_POOL_APIS
{"multi_decompress_to_buffer",
(PyCFunction)Decompressor_multi_decompress_to_buffer,
METH_VARARGS | METH_KEYWORDS, NULL},
#endif
{"memory_size", (PyCFunction)Decompressor_memory_size, METH_NOARGS, NULL},
{NULL, NULL}};
PyType_Slot ZstdDecompressorSlots[] = {
{Py_tp_dealloc, Decompressor_dealloc},
{Py_tp_methods, Decompressor_methods},
{Py_tp_init, Decompressor_init},
{Py_tp_new, PyType_GenericNew},
{0, NULL},
};
PyType_Spec ZstdDecompressorSpec = {
"zstd.ZstdDecompressor",
sizeof(ZstdDecompressor),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
ZstdDecompressorSlots,
};
PyTypeObject *ZstdDecompressorType;
void decompressor_module_init(PyObject *mod) {
ZstdDecompressorType =
(PyTypeObject *)PyType_FromSpec(&ZstdDecompressorSpec);
if (PyType_Ready(ZstdDecompressorType) < 0) {
return;
}
Py_INCREF((PyObject *)ZstdDecompressorType);
PyModule_AddObject(mod, "ZstdDecompressor",
(PyObject *)ZstdDecompressorType);
}
|