2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2018 Ebrahim Byagowi
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
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
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
43 #ifdef HAVE_SYS_MMAN_H
46 #endif /* HAVE_UNISTD_H */
48 #endif /* HAVE_SYS_MMAN_H */
57 * @short_description: Binary data containers
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.
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.
75 * Creates a new "blob" object wrapping @data. The @mode parameter is used
76 * to negotiate ownership and lifecycle of @data.
78 * Return value: New blob, or the empty blob if something failed or if @length is
79 * zero. Destroy with hb_blob_destroy().
84 hb_blob_create (const char *data,
86 hb_memory_mode_t mode,
88 hb_destroy_func_t destroy)
94 !(blob = hb_object_create<hb_blob_t> ())) {
97 return hb_blob_get_empty ();
101 blob->length = length;
104 blob->user_data = user_data;
105 blob->destroy = destroy;
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 ();
119 _hb_blob_destroy (void *data)
121 hb_blob_destroy ((hb_blob_t *) data);
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.
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
136 * Makes @parent immutable.
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().
145 hb_blob_create_sub_blob (hb_blob_t *parent,
151 if (!length || !parent || offset >= parent->length)
152 return hb_blob_get_empty ();
154 hb_blob_make_immutable (parent);
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),
166 * hb_blob_copy_writable_or_fail:
169 * Makes a writable copy of @blob.
171 * Return value: New blob, or nullptr if allocation failed.
176 hb_blob_copy_writable_or_fail (hb_blob_t *blob)
178 blob = hb_blob_create (blob->data,
180 HB_MEMORY_MODE_DUPLICATE,
184 if (unlikely (blob == hb_blob_get_empty ()))
193 * Returns the singleton empty blob.
195 * See TODO:link object types for more information.
197 * Return value: (transfer full): the empty blob.
204 return const_cast<hb_blob_t *> (&Null(hb_blob_t));
208 * hb_blob_reference: (skip)
211 * Increases the reference count on @blob.
213 * See TODO:link object types for more information.
215 * Return value: @blob.
220 hb_blob_reference (hb_blob_t *blob)
222 return hb_object_reference (blob);
226 * hb_blob_destroy: (skip)
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.
233 * See TODO:link object types for more information.
238 hb_blob_destroy (hb_blob_t *blob)
240 if (!hb_object_destroy (blob)) return;
242 blob->fini_shallow ();
248 * hb_blob_set_user_data: (skip)
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.
260 hb_blob_set_user_data (hb_blob_t *blob,
261 hb_user_data_key_t *key,
263 hb_destroy_func_t destroy,
266 return hb_object_set_user_data (blob, key, data, destroy, replace);
270 * hb_blob_get_user_data: (skip)
272 * @key: key for data to get.
276 * Return value: (transfer none):
281 hb_blob_get_user_data (hb_blob_t *blob,
282 hb_user_data_key_t *key)
284 return hb_object_get_user_data (blob, key);
289 * hb_blob_make_immutable:
297 hb_blob_make_immutable (hb_blob_t *blob)
299 if (hb_object_is_immutable (blob))
302 hb_object_make_immutable (blob);
306 * hb_blob_is_immutable:
316 hb_blob_is_immutable (hb_blob_t *blob)
318 return hb_object_is_immutable (blob);
323 * hb_blob_get_length:
328 * Return value: the length of blob data in bytes.
333 hb_blob_get_length (hb_blob_t *blob)
345 * Returns: (transfer none) (array length=length):
350 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
353 *length = blob->length;
359 * hb_blob_get_data_writable:
361 * @length: (out): output length of the writable data.
363 * Tries to make blob data writable (possibly copying it) and
364 * return pointer to data.
366 * Fails if blob has been made immutable, or if memory allocation
369 * Returns: (transfer none) (array length=length): Writable blob data,
370 * or %NULL if failed.
375 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
377 if (!blob->try_make_writable ()) {
385 *length = blob->length;
387 return const_cast<char *> (blob->data);
392 hb_blob_t::try_make_writable_inplace_unix ()
394 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
395 uintptr_t pagesize = -1, mask, length;
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 ();
406 if ((uintptr_t) -1L == pagesize) {
407 DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
410 DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
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));
423 this->mode = HB_MEMORY_MODE_WRITABLE;
425 DEBUG_MSG_FUNC (BLOB, this,
426 "successfully made [%p..%p] (%lu bytes) writable\n",
427 addr, addr+length, (unsigned long) length);
435 hb_blob_t::try_make_writable_inplace ()
437 DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
439 if (this->try_make_writable_inplace_unix ())
442 DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
444 /* Failed to make writable inplace, mark that */
445 this->mode = HB_MEMORY_MODE_READONLY;
450 hb_blob_t::try_make_writable ()
452 if (hb_object_is_immutable (this))
455 if (this->mode == HB_MEMORY_MODE_WRITABLE)
458 if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
461 if (this->mode == HB_MEMORY_MODE_WRITABLE)
465 DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
469 new_data = (char *) malloc (this->length);
470 if (unlikely (!new_data))
473 DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
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;
491 # include <sys/types.h>
492 # include <sys/stat.h>
497 # include <windows.h>
504 #ifndef MAP_NORESERVE
505 # define MAP_NORESERVE 0
508 struct hb_mapped_file_t
511 unsigned long length;
517 #if (defined(HAVE_MMAP) || defined(_WIN32)) && !defined(HB_NO_MMAP)
519 _hb_mapped_file_destroy (void *file_)
521 hb_mapped_file_t *file = (hb_mapped_file_t *) file_;
523 munmap (file->contents, file->length);
524 #elif defined(_WIN32)
525 UnmapViewOfFile (file->contents);
526 CloseHandle (file->mapping);
528 assert (0); // If we don't have mmap we shouldn't reach here
536 * hb_blob_create_from_file:
537 * @file_name: font filename.
539 * Returns: A hb_blob_t pointer with the content of the file
544 hb_blob_create_from_file (const char *file_name)
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 ();
552 int fd = open (file_name, O_RDONLY | O_BINARY, 0);
553 if (unlikely (fd == -1)) goto fail_without_close;
556 if (unlikely (fstat (fd, &st) == -1)) goto fail;
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);
562 if (unlikely (file->contents == MAP_FAILED)) goto fail;
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);
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 ();
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)
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);
597 fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
598 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
601 free (wchar_file_name);
603 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
605 #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
607 LARGE_INTEGER length;
608 GetFileSizeEx (fd, &length);
609 file->length = length.LowPart;
610 file->mapping = CreateFileMappingFromApp (fd, nullptr, PAGE_READONLY, length.QuadPart, nullptr);
613 file->length = (unsigned long) GetFileSize (fd, nullptr);
614 file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
616 if (unlikely (file->mapping == nullptr)) goto fail;
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);
621 file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
623 if (unlikely (file->contents == nullptr)) goto fail;
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);
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 ();
643 FILE *fp = fopen (file_name, "rb");
644 if (unlikely (fp == nullptr)) goto fread_fail_without_close;
648 if (allocated - len < BUFSIZ)
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;
659 unsigned long addition = fread (data + len, 1, allocated - len, fp);
661 int err = ferror (fp);
662 #ifdef EINTR // armcc doesn't have it
663 if (unlikely (err == EINTR)) continue;
665 if (unlikely (err)) goto fread_fail;
670 return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
671 (hb_destroy_func_t) free);
675 fread_fail_without_close:
677 return hb_blob_get_empty ();
679 #endif /* !HB_NO_OPEN */