5389N/A/*
5389N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
5389N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5389N/A *
5389N/A * This code is free software; you can redistribute it and/or modify it
5389N/A * under the terms of the GNU General Public License version 2 only, as
5389N/A * published by the Free Software Foundation. Oracle designates this
5389N/A * particular file as subject to the "Classpath" exception as provided
5389N/A * by Oracle in the LICENSE file that accompanied this code.
5389N/A *
5389N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5389N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5389N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5389N/A * version 2 for more details (a copy is included in the LICENSE file that
5389N/A * accompanied this code).
5389N/A *
5389N/A * You should have received a copy of the GNU General Public License version
5389N/A * 2 along with this work; if not, write to the Free Software Foundation,
5389N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5389N/A *
5389N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5389N/A * or visit www.oracle.com if you need additional information or have any
5389N/A * questions.
5389N/A */
5389N/A
5389N/A#include "jni.h"
5389N/A#include "jni_util.h"
5389N/A#include "jvm.h"
5389N/A#include "jlong.h"
5389N/A
5389N/A#include <stdlib.h>
5389N/A#include <string.h>
5389N/A
5389N/A#include <CoreFoundation/CoreFoundation.h>
5389N/A
5389N/AJNIEXPORT jcharArray JNICALL
5389N/AJava_sun_nio_fs_MacOSXNativeDispatcher_normalizepath(JNIEnv* env, jclass this,
5389N/A jcharArray path,
5389N/A jint form)
5389N/A{
5389N/A jcharArray result = NULL;
5389N/A char chars_buf[(PATH_MAX + 1) * 2]; // utf16 + zero padding
5389N/A CFMutableStringRef csref = CFStringCreateMutable(NULL, 0);
5389N/A if (csref == NULL) {
5389N/A JNU_ThrowOutOfMemoryError(env, "native heap");
5389N/A } else {
5389N/A char *chars = (char*)(*env)->GetPrimitiveArrayCritical(env, path, 0);
5389N/A jsize len = (*env)->GetArrayLength(env, path);
5389N/A CFStringAppendCharacters(csref, (const UniChar*)chars, len);
5389N/A (*env)->ReleasePrimitiveArrayCritical(env, path, chars, 0);
5389N/A CFStringNormalize(csref, form);
5389N/A len = CFStringGetLength(csref);
5389N/A if (len < PATH_MAX) {
5389N/A if (CFStringGetCString(csref, chars_buf, sizeof(chars_buf), kCFStringEncodingUTF16)) {
5389N/A result = (*env)->NewCharArray(env, len);
5389N/A (*env)->SetCharArrayRegion(env, result, 0, len, (jchar*)&chars_buf);
5389N/A }
5389N/A } else {
5389N/A int ulen = (len + 1) * 2;
5389N/A chars = malloc(ulen);
5389N/A if (chars == NULL) {
5389N/A CFRelease(csref);
5389N/A JNU_ThrowOutOfMemoryError(env, "native heap");
5389N/A return result;
5389N/A } else {
5389N/A if (CFStringGetCString(csref, chars, ulen, kCFStringEncodingUTF16)) {
5389N/A result = (*env)->NewCharArray(env, len);
5389N/A (*env)->SetCharArrayRegion(env, result, 0, len, (jchar*)chars);
5389N/A }
5389N/A free(chars);
5389N/A }
5389N/A }
5389N/A CFRelease(csref);
5389N/A }
5389N/A return result;
5389N/A}