QR-Code

 view release on metacpan or  search on metacpan

include/qr/qr_img.h  view on Meta::CPAN

 * an <image> whose MIME type does not match its bytes renders as
 * nothing in some viewers.
 */

#define QR_IMG_UNKNOWN 0
#define QR_IMG_PNG     1
#define QR_IMG_JPEG    2
#define QR_IMG_SVG     3

/* Sniff the format. SVG is anything that opens with '<' once an
 * optional UTF-8 BOM and leading whitespace are skipped - the XML
 * declaration, a comment, or the root element itself all qualify. */
static int qr_img_sniff(const unsigned char *p, size_t n)
{
    static const unsigned char png_sig[8] =
        { 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A };
    size_t i = 0;

    if (n >= 8 && memcmp(p, png_sig, 8) == 0)
        return QR_IMG_PNG;

t/04-logo.t  view on Meta::CPAN

    @r = QR::Code::_sniff(jpeg_bytes(64, 48, progressive => 1));
    is_deeply(\@r, ['jpeg', 64, 48], 'progressive JPEG (SOF2)');

    @r = QR::Code::_sniff(jpeg_bytes(20, 40, app1 => 300));
    is_deeply(\@r, ['jpeg', 20, 40], 'SOF behind a fat APP1 segment');

    @r = QR::Code::_sniff('  <svg viewBox="0 0 10 10"/>');
    is($r[0], 'svg', 'markup sniffs as SVG');

    @r = QR::Code::_sniff("\xEF\xBB\xBF<?xml version=\"1.0\"?><svg/>");
    is($r[0], 'svg', 'BOM and XML declaration still sniff as SVG');

    eval { QR::Code::_sniff('BM' . 'x' x 20) };
    like($@, qr/neither PNG, JPEG nor SVG \(starts 42 4d 78 78\)/,
         'a BMP croaks naming the bytes it saw');

    eval { QR::Code::_sniff(substr(png_bytes(5, 5), 0, 12)) };
    like($@, qr/PNG bytes are truncated or malformed/,
         'a truncated PNG croaks');

    eval { QR::Code::_sniff("\xFF\xD8\xFF\xE0\x00\x04\x00\x00") };



( run in 0.727 second using v1.01-cache-2.11-cpan-8dfa8b56332 )