Merge pull request #166 from mcuelenaere/no-custom-image-header-checking

Let libvips check whether we received a valid image or not
This commit is contained in:
Lovell Fuller 2015-03-01 15:41:37 +00:00
commit 5240eeb518
3 changed files with 35 additions and 63 deletions

View File

@ -73,24 +73,7 @@ var Sharp = function(input) {
this.options.fileIn = input;
} else if (typeof input === 'object' && input instanceof Buffer) {
// input=buffer
if (
(input.length > 3) &&
// JPEG
(input[0] === 0xFF && input[1] === 0xD8) ||
// PNG
(input[0] === 0x89 && input[1] === 0x50) ||
// WebP
(input[0] === 0x52 && input[1] === 0x49) ||
// TIFF
(input[0] === 0x4D && input[1] === 0x4D && input[2] === 0x00 && (input[3] === 0x2A || input[3] === 0x2B)) ||
(input[0] === 0x49 && input[1] === 0x49 && (input[2] === 0x2A || input[2] === 0x2B) && input[3] === 0x00) ||
// GIF
(input[0] === 0x47 && input[1] === 0x49 && input[2] === 0x46 && input[3] === 0x38 && (input[4] === 0x37 || input[4] === 0x39) && input[5] === 0x61)
) {
this.options.bufferIn = input;
} else {
throw new Error('Buffer contains an unsupported image format. JPEG, PNG, WebP, TIFF and GIF are currently supported.');
}
this.options.bufferIn = input;
} else {
// input=stream
this.options.streamIn = true;

View File

@ -29,47 +29,40 @@ namespace sharp {
return EndsWith(str, ".tif") || EndsWith(str, ".tiff") || EndsWith(str, ".TIF") || EndsWith(str, ".TIFF");
}
// Buffer content checkers
unsigned char const MARKER_JPEG[] = {0xff, 0xd8};
unsigned char const MARKER_PNG[] = {0x89, 0x50};
unsigned char const MARKER_WEBP[] = {0x52, 0x49};
static bool buffer_is_tiff(char *buffer, size_t len) {
return (
len >= 4 && (
(buffer[0] == 'M' && buffer[1] == 'M' && buffer[2] == '\0' && (buffer[3] == '*' || buffer[3] == '+')) ||
(buffer[0] == 'I' && buffer[1] == 'I' && (buffer[2] == '*' || buffer[2] == '+') && buffer[3] == '\0')
)
);
}
static bool buffer_is_gif(char *buffer, size_t len) {
return (
len >= 6 && (
(buffer[0] == 'G' && buffer[1] == 'I' && buffer[2] == 'F' &&
buffer[3] == '8' && (buffer[4] == '7' || buffer[4] == '9') && buffer[5] == 'a')
)
);
}
/*
Determine image format of a buffer.
*/
ImageType DetermineImageType(void *buffer, size_t const length) {
ImageType imageType = ImageType::UNKNOWN;
if (length >= 4) {
if (memcmp(MARKER_JPEG, buffer, 2) == 0) {
#if (VIPS_MAJOR_VERSION >= 8)
if (vips_foreign_is_a_buffer("jpegload_buffer", buffer, length)) {
imageType = ImageType::JPEG;
} else if (vips_foreign_is_a_buffer("pngload_buffer", buffer, length)) {
imageType = ImageType::PNG;
} else if (vips_foreign_is_a_buffer("webpload_buffer", buffer, length)) {
imageType = ImageType::WEBP;
} else if (vips_foreign_is_a_buffer("tiffload_buffer", buffer, length)) {
imageType = ImageType::TIFF;
} else if(vips_foreign_is_a_buffer("magickload_buffer", buffer, length)) {
imageType = ImageType::MAGICK;
}
#else
const char* loader = vips_foreign_find_load_buffer(buffer, length);
if (loader != NULL) {
if (!strcmp(loader, "VipsForeignLoadJpegBuffer")) {
imageType = ImageType::JPEG;
} else if (memcmp(MARKER_PNG, buffer, 2) == 0) {
} else if (!strcmp(loader, "VipsForeignLoadPngBuffer")) {
imageType = ImageType::PNG;
} else if (memcmp(MARKER_WEBP, buffer, 2) == 0) {
} else if (!strcmp(loader, "VipsForeignLoadWebpBuffer")) {
imageType = ImageType::WEBP;
} else if (buffer_is_tiff(static_cast<char*>(buffer), length)) {
} else if (!strcmp(loader, "VipsForeignLoadTiffBuffer")) {
imageType = ImageType::TIFF;
} else if (buffer_is_gif(static_cast<char*>(buffer), length)) {
} else if (!strcmp(loader, "VipsForeignLoadMagickBuffer")) {
imageType = ImageType::MAGICK;
}
}
#endif
return imageType;
}

View File

@ -202,27 +202,23 @@ describe('Input/output', function() {
});
it('Fail when input is empty Buffer', function(done) {
var failed = true;
try {
sharp(new Buffer(0));
failed = false;
} catch (err) {
sharp(new Buffer(0)).toBuffer().then(function () {
assert(false);
done();
}).catch(function (err) {
assert(err instanceof Error);
}
assert(failed);
done();
done();
});
});
it('Fail when input is invalid Buffer', function(done) {
var failed = true;
try {
sharp(new Buffer([0x1, 0x2, 0x3, 0x4]));
failed = false;
} catch (err) {
sharp(new Buffer([0x1, 0x2, 0x3, 0x4])).toBuffer().then(function () {
assert(false);
done();
}).catch(function (err) {
assert(err instanceof Error);
}
assert(failed);
done();
done();
});
});
it('Promises/A+', function(done) {