Changed to check for PNG signature

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4082 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber
2003-07-26 02:40:14 +00:00
parent 47abf6ebf0
commit 010b9fa36b
@@ -410,11 +410,17 @@ identify_bits_header(BPositionIO *inSource, translator_info *outInfo,
status_t status_t
identify_png_header(BPositionIO *inSource, BMessage *ioExtension, identify_png_header(BPositionIO *inSource, BMessage *ioExtension,
translator_info *outInfo, uint32 outType) translator_info *outInfo, uint32 outType, ssize_t amtread, uint8 *read)
{ {
// Can only output to bits for now // Can only output to bits for now
if (outType != B_TRANSLATOR_BITMAP) if (outType != B_TRANSLATOR_BITMAP)
return B_NO_TRANSLATOR; return B_NO_TRANSLATOR;
if (amtread != 8)
return B_ERROR;
if (!png_check_sig(read, amtread))
// if first 8 bytes of stream don't match PNG signature bail
return B_NO_TRANSLATOR;
if (outInfo) { if (outInfo) {
outInfo->type = B_PNG_FORMAT; outInfo->type = B_PNG_FORMAT;
@@ -425,8 +431,6 @@ identify_png_header(BPositionIO *inSource, BMessage *ioExtension,
strcpy(outInfo->name, "PNG image"); strcpy(outInfo->name, "PNG image");
} }
// LAZY HACK: everything that is not bits is identified as PNG
return B_OK; return B_OK;
} }
@@ -489,7 +493,7 @@ PNGTranslator::Identify(BPositionIO *inSource,
// Read in the magic number and determine if it // Read in the magic number and determine if it
// is a supported type // is a supported type
uint8 ch[4]; uint8 ch[8];
if (inSource->Read(ch, 4) != 4) if (inSource->Read(ch, 4) != 4)
return B_NO_TRANSLATOR; return B_NO_TRANSLATOR;
@@ -517,19 +521,21 @@ PNGTranslator::Identify(BPositionIO *inSource,
// Might be PNG image // Might be PNG image
else { else {
if (inSource->Read(ch + 4, 4) != 4)
return identify_png_header(inSource, ioExtension, outInfo, outType); return B_NO_TRANSLATOR;
return identify_png_header(inSource, ioExtension, outInfo, outType, 8, ch);
} }
} }
status_t status_t
translate_from_png(BPositionIO *inSource, BMessage *ioExtension, translate_from_png(BPositionIO *inSource, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination, bool bheaderonly, uint32 outType, BPositionIO *outDestination, ssize_t amtread, uint8 *read,
bool bdataonly) bool bheaderonly, bool bdataonly)
{ {
status_t result = B_NO_TRANSLATOR; if (amtread != 8)
return B_ERROR;
inSource->Seek(0, SEEK_SET); status_t result = B_NO_TRANSLATOR;
png_structp ppng = NULL; png_structp ppng = NULL;
png_infop pinfo = NULL; png_infop pinfo = NULL;
@@ -556,6 +562,7 @@ translate_from_png(BPositionIO *inSource, BMessage *ioExtension,
png_set_read_fn(ppng, static_cast<void *>(inSource), pngcb_read_data); png_set_read_fn(ppng, static_cast<void *>(inSource), pngcb_read_data);
// Read in PNG image info // Read in PNG image info
png_set_sig_bytes(ppng, amtread);
png_read_info(ppng, pinfo); png_read_info(ppng, pinfo);
png_uint_32 width, height; png_uint_32 width, height;
@@ -724,11 +731,9 @@ translate_from_png(BPositionIO *inSource, BMessage *ioExtension,
// B_OK, if all went well // B_OK, if all went well
// --------------------------------------------------------------- // ---------------------------------------------------------------
status_t status_t
PNGTranslator::Translate(BPositionIO *inSource, PNGTranslator::Translate(BPositionIO *inSource, const translator_info *inInfo,
const translator_info *inInfo, BMessage *ioExtension, BMessage *ioExtension, uint32 outType, BPositionIO *outDestination)
uint32 outType, BPositionIO *outDestination)
{ {
if (!outType) if (!outType)
outType = B_TRANSLATOR_BITMAP; outType = B_TRANSLATOR_BITMAP;
if (outType != B_TRANSLATOR_BITMAP && outType != B_PNG_FORMAT) if (outType != B_TRANSLATOR_BITMAP && outType != B_PNG_FORMAT)
@@ -744,7 +749,7 @@ PNGTranslator::Translate(BPositionIO *inSource,
// Read in the magic number and determine if it // Read in the magic number and determine if it
// is a supported type // is a supported type
uint8 ch[4]; uint8 ch[8];
inSource->Seek(0, SEEK_SET); inSource->Seek(0, SEEK_SET);
if (inSource->Read(ch, 4) != 4) if (inSource->Read(ch, 4) != 4)
return B_NO_TRANSLATOR; return B_NO_TRANSLATOR;
@@ -769,7 +774,7 @@ PNGTranslator::Translate(BPositionIO *inSource,
memcpy(&n32ch, ch, sizeof(uint32)); memcpy(&n32ch, ch, sizeof(uint32));
if (n32ch == nbits) { if (n32ch == nbits) {
// B_TRANSLATOR_BITMAP type // B_TRANSLATOR_BITMAP type
inSource->Seek(0, SEEK_SET); outDestination->Write(ch, 4);
const size_t kbufsize = 2048; const size_t kbufsize = 2048;
uint8 buffer[kbufsize]; uint8 buffer[kbufsize];
@@ -781,10 +786,16 @@ PNGTranslator::Translate(BPositionIO *inSource,
return B_OK; return B_OK;
} else } else {
// Might be PNG image // Might be PNG image
if (inSource->Read(ch + 4, 4) != 4)
return B_NO_TRANSLATOR;
if (!png_check_sig(ch, 8))
return B_NO_TRANSLATOR;
return translate_from_png(inSource, ioExtension, outType, return translate_from_png(inSource, ioExtension, outType,
outDestination, bheaderonly, bdataonly); outDestination, 8, ch, bheaderonly, bdataonly);
}
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------