Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / hb-blob.cc
index 2e72683..3ca31c5 100644 (file)
  * Red Hat Author(s): Behdad Esfahbod
  */
 
-
-/* https://github.com/harfbuzz/harfbuzz/issues/1308
- * http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
- * https://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html
- */
-#if !defined(_POSIX_C_SOURCE) && !defined(_MSC_VER) && !defined(__NetBSD__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wunused-macros"
-#define _POSIX_C_SOURCE 200809L
-#pragma GCC diagnostic pop
-#endif
-
 #include "hb.hh"
 #include "hb-blob.hh"
 
@@ -201,7 +189,7 @@ hb_blob_copy_writable_or_fail (hb_blob_t *blob)
 hb_blob_t *
 hb_blob_get_empty ()
 {
-  return const_cast<hb_blob_t *> (&Null(hb_blob_t));
+  return const_cast<hb_blob_t *> (&Null (hb_blob_t));
 }
 
 /**
@@ -488,6 +476,9 @@ hb_blob_t::try_make_writable ()
 
 #ifndef HB_NO_OPEN
 #ifdef HAVE_MMAP
+# if !defined(HB_NO_RESOURCE_FORK) && defined(__APPLE__)
+#  include <sys/paths.h>
+# endif
 # include <sys/types.h>
 # include <sys/stat.h>
 # include <fcntl.h>
@@ -532,6 +523,39 @@ _hb_mapped_file_destroy (void *file_)
 }
 #endif
 
+#ifdef _PATH_RSRCFORKSPEC
+static int
+_open_resource_fork (const char *file_name, hb_mapped_file_t *file)
+{
+  size_t name_len = strlen (file_name);
+  size_t len = name_len + sizeof (_PATH_RSRCFORKSPEC);
+
+  char *rsrc_name = (char *) malloc (len);
+  if (unlikely (!rsrc_name)) return -1;
+
+  strncpy (rsrc_name, file_name, name_len);
+  strncpy (rsrc_name + name_len, _PATH_RSRCFORKSPEC,
+          sizeof (_PATH_RSRCFORKSPEC) - 1);
+
+  int fd = open (rsrc_name, O_RDONLY | O_BINARY, 0);
+  free (rsrc_name);
+
+  if (fd != -1)
+  {
+    struct stat st;
+    if (fstat (fd, &st) != -1)
+      file->length = (unsigned long) st.st_size;
+    else
+    {
+      close (fd);
+      fd = -1;
+    }
+  }
+
+  return fd;
+}
+#endif
+
 /**
  * hb_blob_create_from_file:
  * @file_name: font filename.
@@ -556,6 +580,19 @@ hb_blob_create_from_file (const char *file_name)
   if (unlikely (fstat (fd, &st) == -1)) goto fail;
 
   file->length = (unsigned long) st.st_size;
+
+#ifdef _PATH_RSRCFORKSPEC
+  if (unlikely (file->length == 0))
+  {
+    int rfd = _open_resource_fork (file_name, file);
+    if (rfd != -1)
+    {
+      close (fd);
+      fd = rfd;
+    }
+  }
+#endif
+
   file->contents = (char *) mmap (nullptr, file->length, PROT_READ,
                                  MAP_PRIVATE | MAP_NORESERVE, fd, 0);
 
@@ -579,7 +616,7 @@ fail_without_close:
   HANDLE fd;
   unsigned int size = strlen (file_name) + 1;
   wchar_t * wchar_file_name = (wchar_t *) malloc (sizeof (wchar_t) * size);
-  if (unlikely (wchar_file_name == nullptr)) goto fail_without_close;
+  if (unlikely (!wchar_file_name)) goto fail_without_close;
   mbstowcs (wchar_file_name, file_name, size);
 #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
   {
@@ -613,14 +650,14 @@ fail_without_close:
   file->length = (unsigned long) GetFileSize (fd, nullptr);
   file->mapping = CreateFileMapping (fd, nullptr, PAGE_READONLY, 0, 0, nullptr);
 #endif
-  if (unlikely (file->mapping == nullptr)) goto fail;
+  if (unlikely (!file->mapping)) goto fail;
 
 #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
   file->contents = (char *) MapViewOfFileFromApp (file->mapping, FILE_MAP_READ, 0, 0);
 #else
   file->contents = (char *) MapViewOfFile (file->mapping, FILE_MAP_READ, 0, 0, 0);
 #endif
-  if (unlikely (file->contents == nullptr)) goto fail;
+  if (unlikely (!file->contents)) goto fail;
 
   CloseHandle (fd);
   return hb_blob_create (file->contents, file->length,
@@ -638,10 +675,10 @@ fail_without_close:
      It's used as a fallback for systems without mmap or to read from pipes */
   unsigned long len = 0, allocated = BUFSIZ * 16;
   char *data = (char *) malloc (allocated);
-  if (unlikely (data == nullptr)) return hb_blob_get_empty ();
+  if (unlikely (!data)) return hb_blob_get_empty ();
 
   FILE *fp = fopen (file_name, "rb");
-  if (unlikely (fp == nullptr)) goto fread_fail_without_close;
+  if (unlikely (!fp)) goto fread_fail_without_close;
 
   while (!feof (fp))
   {
@@ -652,7 +689,7 @@ fail_without_close:
         can cover files like that but lets limit our fallback reader */
       if (unlikely (allocated > (2 << 28))) goto fread_fail;
       char *new_data = (char *) realloc (data, allocated);
-      if (unlikely (new_data == nullptr)) goto fread_fail;
+      if (unlikely (!new_data)) goto fread_fail;
       data = new_data;
     }
 
@@ -666,6 +703,7 @@ fail_without_close:
 
     len += addition;
   }
+       fclose (fp);
 
   return hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, data,
                         (hb_destroy_func_t) free);