blob: 4b036e80433d945a541b3c0deb7c69d8c55414df [file] [log] [blame]
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009 Red Hat, Inc.
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +04303 * Copyright © 2018 Ebrahim Byagowi
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04006 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 */
27
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070028#include "hb.hh"
29#include "hb-blob.hh"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040030
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040031#ifdef HAVE_SYS_MMAN_H
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040032#ifdef HAVE_UNISTD_H
Behdad Esfahboda2644242009-08-03 17:53:29 -040033#include <unistd.h>
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040034#endif /* HAVE_UNISTD_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040035#include <sys/mman.h>
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040036#endif /* HAVE_SYS_MMAN_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040037
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040038#include <stdio.h>
39#include <errno.h>
Ebrahim Byagowice173402018-04-20 10:29:06 +043040#include <stdlib.h>
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040041
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040042
Behdad Esfahbod00cf4e52018-10-27 04:07:33 -070043/**
44 * SECTION: hb-blob
Behdad Esfahbodcf5fa572018-10-27 04:50:38 -070045 * @title: hb-blob
Behdad Esfahbod00cf4e52018-10-27 04:07:33 -070046 * @short_description: Binary data containers
47 * @include: hb.h
48 *
49 * Blobs wrap a chunk of binary data to handle lifecycle management of data
50 * while it is passed between client and HarfBuzz. Blobs are primarily used
51 * to create font faces, but also to access font face tables, as well as
52 * pass around other binary data.
53 **/
54
55
Behdad Esfahbod35066722018-08-06 06:17:48 -070056DEFINE_NULL_INSTANCE (hb_blob_t) =
57{
58 HB_OBJECT_HEADER_STATIC,
59
Behdad Esfahbod35066722018-08-06 06:17:48 -070060 nullptr, /* data */
61 0, /* length */
62 HB_MEMORY_MODE_READONLY, /* mode */
63
64 nullptr, /* user_data */
65 nullptr /* destroy */
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 Esfahbod4101ca72011-05-11 14:30:56 -0400152 if (!length || 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 Esfahbodd4141a42011-05-03 00:19:18 -0400158 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 *
Behdad Esfahbod49110622011-05-02 19:36:39 -0400203hb_blob_get_empty (void)
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
393hb_blob_t::try_make_writable_inplace_unix (void)
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
436hb_blob_t::try_make_writable_inplace (void)
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
451hb_blob_t::try_make_writable (void)
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 Byagowi7e76d742018-05-11 13:40:33 +0430490#ifdef HAVE_MMAP
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430491# include <sys/types.h>
492# include <sys/stat.h>
493# include <fcntl.h>
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430494#endif
495
Ebrahim Byagowice173402018-04-20 10:29:06 +0430496#if defined(_WIN32) || defined(__CYGWIN__)
Ebrahim Byagowi33eb1bd2018-05-11 14:36:41 +0430497# include <windows.h>
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430498#else
Khaled Hosny30cbe612018-10-19 22:04:56 +0200499# ifndef O_BINARY
500# define O_BINARY 0
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430501# endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430502#endif
503
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430504#ifndef MAP_NORESERVE
505# define MAP_NORESERVE 0
506#endif
507
Ebrahim Byagowice173402018-04-20 10:29:06 +0430508struct hb_mapped_file_t
509{
510 char *contents;
511 unsigned long length;
512#if defined(_WIN32) || defined(__CYGWIN__)
513 HANDLE mapping;
514#endif
515};
516
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430517#if (defined(HAVE_MMAP) || defined(_WIN32) || defined(__CYGWIN__)) && !defined(HB_NO_MMAP)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430518static void
Behdad Esfahbod6e070762018-10-14 22:22:45 -0700519_hb_mapped_file_destroy (void *file_)
Ebrahim Byagowice173402018-04-20 10:29:06 +0430520{
Behdad Esfahbod6e070762018-10-14 22:22:45 -0700521 hb_mapped_file_t *file = (hb_mapped_file_t *) file_;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430522#ifdef HAVE_MMAP
523 munmap (file->contents, file->length);
524#elif defined(_WIN32) || defined(__CYGWIN__)
525 UnmapViewOfFile (file->contents);
526 CloseHandle (file->mapping);
527#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430528 assert (0); // If we don't have mmap we shouldn't reach here
Ebrahim Byagowice173402018-04-20 10:29:06 +0430529#endif
530
531 free (file);
532}
Behdad Esfahbod2cb075f2018-07-03 13:04:05 +0430533#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430534
535/**
536 * hb_blob_create_from_file:
537 * @file_name: font filename.
538 *
539 * Returns: A hb_blob_t pointer with the content of the file
540 *
Behdad Esfahboddf01f3e2018-06-05 15:17:39 -0700541 * Since: 1.7.7
Ebrahim Byagowice173402018-04-20 10:29:06 +0430542 **/
543hb_blob_t *
544hb_blob_create_from_file (const char *file_name)
545{
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430546 /* Adopted from glib's gmappedfile.c with Matthias Clasen and
547 Allison Lortie permission but changed a lot to suit our need. */
548#if defined(HAVE_MMAP) && !defined(HB_NO_MMAP)
Ebrahim Byagowi81003802018-04-22 10:58:37 +0430549 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430550 if (unlikely (!file)) return hb_blob_get_empty ();
551
Khaled Hosny30cbe612018-10-19 22:04:56 +0200552 int fd = open (file_name, O_RDONLY | O_BINARY, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430553 if (unlikely (fd == -1)) goto fail_without_close;
Ebrahim Byagowice173402018-04-20 10:29:06 +0430554
555 struct stat st;
556 if (unlikely (fstat (fd, &st) == -1)) goto fail;
557
Ebrahim Byagowice173402018-04-20 10:29:06 +0430558 file->length = (unsigned long) st.st_size;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430559 file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
Ebrahim Byagowi65c82172018-05-26 23:50:10 +0430560 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430561
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430562 if (unlikely (file->contents == MAP_FAILED)) goto fail;
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430563
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430564 close (fd);
565
566 return hb_blob_create (file->contents, file->length,
567 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
568 (hb_destroy_func_t) _hb_mapped_file_destroy);
569
570fail:
571 close (fd);
572fail_without_close:
573 free (file);
574
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430575#elif (defined(_WIN32) || defined(__CYGWIN__)) && !defined(HB_NO_MMAP)
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430576 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
577 if (unlikely (!file)) return hb_blob_get_empty ();
578
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430579 HANDLE fd;
580 unsigned int size = strlen (file_name) + 1;
581 wchar_t * wchar_file_name = (wchar_t *) malloc (sizeof (wchar_t) * size);
582 if (unlikely (wchar_file_name == nullptr)) goto fail_without_close;
583 mbstowcs (wchar_file_name, file_name, size);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000584#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
585 {
586 CREATEFILE2_EXTENDED_PARAMETERS ceparams = { 0 };
587 ceparams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
588 ceparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFFF;
589 ceparams.dwFileFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFF00000;
590 ceparams.dwSecurityQosFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0x000F0000;
591 ceparams.lpSecurityAttributes = nullptr;
592 ceparams.hTemplateFile = nullptr;
593 fd = CreateFile2 (wchar_file_name, GENERIC_READ, FILE_SHARE_READ,
594 OPEN_EXISTING, &ceparams);
595 }
596#else
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430597 fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
598 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
599 nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000600#endif
Ebrahim Byagowid8a7ded2018-07-04 15:33:39 +0430601 free (wchar_file_name);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430602
603 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
604
Matt Oliver24dd6c12018-09-23 18:08:30 +1000605#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
606 {
607 LARGE_INTEGER length;
608 GetFileSizeEx (fd, &length);
609 file->length = length.LowPart;
610 file->mapping = CreateFileMappingFromApp (fd, nullptr, PAGE_READONLY, length.QuadPart, nullptr);
611 }
612#else
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430613 file->length = (unsigned long) GetFileSize (fd, nullptr);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430614 file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000615#endif
Ebrahim Byagowice173402018-04-20 10:29:06 +0430616 if (unlikely (file->mapping == nullptr)) goto fail;
617
Matt Oliver24dd6c12018-09-23 18:08:30 +1000618#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
619 file->contents = (char *) MapViewOfFileFromApp (file->mapping, FILE_MAP_READ, 0, 0);
620#else
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430621 file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
Matt Oliver24dd6c12018-09-23 18:08:30 +1000622#endif
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430623 if (unlikely (file->contents == nullptr)) goto fail;
624
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430625 CloseHandle (fd);
626 return hb_blob_create (file->contents, file->length,
627 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
Ebrahim Byagowice173402018-04-20 10:29:06 +0430628 (hb_destroy_func_t) _hb_mapped_file_destroy);
629
630fail:
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430631 CloseHandle (fd);
Ebrahim Byagowi7e76d742018-05-11 13:40:33 +0430632fail_without_close:
Ebrahim Byagowice173402018-04-20 10:29:06 +0430633 free (file);
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430634
635#endif
636
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430637 /* The following tries to read a file without knowing its size beforehand
638 It's used as a fallback for systems without mmap or to read from pipes */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430639 unsigned long len = 0, allocated = BUFSIZ * 16;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430640 char *data = (char *) malloc (allocated);
641 if (unlikely (data == nullptr)) return hb_blob_get_empty ();
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430642
643 FILE *fp = fopen (file_name, "rb");
644 if (unlikely (fp == nullptr)) goto fread_fail_without_close;
645
646 while (!feof (fp))
647 {
648 if (allocated - len < BUFSIZ)
649 {
650 allocated *= 2;
Ebrahim Byagowi5d8cafc2018-07-01 01:54:14 +0430651 /* Don't allocate and go more than ~536MB, our mmap reader still
652 can cover files like that but lets limit our fallback reader */
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430653 if (unlikely (allocated > (2 << 28))) goto fread_fail;
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430654 char *new_data = (char *) realloc (data, allocated);
655 if (unlikely (new_data == nullptr)) goto fread_fail;
656 data = new_data;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430657 }
658
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430659 unsigned long addition = fread (data + len, 1, allocated - len, fp);
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430660
661 int err = ferror (fp);
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430662#ifdef EINTR // armcc doesn't have it
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430663 if (unlikely (err == EINTR)) continue;
Ebrahim Byagowi25970a92018-06-28 14:32:36 +0430664#endif
Ebrahim Byagowi8a51f912018-06-28 13:22:21 +0430665 if (unlikely (err)) goto fread_fail;
Ebrahim Byagowifa090ed2018-06-27 14:13:26 +0430666
667 len += addition;
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430668 }
669
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430670 return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
Ebrahim Byagowi35ce8f32018-06-25 22:23:43 +0430671 (hb_destroy_func_t) free);
672
673fread_fail:
674 fclose (fp);
675fread_fail_without_close:
Ebrahim Byagowi7b4099f2018-06-27 16:54:44 +0430676 free (data);
Ebrahim Byagowice173402018-04-20 10:29:06 +0430677 return hb_blob_get_empty ();
678}