1 /*
   2  * Copyright (c) 1994, 2018, 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 <stdlib.h>
  27 #include <string.h>
  28 #include <stddef.h>
  29 
  30 #include "jni.h"
  31 #include "jni_util.h"
  32 #include "jvm.h"
  33 #include "io_util.h"
  34 #include "io_util_md.h"
  35 
  36 /* IO helper functions */
  37 
  38 jint
  39 readSingle(JNIEnv *env, jobject this, jfieldID fid) {
  40     jint nread;
  41     char ret;
  42     FD fd = GET_FD(this, fid);
  43     if (fd == -1) {
  44         JNU_ThrowIOException(env, "Stream Closed");
  45         return -1;
  46     }
  47     nread = IO_Read(fd, &ret, 1);
  48     if (nread == 0) { /* EOF */
  49         return -1;
  50     } else if (nread == -1) { /* error */
  51         JNU_ThrowIOExceptionWithLastError(env, "Read error");
  52     }
  53 
  54     JVM_callFileReadBytes(env, 1);
  55 
  56     return ret & 0xFF;
  57 }
  58 
  59 /* The maximum size of a stack-allocated buffer.
  60  */
  61 #define BUF_SIZE 8192
  62 
  63 /*
  64  * Returns true if the array slice defined by the given offset and length
  65  * is out of bounds.
  66  */
  67 static int
  68 outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) {
  69     return ((off < 0) ||
  70             (len < 0) ||
  71             // We are very careful to avoid signed integer overflow,
  72             // the result of which is undefined in C.
  73             ((*env)->GetArrayLength(env, array) - off < len));
  74 }
  75 
  76 jint
  77 readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
  78           jint off, jint len, jfieldID fid)
  79 {
  80     jint nread;
  81     char stackBuf[BUF_SIZE];
  82     char *buf = NULL;
  83     FD fd;
  84 
  85     if (IS_NULL(bytes)) {
  86         JNU_ThrowNullPointerException(env, NULL);
  87         return -1;
  88     }
  89 
  90     if (outOfBounds(env, off, len, bytes)) {
  91         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
  92         return -1;
  93     }
  94 
  95     if (len == 0) {
  96         return 0;
  97     } else if (len > BUF_SIZE) {
  98         buf = malloc(len);
  99         if (buf == NULL) {
 100             JNU_ThrowOutOfMemoryError(env, NULL);
 101             return 0;
 102         }
 103     } else {
 104         buf = stackBuf;
 105     }
 106 
 107     fd = GET_FD(this, fid);
 108     if (fd == -1) {
 109         JNU_ThrowIOException(env, "Stream Closed");
 110         nread = -1;
 111     } else {
 112         nread = IO_Read(fd, buf, len);
 113         if (nread > 0) {
 114             (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
 115             JVM_callFileReadBytes(env, nread);
 116         } else if (nread == -1) {
 117             JNU_ThrowIOExceptionWithLastError(env, "Read error");
 118         } else { /* EOF */
 119             nread = -1;
 120         }
 121     }
 122 
 123     if (buf != stackBuf) {
 124         free(buf);
 125     }
 126     return nread;
 127 }
 128 
 129 void
 130 writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid) {
 131     // Discard the 24 high-order bits of byte. See OutputStream#write(int)
 132     char c = (char) byte;
 133     jint n;
 134     FD fd = GET_FD(this, fid);
 135     if (fd == -1) {
 136         JNU_ThrowIOException(env, "Stream Closed");
 137         return;
 138     }
 139     if (append == JNI_TRUE) {
 140         n = IO_Append(fd, &c, 1);
 141     } else {
 142         n = IO_Write(fd, &c, 1);
 143     }
 144     if (n == -1) {
 145         JNU_ThrowIOExceptionWithLastError(env, "Write error");
 146     }
 147     JVM_callFileWriteBytes(env, 1);
 148 }
 149 
 150 void
 151 writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
 152            jint off, jint len, jboolean append, jfieldID fid)
 153 {
 154     jint n;
 155     char stackBuf[BUF_SIZE];
 156     char *buf = NULL;
 157     FD fd;
 158 
 159     if (IS_NULL(bytes)) {
 160         JNU_ThrowNullPointerException(env, NULL);
 161         return;
 162     }
 163 
 164     if (outOfBounds(env, off, len, bytes)) {
 165         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
 166         return;
 167     }
 168 
 169     if (len == 0) {
 170         return;
 171     } else if (len > BUF_SIZE) {
 172         buf = malloc(len);
 173         if (buf == NULL) {
 174             JNU_ThrowOutOfMemoryError(env, NULL);
 175             return;
 176         }
 177     } else {
 178         buf = stackBuf;
 179     }
 180 
 181     (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);
 182 
 183     if (!(*env)->ExceptionOccurred(env)) {
 184         off = 0;
 185         while (len > 0) {
 186             fd = GET_FD(this, fid);
 187             if (fd == -1) {
 188                 JNU_ThrowIOException(env, "Stream Closed");
 189                 break;
 190             }
 191             if (append == JNI_TRUE) {
 192                 n = IO_Append(fd, buf+off, len);
 193             } else {
 194                 n = IO_Write(fd, buf+off, len);
 195             }
 196             if (n == -1) {
 197                 JNU_ThrowIOExceptionWithLastError(env, "Write error");
 198                 break;
 199             }
 200             off += n;
 201             len -= n;
 202         }
 203         JVM_callFileWriteBytes(env, off);
 204     }
 205     if (buf != stackBuf) {
 206         free(buf);
 207     }
 208 }
 209 
 210 void
 211 throwFileNotFoundException(JNIEnv *env, jstring path)
 212 {
 213     char buf[256];
 214     size_t n;
 215     jobject x;
 216     jstring why = NULL;
 217 
 218     n = getLastErrorString(buf, sizeof(buf));
 219     if (n > 0) {
 220         why = JNU_NewStringPlatform(env, buf);
 221         CHECK_NULL(why);
 222     }
 223     x = JNU_NewObjectByName(env,
 224                             "java/io/FileNotFoundException",
 225                             "(Ljava/lang/String;Ljava/lang/String;)V",
 226                             path, why);
 227     if (x != NULL) {
 228         (*env)->Throw(env, x);
 229     }
 230 }