diff options
Diffstat (limited to 'src/bin')
| -rw-r--r-- | src/bin/jp2/convertbmp.c | 42 | ||||
| -rw-r--r-- | src/bin/jpwl/convert.c | 41 |
2 files changed, 48 insertions, 35 deletions
diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index 0af52f81..2fc4e9bc 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -622,31 +622,38 @@ static OPJ_BOOL bmp_read_rle8_data(FILE* IN, OPJ_UINT8* pData, static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, OPJ_UINT32 stride, OPJ_UINT32 width, OPJ_UINT32 height) { - OPJ_UINT32 x, y; + OPJ_UINT32 x, y, written; OPJ_UINT8 *pix; const OPJ_UINT8 *beyond; beyond = pData + stride * height; pix = pData; - x = y = 0U; + x = y = written = 0U; while (y < height) { int c = getc(IN); if (c == EOF) { - break; + return OPJ_FALSE; } if (c) { /* encoded mode */ - 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++) { *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU)); + written++; } } else { /* absolute mode */ c = getc(IN); if (c == EOF) { - break; + return OPJ_FALSE; } if (c == 0x00) { /* EOL */ @@ -657,8 +664,14 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, 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 : absolute mode */ @@ -668,16 +681,29 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, for (j = 0; (j < c) && (x < width) && ((OPJ_SIZE_T)pix < (OPJ_SIZE_T)beyond); j++, x++, pix++) { if ((j & 1) == 0) { - c1 = (OPJ_UINT8)getc(IN); + int c1_int; + c1_int = getc(IN); + if (c1_int == EOF) { + return OPJ_FALSE; + } + c1 = (OPJ_UINT8)c1_int; } *pix = (OPJ_UINT8)((j & 1) ? (c1 & 0x0fU) : ((c1 >> 4) & 0x0fU)); + written++; } if (((c & 3) == 1) || ((c & 3) == 2)) { /* skip padding byte */ - getc(IN); + c = getc(IN); + if (c == EOF) { + return OPJ_FALSE; + } } } } } /* while(y < height) */ + if (written != width * height) { + fprintf(stderr, "warning, image's actual size does not match advertized one\n"); + return OPJ_FALSE; + } return OPJ_TRUE; } diff --git a/src/bin/jpwl/convert.c b/src/bin/jpwl/convert.c index a4aa8d04..4f636c17 100644 --- a/src/bin/jpwl/convert.c +++ b/src/bin/jpwl/convert.c @@ -99,10 +99,15 @@ struct tga_header { }; #endif /* INFORMATION_ONLY */ -/* Returns a ushort from a little-endian serialized value */ -static unsigned short get_tga_ushort(const unsigned char *data) +static unsigned short get_ushort(unsigned short val) { - return data[0] | (data[1] << 8); + +#ifdef OPJ_BIG_ENDIAN + return (((val & 0xff) << 8) + (val >> 8)); +#else + return (val); +#endif + } #define TGA_HEADER_SIZE 18 @@ -131,15 +136,15 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, id_len = (unsigned char)tga[0]; cmap_type = (unsigned char)tga[1]; image_type = (unsigned char)tga[2]; - cmap_index = get_tga_ushort(*(unsigned short*)(&tga[3])); - cmap_len = get_tga_ushort(*(unsigned short*)(&tga[5])); + cmap_index = get_ushort(*(unsigned short*)(&tga[3])); + cmap_len = get_ushort(*(unsigned short*)(&tga[5])); cmap_entry_size = (unsigned char)tga[7]; - x_origin = get_tga_ushort(*(unsigned short*)(&tga[8])); - y_origin = get_tga_ushort(*(unsigned short*)(&tga[10])); - image_w = get_tga_ushort(*(unsigned short*)(&tga[12])); - image_h = get_tga_ushort(*(unsigned short*)(&tga[14])); + x_origin = get_ushort(*(unsigned short*)(&tga[8])); + y_origin = get_ushort(*(unsigned short*)(&tga[10])); + image_w = get_ushort(*(unsigned short*)(&tga[12])); + image_h = get_ushort(*(unsigned short*)(&tga[14])); pixel_depth = (unsigned char)tga[16]; image_desc = (unsigned char)tga[17]; @@ -329,24 +334,6 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) color_space = CLRSPC_SRGB; } - /* If the declared file size is > 10 MB, check that the file is big */ - /* enough to avoid excessive memory allocations */ - if (image_height != 0 && image_width > 10000000 / image_height / numcomps) { - char ch; - OPJ_UINT64 expected_file_size = - (OPJ_UINT64)image_width * image_height * numcomps; - long curpos = ftell(f); - if (expected_file_size > (OPJ_UINT64)INT_MAX) { - expected_file_size = (OPJ_UINT64)INT_MAX; - } - fseek(f, (long)expected_file_size - 1, SEEK_SET); - if (fread(&ch, 1, 1, f) != 1) { - fclose(f); - return NULL; - } - fseek(f, curpos, SEEK_SET); - } - subsampling_dx = parameters->subsampling_dx; subsampling_dy = parameters->subsampling_dy; |
