2e72683c8b99e75e372c5091b26500644f57931b
[platform/upstream/harfbuzz.git] / src / hb-blob.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2018  Ebrahim Byagowi
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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
28
29 /* https://github.com/harfbuzz/harfbuzz/issues/1308
30  * http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
31  * https://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html
32  */
33 #if !defined(_POSIX_C_SOURCE) && !defined(_MSC_VER) && !defined(__NetBSD__)
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wunused-macros"
36 #define _POSIX_C_SOURCE 200809L
37 #pragma GCC diagnostic pop
38 #endif
39
40 #include "hb.hh"
41 #include "hb-blob.hh"
42
43 #ifdef HAVE_SYS_MMAN_H
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif /* HAVE_UNISTD_H */
47 #include <sys/mman.h>
48 #endif /* HAVE_SYS_MMAN_H */
49
50 #include <stdio.h>
51 #include <stdlib.h>
52
53
54 /**
55  * SECTION: hb-blob
56  * @title: hb-blob
57  * @short_description: Binary data containers
58  * @include: hb.h
59  *
60  * Blobs wrap a chunk of binary data to handle lifecycle management of data
61  * while it is passed between client and HarfBuzz.  Blobs are primarily used
62  * to create font faces, but also to access font face tables, as well as
63  * pass around other binary data.
64  **/
65
66
67 /**
68  * hb_blob_create: (skip)
69  * @data: Pointer to blob data.
70  * @length: Length of @data in bytes.
71  * @mode: Memory mode for @data.
72  * @user_data: Data parameter to pass to @destroy.
73  * @destroy: Callback to call when @data is not needed anymore.
74  *
75  * Creates a new "blob" object wrapping @data.  The @mode parameter is used
76  * to negotiate ownership and lifecycle of @data.
77  *
78  * Return value: New blob, or the empty blob if something failed or if @length is
79  * zero.  Destroy with hb_blob_destroy().
80  *
81  * Since: 0.9.2
82  **/
83 hb_blob_t *
84 hb_blob_create (const char        *data,
85                 unsigned int       length,
86                 hb_memory_mode_t   mode,
87                 void              *user_data,
88                 hb_destroy_func_t  destroy)
89 {
90   hb_blob_t *blob;
91
92   if (!length ||
93       length >= 1u << 31 ||
94       !(blob = hb_object_create<hb_blob_t> ())) {
95     if (destroy)
96       destroy (user_data);
97     return hb_blob_get_empty ();
98   }
99
100   blob->data = data;
101   blob->length = length;
102   blob->mode = mode;
103
104   blob->user_data = user_data;
105   blob->destroy = destroy;
106
107   if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
108     blob->mode = HB_MEMORY_MODE_READONLY;
109     if (!blob->try_make_writable ()) {
110       hb_blob_destroy (blob);
111       return hb_blob_get_empty ();
112     }
113   }
114
115   return blob;
116 }
117
118 static void
119 _hb_blob_destroy (void *data)
120 {
121   hb_blob_destroy ((hb_blob_t *) data);
122 }
123
124 /**
125  * hb_blob_create_sub_blob:
126  * @parent: Parent blob.
127  * @offset: Start offset of sub-blob within @parent, in bytes.
128  * @length: Length of sub-blob.
129  *
130  * Returns a blob that represents a range of bytes in @parent.  The new
131  * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
132  * will never modify data in the parent blob.  The parent data is not
133  * expected to be modified, and will result in undefined behavior if it
134  * is.
135  *
136  * Makes @parent immutable.
137  *
138  * Return value: New blob, or the empty blob if something failed or if
139  * @length is zero or @offset is beyond the end of @parent's data.  Destroy
140  * with hb_blob_destroy().
141  *
142  * Since: 0.9.2
143  **/
144 hb_blob_t *
145 hb_blob_create_sub_blob (hb_blob_t    *parent,
146                          unsigned int  offset,
147                          unsigned int  length)
148 {
149   hb_blob_t *blob;
150
151   if (!length || !parent || offset >= parent->length)
152     return hb_blob_get_empty ();
153
154   hb_blob_make_immutable (parent);
155
156   blob = hb_blob_create (parent->data + offset,
157                          hb_min (length, parent->length - offset),
158                          HB_MEMORY_MODE_READONLY,
159                          hb_blob_reference (parent),
160                          _hb_blob_destroy);
161
162   return blob;
163 }
164
165 /**
166  * hb_blob_copy_writable_or_fail:
167  * @blob: A blob.
168  *
169  * Makes a writable copy of @blob.
170  *
171  * Return value: New blob, or nullptr if allocation failed.
172  *
173  * Since: 1.8.0
174  **/
175 hb_blob_t *
176 hb_blob_copy_writable_or_fail (hb_blob_t *blob)
177 {
178   blob = hb_blob_create (blob->data,
179                          blob->length,
180                          HB_MEMORY_MODE_DUPLICATE,
181                          nullptr,
182                          nullptr);
183
184   if (unlikely (blob == hb_blob_get_empty ()))
185     blob = nullptr;
186
187   return blob;
188 }
189
190 /**
191  * hb_blob_get_empty:
192  *
193  * Returns the singleton empty blob.
194  *
195  * See TODO:link object types for more information.
196  *
197  * Return value: (transfer full): the empty blob.
198  *
199  * Since: 0.9.2
200  **/
201 hb_blob_t *
202 hb_blob_get_empty ()
203 {
204   return const_cast<hb_blob_t *> (&Null(hb_blob_t));
205 }
206
207 /**
208  * hb_blob_reference: (skip)
209  * @blob: a blob.
210  *
211  * Increases the reference count on @blob.
212  *
213  * See TODO:link object types for more information.
214  *
215  * Return value: @blob.
216  *
217  * Since: 0.9.2
218  **/
219 hb_blob_t *
220 hb_blob_reference (hb_blob_t *blob)
221 {
222   return hb_object_reference (blob);
223 }
224
225 /**
226  * hb_blob_destroy: (skip)
227  * @blob: a blob.
228  *
229  * Decreases the reference count on @blob, and if it reaches zero, destroys
230  * @blob, freeing all memory, possibly calling the destroy-callback the blob
231  * was created for if it has not been called already.
232  *
233  * See TODO:link object types for more information.
234  *
235  * Since: 0.9.2
236  **/
237 void
238 hb_blob_destroy (hb_blob_t *blob)
239 {
240   if (!hb_object_destroy (blob)) return;
241
242   blob->fini_shallow ();
243
244   free (blob);
245 }
246
247 /**
248  * hb_blob_set_user_data: (skip)
249  * @blob: a blob.
250  * @key: key for data to set.
251  * @data: data to set.
252  * @destroy: callback to call when @data is not needed anymore.
253  * @replace: whether to replace an existing data with the same key.
254  *
255  * Return value:
256  *
257  * Since: 0.9.2
258  **/
259 hb_bool_t
260 hb_blob_set_user_data (hb_blob_t          *blob,
261                        hb_user_data_key_t *key,
262                        void *              data,
263                        hb_destroy_func_t   destroy,
264                        hb_bool_t           replace)
265 {
266   return hb_object_set_user_data (blob, key, data, destroy, replace);
267 }
268
269 /**
270  * hb_blob_get_user_data: (skip)
271  * @blob: a blob.
272  * @key: key for data to get.
273  *
274  *
275  *
276  * Return value: (transfer none):
277  *
278  * Since: 0.9.2
279  **/
280 void *
281 hb_blob_get_user_data (hb_blob_t          *blob,
282                        hb_user_data_key_t *key)
283 {
284   return hb_object_get_user_data (blob, key);
285 }
286
287
288 /**
289  * hb_blob_make_immutable:
290  * @blob: a blob.
291  *
292  *
293  *
294  * Since: 0.9.2
295  **/
296 void
297 hb_blob_make_immutable (hb_blob_t *blob)
298 {
299   if (hb_object_is_immutable (blob))
300     return;
301
302   hb_object_make_immutable (blob);
303 }
304
305 /**
306  * hb_blob_is_immutable:
307  * @blob: a blob.
308  *
309  *
310  *
311  * Return value: TODO
312  *
313  * Since: 0.9.2
314  **/
315 hb_bool_t
316 hb_blob_is_immutable (hb_blob_t *blob)
317 {
318   return hb_object_is_immutable (blob);
319 }
320
321
322 /**
323  * hb_blob_get_length:
324  * @blob: a blob.
325  *
326  *
327  *
328  * Return value: the length of blob data in bytes.
329  *
330  * Since: 0.9.2
331  **/
332 unsigned int
333 hb_blob_get_length (hb_blob_t *blob)
334 {
335   return blob->length;
336 }
337
338 /**
339  * hb_blob_get_data:
340  * @blob: a blob.
341  * @length: (out):
342  *
343  *
344  *
345  * Returns: (transfer none) (array length=length):
346  *
347  * Since: 0.9.2
348  **/
349 const char *
350 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
351 {
352   if (length)
353     *length = blob->length;
354
355   return blob->data;
356 }
357
358 /**
359  * hb_blob_get_data_writable:
360  * @blob: a blob.
361  * @length: (out): output length of the writable data.
362  *
363  * Tries to make blob data writable (possibly copying it) and
364  * return pointer to data.
365  *
366  * Fails if blob has been made immutable, or if memory allocation
367  * fails.
368  *
369  * Returns: (transfer none) (array length=length): Writable blob data,
370  * or %NULL if failed.
371  *
372  * Since: 0.9.2
373  **/
374 char *
375 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
376 {
377   if (!blob->try_make_writable ()) {
378     if (length)
379       *length = 0;
380
381     return nullptr;
382   }
383
384   if (length)
385     *length = blob->length;
386
387   return const_cast<char *> (blob->data);
388 }
389
390
391 bool
392 hb_blob_t::try_make_writable_inplace_unix ()
393 {
394 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
395   uintptr_t pagesize = -1, mask, length;
396   const char *addr;
397
398 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
399   pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
400 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
401   pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
402 #elif defined(HAVE_GETPAGESIZE)
403   pagesize = (uintptr_t) getpagesize ();
404 #endif
405
406   if ((uintptr_t) -1L == pagesize) {
407     DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
408     return false;
409   }
410   DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
411
412   mask = ~(pagesize-1);
413   addr = (const char *) (((uintptr_t) this->data) & mask);
414   length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask)  - addr;
415   DEBUG_MSG_FUNC (BLOB, this,
416                   "calling mprotect on [%p..%p] (%lu bytes)",
417                   addr, addr+length, (unsigned long) length);
418   if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
419     DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno));
420     return false;
421   }
422
423   this->mode = HB_MEMORY_MODE_WRITABLE;
424
425   DEBUG_MSG_FUNC (BLOB, this,
426                   "successfully made [%p..%p] (%lu bytes) writable\n",
427                   addr, addr+length, (unsigned long) length);
428   return true;
429 #else
430   return false;
431 #endif
432 }
433
434 bool
435 hb_blob_t::try_make_writable_inplace ()
436 {
437   DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
438
439   if (this->try_make_writable_inplace_unix ())
440     return true;
441
442   DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
443
444   /* Failed to make writable inplace, mark that */
445   this->mode = HB_MEMORY_MODE_READONLY;
446   return false;
447 }
448
449 bool
450 hb_blob_t::try_make_writable ()
451 {
452   if (hb_object_is_immutable (this))
453     return false;
454
455   if (this->mode == HB_MEMORY_MODE_WRITABLE)
456     return true;
457
458   if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
459     return true;
460
461   if (this->mode == HB_MEMORY_MODE_WRITABLE)
462     return true;
463
464
465   DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
466
467   char *new_data;
468
469   new_data = (char *) malloc (this->length);
470   if (unlikely (!new_data))
471     return false;
472
473   DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
474
475   memcpy (new_data, this->data, this->length);
476   this->destroy_user_data ();
477   this->mode = HB_MEMORY_MODE_WRITABLE;
478   this->data = new_data;
479   this->user_data = new_data;
480   this->destroy = free;
481
482   return true;
483 }
484
485 /*
486  * Mmap
487  */
488
489 #ifndef HB_NO_OPEN
490 #ifdef HAVE_MMAP
491 # include <sys/types.h>
492 # include <sys/stat.h>
493 # include <fcntl.h>
494 #endif
495
496 #ifdef _WIN32
497 # include <windows.h>
498 #else
499 # ifndef O_BINARY
500 #  define O_BINARY 0
501 # endif
502 #endif
503
504 #ifndef MAP_NORESERVE
505 # define MAP_NORESERVE 0
506 #endif
507
508 struct hb_mapped_file_t
509 {
510   char *contents;
511   unsigned long length;
512 #ifdef _WIN32
513   HANDLE mapping;
514 #endif
515 };
516
517 #if (defined(HAVE_MMAP) || defined(_WIN32)) && !defined(HB_NO_MMAP)
518 static void
519 _hb_mapped_file_destroy (void *file_)
520 {
521   hb_mapped_file_t *file = (hb_mapped_file_t *) file_;
522 #ifdef HAVE_MMAP
523   munmap (file->contents, file->length);
524 #elif defined(_WIN32)
525   UnmapViewOfFile (file->contents);
526   CloseHandle (file->mapping);
527 #else
528   assert (0); // If we don't have mmap we shouldn't reach here
529 #endif
530
531   free (file);
532 }
533 #endif
534
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  *
541  * Since: 1.7.7
542  **/
543 hb_blob_t *
544 hb_blob_create_from_file (const char *file_name)
545 {
546   /* 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)
549   hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
550   if (unlikely (!file)) return hb_blob_get_empty ();
551
552   int fd = open (file_name, O_RDONLY | O_BINARY, 0);
553   if (unlikely (fd == -1)) goto fail_without_close;
554
555   struct stat st;
556   if (unlikely (fstat (fd, &st) == -1)) goto fail;
557
558   file->length = (unsigned long) st.st_size;
559   file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
560                                   MAP_PRIVATE | MAP_NORESERVE, fd, 0);
561
562   if (unlikely (file->contents == MAP_FAILED)) goto fail;
563
564   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
570 fail:
571   close (fd);
572 fail_without_close:
573   free (file);
574
575 #elif defined(_WIN32) && !defined(HB_NO_MMAP)
576   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
579   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);
584 #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
597   fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
598                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
599                     nullptr);
600 #endif
601   free (wchar_file_name);
602
603   if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
604
605 #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
613   file->length = (unsigned long) GetFileSize (fd, nullptr);
614   file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
615 #endif
616   if (unlikely (file->mapping == nullptr)) goto fail;
617
618 #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
621   file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
622 #endif
623   if (unlikely (file->contents == nullptr)) goto fail;
624
625   CloseHandle (fd);
626   return hb_blob_create (file->contents, file->length,
627                          HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
628                          (hb_destroy_func_t) _hb_mapped_file_destroy);
629
630 fail:
631   CloseHandle (fd);
632 fail_without_close:
633   free (file);
634
635 #endif
636
637   /* 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 */
639   unsigned long len = 0, allocated = BUFSIZ * 16;
640   char *data = (char *) malloc (allocated);
641   if (unlikely (data == nullptr)) return hb_blob_get_empty ();
642
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;
651       /* Don't allocate and go more than ~536MB, our mmap reader still
652          can cover files like that but lets limit our fallback reader */
653       if (unlikely (allocated > (2 << 28))) goto fread_fail;
654       char *new_data = (char *) realloc (data, allocated);
655       if (unlikely (new_data == nullptr)) goto fread_fail;
656       data = new_data;
657     }
658
659     unsigned long addition = fread (data + len, 1, allocated - len, fp);
660
661     int err = ferror (fp);
662 #ifdef EINTR // armcc doesn't have it
663     if (unlikely (err == EINTR)) continue;
664 #endif
665     if (unlikely (err)) goto fread_fail;
666
667     len += addition;
668   }
669
670   return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
671                          (hb_destroy_func_t) free);
672
673 fread_fail:
674   fclose (fp);
675 fread_fail_without_close:
676   free (data);
677   return hb_blob_get_empty ();
678 }
679 #endif /* !HB_NO_OPEN */