Improve handling oof corrupted PNG images (resolves openfl/openfl#1999)

This commit is contained in:
Joshua Granick
2018-10-10 14:57:27 -07:00
parent 080666c95b
commit a5bef44d65
2 changed files with 32 additions and 9 deletions

View File

@@ -21,9 +21,9 @@ namespace lime {
ReadBuffer (const unsigned char* data, int length) : data (data), length (length), position (0) {}
void Read (unsigned char* out, int count) {
bool Read (unsigned char* out, int count) {
if (position >= length) return;
if (position >= length) return false;
if (count > length - position) {
@@ -37,6 +37,8 @@ namespace lime {
}
return true;
}
char unused; // the first byte gets corrupted when passed to libpng?
@@ -57,7 +59,9 @@ namespace lime {
static void user_read_data_fn (png_structp png_ptr, png_bytep data, png_size_t length) {
ReadBuffer* buffer = (ReadBuffer*)png_get_io_ptr (png_ptr);
buffer->Read (data, length);
if (!buffer->Read (data, length)) {
png_error (png_ptr, "Read Error");
}
}

View File

@@ -724,8 +724,11 @@ class Image {
if (bytes == null) return null;
var image = new Image ();
image.__fromBytes (bytes);
return image;
if (image.__fromBytes (bytes)) {
return image;
} else {
return null;
}
}
@@ -768,8 +771,11 @@ class Image {
if (path == null) return null;
var image = new Image ();
image.__fromFile (path);
return image;
if (image.__fromFile (path)) {
return image;
} else {
return null;
}
}
@@ -1694,7 +1700,7 @@ class Image {
}
@:noCompletion private function __fromBytes (bytes:Bytes, onload:Image->Void = null):Void {
@:noCompletion private function __fromBytes (bytes:Bytes, onload:Image->Void = null):Bool {
#if (js && html5)
@@ -1720,6 +1726,7 @@ class Image {
}
__fromBase64 (__base64Encode (bytes), type, onload);
return true;
#elseif (lime_cffi && !macro)
@@ -1744,6 +1751,8 @@ class Image {
}
return true;
}
#else
@@ -1752,10 +1761,12 @@ class Image {
#end
return false;
}
@:noCompletion private function __fromFile (path:String, onload:Image->Void = null, onerror:Void->Void = null):Void {
@:noCompletion private function __fromFile (path:String, onload:Image->Void = null, onerror:Void->Void = null):Bool {
#if (kha && !macro)
@@ -1798,6 +1809,8 @@ class Image {
}
return true;
}
} catch (e:Dynamic) {}
@@ -1852,6 +1865,8 @@ class Image {
// (issue #1019768)
if (image.complete) { }
return true;
#elseif (lime_cffi || java)
var buffer:ImageBuffer = null;
@@ -1921,6 +1936,8 @@ class Image {
}
return true;
}
#else
@@ -1929,6 +1946,8 @@ class Image {
#end
return false;
}