[JP3D] To avoid divisions by zero / undefined behaviour on shift (CVE-2018-14423
[openjpeg.git] / src / lib / openjp3d / cio.c
old mode 100755 (executable)
new mode 100644 (file)
index 4e99c84..99007d0
-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {\r
-       opj_cp_t *cp = NULL;\r
-       opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));\r
-       if(!cio) return NULL;\r
-       cio->cinfo = cinfo;\r
-       if(buffer && length) {\r
-               /* wrap a user buffer containing the encoded image */\r
-               cio->openmode = OPJ_STREAM_READ;\r
-               cio->buffer = buffer;\r
-               cio->length = length;\r
-       }\r
-       else if(!buffer && !length && cinfo) {\r
-               /* allocate a buffer for the encoded image */\r
-               cio->openmode = OPJ_STREAM_WRITE;\r
-               switch(cinfo->codec_format) {\r
-                       case CODEC_J3D:\r
-                       case CODEC_J2K:\r
-                               cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;\r
-                               break;\r
-                       default:\r
-                               opj_free(cio);\r
-                               return NULL;\r
-               }\r
-               cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;\r
-               cio->buffer = (unsigned char *)opj_malloc(cio->length);\r
-               if(!cio->buffer) {\r
-                       opj_free(cio);\r
-                       return NULL;\r
-               }\r
-       }\r
-       else {\r
-               opj_free(cio);\r
-               return NULL;\r
-       }\r
-\r
-       /* Initialize byte IO */\r
-       cio->start = cio->buffer;\r
-       cio->end = cio->buffer + cio->length;\r
-       cio->bp = cio->buffer;\r
-\r
-       return cio;\r
-}\r
-\r
-void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {\r
-       if(cio) {\r
-               if(cio->openmode == OPJ_STREAM_WRITE) {\r
-                       /* destroy the allocated buffer */\r
-                       opj_free(cio->buffer);\r
-               }\r
-               /* destroy the cio */\r
-               opj_free(cio);\r
-       }\r
-}\r
-\r
-\r
-/* ----------------------------------------------------------------------- */\r
-\r
-/*\r
- * Get position in byte stream.\r
- */\r
-int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {\r
-       return cio->bp - cio->start;\r
-}\r
-\r
-/*\r
- * Set position in byte stream.\r
- *\r
- * pos : position, in number of bytes, from the beginning of the stream\r
- */\r
-void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {\r
-       cio->bp = cio->start + pos;\r
-}\r
-\r
-/*\r
- * Number of bytes left before the end of the stream.\r
- */\r
-int cio_numbytesleft(opj_cio_t *cio) {\r
-       return cio->end - cio->bp;\r
-}\r
-\r
-/*\r
- * Get pointer to the current position in the stream.\r
- */\r
-unsigned char *cio_getbp(opj_cio_t *cio) {\r
-       return cio->bp;\r
-}\r
-\r
-/*\r
- * Write a byte.\r
- */\r
-static bool cio_byteout(opj_cio_t *cio, unsigned char v) {\r
-       if (cio->bp >= cio->end) {\r
-               opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");\r
-               return false;\r
-       }\r
-       *cio->bp++ = v;\r
-       return true;\r
-}\r
-\r
-/*\r
- * Read a byte.\r
- */\r
-static unsigned char cio_bytein(opj_cio_t *cio) {\r
-       if (cio->bp >= cio->end) {\r
-               opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");\r
-               return 0;\r
-       }\r
-       return *cio->bp++;\r
-}\r
-\r
-/*\r
- * Write some bytes.\r
- *\r
- * v : value to write\r
- * n : number of bytes to write\r
- */\r
-unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {\r
-       int i;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )\r
-                       return 0;\r
-       }\r
-       return n;\r
-}\r
-\r
-/*\r
- * Read some bytes.\r
- *\r
- * n : number of bytes to read\r
- *\r
- * return : value of the n bytes read\r
- */\r
-unsigned int cio_read(opj_cio_t *cio, int n) {\r
-       int i;\r
-       unsigned int v;\r
-       v = 0;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               v += cio_bytein(cio) << (i << 3);\r
-       }\r
-       return v;\r
-}\r
-\r
-/* \r
- * Skip some bytes.\r
- *\r
- * n : number of bytes to skip\r
- */\r
-void cio_skip(opj_cio_t *cio, int n) {\r
-       cio->bp += n;\r
-}\r
-\r
-/*\r
- * Write some bytes.\r
- *\r
- * v : value to write\r
- * n : number of bytes to write\r
- */\r
-int cio_write_int(opj_cio_t *cio, int v, int n) {\r
-       int i;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) )\r
-                       return 0;\r
-       }\r
-       return n;\r
-}\r
-\r
-/*\r
- * Read some bytes.\r
- *\r
- * n : number of bytes to read\r
- *\r
- * return : value of the n bytes read\r
- */\r
-int cio_read_int(opj_cio_t *cio, int n) {\r
-       int i;\r
-       int v;\r
-       v = 0;\r
-       for (i = n - 1; i >= 0; i--) {\r
-               v += cio_bytein(cio) << (i << 3);\r
-       }\r
-       return v;\r
-}\r
-\r
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* ----------------------------------------------------------------------- */
+
+opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo,
+                                     unsigned char *buffer, int length)
+{
+    opj_cp_t *cp = NULL;
+    opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
+    if (!cio) {
+        return NULL;
+    }
+    cio->cinfo = cinfo;
+    if (buffer && length) {
+        /* wrap a user buffer containing the encoded image */
+        cio->openmode = OPJ_STREAM_READ;
+        cio->buffer = buffer;
+        cio->length = length;
+    } else if (!buffer && !length && cinfo) {
+        /* allocate a buffer for the encoded image */
+        cio->openmode = OPJ_STREAM_WRITE;
+        switch (cinfo->codec_format) {
+        case CODEC_J3D:
+        case CODEC_J2K:
+            cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;
+            break;
+        default:
+            opj_free(cio);
+            return NULL;
+        }
+        cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;
+        cio->buffer = (unsigned char *)opj_malloc(cio->length);
+        if (!cio->buffer) {
+            opj_event_msg(cio->cinfo, EVT_ERROR,
+                          "Error allocating memory for compressed bitstream\n");
+            opj_free(cio);
+            return NULL;
+        }
+    } else {
+        opj_free(cio);
+        return NULL;
+    }
+
+    /* Initialize byte IO */
+    cio->start = cio->buffer;
+    cio->end = cio->buffer + cio->length;
+    cio->bp = cio->buffer;
+
+    return cio;
+}
+
+void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio)
+{
+    if (cio) {
+        if (cio->openmode == OPJ_STREAM_WRITE) {
+            /* destroy the allocated buffer */
+            opj_free(cio->buffer);
+        }
+        /* destroy the cio */
+        opj_free(cio);
+    }
+}
+
+
+/* ----------------------------------------------------------------------- */
+
+/*
+ * Get position in byte stream.
+ */
+int OPJ_CALLCONV cio_tell(opj_cio_t *cio)
+{
+    return cio->bp - cio->start;
+}
+
+/*
+ * Set position in byte stream.
+ *
+ * pos : position, in number of bytes, from the beginning of the stream
+ */
+void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos)
+{
+    cio->bp = cio->start + pos;
+}
+
+/*
+ * Number of bytes left before the end of the stream.
+ */
+int cio_numbytesleft(opj_cio_t *cio)
+{
+    return cio->end - cio->bp;
+}
+
+/*
+ * Get pointer to the current position in the stream.
+ */
+unsigned char *cio_getbp(opj_cio_t *cio)
+{
+    return cio->bp;
+}
+
+/*
+ * Write a byte.
+ */
+static bool cio_byteout(opj_cio_t *cio, unsigned char v)
+{
+    if (cio->bp >= cio->end) {
+        opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
+        return false;
+    }
+    *cio->bp++ = v;
+    return true;
+}
+
+/*
+ * Read a byte.
+ */
+static unsigned char cio_bytein(opj_cio_t *cio)
+{
+    if (cio->bp >= cio->end) {
+        opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
+        return 0;
+    }
+    return *cio->bp++;
+}
+
+/*
+ * Write some bytes.
+ *
+ * v : value to write
+ * n : number of bytes to write
+ */
+unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n)
+{
+    int i;
+    for (i = n - 1; i >= 0; i--) {
+        if (!cio_byteout(cio, (unsigned char)((v >> (i << 3)) & 0xff))) {
+            return 0;
+        }
+    }
+    return n;
+}
+
+/*
+ * Read some bytes.
+ *
+ * n : number of bytes to read
+ *
+ * return : value of the n bytes read
+ */
+unsigned int cio_read(opj_cio_t *cio, int n)
+{
+    int i;
+    unsigned int v;
+    v = 0;
+    for (i = n - 1; i >= 0; i--) {
+        v += cio_bytein(cio) << (i << 3);
+    }
+    return v;
+}
+
+/*
+ * Skip some bytes.
+ *
+ * n : number of bytes to skip
+ */
+void cio_skip(opj_cio_t *cio, int n)
+{
+    cio->bp += n;
+}
+
+/*
+ * Write some bytes.
+ *
+ * v : value to write
+ * n : number of bytes to write
+ */
+int cio_write_int(opj_cio_t *cio, int v, int n)
+{
+    int i;
+    for (i = n - 1; i >= 0; i--) {
+        if (!cio_byteout(cio, (char)((v >> (i << 3)) & 0xff))) {
+            return 0;
+        }
+    }
+    return n;
+}
+
+/*
+ * Read some bytes.
+ *
+ * n : number of bytes to read
+ *
+ * return : value of the n bytes read
+ */
+int cio_read_int(opj_cio_t *cio, int n)
+{
+    int i;
+    int v;
+    v = 0;
+    for (i = n - 1; i >= 0; i--) {
+        v += cio_bytein(cio) << (i << 3);
+    }
+    return v;
+}
+