buffer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #ifndef MUPDF_FITZ_BUFFER_H
  23. #define MUPDF_FITZ_BUFFER_H
  24. #include "mupdf/fitz/system.h"
  25. #include "mupdf/fitz/context.h"
  26. /**
  27. fz_buffer is a wrapper around a dynamically allocated array of
  28. bytes.
  29. Buffers have a capacity (the number of bytes storage immediately
  30. available) and a current size.
  31. The contents of the structure are considered implementation
  32. details and are subject to change. Users should use the accessor
  33. functions in preference.
  34. */
  35. typedef struct
  36. {
  37. int refs;
  38. unsigned char *data;
  39. size_t cap, len;
  40. int unused_bits;
  41. int shared;
  42. } fz_buffer;
  43. /**
  44. Take an additional reference to the buffer. The same pointer
  45. is returned.
  46. Never throws exceptions.
  47. */
  48. fz_buffer *fz_keep_buffer(fz_context *ctx, fz_buffer *buf);
  49. /**
  50. Drop a reference to the buffer. When the reference count reaches
  51. zero, the buffer is destroyed.
  52. Never throws exceptions.
  53. */
  54. void fz_drop_buffer(fz_context *ctx, fz_buffer *buf);
  55. /**
  56. Retrieve internal memory of buffer.
  57. datap: Output parameter that will be pointed to the data.
  58. Returns the current size of the data in bytes.
  59. */
  60. size_t fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap);
  61. /**
  62. Ensure that a buffer's data ends in a
  63. 0 byte, and return a pointer to it.
  64. */
  65. const char *fz_string_from_buffer(fz_context *ctx, fz_buffer *buf);
  66. fz_buffer *fz_new_buffer(fz_context *ctx, size_t capacity);
  67. /**
  68. Create a new buffer with existing data.
  69. data: Pointer to existing data.
  70. size: Size of existing data.
  71. Takes ownership of data. Does not make a copy. Calls fz_free on
  72. the data when the buffer is deallocated. Do not use 'data' after
  73. passing to this function.
  74. Returns pointer to new buffer. Throws exception on allocation
  75. failure.
  76. */
  77. fz_buffer *fz_new_buffer_from_data(fz_context *ctx, unsigned char *data, size_t size);
  78. /**
  79. Like fz_new_buffer, but does not take ownership.
  80. */
  81. fz_buffer *fz_new_buffer_from_shared_data(fz_context *ctx, const unsigned char *data, size_t size);
  82. /**
  83. Create a new buffer containing a copy of the passed data.
  84. */
  85. fz_buffer *fz_new_buffer_from_copied_data(fz_context *ctx, const unsigned char *data, size_t size);
  86. /**
  87. Make a new buffer, containing a copy of the data used in
  88. the original.
  89. */
  90. fz_buffer *fz_clone_buffer(fz_context *ctx, fz_buffer *buf);
  91. /**
  92. Create a new buffer with data decoded from a base64 input string.
  93. */
  94. fz_buffer *fz_new_buffer_from_base64(fz_context *ctx, const char *data, size_t size);
  95. /**
  96. Ensure that a buffer has a given capacity,
  97. truncating data if required.
  98. capacity: The desired capacity for the buffer. If the current
  99. size of the buffer contents is smaller than capacity, it is
  100. truncated.
  101. */
  102. void fz_resize_buffer(fz_context *ctx, fz_buffer *buf, size_t capacity);
  103. /**
  104. Make some space within a buffer (i.e. ensure that
  105. capacity > size).
  106. */
  107. void fz_grow_buffer(fz_context *ctx, fz_buffer *buf);
  108. /**
  109. Trim wasted capacity from a buffer by resizing internal memory.
  110. */
  111. void fz_trim_buffer(fz_context *ctx, fz_buffer *buf);
  112. /**
  113. Empties the buffer. Storage is not freed, but is held ready
  114. to be reused as the buffer is refilled.
  115. Never throws exceptions.
  116. */
  117. void fz_clear_buffer(fz_context *ctx, fz_buffer *buf);
  118. /**
  119. Create a new buffer with a (subset of) the data from the buffer.
  120. start: if >= 0, offset from start of buffer, if < 0 offset from end of buffer.
  121. end: if >= 0, offset from start of buffer, if < 0 offset from end of buffer.
  122. */
  123. fz_buffer *fz_slice_buffer(fz_context *ctx, fz_buffer *buf, int64_t start, int64_t end);
  124. /**
  125. Append the contents of the source buffer onto the end of the
  126. destination buffer, extending automatically as required.
  127. Ownership of buffers does not change.
  128. */
  129. void fz_append_buffer(fz_context *ctx, fz_buffer *destination, fz_buffer *source);
  130. /**
  131. Write a base64 encoded data block, optionally with periodic newlines.
  132. */
  133. void fz_append_base64(fz_context *ctx, fz_buffer *out, const unsigned char *data, size_t size, int newline);
  134. /**
  135. Append a base64 encoded fz_buffer, optionally with periodic newlines.
  136. */
  137. void fz_append_base64_buffer(fz_context *ctx, fz_buffer *out, fz_buffer *data, int newline);
  138. /**
  139. fz_append_*: Append data to a buffer.
  140. The buffer will automatically grow as required.
  141. */
  142. void fz_append_data(fz_context *ctx, fz_buffer *buf, const void *data, size_t len);
  143. void fz_append_string(fz_context *ctx, fz_buffer *buf, const char *data);
  144. void fz_append_byte(fz_context *ctx, fz_buffer *buf, int c);
  145. void fz_append_rune(fz_context *ctx, fz_buffer *buf, int c);
  146. void fz_append_int32_le(fz_context *ctx, fz_buffer *buf, int x);
  147. void fz_append_int16_le(fz_context *ctx, fz_buffer *buf, int x);
  148. void fz_append_int32_be(fz_context *ctx, fz_buffer *buf, int x);
  149. void fz_append_int16_be(fz_context *ctx, fz_buffer *buf, int x);
  150. void fz_append_bits(fz_context *ctx, fz_buffer *buf, int value, int count);
  151. void fz_append_bits_pad(fz_context *ctx, fz_buffer *buf);
  152. /**
  153. fz_append_pdf_string: Append a string with PDF syntax quotes and
  154. escapes.
  155. The buffer will automatically grow as required.
  156. */
  157. void fz_append_pdf_string(fz_context *ctx, fz_buffer *buffer, const char *text);
  158. /**
  159. fz_append_printf: Format and append data to buffer using
  160. printf-like formatting (see fz_vsnprintf).
  161. The buffer will automatically grow as required.
  162. */
  163. void fz_append_printf(fz_context *ctx, fz_buffer *buffer, const char *fmt, ...);
  164. /**
  165. fz_append_vprintf: Format and append data to buffer using
  166. printf-like formatting with varargs (see fz_vsnprintf).
  167. */
  168. void fz_append_vprintf(fz_context *ctx, fz_buffer *buffer, const char *fmt, va_list args);
  169. /**
  170. Zero-terminate buffer in order to use as a C string.
  171. This byte is invisible and does not affect the length of the
  172. buffer as returned by fz_buffer_storage. The zero byte is
  173. written *after* the data, and subsequent writes will overwrite
  174. the terminating byte.
  175. Subsequent changes to the size of the buffer (such as by
  176. fz_buffer_trim, fz_buffer_grow, fz_resize_buffer, etc) may
  177. invalidate this.
  178. */
  179. void fz_terminate_buffer(fz_context *ctx, fz_buffer *buf);
  180. /**
  181. Create an MD5 digest from buffer contents.
  182. Never throws exceptions.
  183. */
  184. void fz_md5_buffer(fz_context *ctx, fz_buffer *buffer, unsigned char digest[16]);
  185. /**
  186. Take ownership of buffer contents.
  187. Performs the same task as fz_buffer_storage, but ownership of
  188. the data buffer returns with this call. The buffer is left
  189. empty.
  190. Note: Bad things may happen if this is called on a buffer with
  191. multiple references that is being used from multiple threads.
  192. data: Pointer to place to retrieve data pointer.
  193. Returns length of stream.
  194. */
  195. size_t fz_buffer_extract(fz_context *ctx, fz_buffer *buf, unsigned char **data);
  196. #endif