1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 
  29 #include <CoreFoundation/CoreFoundation.h>
  30 #include <CoreServices/CoreServices.h>
  31 
  32 // NB: toCFString() and toJavaString copied with modifications from
  33 // src/java.prefs/macosx/native/libprefs/MacOSXPreferencesFile.m.
  34 
  35 // Create a CF string from the given Java string.
  36 // returns NULL if javaString is NULL
  37 // throws and returns NULL on memory error
  38 static CFStringRef toCFString(JNIEnv *env, jstring javaString)
  39 {
  40     if (javaString == NULL) {
  41         return NULL;
  42     } else {
  43         CFStringRef result = NULL;
  44         jsize length = (*env)->GetStringLength(env, javaString);
  45         const jchar *chars = (*env)->GetStringChars(env, javaString, NULL);
  46         if (chars == NULL) {
  47             JNU_ThrowOutOfMemoryError(env, "toCFString failed");
  48             return NULL;
  49         }
  50         result = CFStringCreateWithCharacters(NULL, (const UniChar *)chars,
  51                                               length);
  52         (*env)->ReleaseStringChars(env, javaString, chars);
  53         if (result == NULL) {
  54             JNU_ThrowOutOfMemoryError(env, "toCFString failed");
  55             return NULL;
  56         }
  57         return result;
  58     }
  59 }
  60 
  61 // Create a Java string from the given CF string.
  62 // returns NULL if cfString is NULL
  63 // throws and returns NULL on memory error
  64 static jstring toJavaString(JNIEnv *env, CFStringRef cfString)
  65 {
  66     if (cfString == NULL) {
  67         return NULL;
  68     } else {
  69         jstring javaString = NULL;
  70 
  71         CFIndex length = CFStringGetLength(cfString);
  72         const UniChar *constchars = CFStringGetCharactersPtr(cfString);
  73         if (constchars) {
  74             javaString = (*env)->NewString(env, constchars, length);
  75         } else {
  76             UniChar *chars = malloc(length * sizeof(UniChar));
  77             if (chars == NULL) {
  78                 JNU_ThrowOutOfMemoryError(env, "toJavaString failed");
  79                 return NULL;
  80             }
  81             CFStringGetCharacters(cfString, CFRangeMake(0, length), chars);
  82             javaString = (*env)->NewString(env, chars, length);
  83             free(chars);
  84         }
  85         return javaString;
  86     }
  87 }
  88 
  89 JNIEXPORT jstring JNICALL
  90 Java_sun_nio_fs_UTIFileTypeDetector_probe0(JNIEnv* env, jobject ftd,
  91                                            jstring ext)
  92 {
  93     jstring result = NULL;
  94 
  95     CFStringRef extension = toCFString(env, ext);
  96     if (extension != NULL) {
  97         CFStringRef uti =
  98             UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
  99                                                   extension, NULL);
 100         CFRelease(extension);
 101 
 102         if (uti != NULL) {
 103             CFStringRef mimeType =
 104                 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
 105             CFRelease(uti);
 106 
 107             if (mimeType != NULL) {
 108                 result = toJavaString(env, mimeType);
 109                 CFRelease(mimeType);
 110             }
 111         }
 112     }
 113 
 114     return result;
 115 }