Skip to content

Commit

Permalink
Improve oslWriteImageFile function for better safety and extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Aug 19, 2024
1 parent 77d3cd0 commit 57b761f
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/Special/oslWriteImageFile.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#include "../oslib.h"

//Generic, includes support for all image types!
// Generic, includes support for all image types!
int oslWriteImageFile(OSL_IMAGE *img, const char* filename, int flags)
{
const char *ext;
char extension[10];
int i;
// Check if the image is swizzled; writing swizzled images is not supported
if (oslImageIsSwizzled(img))
return 0;

//Impossible to write swizzled images
if (oslImageIsSwizzled(img))
return 0;
ext = strrchr(filename, '.');
if (!ext)
return 0;
i = 0;
while(ext[i] && i < sizeof(extension) - 2)
{
extension[i] = tolower((unsigned char) ext[i]);
i++;
}
extension[i] = 0;
if (!strcmp(extension, ".png"))
return oslWriteImageFilePNG(img, filename, flags);
return 0;
}
const char *ext = strrchr(filename, '.');
if (!ext || ext == filename)
return 0;

char extension[10] = {0};
int i = 0;
while (ext[i] && i < sizeof(extension) - 1) // Leave space for the null terminator
{
extension[i] = tolower((unsigned char) ext[i]);
i++;
}

if (strcmp(extension, ".png") == 0)
return oslWriteImageFilePNG(img, filename, flags);

return 0;
}

0 comments on commit 57b761f

Please sign in to comment.