libpng错误:PNG无符号整数超出范围 [英] libpng error: PNG unsigned integer out of range

查看:3137
本文介绍了libpng错误:PNG无符号整数超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试从内存中读取PNG时,我遇到了这个有趣的错误:

  libpng错误:: PNG无符号整数的范围

此错误是由

  png_read_info(png_ptr,info_ptr); 

其中使用以下处理程序:

  static void ReadDataFromBuffer(png_structp png_ptr,png_bytep outBytes,
png_size_t byteCountToRead){
PNGDataPtr dataptr =(PNGDataPtr)png_get_io_ptr(png_ptr);
png_uint_32 i;
cout<< byteCountToRead<< endl;
cout<<& outBytes<< endl;
cout<< dataptr-> len<<< endl;
cout<< dataptr-> p<< endl;

if(byteCountToRead> dataptr-> len){
png_error(png_ptr,EOF);
return;
}

for(i = 0; i
outBytes [i] = dataptr-> p [i]
}
dataptr-> p + = byteCountToRead;
dataptr-> len- = byteCountToRead;
png_uint_32 a = png_get_uint_32(outBytes);
cout<< a<<<<< PNG_UINT_31_MAX<< endl;

}



通过一些密集的搜索我发现错误被调用
中的以下代码pgnrutil.c:

  png_uint_32 / * PRIVATE * / 
png_get_uint_31(png_structp png_ptr ,png_bytep buf)
{
png_uint_32 i = png_get_uint_32(buf);
if(i> PNG_UINT_31_MAX)
png_error(png_ptr,PNG unsigned integer out of range.\\\
);
return(i);
}


$ b $ p因此我检查了png_get_uint_32(outBytes)的值, PNG_UINT_31_MAX,230374511。



如何解决此问题?



编辑:



有关PNGDataPtr的一些说明:

  typedef struct {
png_bytep p;
png_uint_32 len;
} PNGData,* PNGDataPtr;

调用



  png_set_read_fn(png_ptr,(png_voidp)& pngdata,ReadDataFromBuffer); 

pngdata表示一个PNGData对象,它有一个指向我的databuffer的指针,和len包含整个PNG的大小,在40到80KB之间



编辑:这里是一个链接到一个png我使用fwrite保存接收的databuffer : https://www.dropbox.com/s/m5enp9jljglhs5c/test.png

解决方案

以前我写的:
你可能有你的endian条件混合...



编辑:

当您跳过前8个字节时,您正在阅读PNG签名。您的报告有一个拼写错误:该数字实际上是2303741511(您从您的问题中省略了1),它是字节字符串\211 PNG



对于此问题,您的代码是使用

  png_set_sig_bytes(png_ptr,0)



通知libpng您的数据指针指向8字节签名的开头,读者需要跳过它们。


When trying to read a PNG from memory I came across this funky error:

libpng error:: PNG unsigned integer out of range

This error is caused by

png_read_info(png_ptr,info_ptr);

Which uses following handler:

static void ReadDataFromBuffer(png_structp png_ptr, png_bytep outBytes,
    png_size_t byteCountToRead){
        PNGDataPtr dataptr=(PNGDataPtr)png_get_io_ptr(png_ptr);
        png_uint_32 i;
        cout<<byteCountToRead<<endl;
        cout<<&outBytes<<endl;
        cout<<dataptr->len<<endl;
        cout<<dataptr->p<<endl;

        if(byteCountToRead>dataptr->len){
            png_error(png_ptr,"EOF");
            return;
        }

        for(i=0;i<byteCountToRead;i++){

            outBytes[i]=dataptr->p[i];
        }
        dataptr->p+=byteCountToRead;
        dataptr->len-=byteCountToRead;
        png_uint_32 a = png_get_uint_32(outBytes);
        cout<<a<<" "<<PNG_UINT_31_MAX<<endl;

}

Through some intensive searching I found that the error gets called by the following code in pgnrutil.c:

png_uint_32 /* PRIVATE */
png_get_uint_31(png_structp png_ptr, png_bytep buf)
{
    png_uint_32 i = png_get_uint_32(buf);
    if (i > PNG_UINT_31_MAX)
    png_error(png_ptr, "PNG unsigned integer out of range.\n");
    return (i);
}

So I checked the value of png_get_uint_32(outBytes) and it was indeed higher than PNG_UINT_31_MAX, 230374511 to be exact.

How do I fix this?

Edit:

Some clarification on the PNGDataPtr:

typedef struct{
    png_bytep p;
    png_uint_32 len;
} PNGData,*PNGDataPtr;

When calling

png_set_read_fn(png_ptr,(png_voidp) &pngdata, ReadDataFromBuffer);

pngdata represents a PNGData object, which has a pointer to my databuffer which contains the entire PNG in memory, and len contains the size of the entire PNG, which is between 40 and 80KB

Edit: Here's a link to a png I get when I use fwrite to save the received databuffer: https://www.dropbox.com/s/m5enp9jljglhs5c/test.png

解决方案

Previously I wrote: You've probably got your endian condition mixed up...

EDIT:
You are reading the PNG signature when you should have skipped past the first 8 bytes. Your report has a typo: the number is actually 2303741511 (you omitted a "1" from your question) which is the byte string "\211 P N G"

A workaround for this problem in your code is to use

png_set_sig_bytes(png_ptr,0)

to inform libpng that your data pointer is pointing to the beginning of the 8-byte signature and the reader needs to skip them.

这篇关于libpng错误:PNG无符号整数超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆