blob: 4cf6d1e54513df0032e8766d8eb1c896c3fb3d0f [file] [log] [blame]
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009 Red Hat, Inc.
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04003 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04004 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04005 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Red Hat Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbodf88d3bd2013-01-14 00:33:58 -060027/* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */
Behdad Esfahbod73f7f892014-07-09 17:17:18 -040028#ifndef _POSIX_C_SOURCE
Ting-Wei Lane3a15d02018-01-12 15:33:16 +080029#define _POSIX_C_SOURCE 200809L
Behdad Esfahbod73f7f892014-07-09 17:17:18 -040030#endif
Behdad Esfahbodf88d3bd2013-01-14 00:33:58 -060031
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040032#include "hb-private.hh"
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040033#include "hb-debug.hh"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040034
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040035#include "hb-object-private.hh"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040036
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040037#ifdef HAVE_SYS_MMAN_H
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040038#ifdef HAVE_UNISTD_H
Behdad Esfahboda2644242009-08-03 17:53:29 -040039#include <unistd.h>
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040040#endif /* HAVE_UNISTD_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040041#include <sys/mman.h>
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040042#endif /* HAVE_SYS_MMAN_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040043
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040044#include <stdio.h>
45#include <errno.h>
46
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040047
Behdad Esfahbod1bc1cb32012-06-16 15:21:55 -040048struct hb_blob_t {
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040049 hb_object_header_t header;
Behdad Esfahbod6220e5f2012-06-06 03:30:09 -040050 ASSERT_POD ();
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040051
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040052 bool immutable;
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040053
54 const char *data;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040055 unsigned int length;
56 hb_memory_mode_t mode;
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040057
58 void *user_data;
59 hb_destroy_func_t destroy;
60};
61
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040062
63static bool _try_writable (hb_blob_t *blob);
64
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040065static void
66_hb_blob_destroy_user_data (hb_blob_t *blob)
67{
68 if (blob->destroy) {
69 blob->destroy (blob->user_data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020070 blob->user_data = nullptr;
71 blob->destroy = nullptr;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040072 }
73}
74
Behdad Esfahbod5f512012013-09-04 18:28:39 -040075/**
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080076 * hb_blob_create: (skip)
77 * @data: Pointer to blob data.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040078 * @length: Length of @data in bytes.
79 * @mode: Memory mode for @data.
Behdad Esfahbod2cd53232015-01-06 19:16:38 -080080 * @user_data: Data parameter to pass to @destroy.
81 * @destroy: Callback to call when @data is not needed anymore.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040082 *
83 * Creates a new "blob" object wrapping @data. The @mode parameter is used
84 * to negotiate ownership and lifecycle of @data.
85 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -040086 * Return value: New blob, or the empty blob if something failed or if @length is
Behdad Esfahbod5f512012013-09-04 18:28:39 -040087 * zero. Destroy with hb_blob_destroy().
88 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +043089 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -040090 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040091hb_blob_t *
92hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040093 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040094 hb_memory_mode_t mode,
Behdad Esfahbode5847f72011-04-20 02:59:28 -040095 void *user_data,
96 hb_destroy_func_t destroy)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040097{
98 hb_blob_t *blob;
99
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -0800100 if (!length ||
101 length >= 1u << 31 ||
Behdad Esfahbod7d5e7612014-12-18 18:22:21 -0800102 !(blob = hb_object_create<hb_blob_t> ())) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400103 if (destroy)
104 destroy (user_data);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400105 return hb_blob_get_empty ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400106 }
107
108 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400109 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400110 blob->mode = mode;
111
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400112 blob->user_data = user_data;
Behdad Esfahbode5847f72011-04-20 02:59:28 -0400113 blob->destroy = destroy;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400114
115 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
116 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400117 if (!_try_writable (blob)) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400118 hb_blob_destroy (blob);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400119 return hb_blob_get_empty ();
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400120 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400121 }
122
123 return blob;
124}
125
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200126static void
127_hb_blob_destroy (void *data)
128{
129 hb_blob_destroy ((hb_blob_t *) data);
130}
131
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400132/**
133 * hb_blob_create_sub_blob:
134 * @parent: Parent blob.
135 * @offset: Start offset of sub-blob within @parent, in bytes.
136 * @length: Length of sub-blob.
137 *
138 * Returns a blob that represents a range of bytes in @parent. The new
139 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
140 * will never modify data in the parent blob. The parent data is not
141 * expected to be modified, and will result in undefined behavior if it
142 * is.
143 *
144 * Makes @parent immutable.
145 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400146 * Return value: New blob, or the empty blob if something failed or if
147 * @length is zero or @offset is beyond the end of @parent's data. Destroy
148 * with hb_blob_destroy().
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400149 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430150 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400151 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400152hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400153hb_blob_create_sub_blob (hb_blob_t *parent,
154 unsigned int offset,
155 unsigned int length)
156{
157 hb_blob_t *blob;
158
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400159 if (!length || offset >= parent->length)
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400160 return hb_blob_get_empty ();
Behdad Esfahboda2644242009-08-03 17:53:29 -0400161
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400162 hb_blob_make_immutable (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400163
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400164 blob = hb_blob_create (parent->data + offset,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400165 MIN (length, parent->length - offset),
Behdad Esfahbodc3ba49b2013-02-25 17:06:35 -0500166 HB_MEMORY_MODE_READONLY,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400167 hb_blob_reference (parent),
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200168 _hb_blob_destroy);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400169
Behdad Esfahboda2644242009-08-03 17:53:29 -0400170 return blob;
171}
172
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400173/**
174 * hb_blob_get_empty:
175 *
176 * Returns the singleton empty blob.
177 *
178 * See TODO:link object types for more information.
179 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400180 * Return value: (transfer full): the empty blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400181 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430182 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400183 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400184hb_blob_t *
Behdad Esfahbod49110622011-05-02 19:36:39 -0400185hb_blob_get_empty (void)
Behdad Esfahboda2644242009-08-03 17:53:29 -0400186{
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400187 static const hb_blob_t _hb_blob_nil = {
188 HB_OBJECT_HEADER_STATIC,
189
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400190 true, /* immutable */
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400191
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200192 nullptr, /* data */
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400193 0, /* length */
194 HB_MEMORY_MODE_READONLY, /* mode */
195
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200196 nullptr, /* user_data */
197 nullptr /* destroy */
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400198 };
199
200 return const_cast<hb_blob_t *> (&_hb_blob_nil);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400201}
202
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400203/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400204 * hb_blob_reference: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400205 * @blob: a blob.
206 *
207 * Increases the reference count on @blob.
208 *
209 * See TODO:link object types for more information.
210 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400211 * Return value: @blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400212 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430213 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400214 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400215hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400216hb_blob_reference (hb_blob_t *blob)
217{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400218 return hb_object_reference (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400219}
220
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400221/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400222 * hb_blob_destroy: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400223 * @blob: a blob.
224 *
225 * Descreases the reference count on @blob, and if it reaches zero, destroys
226 * @blob, freeing all memory, possibly calling the destroy-callback the blob
227 * was created for if it has not been called already.
228 *
229 * See TODO:link object types for more information.
230 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430231 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400232 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400233void
234hb_blob_destroy (hb_blob_t *blob)
235{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400236 if (!hb_object_destroy (blob)) return;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400237
238 _hb_blob_destroy_user_data (blob);
239
240 free (blob);
241}
242
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400243/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400244 * hb_blob_set_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400245 * @blob: a blob.
246 * @key: key for data to set.
247 * @data: data to set.
248 * @destroy: callback to call when @data is not needed anymore.
249 * @replace: whether to replace an existing data with the same key.
250 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400251 * Return value:
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400252 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430253 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400254 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400255hb_bool_t
256hb_blob_set_user_data (hb_blob_t *blob,
257 hb_user_data_key_t *key,
258 void * data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200259 hb_destroy_func_t destroy,
260 hb_bool_t replace)
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400261{
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200262 return hb_object_set_user_data (blob, key, data, destroy, replace);
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400263}
264
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400265/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400266 * hb_blob_get_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400267 * @blob: a blob.
268 * @key: key for data to get.
269 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400270 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400271 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400272 * Return value: (transfer none):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400273 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430274 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400275 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400276void *
277hb_blob_get_user_data (hb_blob_t *blob,
278 hb_user_data_key_t *key)
279{
280 return hb_object_get_user_data (blob, key);
281}
282
283
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400284/**
285 * hb_blob_make_immutable:
286 * @blob: a blob.
287 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400288 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400289 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430290 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400291 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400292void
293hb_blob_make_immutable (hb_blob_t *blob)
294{
295 if (hb_object_is_inert (blob))
296 return;
297
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400298 blob->immutable = true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400299}
300
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400301/**
302 * hb_blob_is_immutable:
303 * @blob: a blob.
304 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400305 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400306 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400307 * Return value: TODO
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400308 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430309 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400310 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400311hb_bool_t
312hb_blob_is_immutable (hb_blob_t *blob)
313{
314 return blob->immutable;
315}
316
317
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400318/**
319 * hb_blob_get_length:
320 * @blob: a blob.
321 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400322 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400323 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400324 * Return value: the length of blob data in bytes.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400325 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430326 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400327 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400328unsigned int
329hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400330{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400331 return blob->length;
332}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400333
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400334/**
335 * hb_blob_get_data:
336 * @blob: a blob.
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400337 * @length: (out):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400338 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400339 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400340 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400341 * Returns: (transfer none) (array length=length):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400342 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430343 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400344 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400345const char *
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400346hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400347{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400348 if (length)
349 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400350
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400351 return blob->data;
352}
353
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400354/**
355 * hb_blob_get_data_writable:
356 * @blob: a blob.
357 * @length: (out): output length of the writable data.
358 *
359 * Tries to make blob data writable (possibly copying it) and
360 * return pointer to data.
361 *
362 * Fails if blob has been made immutable, or if memory allocation
363 * fails.
364 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400365 * Returns: (transfer none) (array length=length): Writable blob data,
366 * or %NULL if failed.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400367 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430368 * Since: 0.9.2
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400369 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400370char *
371hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400372{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400373 if (!_try_writable (blob)) {
374 if (length)
375 *length = 0;
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400376
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200377 return nullptr;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400378 }
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400379
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400380 if (length)
381 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400382
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400383 return const_cast<char *> (blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400384}
385
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400386
387static hb_bool_t
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400388_try_make_writable_inplace_unix (hb_blob_t *blob)
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400389{
390#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500391 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400392 const char *addr;
393
394#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500395 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400396#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500397 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400398#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500399 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400400#endif
401
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500402 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400403 DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400404 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400405 }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400406 DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400407
408 mask = ~(pagesize-1);
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500409 addr = (const char *) (((uintptr_t) blob->data) & mask);
410 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400411 DEBUG_MSG_FUNC (BLOB, blob,
412 "calling mprotect on [%p..%p] (%lu bytes)",
413 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400414 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400415 DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400416 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400417 }
418
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400419 blob->mode = HB_MEMORY_MODE_WRITABLE;
420
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400421 DEBUG_MSG_FUNC (BLOB, blob,
422 "successfully made [%p..%p] (%lu bytes) writable\n",
423 addr, addr+length, (unsigned long) length);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400424 return true;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400425#else
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400426 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400427#endif
428}
429
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400430static bool
431_try_writable_inplace (hb_blob_t *blob)
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400432{
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400433 DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400434
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400435 if (_try_make_writable_inplace_unix (blob))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400436 return true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400437
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400438 DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400439
440 /* Failed to make writable inplace, mark that */
441 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400442 return false;
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400443}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400444
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400445static bool
446_try_writable (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400447{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400448 if (blob->immutable)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400449 return false;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400450
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400451 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400452 return true;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400453
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400454 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400455 return true;
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400456
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400457 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400458 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400459
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400460
Behdad Esfahbodbc71ad42012-03-01 17:30:29 -0800461 DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400462
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400463 char *new_data;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400464
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400465 new_data = (char *) malloc (blob->length);
466 if (unlikely (!new_data))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400467 return false;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400468
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400469 DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400470
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400471 memcpy (new_data, blob->data, blob->length);
472 _hb_blob_destroy_user_data (blob);
473 blob->mode = HB_MEMORY_MODE_WRITABLE;
474 blob->data = new_data;
475 blob->user_data = new_data;
476 blob->destroy = free;
477
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400478 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400479}