blob: b82b4b2a3a3f80de0b3e0484b7b95a75140a9b3f [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
Behdad Esfahbod52c8d122013-01-14 13:51:46 -060029#define _POSIX_C_SOURCE 199309L
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 Esfahbodf0954d12009-07-30 15:33:57 -040033
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040034#include "hb-object-private.hh"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040035
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040036#ifdef HAVE_SYS_MMAN_H
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040037#ifdef HAVE_UNISTD_H
Behdad Esfahboda2644242009-08-03 17:53:29 -040038#include <unistd.h>
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040039#endif /* HAVE_UNISTD_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040040#include <sys/mman.h>
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040041#endif /* HAVE_SYS_MMAN_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040042
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040043#include <stdio.h>
44#include <errno.h>
45
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040046
47
Behdad Esfahbod95e20242009-08-28 16:31:20 -040048#ifndef HB_DEBUG_BLOB
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -040049#define HB_DEBUG_BLOB (HB_DEBUG+0)
Behdad Esfahbod95e20242009-08-28 16:31:20 -040050#endif
51
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040052
Behdad Esfahbod1bc1cb32012-06-16 15:21:55 -040053struct hb_blob_t {
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040054 hb_object_header_t header;
Behdad Esfahbod6220e5f2012-06-06 03:30:09 -040055 ASSERT_POD ();
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040056
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040057 bool immutable;
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040058
59 const char *data;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040060 unsigned int length;
61 hb_memory_mode_t mode;
Behdad Esfahbod56eb5ad2011-05-04 19:27:37 -040062
63 void *user_data;
64 hb_destroy_func_t destroy;
65};
66
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -040067
68static bool _try_writable (hb_blob_t *blob);
69
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040070static void
71_hb_blob_destroy_user_data (hb_blob_t *blob)
72{
73 if (blob->destroy) {
74 blob->destroy (blob->user_data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040075 blob->user_data = NULL;
Behdad Esfahbode5847f72011-04-20 02:59:28 -040076 blob->destroy = NULL;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040077 }
78}
79
Behdad Esfahbod5f512012013-09-04 18:28:39 -040080/**
Behdad Esfahbod085d4292013-09-12 17:14:33 -040081 * hb_blob_create: (Xconstructor)
Behdad Esfahboda8949df2013-09-13 20:23:51 -040082 * @data: (array length=length) (closure user_data) (destroy destroy) (scope notified) (transfer none): Pointer to blob data.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040083 * @length: Length of @data in bytes.
84 * @mode: Memory mode for @data.
Behdad Esfahbod288f2892013-09-06 15:40:22 -040085 * @user_data: Data parameter to pass to @destroy.
Behdad Esfahboda8949df2013-09-13 20:23:51 -040086 * @destroy: Callback to call when @data is not needed anymore.
Behdad Esfahbod5f512012013-09-04 18:28:39 -040087 *
88 * Creates a new "blob" object wrapping @data. The @mode parameter is used
89 * to negotiate ownership and lifecycle of @data.
90 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -040091 * Return value: New blob, or the empty blob if something failed or if @length is
Behdad Esfahbod5f512012013-09-04 18:28:39 -040092 * zero. Destroy with hb_blob_destroy().
93 *
94 * Since: 1.0
95 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040096hb_blob_t *
97hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040098 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040099 hb_memory_mode_t mode,
Behdad Esfahbode5847f72011-04-20 02:59:28 -0400100 void *user_data,
101 hb_destroy_func_t destroy)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400102{
103 hb_blob_t *blob;
104
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400105 if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400106 if (destroy)
107 destroy (user_data);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400108 return hb_blob_get_empty ();
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400109 }
110
111 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400112 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400113 blob->mode = mode;
114
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400115 blob->user_data = user_data;
Behdad Esfahbode5847f72011-04-20 02:59:28 -0400116 blob->destroy = destroy;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400117
118 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
119 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400120 if (!_try_writable (blob)) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400121 hb_blob_destroy (blob);
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400122 return hb_blob_get_empty ();
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400123 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400124 }
125
126 return blob;
127}
128
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400129/**
130 * hb_blob_create_sub_blob:
131 * @parent: Parent blob.
132 * @offset: Start offset of sub-blob within @parent, in bytes.
133 * @length: Length of sub-blob.
134 *
135 * Returns a blob that represents a range of bytes in @parent. The new
136 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
137 * will never modify data in the parent blob. The parent data is not
138 * expected to be modified, and will result in undefined behavior if it
139 * is.
140 *
141 * Makes @parent immutable.
142 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400143 * Return value: New blob, or the empty blob if something failed or if
144 * @length is zero or @offset is beyond the end of @parent's data. Destroy
145 * with hb_blob_destroy().
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400146 *
147 * Since: 1.0
148 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400149hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400150hb_blob_create_sub_blob (hb_blob_t *parent,
151 unsigned int offset,
152 unsigned int length)
153{
154 hb_blob_t *blob;
155
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400156 if (!length || offset >= parent->length)
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400157 return hb_blob_get_empty ();
Behdad Esfahboda2644242009-08-03 17:53:29 -0400158
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400159 hb_blob_make_immutable (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400160
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400161 blob = hb_blob_create (parent->data + offset,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400162 MIN (length, parent->length - offset),
Behdad Esfahbodc3ba49b2013-02-25 17:06:35 -0500163 HB_MEMORY_MODE_READONLY,
Behdad Esfahbodd4141a42011-05-03 00:19:18 -0400164 hb_blob_reference (parent),
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400165 (hb_destroy_func_t) hb_blob_destroy);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400166
Behdad Esfahboda2644242009-08-03 17:53:29 -0400167 return blob;
168}
169
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400170/**
171 * hb_blob_get_empty:
172 *
173 * Returns the singleton empty blob.
174 *
175 * See TODO:link object types for more information.
176 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400177 * Return value: (transfer full): the empty blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400178 *
179 * Since: 1.0
180 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400181hb_blob_t *
Behdad Esfahbod49110622011-05-02 19:36:39 -0400182hb_blob_get_empty (void)
Behdad Esfahboda2644242009-08-03 17:53:29 -0400183{
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400184 static const hb_blob_t _hb_blob_nil = {
185 HB_OBJECT_HEADER_STATIC,
186
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400187 true, /* immutable */
Behdad Esfahbodf06ab8a2012-06-05 12:31:51 -0400188
189 NULL, /* data */
190 0, /* length */
191 HB_MEMORY_MODE_READONLY, /* mode */
192
193 NULL, /* user_data */
194 NULL /* destroy */
195 };
196
197 return const_cast<hb_blob_t *> (&_hb_blob_nil);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400198}
199
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400200/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400201 * hb_blob_reference: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400202 * @blob: a blob.
203 *
204 * Increases the reference count on @blob.
205 *
206 * See TODO:link object types for more information.
207 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400208 * Return value: @blob.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400209 *
210 * Since: 1.0
211 **/
Behdad Esfahboda2644242009-08-03 17:53:29 -0400212hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400213hb_blob_reference (hb_blob_t *blob)
214{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400215 return hb_object_reference (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400216}
217
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400218/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400219 * hb_blob_destroy: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400220 * @blob: a blob.
221 *
222 * Descreases the reference count on @blob, and if it reaches zero, destroys
223 * @blob, freeing all memory, possibly calling the destroy-callback the blob
224 * was created for if it has not been called already.
225 *
226 * See TODO:link object types for more information.
227 *
228 * Since: 1.0
229 **/
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400230void
231hb_blob_destroy (hb_blob_t *blob)
232{
Behdad Esfahbod47e71d92011-04-27 16:38:03 -0400233 if (!hb_object_destroy (blob)) return;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400234
235 _hb_blob_destroy_user_data (blob);
236
237 free (blob);
238}
239
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400240/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400241 * hb_blob_set_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400242 * @blob: a blob.
243 * @key: key for data to set.
244 * @data: data to set.
245 * @destroy: callback to call when @data is not needed anymore.
246 * @replace: whether to replace an existing data with the same key.
247 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400248 * Return value:
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400249 *
250 * Since: 1.0
251 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400252hb_bool_t
253hb_blob_set_user_data (hb_blob_t *blob,
254 hb_user_data_key_t *key,
255 void * data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200256 hb_destroy_func_t destroy,
257 hb_bool_t replace)
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400258{
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200259 return hb_object_set_user_data (blob, key, data, destroy, replace);
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400260}
261
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400262/**
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400263 * hb_blob_get_user_data: (skip)
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400264 * @blob: a blob.
265 * @key: key for data to get.
266 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400267 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400268 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400269 * Return value: (transfer none):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400270 *
271 * Since: 1.0
272 **/
Behdad Esfahbod5fa849b2011-04-27 21:46:01 -0400273void *
274hb_blob_get_user_data (hb_blob_t *blob,
275 hb_user_data_key_t *key)
276{
277 return hb_object_get_user_data (blob, key);
278}
279
280
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400281/**
282 * hb_blob_make_immutable:
283 * @blob: a blob.
284 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400285 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400286 *
287 * Since: 1.0
288 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400289void
290hb_blob_make_immutable (hb_blob_t *blob)
291{
292 if (hb_object_is_inert (blob))
293 return;
294
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400295 blob->immutable = true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400296}
297
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400298/**
299 * hb_blob_is_immutable:
300 * @blob: a blob.
301 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400302 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400303 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400304 * Return value: TODO
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400305 *
306 * Since: 1.0
307 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400308hb_bool_t
309hb_blob_is_immutable (hb_blob_t *blob)
310{
311 return blob->immutable;
312}
313
314
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400315/**
316 * hb_blob_get_length:
317 * @blob: a blob.
318 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400319 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400320 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400321 * Return value: the length of blob data in bytes.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400322 *
323 * Since: 1.0
324 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400325unsigned int
326hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400327{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400328 return blob->length;
329}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400330
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400331/**
332 * hb_blob_get_data:
333 * @blob: a blob.
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400334 * @length: (out):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400335 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400336 *
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400337 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400338 * Returns: (transfer none) (array length=length):
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400339 *
340 * Since: 1.0
341 **/
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400342const char *
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400343hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400344{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400345 if (length)
346 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400347
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400348 return blob->data;
349}
350
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400351/**
352 * hb_blob_get_data_writable:
353 * @blob: a blob.
354 * @length: (out): output length of the writable data.
355 *
356 * Tries to make blob data writable (possibly copying it) and
357 * return pointer to data.
358 *
359 * Fails if blob has been made immutable, or if memory allocation
360 * fails.
361 *
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400362 * Returns: (transfer none) (array length=length): Writable blob data,
363 * or %NULL if failed.
Behdad Esfahbod5f512012013-09-04 18:28:39 -0400364 *
365 * Since: 1.0
366 **/
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400367char *
368hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400369{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400370 if (!_try_writable (blob)) {
371 if (length)
372 *length = 0;
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400373
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400374 return NULL;
375 }
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400376
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400377 if (length)
378 *length = blob->length;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400379
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400380 return const_cast<char *> (blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400381}
382
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400383
384static hb_bool_t
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400385_try_make_writable_inplace_unix (hb_blob_t *blob)
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400386{
387#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500388 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400389 const char *addr;
390
391#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500392 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400393#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500394 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400395#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500396 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400397#endif
398
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500399 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400400 DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400401 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400402 }
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400403 DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400404
405 mask = ~(pagesize-1);
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500406 addr = (const char *) (((uintptr_t) blob->data) & mask);
407 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400408 DEBUG_MSG_FUNC (BLOB, blob,
409 "calling mprotect on [%p..%p] (%lu bytes)",
410 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400411 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400412 DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400413 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400414 }
415
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400416 blob->mode = HB_MEMORY_MODE_WRITABLE;
417
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400418 DEBUG_MSG_FUNC (BLOB, blob,
419 "successfully made [%p..%p] (%lu bytes) writable\n",
420 addr, addr+length, (unsigned long) length);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400421 return true;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400422#else
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400423 return false;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400424#endif
425}
426
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400427static bool
428_try_writable_inplace (hb_blob_t *blob)
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400429{
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400430 DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400431
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400432 if (_try_make_writable_inplace_unix (blob))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400433 return true;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400434
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400435 DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400436
437 /* Failed to make writable inplace, mark that */
438 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400439 return false;
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400440}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400441
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400442static bool
443_try_writable (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400444{
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400445 if (blob->immutable)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400446 return false;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400447
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400448 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400449 return true;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400450
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400451 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400452 return true;
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400453
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400454 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400455 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400456
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400457
Behdad Esfahbodbc71ad42012-03-01 17:30:29 -0800458 DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400459
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400460 char *new_data;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400461
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400462 new_data = (char *) malloc (blob->length);
463 if (unlikely (!new_data))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400464 return false;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400465
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400466 DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400467
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400468 memcpy (new_data, blob->data, blob->length);
469 _hb_blob_destroy_user_data (blob);
470 blob->mode = HB_MEMORY_MODE_WRITABLE;
471 blob->data = new_data;
472 blob->user_data = new_data;
473 blob->destroy = free;
474
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400475 return true;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400476}