header->biBitCount = (OPJ_UINT16)getc(IN);
header->biBitCount |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+ if (header->biBitCount == 0) {
+ fprintf(stderr, "Error, invalid biBitCount %d\n", 0);
+ return OPJ_FALSE;
+ }
if (header->biSize >= 40U) {
header->biCompression = (OPJ_UINT32)getc(IN);
x = y = 0U;
while (y < height) {
int c = getc(IN);
+ if (c == EOF) {
+ return OPJ_FALSE;
+ }
if (c) {
- int j;
- OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
+ int j, c1_int;
+ OPJ_UINT8 c1;
+
+ c1_int = getc(IN);
+ if (c1_int == EOF) {
+ return OPJ_FALSE;
+ }
+ c1 = (OPJ_UINT8)c1_int;
for (j = 0; (j < c) && (x < width) &&
((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
}
} else {
c = getc(IN);
+ if (c == EOF) {
+ return OPJ_FALSE;
+ }
+
if (c == 0x00) { /* EOL */
x = 0;
++y;
break;
} else if (c == 0x02) { /* MOVE by dxdy */
c = getc(IN);
+ if (c == EOF) {
+ return OPJ_FALSE;
+ }
x += (OPJ_UINT32)c;
c = getc(IN);
+ if (c == EOF) {
+ return OPJ_FALSE;
+ }
y += (OPJ_UINT32)c;
pix = pData + y * stride + x;
} else { /* 03 .. 255 */
int j;
for (j = 0; (j < c) && (x < width) &&
((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) {
- OPJ_UINT8 c1 = (OPJ_UINT8)getc(IN);
+ int c1_int;
+ OPJ_UINT8 c1;
+ c1_int = getc(IN);
+ if (c1_int == EOF) {
+ return OPJ_FALSE;
+ }
+ c1 = (OPJ_UINT8)c1_int;
*pix = c1;
}
if ((OPJ_UINT32)c & 1U) { /* skip padding byte */
- getc(IN);
+ c = getc(IN);
+ if (c == EOF) {
+ return OPJ_FALSE;
+ }
}
}
}
r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
+ if (adjustR > 0) {
+ r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
+ }
if (r > 255) {
r = 255;
} else if (r < 0) {
g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
- g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
+ if (adjustG > 0) {
+ g = ((g >> adjustG) + ((g >> (adjustG - 1)) % 2));
+ }
if (g > 255) {
g = 255;
} else if (g < 0) {
b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
- b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
+ if (adjustB > 0) {
+ b = ((b >> adjustB) + ((b >> (adjustB - 1)) % 2));
+ }
if (b > 255) {
b = 255;
} else if (b < 0) {
r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
+ if (adjustR > 0) {
+ r = ((r >> adjustR) + ((r >> (adjustR - 1)) % 2));
+ }
if (r > 255) {
r = 255;
} else if (r < 0) {