Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-blob.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
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
27 /* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */
28 #ifndef _POSIX_C_SOURCE
29 #define _POSIX_C_SOURCE 200809L
30 #endif
31
32 #include "hb-private.hh"
33 #include "hb-debug.hh"
34
35 #include "hb-object-private.hh"
36
37 #ifdef HAVE_SYS_MMAN_H
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif /* HAVE_UNISTD_H */
41 #include <sys/mman.h>
42 #endif /* HAVE_SYS_MMAN_H */
43
44 #include <stdio.h>
45 #include <errno.h>
46
47
48 struct hb_blob_t {
49   hb_object_header_t header;
50   ASSERT_POD ();
51
52   bool immutable;
53
54   const char *data;
55   unsigned int length;
56   hb_memory_mode_t mode;
57
58   void *user_data;
59   hb_destroy_func_t destroy;
60 };
61
62
63 static bool _try_writable (hb_blob_t *blob);
64
65 static void
66 _hb_blob_destroy_user_data (hb_blob_t *blob)
67 {
68   if (blob->destroy) {
69     blob->destroy (blob->user_data);
70     blob->user_data = nullptr;
71     blob->destroy = nullptr;
72   }
73 }
74
75 /**
76  * hb_blob_create: (skip)
77  * @data: Pointer to blob data.
78  * @length: Length of @data in bytes.
79  * @mode: Memory mode for @data.
80  * @user_data: Data parameter to pass to @destroy.
81  * @destroy: Callback to call when @data is not needed anymore.
82  *
83  * Creates a new "blob" object wrapping @data.  The @mode parameter is used
84  * to negotiate ownership and lifecycle of @data.
85  *
86  * Return value: New blob, or the empty blob if something failed or if @length is
87  * zero.  Destroy with hb_blob_destroy().
88  *
89  * Since: 0.9.2
90  **/
91 hb_blob_t *
92 hb_blob_create (const char        *data,
93                 unsigned int       length,
94                 hb_memory_mode_t   mode,
95                 void              *user_data,
96                 hb_destroy_func_t  destroy)
97 {
98   hb_blob_t *blob;
99
100   if (!length ||
101       length >= 1u << 31 ||
102       !(blob = hb_object_create<hb_blob_t> ())) {
103     if (destroy)
104       destroy (user_data);
105     return hb_blob_get_empty ();
106   }
107
108   blob->data = data;
109   blob->length = length;
110   blob->mode = mode;
111
112   blob->user_data = user_data;
113   blob->destroy = destroy;
114
115   if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
116     blob->mode = HB_MEMORY_MODE_READONLY;
117     if (!_try_writable (blob)) {
118       hb_blob_destroy (blob);
119       return hb_blob_get_empty ();
120     }
121   }
122
123   return blob;
124 }
125
126 static void
127 _hb_blob_destroy (void *data)
128 {
129   hb_blob_destroy ((hb_blob_t *) data);
130 }
131
132 /**
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  *
146  * 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().
149  *
150  * Since: 0.9.2
151  **/
152 hb_blob_t *
153 hb_blob_create_sub_blob (hb_blob_t    *parent,
154                          unsigned int  offset,
155                          unsigned int  length)
156 {
157   hb_blob_t *blob;
158
159   if (!length || offset >= parent->length)
160     return hb_blob_get_empty ();
161
162   hb_blob_make_immutable (parent);
163
164   blob = hb_blob_create (parent->data + offset,
165                          MIN (length, parent->length - offset),
166                          HB_MEMORY_MODE_READONLY,
167                          hb_blob_reference (parent),
168                          _hb_blob_destroy);
169
170   return blob;
171 }
172
173 /**
174  * hb_blob_copy_writable_or_fail:
175  * @blob: A blob.
176  *
177  * Makes a writable copy of @blob.
178  *
179  * Return value: New blob, or nullptr if allocation failed.
180  *
181  * Since: 1.8.0
182  **/
183 hb_blob_t *
184 hb_blob_copy_writable_or_fail (hb_blob_t *blob)
185 {
186   blob = hb_blob_create (blob->data,
187                          blob->length,
188                          HB_MEMORY_MODE_DUPLICATE,
189                          nullptr,
190                          nullptr);
191
192   if (unlikely (blob == hb_blob_get_empty ()))
193     blob = nullptr;
194
195   return blob;
196 }
197
198 /**
199  * hb_blob_get_empty:
200  *
201  * Returns the singleton empty blob.
202  *
203  * See TODO:link object types for more information.
204  *
205  * Return value: (transfer full): the empty blob.
206  *
207  * Since: 0.9.2
208  **/
209 hb_blob_t *
210 hb_blob_get_empty (void)
211 {
212   static const hb_blob_t _hb_blob_nil = {
213     HB_OBJECT_HEADER_STATIC,
214
215     true, /* immutable */
216
217     nullptr, /* data */
218     0, /* length */
219     HB_MEMORY_MODE_READONLY, /* mode */
220
221     nullptr, /* user_data */
222     nullptr  /* destroy */
223   };
224
225   return const_cast<hb_blob_t *> (&_hb_blob_nil);
226 }
227
228 /**
229  * hb_blob_reference: (skip)
230  * @blob: a blob.
231  *
232  * Increases the reference count on @blob.
233  *
234  * See TODO:link object types for more information.
235  *
236  * Return value: @blob.
237  *
238  * Since: 0.9.2
239  **/
240 hb_blob_t *
241 hb_blob_reference (hb_blob_t *blob)
242 {
243   return hb_object_reference (blob);
244 }
245
246 /**
247  * hb_blob_destroy: (skip)
248  * @blob: a blob.
249  *
250  * Decreases the reference count on @blob, and if it reaches zero, destroys
251  * @blob, freeing all memory, possibly calling the destroy-callback the blob
252  * was created for if it has not been called already.
253  *
254  * See TODO:link object types for more information.
255  *
256  * Since: 0.9.2
257  **/
258 void
259 hb_blob_destroy (hb_blob_t *blob)
260 {
261   if (!hb_object_destroy (blob)) return;
262
263   _hb_blob_destroy_user_data (blob);
264
265   free (blob);
266 }
267
268 /**
269  * hb_blob_set_user_data: (skip)
270  * @blob: a blob.
271  * @key: key for data to set.
272  * @data: data to set.
273  * @destroy: callback to call when @data is not needed anymore.
274  * @replace: whether to replace an existing data with the same key.
275  *
276  * Return value: 
277  *
278  * Since: 0.9.2
279  **/
280 hb_bool_t
281 hb_blob_set_user_data (hb_blob_t          *blob,
282                        hb_user_data_key_t *key,
283                        void *              data,
284                        hb_destroy_func_t   destroy,
285                        hb_bool_t           replace)
286 {
287   return hb_object_set_user_data (blob, key, data, destroy, replace);
288 }
289
290 /**
291  * hb_blob_get_user_data: (skip)
292  * @blob: a blob.
293  * @key: key for data to get.
294  *
295  * 
296  *
297  * Return value: (transfer none): 
298  *
299  * Since: 0.9.2
300  **/
301 void *
302 hb_blob_get_user_data (hb_blob_t          *blob,
303                        hb_user_data_key_t *key)
304 {
305   return hb_object_get_user_data (blob, key);
306 }
307
308
309 /**
310  * hb_blob_make_immutable:
311  * @blob: a blob.
312  *
313  * 
314  *
315  * Since: 0.9.2
316  **/
317 void
318 hb_blob_make_immutable (hb_blob_t *blob)
319 {
320   if (hb_object_is_inert (blob))
321     return;
322
323   blob->immutable = true;
324 }
325
326 /**
327  * hb_blob_is_immutable:
328  * @blob: a blob.
329  *
330  * 
331  *
332  * Return value: TODO
333  *
334  * Since: 0.9.2
335  **/
336 hb_bool_t
337 hb_blob_is_immutable (hb_blob_t *blob)
338 {
339   return blob->immutable;
340 }
341
342
343 /**
344  * hb_blob_get_length:
345  * @blob: a blob.
346  *
347  * 
348  *
349  * Return value: the length of blob data in bytes.
350  *
351  * Since: 0.9.2
352  **/
353 unsigned int
354 hb_blob_get_length (hb_blob_t *blob)
355 {
356   return blob->length;
357 }
358
359 /**
360  * hb_blob_get_data:
361  * @blob: a blob.
362  * @length: (out):
363  *
364  * 
365  *
366  * Returns: (transfer none) (array length=length): 
367  *
368  * Since: 0.9.2
369  **/
370 const char *
371 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
372 {
373   if (length)
374     *length = blob->length;
375
376   return blob->data;
377 }
378
379 /**
380  * hb_blob_get_data_writable:
381  * @blob: a blob.
382  * @length: (out): output length of the writable data.
383  *
384  * Tries to make blob data writable (possibly copying it) and
385  * return pointer to data.
386  *
387  * Fails if blob has been made immutable, or if memory allocation
388  * fails.
389  *
390  * Returns: (transfer none) (array length=length): Writable blob data,
391  * or %NULL if failed.
392  *
393  * Since: 0.9.2
394  **/
395 char *
396 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
397 {
398   if (!_try_writable (blob)) {
399     if (length)
400       *length = 0;
401
402     return nullptr;
403   }
404
405   if (length)
406     *length = blob->length;
407
408   return const_cast<char *> (blob->data);
409 }
410
411
412 static hb_bool_t
413 _try_make_writable_inplace_unix (hb_blob_t *blob)
414 {
415 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
416   uintptr_t pagesize = -1, mask, length;
417   const char *addr;
418
419 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
420   pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
421 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
422   pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
423 #elif defined(HAVE_GETPAGESIZE)
424   pagesize = (uintptr_t) getpagesize ();
425 #endif
426
427   if ((uintptr_t) -1L == pagesize) {
428     DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
429     return false;
430   }
431   DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
432
433   mask = ~(pagesize-1);
434   addr = (const char *) (((uintptr_t) blob->data) & mask);
435   length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask)  - addr;
436   DEBUG_MSG_FUNC (BLOB, blob,
437                   "calling mprotect on [%p..%p] (%lu bytes)",
438                   addr, addr+length, (unsigned long) length);
439   if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
440     DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
441     return false;
442   }
443
444   blob->mode = HB_MEMORY_MODE_WRITABLE;
445
446   DEBUG_MSG_FUNC (BLOB, blob,
447                   "successfully made [%p..%p] (%lu bytes) writable\n",
448                   addr, addr+length, (unsigned long) length);
449   return true;
450 #else
451   return false;
452 #endif
453 }
454
455 static bool
456 _try_writable_inplace (hb_blob_t *blob)
457 {
458   DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
459
460   if (_try_make_writable_inplace_unix (blob))
461     return true;
462
463   DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
464
465   /* Failed to make writable inplace, mark that */
466   blob->mode = HB_MEMORY_MODE_READONLY;
467   return false;
468 }
469
470 static bool
471 _try_writable (hb_blob_t *blob)
472 {
473   if (blob->immutable)
474     return false;
475
476   if (blob->mode == HB_MEMORY_MODE_WRITABLE)
477     return true;
478
479   if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
480     return true;
481
482   if (blob->mode == HB_MEMORY_MODE_WRITABLE)
483     return true;
484
485
486   DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
487
488   char *new_data;
489
490   new_data = (char *) malloc (blob->length);
491   if (unlikely (!new_data))
492     return false;
493
494   DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
495
496   memcpy (new_data, blob->data, blob->length);
497   _hb_blob_destroy_user_data (blob);
498   blob->mode = HB_MEMORY_MODE_WRITABLE;
499   blob->data = new_data;
500   blob->user_data = new_data;
501   blob->destroy = free;
502
503   return true;
504 }