1 #ifndef Py_CPYTHON_BYTESOBJECT_H
2 #  error "this header file must not be included directly"
3 #endif
4 
5 typedef struct {
6     PyObject_VAR_HEAD
7     Py_hash_t ob_shash;
8     char ob_sval[1];
9 
10     /* Invariants:
11      *     ob_sval contains space for 'ob_size+1' elements.
12      *     ob_sval[ob_size] == 0.
13      *     ob_shash is the hash of the byte string or -1 if not computed yet.
14      */
15 } PyBytesObject;
16 
17 PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
18 PyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
19     const char *format,
20     Py_ssize_t format_len,
21     PyObject *args,
22     int use_bytearray);
23 PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
24     PyObject *string,
25     int use_bytearray);
26 
27 /* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
28 PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
29                                              const char *, const char **);
30 
31 /* Macro, trading safety for speed */
32 #define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
33                                 (((PyBytesObject *)(op))->ob_sval))
34 #define PyBytes_GET_SIZE(op)  (assert(PyBytes_Check(op)),Py_SIZE(op))
35 
36 /* _PyBytes_Join(sep, x) is like sep.join(x).  sep must be PyBytesObject*,
37    x must be an iterable object. */
38 PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
39 
40 
41 /* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
42    A _PyBytesWriter variable must be declared at the end of variables in a
43    function to optimize the memory allocation on the stack. */
44 typedef struct {
45     /* bytes, bytearray or NULL (when the small buffer is used) */
46     PyObject *buffer;
47 
48     /* Number of allocated size. */
49     Py_ssize_t allocated;
50 
51     /* Minimum number of allocated bytes,
52        incremented by _PyBytesWriter_Prepare() */
53     Py_ssize_t min_size;
54 
55     /* If non-zero, use a bytearray instead of a bytes object for buffer. */
56     int use_bytearray;
57 
58     /* If non-zero, overallocate the buffer (default: 0).
59        This flag must be zero if use_bytearray is non-zero. */
60     int overallocate;
61 
62     /* Stack buffer */
63     int use_small_buffer;
64     char small_buffer[512];
65 } _PyBytesWriter;
66 
67 /* Initialize a bytes writer
68 
69    By default, the overallocation is disabled. Set the overallocate attribute
70    to control the allocation of the buffer. */
71 PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
72 
73 /* Get the buffer content and reset the writer.
74    Return a bytes object, or a bytearray object if use_bytearray is non-zero.
75    Raise an exception and return NULL on error. */
76 PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
77     void *str);
78 
79 /* Deallocate memory of a writer (clear its internal buffer). */
80 PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
81 
82 /* Allocate the buffer to write size bytes.
83    Return the pointer to the beginning of buffer data.
84    Raise an exception and return NULL on error. */
85 PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
86     Py_ssize_t size);
87 
88 /* Ensure that the buffer is large enough to write *size* bytes.
89    Add size to the writer minimum size (min_size attribute).
90 
91    str is the current pointer inside the buffer.
92    Return the updated current pointer inside the buffer.
93    Raise an exception and return NULL on error. */
94 PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
95     void *str,
96     Py_ssize_t size);
97 
98 /* Resize the buffer to make it larger.
99    The new buffer may be larger than size bytes because of overallocation.
100    Return the updated current pointer inside the buffer.
101    Raise an exception and return NULL on error.
102 
103    Note: size must be greater than the number of allocated bytes in the writer.
104 
105    This function doesn't use the writer minimum size (min_size attribute).
106 
107    See also _PyBytesWriter_Prepare().
108    */
109 PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
110     void *str,
111     Py_ssize_t size);
112 
113 /* Write bytes.
114    Raise an exception and return NULL on error. */
115 PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
116     void *str,
117     const void *bytes,
118     Py_ssize_t size);
119