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
28 /* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */
29 #ifndef _POSIX_C_SOURCE
30 #define _POSIX_C_SOURCE 200809L
33 #include "hb-private.hh"
34 #include "hb-debug.hh"
35 #include "hb-blob-private.hh"
37 #ifdef HAVE_SYS_MMAN_H
40 #endif /* HAVE_UNISTD_H */
42 #endif /* HAVE_SYS_MMAN_H */
50 * hb_blob_create: (skip)
51 * @data: Pointer to blob data.
52 * @length: Length of @data in bytes.
53 * @mode: Memory mode for @data.
54 * @user_data: Data parameter to pass to @destroy.
55 * @destroy: Callback to call when @data is not needed anymore.
57 * Creates a new "blob" object wrapping @data. The @mode parameter is used
58 * to negotiate ownership and lifecycle of @data.
60 * Return value: New blob, or the empty blob if something failed or if @length is
61 * zero. Destroy with hb_blob_destroy().
66 hb_blob_create (const char *data,
68 hb_memory_mode_t mode,
70 hb_destroy_func_t destroy)
76 !(blob = hb_object_create<hb_blob_t> ())) {
79 return hb_blob_get_empty ();
83 blob->length = length;
86 blob->user_data = user_data;
87 blob->destroy = destroy;
89 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
90 blob->mode = HB_MEMORY_MODE_READONLY;
91 if (!blob->try_make_writable ()) {
92 hb_blob_destroy (blob);
93 return hb_blob_get_empty ();
101 _hb_blob_destroy (void *data)
103 hb_blob_destroy ((hb_blob_t *) data);
107 * hb_blob_create_sub_blob:
108 * @parent: Parent blob.
109 * @offset: Start offset of sub-blob within @parent, in bytes.
110 * @length: Length of sub-blob.
112 * Returns a blob that represents a range of bytes in @parent. The new
113 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
114 * will never modify data in the parent blob. The parent data is not
115 * expected to be modified, and will result in undefined behavior if it
118 * Makes @parent immutable.
120 * Return value: New blob, or the empty blob if something failed or if
121 * @length is zero or @offset is beyond the end of @parent's data. Destroy
122 * with hb_blob_destroy().
127 hb_blob_create_sub_blob (hb_blob_t *parent,
133 if (!length || offset >= parent->length)
134 return hb_blob_get_empty ();
136 hb_blob_make_immutable (parent);
138 blob = hb_blob_create (parent->data + offset,
139 MIN (length, parent->length - offset),
140 HB_MEMORY_MODE_READONLY,
141 hb_blob_reference (parent),
148 * hb_blob_copy_writable_or_fail:
151 * Makes a writable copy of @blob.
153 * Return value: New blob, or nullptr if allocation failed.
158 hb_blob_copy_writable_or_fail (hb_blob_t *blob)
160 blob = hb_blob_create (blob->data,
162 HB_MEMORY_MODE_DUPLICATE,
166 if (unlikely (blob == hb_blob_get_empty ()))
175 * Returns the singleton empty blob.
177 * See TODO:link object types for more information.
179 * Return value: (transfer full): the empty blob.
184 hb_blob_get_empty (void)
186 static const hb_blob_t _hb_blob_nil = {
187 HB_OBJECT_HEADER_STATIC,
189 true, /* immutable */
193 HB_MEMORY_MODE_READONLY, /* mode */
195 nullptr, /* user_data */
196 nullptr /* destroy */
199 return const_cast<hb_blob_t *> (&_hb_blob_nil);
203 * hb_blob_reference: (skip)
206 * Increases the reference count on @blob.
208 * See TODO:link object types for more information.
210 * Return value: @blob.
215 hb_blob_reference (hb_blob_t *blob)
217 return hb_object_reference (blob);
221 * hb_blob_destroy: (skip)
224 * Decreases the reference count on @blob, and if it reaches zero, destroys
225 * @blob, freeing all memory, possibly calling the destroy-callback the blob
226 * was created for if it has not been called already.
228 * See TODO:link object types for more information.
233 hb_blob_destroy (hb_blob_t *blob)
235 if (!hb_object_destroy (blob)) return;
237 blob->fini_shallow ();
243 * hb_blob_set_user_data: (skip)
245 * @key: key for data to set.
246 * @data: data to set.
247 * @destroy: callback to call when @data is not needed anymore.
248 * @replace: whether to replace an existing data with the same key.
255 hb_blob_set_user_data (hb_blob_t *blob,
256 hb_user_data_key_t *key,
258 hb_destroy_func_t destroy,
261 return hb_object_set_user_data (blob, key, data, destroy, replace);
265 * hb_blob_get_user_data: (skip)
267 * @key: key for data to get.
271 * Return value: (transfer none):
276 hb_blob_get_user_data (hb_blob_t *blob,
277 hb_user_data_key_t *key)
279 return hb_object_get_user_data (blob, key);
284 * hb_blob_make_immutable:
292 hb_blob_make_immutable (hb_blob_t *blob)
294 if (hb_object_is_inert (blob))
297 blob->immutable = true;
301 * hb_blob_is_immutable:
311 hb_blob_is_immutable (hb_blob_t *blob)
313 return blob->immutable;
318 * hb_blob_get_length:
323 * Return value: the length of blob data in bytes.
328 hb_blob_get_length (hb_blob_t *blob)
340 * Returns: (transfer none) (array length=length):
345 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
348 *length = blob->length;
354 * hb_blob_get_data_writable:
356 * @length: (out): output length of the writable data.
358 * Tries to make blob data writable (possibly copying it) and
359 * return pointer to data.
361 * Fails if blob has been made immutable, or if memory allocation
364 * Returns: (transfer none) (array length=length): Writable blob data,
365 * or %NULL if failed.
370 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
372 if (!blob->try_make_writable ()) {
380 *length = blob->length;
382 return const_cast<char *> (blob->data);
387 hb_blob_t::try_make_writable_inplace_unix (void)
389 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
390 uintptr_t pagesize = -1, mask, length;
393 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
394 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
395 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
396 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
397 #elif defined(HAVE_GETPAGESIZE)
398 pagesize = (uintptr_t) getpagesize ();
401 if ((uintptr_t) -1L == pagesize) {
402 DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno));
405 DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize);
407 mask = ~(pagesize-1);
408 addr = (const char *) (((uintptr_t) this->data) & mask);
409 length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask) - addr;
410 DEBUG_MSG_FUNC (BLOB, this,
411 "calling mprotect on [%p..%p] (%lu bytes)",
412 addr, addr+length, (unsigned long) length);
413 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
414 DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno));
418 this->mode = HB_MEMORY_MODE_WRITABLE;
420 DEBUG_MSG_FUNC (BLOB, this,
421 "successfully made [%p..%p] (%lu bytes) writable\n",
422 addr, addr+length, (unsigned long) length);
430 hb_blob_t::try_make_writable_inplace (void)
432 DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n");
434 if (this->try_make_writable_inplace_unix ())
437 DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n");
439 /* Failed to make writable inplace, mark that */
440 this->mode = HB_MEMORY_MODE_READONLY;
445 hb_blob_t::try_make_writable (void)
450 if (this->mode == HB_MEMORY_MODE_WRITABLE)
453 if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ())
456 if (this->mode == HB_MEMORY_MODE_WRITABLE)
460 DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data);
464 new_data = (char *) malloc (this->length);
465 if (unlikely (!new_data))
468 DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data);
470 memcpy (new_data, this->data, this->length);
471 this->destroy_user_data ();
472 this->mode = HB_MEMORY_MODE_WRITABLE;
473 this->data = new_data;
474 this->user_data = new_data;
475 this->destroy = free;
485 # include <sys/types.h>
486 # include <sys/stat.h>
490 #if defined(_WIN32) || defined(__CYGWIN__)
491 # include <windows.h>
498 #ifndef MAP_NORESERVE
499 # define MAP_NORESERVE 0
502 struct hb_mapped_file_t
505 unsigned long length;
506 #if defined(_WIN32) || defined(__CYGWIN__)
512 _hb_mapped_file_destroy (hb_mapped_file_t *file)
515 munmap (file->contents, file->length);
516 #elif defined(_WIN32) || defined(__CYGWIN__)
517 UnmapViewOfFile (file->contents);
518 CloseHandle (file->mapping);
520 free (file->contents);
527 * hb_blob_create_from_file:
528 * @file_name: font filename.
530 * Returns: A hb_blob_t pointer with the content of the file
535 hb_blob_create_from_file (const char *file_name)
537 // Adopted from glib's gmappedfile.c with Matthias Clasen and
538 // Allison Lortie permission but changed a lot to suit our need.
539 bool writable = false;
540 hb_memory_mode_t mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
541 hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t));
542 if (unlikely (!file)) return hb_blob_get_empty ();
545 int fd = open (file_name, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
547 if (unlikely (fd == -1)) goto fail_without_close;
550 if (unlikely (fstat (fd, &st) == -1)) goto fail;
552 // See https://github.com/GNOME/glib/blob/f9faac7/glib/gmappedfile.c#L139-L142
553 if (unlikely (st.st_size == 0 && S_ISREG (st.st_mode))) goto fail;
555 file->length = (unsigned long) st.st_size;
556 file->contents = (char *) mmap (nullptr, file->length,
557 writable ? PROT_READ|PROT_WRITE : PROT_READ,
558 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
560 if (unlikely (file->contents == MAP_FAILED)) goto fail;
562 #elif defined(_WIN32) || defined(__CYGWIN__)
563 HANDLE fd = CreateFile (file_name,
564 writable ? GENERIC_READ|GENERIC_WRITE : GENERIC_READ,
565 FILE_SHARE_READ, nullptr, OPEN_EXISTING,
566 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, nullptr);
567 # define CLOSE CloseHandle
569 if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close;
571 file->length = (unsigned long) GetFileSize (fd, nullptr);
572 file->mapping = CreateFileMapping (fd, nullptr,
573 writable ? PAGE_WRITECOPY : PAGE_READONLY,
575 if (unlikely (file->mapping == nullptr)) goto fail;
577 file->contents = (char *) MapViewOfFile (file->mapping,
578 writable ? FILE_MAP_COPY : FILE_MAP_READ,
580 if (unlikely (file->contents == nullptr)) goto fail;
583 mm = HB_MEMORY_MODE_WRITABLE;
585 FILE *fd = fopen (file_name, "rb");
586 # define CLOSE fclose
587 if (unlikely (!fd)) goto fail_without_close;
589 fseek (fd, 0, SEEK_END);
590 file->length = ftell (fd);
592 file->contents = (char *) malloc (file->length);
593 if (unlikely (!file->contents)) goto fail;
595 if (unlikely (fread (file->contents, 1, file->length, fd) != file->length))
601 return hb_blob_create (file->contents, file->length, mm, (void *) file,
602 (hb_destroy_func_t) _hb_mapped_file_destroy);
609 return hb_blob_get_empty ();