A sub-function to Validate() that handles the palette. Returns 0 on EOF or * invalid palette, or nonzero on success. */
| 461 | * invalid palette, or nonzero on success. |
| 462 | */ |
| 463 | static int ValidateAndReadPalette(read_context * p_ctx) |
| 464 | { |
| 465 | uint32_t colors = UINT32_C(1) << p_ctx->info.bits; |
| 466 | uint32_t file_colors = p_ctx->info.colors; |
| 467 | |
| 468 | if(p_ctx->info.bits > 8) |
| 469 | return 1; |
| 470 | |
| 471 | if(file_colors > colors) return 0; |
| 472 | if(!file_colors) |
| 473 | file_colors = colors; |
| 474 | |
| 475 | /* Make sure we actually have space in the file for all the colors. */ |
| 476 | if(p_ctx->after_headers / BMP_COLOR_SIZE < file_colors) return 0; |
| 477 | |
| 478 | /* We always allocate a full palette even if the file only claims to |
| 479 | * contain a smaller number, so we don't have to check for out of bound |
| 480 | * color lookups. Not sure what the desired behavior is, but loading the |
| 481 | * image anyway and treating OOB colors as black seems ok to me. 0-fill so |
| 482 | * lookups beyond the file's palette get set to black. |
| 483 | */ |
| 484 | if(!(p_ctx->palette = (bmp_color *) |
| 485 | calloc(colors, sizeof(p_ctx->palette[0])))) return 0; |
| 486 | |
| 487 | if(!CanMakeLong(p_ctx->headers_size)) return 0; |
| 488 | if(fseek(p_ctx->fp, p_ctx->headers_size, SEEK_SET)) return 0; |
| 489 | if(!ReadPalette(p_ctx->palette, file_colors, p_ctx->fp)) return 0; |
| 490 | |
| 491 | return 1; |
| 492 | } |
| 493 | |
| 494 | /* Returns whether a non-negative integer is a power of 2. |
| 495 | */ |
no test coverage detected