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
31 #ifdef HAVE_SYS_MMAN_H
34 #endif /* HAVE_UNISTD_H */
36 #endif /* HAVE_SYS_MMAN_H */
45 * @short_description: Binary data containers
48 * Blobs wrap a chunk of binary data to handle lifecycle management of data
49 * while it is passed between client and HarfBuzz. Blobs are primarily used
50 * to create font faces, but also to access font face tables, as well as
51 * pass around other binary data.
56 * hb_blob_create: (skip)
57 * @data: Pointer to blob data.
58 * @length: Length of @data in bytes.
59 * @mode: Memory mode for @data.
60 * @user_data: Data parameter to pass to @destroy.
61 * @destroy: Callback to call when @data is not needed anymore.
63 * Creates a new "blob" object wrapping @data. The @mode parameter is used
64 * to negotiate ownership and lifecycle of @data.
66 * Return value: New blob, or the empty blob if something failed or if @length is
67 * zero. Destroy with hb_blob_destroy().
72 hb_blob_create (const char *data,
74 hb_memory_mode_t mode,
76 hb_destroy_func_t destroy)
82 !(blob = hb_object_create<hb_blob_t> ())) {
85 return hb_blob_get_empty ();
89 blob->length = length;
92 blob->user_data = user_data;
93 blob->destroy = destroy;
95 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
96 blob->mode = HB_MEMORY_MODE_READONLY;
97 if (!blob->try_make_writable ()) {
98 hb_blob_destroy (blob);
99 return hb_blob_get_empty ();
107 _hb_blob_destroy (void *data)
109 hb_blob_destroy ((hb_blob_t *) data);
113 * hb_blob_create_sub_blob:
114 * @parent: Parent blob.
115 * @offset: Start offset of sub-blob within @parent, in bytes.
116 * @length: Length of sub-blob.
118 * Returns a blob that represents a range of bytes in @parent. The new
119 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
120 * will never modify data in the parent blob. The parent data is not
121 * expected to be modified, and will result in undefined behavior if it
124 * Makes @parent immutable.
126 * Return value: New blob, or the empty blob if something failed or if
127 * @length is zero or @offset is beyond the end of @parent's data. Destroy
128 * with hb_blob_destroy().
133 hb_blob_create_sub_blob (hb_blob_t *parent,
139 if (!length || !parent || offset >= parent->length)
140 return hb_blob_get_empty ();
142 hb_blob_make_immutable (parent);
144 blob = hb_blob_create (parent->data + offset,
145 hb_min (length, parent->length - offset),
146 HB_MEMORY_MODE_READONLY,
147 hb_blob_reference (parent),
154 * hb_blob_copy_writable_or_fail:
157 * Makes a writable copy of @blob.
159 * Return value: New blob, or nullptr if allocation failed.
164 hb_blob_copy_writable_or_fail (hb_blob_t *blob)
166 blob = hb_blob_create (blob->data,
168 HB_MEMORY_MODE_DUPLICATE,
172 if (unlikely (blob == hb_blob_get_empty ()))
181 * Returns the singleton empty blob.
183 * See TODO:link object types for more information.
185 * Return value: (transfer full): the empty blob.
192 return const_cast<hb_blob_t *> (&Null (hb_blob_t));
196 * hb_blob_reference: (skip)
199 * Increases the reference count on @blob.
201 * See TODO:link object types for more information.
203 * Return value: @blob.
208 hb_blob_reference (hb_blob_t *blob)
210 return hb_object_reference (blob);
214 * hb_blob_destroy: (skip)
217 * Decreases the reference count on @blob, and if it reaches zero, destroys
218 * @blob, freeing all memory, possibly calling the destroy-callback the blob
219 * was created for if it has not been called already.
221 * See TODO:link object types for more information.
226 hb_blob_destroy (hb_blob_t *blob)
228 if (!hb_object_destroy (blob)) return;
230 blob->fini_shallow ();
236 * hb_blob_set_user_data: (skip)
238 * @key: key for data to set.
239 * @data: data to set.
240 * @destroy: callback to call when @data is not needed anymore.
241 * @replace: whether to replace an existing data with the same key.
248 hb_blob_set_user_data (hb_blob_t *blob,
249 hb_user_data_key_t *key,
251 hb_destroy_func_t destroy,
254 return hb_object_set_user_data (blob, key, data, destroy, replace);
258 * hb_blob_get_user_data: (skip)
260 * @key: key for data to get.
264 * Return value: (transfer none):
269 hb_blob_get_user_data (hb_blob_t *blob,
270 hb_user_data_key_t *key)
272 return hb_object_get_user_data (blob, key);
277 * hb_blob_make_immutable:
285 hb_blob_make_immutable (hb_blob_t *blob)
287 if (hb_object_is_immutable (blob))
290 hb_object_make_immutable (blob);
294 * hb_blob_is_immutable:
304 hb_blob_is_immutable (hb_blob_t *blob)
306 return hb_object_is_immutable (blob);
311 * hb_blob_get_length:
316 * Return value: the length of blob data in bytes.
321 hb_blob_get_length (hb_blob_t *blob)
333 * Returns: (transfer none) (array length=length):
338 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
341 *length = blob->length;
347 * hb_blob_get_data_writable:
349 * @length: (out): output length of the writable data.
351 * Tries to make blob data writable (possibly copying it) and
352 * return pointer to data.
354 * Fails if blob has been made immutable, or if memory allocation
357 * Returns: (transfer none) (array length=length): Writable blob data,
358 * or %NULL if failed.
363 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
365 if (!blob->try_make_writable ()) {
373 *length = blob->length;
375 return const_cast<char *> (blob->data);
380 hb_blob_t::try_make_writable_inplace_unix ()
382 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
383 uintptr_t pagesize = -1, mask, length;
386 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
387 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
388 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
389 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
390 #elif defined(HAVE_GETPAGESIZE)
391 pagesize = (uintptr_t) getpagesize ();
394 if ((uintptr_t) -1L == pagesize) {
395 DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
398 DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
400 mask = ~(pagesize-1);
401 addr = (const char *) (((uintptr_t) this->data) & mask);
402 length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask) - addr;
403 DEBUG_MSG_FUNC (BLOB, this,
404 "calling mprotect on [%p..%p] (%lu bytes)",
405 addr, addr+length, (unsigned long) length);
406 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
407 DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno));
411 this->mode = HB_MEMORY_MODE_WRITABLE;
413 DEBUG_MSG_FUNC (BLOB, this,
414 "successfully made [%p..%p] (%lu bytes) writable\n",
415 addr, addr+length, (unsigned long) length);
423 hb_blob_t::try_make_writable_inplace ()
425 DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
427 if (this->try_make_writable_inplace_unix ())
430 DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
432 /* Failed to make writable inplace, mark that */
433 this->mode = HB_MEMORY_MODE_READONLY;
438 hb_blob_t::try_make_writable ()
440 if (hb_object_is_immutable (this))
443 if (this->mode == HB_MEMORY_MODE_WRITABLE)
446 if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
449 if (this->mode == HB_MEMORY_MODE_WRITABLE)
453 DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
457 new_data = (char *) malloc (this->length);
458 if (unlikely (!new_data))
461 DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
463 memcpy (new_data, this->data, this->length);
464 this->destroy_user_data ();
465 this->mode = HB_MEMORY_MODE_WRITABLE;
466 this->data = new_data;
467 this->user_data = new_data;
468 this->destroy = free;
479 # if !defined(HB_NO_RESOURCE_FORK) && defined(__APPLE__)
480 # include <sys/paths.h>
482 # include <sys/types.h>
483 # include <sys/stat.h>
488 # include <windows.h>
495 #ifndef MAP_NORESERVE
496 # define MAP_NORESERVE 0
499 struct hb_mapped_file_t
502 unsigned long length;
508 #if (defined(HAVE_MMAP) || defined(_WIN32)) && !defined(HB_NO_MMAP)
510 _hb_mapped_file_destroy (void *file_)
512 hb_mapped_file_t *file = (hb_mapped_file_t *) file_;
514 munmap (file->contents, file->length);
515 #elif defined(_WIN32)
516 UnmapViewOfFile (file->contents);
517 CloseHandle (file->mapping);
519 assert (0); // If we don't have mmap we shouldn't reach here
526 #ifdef _PATH_RSRCFORKSPEC
528 _open_resource_fork (const char *file_name, hb_mapped_file_t *file)
530 size_t name_len = strlen (file_name);
531 size_t len = name_len + sizeof (_PATH_RSRCFORKSPEC);
533 char *rsrc_name = (char *) malloc (len);
534 if (unlikely (!rsrc_name)) return -1;
536 strncpy (rsrc_name, file_name, name_len);
537 strncpy (rsrc_name + name_len, _PATH_RSRCFORKSPEC,
538 sizeof (_PATH_RSRCFORKSPEC) - 1);
540 int fd = open (rsrc_name, O_RDONLY | O_BINARY, 0);
546 if (fstat (fd, &st) != -1)
547 file->length = (unsigned long) st.st_size;
560 * hb_blob_create_from_file:
561 * @file_name: font filename.
563 * Returns: A hb_blob_t pointer with the content of the file
568 hb_blob_create_from_file (const char *file_name)
570 /* Adopted from glib's gmappedfile.c with Matthias Clasen and
571 Allison Lortie permission but changed a lot to suit our need. */
572 #if defined(HAVE_MMAP) && !defined(HB_NO_MMAP)
573 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
574 if (unlikely (!file)) return hb_blob_get_empty ();
576 int fd = open (file_name, O_RDONLY | O_BINARY, 0);
577 if (unlikely (fd == -1)) goto fail_without_close;
580 if (unlikely (fstat (fd, &st) == -1)) goto fail;
582 file->length = (unsigned long) st.st_size;
584 #ifdef _PATH_RSRCFORKSPEC
585 if (unlikely (file->length == 0))
587 int rfd = _open_resource_fork (file_name, file);
596 file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
597 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
599 if (unlikely (file->contents == MAP_FAILED)) goto fail;
603 return hb_blob_create (file->contents, file->length,
604 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
605 (hb_destroy_func_t) _hb_mapped_file_destroy);
612 #elif defined(_WIN32) && !defined(HB_NO_MMAP)
613 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
614 if (unlikely (!file)) return hb_blob_get_empty ();
617 unsigned int size = strlen (file_name) + 1;
618 wchar_t * wchar_file_name = (wchar_t *) malloc (sizeof (wchar_t) * size);
619 if (unlikely (!wchar_file_name)) goto fail_without_close;
620 mbstowcs (wchar_file_name, file_name, size);
621 #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
623 CREATEFILE2_EXTENDED_PARAMETERS ceparams = { 0 };
624 ceparams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
625 ceparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFFF;
626 ceparams.dwFileFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0xFFF00000;
627 ceparams.dwSecurityQosFlags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED & 0x000F0000;
628 ceparams.lpSecurityAttributes = nullptr;
629 ceparams.hTemplateFile = nullptr;
630 fd = CreateFile2 (wchar_file_name, GENERIC_READ, FILE_SHARE_READ,
631 OPEN_EXISTING, &ceparams);
634 fd = CreateFileW (wchar_file_name, GENERIC_READ, FILE_SHARE_READ, nullptr,
635 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
638 free (wchar_file_name);
640 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
642 #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
644 LARGE_INTEGER length;
645 GetFileSizeEx (fd, &length);
646 file->length = length.LowPart;
647 file->mapping = CreateFileMappingFromApp (fd, nullptr, PAGE_READONLY, length.QuadPart, nullptr);
650 file->length = (unsigned long) GetFileSize (fd, nullptr);
651 file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
653 if (unlikely (!file->mapping)) goto fail;
655 #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
656 file->contents = (char *) MapViewOfFileFromApp (file->mapping, FILE_MAP_READ, 0, 0);
658 file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
660 if (unlikely (!file->contents)) goto fail;
663 return hb_blob_create (file->contents, file->length,
664 HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE, (void *) file,
665 (hb_destroy_func_t) _hb_mapped_file_destroy);
674 /* The following tries to read a file without knowing its size beforehand
675 It's used as a fallback for systems without mmap or to read from pipes */
676 unsigned long len = 0, allocated = BUFSIZ * 16;
677 char *data = (char *) malloc (allocated);
678 if (unlikely (!data)) return hb_blob_get_empty ();
680 FILE *fp = fopen (file_name, "rb");
681 if (unlikely (!fp)) goto fread_fail_without_close;
685 if (allocated - len < BUFSIZ)
688 /* Don't allocate and go more than ~536MB, our mmap reader still
689 can cover files like that but lets limit our fallback reader */
690 if (unlikely (allocated > (2 << 28))) goto fread_fail;
691 char *new_data = (char *) realloc (data, allocated);
692 if (unlikely (!new_data)) goto fread_fail;
696 unsigned long addition = fread (data + len, 1, allocated - len, fp);
698 int err = ferror (fp);
699 #ifdef EINTR // armcc doesn't have it
700 if (unlikely (err == EINTR)) continue;
702 if (unlikely (err)) goto fread_fail;
708 return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
709 (hb_destroy_func_t) free);
713 fread_fail_without_close:
715 return hb_blob_get_empty ();
717 #endif /* !HB_NO_OPEN */