blob: a335df308a5e5a95eed8447fedf1d1d3c52dcfac [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 Esfahbodf88d3bd2013-01-14 00:33:58 -060028/* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */
Behdad Esfahbod73f7f892014-07-09 17:17:18 -040029#ifndef _POSIX_C_SOURCE
Ting-Wei Lane3a15d02018-01-12 15:33:16 +080030#define _POSIX_C_SOURCE 200809L
Behdad Esfahbod73f7f892014-07-09 17:17:18 -040031#endif
Behdad Esfahbodf88d3bd2013-01-14 00:33:58 -060032
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070033#include "hb.hh"
34#include "hb-blob.hh"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040035
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040036#ifdef HAVE_SYS_MMAN_H
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040037#ifdef HAVE_UNISTD_H
Behdad Esfahboda2644242009-08-03 17:53:29 -040038#include <unistd.h>
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040039#endif /* HAVE_UNISTD_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040040#include <sys/mman.h>
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040041#endif /* HAVE_SYS_MMAN_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040042
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040043#include <stdio.h>
44#include <errno.h>
Ebrahim Byagowice173402018-04-20 10:29:06 +043045#include <stdlib.h>
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040046
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040047
Behdad Esfahbod35066722018-08-06 06:17:48 -070048DEFINE_NULL_INSTANCE (hb_blob_t) =
49{
50 HB_OBJECT_HEADER_STATIC,
51
52 true, /* immutable */
53
54 nullptr, /* data */
55 0, /* length */
56 HB_MEMORY_MODE_READONLY, /* mode */
57
58 nullptr, /* user_data */
59 nullptr /* destroy */
60};
61
Behdad Esfahbod5f512012013-09-04 18:28:39 -040062/**
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080063 * hb_blob_create: (skip)
64 * @data: Pointer to blob data.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040065 * @length: Length of @data in bytes.
66 * @mode: Memory mode for @data.
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080067 * @user_data: Data parameter to pass to @destroy.
68 * @destroy: Callback to call when @data is not needed anymore.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040069 *
70 * Creates a new "blob" object wrapping @data. The @mode parameter is used
71 * to negotiate ownership and lifecycle of @data.
72 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -040073 * Return value: New blob, or the empty blob if something failed or if @length is
Behdad Esfahbod5f512012013-09-04 18:28:39 -040074 * zero. Destroy with hb_blob_destroy().
75 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +043076 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -040077 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040078hb_blob_t *
79hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040080 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040081 hb_memory_mode_t mode,
Behdad Esfahbode5847f72011-04-20 02:59:28 -040082 void *user_data,
83 hb_destroy_func_t destroy)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040084{
85 hb_blob_t *blob;
86
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080087 if (!length ||
88 length >= 1u << 31 ||
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080089 !(blob = hb_object_create<hb_blob_t> ())) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040090 if (destroy)
91 destroy (user_data);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -040092 return hb_blob_get_empty ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040093 }
94
95 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -040096 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040097 blob->mode = mode;
98
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040099 blob->user_data = user_data;
Behdad Esfahbode5847f72011-04-20 02:59:28 -0400100 blob->destroy = destroy;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400101
102 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
103 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400104 if (!blob->try_make_writable ()) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400105 hb_blob_destroy (blob);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400106 return hb_blob_get_empty ();
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400107 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400108 }
109
110 return blob;
111}
112
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200113static void
114_hb_blob_destroy (void *data)
115{
116 hb_blob_destroy ((hb_blob_t *) data);
117}
118
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400119/**
120 * hb_blob_create_sub_blob:
121 * @parent: Parent blob.
122 * @offset: Start offset of sub-blob within @parent, in bytes.
123 * @length: Length of sub-blob.
124 *
125 * Returns a blob that represents a range of bytes in @parent. The new
126 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
127 * will never modify data in the parent blob. The parent data is not
128 * expected to be modified, and will result in undefined behavior if it
129 * is.
130 *
131 * Makes @parent immutable.
132 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400133 * Return value: New blob, or the empty blob if something failed or if
134 * @length is zero or @offset is beyond the end of @parent's data. Destroy
135 * with hb_blob_destroy().
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400136 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430137 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400138 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400139hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400140hb_blob_create_sub_blob (hb_blob_t *parent,
141 unsigned int offset,
142 unsigned int length)
143{
144 hb_blob_t *blob;
145
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400146 if (!length || offset >= parent->length)
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400147 return hb_blob_get_empty ();
Behdad Esfahboda2644242009-08-03 17:53:29 -0400148
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400149 hb_blob_make_immutable (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400150
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400151 blob = hb_blob_create (parent->data + offset,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400152 MIN (length, parent->length - offset),
Behdad Esfahbodc3ba49b2013-02-25 17:06:35 -0500153 HB_MEMORY_MODE_READONLY,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400154 hb_blob_reference (parent),
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200155 _hb_blob_destroy);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400156
Behdad Esfahboda2644242009-08-03 17:53:29 -0400157 return blob;
158}
159
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400160/**
Behdad Esfahboda989f3e2018-02-13 22:12:36 -0800161 * hb_blob_copy_writable_or_fail:
162 * @blob: A blob.
163 *
164 * Makes a writable copy of @blob.
165 *
166 * Return value: New blob, or nullptr if allocation failed.
167 *
168 * Since: 1.8.0
169 **/
170hb_blob_t *
171hb_blob_copy_writable_or_fail (hb_blob_t *blob)
172{
173 blob = hb_blob_create (blob->data,
174 blob->length,
175 HB_MEMORY_MODE_DUPLICATE,
176 nullptr,
177 nullptr);
178
179 if (unlikely (blob == hb_blob_get_empty ()))
180 blob = nullptr;
181
182 return blob;
183}
184
185/**
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400186 * hb_blob_get_empty:
187 *
188 * Returns the singleton empty blob.
189 *
190 * See TODO:link object types for more information.
191 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400192 * Return value: (transfer full): the empty blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400193 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430194 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400195 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400196hb_blob_t *
Behdad Esfahbod49110622011-05-02 19:36:39 -0400197hb_blob_get_empty (void)
Behdad Esfahboda2644242009-08-03 17:53:29 -0400198{
Behdad Esfahbod35066722018-08-06 06:17:48 -0700199 return const_cast<hb_blob_t *> (&Null(hb_blob_t));
Behdad Esfahboda2644242009-08-03 17:53:29 -0400200}
201
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400202/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400203 * hb_blob_reference: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400204 * @blob: a blob.
205 *
206 * Increases the reference count on @blob.
207 *
208 * See TODO:link object types for more information.
209 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400210 * Return value: @blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400211 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430212 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400213 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400214hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400215hb_blob_reference (hb_blob_t *blob)
216{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400217 return hb_object_reference (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400218}
219
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400220/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400221 * hb_blob_destroy: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400222 * @blob: a blob.
223 *
Bruce Mitchener90218fa2018-01-31 20:44:45 +0700224 * Decreases the reference count on @blob, and if it reaches zero, destroys
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400225 * @blob, freeing all memory, possibly calling the destroy-callback the blob
226 * was created for if it has not been called already.
227 *
228 * See TODO:link object types for more information.
229 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430230 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400231 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400232void
233hb_blob_destroy (hb_blob_t *blob)
234{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400235 if (!hb_object_destroy (blob)) return;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400236
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400237 blob->fini_shallow ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400238
239 free (blob);
240}
241
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400242/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400243 * hb_blob_set_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400244 * @blob: a blob.
245 * @key: key for data to set.
246 * @data: data to set.
247 * @destroy: callback to call when @data is not needed anymore.
248 * @replace: whether to replace an existing data with the same key.
249 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430250 * Return value:
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400251 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430252 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400253 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400254hb_bool_t
255hb_blob_set_user_data (hb_blob_t *blob,
256 hb_user_data_key_t *key,
257 void * data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200258 hb_destroy_func_t destroy,
259 hb_bool_t replace)
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400260{
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200261 return hb_object_set_user_data (blob, key, data, destroy, replace);
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400262}
263
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400264/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400265 * hb_blob_get_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400266 * @blob: a blob.
267 * @key: key for data to get.
268 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400269 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430270 *
271 * Return value: (transfer none):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400272 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430273 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400274 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400275void *
276hb_blob_get_user_data (hb_blob_t *blob,
277 hb_user_data_key_t *key)
278{
279 return hb_object_get_user_data (blob, key);
280}
281
282
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400283/**
284 * hb_blob_make_immutable:
285 * @blob: a blob.
286 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430287 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400288 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430289 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400290 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400291void
292hb_blob_make_immutable (hb_blob_t *blob)
293{
294 if (hb_object_is_inert (blob))
295 return;
296
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400297 blob->immutable = true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400298}
299
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400300/**
301 * hb_blob_is_immutable:
302 * @blob: a blob.
303 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430304 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400305 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400306 * Return value: TODO
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400307 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430308 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400309 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400310hb_bool_t
311hb_blob_is_immutable (hb_blob_t *blob)
312{
313 return blob->immutable;
314}
315
316
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400317/**
318 * hb_blob_get_length:
319 * @blob: a blob.
320 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430321 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400322 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400323 * Return value: the length of blob data in bytes.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400324 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430325 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400326 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400327unsigned int
328hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400329{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400330 return blob->length;
331}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400332
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400333/**
334 * hb_blob_get_data:
335 * @blob: a blob.
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400336 * @length: (out):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400337 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400338 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430339 *
340 * Returns: (transfer none) (array length=length):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400341 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430342 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400343 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400344const char *
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400345hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400346{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400347 if (length)
348 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400349
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400350 return blob->data;
351}
352
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400353/**
354 * hb_blob_get_data_writable:
355 * @blob: a blob.
356 * @length: (out): output length of the writable data.
357 *
358 * Tries to make blob data writable (possibly copying it) and
359 * return pointer to data.
360 *
361 * Fails if blob has been made immutable, or if memory allocation
362 * fails.
363 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400364 * Returns: (transfer none) (array length=length): Writable blob data,
365 * or %NULL if failed.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400366 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430367 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400368 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400369char *
370hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400371{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400372 if (!blob->try_make_writable ()) {
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400373 if (length)
374 *length = 0;
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400375
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200376 return nullptr;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400377 }
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400378
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400379 if (length)
380 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400381
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400382 return const_cast<char *> (blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400383}
384
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400385
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400386bool
387hb_blob_t::try_make_writable_inplace_unix (void)
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400388{
389#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500390 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400391 const char *addr;
392
393#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500394 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400395#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500396 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400397#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500398 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400399#endif
400
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500401 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400402 DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400403 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400404 }
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400405 DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400406
407 mask = ~(pagesize-1);
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400408 addr = (const char *) (((uintptr_t) this->data) & mask);
409 length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask) - addr;
410 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400411 "calling mprotect on [%p..%p] (%lu bytes)",
412 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400413 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400414 DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400415 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400416 }
417
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400418 this->mode = HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400419
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400420 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400421 "successfully made [%p..%p] (%lu bytes) writable\n",
422 addr, addr+length, (unsigned long) length);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400423 return true;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400424#else
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400425 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400426#endif
427}
428
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400429bool
430hb_blob_t::try_make_writable_inplace (void)
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400431{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400432 DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400433
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400434 if (this->try_make_writable_inplace_unix ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400435 return true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400436
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400437 DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400438
439 /* Failed to make writable inplace, mark that */
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400440 this->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400441 return false;
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400442}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400443
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400444bool
445hb_blob_t::try_make_writable (void)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400446{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400447 if (this->immutable)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400448 return false;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -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 Esfahbod7f3d5c82009-08-06 13:33:51 -0400452
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400453 if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400454 return true;
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400455
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400456 if (this->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400457 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400458
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400459
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400460 DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400461
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400462 char *new_data;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400463
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400464 new_data = (char *) malloc (this->length);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400465 if (unlikely (!new_data))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400466 return false;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400467
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400468 DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400469
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400470 memcpy (new_data, this->data, this->length);
471 this->destroy_user_data ();
472 this->mode = HB_MEMORY_MODE_WRITABLE;
473 this->data = new_data;
474 this->user_data = new_data;
475 this->destroy = free;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400476
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400477 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400478}
Ebrahim Byagowice173402018-04-20 10:29:06 +0430479
Behdad Esfahbod5c64d612018-05-03 21:10:57 -0400480/*
481 * Mmap
482 */
483
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430484#ifdef HAVE_MMAP
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430485# include <sys/types.h>
486# include <sys/stat.h>
487# include <fcntl.h>
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430488#endif
489
Ebrahim Byagowice173402018-04-20 10:29:06 +0430490#if defined(_WIN32) || defined(__CYGWIN__)
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430491# include <windows.h>
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430492#else
493# ifndef _O_BINARY
494# define _O_BINARY 0
495# endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430496#endif
497
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430498#ifndef MAP_NORESERVE
499# define MAP_NORESERVE 0
500#endif
501
Ebrahim Byagowice173402018-04-20 10:29:06 +0430502struct hb_mapped_file_t
503{
504 char *contents;
505 unsigned long length;
506#if defined(_WIN32) || defined(__CYGWIN__)
507 HANDLE mapping;
508#endif
509};
510
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430511#if (defined(HAVE_MMAP) || defined(_WIN32) || defined(__CYGWIN__)) && !defined(HB_NO_MMAP)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430512static void
513_hb_mapped_file_destroy (hb_mapped_file_t *file)
514{
515#ifdef HAVE_MMAP
516 munmap (file->contents, file->length);
517#elif defined(_WIN32) || defined(__CYGWIN__)
518 UnmapViewOfFile (file->contents);
519 CloseHandle (file->mapping);
520#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430521 assert (0); // If we don't have mmap we shouldn't reach here
Ebrahim Byagowice173402018-04-20 10:29:06 +0430522#endif
523
524 free (file);
525}
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430526#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430527
528/**
529 * hb_blob_create_from_file:
530 * @file_name: font filename.
531 *
532 * Returns: A hb_blob_t pointer with the content of the file
533 *
Behdad Esfahboddf01f3e2018-06-05 15:17:39 -0700534 * Since: 1.7.7
Ebrahim Byagowice173402018-04-20 10:29:06 +0430535 **/
536hb_blob_t *
537hb_blob_create_from_file (const char *file_name)
538{
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430539 /* Adopted from glib's gmappedfile.c with Matthias Clasen and
540 Allison Lortie permission but changed a lot to suit our need. */
541#if defined(HAVE_MMAP) && !defined(HB_NO_MMAP)
Ebrahim Byagowi81003802018-04-22 10:58:37 +0430542 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430543 if (unlikely (!file)) return hb_blob_get_empty ();
544
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430545 int fd = open (file_name, O_RDONLY | _O_BINARY, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430546 if (unlikely (fd == -1)) goto fail_without_close;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430547
548 struct stat st;
549 if (unlikely (fstat (fd, &st) == -1)) goto fail;
550
Ebrahim Byagowice173402018-04-20 10:29:06 +0430551 file->length = (unsigned long) st.st_size;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430552 file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430553 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430554
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430555 if (unlikely (file->contents == MAP_FAILED)) goto fail;
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430556
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430557 close (fd);
558
559 return hb_blob_create (file->contents, file->length,
560 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
561 (hb_destroy_func_t) _hb_mapped_file_destroy);
562
563fail:
564 close (fd);
565fail_without_close:
566 free (file);
567
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430568#elif (defined(_WIN32) || defined(__CYGWIN__)) && !defined(HB_NO_MMAP)
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430569 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
570 if (unlikely (!file)) return hb_blob_get_empty ();
571
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430572 HANDLE fd;
573 unsigned int size = strlen (file_name) + 1;
574 wchar_t * wchar_file_name = (wchar_t *) malloc (sizeof (wchar_t) * size);
575 if (unlikely (wchar_file_name == nullptr)) goto fail_without_close;
576 mbstowcs (wchar_file_name, file_name, size);
577 fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
578 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
579 nullptr);
580 free (wchar_file_name);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430581
582 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
583
584 file->length = (unsigned long) GetFileSize (fd, nullptr);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430585 file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
Ebrahim Byagowice173402018-04-20 10:29:06 +0430586 if (unlikely (file->mapping == nullptr)) goto fail;
587
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430588 file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430589 if (unlikely (file->contents == nullptr)) goto fail;
590
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430591 CloseHandle (fd);
592 return hb_blob_create (file->contents, file->length,
593 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
Ebrahim Byagowice173402018-04-20 10:29:06 +0430594 (hb_destroy_func_t) _hb_mapped_file_destroy);
595
596fail:
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430597 CloseHandle (fd);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430598fail_without_close:
Ebrahim Byagowice173402018-04-20 10:29:06 +0430599 free (file);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430600
601#endif
602
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430603 /* The following tries to read a file without knowing its size beforehand
604 It's used as a fallback for systems without mmap or to read from pipes */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430605 unsigned long len = 0, allocated = BUFSIZ * 16;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430606 char *data = (char *) malloc (allocated);
607 if (unlikely (data == nullptr)) return hb_blob_get_empty ();
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430608
609 FILE *fp = fopen (file_name, "rb");
610 if (unlikely (fp == nullptr)) goto fread_fail_without_close;
611
612 while (!feof (fp))
613 {
614 if (allocated - len < BUFSIZ)
615 {
616 allocated *= 2;
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430617 /* Don't allocate and go more than ~536MB, our mmap reader still
618 can cover files like that but lets limit our fallback reader */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430619 if (unlikely (allocated > (2 << 28))) goto fread_fail;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430620 char *new_data = (char *) realloc (data, allocated);
621 if (unlikely (new_data == nullptr)) goto fread_fail;
622 data = new_data;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430623 }
624
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430625 unsigned long addition = fread (data + len, 1, allocated - len, fp);
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430626
627 int err = ferror (fp);
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430628#ifdef EINTR // armcc doesn't have it
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430629 if (unlikely (err == EINTR)) continue;
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430630#endif
Ebrahim Byagowi8a51f912018-06-28 13:22:21 +0430631 if (unlikely (err)) goto fread_fail;
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430632
633 len += addition;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430634 }
635
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430636 return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430637 (hb_destroy_func_t) free);
638
639fread_fail:
640 fclose (fp);
641fread_fail_without_close:
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430642 free (data);
Ebrahim Byagowice173402018-04-20 10:29:06 +0430643 return hb_blob_get_empty ();
644}