⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inflate.c

📁 vxworks MPC8541 BSP
💻 C
📖 第 1 页 / 共 5 页
字号:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
      z->state->sub.check.need += (ULONG)ZSTREAM_NEXT_BYTE(z) << 8;
      z->state->mode = CHECK1;
    case CHECK1:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
      z->state->sub.check.need += (ULONG)ZSTREAM_NEXT_BYTE(z);

      if (z->state->sub.check.was != z->state->sub.check.need)
      {
        z->state->mode = INF_BAD;
        z->msg = (char*)"incorrect data check";
        break;
      }

      Trace((stderr, "inflate: zlib check ok\n"));
      z->state->mode = INF_DONE;
    case INF_DONE:
      return Z_STREAM_END;
     case INF_BAD:
      return Z_DATA_ERROR;
    default:
      return Z_STREAM_ERROR;
  }
}

/* global variables */

/******************************************************************************
*
* inflate - inflate compressed code
*
* This routine inflates <nBytes> of data starting at address <src>.
* The inflated code is copied starting at address <dest>.
* Two sanity checks are performed on the data being decompressed.
* First, we look for a magic number at the start of the data to
* verify that it is really a compressed stream.
* Second, the entire data is optionally checksummed to verify its
* integrity. By default, the checksum is not verified in order to
* speed up the booting process. To turn on checksum verification,
* set the global variable `inflateCksum' to TRUE in the BSP.
*
* RETURNS: OK or ERROR.
*/
/*ZXR 10 ROS Inflate */

#ifndef VXWORKS

int main(int argc ,char ** argv)
{
    FILE * inFile;
    FILE * outFile;
    unsigned char  outBuf[OUTBUF_SIZE];
    unsigned char  inBuf [INBUF_SIZE];

    ZAR_STREAM        d_stream;
    unsigned short  checksum = 0;
    int             result,bytes;
    int             total_in_bytes;

    if (    argc != 3
        || (inFile = fopen (argv[1],"rb")) == NULL
        || (outFile= fopen (argv[2],"wb")) == NULL  )
    {
        printf ("\ninvalid arguments.\nusage :inflate inFile outFile\n");
        fcloseall();
        return Z_ERRNO;
    }

    if ( (result = inflateInit(&d_stream)) != Z_OK)
    {
        fcloseall();
        remove(argv[2]);
        inflateEnd(&d_stream);
        return Z_ERRNO;
    }

    d_stream.next_in    = inBuf;
    d_stream.avail_in   = 0;
    d_stream.next_out   = outBuf;
    d_stream.avail_out  = OUTBUF_SIZE;
    d_stream.total_in  = total_in_bytes = 0;

    while( result == Z_OK )
    {
        if (d_stream.avail_in==0)
        {
            #ifdef   VERBOSE_DEBUG
            printf(".");
            #endif
            d_stream.avail_in = fread (inBuf, 1, INBUF_SIZE , inFile);  /* return bytes */
            d_stream.next_in  = inBuf;
            checksum = z_cksum(checksum,d_stream.next_in,d_stream.avail_in,total_in_bytes &0x1);

            total_in_bytes += d_stream.avail_in;
            if( ferror(inFile) || d_stream.avail_in <= 0)
                result = Z_ERRNO;
            /* check the checksum */
        }

        switch( zinflate(&d_stream, 0) )
        {
            case Z_STREAM_END:
                result = Z_STREAM_END;
                break;

            case Z_STREAM_ERROR:
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
                result = Z_ERRNO;
                break;

            default:
                break;
        }

        if ( d_stream.avail_out < OUTBUF_SIZE )
        {
            fwrite (outBuf, 1, OUTBUF_SIZE-d_stream.avail_out, outFile);
            d_stream.next_out  = outBuf;
            d_stream.avail_out = OUTBUF_SIZE;

            if( ferror(outFile) )
                result = Z_ERRNO;
        }

    }  /* end while( ! feof(inFile) )*/

    /* check checksum */
    while(!feof(inFile) && !ferror(inFile))
    {   bytes = fread (inBuf, 1, INBUF_SIZE , inFile);
        checksum = z_cksum(checksum,inBuf,bytes,total_in_bytes &1);
        total_in_bytes += bytes;
    }
    checksum = (unsigned short)(checksum + total_in_bytes);

    fcloseall();

    if( result == Z_STREAM_END && checksum == 0xFFFF )
    {
        printf(" Inflation: %u ===> %u \n",
                total_in_bytes,(UINT)d_stream.total_out);
        result = Z_OK;
    }
    else
    {
        printf("inflate encounter error...");
        remove(argv[2]);
        result = Z_ERRNO;
    }

    inflateEnd(&d_stream);

    printf(" Maxium Memory : %u\n",maxMemAdr - (UINT)d_stream.z_mem_pool);
    return result;
}
#else  /* VXWORKS */
int inflate_mem(char* p_src_mem,UINT src_mem_size,char* p_dst_mem,UINT dst_mem_size)
{

    ZAR_STREAM      d_stream;
#if 0
    unsigned short  checksum = 0;
    int             bytes;
#endif
    int             result;
    int             total_in_bytes;

    if ( (result = inflateInit(&d_stream)) != Z_OK)
    {
        inflateEnd(&d_stream);
        return ERROR;
    }

    d_stream.next_in    = p_src_mem;
    d_stream.avail_in   = src_mem_size;
    d_stream.next_out   = p_dst_mem;
    d_stream.avail_out  = dst_mem_size;
    d_stream.total_in   = 0;
    total_in_bytes      = src_mem_size;

    result = zinflate(&d_stream, 0);

    if( result == Z_STREAM_END )
    {
    /*yuqiang changed on 2003-2-17:统一各产品启动打印信息*/
        /*(printf(" Inflation: %u ===> %u \n",
                total_in_bytes,(UINT)d_stream.total_out);*/
        result = OK;
    }
    else
    {
        printf("inflate encounter error...");
        result = ERROR;
    }

    inflateEnd(&d_stream);

    /*yuqiang changed on 2003-2-17:统一各产品启动打印信息*/
    /*printf(" Maxium Memory : %u\n",maxMemAdr - (UINT)d_stream.z_mem_pool);*/
    return result;
}


#endif


/*2002-1-4*/

/* global variables */
extern int z_seek_pointer(FILE*, char*, unsigned int *);
extern long int checkDestFile(FILE *destFile,struct stat * staBuf);
/*Extract the specified Img file(s) from the destination file
  Input: The number of the arguments: Num_Files
         the name of each argument:   FileName;
  Return: Success:1, Failure: 0
*/
#define INBUF_SIZE  512
#define OUT_BUF_SIZE 4096

int Extract_Inflate_File(int Num_Files, char **FileName)
{
    FILE * inFile;
    FILE * outFile;
    unsigned char  outBuf[OUT_BUF_SIZE];
    unsigned char  inBuf [INBUF_SIZE];

    ZAR_STREAM        d_stream;
    unsigned short  checksum = 0;
    int             result,bytes,i,len_srcfile;
    unsigned int    length = 1;
    int             total_in_bytes;

    result = 0;
    inFile = fopen (FileName[Num_Files-1],"rb");
    if (inFile == NULL )
    {
        printf ("\nCannot open the source file\n");
      /*  fclose(inFile);*/
        return Z_ERRNO;
    }

    for (i=2; i<Num_Files-1; i++)
    {   outFile = fopen(FileName[i],"wb");
        if (outFile == NULL )
        {
            printf ("\nCannot create the destinate file\n");
            fclose(inFile);
            return Z_ERRNO;
        }

        len_srcfile = z_seek_pointer(inFile, FileName[i], &length);
        if ((result = inflateInit(&d_stream)) != Z_OK||(len_srcfile==-1))
        {
            fclose(outFile);
            fclose(inFile);
            remove(FileName[i]);
            inflateEnd(&d_stream);
            return Z_ERRNO;
        }

        d_stream.next_in    = inBuf;
        d_stream.avail_in   = 0;
        d_stream.next_out   = outBuf;
        d_stream.avail_out  = OUT_BUF_SIZE;
        d_stream.total_in   = total_in_bytes = 0;

        while( (result == Z_OK)&&(total_in_bytes <= len_srcfile) )
        {
            if (d_stream.avail_in==0)
            {
                #ifdef   VERBOSE_DEBUG
                printf(".");
                #endif
                if ((len_srcfile-total_in_bytes)>=512)
                    d_stream.avail_in = fread (inBuf, 1, INBUF_SIZE , inFile);  /* return bytes */
                else
                    d_stream.avail_in = fread (inBuf, 1, len_srcfile-total_in_bytes, inFile);  /* return bytes */
                d_stream.next_in  = inBuf;
                checksum = z_cksum(checksum,d_stream.next_in,d_stream.avail_in,total_in_bytes &0x1);

                total_in_bytes += d_stream.avail_in;
                if( ferror(inFile) || d_stream.avail_in <= 0)
                    result = Z_ERRNO;
                /* check the checksum */
            }

            switch( zinflate(&d_stream, 0) )
            {
                case Z_STREAM_END:
                    result = Z_STREAM_END;
                    break;

                case Z_STREAM_ERROR:
                case Z_DATA_ERROR:
                case Z_MEM_ERROR:
                    result = Z_ERRNO;
                    break;

                default:
                    break;
            }

            if ( d_stream.avail_out < OUT_BUF_SIZE )
            {
                fwrite (outBuf, 1, OUT_BUF_SIZE - d_stream.avail_out, outFile);
                d_stream.next_out  = outBuf;
                d_stream.avail_out = OUT_BUF_SIZE;

                if( ferror(outFile) )
                    result = Z_ERRNO;
            }

        }  /* end while( (result == Z_OK)&&(total_in_bytes<len_srcfile) )*/

        /* check checksum */
        while((total_in_bytes<len_srcfile) && !ferror(inFile))
        {   bytes = fread (inBuf, 1, INBUF_SIZE , inFile);
            checksum = z_cksum(checksum,inBuf,bytes,total_in_bytes &1);
            total_in_bytes += bytes;
        }
        checksum = (unsigned short)(checksum + (unsigned short)total_in_bytes);

        fclose(outFile);

        if( result == Z_STREAM_END && checksum == 0xFFFF )
        {
            result = 1;
        }
        else
        {
            remove(FileName[i]);
            result = Z_ERRNO;
        }

        inflateEnd(&d_stream);
        checksum = 0;
    }/*End of for (i=2; i<Num_Files; i++)*/
    fclose(inFile);
    return result;
}

int Extract_Inflate_Files(int Num_Files, char **FileName)
{
    FILE * inFile;
    FILE * outFile;
    unsigned char  outBuf[OUT_BUF_SIZE];
    unsigned char  inBuf [INBUF_SIZE];

    ZAR_STREAM      d_stream;
    unsigned short  checksum = 0;
    int             result, bytes, total_len=0;
    int             total_in_bytes=0;
    unsigned int	len_imgfile, len_zarfile, len_srcfile = 1;
    char            file_name[16];
    struct  stat        File_Statbuf;

    result = 0;
    inFile = fopen (FileName[Num_Files-1],"rb");
    if (inFile == NULL )
    {
        printf ("\nCannot open the source file\n");
       /* fclose(inFile);*/
        return Z_ERRNO;
    }

    len_imgfile = checkDestFile(inFile,&File_Statbuf);
    if (len_imgfile <= 0)
    {   fclose(inFile);
        return Z_ERRNO;
    }
    while ((unsigned int)total_len < (len_imgfile-1))
    {   fread(file_name, 1, 16, inFile);
        outFile = fopen(file_name,"wb");
        if (outFile == NULL )
        {
            printf ("\nCannot create the destinate file\n");
            fclose(inFile);
            return Z_ERRNO;
        }
        fseek(inFile, 0, SEEK_SET);
        len_zarfile = z_seek_pointer(inFile, file_name, &len_srcfile);
        if(len_srcfile != 0)    /* zar file */
        {
            if ( (result = inflateInit(&d_stream)) != Z_OK)
            {
                fclose(outFile);
                fclose(inFile);
                remove(file_name);
                inflateEnd(&d_stream);
                return Z_ERRNO;
            }

            d_stream.next_in    = inBuf;
            d_stream.avail_in   = 0;
            d_stream.next_out   = outBuf;
            d_stream.avail_out  = OUT_BUF_SIZE;
            d_stream.total_in   = total_in_bytes = 0;

            while( ((unsigned int)result == Z_OK)&&((unsigned int)total_in_bytes <= len_zarfile) )
            {
                if (d_stream.avail_in==0)
                {
                    #ifdef   VERBOSE_DEBUG
                    printf(".");
                    #endif
                    if ((len_zarfile-total_in_bytes)>=512)
                        d_stream.avail_in = fread (inBuf, 1, INBUF_SIZE , inFile);  /* return bytes */
                    else
                        d_stream.avail_in = fread (inBuf, 1, len_zarfile-total_in_bytes, inFile);  /* return bytes */
                    d_stream.next_in  = inBuf;
                    checksum = z_cksum(checksum,d_stream.next_in,d_stream.avail_in,total_in_bytes &0x1);

                    total_in_bytes += d_stream.avail_in;
                    if( ferror(inFile) || d_stream.avail_in <= 0)
                        result = Z_ERRNO;
                    /* check the checksum */
                }

                switch( zinflate(&d_stream, 0) )
                {
                    case Z_STREAM

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -