17db96d56Sopenharmony_ci#ifndef Py_CPYTHON_BYTESOBJECT_H
27db96d56Sopenharmony_ci#  error "this header file must not be included directly"
37db96d56Sopenharmony_ci#endif
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_citypedef struct {
67db96d56Sopenharmony_ci    PyObject_VAR_HEAD
77db96d56Sopenharmony_ci    Py_DEPRECATED(3.11) Py_hash_t ob_shash;
87db96d56Sopenharmony_ci    char ob_sval[1];
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci    /* Invariants:
117db96d56Sopenharmony_ci     *     ob_sval contains space for 'ob_size+1' elements.
127db96d56Sopenharmony_ci     *     ob_sval[ob_size] == 0.
137db96d56Sopenharmony_ci     *     ob_shash is the hash of the byte string or -1 if not computed yet.
147db96d56Sopenharmony_ci     */
157db96d56Sopenharmony_ci} PyBytesObject;
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ciPyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
187db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
197db96d56Sopenharmony_ci    const char *format,
207db96d56Sopenharmony_ci    Py_ssize_t format_len,
217db96d56Sopenharmony_ci    PyObject *args,
227db96d56Sopenharmony_ci    int use_bytearray);
237db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) _PyBytes_FromHex(
247db96d56Sopenharmony_ci    PyObject *string,
257db96d56Sopenharmony_ci    int use_bytearray);
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ci/* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
287db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
297db96d56Sopenharmony_ci                                             const char *, const char **);
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci/* Macros and static inline functions, trading safety for speed */
327db96d56Sopenharmony_ci#define _PyBytes_CAST(op) \
337db96d56Sopenharmony_ci    (assert(PyBytes_Check(op)), _Py_CAST(PyBytesObject*, op))
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_cistatic inline char* PyBytes_AS_STRING(PyObject *op)
367db96d56Sopenharmony_ci{
377db96d56Sopenharmony_ci    return _PyBytes_CAST(op)->ob_sval;
387db96d56Sopenharmony_ci}
397db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
407db96d56Sopenharmony_ci#  define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op))
417db96d56Sopenharmony_ci#endif
427db96d56Sopenharmony_ci
437db96d56Sopenharmony_cistatic inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) {
447db96d56Sopenharmony_ci    PyBytesObject *self = _PyBytes_CAST(op);
457db96d56Sopenharmony_ci    return Py_SIZE(self);
467db96d56Sopenharmony_ci}
477db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
487db96d56Sopenharmony_ci#  define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self))
497db96d56Sopenharmony_ci#endif
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ci/* _PyBytes_Join(sep, x) is like sep.join(x).  sep must be PyBytesObject*,
527db96d56Sopenharmony_ci   x must be an iterable object. */
537db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ci/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
577db96d56Sopenharmony_ci   A _PyBytesWriter variable must be declared at the end of variables in a
587db96d56Sopenharmony_ci   function to optimize the memory allocation on the stack. */
597db96d56Sopenharmony_citypedef struct {
607db96d56Sopenharmony_ci    /* bytes, bytearray or NULL (when the small buffer is used) */
617db96d56Sopenharmony_ci    PyObject *buffer;
627db96d56Sopenharmony_ci
637db96d56Sopenharmony_ci    /* Number of allocated size. */
647db96d56Sopenharmony_ci    Py_ssize_t allocated;
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci    /* Minimum number of allocated bytes,
677db96d56Sopenharmony_ci       incremented by _PyBytesWriter_Prepare() */
687db96d56Sopenharmony_ci    Py_ssize_t min_size;
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ci    /* If non-zero, use a bytearray instead of a bytes object for buffer. */
717db96d56Sopenharmony_ci    int use_bytearray;
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ci    /* If non-zero, overallocate the buffer (default: 0).
747db96d56Sopenharmony_ci       This flag must be zero if use_bytearray is non-zero. */
757db96d56Sopenharmony_ci    int overallocate;
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci    /* Stack buffer */
787db96d56Sopenharmony_ci    int use_small_buffer;
797db96d56Sopenharmony_ci    char small_buffer[512];
807db96d56Sopenharmony_ci} _PyBytesWriter;
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ci/* Initialize a bytes writer
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci   By default, the overallocation is disabled. Set the overallocate attribute
857db96d56Sopenharmony_ci   to control the allocation of the buffer. */
867db96d56Sopenharmony_ciPyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
877db96d56Sopenharmony_ci
887db96d56Sopenharmony_ci/* Get the buffer content and reset the writer.
897db96d56Sopenharmony_ci   Return a bytes object, or a bytearray object if use_bytearray is non-zero.
907db96d56Sopenharmony_ci   Raise an exception and return NULL on error. */
917db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
927db96d56Sopenharmony_ci    void *str);
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci/* Deallocate memory of a writer (clear its internal buffer). */
957db96d56Sopenharmony_ciPyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci/* Allocate the buffer to write size bytes.
987db96d56Sopenharmony_ci   Return the pointer to the beginning of buffer data.
997db96d56Sopenharmony_ci   Raise an exception and return NULL on error. */
1007db96d56Sopenharmony_ciPyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
1017db96d56Sopenharmony_ci    Py_ssize_t size);
1027db96d56Sopenharmony_ci
1037db96d56Sopenharmony_ci/* Ensure that the buffer is large enough to write *size* bytes.
1047db96d56Sopenharmony_ci   Add size to the writer minimum size (min_size attribute).
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ci   str is the current pointer inside the buffer.
1077db96d56Sopenharmony_ci   Return the updated current pointer inside the buffer.
1087db96d56Sopenharmony_ci   Raise an exception and return NULL on error. */
1097db96d56Sopenharmony_ciPyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
1107db96d56Sopenharmony_ci    void *str,
1117db96d56Sopenharmony_ci    Py_ssize_t size);
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ci/* Resize the buffer to make it larger.
1147db96d56Sopenharmony_ci   The new buffer may be larger than size bytes because of overallocation.
1157db96d56Sopenharmony_ci   Return the updated current pointer inside the buffer.
1167db96d56Sopenharmony_ci   Raise an exception and return NULL on error.
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ci   Note: size must be greater than the number of allocated bytes in the writer.
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci   This function doesn't use the writer minimum size (min_size attribute).
1217db96d56Sopenharmony_ci
1227db96d56Sopenharmony_ci   See also _PyBytesWriter_Prepare().
1237db96d56Sopenharmony_ci   */
1247db96d56Sopenharmony_ciPyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
1257db96d56Sopenharmony_ci    void *str,
1267db96d56Sopenharmony_ci    Py_ssize_t size);
1277db96d56Sopenharmony_ci
1287db96d56Sopenharmony_ci/* Write bytes.
1297db96d56Sopenharmony_ci   Raise an exception and return NULL on error. */
1307db96d56Sopenharmony_ciPyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
1317db96d56Sopenharmony_ci    void *str,
1327db96d56Sopenharmony_ci    const void *bytes,
1337db96d56Sopenharmony_ci    Py_ssize_t size);
134