cpython/Modules/_zstd/buffer.h
Miss Islington (bot) b7168d216b
Some checks are pending
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Check if the ABI has changed (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
[3.14] GH-136975: Emend a spelling error (algorthm -> algorithm) (GH-136999) (#137003)
GH-136975: Emend a spelling error (algorthm -> algorithm) (GH-136999)
(cherry picked from commit b6d3242244)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
2025-07-23 23:48:11 +03:00

108 lines
2.8 KiB
C

/* Low level interface to the Zstandard algorithm & the zstd library. */
#ifndef ZSTD_BUFFER_H
#define ZSTD_BUFFER_H
#include "pycore_blocks_output_buffer.h"
#include <zstd.h> // ZSTD_outBuffer
/* Blocks output buffer wrapper code */
/* Initialize the buffer, and grow the buffer.
Return 0 on success
Return -1 on failure */
static inline int
_OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
Py_ssize_t max_length)
{
/* Ensure .list was set to NULL */
assert(buffer->list == NULL);
Py_ssize_t res = _BlocksOutputBuffer_InitAndGrow(buffer, max_length,
&ob->dst);
if (res < 0) {
return -1;
}
ob->size = (size_t) res;
ob->pos = 0;
return 0;
}
/* Initialize the buffer, with an initial size.
init_size: the initial size.
Return 0 on success
Return -1 on failure */
static inline int
_OutputBuffer_InitWithSize(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
Py_ssize_t max_length, Py_ssize_t init_size)
{
Py_ssize_t block_size;
/* Ensure .list was set to NULL */
assert(buffer->list == NULL);
/* Get block size */
if (0 <= max_length && max_length < init_size) {
block_size = max_length;
}
else {
block_size = init_size;
}
Py_ssize_t res = _BlocksOutputBuffer_InitWithSize(buffer, block_size,
&ob->dst);
if (res < 0) {
return -1;
}
// Set max_length, InitWithSize doesn't do this
buffer->max_length = max_length;
ob->size = (size_t) res;
ob->pos = 0;
return 0;
}
/* Grow the buffer.
Return 0 on success
Return -1 on failure */
static inline int
_OutputBuffer_Grow(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob)
{
assert(ob->pos == ob->size);
Py_ssize_t res = _BlocksOutputBuffer_Grow(buffer, &ob->dst, 0);
if (res < 0) {
return -1;
}
ob->size = (size_t) res;
ob->pos = 0;
return 0;
}
/* Finish the buffer.
Return a bytes object on success
Return NULL on failure */
static inline PyObject *
_OutputBuffer_Finish(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob)
{
return _BlocksOutputBuffer_Finish(buffer, ob->size - ob->pos);
}
/* Clean up the buffer */
static inline void
_OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
{
_BlocksOutputBuffer_OnError(buffer);
}
/* Whether the output data has reached max_length.
The avail_out must be 0, please check it before calling. */
static inline int
_OutputBuffer_ReachedMaxLength(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob)
{
/* Ensure (data size == allocated size) */
assert(ob->pos == ob->size);
return buffer->allocated == buffer->max_length;
}
#endif // !ZSTD_BUFFER_H