Skip to content

Commit 2d1c382

Browse files
authored
Simplify WBMP imagecreatefromstring() detection (GH-16782)
According to the WBMP specification[1], the first field (type) of a WBMP is a multi-byte integer, but only type `0` is supported. Thus there is no need to read a multi-byte integer. The second field (fix header) is a single byte; reading a multi-byte integer is not really wrong, since the fix header field is laid out in a way which allows it to be treated as such, but the check whether the MBI is greater than or equal to zero is pretty useless, because negative values could only be returned if overflow occurs (MBIs are unsigned). So the only useful assumption we can make is that the first byte is zero; we let `gdImageCreateFromWBMPCtx()` figure out the rest. [1] <https://www.wapforum.org/what/technical/SPEC-WAESpec-19990524.pdf> section 6
1 parent d4103b3 commit 2d1c382

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

ext/gd/gd.c

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,24 +1358,6 @@ PHP_FUNCTION(imagetypes)
13581358
}
13591359
/* }}} */
13601360

1361-
/* {{{ _php_ctx_getmbi */
1362-
1363-
static int _php_ctx_getmbi(gdIOCtx *ctx)
1364-
{
1365-
int i, mbi = 0;
1366-
1367-
do {
1368-
i = (ctx->getC)(ctx);
1369-
if (i < 0 || mbi > (INT_MAX >> 7)) {
1370-
return -1;
1371-
}
1372-
mbi = (mbi << 7) | (i & 0x7f);
1373-
} while (i & 0x80);
1374-
1375-
return mbi;
1376-
}
1377-
/* }}} */
1378-
13791361
/* {{{ _php_image_type
13801362
* Based on ext/standard/image.c
13811363
*/
@@ -1413,15 +1395,8 @@ static int _php_image_type(zend_string *data)
14131395
}
14141396
}
14151397

1416-
gdIOCtx *io_ctx;
1417-
io_ctx = gdNewDynamicCtxEx(8, ZSTR_VAL(data), 0);
1418-
if (io_ctx) {
1419-
if (_php_ctx_getmbi(io_ctx) == 0 && _php_ctx_getmbi(io_ctx) >= 0) {
1420-
io_ctx->gd_free(io_ctx);
1421-
return PHP_GDIMG_TYPE_WBM;
1422-
} else {
1423-
io_ctx->gd_free(io_ctx);
1424-
}
1398+
if (ZSTR_VAL(data)[0] == 0) {
1399+
return PHP_GDIMG_TYPE_WBM;
14251400
}
14261401

14271402
return -1;
4.89 KB
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
imagecreatefromstring() - WBMP format
3+
--EXTENSIONS--
4+
gd
5+
--SKIPIF--
6+
<?php
7+
if (!(imagetypes() & IMG_WBMP)) die('skip GIF support required');
8+
?>
9+
--FILE--
10+
<?php
11+
// create an image from a WBMP string representation
12+
$im = imagecreatefromstring(file_get_contents(__DIR__ . '/imagecreatefromstring.wbmp'));
13+
var_dump(imagesx($im));
14+
var_dump(imagesy($im));
15+
16+
?>
17+
--EXPECT--
18+
int(200)
19+
int(200)

0 commit comments

Comments
 (0)