blob: 006de7d1215ee05edd9918c38bc4025bf111844f [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 Esfahbod9f6172d2019-01-14 20:45:31 -050028
29/* https://github.com/harfbuzz/harfbuzz/issues/1308
30 * http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
31 * https://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html
32 */
Ebrahim Byagowie66eb212019-05-18 07:44:48 -070033#if !defined(_POSIX_C_SOURCE) && !defined(_MSC_VER) && !defined(__NetBSD__)
Behdad Esfahbod8874eef2019-01-17 15:04:44 -050034#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wunused-macros"
Behdad Esfahbod9f6172d2019-01-14 20:45:31 -050036#define _POSIX_C_SOURCE 200809L
Behdad Esfahbod8874eef2019-01-17 15:04:44 -050037#pragma GCC diagnostic pop
Behdad Esfahbod9f6172d2019-01-14 20:45:31 -050038#endif
39
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070040#include "hb.hh"
41#include "hb-blob.hh"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040042
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040043#ifdef HAVE_SYS_MMAN_H
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040044#ifdef HAVE_UNISTD_H
Behdad Esfahboda2644242009-08-03 17:53:29 -040045#include <unistd.h>
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040046#endif /* HAVE_UNISTD_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040047#include <sys/mman.h>
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040048#endif /* HAVE_SYS_MMAN_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040049
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040050#include <stdio.h>
51#include <errno.h>
Ebrahim Byagowice173402018-04-20 10:29:06 +043052#include <stdlib.h>
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040053
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040054
Behdad Esfahbod00cf4e52018-10-27 04:07:33 -070055/**
56 * SECTION: hb-blob
Behdad Esfahbodcf5fa572018-10-27 04:50:38 -070057 * @title: hb-blob
Behdad Esfahbod00cf4e52018-10-27 04:07:33 -070058 * @short_description: Binary data containers
59 * @include: hb.h
60 *
61 * Blobs wrap a chunk of binary data to handle lifecycle management of data
62 * while it is passed between client and HarfBuzz. Blobs are primarily used
63 * to create font faces, but also to access font face tables, as well as
64 * pass around other binary data.
65 **/
66
67
Behdad Esfahbod5f512012013-09-04 18:28:39 -040068/**
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080069 * hb_blob_create: (skip)
70 * @data: Pointer to blob data.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040071 * @length: Length of @data in bytes.
72 * @mode: Memory mode for @data.
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080073 * @user_data: Data parameter to pass to @destroy.
74 * @destroy: Callback to call when @data is not needed anymore.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040075 *
76 * Creates a new "blob" object wrapping @data. The @mode parameter is used
77 * to negotiate ownership and lifecycle of @data.
78 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -040079 * Return value: New blob, or the empty blob if something failed or if @length is
Behdad Esfahbod5f512012013-09-04 18:28:39 -040080 * zero. Destroy with hb_blob_destroy().
81 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +043082 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -040083 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040084hb_blob_t *
85hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040086 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040087 hb_memory_mode_t mode,
Behdad Esfahbode5847f72011-04-20 02:59:28 -040088 void *user_data,
89 hb_destroy_func_t destroy)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040090{
91 hb_blob_t *blob;
92
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080093 if (!length ||
94 length >= 1u << 31 ||
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -080095 !(blob = hb_object_create<hb_blob_t> ())) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040096 if (destroy)
97 destroy (user_data);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -040098 return hb_blob_get_empty ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040099 }
100
101 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400102 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400103 blob->mode = mode;
104
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400105 blob->user_data = user_data;
Behdad Esfahbode5847f72011-04-20 02:59:28 -0400106 blob->destroy = destroy;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400107
108 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
109 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400110 if (!blob->try_make_writable ()) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400111 hb_blob_destroy (blob);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400112 return hb_blob_get_empty ();
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400113 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400114 }
115
116 return blob;
117}
118
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200119static void
120_hb_blob_destroy (void *data)
121{
122 hb_blob_destroy ((hb_blob_t *) data);
123}
124
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400125/**
126 * hb_blob_create_sub_blob:
127 * @parent: Parent blob.
128 * @offset: Start offset of sub-blob within @parent, in bytes.
129 * @length: Length of sub-blob.
130 *
131 * Returns a blob that represents a range of bytes in @parent. The new
132 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
133 * will never modify data in the parent blob. The parent data is not
134 * expected to be modified, and will result in undefined behavior if it
135 * is.
136 *
137 * Makes @parent immutable.
138 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400139 * Return value: New blob, or the empty blob if something failed or if
140 * @length is zero or @offset is beyond the end of @parent's data. Destroy
141 * with hb_blob_destroy().
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400142 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430143 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400144 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400145hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400146hb_blob_create_sub_blob (hb_blob_t *parent,
147 unsigned int offset,
148 unsigned int length)
149{
150 hb_blob_t *blob;
151
Behdad Esfahbodda408fc2018-11-03 15:49:37 -0400152 if (!length || !parent || offset >= parent->length)
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400153 return hb_blob_get_empty ();
Behdad Esfahboda2644242009-08-03 17:53:29 -0400154
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400155 hb_blob_make_immutable (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400156
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400157 blob = hb_blob_create (parent->data + offset,
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700158 hb_min (length, parent->length - offset),
Behdad Esfahbodc3ba49b2013-02-25 17:06:35 -0500159 HB_MEMORY_MODE_READONLY,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400160 hb_blob_reference (parent),
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200161 _hb_blob_destroy);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400162
Behdad Esfahboda2644242009-08-03 17:53:29 -0400163 return blob;
164}
165
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400166/**
Behdad Esfahboda989f3e2018-02-13 22:12:36 -0800167 * hb_blob_copy_writable_or_fail:
168 * @blob: A blob.
169 *
170 * Makes a writable copy of @blob.
171 *
172 * Return value: New blob, or nullptr if allocation failed.
173 *
174 * Since: 1.8.0
175 **/
176hb_blob_t *
177hb_blob_copy_writable_or_fail (hb_blob_t *blob)
178{
179 blob = hb_blob_create (blob->data,
180 blob->length,
181 HB_MEMORY_MODE_DUPLICATE,
182 nullptr,
183 nullptr);
184
185 if (unlikely (blob == hb_blob_get_empty ()))
186 blob = nullptr;
187
188 return blob;
189}
190
191/**
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400192 * hb_blob_get_empty:
193 *
194 * Returns the singleton empty blob.
195 *
196 * See TODO:link object types for more information.
197 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400198 * Return value: (transfer full): the empty blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400199 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430200 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400201 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400202hb_blob_t *
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330203hb_blob_get_empty ()
Behdad Esfahboda2644242009-08-03 17:53:29 -0400204{
Behdad Esfahbod35066722018-08-06 06:17:48 -0700205 return const_cast<hb_blob_t *> (&Null(hb_blob_t));
Behdad Esfahboda2644242009-08-03 17:53:29 -0400206}
207
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400208/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400209 * hb_blob_reference: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400210 * @blob: a blob.
211 *
212 * Increases the reference count on @blob.
213 *
214 * See TODO:link object types for more information.
215 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400216 * Return value: @blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400217 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430218 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400219 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400220hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400221hb_blob_reference (hb_blob_t *blob)
222{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400223 return hb_object_reference (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400224}
225
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400226/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400227 * hb_blob_destroy: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400228 * @blob: a blob.
229 *
Bruce Mitchener90218fa2018-01-31 20:44:45 +0700230 * Decreases the reference count on @blob, and if it reaches zero, destroys
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400231 * @blob, freeing all memory, possibly calling the destroy-callback the blob
232 * was created for if it has not been called already.
233 *
234 * See TODO:link object types for more information.
235 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430236 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400237 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400238void
239hb_blob_destroy (hb_blob_t *blob)
240{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400241 if (!hb_object_destroy (blob)) return;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400242
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400243 blob->fini_shallow ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400244
245 free (blob);
246}
247
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400248/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400249 * hb_blob_set_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400250 * @blob: a blob.
251 * @key: key for data to set.
252 * @data: data to set.
253 * @destroy: callback to call when @data is not needed anymore.
254 * @replace: whether to replace an existing data with the same key.
255 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430256 * Return value:
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400257 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430258 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400259 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400260hb_bool_t
261hb_blob_set_user_data (hb_blob_t *blob,
262 hb_user_data_key_t *key,
263 void * data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200264 hb_destroy_func_t destroy,
265 hb_bool_t replace)
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400266{
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200267 return hb_object_set_user_data (blob, key, data, destroy, replace);
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400268}
269
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400270/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400271 * hb_blob_get_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400272 * @blob: a blob.
273 * @key: key for data to get.
274 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400275 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430276 *
277 * Return value: (transfer none):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400278 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430279 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400280 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400281void *
282hb_blob_get_user_data (hb_blob_t *blob,
283 hb_user_data_key_t *key)
284{
285 return hb_object_get_user_data (blob, key);
286}
287
288
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400289/**
290 * hb_blob_make_immutable:
291 * @blob: a blob.
292 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430293 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400294 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430295 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400296 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400297void
298hb_blob_make_immutable (hb_blob_t *blob)
299{
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400300 if (hb_object_is_immutable (blob))
Behdad Esfahbod90a0f9f2018-09-26 15:03:07 -0400301 return;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400302
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400303 hb_object_make_immutable (blob);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400304}
305
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400306/**
307 * hb_blob_is_immutable:
308 * @blob: a blob.
309 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430310 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400311 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400312 * Return value: TODO
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400313 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430314 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400315 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400316hb_bool_t
317hb_blob_is_immutable (hb_blob_t *blob)
318{
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400319 return hb_object_is_immutable (blob);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400320}
321
322
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400323/**
324 * hb_blob_get_length:
325 * @blob: a blob.
326 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430327 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400328 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400329 * Return value: the length of blob data in bytes.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400330 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430331 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400332 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400333unsigned int
334hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400335{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400336 return blob->length;
337}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400338
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400339/**
340 * hb_blob_get_data:
341 * @blob: a blob.
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400342 * @length: (out):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400343 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400344 *
Ebrahim Byagowice173402018-04-20 10:29:06 +0430345 *
346 * Returns: (transfer none) (array length=length):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400347 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430348 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400349 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400350const char *
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400351hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400352{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400353 if (length)
354 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400355
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400356 return blob->data;
357}
358
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400359/**
360 * hb_blob_get_data_writable:
361 * @blob: a blob.
362 * @length: (out): output length of the writable data.
363 *
364 * Tries to make blob data writable (possibly copying it) and
365 * return pointer to data.
366 *
367 * Fails if blob has been made immutable, or if memory allocation
368 * fails.
369 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400370 * Returns: (transfer none) (array length=length): Writable blob data,
371 * or %NULL if failed.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400372 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430373 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400374 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400375char *
376hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400377{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400378 if (!blob->try_make_writable ()) {
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400379 if (length)
380 *length = 0;
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400381
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200382 return nullptr;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400383 }
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400384
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400385 if (length)
386 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400387
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400388 return const_cast<char *> (blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400389}
390
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400391
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400392bool
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330393hb_blob_t::try_make_writable_inplace_unix ()
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400394{
395#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500396 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400397 const char *addr;
398
399#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500400 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400401#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500402 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400403#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500404 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400405#endif
406
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500407 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400408 DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400409 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400410 }
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400411 DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400412
413 mask = ~(pagesize-1);
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400414 addr = (const char *) (((uintptr_t) this->data) & mask);
415 length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask) - addr;
416 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400417 "calling mprotect on [%p..%p] (%lu bytes)",
418 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400419 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400420 DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400421 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400422 }
423
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400424 this->mode = HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400425
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400426 DEBUG_MSG_FUNC (BLOB, this,
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400427 "successfully made [%p..%p] (%lu bytes) writable\n",
428 addr, addr+length, (unsigned long) length);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400429 return true;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400430#else
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400431 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400432#endif
433}
434
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400435bool
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330436hb_blob_t::try_make_writable_inplace ()
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400437{
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400438 DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400439
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400440 if (this->try_make_writable_inplace_unix ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400441 return true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400442
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400443 DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400444
445 /* Failed to make writable inplace, mark that */
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400446 this->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400447 return false;
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400448}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400449
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400450bool
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330451hb_blob_t::try_make_writable ()
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400452{
Behdad Esfahbod5570c872018-11-03 14:51:38 -0400453 if (hb_object_is_immutable (this))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400454 return false;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -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 Esfahbod7f3d5c82009-08-06 13:33:51 -0400458
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400459 if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400460 return true;
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400461
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400462 if (this->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400463 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400464
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400465
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400466 DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400467
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400468 char *new_data;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400469
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400470 new_data = (char *) malloc (this->length);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400471 if (unlikely (!new_data))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400472 return false;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400473
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400474 DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400475
Behdad Esfahbod90baf722018-05-03 22:14:54 -0400476 memcpy (new_data, this->data, this->length);
477 this->destroy_user_data ();
478 this->mode = HB_MEMORY_MODE_WRITABLE;
479 this->data = new_data;
480 this->user_data = new_data;
481 this->destroy = free;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400482
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400483 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400484}
Ebrahim Byagowice173402018-04-20 10:29:06 +0430485
Behdad Esfahbod5c64d612018-05-03 21:10:57 -0400486/*
487 * Mmap
488 */
489
Ebrahim Byagowi12cec6c2019-06-21 22:39:42 +0430490#ifndef HB_NO_OPEN
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430491#ifdef HAVE_MMAP
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430492# include <sys/types.h>
493# include <sys/stat.h>
494# include <fcntl.h>
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430495#endif
496
Ken Browneee5b5e2018-11-12 21:05:39 -0500497#ifdef _WIN32
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430498# include <windows.h>
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430499#else
Khaled Hosny30cbe612018-10-19 22:04:56 +0200500# ifndef O_BINARY
501# define O_BINARY 0
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430502# endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430503#endif
504
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430505#ifndef MAP_NORESERVE
506# define MAP_NORESERVE 0
507#endif
508
Ebrahim Byagowice173402018-04-20 10:29:06 +0430509struct hb_mapped_file_t
510{
511 char *contents;
512 unsigned long length;
Ken Browneee5b5e2018-11-12 21:05:39 -0500513#ifdef _WIN32
Ebrahim Byagowice173402018-04-20 10:29:06 +0430514 HANDLE mapping;
515#endif
516};
517
Ken Browneee5b5e2018-11-12 21:05:39 -0500518#if (defined(HAVE_MMAP) || defined(_WIN32)) && !defined(HB_NO_MMAP)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430519static void
Behdad Esfahbod6e070762018-10-14 22:22:45 -0700520_hb_mapped_file_destroy (void *file_)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430521{
Behdad Esfahbod6e070762018-10-14 22:22:45 -0700522 hb_mapped_file_t *file = (hb_mapped_file_t *) file_;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430523#ifdef HAVE_MMAP
524 munmap (file->contents, file->length);
Ken Browneee5b5e2018-11-12 21:05:39 -0500525#elif defined(_WIN32)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430526 UnmapViewOfFile (file->contents);
527 CloseHandle (file->mapping);
528#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430529 assert (0); // If we don't have mmap we shouldn't reach here
Ebrahim Byagowice173402018-04-20 10:29:06 +0430530#endif
531
532 free (file);
533}
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430534#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430535
536/**
537 * hb_blob_create_from_file:
538 * @file_name: font filename.
539 *
540 * Returns: A hb_blob_t pointer with the content of the file
541 *
Behdad Esfahboddf01f3e2018-06-05 15:17:39 -0700542 * Since: 1.7.7
Ebrahim Byagowice173402018-04-20 10:29:06 +0430543 **/
544hb_blob_t *
545hb_blob_create_from_file (const char *file_name)
546{
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430547 /* Adopted from glib's gmappedfile.c with Matthias Clasen and
548 Allison Lortie permission but changed a lot to suit our need. */
549#if defined(HAVE_MMAP) && !defined(HB_NO_MMAP)
Ebrahim Byagowi81003802018-04-22 10:58:37 +0430550 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430551 if (unlikely (!file)) return hb_blob_get_empty ();
552
Khaled Hosny30cbe612018-10-19 22:04:56 +0200553 int fd = open (file_name, O_RDONLY | O_BINARY, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430554 if (unlikely (fd == -1)) goto fail_without_close;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430555
556 struct stat st;
557 if (unlikely (fstat (fd, &st) == -1)) goto fail;
558
Ebrahim Byagowice173402018-04-20 10:29:06 +0430559 file->length = (unsigned long) st.st_size;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430560 file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430561 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430562
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430563 if (unlikely (file->contents == MAP_FAILED)) goto fail;
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430564
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430565 close (fd);
566
567 return hb_blob_create (file->contents, file->length,
568 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
569 (hb_destroy_func_t) _hb_mapped_file_destroy);
570
571fail:
572 close (fd);
573fail_without_close:
574 free (file);
575
Ken Browneee5b5e2018-11-12 21:05:39 -0500576#elif defined(_WIN32) && !defined(HB_NO_MMAP)
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430577 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
578 if (unlikely (!file)) return hb_blob_get_empty ();
579
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430580 HANDLE fd;
581 unsigned int size = strlen (file_name) + 1;
582 wchar_t * wchar_file_name = (wchar_t *) malloc (sizeof (wchar_t) * size);
583 if (unlikely (wchar_file_name == nullptr)) goto fail_without_close;
584 mbstowcs (wchar_file_name, file_name, size);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000585#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
586 {
587 CREATEFILE2_EXTENDED_PARAMETERS ceparams = { 0 };
588 ceparams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
589 ceparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFFF;
590 ceparams.dwFileFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFF00000;
591 ceparams.dwSecurityQosFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0x000F0000;
592 ceparams.lpSecurityAttributes = nullptr;
593 ceparams.hTemplateFile = nullptr;
594 fd = CreateFile2 (wchar_file_name, GENERIC_READ, FILE_SHARE_READ,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430595 OPEN_EXISTING, &ceparams);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000596 }
597#else
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430598 fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
599 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
600 nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000601#endif
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430602 free (wchar_file_name);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430603
604 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
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 {
608 LARGE_INTEGER length;
609 GetFileSizeEx (fd, &length);
610 file->length = length.LowPart;
611 file->mapping = CreateFileMappingFromApp (fd, nullptr, PAGE_READONLY, length.QuadPart, nullptr);
612 }
613#else
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430614 file->length = (unsigned long) GetFileSize (fd, nullptr);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430615 file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000616#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430617 if (unlikely (file->mapping == nullptr)) goto fail;
618
Matt Oliver24dd6c12018-09-23 18:08:30 +1000619#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
620 file->contents = (char *) MapViewOfFileFromApp (file->mapping, FILE_MAP_READ, 0, 0);
621#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430622 file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000623#endif
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430624 if (unlikely (file->contents == nullptr)) goto fail;
625
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430626 CloseHandle (fd);
627 return hb_blob_create (file->contents, file->length,
628 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
Ebrahim Byagowice173402018-04-20 10:29:06 +0430629 (hb_destroy_func_t) _hb_mapped_file_destroy);
630
631fail:
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430632 CloseHandle (fd);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430633fail_without_close:
Ebrahim Byagowice173402018-04-20 10:29:06 +0430634 free (file);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430635
636#endif
637
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430638 /* The following tries to read a file without knowing its size beforehand
639 It's used as a fallback for systems without mmap or to read from pipes */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430640 unsigned long len = 0, allocated = BUFSIZ * 16;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430641 char *data = (char *) malloc (allocated);
642 if (unlikely (data == nullptr)) return hb_blob_get_empty ();
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430643
644 FILE *fp = fopen (file_name, "rb");
645 if (unlikely (fp == nullptr)) goto fread_fail_without_close;
646
647 while (!feof (fp))
648 {
649 if (allocated - len < BUFSIZ)
650 {
651 allocated *= 2;
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430652 /* Don't allocate and go more than ~536MB, our mmap reader still
653 can cover files like that but lets limit our fallback reader */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430654 if (unlikely (allocated > (2 << 28))) goto fread_fail;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430655 char *new_data = (char *) realloc (data, allocated);
656 if (unlikely (new_data == nullptr)) goto fread_fail;
657 data = new_data;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430658 }
659
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430660 unsigned long addition = fread (data + len, 1, allocated - len, fp);
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430661
662 int err = ferror (fp);
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430663#ifdef EINTR // armcc doesn't have it
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430664 if (unlikely (err == EINTR)) continue;
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430665#endif
Ebrahim Byagowi8a51f912018-06-28 13:22:21 +0430666 if (unlikely (err)) goto fread_fail;
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430667
668 len += addition;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430669 }
670
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430671 return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430672 (hb_destroy_func_t) free);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430673
674fread_fail:
675 fclose (fp);
676fread_fail_without_close:
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430677 free (data);
Ebrahim Byagowice173402018-04-20 10:29:06 +0430678 return hb_blob_get_empty ();
679}
Ebrahim Byagowi12cec6c2019-06-21 22:39:42 +0430680#endif /* !HB_NO_OPEN */