Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 54 additions & 47 deletions Source/Plugins/PluginXBM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
// Internal functions
// ==========================================================

#define MAX_LINE 512
#define MAX_LINE 512
#define LINE_FORMAT_WIDTH_OR_HEIGHT "#define %" FI_QUOTE(MAX_LINE) "s %d"
#define LINE_FORMAT_BITS_V10 "static short %" FI_QUOTE(MAX_LINE) "s = {"
#define LINE_FORMAT_BITS_V11_OPT1 "static char %" FI_QUOTE(MAX_LINE) "s = {"
#define LINE_FORMAT_BITS_V11_OPT2 "static unsigned char %" FI_QUOTE(MAX_LINE) "s = {"

static const char *ERR_XBM_SYNTAX = "Syntax error";
static const char *ERR_XBM_LINE = "Line too long";
Expand Down Expand Up @@ -87,76 +91,79 @@ Read an XBM file into a buffer
*/
static const char*
readXBMFile(FreeImageIO *io, fi_handle handle, int *widthP, int *heightP, std::unique_ptr<void, decltype(&free)> &dataP) {
char line[MAX_LINE], name_and_type[MAX_LINE];
char *ptr{};
char *t{};
std::string line(MAX_LINE, '\0'), name(MAX_LINE + 1, '\0');
char *ptr{};
int version = 0;
size_t bytes, bytes_per_line, raster_length;
int v, padding;
int c1, c2, value1, value2;
int hex_table[256];
bool found_declaration{}; // haven't found it yet; haven't even looked
bool found_declaration{}; // haven't found it yet; haven't even looked
/* in scanning through the bitmap file, we have found the first
line of the C declaration of the array (the "static char ..."
or whatever line)
line of the C declaration of the array (the "static char ..." or whatever line)
*/
bool eof{}; // we've encountered end of file while searching file

*widthP = *heightP = -1;

while (!found_declaration && !eof) {
for (;;) {
if (!readLine(line.data(), MAX_LINE, io, handle)) {
break;
}

if (!readLine(line, MAX_LINE, io, handle)) {
eof = true;
if (strlen(line.c_str()) >= MAX_LINE - 1) {
return ERR_XBM_LINE;
}
else {
if (strlen(line) == MAX_LINE - 1)
return( ERR_XBM_LINE );
if (sscanf_s(line, "#define %s %d", name_and_type, MAX_LINE, &v) == 2) {
if ((t = strrchr(name_and_type, '_')) == nullptr)
t = name_and_type;
else
t++;
if (!strcmp("width", t))
*widthP = v;
else if (!strcmp("height", t))
*heightP = v;
continue;
}

if (sscanf_s(line, "static short %s = {", name_and_type, MAX_LINE) == 1) {
version = 10;
found_declaration = true;
}
else if (sscanf_s(line, "static char %s = {", name_and_type, MAX_LINE) == 1) {
version = 11;
found_declaration = true;
}
else if (sscanf_s(line, "static unsigned char %s = {", name_and_type, MAX_LINE) == 1) {
version = 11;
found_declaration = true;
int val{};
if (2 == std::sscanf(line.c_str(), LINE_FORMAT_WIDTH_OR_HEIGHT, name.data(), &val)) {
if (const auto suffix = std::strrchr(name.c_str(), '_')) {
if (0 == std::strcmp("_width", suffix)) {
*widthP = val;
continue;
}
if (0 == std::strcmp("_height", suffix)) {
*heightP = val;
continue;
}
}
}

if (1 == std::sscanf(line.c_str(), LINE_FORMAT_BITS_V10, name.data())) {
version = 10;
found_declaration = true;
break;
}

if (1 == std::sscanf(line.c_str(), LINE_FORMAT_BITS_V11_OPT1, name.data()) ||
1 == std::sscanf(line.c_str(), LINE_FORMAT_BITS_V11_OPT2, name.data())) {
version = 11;
found_declaration = true;
break;
}
}

if (!found_declaration)
return( ERR_XBM_DECL );
if (!found_declaration) {
return ERR_XBM_DECL;
}

if (*widthP == -1)
return( ERR_XBM_WIDTH );
if (*heightP == -1)
return( ERR_XBM_HEIGHT );
if (*widthP <= 0) {
return ERR_XBM_WIDTH;
}
if (*heightP <= 0) {
return ERR_XBM_HEIGHT;
}

padding = 0;
if ( ((*widthP % 16) >= 1) && ((*widthP % 16) <= 8) && (version == 10) )
int padding = 0;
if (((*widthP % 16) >= 1) && ((*widthP % 16) <= 8) && (version == 10)) {
padding = 1;
}

bytes_per_line = (*widthP + 7) / 8 + padding;

raster_length = bytes_per_line * *heightP;
dataP.reset(malloc(raster_length));
if (!dataP)
return( ERR_XBM_MEMORY );
if (!dataP) {
return ERR_XBM_MEMORY;
}

// initialize hex_table
for ( c1 = 0; c1 < 256; c1++ ) {
Expand Down
Loading