comparison mupdf-source/include/mupdf/fitz/buffer.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2023 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 #ifndef MUPDF_FITZ_BUFFER_H
24 #define MUPDF_FITZ_BUFFER_H
25
26 #include "mupdf/fitz/system.h"
27 #include "mupdf/fitz/context.h"
28
29 /**
30 fz_buffer is a wrapper around a dynamically allocated array of
31 bytes.
32
33 Buffers have a capacity (the number of bytes storage immediately
34 available) and a current size.
35
36 The contents of the structure are considered implementation
37 details and are subject to change. Users should use the accessor
38 functions in preference.
39 */
40 typedef struct
41 {
42 int refs;
43 unsigned char *data;
44 size_t cap, len;
45 int unused_bits;
46 int shared;
47 } fz_buffer;
48
49 /**
50 Take an additional reference to the buffer. The same pointer
51 is returned.
52
53 Never throws exceptions.
54 */
55 fz_buffer *fz_keep_buffer(fz_context *ctx, fz_buffer *buf);
56
57 /**
58 Drop a reference to the buffer. When the reference count reaches
59 zero, the buffer is destroyed.
60
61 Never throws exceptions.
62 */
63 void fz_drop_buffer(fz_context *ctx, fz_buffer *buf);
64
65 /**
66 Retrieve internal memory of buffer.
67
68 datap: Output parameter that will be pointed to the data.
69
70 Returns the current size of the data in bytes.
71 */
72 size_t fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap);
73
74 /**
75 Ensure that a buffer's data ends in a
76 0 byte, and return a pointer to it.
77 */
78 const char *fz_string_from_buffer(fz_context *ctx, fz_buffer *buf);
79
80 fz_buffer *fz_new_buffer(fz_context *ctx, size_t capacity);
81
82 /**
83 Create a new buffer with existing data.
84
85 data: Pointer to existing data.
86 size: Size of existing data.
87
88 Takes ownership of data. Does not make a copy. Calls fz_free on
89 the data when the buffer is deallocated. Do not use 'data' after
90 passing to this function.
91
92 Returns pointer to new buffer. Throws exception on allocation
93 failure.
94 */
95 fz_buffer *fz_new_buffer_from_data(fz_context *ctx, unsigned char *data, size_t size);
96
97 /**
98 Like fz_new_buffer, but does not take ownership.
99 */
100 fz_buffer *fz_new_buffer_from_shared_data(fz_context *ctx, const unsigned char *data, size_t size);
101
102 /**
103 Create a new buffer containing a copy of the passed data.
104 */
105 fz_buffer *fz_new_buffer_from_copied_data(fz_context *ctx, const unsigned char *data, size_t size);
106
107 /**
108 Make a new buffer, containing a copy of the data used in
109 the original.
110 */
111 fz_buffer *fz_clone_buffer(fz_context *ctx, fz_buffer *buf);
112
113 /**
114 Create a new buffer with data decoded from a base64 input string.
115 */
116 fz_buffer *fz_new_buffer_from_base64(fz_context *ctx, const char *data, size_t size);
117
118 /**
119 Ensure that a buffer has a given capacity,
120 truncating data if required.
121
122 capacity: The desired capacity for the buffer. If the current
123 size of the buffer contents is smaller than capacity, it is
124 truncated.
125 */
126 void fz_resize_buffer(fz_context *ctx, fz_buffer *buf, size_t capacity);
127
128 /**
129 Make some space within a buffer (i.e. ensure that
130 capacity > size).
131 */
132 void fz_grow_buffer(fz_context *ctx, fz_buffer *buf);
133
134 /**
135 Trim wasted capacity from a buffer by resizing internal memory.
136 */
137 void fz_trim_buffer(fz_context *ctx, fz_buffer *buf);
138
139 /**
140 Empties the buffer. Storage is not freed, but is held ready
141 to be reused as the buffer is refilled.
142
143 Never throws exceptions.
144 */
145 void fz_clear_buffer(fz_context *ctx, fz_buffer *buf);
146
147 /**
148 Create a new buffer with a (subset of) the data from the buffer.
149
150 start: if >= 0, offset from start of buffer, if < 0 offset from end of buffer.
151
152 end: if >= 0, offset from start of buffer, if < 0 offset from end of buffer.
153
154 */
155 fz_buffer *fz_slice_buffer(fz_context *ctx, fz_buffer *buf, int64_t start, int64_t end);
156
157 /**
158 Append the contents of the source buffer onto the end of the
159 destination buffer, extending automatically as required.
160
161 Ownership of buffers does not change.
162 */
163 void fz_append_buffer(fz_context *ctx, fz_buffer *destination, fz_buffer *source);
164
165 /**
166 Write a base64 encoded data block, optionally with periodic newlines.
167 */
168 void fz_append_base64(fz_context *ctx, fz_buffer *out, const unsigned char *data, size_t size, int newline);
169
170 /**
171 Append a base64 encoded fz_buffer, optionally with periodic newlines.
172 */
173 void fz_append_base64_buffer(fz_context *ctx, fz_buffer *out, fz_buffer *data, int newline);
174
175 /**
176 fz_append_*: Append data to a buffer.
177
178 The buffer will automatically grow as required.
179 */
180 void fz_append_data(fz_context *ctx, fz_buffer *buf, const void *data, size_t len);
181 void fz_append_string(fz_context *ctx, fz_buffer *buf, const char *data);
182 void fz_append_byte(fz_context *ctx, fz_buffer *buf, int c);
183 void fz_append_rune(fz_context *ctx, fz_buffer *buf, int c);
184 void fz_append_int32_le(fz_context *ctx, fz_buffer *buf, int x);
185 void fz_append_int16_le(fz_context *ctx, fz_buffer *buf, int x);
186 void fz_append_int32_be(fz_context *ctx, fz_buffer *buf, int x);
187 void fz_append_int16_be(fz_context *ctx, fz_buffer *buf, int x);
188 void fz_append_bits(fz_context *ctx, fz_buffer *buf, int value, int count);
189 void fz_append_bits_pad(fz_context *ctx, fz_buffer *buf);
190
191 /**
192 fz_append_pdf_string: Append a string with PDF syntax quotes and
193 escapes.
194
195 The buffer will automatically grow as required.
196 */
197 void fz_append_pdf_string(fz_context *ctx, fz_buffer *buffer, const char *text);
198
199 /**
200 fz_append_printf: Format and append data to buffer using
201 printf-like formatting (see fz_vsnprintf).
202
203 The buffer will automatically grow as required.
204 */
205 void fz_append_printf(fz_context *ctx, fz_buffer *buffer, const char *fmt, ...);
206
207 /**
208 fz_append_vprintf: Format and append data to buffer using
209 printf-like formatting with varargs (see fz_vsnprintf).
210 */
211 void fz_append_vprintf(fz_context *ctx, fz_buffer *buffer, const char *fmt, va_list args);
212
213 /**
214 Zero-terminate buffer in order to use as a C string.
215
216 This byte is invisible and does not affect the length of the
217 buffer as returned by fz_buffer_storage. The zero byte is
218 written *after* the data, and subsequent writes will overwrite
219 the terminating byte.
220
221 Subsequent changes to the size of the buffer (such as by
222 fz_buffer_trim, fz_buffer_grow, fz_resize_buffer, etc) may
223 invalidate this.
224 */
225 void fz_terminate_buffer(fz_context *ctx, fz_buffer *buf);
226
227 /**
228 Create an MD5 digest from buffer contents.
229
230 Never throws exceptions.
231 */
232 void fz_md5_buffer(fz_context *ctx, fz_buffer *buffer, unsigned char digest[16]);
233
234 /**
235 Take ownership of buffer contents.
236
237 Performs the same task as fz_buffer_storage, but ownership of
238 the data buffer returns with this call. The buffer is left
239 empty.
240
241 Note: Bad things may happen if this is called on a buffer with
242 multiple references that is being used from multiple threads.
243
244 data: Pointer to place to retrieve data pointer.
245
246 Returns length of stream.
247 */
248 size_t fz_buffer_extract(fz_context *ctx, fz_buffer *buf, unsigned char **data);
249
250 #endif