blob: c1ed0f2a77b8833321399be7b651bd73eec0a62f [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 Esfahbod35066722018-08-06 06:17:48 -070043DEFINE_NULL_INSTANCE (hb_blob_t) =
44{
45 HB_OBJECT_HEADER_STATIC,
46
47 true, /* immutable */
48
49 nullptr, /* data */
50 0, /* length */
51 HB_MEMORY_MODE_READONLY, /* mode */
52
53 nullptr, /* user_data */
54 nullptr /* destroy */
55};
56
Behdad Esfahbod5f512012013-09-04 18:28:39 -040057/**
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080058 * hb_blob_create: (skip)
59 * @data: Pointer to blob data.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040060 * @length: Length of @data in bytes.
61 * @mode: Memory mode for @data.
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080062 * @user_data: Data parameter to pass to @destroy.
63 * @destroy: Callback to call when @data is not needed anymore.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040064 *
65 * Creates a new "blob" object wrapping @data. The @mode parameter is used
66 * to negotiate ownership and lifecycle of @data.
67 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -040068 * Return value: New blob, or the empty blob if something failed or if @length is
Behdad Esfahbod5f512012013-09-04 18:28:39 -040069 * zero. Destroy with hb_blob_destroy().
70 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +043071 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -040072 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040073hb_blob_t *
74hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040075 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040076 hb_memory_mode_t mode,
Behdad Esfahbode5847f72011-04-20 02:59:28 -040077 void *user_data,
78 hb_destroy_func_t destroy)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040079{
80 hb_blob_t *blob;
81
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080082 if (!length ||
83 length >= 1u << 31 ||
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080084 !(blob = hb_object_create<hb_blob_t> ())) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040085 if (destroy)
86 destroy (user_data);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -040087 return hb_blob_get_empty ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040088 }
89
90 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -040091 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040092 blob->mode = mode;
93
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040094 blob->user_data = user_data;
Behdad Esfahbode5847f72011-04-20 02:59:28 -040095 blob->destroy = destroy;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040096
97 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
98 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod90baf722018-05-03 22:14:54 -040099 if (!blob->try_make_writable ()) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400100 hb_blob_destroy (blob);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400101 return hb_blob_get_empty ();
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400102 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400103 }
104
105 return blob;
106}
107
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200108static void
109_hb_blob_destroy (void *data)
110{
111 hb_blob_destroy ((hb_blob_t *) data);
112}
113
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400114/**
115 * hb_blob_create_sub_blob:
116 * @parent: Parent blob.
117 * @offset: Start offset of sub-blob within @parent, in bytes.
118 * @length: Length of sub-blob.
119 *
120 * Returns a blob that represents a range of bytes in @parent. The new
121 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
122 * will never modify data in the parent blob. The parent data is not
123 * expected to be modified, and will result in undefined behavior if it
124 * is.
125 *
126 * Makes @parent immutable.
127 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400128 * Return value: New blob, or the empty blob if something failed or if
129 * @length is zero or @offset is beyond the end of @parent's data. Destroy
130 * with hb_blob_destroy().
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400131 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430132 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400133 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400134hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400135hb_blob_create_sub_blob (hb_blob_t *parent,
136 unsigned int offset,
137 unsigned int length)
138{
139 hb_blob_t *blob;
140
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400141 if (!length || offset >= parent->length)
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400142 return hb_blob_get_empty ();
Behdad Esfahboda2644242009-08-03 17:53:29 -0400143
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400144 hb_blob_make_immutable (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400145
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400146 blob = hb_blob_create (parent->data + offset,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400147 MIN (length, parent->length - offset),
Behdad Esfahbodc3ba49b2013-02-25 17:06:35 -0500148 HB_MEMORY_MODE_READONLY,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400149 hb_blob_reference (parent),
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200150 _hb_blob_destroy);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400151
Behdad Esfahboda2644242009-08-03 17:53:29 -0400152 return blob;
153}
154
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400155/**
Behdad Esfahboda989f3e2018-02-13 22:12:36 -0800156 * hb_blob_copy_writable_or_fail:
157 * @blob: A blob.
158 *
159 * Makes a writable copy of @blob.
160 *
161 * Return value: New blob, or nullptr if allocation failed.
162 *
163 * Since: 1.8.0
164 **/
165hb_blob_t *
166hb_blob_copy_writable_or_fail (hb_blob_t *blob)
167{
168 blob = hb_blob_create (blob->data,
169 blob->length,
170 HB_MEMORY_MODE_DUPLICATE,
171 nullptr,
172 nullptr);
173
174 if (unlikely (blob == hb_blob_get_empty ()))
175 blob = nullptr;
176
177 return blob;
178}
179
180/**
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400181 * hb_blob_get_empty:
182 *
183 * Returns the singleton empty blob.
184 *
185 * See TODO:link object types for more information.
186 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400187 * Return value: (transfer full): the empty blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400188 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430189 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400190 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400191hb_blob_t *
Behdad Esfahbod49110622011-05-02 19:36:39 -0400192hb_blob_get_empty (void)
Behdad Esfahboda2644242009-08-03 17:53:29 -0400193{
Behdad Esfahbod35066722018-08-06 06:17:48 -0700194 return const_cast<hb_blob_t *> (&Null(hb_blob_t));
Behdad Esfahboda2644242009-08-03 17:53:29 -0400195}
196
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400197/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400198 * hb_blob_reference: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400199 * @blob: a blob.
200 *
201 * Increases the reference count on @blob.
202 *
203 * See TODO:link object types for more information.
204 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400205 * Return value: @blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400206 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430207 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400208 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400209hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400210hb_blob_reference (hb_blob_t *blob)
211{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400212 return hb_object_reference (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400213}
214
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400215/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400216 * hb_blob_destroy: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400217 * @blob: a blob.
218 *
Bruce Mitchener90218fa2018-01-31 20:44:45 +0700219 * Decreases the reference count on @blob, and if it reaches zero, destroys
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400220 * @blob, freeing all memory, possibly calling the destroy-callback the blob
221 * was created for if it has not been called already.
222 *
223 * See TODO:link object types for more information.
224 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430225 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400226 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400227void
228hb_blob_destroy (hb_blob_t *blob)
229{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400230 if (!hb_object_destroy (blob)) return;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400231
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400232 blob->fini_shallow ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400233
234 free (blob);
235}
236
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400237/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400238 * hb_blob_set_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400239 * @blob: a blob.
240 * @key: key for data to set.
241 * @data: data to set.
242 * @destroy: callback to call when @data is not needed anymore.
243 * @replace: whether to replace an existing data with the same key.
244 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430245 * Return value:
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400246 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430247 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400248 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400249hb_bool_t
250hb_blob_set_user_data (hb_blob_t *blob,
251 hb_user_data_key_t *key,
252 void * data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200253 hb_destroy_func_t destroy,
254 hb_bool_t replace)
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400255{
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200256 return hb_object_set_user_data (blob, key, data, destroy, replace);
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400257}
258
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400259/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400260 * hb_blob_get_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400261 * @blob: a blob.
262 * @key: key for data to get.
263 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400264 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430265 *
266 * Return value: (transfer none):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400267 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430268 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400269 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400270void *
271hb_blob_get_user_data (hb_blob_t *blob,
272 hb_user_data_key_t *key)
273{
274 return hb_object_get_user_data (blob, key);
275}
276
277
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400278/**
279 * hb_blob_make_immutable:
280 * @blob: a blob.
281 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430282 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400283 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430284 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400285 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400286void
287hb_blob_make_immutable (hb_blob_t *blob)
288{
289 if (hb_object_is_inert (blob))
290 return;
Behdad Esfahbod90a0f9f2018-09-26 15:03:07 -0400291 if (blob->immutable)
292 return;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400293
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400294 blob->immutable = true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400295}
296
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400297/**
298 * hb_blob_is_immutable:
299 * @blob: a blob.
300 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430301 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400302 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400303 * Return value: TODO
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400304 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430305 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400306 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400307hb_bool_t
308hb_blob_is_immutable (hb_blob_t *blob)
309{
310 return blob->immutable;
311}
312
313
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400314/**
315 * hb_blob_get_length:
316 * @blob: a blob.
317 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430318 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400319 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400320 * Return value: the length of blob data in bytes.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400321 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430322 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400323 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400324unsigned int
325hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400326{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400327 return blob->length;
328}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400329
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400330/**
331 * hb_blob_get_data:
332 * @blob: a blob.
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400333 * @length: (out):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400334 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400335 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430336 *
337 * Returns: (transfer none) (array length=length):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400338 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430339 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400340 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400341const char *
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400342hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400343{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400344 if (length)
345 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400346
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400347 return blob->data;
348}
349
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400350/**
351 * hb_blob_get_data_writable:
352 * @blob: a blob.
353 * @length: (out): output length of the writable data.
354 *
355 * Tries to make blob data writable (possibly copying it) and
356 * return pointer to data.
357 *
358 * Fails if blob has been made immutable, or if memory allocation
359 * fails.
360 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400361 * Returns: (transfer none) (array length=length): Writable blob data,
362 * or %NULL if failed.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400363 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430364 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400365 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400366char *
367hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400368{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400369 if (!blob->try_make_writable ()) {
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400370 if (length)
371 *length = 0;
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400372
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200373 return nullptr;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400374 }
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400375
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400376 if (length)
377 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400378
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400379 return const_cast<char *> (blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400380}
381
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400382
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400383bool
384hb_blob_t::try_make_writable_inplace_unix (void)
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400385{
386#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500387 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400388 const char *addr;
389
390#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500391 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400392#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500393 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400394#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500395 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400396#endif
397
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500398 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400399 DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400400 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400401 }
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400402 DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400403
404 mask = ~(pagesize-1);
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400405 addr = (const char *) (((uintptr_t) this->data) & mask);
406 length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask) - addr;
407 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400408 "calling mprotect on [%p..%p] (%lu bytes)",
409 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400410 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400411 DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400412 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400413 }
414
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400415 this->mode = HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400416
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400417 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400418 "successfully made [%p..%p] (%lu bytes) writable\n",
419 addr, addr+length, (unsigned long) length);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400420 return true;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400421#else
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400422 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400423#endif
424}
425
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400426bool
427hb_blob_t::try_make_writable_inplace (void)
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400428{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400429 DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400430
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400431 if (this->try_make_writable_inplace_unix ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400432 return true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400433
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400434 DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400435
436 /* Failed to make writable inplace, mark that */
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400437 this->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400438 return false;
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400439}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400440
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400441bool
442hb_blob_t::try_make_writable (void)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400443{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400444 if (this->immutable)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400445 return false;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400446
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400447 if (this->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400448 return true;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400449
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400450 if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400451 return true;
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400452
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400453 if (this->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400454 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400455
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400456
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400457 DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400458
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400459 char *new_data;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400460
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400461 new_data = (char *) malloc (this->length);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400462 if (unlikely (!new_data))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400463 return false;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400464
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400465 DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400466
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400467 memcpy (new_data, this->data, this->length);
468 this->destroy_user_data ();
469 this->mode = HB_MEMORY_MODE_WRITABLE;
470 this->data = new_data;
471 this->user_data = new_data;
472 this->destroy = free;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400473
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400474 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400475}
Ebrahim Byagowice173402018-04-20 10:29:06 +0430476
Behdad Esfahbod5c64d612018-05-03 21:10:57 -0400477/*
478 * Mmap
479 */
480
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430481#ifdef HAVE_MMAP
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430482# include <sys/types.h>
483# include <sys/stat.h>
484# include <fcntl.h>
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430485#endif
486
Ebrahim Byagowice173402018-04-20 10:29:06 +0430487#if defined(_WIN32) || defined(__CYGWIN__)
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430488# include <windows.h>
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430489#else
490# ifndef _O_BINARY
491# define _O_BINARY 0
492# endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430493#endif
494
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430495#ifndef MAP_NORESERVE
496# define MAP_NORESERVE 0
497#endif
498
Ebrahim Byagowice173402018-04-20 10:29:06 +0430499struct hb_mapped_file_t
500{
501 char *contents;
502 unsigned long length;
503#if defined(_WIN32) || defined(__CYGWIN__)
504 HANDLE mapping;
505#endif
506};
507
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430508#if (defined(HAVE_MMAP) || defined(_WIN32) || defined(__CYGWIN__)) && !defined(HB_NO_MMAP)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430509static void
510_hb_mapped_file_destroy (hb_mapped_file_t *file)
511{
512#ifdef HAVE_MMAP
513 munmap (file->contents, file->length);
514#elif defined(_WIN32) || defined(__CYGWIN__)
515 UnmapViewOfFile (file->contents);
516 CloseHandle (file->mapping);
517#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430518 assert (0); // If we don't have mmap we shouldn't reach here
Ebrahim Byagowice173402018-04-20 10:29:06 +0430519#endif
520
521 free (file);
522}
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430523#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430524
525/**
526 * hb_blob_create_from_file:
527 * @file_name: font filename.
528 *
529 * Returns: A hb_blob_t pointer with the content of the file
530 *
Behdad Esfahboddf01f3e2018-06-05 15:17:39 -0700531 * Since: 1.7.7
Ebrahim Byagowice173402018-04-20 10:29:06 +0430532 **/
533hb_blob_t *
534hb_blob_create_from_file (const char *file_name)
535{
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430536 /* Adopted from glib's gmappedfile.c with Matthias Clasen and
537 Allison Lortie permission but changed a lot to suit our need. */
538#if defined(HAVE_MMAP) && !defined(HB_NO_MMAP)
Ebrahim Byagowi81003802018-04-22 10:58:37 +0430539 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430540 if (unlikely (!file)) return hb_blob_get_empty ();
541
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430542 int fd = open (file_name, O_RDONLY | _O_BINARY, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430543 if (unlikely (fd == -1)) goto fail_without_close;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430544
545 struct stat st;
546 if (unlikely (fstat (fd, &st) == -1)) goto fail;
547
Ebrahim Byagowice173402018-04-20 10:29:06 +0430548 file->length = (unsigned long) st.st_size;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430549 file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430550 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430551
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430552 if (unlikely (file->contents == MAP_FAILED)) goto fail;
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430553
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430554 close (fd);
555
556 return hb_blob_create (file->contents, file->length,
557 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
558 (hb_destroy_func_t) _hb_mapped_file_destroy);
559
560fail:
561 close (fd);
562fail_without_close:
563 free (file);
564
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430565#elif (defined(_WIN32) || defined(__CYGWIN__)) && !defined(HB_NO_MMAP)
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430566 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
567 if (unlikely (!file)) return hb_blob_get_empty ();
568
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430569 HANDLE fd;
570 unsigned int size = strlen (file_name) + 1;
571 wchar_t * wchar_file_name = (wchar_t *) malloc (sizeof (wchar_t) * size);
572 if (unlikely (wchar_file_name == nullptr)) goto fail_without_close;
573 mbstowcs (wchar_file_name, file_name, size);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000574#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
575 {
576 CREATEFILE2_EXTENDED_PARAMETERS ceparams = { 0 };
577 ceparams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
578 ceparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFFF;
579 ceparams.dwFileFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFF00000;
580 ceparams.dwSecurityQosFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0x000F0000;
581 ceparams.lpSecurityAttributes = nullptr;
582 ceparams.hTemplateFile = nullptr;
583 fd = CreateFile2 (wchar_file_name, GENERIC_READ, FILE_SHARE_READ,
584 OPEN_EXISTING, &ceparams);
585 }
586#else
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430587 fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
588 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
589 nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000590#endif
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430591 free (wchar_file_name);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430592
593 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
594
Matt Oliver24dd6c12018-09-23 18:08:30 +1000595#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
596 {
597 LARGE_INTEGER length;
598 GetFileSizeEx (fd, &length);
599 file->length = length.LowPart;
600 file->mapping = CreateFileMappingFromApp (fd, nullptr, PAGE_READONLY, length.QuadPart, nullptr);
601 }
602#else
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430603 file->length = (unsigned long) GetFileSize (fd, nullptr);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430604 file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000605#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430606 if (unlikely (file->mapping == nullptr)) goto fail;
607
Matt Oliver24dd6c12018-09-23 18:08:30 +1000608#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
609 file->contents = (char *) MapViewOfFileFromApp (file->mapping, FILE_MAP_READ, 0, 0);
610#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430611 file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000612#endif
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430613 if (unlikely (file->contents == nullptr)) goto fail;
614
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430615 CloseHandle (fd);
616 return hb_blob_create (file->contents, file->length,
617 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
Ebrahim Byagowice173402018-04-20 10:29:06 +0430618 (hb_destroy_func_t) _hb_mapped_file_destroy);
619
620fail:
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430621 CloseHandle (fd);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430622fail_without_close:
Ebrahim Byagowice173402018-04-20 10:29:06 +0430623 free (file);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430624
625#endif
626
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430627 /* The following tries to read a file without knowing its size beforehand
628 It's used as a fallback for systems without mmap or to read from pipes */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430629 unsigned long len = 0, allocated = BUFSIZ * 16;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430630 char *data = (char *) malloc (allocated);
631 if (unlikely (data == nullptr)) return hb_blob_get_empty ();
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430632
633 FILE *fp = fopen (file_name, "rb");
634 if (unlikely (fp == nullptr)) goto fread_fail_without_close;
635
636 while (!feof (fp))
637 {
638 if (allocated - len < BUFSIZ)
639 {
640 allocated *= 2;
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430641 /* Don't allocate and go more than ~536MB, our mmap reader still
642 can cover files like that but lets limit our fallback reader */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430643 if (unlikely (allocated > (2 << 28))) goto fread_fail;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430644 char *new_data = (char *) realloc (data, allocated);
645 if (unlikely (new_data == nullptr)) goto fread_fail;
646 data = new_data;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430647 }
648
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430649 unsigned long addition = fread (data + len, 1, allocated - len, fp);
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430650
651 int err = ferror (fp);
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430652#ifdef EINTR // armcc doesn't have it
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430653 if (unlikely (err == EINTR)) continue;
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430654#endif
Ebrahim Byagowi8a51f912018-06-28 13:22:21 +0430655 if (unlikely (err)) goto fread_fail;
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430656
657 len += addition;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430658 }
659
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430660 return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430661 (hb_destroy_func_t) free);
662
663fread_fail:
664 fclose (fp);
665fread_fail_without_close:
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430666 free (data);
Ebrahim Byagowice173402018-04-20 10:29:06 +0430667 return hb_blob_get_empty ();
668}