zlibmodule.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  1. /* zlibmodule.c -- gzip-compatible data compression */
  2. /* See http://zlib.net/ */
  3. /* Windows users: read Python's PCbuild\readme.txt */
  4. #define PY_SSIZE_T_CLEAN
  5. #include "Python.h"
  6. #include "structmember.h" // PyMemberDef
  7. #include "zlib.h"
  8. #include "stdbool.h"
  9. #if defined(ZLIB_VERNUM) && ZLIB_VERNUM < 0x1221
  10. #error "At least zlib version 1.2.2.1 is required"
  11. #endif
  12. // Blocks output buffer wrappers
  13. #include "pycore_blocks_output_buffer.h"
  14. #if OUTPUT_BUFFER_MAX_BLOCK_SIZE > UINT32_MAX
  15. #error "The maximum block size accepted by zlib is UINT32_MAX."
  16. #endif
  17. /* On success, return value >= 0
  18. On failure, return -1 */
  19. static inline Py_ssize_t
  20. OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
  21. Bytef **next_out, uint32_t *avail_out)
  22. {
  23. Py_ssize_t allocated;
  24. allocated = _BlocksOutputBuffer_InitAndGrow(
  25. buffer, max_length, (void**) next_out);
  26. *avail_out = (uint32_t) allocated;
  27. return allocated;
  28. }
  29. /* On success, return value >= 0
  30. On failure, return -1 */
  31. static inline Py_ssize_t
  32. OutputBuffer_Grow(_BlocksOutputBuffer *buffer,
  33. Bytef **next_out, uint32_t *avail_out)
  34. {
  35. Py_ssize_t allocated;
  36. allocated = _BlocksOutputBuffer_Grow(
  37. buffer, (void**) next_out, (Py_ssize_t) *avail_out);
  38. *avail_out = (uint32_t) allocated;
  39. return allocated;
  40. }
  41. static inline Py_ssize_t
  42. OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
  43. {
  44. return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
  45. }
  46. static inline PyObject *
  47. OutputBuffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
  48. {
  49. return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
  50. }
  51. static inline void
  52. OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
  53. {
  54. _BlocksOutputBuffer_OnError(buffer);
  55. }
  56. /* The max buffer size accepted by zlib is UINT32_MAX, the initial buffer size
  57. `init_size` may > it in 64-bit build. These wrapper functions maintain an
  58. UINT32_MAX sliding window for the first block:
  59. 1. OutputBuffer_WindowInitWithSize()
  60. 2. OutputBuffer_WindowGrow()
  61. 3. OutputBuffer_WindowFinish()
  62. 4. OutputBuffer_WindowOnError()
  63. ==== is the sliding window:
  64. 1. ====------
  65. ^ next_posi, left_bytes is 6
  66. 2. ----====--
  67. ^ next_posi, left_bytes is 2
  68. 3. --------==
  69. ^ next_posi, left_bytes is 0 */
  70. typedef struct {
  71. Py_ssize_t left_bytes;
  72. Bytef *next_posi;
  73. } _Uint32Window;
  74. /* Initialize the buffer with an initial buffer size.
  75. On success, return value >= 0
  76. On failure, return value < 0 */
  77. static inline Py_ssize_t
  78. OutputBuffer_WindowInitWithSize(_BlocksOutputBuffer *buffer, _Uint32Window *window,
  79. Py_ssize_t init_size,
  80. Bytef **next_out, uint32_t *avail_out)
  81. {
  82. Py_ssize_t allocated = _BlocksOutputBuffer_InitWithSize(
  83. buffer, init_size, (void**) next_out);
  84. if (allocated >= 0) {
  85. // the UINT32_MAX sliding window
  86. Py_ssize_t window_size = Py_MIN((size_t)allocated, UINT32_MAX);
  87. *avail_out = (uint32_t) window_size;
  88. window->left_bytes = allocated - window_size;
  89. window->next_posi = *next_out + window_size;
  90. }
  91. return allocated;
  92. }
  93. /* Grow the buffer.
  94. On success, return value >= 0
  95. On failure, return value < 0 */
  96. static inline Py_ssize_t
  97. OutputBuffer_WindowGrow(_BlocksOutputBuffer *buffer, _Uint32Window *window,
  98. Bytef **next_out, uint32_t *avail_out)
  99. {
  100. Py_ssize_t allocated;
  101. /* ensure no gaps in the data.
  102. if inlined, this check could be optimized away.*/
  103. if (*avail_out != 0) {
  104. PyErr_SetString(PyExc_SystemError,
  105. "*avail_out != 0 in OutputBuffer_WindowGrow().");
  106. return -1;
  107. }
  108. // slide the UINT32_MAX sliding window
  109. if (window->left_bytes > 0) {
  110. Py_ssize_t window_size = Py_MIN((size_t)window->left_bytes, UINT32_MAX);
  111. *next_out = window->next_posi;
  112. *avail_out = (uint32_t) window_size;
  113. window->left_bytes -= window_size;
  114. window->next_posi += window_size;
  115. return window_size;
  116. }
  117. assert(window->left_bytes == 0);
  118. // only the first block may > UINT32_MAX
  119. allocated = _BlocksOutputBuffer_Grow(
  120. buffer, (void**) next_out, (Py_ssize_t) *avail_out);
  121. *avail_out = (uint32_t) allocated;
  122. return allocated;
  123. }
  124. /* Finish the buffer.
  125. On success, return a bytes object
  126. On failure, return NULL */
  127. static inline PyObject *
  128. OutputBuffer_WindowFinish(_BlocksOutputBuffer *buffer, _Uint32Window *window,
  129. uint32_t avail_out)
  130. {
  131. Py_ssize_t real_avail_out = (Py_ssize_t) avail_out + window->left_bytes;
  132. return _BlocksOutputBuffer_Finish(buffer, real_avail_out);
  133. }
  134. static inline void
  135. OutputBuffer_WindowOnError(_BlocksOutputBuffer *buffer, _Uint32Window *window)
  136. {
  137. _BlocksOutputBuffer_OnError(buffer);
  138. }
  139. #define ENTER_ZLIB(obj) do { \
  140. if (!PyThread_acquire_lock((obj)->lock, 0)) { \
  141. Py_BEGIN_ALLOW_THREADS \
  142. PyThread_acquire_lock((obj)->lock, 1); \
  143. Py_END_ALLOW_THREADS \
  144. } } while (0)
  145. #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
  146. /* The following parameters are copied from zutil.h, version 0.95 */
  147. #define DEFLATED 8
  148. #if MAX_MEM_LEVEL >= 8
  149. # define DEF_MEM_LEVEL 8
  150. #else
  151. # define DEF_MEM_LEVEL MAX_MEM_LEVEL
  152. #endif
  153. /* Initial buffer size. */
  154. #define DEF_BUF_SIZE (16*1024)
  155. #define DEF_MAX_INITIAL_BUF_SIZE (16 * 1024 * 1024)
  156. static PyModuleDef zlibmodule;
  157. typedef struct {
  158. PyTypeObject *Comptype;
  159. PyTypeObject *Decomptype;
  160. PyTypeObject *ZlibDecompressorType;
  161. PyObject *ZlibError;
  162. } zlibstate;
  163. static inline zlibstate*
  164. get_zlib_state(PyObject *module)
  165. {
  166. void *state = PyModule_GetState(module);
  167. assert(state != NULL);
  168. return (zlibstate *)state;
  169. }
  170. typedef struct
  171. {
  172. PyObject_HEAD
  173. z_stream zst;
  174. PyObject *unused_data;
  175. PyObject *unconsumed_tail;
  176. char eof;
  177. bool is_initialised;
  178. PyObject *zdict;
  179. PyThread_type_lock lock;
  180. } compobject;
  181. static void
  182. zlib_error(zlibstate *state, z_stream zst, int err, const char *msg)
  183. {
  184. const char *zmsg = Z_NULL;
  185. /* In case of a version mismatch, zst.msg won't be initialized.
  186. Check for this case first, before looking at zst.msg. */
  187. if (err == Z_VERSION_ERROR)
  188. zmsg = "library version mismatch";
  189. if (zmsg == Z_NULL)
  190. zmsg = zst.msg;
  191. if (zmsg == Z_NULL) {
  192. switch (err) {
  193. case Z_BUF_ERROR:
  194. zmsg = "incomplete or truncated stream";
  195. break;
  196. case Z_STREAM_ERROR:
  197. zmsg = "inconsistent stream state";
  198. break;
  199. case Z_DATA_ERROR:
  200. zmsg = "invalid input data";
  201. break;
  202. }
  203. }
  204. if (zmsg == Z_NULL)
  205. PyErr_Format(state->ZlibError, "Error %d %s", err, msg);
  206. else
  207. PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
  208. }
  209. /*[clinic input]
  210. module zlib
  211. class zlib.Compress "compobject *" "&Comptype"
  212. class zlib.Decompress "compobject *" "&Decomptype"
  213. [clinic start generated code]*/
  214. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
  215. static compobject *
  216. newcompobject(PyTypeObject *type)
  217. {
  218. compobject *self;
  219. self = PyObject_New(compobject, type);
  220. if (self == NULL)
  221. return NULL;
  222. self->eof = 0;
  223. self->is_initialised = 0;
  224. self->zdict = NULL;
  225. self->unused_data = PyBytes_FromStringAndSize("", 0);
  226. if (self->unused_data == NULL) {
  227. Py_DECREF(self);
  228. return NULL;
  229. }
  230. self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
  231. if (self->unconsumed_tail == NULL) {
  232. Py_DECREF(self);
  233. return NULL;
  234. }
  235. self->lock = PyThread_allocate_lock();
  236. if (self->lock == NULL) {
  237. Py_DECREF(self);
  238. PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
  239. return NULL;
  240. }
  241. return self;
  242. }
  243. static void*
  244. PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
  245. {
  246. if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
  247. return NULL;
  248. /* PyMem_Malloc() cannot be used: the GIL is not held when
  249. inflate() and deflate() are called */
  250. return PyMem_RawMalloc((size_t)items * (size_t)size);
  251. }
  252. static void
  253. PyZlib_Free(voidpf ctx, void *ptr)
  254. {
  255. PyMem_RawFree(ptr);
  256. }
  257. static void
  258. arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
  259. {
  260. zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
  261. *remains -= zst->avail_in;
  262. }
  263. /*[clinic input]
  264. zlib.compress
  265. data: Py_buffer
  266. Binary data to be compressed.
  267. /
  268. level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
  269. Compression level, in 0-9 or -1.
  270. wbits: int(c_default="MAX_WBITS") = MAX_WBITS
  271. The window buffer size and container format.
  272. Returns a bytes object containing compressed data.
  273. [clinic start generated code]*/
  274. static PyObject *
  275. zlib_compress_impl(PyObject *module, Py_buffer *data, int level, int wbits)
  276. /*[clinic end generated code: output=46bd152fadd66df2 input=c4d06ee5782a7e3f]*/
  277. {
  278. PyObject *return_value;
  279. int flush;
  280. z_stream zst;
  281. _BlocksOutputBuffer buffer = {.list = NULL};
  282. zlibstate *state = get_zlib_state(module);
  283. Byte *ibuf = data->buf;
  284. Py_ssize_t ibuflen = data->len;
  285. if (OutputBuffer_InitAndGrow(&buffer, -1, &zst.next_out, &zst.avail_out) < 0) {
  286. goto error;
  287. }
  288. zst.opaque = NULL;
  289. zst.zalloc = PyZlib_Malloc;
  290. zst.zfree = PyZlib_Free;
  291. zst.next_in = ibuf;
  292. int err = deflateInit2(&zst, level, DEFLATED, wbits, DEF_MEM_LEVEL,
  293. Z_DEFAULT_STRATEGY);
  294. switch (err) {
  295. case Z_OK:
  296. break;
  297. case Z_MEM_ERROR:
  298. PyErr_SetString(PyExc_MemoryError,
  299. "Out of memory while compressing data");
  300. goto error;
  301. case Z_STREAM_ERROR:
  302. PyErr_SetString(state->ZlibError, "Bad compression level");
  303. goto error;
  304. default:
  305. deflateEnd(&zst);
  306. zlib_error(state, zst, err, "while compressing data");
  307. goto error;
  308. }
  309. do {
  310. arrange_input_buffer(&zst, &ibuflen);
  311. flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
  312. do {
  313. if (zst.avail_out == 0) {
  314. if (OutputBuffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
  315. deflateEnd(&zst);
  316. goto error;
  317. }
  318. }
  319. Py_BEGIN_ALLOW_THREADS
  320. err = deflate(&zst, flush);
  321. Py_END_ALLOW_THREADS
  322. if (err == Z_STREAM_ERROR) {
  323. deflateEnd(&zst);
  324. zlib_error(state, zst, err, "while compressing data");
  325. goto error;
  326. }
  327. } while (zst.avail_out == 0);
  328. assert(zst.avail_in == 0);
  329. } while (flush != Z_FINISH);
  330. assert(err == Z_STREAM_END);
  331. err = deflateEnd(&zst);
  332. if (err == Z_OK) {
  333. return_value = OutputBuffer_Finish(&buffer, zst.avail_out);
  334. if (return_value == NULL) {
  335. goto error;
  336. }
  337. return return_value;
  338. }
  339. else
  340. zlib_error(state, zst, err, "while finishing compression");
  341. error:
  342. OutputBuffer_OnError(&buffer);
  343. return NULL;
  344. }
  345. /*[clinic input]
  346. zlib.decompress
  347. data: Py_buffer
  348. Compressed data.
  349. /
  350. wbits: int(c_default="MAX_WBITS") = MAX_WBITS
  351. The window buffer size and container format.
  352. bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
  353. The initial output buffer size.
  354. Returns a bytes object containing the uncompressed data.
  355. [clinic start generated code]*/
  356. static PyObject *
  357. zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
  358. Py_ssize_t bufsize)
  359. /*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
  360. {
  361. PyObject *return_value;
  362. Byte *ibuf;
  363. Py_ssize_t ibuflen;
  364. int err, flush;
  365. z_stream zst;
  366. _BlocksOutputBuffer buffer = {.list = NULL};
  367. _Uint32Window window; // output buffer's UINT32_MAX sliding window
  368. zlibstate *state = get_zlib_state(module);
  369. if (bufsize < 0) {
  370. PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
  371. return NULL;
  372. } else if (bufsize == 0) {
  373. bufsize = 1;
  374. }
  375. if (OutputBuffer_WindowInitWithSize(&buffer, &window, bufsize,
  376. &zst.next_out, &zst.avail_out) < 0) {
  377. goto error;
  378. }
  379. ibuf = data->buf;
  380. ibuflen = data->len;
  381. zst.opaque = NULL;
  382. zst.zalloc = PyZlib_Malloc;
  383. zst.zfree = PyZlib_Free;
  384. zst.avail_in = 0;
  385. zst.next_in = ibuf;
  386. err = inflateInit2(&zst, wbits);
  387. switch (err) {
  388. case Z_OK:
  389. break;
  390. case Z_MEM_ERROR:
  391. PyErr_SetString(PyExc_MemoryError,
  392. "Out of memory while decompressing data");
  393. goto error;
  394. default:
  395. inflateEnd(&zst);
  396. zlib_error(state, zst, err, "while preparing to decompress data");
  397. goto error;
  398. }
  399. do {
  400. arrange_input_buffer(&zst, &ibuflen);
  401. flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
  402. do {
  403. if (zst.avail_out == 0) {
  404. if (OutputBuffer_WindowGrow(&buffer, &window,
  405. &zst.next_out, &zst.avail_out) < 0) {
  406. inflateEnd(&zst);
  407. goto error;
  408. }
  409. }
  410. Py_BEGIN_ALLOW_THREADS
  411. err = inflate(&zst, flush);
  412. Py_END_ALLOW_THREADS
  413. switch (err) {
  414. case Z_OK: /* fall through */
  415. case Z_BUF_ERROR: /* fall through */
  416. case Z_STREAM_END:
  417. break;
  418. case Z_MEM_ERROR:
  419. inflateEnd(&zst);
  420. PyErr_SetString(PyExc_MemoryError,
  421. "Out of memory while decompressing data");
  422. goto error;
  423. default:
  424. inflateEnd(&zst);
  425. zlib_error(state, zst, err, "while decompressing data");
  426. goto error;
  427. }
  428. } while (zst.avail_out == 0);
  429. } while (err != Z_STREAM_END && ibuflen != 0);
  430. if (err != Z_STREAM_END) {
  431. inflateEnd(&zst);
  432. zlib_error(state, zst, err, "while decompressing data");
  433. goto error;
  434. }
  435. err = inflateEnd(&zst);
  436. if (err != Z_OK) {
  437. zlib_error(state, zst, err, "while finishing decompression");
  438. goto error;
  439. }
  440. return_value = OutputBuffer_WindowFinish(&buffer, &window, zst.avail_out);
  441. if (return_value != NULL) {
  442. return return_value;
  443. }
  444. error:
  445. OutputBuffer_WindowOnError(&buffer, &window);
  446. return NULL;
  447. }
  448. /*[clinic input]
  449. zlib.compressobj
  450. level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
  451. The compression level (an integer in the range 0-9 or -1; default is
  452. currently equivalent to 6). Higher compression levels are slower,
  453. but produce smaller results.
  454. method: int(c_default="DEFLATED") = DEFLATED
  455. The compression algorithm. If given, this must be DEFLATED.
  456. wbits: int(c_default="MAX_WBITS") = MAX_WBITS
  457. +9 to +15: The base-two logarithm of the window size. Include a zlib
  458. container.
  459. -9 to -15: Generate a raw stream.
  460. +25 to +31: Include a gzip container.
  461. memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
  462. Controls the amount of memory used for internal compression state.
  463. Valid values range from 1 to 9. Higher values result in higher memory
  464. usage, faster compression, and smaller output.
  465. strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
  466. Used to tune the compression algorithm. Possible values are
  467. Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
  468. zdict: Py_buffer = None
  469. The predefined compression dictionary - a sequence of bytes
  470. containing subsequences that are likely to occur in the input data.
  471. Return a compressor object.
  472. [clinic start generated code]*/
  473. static PyObject *
  474. zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
  475. int memLevel, int strategy, Py_buffer *zdict)
  476. /*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
  477. {
  478. zlibstate *state = get_zlib_state(module);
  479. if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
  480. PyErr_SetString(PyExc_OverflowError,
  481. "zdict length does not fit in an unsigned int");
  482. return NULL;
  483. }
  484. compobject *self = newcompobject(state->Comptype);
  485. if (self == NULL)
  486. goto error;
  487. self->zst.opaque = NULL;
  488. self->zst.zalloc = PyZlib_Malloc;
  489. self->zst.zfree = PyZlib_Free;
  490. self->zst.next_in = NULL;
  491. self->zst.avail_in = 0;
  492. int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
  493. switch (err) {
  494. case Z_OK:
  495. self->is_initialised = 1;
  496. if (zdict->buf == NULL) {
  497. goto success;
  498. } else {
  499. err = deflateSetDictionary(&self->zst,
  500. zdict->buf, (unsigned int)zdict->len);
  501. switch (err) {
  502. case Z_OK:
  503. goto success;
  504. case Z_STREAM_ERROR:
  505. PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
  506. goto error;
  507. default:
  508. PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
  509. goto error;
  510. }
  511. }
  512. case Z_MEM_ERROR:
  513. PyErr_SetString(PyExc_MemoryError,
  514. "Can't allocate memory for compression object");
  515. goto error;
  516. case Z_STREAM_ERROR:
  517. PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
  518. goto error;
  519. default:
  520. zlib_error(state, self->zst, err, "while creating compression object");
  521. goto error;
  522. }
  523. error:
  524. Py_CLEAR(self);
  525. success:
  526. return (PyObject *)self;
  527. }
  528. static int
  529. set_inflate_zdict(zlibstate *state, compobject *self)
  530. {
  531. Py_buffer zdict_buf;
  532. if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
  533. return -1;
  534. }
  535. if ((size_t)zdict_buf.len > UINT_MAX) {
  536. PyErr_SetString(PyExc_OverflowError,
  537. "zdict length does not fit in an unsigned int");
  538. PyBuffer_Release(&zdict_buf);
  539. return -1;
  540. }
  541. int err;
  542. err = inflateSetDictionary(&self->zst,
  543. zdict_buf.buf, (unsigned int)zdict_buf.len);
  544. PyBuffer_Release(&zdict_buf);
  545. if (err != Z_OK) {
  546. zlib_error(state, self->zst, err, "while setting zdict");
  547. return -1;
  548. }
  549. return 0;
  550. }
  551. /*[clinic input]
  552. zlib.decompressobj
  553. wbits: int(c_default="MAX_WBITS") = MAX_WBITS
  554. The window buffer size and container format.
  555. zdict: object(c_default="NULL") = b''
  556. The predefined compression dictionary. This must be the same
  557. dictionary as used by the compressor that produced the input data.
  558. Return a decompressor object.
  559. [clinic start generated code]*/
  560. static PyObject *
  561. zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
  562. /*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
  563. {
  564. zlibstate *state = get_zlib_state(module);
  565. if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
  566. PyErr_SetString(PyExc_TypeError,
  567. "zdict argument must support the buffer protocol");
  568. return NULL;
  569. }
  570. compobject *self = newcompobject(state->Decomptype);
  571. if (self == NULL)
  572. return NULL;
  573. self->zst.opaque = NULL;
  574. self->zst.zalloc = PyZlib_Malloc;
  575. self->zst.zfree = PyZlib_Free;
  576. self->zst.next_in = NULL;
  577. self->zst.avail_in = 0;
  578. if (zdict != NULL) {
  579. self->zdict = Py_NewRef(zdict);
  580. }
  581. int err = inflateInit2(&self->zst, wbits);
  582. switch (err) {
  583. case Z_OK:
  584. self->is_initialised = 1;
  585. if (self->zdict != NULL && wbits < 0) {
  586. if (set_inflate_zdict(state, self) < 0) {
  587. Py_DECREF(self);
  588. return NULL;
  589. }
  590. }
  591. return (PyObject *)self;
  592. case Z_STREAM_ERROR:
  593. Py_DECREF(self);
  594. PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
  595. return NULL;
  596. case Z_MEM_ERROR:
  597. Py_DECREF(self);
  598. PyErr_SetString(PyExc_MemoryError,
  599. "Can't allocate memory for decompression object");
  600. return NULL;
  601. default:
  602. zlib_error(state, self->zst, err, "while creating decompression object");
  603. Py_DECREF(self);
  604. return NULL;
  605. }
  606. }
  607. static void
  608. Dealloc(compobject *self)
  609. {
  610. PyObject *type = (PyObject *)Py_TYPE(self);
  611. PyThread_free_lock(self->lock);
  612. Py_XDECREF(self->unused_data);
  613. Py_XDECREF(self->unconsumed_tail);
  614. Py_XDECREF(self->zdict);
  615. PyObject_Free(self);
  616. Py_DECREF(type);
  617. }
  618. static void
  619. Comp_dealloc(compobject *self)
  620. {
  621. if (self->is_initialised)
  622. deflateEnd(&self->zst);
  623. Dealloc(self);
  624. }
  625. static void
  626. Decomp_dealloc(compobject *self)
  627. {
  628. if (self->is_initialised)
  629. inflateEnd(&self->zst);
  630. Dealloc(self);
  631. }
  632. /*[clinic input]
  633. zlib.Compress.compress
  634. cls: defining_class
  635. data: Py_buffer
  636. Binary data to be compressed.
  637. /
  638. Returns a bytes object containing compressed data.
  639. After calling this function, some of the input data may still
  640. be stored in internal buffers for later processing.
  641. Call the flush() method to clear these buffers.
  642. [clinic start generated code]*/
  643. static PyObject *
  644. zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
  645. Py_buffer *data)
  646. /*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/
  647. {
  648. PyObject *return_value;
  649. int err;
  650. _BlocksOutputBuffer buffer = {.list = NULL};
  651. zlibstate *state = PyType_GetModuleState(cls);
  652. ENTER_ZLIB(self);
  653. self->zst.next_in = data->buf;
  654. Py_ssize_t ibuflen = data->len;
  655. if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
  656. goto error;
  657. }
  658. do {
  659. arrange_input_buffer(&self->zst, &ibuflen);
  660. do {
  661. if (self->zst.avail_out == 0) {
  662. if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
  663. goto error;
  664. }
  665. }
  666. Py_BEGIN_ALLOW_THREADS
  667. err = deflate(&self->zst, Z_NO_FLUSH);
  668. Py_END_ALLOW_THREADS
  669. if (err == Z_STREAM_ERROR) {
  670. zlib_error(state, self->zst, err, "while compressing data");
  671. goto error;
  672. }
  673. } while (self->zst.avail_out == 0);
  674. assert(self->zst.avail_in == 0);
  675. } while (ibuflen != 0);
  676. return_value = OutputBuffer_Finish(&buffer, self->zst.avail_out);
  677. if (return_value != NULL) {
  678. goto success;
  679. }
  680. error:
  681. OutputBuffer_OnError(&buffer);
  682. return_value = NULL;
  683. success:
  684. LEAVE_ZLIB(self);
  685. return return_value;
  686. }
  687. /* Helper for objdecompress() and flush(). Saves any unconsumed input data in
  688. self->unused_data or self->unconsumed_tail, as appropriate. */
  689. static int
  690. save_unconsumed_input(compobject *self, Py_buffer *data, int err)
  691. {
  692. if (err == Z_STREAM_END) {
  693. /* The end of the compressed data has been reached. Store the leftover
  694. input data in self->unused_data. */
  695. if (self->zst.avail_in > 0) {
  696. Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
  697. Py_ssize_t new_size, left_size;
  698. PyObject *new_data;
  699. left_size = (Byte *)data->buf + data->len - self->zst.next_in;
  700. if (left_size > (PY_SSIZE_T_MAX - old_size)) {
  701. PyErr_NoMemory();
  702. return -1;
  703. }
  704. new_size = old_size + left_size;
  705. new_data = PyBytes_FromStringAndSize(NULL, new_size);
  706. if (new_data == NULL)
  707. return -1;
  708. memcpy(PyBytes_AS_STRING(new_data),
  709. PyBytes_AS_STRING(self->unused_data), old_size);
  710. memcpy(PyBytes_AS_STRING(new_data) + old_size,
  711. self->zst.next_in, left_size);
  712. Py_SETREF(self->unused_data, new_data);
  713. self->zst.avail_in = 0;
  714. }
  715. }
  716. if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
  717. /* This code handles two distinct cases:
  718. 1. Output limit was reached. Save leftover input in unconsumed_tail.
  719. 2. All input data was consumed. Clear unconsumed_tail. */
  720. Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
  721. PyObject *new_data = PyBytes_FromStringAndSize(
  722. (char *)self->zst.next_in, left_size);
  723. if (new_data == NULL)
  724. return -1;
  725. Py_SETREF(self->unconsumed_tail, new_data);
  726. }
  727. return 0;
  728. }
  729. /*[clinic input]
  730. zlib.Decompress.decompress
  731. cls: defining_class
  732. data: Py_buffer
  733. The binary data to decompress.
  734. /
  735. max_length: Py_ssize_t = 0
  736. The maximum allowable length of the decompressed data.
  737. Unconsumed input data will be stored in
  738. the unconsumed_tail attribute.
  739. Return a bytes object containing the decompressed version of the data.
  740. After calling this function, some of the input data may still be stored in
  741. internal buffers for later processing.
  742. Call the flush() method to clear these buffers.
  743. [clinic start generated code]*/
  744. static PyObject *
  745. zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
  746. Py_buffer *data, Py_ssize_t max_length)
  747. /*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/
  748. {
  749. int err = Z_OK;
  750. Py_ssize_t ibuflen;
  751. PyObject *return_value;
  752. _BlocksOutputBuffer buffer = {.list = NULL};
  753. PyObject *module = PyType_GetModule(cls);
  754. if (module == NULL)
  755. return NULL;
  756. zlibstate *state = get_zlib_state(module);
  757. if (max_length < 0) {
  758. PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
  759. return NULL;
  760. } else if (max_length == 0) {
  761. max_length = -1;
  762. }
  763. ENTER_ZLIB(self);
  764. self->zst.next_in = data->buf;
  765. ibuflen = data->len;
  766. if (OutputBuffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) {
  767. goto abort;
  768. }
  769. do {
  770. arrange_input_buffer(&self->zst, &ibuflen);
  771. do {
  772. if (self->zst.avail_out == 0) {
  773. if (OutputBuffer_GetDataSize(&buffer, self->zst.avail_out) == max_length) {
  774. goto save;
  775. }
  776. if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
  777. goto abort;
  778. }
  779. }
  780. Py_BEGIN_ALLOW_THREADS
  781. err = inflate(&self->zst, Z_SYNC_FLUSH);
  782. Py_END_ALLOW_THREADS
  783. switch (err) {
  784. case Z_OK: /* fall through */
  785. case Z_BUF_ERROR: /* fall through */
  786. case Z_STREAM_END:
  787. break;
  788. default:
  789. if (err == Z_NEED_DICT && self->zdict != NULL) {
  790. if (set_inflate_zdict(state, self) < 0) {
  791. goto abort;
  792. }
  793. else
  794. break;
  795. }
  796. goto save;
  797. }
  798. } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
  799. } while (err != Z_STREAM_END && ibuflen != 0);
  800. save:
  801. if (save_unconsumed_input(self, data, err) < 0)
  802. goto abort;
  803. if (err == Z_STREAM_END) {
  804. /* This is the logical place to call inflateEnd, but the old behaviour
  805. of only calling it on flush() is preserved. */
  806. self->eof = 1;
  807. } else if (err != Z_OK && err != Z_BUF_ERROR) {
  808. /* We will only get Z_BUF_ERROR if the output buffer was full
  809. but there wasn't more output when we tried again, so it is
  810. not an error condition.
  811. */
  812. zlib_error(state, self->zst, err, "while decompressing data");
  813. goto abort;
  814. }
  815. return_value = OutputBuffer_Finish(&buffer, self->zst.avail_out);
  816. if (return_value != NULL) {
  817. goto success;
  818. }
  819. abort:
  820. OutputBuffer_OnError(&buffer);
  821. return_value = NULL;
  822. success:
  823. LEAVE_ZLIB(self);
  824. return return_value;
  825. }
  826. /*[clinic input]
  827. zlib.Compress.flush
  828. cls: defining_class
  829. mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
  830. One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
  831. If mode == Z_FINISH, the compressor object can no longer be
  832. used after calling the flush() method. Otherwise, more data
  833. can still be compressed.
  834. /
  835. Return a bytes object containing any remaining compressed data.
  836. [clinic start generated code]*/
  837. static PyObject *
  838. zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
  839. /*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/
  840. {
  841. int err;
  842. PyObject *return_value;
  843. _BlocksOutputBuffer buffer = {.list = NULL};
  844. zlibstate *state = PyType_GetModuleState(cls);
  845. /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
  846. doing any work at all; just return an empty string. */
  847. if (mode == Z_NO_FLUSH) {
  848. return PyBytes_FromStringAndSize(NULL, 0);
  849. }
  850. ENTER_ZLIB(self);
  851. self->zst.avail_in = 0;
  852. if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
  853. goto error;
  854. }
  855. do {
  856. if (self->zst.avail_out == 0) {
  857. if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
  858. goto error;
  859. }
  860. }
  861. Py_BEGIN_ALLOW_THREADS
  862. err = deflate(&self->zst, mode);
  863. Py_END_ALLOW_THREADS
  864. if (err == Z_STREAM_ERROR) {
  865. zlib_error(state, self->zst, err, "while flushing");
  866. goto error;
  867. }
  868. } while (self->zst.avail_out == 0);
  869. assert(self->zst.avail_in == 0);
  870. /* If mode is Z_FINISH, we also have to call deflateEnd() to free
  871. various data structures. Note we should only get Z_STREAM_END when
  872. mode is Z_FINISH, but checking both for safety*/
  873. if (err == Z_STREAM_END && mode == Z_FINISH) {
  874. err = deflateEnd(&self->zst);
  875. if (err != Z_OK) {
  876. zlib_error(state, self->zst, err, "while finishing compression");
  877. goto error;
  878. }
  879. else
  880. self->is_initialised = 0;
  881. /* We will only get Z_BUF_ERROR if the output buffer was full
  882. but there wasn't more output when we tried again, so it is
  883. not an error condition.
  884. */
  885. } else if (err != Z_OK && err != Z_BUF_ERROR) {
  886. zlib_error(state, self->zst, err, "while flushing");
  887. goto error;
  888. }
  889. return_value = OutputBuffer_Finish(&buffer, self->zst.avail_out);
  890. if (return_value != NULL) {
  891. goto success;
  892. }
  893. error:
  894. OutputBuffer_OnError(&buffer);
  895. return_value = NULL;
  896. success:
  897. LEAVE_ZLIB(self);
  898. return return_value;
  899. }
  900. #ifdef HAVE_ZLIB_COPY
  901. /*[clinic input]
  902. zlib.Compress.copy
  903. cls: defining_class
  904. Return a copy of the compression object.
  905. [clinic start generated code]*/
  906. static PyObject *
  907. zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
  908. /*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
  909. {
  910. zlibstate *state = PyType_GetModuleState(cls);
  911. compobject *return_value = newcompobject(state->Comptype);
  912. if (!return_value) return NULL;
  913. /* Copy the zstream state
  914. * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
  915. */
  916. ENTER_ZLIB(self);
  917. int err = deflateCopy(&return_value->zst, &self->zst);
  918. switch (err) {
  919. case Z_OK:
  920. break;
  921. case Z_STREAM_ERROR:
  922. PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
  923. goto error;
  924. case Z_MEM_ERROR:
  925. PyErr_SetString(PyExc_MemoryError,
  926. "Can't allocate memory for compression object");
  927. goto error;
  928. default:
  929. zlib_error(state, self->zst, err, "while copying compression object");
  930. goto error;
  931. }
  932. Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
  933. Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
  934. Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
  935. return_value->eof = self->eof;
  936. /* Mark it as being initialized */
  937. return_value->is_initialised = 1;
  938. LEAVE_ZLIB(self);
  939. return (PyObject *)return_value;
  940. error:
  941. LEAVE_ZLIB(self);
  942. Py_XDECREF(return_value);
  943. return NULL;
  944. }
  945. /*[clinic input]
  946. zlib.Compress.__copy__
  947. cls: defining_class
  948. [clinic start generated code]*/
  949. static PyObject *
  950. zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
  951. /*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
  952. {
  953. return zlib_Compress_copy_impl(self, cls);
  954. }
  955. /*[clinic input]
  956. zlib.Compress.__deepcopy__
  957. cls: defining_class
  958. memo: object
  959. /
  960. [clinic start generated code]*/
  961. static PyObject *
  962. zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
  963. PyObject *memo)
  964. /*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
  965. {
  966. return zlib_Compress_copy_impl(self, cls);
  967. }
  968. /*[clinic input]
  969. zlib.Decompress.copy
  970. cls: defining_class
  971. Return a copy of the decompression object.
  972. [clinic start generated code]*/
  973. static PyObject *
  974. zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
  975. /*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
  976. {
  977. zlibstate *state = PyType_GetModuleState(cls);
  978. compobject *return_value = newcompobject(state->Decomptype);
  979. if (!return_value) return NULL;
  980. /* Copy the zstream state
  981. * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
  982. */
  983. ENTER_ZLIB(self);
  984. int err = inflateCopy(&return_value->zst, &self->zst);
  985. switch (err) {
  986. case Z_OK:
  987. break;
  988. case Z_STREAM_ERROR:
  989. PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
  990. goto error;
  991. case Z_MEM_ERROR:
  992. PyErr_SetString(PyExc_MemoryError,
  993. "Can't allocate memory for decompression object");
  994. goto error;
  995. default:
  996. zlib_error(state, self->zst, err, "while copying decompression object");
  997. goto error;
  998. }
  999. Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
  1000. Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
  1001. Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
  1002. return_value->eof = self->eof;
  1003. /* Mark it as being initialized */
  1004. return_value->is_initialised = 1;
  1005. LEAVE_ZLIB(self);
  1006. return (PyObject *)return_value;
  1007. error:
  1008. LEAVE_ZLIB(self);
  1009. Py_XDECREF(return_value);
  1010. return NULL;
  1011. }
  1012. /*[clinic input]
  1013. zlib.Decompress.__copy__
  1014. cls: defining_class
  1015. [clinic start generated code]*/
  1016. static PyObject *
  1017. zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
  1018. /*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
  1019. {
  1020. return zlib_Decompress_copy_impl(self, cls);
  1021. }
  1022. /*[clinic input]
  1023. zlib.Decompress.__deepcopy__
  1024. cls: defining_class
  1025. memo: object
  1026. /
  1027. [clinic start generated code]*/
  1028. static PyObject *
  1029. zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
  1030. PyObject *memo)
  1031. /*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
  1032. {
  1033. return zlib_Decompress_copy_impl(self, cls);
  1034. }
  1035. #endif
  1036. /*[clinic input]
  1037. zlib.Decompress.flush
  1038. cls: defining_class
  1039. length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
  1040. the initial size of the output buffer.
  1041. /
  1042. Return a bytes object containing any remaining decompressed data.
  1043. [clinic start generated code]*/
  1044. static PyObject *
  1045. zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
  1046. Py_ssize_t length)
  1047. /*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/
  1048. {
  1049. int err, flush;
  1050. Py_buffer data;
  1051. PyObject *return_value;
  1052. Py_ssize_t ibuflen;
  1053. _BlocksOutputBuffer buffer = {.list = NULL};
  1054. _Uint32Window window; // output buffer's UINT32_MAX sliding window
  1055. PyObject *module = PyType_GetModule(cls);
  1056. if (module == NULL) {
  1057. return NULL;
  1058. }
  1059. zlibstate *state = get_zlib_state(module);
  1060. if (length <= 0) {
  1061. PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
  1062. return NULL;
  1063. }
  1064. ENTER_ZLIB(self);
  1065. if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
  1066. LEAVE_ZLIB(self);
  1067. return NULL;
  1068. }
  1069. self->zst.next_in = data.buf;
  1070. ibuflen = data.len;
  1071. if (OutputBuffer_WindowInitWithSize(&buffer, &window, length,
  1072. &self->zst.next_out, &self->zst.avail_out) < 0) {
  1073. goto abort;
  1074. }
  1075. do {
  1076. arrange_input_buffer(&self->zst, &ibuflen);
  1077. flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
  1078. do {
  1079. if (self->zst.avail_out == 0) {
  1080. if (OutputBuffer_WindowGrow(&buffer, &window,
  1081. &self->zst.next_out, &self->zst.avail_out) < 0) {
  1082. goto abort;
  1083. }
  1084. }
  1085. Py_BEGIN_ALLOW_THREADS
  1086. err = inflate(&self->zst, flush);
  1087. Py_END_ALLOW_THREADS
  1088. switch (err) {
  1089. case Z_OK: /* fall through */
  1090. case Z_BUF_ERROR: /* fall through */
  1091. case Z_STREAM_END:
  1092. break;
  1093. default:
  1094. goto save;
  1095. }
  1096. } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
  1097. } while (err != Z_STREAM_END && ibuflen != 0);
  1098. save:
  1099. if (save_unconsumed_input(self, &data, err) < 0) {
  1100. goto abort;
  1101. }
  1102. /* If at end of stream, clean up any memory allocated by zlib. */
  1103. if (err == Z_STREAM_END) {
  1104. self->eof = 1;
  1105. self->is_initialised = 0;
  1106. err = inflateEnd(&self->zst);
  1107. if (err != Z_OK) {
  1108. zlib_error(state, self->zst, err, "while finishing decompression");
  1109. goto abort;
  1110. }
  1111. }
  1112. return_value = OutputBuffer_WindowFinish(&buffer, &window, self->zst.avail_out);
  1113. if (return_value != NULL) {
  1114. goto success;
  1115. }
  1116. abort:
  1117. OutputBuffer_WindowOnError(&buffer, &window);
  1118. return_value = NULL;
  1119. success:
  1120. PyBuffer_Release(&data);
  1121. LEAVE_ZLIB(self);
  1122. return return_value;
  1123. }
  1124. typedef struct {
  1125. PyObject_HEAD
  1126. z_stream zst;
  1127. PyObject *zdict;
  1128. PyThread_type_lock lock;
  1129. PyObject *unused_data;
  1130. uint8_t *input_buffer;
  1131. Py_ssize_t input_buffer_size;
  1132. /* zst>avail_in is only 32 bit, so we store the true length
  1133. separately. Conversion and looping is encapsulated in
  1134. decompress_buf() */
  1135. Py_ssize_t avail_in_real;
  1136. bool is_initialised;
  1137. char eof; /* T_BOOL expects a char */
  1138. char needs_input;
  1139. } ZlibDecompressor;
  1140. /*[clinic input]
  1141. class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
  1142. [clinic start generated code]*/
  1143. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=0658178ab94645df]*/
  1144. static void
  1145. ZlibDecompressor_dealloc(ZlibDecompressor *self)
  1146. {
  1147. PyObject *type = (PyObject *)Py_TYPE(self);
  1148. PyThread_free_lock(self->lock);
  1149. if (self->is_initialised) {
  1150. inflateEnd(&self->zst);
  1151. }
  1152. PyMem_Free(self->input_buffer);
  1153. Py_CLEAR(self->unused_data);
  1154. Py_CLEAR(self->zdict);
  1155. PyObject_Free(self);
  1156. Py_DECREF(type);
  1157. }
  1158. static int
  1159. set_inflate_zdict_ZlibDecompressor(zlibstate *state, ZlibDecompressor *self)
  1160. {
  1161. Py_buffer zdict_buf;
  1162. if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
  1163. return -1;
  1164. }
  1165. if ((size_t)zdict_buf.len > UINT_MAX) {
  1166. PyErr_SetString(PyExc_OverflowError,
  1167. "zdict length does not fit in an unsigned int");
  1168. PyBuffer_Release(&zdict_buf);
  1169. return -1;
  1170. }
  1171. int err;
  1172. err = inflateSetDictionary(&self->zst,
  1173. zdict_buf.buf, (unsigned int)zdict_buf.len);
  1174. PyBuffer_Release(&zdict_buf);
  1175. if (err != Z_OK) {
  1176. zlib_error(state, self->zst, err, "while setting zdict");
  1177. return -1;
  1178. }
  1179. return 0;
  1180. }
  1181. static Py_ssize_t
  1182. arrange_output_buffer_with_maximum(uint32_t *avail_out,
  1183. uint8_t **next_out,
  1184. PyObject **buffer,
  1185. Py_ssize_t length,
  1186. Py_ssize_t max_length)
  1187. {
  1188. Py_ssize_t occupied;
  1189. if (*buffer == NULL) {
  1190. if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
  1191. return -1;
  1192. occupied = 0;
  1193. }
  1194. else {
  1195. occupied = *next_out - (uint8_t *)PyBytes_AS_STRING(*buffer);
  1196. if (length == occupied) {
  1197. Py_ssize_t new_length;
  1198. assert(length <= max_length);
  1199. /* can not scale the buffer over max_length */
  1200. if (length == max_length)
  1201. return -2;
  1202. if (length <= (max_length >> 1))
  1203. new_length = length << 1;
  1204. else
  1205. new_length = max_length;
  1206. if (_PyBytes_Resize(buffer, new_length) < 0)
  1207. return -1;
  1208. length = new_length;
  1209. }
  1210. }
  1211. *avail_out = (uint32_t)Py_MIN((size_t)(length - occupied), UINT32_MAX);
  1212. *next_out = (uint8_t *)PyBytes_AS_STRING(*buffer) + occupied;
  1213. return length;
  1214. }
  1215. /* Decompress data of length self->avail_in_real in self->state.next_in. The
  1216. output buffer is allocated dynamically and returned. If the max_length is
  1217. of sufficiently low size, max_length is allocated immediately. At most
  1218. max_length bytes are returned, so some of the input may not be consumed.
  1219. self->state.next_in and self->avail_in_real are updated to reflect the
  1220. consumed input. */
  1221. static PyObject*
  1222. decompress_buf(ZlibDecompressor *self, Py_ssize_t max_length)
  1223. {
  1224. /* data_size is strictly positive, but because we repeatedly have to
  1225. compare against max_length and PyBytes_GET_SIZE we declare it as
  1226. signed */
  1227. PyObject *return_value = NULL;
  1228. Py_ssize_t hard_limit;
  1229. Py_ssize_t obuflen;
  1230. zlibstate *state = PyType_GetModuleState(Py_TYPE(self));
  1231. int err = Z_OK;
  1232. /* When sys.maxsize is passed as default use DEF_BUF_SIZE as start buffer.
  1233. In this particular case the data may not necessarily be very big, so
  1234. it is better to grow dynamically.*/
  1235. if ((max_length < 0) || max_length == PY_SSIZE_T_MAX) {
  1236. hard_limit = PY_SSIZE_T_MAX;
  1237. obuflen = DEF_BUF_SIZE;
  1238. } else {
  1239. /* Assume that decompressor is used in file decompression with a fixed
  1240. block size of max_length. In that case we will reach max_length almost
  1241. always (except at the end of the file). So it makes sense to allocate
  1242. max_length. */
  1243. hard_limit = max_length;
  1244. obuflen = max_length;
  1245. if (obuflen > DEF_MAX_INITIAL_BUF_SIZE){
  1246. // Safeguard against memory overflow.
  1247. obuflen = DEF_MAX_INITIAL_BUF_SIZE;
  1248. }
  1249. }
  1250. do {
  1251. arrange_input_buffer(&(self->zst), &(self->avail_in_real));
  1252. do {
  1253. obuflen = arrange_output_buffer_with_maximum(&(self->zst.avail_out),
  1254. &(self->zst.next_out),
  1255. &return_value,
  1256. obuflen,
  1257. hard_limit);
  1258. if (obuflen == -1){
  1259. PyErr_SetString(PyExc_MemoryError,
  1260. "Insufficient memory for buffer allocation");
  1261. goto error;
  1262. }
  1263. else if (obuflen == -2) {
  1264. break;
  1265. }
  1266. Py_BEGIN_ALLOW_THREADS
  1267. err = inflate(&self->zst, Z_SYNC_FLUSH);
  1268. Py_END_ALLOW_THREADS
  1269. switch (err) {
  1270. case Z_OK: /* fall through */
  1271. case Z_BUF_ERROR: /* fall through */
  1272. case Z_STREAM_END:
  1273. break;
  1274. default:
  1275. if (err == Z_NEED_DICT) {
  1276. goto error;
  1277. }
  1278. else {
  1279. break;
  1280. }
  1281. }
  1282. } while (self->zst.avail_out == 0);
  1283. } while(err != Z_STREAM_END && self->avail_in_real != 0);
  1284. if (err == Z_STREAM_END) {
  1285. self->eof = 1;
  1286. self->is_initialised = 0;
  1287. /* Unlike the Decompress object we call inflateEnd here as there are no
  1288. backwards compatibility issues */
  1289. err = inflateEnd(&self->zst);
  1290. if (err != Z_OK) {
  1291. zlib_error(state, self->zst, err, "while finishing decompression");
  1292. goto error;
  1293. }
  1294. } else if (err != Z_OK && err != Z_BUF_ERROR) {
  1295. zlib_error(state, self->zst, err, "while decompressing data");
  1296. goto error;
  1297. }
  1298. self->avail_in_real += self->zst.avail_in;
  1299. if (_PyBytes_Resize(&return_value, self->zst.next_out -
  1300. (uint8_t *)PyBytes_AS_STRING(return_value)) != 0) {
  1301. goto error;
  1302. }
  1303. goto success;
  1304. error:
  1305. Py_CLEAR(return_value);
  1306. success:
  1307. return return_value;
  1308. }
  1309. static PyObject *
  1310. decompress(ZlibDecompressor *self, uint8_t *data,
  1311. size_t len, Py_ssize_t max_length)
  1312. {
  1313. bool input_buffer_in_use;
  1314. PyObject *result;
  1315. /* Prepend unconsumed input if necessary */
  1316. if (self->zst.next_in != NULL) {
  1317. size_t avail_now, avail_total;
  1318. /* Number of bytes we can append to input buffer */
  1319. avail_now = (self->input_buffer + self->input_buffer_size)
  1320. - (self->zst.next_in + self->avail_in_real);
  1321. /* Number of bytes we can append if we move existing
  1322. contents to beginning of buffer (overwriting
  1323. consumed input) */
  1324. avail_total = self->input_buffer_size - self->avail_in_real;
  1325. if (avail_total < len) {
  1326. size_t offset = self->zst.next_in - self->input_buffer;
  1327. uint8_t *tmp;
  1328. size_t new_size = self->input_buffer_size + len - avail_now;
  1329. /* Assign to temporary variable first, so we don't
  1330. lose address of allocated buffer if realloc fails */
  1331. tmp = PyMem_Realloc(self->input_buffer, new_size);
  1332. if (tmp == NULL) {
  1333. PyErr_SetNone(PyExc_MemoryError);
  1334. return NULL;
  1335. }
  1336. self->input_buffer = tmp;
  1337. self->input_buffer_size = new_size;
  1338. self->zst.next_in = self->input_buffer + offset;
  1339. }
  1340. else if (avail_now < len) {
  1341. memmove(self->input_buffer, self->zst.next_in,
  1342. self->avail_in_real);
  1343. self->zst.next_in = self->input_buffer;
  1344. }
  1345. memcpy((void*)(self->zst.next_in + self->avail_in_real), data, len);
  1346. self->avail_in_real += len;
  1347. input_buffer_in_use = 1;
  1348. }
  1349. else {
  1350. self->zst.next_in = data;
  1351. self->avail_in_real = len;
  1352. input_buffer_in_use = 0;
  1353. }
  1354. result = decompress_buf(self, max_length);
  1355. if(result == NULL) {
  1356. self->zst.next_in = NULL;
  1357. return NULL;
  1358. }
  1359. if (self->eof) {
  1360. self->needs_input = 0;
  1361. if (self->avail_in_real > 0) {
  1362. PyObject *unused_data = PyBytes_FromStringAndSize(
  1363. (char *)self->zst.next_in, self->avail_in_real);
  1364. if (unused_data == NULL) {
  1365. goto error;
  1366. }
  1367. Py_XSETREF(self->unused_data, unused_data);
  1368. }
  1369. }
  1370. else if (self->avail_in_real == 0) {
  1371. self->zst.next_in = NULL;
  1372. self->needs_input = 1;
  1373. }
  1374. else {
  1375. self->needs_input = 0;
  1376. /* If we did not use the input buffer, we now have
  1377. to copy the tail from the caller's buffer into the
  1378. input buffer */
  1379. if (!input_buffer_in_use) {
  1380. /* Discard buffer if it's too small
  1381. (resizing it may needlessly copy the current contents) */
  1382. if (self->input_buffer != NULL &&
  1383. self->input_buffer_size < self->avail_in_real) {
  1384. PyMem_Free(self->input_buffer);
  1385. self->input_buffer = NULL;
  1386. }
  1387. /* Allocate if necessary */
  1388. if (self->input_buffer == NULL) {
  1389. self->input_buffer = PyMem_Malloc(self->avail_in_real);
  1390. if (self->input_buffer == NULL) {
  1391. PyErr_SetNone(PyExc_MemoryError);
  1392. goto error;
  1393. }
  1394. self->input_buffer_size = self->avail_in_real;
  1395. }
  1396. /* Copy tail */
  1397. memcpy(self->input_buffer, self->zst.next_in, self->avail_in_real);
  1398. self->zst.next_in = self->input_buffer;
  1399. }
  1400. }
  1401. return result;
  1402. error:
  1403. Py_XDECREF(result);
  1404. return NULL;
  1405. }
  1406. /*[clinic input]
  1407. zlib.ZlibDecompressor.decompress
  1408. data: Py_buffer
  1409. max_length: Py_ssize_t=-1
  1410. Decompress *data*, returning uncompressed data as bytes.
  1411. If *max_length* is nonnegative, returns at most *max_length* bytes of
  1412. decompressed data. If this limit is reached and further output can be
  1413. produced, *self.needs_input* will be set to ``False``. In this case, the next
  1414. call to *decompress()* may provide *data* as b'' to obtain more of the output.
  1415. If all of the input data was decompressed and returned (either because this
  1416. was less than *max_length* bytes, or because *max_length* was negative),
  1417. *self.needs_input* will be set to True.
  1418. Attempting to decompress data after the end of stream is reached raises an
  1419. EOFError. Any data found after the end of the stream is ignored and saved in
  1420. the unused_data attribute.
  1421. [clinic start generated code]*/
  1422. static PyObject *
  1423. zlib_ZlibDecompressor_decompress_impl(ZlibDecompressor *self,
  1424. Py_buffer *data, Py_ssize_t max_length)
  1425. /*[clinic end generated code: output=990d32787b775f85 input=0b29d99715250b96]*/
  1426. {
  1427. PyObject *result = NULL;
  1428. ENTER_ZLIB(self);
  1429. if (self->eof) {
  1430. PyErr_SetString(PyExc_EOFError, "End of stream already reached");
  1431. }
  1432. else {
  1433. result = decompress(self, data->buf, data->len, max_length);
  1434. }
  1435. LEAVE_ZLIB(self);
  1436. return result;
  1437. }
  1438. PyDoc_STRVAR(ZlibDecompressor__new____doc__,
  1439. "_ZlibDecompressor(wbits=15, zdict=b\'\')\n"
  1440. "--\n"
  1441. "\n"
  1442. "Create a decompressor object for decompressing data incrementally.\n"
  1443. "\n"
  1444. " wbits = 15\n"
  1445. " zdict\n"
  1446. " The predefined compression dictionary. This is a sequence of bytes\n"
  1447. " (such as a bytes object) containing subsequences that are expected\n"
  1448. " to occur frequently in the data that is to be compressed. Those\n"
  1449. " subsequences that are expected to be most common should come at the\n"
  1450. " end of the dictionary. This must be the same dictionary as used by the\n"
  1451. " compressor that produced the input data.\n"
  1452. "\n");
  1453. static PyObject *
  1454. ZlibDecompressor__new__(PyTypeObject *cls,
  1455. PyObject *args,
  1456. PyObject *kwargs)
  1457. {
  1458. static char *keywords[] = {"wbits", "zdict", NULL};
  1459. static const char * const format = "|iO:_ZlibDecompressor";
  1460. int wbits = MAX_WBITS;
  1461. PyObject *zdict = NULL;
  1462. zlibstate *state = PyType_GetModuleState(cls);
  1463. if (!PyArg_ParseTupleAndKeywords(
  1464. args, kwargs, format, keywords, &wbits, &zdict)) {
  1465. return NULL;
  1466. }
  1467. ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls);
  1468. self->eof = 0;
  1469. self->needs_input = 1;
  1470. self->avail_in_real = 0;
  1471. self->input_buffer = NULL;
  1472. self->input_buffer_size = 0;
  1473. self->zdict = Py_XNewRef(zdict);
  1474. self->zst.opaque = NULL;
  1475. self->zst.zalloc = PyZlib_Malloc;
  1476. self->zst.zfree = PyZlib_Free;
  1477. self->zst.next_in = NULL;
  1478. self->zst.avail_in = 0;
  1479. self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
  1480. if (self->unused_data == NULL) {
  1481. Py_CLEAR(self);
  1482. return NULL;
  1483. }
  1484. self->lock = PyThread_allocate_lock();
  1485. if (self->lock == NULL) {
  1486. Py_DECREF(self);
  1487. PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
  1488. return NULL;
  1489. }
  1490. int err = inflateInit2(&(self->zst), wbits);
  1491. switch (err) {
  1492. case Z_OK:
  1493. self->is_initialised = 1;
  1494. if (self->zdict != NULL && wbits < 0) {
  1495. if (set_inflate_zdict_ZlibDecompressor(state, self) < 0) {
  1496. Py_DECREF(self);
  1497. return NULL;
  1498. }
  1499. }
  1500. return (PyObject *)self;
  1501. case Z_STREAM_ERROR:
  1502. Py_DECREF(self);
  1503. PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
  1504. return NULL;
  1505. case Z_MEM_ERROR:
  1506. Py_DECREF(self);
  1507. PyErr_SetString(PyExc_MemoryError,
  1508. "Can't allocate memory for decompression object");
  1509. return NULL;
  1510. default:
  1511. zlib_error(state, self->zst, err, "while creating decompression object");
  1512. Py_DECREF(self);
  1513. return NULL;
  1514. }
  1515. }
  1516. #include "clinic/zlibmodule.c.h"
  1517. static PyMethodDef comp_methods[] =
  1518. {
  1519. ZLIB_COMPRESS_COMPRESS_METHODDEF
  1520. ZLIB_COMPRESS_FLUSH_METHODDEF
  1521. ZLIB_COMPRESS_COPY_METHODDEF
  1522. ZLIB_COMPRESS___COPY___METHODDEF
  1523. ZLIB_COMPRESS___DEEPCOPY___METHODDEF
  1524. {NULL, NULL}
  1525. };
  1526. static PyMethodDef Decomp_methods[] =
  1527. {
  1528. ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
  1529. ZLIB_DECOMPRESS_FLUSH_METHODDEF
  1530. ZLIB_DECOMPRESS_COPY_METHODDEF
  1531. ZLIB_DECOMPRESS___COPY___METHODDEF
  1532. ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
  1533. {NULL, NULL}
  1534. };
  1535. static PyMethodDef ZlibDecompressor_methods[] = {
  1536. ZLIB_ZLIBDECOMPRESSOR_DECOMPRESS_METHODDEF
  1537. {NULL}
  1538. };
  1539. #define COMP_OFF(x) offsetof(compobject, x)
  1540. static PyMemberDef Decomp_members[] = {
  1541. {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
  1542. {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
  1543. {"eof", T_BOOL, COMP_OFF(eof), READONLY},
  1544. {NULL},
  1545. };
  1546. PyDoc_STRVAR(ZlibDecompressor_eof__doc__,
  1547. "True if the end-of-stream marker has been reached.");
  1548. PyDoc_STRVAR(ZlibDecompressor_unused_data__doc__,
  1549. "Data found after the end of the compressed stream.");
  1550. PyDoc_STRVAR(ZlibDecompressor_needs_input_doc,
  1551. "True if more input is needed before more decompressed data can be produced.");
  1552. static PyMemberDef ZlibDecompressor_members[] = {
  1553. {"eof", T_BOOL, offsetof(ZlibDecompressor, eof),
  1554. READONLY, ZlibDecompressor_eof__doc__},
  1555. {"unused_data", T_OBJECT_EX, offsetof(ZlibDecompressor, unused_data),
  1556. READONLY, ZlibDecompressor_unused_data__doc__},
  1557. {"needs_input", T_BOOL, offsetof(ZlibDecompressor, needs_input), READONLY,
  1558. ZlibDecompressor_needs_input_doc},
  1559. {NULL},
  1560. };
  1561. /*[clinic input]
  1562. zlib.adler32
  1563. data: Py_buffer
  1564. value: unsigned_int(bitwise=True) = 1
  1565. Starting value of the checksum.
  1566. /
  1567. Compute an Adler-32 checksum of data.
  1568. The returned checksum is an integer.
  1569. [clinic start generated code]*/
  1570. static PyObject *
  1571. zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
  1572. /*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
  1573. {
  1574. /* Releasing the GIL for very small buffers is inefficient
  1575. and may lower performance */
  1576. if (data->len > 1024*5) {
  1577. unsigned char *buf = data->buf;
  1578. Py_ssize_t len = data->len;
  1579. Py_BEGIN_ALLOW_THREADS
  1580. /* Avoid truncation of length for very large buffers. adler32() takes
  1581. length as an unsigned int, which may be narrower than Py_ssize_t. */
  1582. while ((size_t)len > UINT_MAX) {
  1583. value = adler32(value, buf, UINT_MAX);
  1584. buf += (size_t) UINT_MAX;
  1585. len -= (size_t) UINT_MAX;
  1586. }
  1587. value = adler32(value, buf, (unsigned int)len);
  1588. Py_END_ALLOW_THREADS
  1589. } else {
  1590. value = adler32(value, data->buf, (unsigned int)data->len);
  1591. }
  1592. return PyLong_FromUnsignedLong(value & 0xffffffffU);
  1593. }
  1594. /*[clinic input]
  1595. zlib.crc32 -> unsigned_int
  1596. data: Py_buffer
  1597. value: unsigned_int(bitwise=True) = 0
  1598. Starting value of the checksum.
  1599. /
  1600. Compute a CRC-32 checksum of data.
  1601. The returned checksum is an integer.
  1602. [clinic start generated code]*/
  1603. static unsigned int
  1604. zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
  1605. /*[clinic end generated code: output=b217562e4fe6d6a6 input=1229cb2fb5ea948a]*/
  1606. {
  1607. /* Releasing the GIL for very small buffers is inefficient
  1608. and may lower performance */
  1609. if (data->len > 1024*5) {
  1610. unsigned char *buf = data->buf;
  1611. Py_ssize_t len = data->len;
  1612. Py_BEGIN_ALLOW_THREADS
  1613. /* Avoid truncation of length for very large buffers. crc32() takes
  1614. length as an unsigned int, which may be narrower than Py_ssize_t.
  1615. We further limit size due to bugs in Apple's macOS zlib.
  1616. See https://github.com/python/cpython/issues/105967.
  1617. */
  1618. #define ZLIB_CRC_CHUNK_SIZE 0x40000000
  1619. #if ZLIB_CRC_CHUNK_SIZE > INT_MAX
  1620. # error "unsupported less than 32-bit platform?"
  1621. #endif
  1622. while ((size_t)len > ZLIB_CRC_CHUNK_SIZE) {
  1623. value = crc32(value, buf, ZLIB_CRC_CHUNK_SIZE);
  1624. buf += (size_t) ZLIB_CRC_CHUNK_SIZE;
  1625. len -= (size_t) ZLIB_CRC_CHUNK_SIZE;
  1626. }
  1627. #undef ZLIB_CRC_CHUNK_SIZE
  1628. value = crc32(value, buf, (unsigned int)len);
  1629. Py_END_ALLOW_THREADS
  1630. } else {
  1631. value = crc32(value, data->buf, (unsigned int)data->len);
  1632. }
  1633. return value;
  1634. }
  1635. static PyMethodDef zlib_methods[] =
  1636. {
  1637. ZLIB_ADLER32_METHODDEF
  1638. ZLIB_COMPRESS_METHODDEF
  1639. ZLIB_COMPRESSOBJ_METHODDEF
  1640. ZLIB_CRC32_METHODDEF
  1641. ZLIB_DECOMPRESS_METHODDEF
  1642. ZLIB_DECOMPRESSOBJ_METHODDEF
  1643. {NULL, NULL}
  1644. };
  1645. static PyType_Slot Comptype_slots[] = {
  1646. {Py_tp_dealloc, Comp_dealloc},
  1647. {Py_tp_methods, comp_methods},
  1648. {0, 0},
  1649. };
  1650. static PyType_Spec Comptype_spec = {
  1651. .name = "zlib.Compress",
  1652. .basicsize = sizeof(compobject),
  1653. .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
  1654. .slots= Comptype_slots,
  1655. };
  1656. static PyType_Slot Decomptype_slots[] = {
  1657. {Py_tp_dealloc, Decomp_dealloc},
  1658. {Py_tp_methods, Decomp_methods},
  1659. {Py_tp_members, Decomp_members},
  1660. {0, 0},
  1661. };
  1662. static PyType_Spec Decomptype_spec = {
  1663. .name = "zlib.Decompress",
  1664. .basicsize = sizeof(compobject),
  1665. .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
  1666. .slots = Decomptype_slots,
  1667. };
  1668. static PyType_Slot ZlibDecompressor_type_slots[] = {
  1669. {Py_tp_dealloc, ZlibDecompressor_dealloc},
  1670. {Py_tp_members, ZlibDecompressor_members},
  1671. {Py_tp_new, ZlibDecompressor__new__},
  1672. {Py_tp_doc, (char *)ZlibDecompressor__new____doc__},
  1673. {Py_tp_methods, ZlibDecompressor_methods},
  1674. {0, 0},
  1675. };
  1676. static PyType_Spec ZlibDecompressor_type_spec = {
  1677. .name = "zlib._ZlibDecompressor",
  1678. .basicsize = sizeof(ZlibDecompressor),
  1679. // Calling PyType_GetModuleState() on a subclass is not safe.
  1680. // ZlibDecompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
  1681. // which prevents to create a subclass.
  1682. // So calling PyType_GetModuleState() in this file is always safe.
  1683. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
  1684. .slots = ZlibDecompressor_type_slots,
  1685. };
  1686. PyDoc_STRVAR(zlib_module_documentation,
  1687. "The functions in this module allow compression and decompression using the\n"
  1688. "zlib library, which is based on GNU zip.\n"
  1689. "\n"
  1690. "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
  1691. "compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
  1692. "compressobj([level[, ...]]) -- Return a compressor object.\n"
  1693. "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
  1694. "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
  1695. "decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
  1696. "\n"
  1697. "'wbits' is window buffer size and container format.\n"
  1698. "Compressor objects support compress() and flush() methods; decompressor\n"
  1699. "objects support decompress() and flush().");
  1700. static int
  1701. zlib_clear(PyObject *mod)
  1702. {
  1703. zlibstate *state = get_zlib_state(mod);
  1704. Py_CLEAR(state->Comptype);
  1705. Py_CLEAR(state->Decomptype);
  1706. Py_CLEAR(state->ZlibDecompressorType);
  1707. Py_CLEAR(state->ZlibError);
  1708. return 0;
  1709. }
  1710. static int
  1711. zlib_traverse(PyObject *mod, visitproc visit, void *arg)
  1712. {
  1713. zlibstate *state = get_zlib_state(mod);
  1714. Py_VISIT(state->Comptype);
  1715. Py_VISIT(state->Decomptype);
  1716. Py_VISIT(state->ZlibDecompressorType);
  1717. Py_VISIT(state->ZlibError);
  1718. return 0;
  1719. }
  1720. static void
  1721. zlib_free(void *mod)
  1722. {
  1723. zlib_clear((PyObject *)mod);
  1724. }
  1725. static int
  1726. zlib_exec(PyObject *mod)
  1727. {
  1728. zlibstate *state = get_zlib_state(mod);
  1729. state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
  1730. mod, &Comptype_spec, NULL);
  1731. if (state->Comptype == NULL) {
  1732. return -1;
  1733. }
  1734. state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
  1735. mod, &Decomptype_spec, NULL);
  1736. if (state->Decomptype == NULL) {
  1737. return -1;
  1738. }
  1739. state->ZlibDecompressorType = (PyTypeObject *)PyType_FromModuleAndSpec(
  1740. mod, &ZlibDecompressor_type_spec, NULL);
  1741. if (state->ZlibDecompressorType == NULL) {
  1742. return -1;
  1743. }
  1744. state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
  1745. if (state->ZlibError == NULL) {
  1746. return -1;
  1747. }
  1748. if (PyModule_AddObject(mod, "error", Py_NewRef(state->ZlibError)) < 0) {
  1749. Py_DECREF(state->ZlibError);
  1750. return -1;
  1751. }
  1752. if (PyModule_AddObject(mod, "_ZlibDecompressor",
  1753. Py_NewRef(state->ZlibDecompressorType)) < 0) {
  1754. Py_DECREF(state->ZlibDecompressorType);
  1755. return -1;
  1756. }
  1757. #define ZLIB_ADD_INT_MACRO(c) \
  1758. do { \
  1759. if ((PyModule_AddIntConstant(mod, #c, c)) < 0) { \
  1760. return -1; \
  1761. } \
  1762. } while(0)
  1763. ZLIB_ADD_INT_MACRO(MAX_WBITS);
  1764. ZLIB_ADD_INT_MACRO(DEFLATED);
  1765. ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
  1766. ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
  1767. // compression levels
  1768. ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
  1769. ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
  1770. ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
  1771. ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
  1772. // compression strategies
  1773. ZLIB_ADD_INT_MACRO(Z_FILTERED);
  1774. ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
  1775. #ifdef Z_RLE // 1.2.0.1
  1776. ZLIB_ADD_INT_MACRO(Z_RLE);
  1777. #endif
  1778. #ifdef Z_FIXED // 1.2.2.2
  1779. ZLIB_ADD_INT_MACRO(Z_FIXED);
  1780. #endif
  1781. ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
  1782. // allowed flush values
  1783. ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
  1784. ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
  1785. ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
  1786. ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
  1787. ZLIB_ADD_INT_MACRO(Z_FINISH);
  1788. #ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
  1789. ZLIB_ADD_INT_MACRO(Z_BLOCK);
  1790. #endif
  1791. #ifdef Z_TREES // 1.2.3.4, only for inflate
  1792. ZLIB_ADD_INT_MACRO(Z_TREES);
  1793. #endif
  1794. PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
  1795. if (ver == NULL) {
  1796. return -1;
  1797. }
  1798. if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
  1799. Py_DECREF(ver);
  1800. return -1;
  1801. }
  1802. ver = PyUnicode_FromString(zlibVersion());
  1803. if (ver == NULL) {
  1804. return -1;
  1805. }
  1806. if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
  1807. Py_DECREF(ver);
  1808. return -1;
  1809. }
  1810. if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
  1811. return -1;
  1812. }
  1813. return 0;
  1814. }
  1815. static PyModuleDef_Slot zlib_slots[] = {
  1816. {Py_mod_exec, zlib_exec},
  1817. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  1818. {0, NULL}
  1819. };
  1820. static struct PyModuleDef zlibmodule = {
  1821. PyModuleDef_HEAD_INIT,
  1822. .m_name = "zlib",
  1823. .m_doc = zlib_module_documentation,
  1824. .m_size = sizeof(zlibstate),
  1825. .m_methods = zlib_methods,
  1826. .m_slots = zlib_slots,
  1827. .m_traverse = zlib_traverse,
  1828. .m_clear = zlib_clear,
  1829. .m_free = zlib_free,
  1830. };
  1831. PyMODINIT_FUNC
  1832. PyInit_zlib(void)
  1833. {
  1834. return PyModuleDef_Init(&zlibmodule);
  1835. }