blob: 8ebedca78876be03e60d150f63a519ae7828e03d [file] [log] [blame]
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009 Red Hat, Inc.
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +04303 * Copyright © 2018 Ebrahim Byagowi
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04006 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 */
27
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070028#include "hb.hh"
29#include "hb-blob.hh"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040030
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040031#ifdef HAVE_SYS_MMAN_H
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040032#ifdef HAVE_UNISTD_H
Behdad Esfahboda2644242009-08-03 17:53:29 -040033#include <unistd.h>
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040034#endif /* HAVE_UNISTD_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040035#include <sys/mman.h>
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040036#endif /* HAVE_SYS_MMAN_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040037
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040038#include <stdio.h>
39#include <errno.h>
Ebrahim Byagowice173402018-04-20 10:29:06 +043040#include <stdlib.h>
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040041
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040042
Behdad Esfahbod00cf4e52018-10-27 04:07:33 -070043/**
44 * SECTION: hb-blob
Behdad Esfahbodcf5fa572018-10-27 04:50:38 -070045 * @title: hb-blob
Behdad Esfahbod00cf4e52018-10-27 04:07:33 -070046 * @short_description: Binary data containers
47 * @include: hb.h
48 *
49 * Blobs wrap a chunk of binary data to handle lifecycle management of data
50 * while it is passed between client and HarfBuzz. Blobs are primarily used
51 * to create font faces, but also to access font face tables, as well as
52 * pass around other binary data.
53 **/
54
55
Behdad Esfahbod5f512012013-09-04 18:28:39 -040056/**
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080057 * hb_blob_create: (skip)
58 * @data: Pointer to blob data.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040059 * @length: Length of @data in bytes.
60 * @mode: Memory mode for @data.
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080061 * @user_data: Data parameter to pass to @destroy.
62 * @destroy: Callback to call when @data is not needed anymore.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040063 *
64 * Creates a new "blob" object wrapping @data. The @mode parameter is used
65 * to negotiate ownership and lifecycle of @data.
66 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -040067 * Return value: New blob, or the empty blob if something failed or if @length is
Behdad Esfahbod5f512012013-09-04 18:28:39 -040068 * zero. Destroy with hb_blob_destroy().
69 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +043070 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -040071 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040072hb_blob_t *
73hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040074 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040075 hb_memory_mode_t mode,
Behdad Esfahbode5847f72011-04-20 02:59:28 -040076 void *user_data,
77 hb_destroy_func_t destroy)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040078{
79 hb_blob_t *blob;
80
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080081 if (!length ||
82 length >= 1u << 31 ||
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080083 !(blob = hb_object_create<hb_blob_t> ())) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040084 if (destroy)
85 destroy (user_data);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -040086 return hb_blob_get_empty ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040087 }
88
89 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -040090 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040091 blob->mode = mode;
92
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040093 blob->user_data = user_data;
Behdad Esfahbode5847f72011-04-20 02:59:28 -040094 blob->destroy = destroy;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040095
96 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
97 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod90baf722018-05-03 22:14:54 -040098 if (!blob->try_make_writable ()) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -040099 hb_blob_destroy (blob);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400100 return hb_blob_get_empty ();
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400101 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400102 }
103
104 return blob;
105}
106
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200107static void
108_hb_blob_destroy (void *data)
109{
110 hb_blob_destroy ((hb_blob_t *) data);
111}
112
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400113/**
114 * hb_blob_create_sub_blob:
115 * @parent: Parent blob.
116 * @offset: Start offset of sub-blob within @parent, in bytes.
117 * @length: Length of sub-blob.
118 *
119 * Returns a blob that represents a range of bytes in @parent. The new
120 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
121 * will never modify data in the parent blob. The parent data is not
122 * expected to be modified, and will result in undefined behavior if it
123 * is.
124 *
125 * Makes @parent immutable.
126 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400127 * Return value: New blob, or the empty blob if something failed or if
128 * @length is zero or @offset is beyond the end of @parent's data. Destroy
129 * with hb_blob_destroy().
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400130 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430131 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400132 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400133hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400134hb_blob_create_sub_blob (hb_blob_t *parent,
135 unsigned int offset,
136 unsigned int length)
137{
138 hb_blob_t *blob;
139
Behdad Esfahbodda408fc2018-11-03 15:49:37 -0400140 if (!length || !parent || offset >= parent->length)
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400141 return hb_blob_get_empty ();
Behdad Esfahboda2644242009-08-03 17:53:29 -0400142
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400143 hb_blob_make_immutable (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400144
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400145 blob = hb_blob_create (parent->data + offset,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400146 MIN (length, parent->length - offset),
Behdad Esfahbodc3ba49b2013-02-25 17:06:35 -0500147 HB_MEMORY_MODE_READONLY,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400148 hb_blob_reference (parent),
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200149 _hb_blob_destroy);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400150
Behdad Esfahboda2644242009-08-03 17:53:29 -0400151 return blob;
152}
153
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400154/**
Behdad Esfahboda989f3e2018-02-13 22:12:36 -0800155 * hb_blob_copy_writable_or_fail:
156 * @blob: A blob.
157 *
158 * Makes a writable copy of @blob.
159 *
160 * Return value: New blob, or nullptr if allocation failed.
161 *
162 * Since: 1.8.0
163 **/
164hb_blob_t *
165hb_blob_copy_writable_or_fail (hb_blob_t *blob)
166{
167 blob = hb_blob_create (blob->data,
168 blob->length,
169 HB_MEMORY_MODE_DUPLICATE,
170 nullptr,
171 nullptr);
172
173 if (unlikely (blob == hb_blob_get_empty ()))
174 blob = nullptr;
175
176 return blob;
177}
178
179/**
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400180 * hb_blob_get_empty:
181 *
182 * Returns the singleton empty blob.
183 *
184 * See TODO:link object types for more information.
185 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400186 * Return value: (transfer full): the empty blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400187 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430188 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400189 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400190hb_blob_t *
Behdad Esfahbod49110622011-05-02 19:36:39 -0400191hb_blob_get_empty (void)
Behdad Esfahboda2644242009-08-03 17:53:29 -0400192{
Behdad Esfahbod35066722018-08-06 06:17:48 -0700193 return const_cast<hb_blob_t *> (&Null(hb_blob_t));
Behdad Esfahboda2644242009-08-03 17:53:29 -0400194}
195
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400196/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400197 * hb_blob_reference: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400198 * @blob: a blob.
199 *
200 * Increases the reference count on @blob.
201 *
202 * See TODO:link object types for more information.
203 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400204 * Return value: @blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400205 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430206 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400207 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400208hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400209hb_blob_reference (hb_blob_t *blob)
210{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400211 return hb_object_reference (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400212}
213
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400214/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400215 * hb_blob_destroy: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400216 * @blob: a blob.
217 *
Bruce Mitchener90218fa2018-01-31 20:44:45 +0700218 * Decreases the reference count on @blob, and if it reaches zero, destroys
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400219 * @blob, freeing all memory, possibly calling the destroy-callback the blob
220 * was created for if it has not been called already.
221 *
222 * See TODO:link object types for more information.
223 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430224 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400225 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400226void
227hb_blob_destroy (hb_blob_t *blob)
228{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400229 if (!hb_object_destroy (blob)) return;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400230
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400231 blob->fini_shallow ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400232
233 free (blob);
234}
235
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400236/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400237 * hb_blob_set_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400238 * @blob: a blob.
239 * @key: key for data to set.
240 * @data: data to set.
241 * @destroy: callback to call when @data is not needed anymore.
242 * @replace: whether to replace an existing data with the same key.
243 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430244 * Return value:
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400245 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430246 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400247 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400248hb_bool_t
249hb_blob_set_user_data (hb_blob_t *blob,
250 hb_user_data_key_t *key,
251 void * data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200252 hb_destroy_func_t destroy,
253 hb_bool_t replace)
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400254{
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200255 return hb_object_set_user_data (blob, key, data, destroy, replace);
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400256}
257
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400258/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400259 * hb_blob_get_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400260 * @blob: a blob.
261 * @key: key for data to get.
262 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400263 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430264 *
265 * Return value: (transfer none):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400266 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430267 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400268 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400269void *
270hb_blob_get_user_data (hb_blob_t *blob,
271 hb_user_data_key_t *key)
272{
273 return hb_object_get_user_data (blob, key);
274}
275
276
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400277/**
278 * hb_blob_make_immutable:
279 * @blob: a blob.
280 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430281 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400282 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430283 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400284 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400285void
286hb_blob_make_immutable (hb_blob_t *blob)
287{
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400288 if (hb_object_is_immutable (blob))
Behdad Esfahbod90a0f9f2018-09-26 15:03:07 -0400289 return;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400290
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400291 hb_object_make_immutable (blob);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400292}
293
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400294/**
295 * hb_blob_is_immutable:
296 * @blob: a blob.
297 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430298 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400299 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400300 * Return value: TODO
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400301 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430302 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400303 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400304hb_bool_t
305hb_blob_is_immutable (hb_blob_t *blob)
306{
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400307 return hb_object_is_immutable (blob);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400308}
309
310
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400311/**
312 * hb_blob_get_length:
313 * @blob: a blob.
314 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430315 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400316 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400317 * Return value: the length of blob data in bytes.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400318 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430319 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400320 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400321unsigned int
322hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400323{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400324 return blob->length;
325}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400326
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400327/**
328 * hb_blob_get_data:
329 * @blob: a blob.
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400330 * @length: (out):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400331 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400332 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430333 *
334 * Returns: (transfer none) (array length=length):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400335 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430336 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400337 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400338const char *
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400339hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400340{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400341 if (length)
342 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400343
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400344 return blob->data;
345}
346
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400347/**
348 * hb_blob_get_data_writable:
349 * @blob: a blob.
350 * @length: (out): output length of the writable data.
351 *
352 * Tries to make blob data writable (possibly copying it) and
353 * return pointer to data.
354 *
355 * Fails if blob has been made immutable, or if memory allocation
356 * fails.
357 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400358 * Returns: (transfer none) (array length=length): Writable blob data,
359 * or %NULL if failed.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400360 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430361 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400362 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400363char *
364hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400365{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400366 if (!blob->try_make_writable ()) {
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400367 if (length)
368 *length = 0;
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400369
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200370 return nullptr;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400371 }
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400372
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400373 if (length)
374 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400375
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400376 return const_cast<char *> (blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400377}
378
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400379
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400380bool
381hb_blob_t::try_make_writable_inplace_unix (void)
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400382{
383#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500384 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400385 const char *addr;
386
387#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500388 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400389#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500390 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400391#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500392 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400393#endif
394
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500395 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400396 DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400397 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400398 }
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400399 DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400400
401 mask = ~(pagesize-1);
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400402 addr = (const char *) (((uintptr_t) this->data) & mask);
403 length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask) - addr;
404 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400405 "calling mprotect on [%p..%p] (%lu bytes)",
406 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400407 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400408 DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400409 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400410 }
411
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400412 this->mode = HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400413
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400414 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400415 "successfully made [%p..%p] (%lu bytes) writable\n",
416 addr, addr+length, (unsigned long) length);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400417 return true;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400418#else
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400419 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400420#endif
421}
422
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400423bool
424hb_blob_t::try_make_writable_inplace (void)
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400425{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400426 DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400427
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400428 if (this->try_make_writable_inplace_unix ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400429 return true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400430
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400431 DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400432
433 /* Failed to make writable inplace, mark that */
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400434 this->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400435 return false;
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400436}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400437
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400438bool
439hb_blob_t::try_make_writable (void)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400440{
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400441 if (hb_object_is_immutable (this))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400442 return false;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400443
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400444 if (this->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400445 return true;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400446
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400447 if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400448 return true;
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400449
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400450 if (this->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400451 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400452
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400453
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400454 DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400455
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400456 char *new_data;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400457
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400458 new_data = (char *) malloc (this->length);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400459 if (unlikely (!new_data))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400460 return false;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400461
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400462 DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400463
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400464 memcpy (new_data, this->data, this->length);
465 this->destroy_user_data ();
466 this->mode = HB_MEMORY_MODE_WRITABLE;
467 this->data = new_data;
468 this->user_data = new_data;
469 this->destroy = free;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400470
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400471 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400472}
Ebrahim Byagowice173402018-04-20 10:29:06 +0430473
Behdad Esfahbod5c64d612018-05-03 21:10:57 -0400474/*
475 * Mmap
476 */
477
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430478#ifdef HAVE_MMAP
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430479# include <sys/types.h>
480# include <sys/stat.h>
481# include <fcntl.h>
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430482#endif
483
Ken Browneee5b5e2018-11-12 21:05:39 -0500484#ifdef _WIN32
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430485# include <windows.h>
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430486#else
Khaled Hosny30cbe612018-10-19 22:04:56 +0200487# ifndef O_BINARY
488# define O_BINARY 0
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430489# endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430490#endif
491
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430492#ifndef MAP_NORESERVE
493# define MAP_NORESERVE 0
494#endif
495
Ebrahim Byagowice173402018-04-20 10:29:06 +0430496struct hb_mapped_file_t
497{
498 char *contents;
499 unsigned long length;
Ken Browneee5b5e2018-11-12 21:05:39 -0500500#ifdef _WIN32
Ebrahim Byagowice173402018-04-20 10:29:06 +0430501 HANDLE mapping;
502#endif
503};
504
Ken Browneee5b5e2018-11-12 21:05:39 -0500505#if (defined(HAVE_MMAP) || defined(_WIN32)) && !defined(HB_NO_MMAP)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430506static void
Behdad Esfahbod6e070762018-10-14 22:22:45 -0700507_hb_mapped_file_destroy (void *file_)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430508{
Behdad Esfahbod6e070762018-10-14 22:22:45 -0700509 hb_mapped_file_t *file = (hb_mapped_file_t *) file_;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430510#ifdef HAVE_MMAP
511 munmap (file->contents, file->length);
Ken Browneee5b5e2018-11-12 21:05:39 -0500512#elif defined(_WIN32)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430513 UnmapViewOfFile (file->contents);
514 CloseHandle (file->mapping);
515#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430516 assert (0); // If we don't have mmap we shouldn't reach here
Ebrahim Byagowice173402018-04-20 10:29:06 +0430517#endif
518
519 free (file);
520}
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430521#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430522
523/**
524 * hb_blob_create_from_file:
525 * @file_name: font filename.
526 *
527 * Returns: A hb_blob_t pointer with the content of the file
528 *
Behdad Esfahboddf01f3e2018-06-05 15:17:39 -0700529 * Since: 1.7.7
Ebrahim Byagowice173402018-04-20 10:29:06 +0430530 **/
531hb_blob_t *
532hb_blob_create_from_file (const char *file_name)
533{
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430534 /* Adopted from glib's gmappedfile.c with Matthias Clasen and
535 Allison Lortie permission but changed a lot to suit our need. */
536#if defined(HAVE_MMAP) && !defined(HB_NO_MMAP)
Ebrahim Byagowi81003802018-04-22 10:58:37 +0430537 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430538 if (unlikely (!file)) return hb_blob_get_empty ();
539
Khaled Hosny30cbe612018-10-19 22:04:56 +0200540 int fd = open (file_name, O_RDONLY | O_BINARY, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430541 if (unlikely (fd == -1)) goto fail_without_close;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430542
543 struct stat st;
544 if (unlikely (fstat (fd, &st) == -1)) goto fail;
545
Ebrahim Byagowice173402018-04-20 10:29:06 +0430546 file->length = (unsigned long) st.st_size;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430547 file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430548 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430549
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430550 if (unlikely (file->contents == MAP_FAILED)) goto fail;
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430551
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430552 close (fd);
553
554 return hb_blob_create (file->contents, file->length,
555 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
556 (hb_destroy_func_t) _hb_mapped_file_destroy);
557
558fail:
559 close (fd);
560fail_without_close:
561 free (file);
562
Ken Browneee5b5e2018-11-12 21:05:39 -0500563#elif defined(_WIN32) && !defined(HB_NO_MMAP)
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430564 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
565 if (unlikely (!file)) return hb_blob_get_empty ();
566
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430567 HANDLE fd;
568 unsigned int size = strlen (file_name) + 1;
569 wchar_t * wchar_file_name = (wchar_t *) malloc (sizeof (wchar_t) * size);
570 if (unlikely (wchar_file_name == nullptr)) goto fail_without_close;
571 mbstowcs (wchar_file_name, file_name, size);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000572#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
573 {
574 CREATEFILE2_EXTENDED_PARAMETERS ceparams = { 0 };
575 ceparams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
576 ceparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFFF;
577 ceparams.dwFileFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFF00000;
578 ceparams.dwSecurityQosFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0x000F0000;
579 ceparams.lpSecurityAttributes = nullptr;
580 ceparams.hTemplateFile = nullptr;
581 fd = CreateFile2 (wchar_file_name, GENERIC_READ, FILE_SHARE_READ,
582 OPEN_EXISTING, &ceparams);
583 }
584#else
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430585 fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
586 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
587 nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000588#endif
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430589 free (wchar_file_name);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430590
591 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
592
Matt Oliver24dd6c12018-09-23 18:08:30 +1000593#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
594 {
595 LARGE_INTEGER length;
596 GetFileSizeEx (fd, &length);
597 file->length = length.LowPart;
598 file->mapping = CreateFileMappingFromApp (fd, nullptr, PAGE_READONLY, length.QuadPart, nullptr);
599 }
600#else
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430601 file->length = (unsigned long) GetFileSize (fd, nullptr);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430602 file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000603#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430604 if (unlikely (file->mapping == nullptr)) goto fail;
605
Matt Oliver24dd6c12018-09-23 18:08:30 +1000606#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
607 file->contents = (char *) MapViewOfFileFromApp (file->mapping, FILE_MAP_READ, 0, 0);
608#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430609 file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000610#endif
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430611 if (unlikely (file->contents == nullptr)) goto fail;
612
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430613 CloseHandle (fd);
614 return hb_blob_create (file->contents, file->length,
615 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
Ebrahim Byagowice173402018-04-20 10:29:06 +0430616 (hb_destroy_func_t) _hb_mapped_file_destroy);
617
618fail:
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430619 CloseHandle (fd);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430620fail_without_close:
Ebrahim Byagowice173402018-04-20 10:29:06 +0430621 free (file);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430622
623#endif
624
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430625 /* The following tries to read a file without knowing its size beforehand
626 It's used as a fallback for systems without mmap or to read from pipes */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430627 unsigned long len = 0, allocated = BUFSIZ * 16;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430628 char *data = (char *) malloc (allocated);
629 if (unlikely (data == nullptr)) return hb_blob_get_empty ();
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430630
631 FILE *fp = fopen (file_name, "rb");
632 if (unlikely (fp == nullptr)) goto fread_fail_without_close;
633
634 while (!feof (fp))
635 {
636 if (allocated - len < BUFSIZ)
637 {
638 allocated *= 2;
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430639 /* Don't allocate and go more than ~536MB, our mmap reader still
640 can cover files like that but lets limit our fallback reader */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430641 if (unlikely (allocated > (2 << 28))) goto fread_fail;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430642 char *new_data = (char *) realloc (data, allocated);
643 if (unlikely (new_data == nullptr)) goto fread_fail;
644 data = new_data;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430645 }
646
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430647 unsigned long addition = fread (data + len, 1, allocated - len, fp);
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430648
649 int err = ferror (fp);
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430650#ifdef EINTR // armcc doesn't have it
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430651 if (unlikely (err == EINTR)) continue;
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430652#endif
Ebrahim Byagowi8a51f912018-06-28 13:22:21 +0430653 if (unlikely (err)) goto fread_fail;
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430654
655 len += addition;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430656 }
657
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430658 return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430659 (hb_destroy_func_t) free);
660
661fread_fail:
662 fclose (fp);
663fread_fail_without_close:
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430664 free (data);
Ebrahim Byagowice173402018-04-20 10:29:06 +0430665 return hb_blob_get_empty ();
666}