[1.5] Adding some simple unit tests
[openjpeg.git] / libopenjpeg / cio.c
index 6082e9be9f038736fc9755be97e769dcf9240781..b8a7ecf8a873a7f4cf001e59334a98d4dc6ae383 100644 (file)
@@ -58,9 +58,10 @@ opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer
                                opj_free(cio);
                                return NULL;
                }
-               cio->length = (int) (1.3 * cp->img_size);
+               cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
                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;
                }
@@ -125,13 +126,13 @@ unsigned char *cio_getbp(opj_cio_t *cio) {
 /*
  * Write a byte.
  */
-bool cio_byteout(opj_cio_t *cio, unsigned char v) {
+opj_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;
+               return OPJ_FALSE;
        }
        *cio->bp++ = v;
-       return true;
+       return OPJ_TRUE;
 }
 
 /*
@@ -139,7 +140,7 @@ bool cio_byteout(opj_cio_t *cio, unsigned char v) {
  */
 unsigned char cio_bytein(opj_cio_t *cio) {
        if (cio->bp >= cio->end) {
-               opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
+               opj_event_msg(cio->cinfo, EVT_ERROR, "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
                return 0;
        }
        return *cio->bp++;
@@ -151,7 +152,7 @@ unsigned char cio_bytein(opj_cio_t *cio) {
  * v : value to write
  * n : number of bytes to write
  */
-unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
+unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n) {
        int i;
        for (i = n - 1; i >= 0; i--) {
                if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )