cleanup error-handling in SDL_bmp.c

- add missing error-message in SDL_LoadBMP_RW
- check return value of SDL_RWtell in SDL_LoadBMP_RW
- use standard SDL_EFREAD error instead of custom strings
+ adjust return type of readRlePixels
This commit is contained in:
pionere 2022-01-22 15:34:22 +01:00 committed by Ryan C. Gordon
parent 19df4af2aa
commit b5c5052608
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 18 additions and 13 deletions

View File

@ -53,7 +53,7 @@
#define LCS_WINDOWS_COLOR_SPACE 0x57696E20 #define LCS_WINDOWS_COLOR_SPACE 0x57696E20
#endif #endif
static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8) static SDL_bool readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
{ {
/* /*
| Sets the surface pixels from src. A bmp image is upside down. | Sets the surface pixels from src. A bmp image is upside down.
@ -70,14 +70,14 @@ static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
#define COPY_PIXEL(x) spot = &bits[ofs++]; if(spot >= start && spot < end) *spot = (x) #define COPY_PIXEL(x) spot = &bits[ofs++]; if(spot >= start && spot < end) *spot = (x)
for (;;) { for (;;) {
if (!SDL_RWread(src, &ch, 1, 1)) return 1; if (!SDL_RWread(src, &ch, 1, 1)) return SDL_TRUE;
/* /*
| encoded mode starts with a run length, and then a byte | encoded mode starts with a run length, and then a byte
| with two colour indexes to alternate between for the run | with two colour indexes to alternate between for the run
*/ */
if (ch) { if (ch) {
Uint8 pixel; Uint8 pixel;
if (!SDL_RWread(src, &pixel, 1, 1)) return 1; if (!SDL_RWread(src, &pixel, 1, 1)) return SDL_TRUE;
if (isRle8) { /* 256-color bitmap, compressed */ if (isRle8) { /* 256-color bitmap, compressed */
do { do {
COPY_PIXEL(pixel); COPY_PIXEL(pixel);
@ -98,18 +98,18 @@ static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
| a cursor move, or some absolute data. | a cursor move, or some absolute data.
| zero tag may be absolute mode or an escape | zero tag may be absolute mode or an escape
*/ */
if (!SDL_RWread(src, &ch, 1, 1)) return 1; if (!SDL_RWread(src, &ch, 1, 1)) return SDL_TRUE;
switch (ch) { switch (ch) {
case 0: /* end of line */ case 0: /* end of line */
ofs = 0; ofs = 0;
bits -= pitch; /* go to previous */ bits -= pitch; /* go to previous */
break; break;
case 1: /* end of bitmap */ case 1: /* end of bitmap */
return 0; /* success! */ return SDL_FALSE; /* success! */
case 2: /* delta */ case 2: /* delta */
if (!SDL_RWread(src, &ch, 1, 1)) return 1; if (!SDL_RWread(src, &ch, 1, 1)) return SDL_TRUE;
ofs += ch; ofs += ch;
if (!SDL_RWread(src, &ch, 1, 1)) return 1; if (!SDL_RWread(src, &ch, 1, 1)) return SDL_TRUE;
bits -= (ch * pitch); bits -= (ch * pitch);
break; break;
default: /* no compression */ default: /* no compression */
@ -117,14 +117,14 @@ static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
needsPad = (ch & 1); needsPad = (ch & 1);
do { do {
Uint8 pixel; Uint8 pixel;
if (!SDL_RWread(src, &pixel, 1, 1)) return 1; if (!SDL_RWread(src, &pixel, 1, 1)) return SDL_TRUE;
COPY_PIXEL(pixel); COPY_PIXEL(pixel);
} while (--ch); } while (--ch);
} else { } else {
needsPad = (((ch+1)>>1) & 1); /* (ch+1)>>1: bytes size */ needsPad = (((ch+1)>>1) & 1); /* (ch+1)>>1: bytes size */
for (;;) { for (;;) {
Uint8 pixel; Uint8 pixel;
if (!SDL_RWread(src, &pixel, 1, 1)) return 1; if (!SDL_RWread(src, &pixel, 1, 1)) return SDL_TRUE;
COPY_PIXEL(pixel >> 4); COPY_PIXEL(pixel >> 4);
if (!--ch) break; if (!--ch) break;
COPY_PIXEL(pixel & 0x0F); COPY_PIXEL(pixel & 0x0F);
@ -132,7 +132,7 @@ static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
} }
} }
/* pad at even boundary */ /* pad at even boundary */
if (needsPad && !SDL_RWread(src, &ch, 1, 1)) return 1; if (needsPad && !SDL_RWread(src, &ch, 1, 1)) return SDL_TRUE;
break; break;
} }
} }
@ -213,12 +213,17 @@ SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
surface = NULL; surface = NULL;
was_error = SDL_FALSE; was_error = SDL_FALSE;
if (src == NULL) { if (src == NULL) {
SDL_InvalidParamError("src");
was_error = SDL_TRUE; was_error = SDL_TRUE;
goto done; goto done;
} }
/* Read in the BMP file header */ /* Read in the BMP file header */
fp_offset = SDL_RWtell(src); fp_offset = SDL_RWtell(src);
if (fp_offset < 0) {
was_error = SDL_TRUE;
goto done;
}
SDL_ClearError(); SDL_ClearError();
if (SDL_RWread(src, magic, 1, 2) != 2) { if (SDL_RWread(src, magic, 1, 2) != 2) {
SDL_Error(SDL_EFREAD); SDL_Error(SDL_EFREAD);
@ -451,8 +456,8 @@ SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
goto done; goto done;
} }
if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) { if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
was_error = (SDL_bool)readRlePixels(surface, src, biCompression == BI_RLE8); was_error = readRlePixels(surface, src, biCompression == BI_RLE8);
if (was_error) SDL_SetError("Error reading from BMP"); if (was_error) SDL_Error(SDL_EFREAD);
goto done; goto done;
} }
top = (Uint8 *)surface->pixels; top = (Uint8 *)surface->pixels;
@ -484,7 +489,7 @@ SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
for (i = 0; i < surface->w; ++i) { for (i = 0; i < surface->w; ++i) {
if (i % (8 / ExpandBMP) == 0) { if (i % (8 / ExpandBMP) == 0) {
if (!SDL_RWread(src, &pixel, 1, 1)) { if (!SDL_RWread(src, &pixel, 1, 1)) {
SDL_SetError("Error reading from BMP"); SDL_Error(SDL_EFREAD);
was_error = SDL_TRUE; was_error = SDL_TRUE;
goto done; goto done;
} }