2d0c016b347d71340313cb6c8c12045ef051df0b
[framework/uifw/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 #include "hb-private.hh"
28
29 #include "hb-blob.h"
30 #include "hb-object-private.hh"
31
32 #ifdef HAVE_SYS_MMAN_H
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif /* HAVE_UNISTD_H */
36 #include <sys/mman.h>
37 #endif /* HAVE_SYS_MMAN_H */
38
39 #include <stdio.h>
40 #include <errno.h>
41
42 HB_BEGIN_DECLS
43
44
45 #ifndef HB_DEBUG_BLOB
46 #define HB_DEBUG_BLOB (HB_DEBUG+0)
47 #endif
48
49
50 struct _hb_blob_t {
51   hb_object_header_t header;
52
53   bool immutable;
54
55   const char *data;
56   unsigned int length;
57   hb_memory_mode_t mode;
58
59   void *user_data;
60   hb_destroy_func_t destroy;
61 };
62
63 static hb_blob_t _hb_blob_nil = {
64   HB_OBJECT_HEADER_STATIC,
65
66   TRUE, /* immutable */
67
68   0, /* length */
69   NULL, /* data */
70   HB_MEMORY_MODE_READONLY, /* mode */
71
72   NULL, /* user_data */
73   NULL  /* destroy */
74 };
75
76
77 static bool _try_writable (hb_blob_t *blob);
78
79 static void
80 _hb_blob_destroy_user_data (hb_blob_t *blob)
81 {
82   if (blob->destroy) {
83     blob->destroy (blob->user_data);
84     blob->user_data = NULL;
85     blob->destroy = NULL;
86   }
87 }
88
89 hb_blob_t *
90 hb_blob_create (const char        *data,
91                 unsigned int       length,
92                 hb_memory_mode_t   mode,
93                 void              *user_data,
94                 hb_destroy_func_t  destroy)
95 {
96   hb_blob_t *blob;
97
98   if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
99     if (destroy)
100       destroy (user_data);
101     return &_hb_blob_nil;
102   }
103
104   blob->data = data;
105   blob->length = length;
106   blob->mode = mode;
107
108   blob->user_data = user_data;
109   blob->destroy = destroy;
110
111   if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
112     blob->mode = HB_MEMORY_MODE_READONLY;
113     if (!_try_writable (blob)) {
114       hb_blob_destroy (blob);
115       return &_hb_blob_nil;
116     }
117   }
118
119   return blob;
120 }
121
122 hb_blob_t *
123 hb_blob_create_sub_blob (hb_blob_t    *parent,
124                          unsigned int  offset,
125                          unsigned int  length)
126 {
127   hb_blob_t *blob;
128
129   if (!length || offset >= parent->length)
130     return &_hb_blob_nil;
131
132   hb_blob_make_immutable (parent);
133
134   blob = hb_blob_create (parent->data + offset,
135                          MIN (length, parent->length - offset),
136                          parent->mode,
137                          hb_blob_reference (parent),
138                          (hb_destroy_func_t) hb_blob_destroy);
139
140   return blob;
141 }
142
143 hb_blob_t *
144 hb_blob_get_empty (void)
145 {
146   return &_hb_blob_nil;
147 }
148
149 hb_blob_t *
150 hb_blob_reference (hb_blob_t *blob)
151 {
152   return hb_object_reference (blob);
153 }
154
155 void
156 hb_blob_destroy (hb_blob_t *blob)
157 {
158   if (!hb_object_destroy (blob)) return;
159
160   _hb_blob_destroy_user_data (blob);
161
162   free (blob);
163 }
164
165 hb_bool_t
166 hb_blob_set_user_data (hb_blob_t          *blob,
167                        hb_user_data_key_t *key,
168                        void *              data,
169                        hb_destroy_func_t   destroy)
170 {
171   return hb_object_set_user_data (blob, key, data, destroy);
172 }
173
174 void *
175 hb_blob_get_user_data (hb_blob_t          *blob,
176                        hb_user_data_key_t *key)
177 {
178   return hb_object_get_user_data (blob, key);
179 }
180
181
182 void
183 hb_blob_make_immutable (hb_blob_t *blob)
184 {
185   if (hb_object_is_inert (blob))
186     return;
187
188   blob->immutable = TRUE;
189 }
190
191 hb_bool_t
192 hb_blob_is_immutable (hb_blob_t *blob)
193 {
194   return blob->immutable;
195 }
196
197
198 unsigned int
199 hb_blob_get_length (hb_blob_t *blob)
200 {
201   return blob->length;
202 }
203
204 const char *
205 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
206 {
207   if (length)
208     *length = blob->length;
209
210   return blob->data;
211 }
212
213 char *
214 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
215 {
216   if (!_try_writable (blob)) {
217     if (length)
218       *length = 0;
219
220     return NULL;
221   }
222
223   if (length)
224     *length = blob->length;
225
226   return const_cast<char *> (blob->data);
227 }
228
229
230 static hb_bool_t
231 _try_make_writable_inplace_unix (hb_blob_t *blob)
232 {
233 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
234   uintptr_t pagesize = -1, mask, length;
235   const char *addr;
236
237 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
238   pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
239 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
240   pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
241 #elif defined(HAVE_GETPAGESIZE)
242   pagesize = (uintptr_t) getpagesize ();
243 #endif
244
245   if ((uintptr_t) -1L == pagesize) {
246     DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
247     return FALSE;
248   }
249   DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
250
251   mask = ~(pagesize-1);
252   addr = (const char *) (((uintptr_t) blob->data) & mask);
253   length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask)  - addr;
254   DEBUG_MSG_FUNC (BLOB, blob,
255                   "calling mprotect on [%p..%p] (%lu bytes)",
256                   addr, addr+length, (unsigned long) length);
257   if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
258     DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
259     return FALSE;
260   }
261
262   blob->mode = HB_MEMORY_MODE_WRITABLE;
263
264   DEBUG_MSG_FUNC (BLOB, blob,
265                   "successfully made [%p..%p] (%lu bytes) writable\n",
266                   addr, addr+length, (unsigned long) length);
267   return TRUE;
268 #else
269   return FALSE;
270 #endif
271 }
272
273 static bool
274 _try_writable_inplace (hb_blob_t *blob)
275 {
276   DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
277
278   if (_try_make_writable_inplace_unix (blob))
279     return TRUE;
280
281   DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
282
283   /* Failed to make writable inplace, mark that */
284   blob->mode = HB_MEMORY_MODE_READONLY;
285   return FALSE;
286 }
287
288 static bool
289 _try_writable (hb_blob_t *blob)
290 {
291   if (blob->immutable)
292     return FALSE;
293
294   if (blob->mode == HB_MEMORY_MODE_WRITABLE)
295     return TRUE;
296
297   if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
298     return TRUE;
299
300   if (blob->mode == HB_MEMORY_MODE_WRITABLE)
301     return TRUE;
302
303
304   DEBUG_MSG_FUNC (BLOB, blob, "currect data is -> %p\n", blob->data);
305
306   char *new_data;
307
308   new_data = (char *) malloc (blob->length);
309   if (unlikely (!new_data))
310     return FALSE;
311
312   DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
313
314   memcpy (new_data, blob->data, blob->length);
315   _hb_blob_destroy_user_data (blob);
316   blob->mode = HB_MEMORY_MODE_WRITABLE;
317   blob->data = new_data;
318   blob->user_data = new_data;
319   blob->destroy = free;
320
321   return TRUE;
322 }
323
324
325 HB_END_DECLS