inflate.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* inflate.c -- zlib interface to inflate modules
  2.  * Copyright (C) 1995-1998 Mark Adler
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5. #include <linux/module.h>
  6. #include <linux/zutil.h>
  7. #include "infblock.h"
  8. #include "infutil.h"
  9. int ZEXPORT zlib_inflate_workspacesize(void)
  10. {
  11.   return sizeof(struct inflate_workspace);
  12. }
  13. int ZEXPORT zlib_inflateReset(z)
  14. z_streamp z;
  15. {
  16.   if (z == Z_NULL || z->state == Z_NULL || z->workspace == Z_NULL)
  17.     return Z_STREAM_ERROR;
  18.   z->total_in = z->total_out = 0;
  19.   z->msg = Z_NULL;
  20.   z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  21.   zlib_inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  22.   return Z_OK;
  23. }
  24. int ZEXPORT zlib_inflateEnd(z)
  25. z_streamp z;
  26. {
  27.   if (z == Z_NULL || z->state == Z_NULL || z->workspace == Z_NULL)
  28.     return Z_STREAM_ERROR;
  29.   if (z->state->blocks != Z_NULL)
  30.     zlib_inflate_blocks_free(z->state->blocks, z);
  31.   z->state = Z_NULL;
  32.   return Z_OK;
  33. }
  34. int ZEXPORT zlib_inflateInit2_(z, w, version, stream_size)
  35. z_streamp z;
  36. int w;
  37. const char *version;
  38. int stream_size;
  39. {
  40.   if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  41.       stream_size != sizeof(z_stream) || z->workspace == Z_NULL)
  42.       return Z_VERSION_ERROR;
  43.   /* initialize state */
  44.   if (z == Z_NULL)
  45.     return Z_STREAM_ERROR;
  46.   z->msg = Z_NULL;
  47.   z->state = &WS(z)->internal_state;
  48.   z->state->blocks = Z_NULL;
  49.   /* handle undocumented nowrap option (no zlib header or check) */
  50.   z->state->nowrap = 0;
  51.   if (w < 0)
  52.   {
  53.     w = - w;
  54.     z->state->nowrap = 1;
  55.   }
  56.   /* set window size */
  57.   if (w < 8 || w > 15)
  58.   {
  59.     zlib_inflateEnd(z);
  60.     return Z_STREAM_ERROR;
  61.   }
  62.   z->state->wbits = (uInt)w;
  63.   /* create inflate_blocks state */
  64.   if ((z->state->blocks =
  65.       zlib_inflate_blocks_new(z, z->state->nowrap ? Z_NULL : zlib_adler32, (uInt)1 << w))
  66.       == Z_NULL)
  67.   {
  68.     zlib_inflateEnd(z);
  69.     return Z_MEM_ERROR;
  70.   }
  71.   /* reset state */
  72.   zlib_inflateReset(z);
  73.   return Z_OK;
  74. }
  75. /*
  76.  * At the end of a Deflate-compressed PPP packet, we expect to have seen
  77.  * a `stored' block type value but not the (zero) length bytes.
  78.  */
  79. static int zlib_inflate_packet_flush(inflate_blocks_statef *s)
  80. {
  81.     if (s->mode != LENS)
  82. return Z_DATA_ERROR;
  83.     s->mode = TYPE;
  84.     return Z_OK;
  85. }
  86. int ZEXPORT zlib_inflateInit_(z, version, stream_size)
  87. z_streamp z;
  88. const char *version;
  89. int stream_size;
  90. {
  91.   return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size);
  92. }
  93. #undef NEEDBYTE
  94. #undef NEXTBYTE
  95. #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;}
  96. #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  97. int ZEXPORT zlib_inflate(z, f)
  98. z_streamp z;
  99. int f;
  100. {
  101.   int r, trv;
  102.   uInt b;
  103.   if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  104.     return Z_STREAM_ERROR;
  105.   trv = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  106.   r = Z_BUF_ERROR;
  107.   while (1) switch (z->state->mode)
  108.   {
  109.     case METHOD:
  110.       NEEDBYTE
  111.       if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
  112.       {
  113.         z->state->mode = I_BAD;
  114.         z->msg = (char*)"unknown compression method";
  115.         z->state->sub.marker = 5;       /* can't try inflateSync */
  116.         break;
  117.       }
  118.       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  119.       {
  120.         z->state->mode = I_BAD;
  121.         z->msg = (char*)"invalid window size";
  122.         z->state->sub.marker = 5;       /* can't try inflateSync */
  123.         break;
  124.       }
  125.       z->state->mode = FLAG;
  126.     case FLAG:
  127.       NEEDBYTE
  128.       b = NEXTBYTE;
  129.       if (((z->state->sub.method << 8) + b) % 31)
  130.       {
  131.         z->state->mode = I_BAD;
  132.         z->msg = (char*)"incorrect header check";
  133.         z->state->sub.marker = 5;       /* can't try inflateSync */
  134.         break;
  135.       }
  136.       if (!(b & PRESET_DICT))
  137.       {
  138.         z->state->mode = BLOCKS;
  139.         break;
  140.       }
  141.       z->state->mode = DICT4;
  142.     case DICT4:
  143.       NEEDBYTE
  144.       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  145.       z->state->mode = DICT3;
  146.     case DICT3:
  147.       NEEDBYTE
  148.       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  149.       z->state->mode = DICT2;
  150.     case DICT2:
  151.       NEEDBYTE
  152.       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  153.       z->state->mode = DICT1;
  154.     case DICT1:
  155.       NEEDBYTE
  156.       z->state->sub.check.need += (uLong)NEXTBYTE;
  157.       z->adler = z->state->sub.check.need;
  158.       z->state->mode = DICT0;
  159.       return Z_NEED_DICT;
  160.     case DICT0:
  161.       z->state->mode = I_BAD;
  162.       z->msg = (char*)"need dictionary";
  163.       z->state->sub.marker = 0;       /* can try inflateSync */
  164.       return Z_STREAM_ERROR;
  165.     case BLOCKS:
  166.       r = zlib_inflate_blocks(z->state->blocks, z, r);
  167.       if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
  168.   r = zlib_inflate_packet_flush(z->state->blocks);
  169.       if (r == Z_DATA_ERROR)
  170.       {
  171.         z->state->mode = I_BAD;
  172.         z->state->sub.marker = 0;       /* can try inflateSync */
  173.         break;
  174.       }
  175.       if (r == Z_OK)
  176.         r = trv;
  177.       if (r != Z_STREAM_END)
  178.         return r;
  179.       r = trv;
  180.       zlib_inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  181.       if (z->state->nowrap)
  182.       {
  183.         z->state->mode = I_DONE;
  184.         break;
  185.       }
  186.       z->state->mode = CHECK4;
  187.     case CHECK4:
  188.       NEEDBYTE
  189.       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  190.       z->state->mode = CHECK3;
  191.     case CHECK3:
  192.       NEEDBYTE
  193.       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  194.       z->state->mode = CHECK2;
  195.     case CHECK2:
  196.       NEEDBYTE
  197.       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  198.       z->state->mode = CHECK1;
  199.     case CHECK1:
  200.       NEEDBYTE
  201.       z->state->sub.check.need += (uLong)NEXTBYTE;
  202.       if (z->state->sub.check.was != z->state->sub.check.need)
  203.       {
  204.         z->state->mode = I_BAD;
  205.         z->msg = (char*)"incorrect data check";
  206.         z->state->sub.marker = 5;       /* can't try inflateSync */
  207.         break;
  208.       }
  209.       z->state->mode = I_DONE;
  210.     case I_DONE:
  211.       return Z_STREAM_END;
  212.     case I_BAD:
  213.       return Z_DATA_ERROR;
  214.     default:
  215.       return Z_STREAM_ERROR;
  216.   }
  217.  empty:
  218.   if (f != Z_PACKET_FLUSH)
  219.     return r;
  220.   z->state->mode = I_BAD;
  221.   z->msg = (char *)"need more for packet flush";
  222.   z->state->sub.marker = 0;       /* can try inflateSync */
  223.   return Z_DATA_ERROR;
  224. }
  225. int ZEXPORT zlib_inflateSync(z)
  226. z_streamp z;
  227. {
  228.   uInt n;       /* number of bytes to look at */
  229.   Bytef *p;     /* pointer to bytes */
  230.   uInt m;       /* number of marker bytes found in a row */
  231.   uLong r, w;   /* temporaries to save total_in and total_out */
  232.   /* set up */
  233.   if (z == Z_NULL || z->state == Z_NULL)
  234.     return Z_STREAM_ERROR;
  235.   if (z->state->mode != I_BAD)
  236.   {
  237.     z->state->mode = I_BAD;
  238.     z->state->sub.marker = 0;
  239.   }
  240.   if ((n = z->avail_in) == 0)
  241.     return Z_BUF_ERROR;
  242.   p = z->next_in;
  243.   m = z->state->sub.marker;
  244.   /* search */
  245.   while (n && m < 4)
  246.   {
  247.     static const Byte mark[4] = {0, 0, 0xff, 0xff};
  248.     if (*p == mark[m])
  249.       m++;
  250.     else if (*p)
  251.       m = 0;
  252.     else
  253.       m = 4 - m;
  254.     p++, n--;
  255.   }
  256.   /* restore */
  257.   z->total_in += p - z->next_in;
  258.   z->next_in = p;
  259.   z->avail_in = n;
  260.   z->state->sub.marker = m;
  261.   /* return no joy or set up to restart on a new block */
  262.   if (m != 4)
  263.     return Z_DATA_ERROR;
  264.   r = z->total_in;  w = z->total_out;
  265.   zlib_inflateReset(z);
  266.   z->total_in = r;  z->total_out = w;
  267.   z->state->mode = BLOCKS;
  268.   return Z_OK;
  269. }
  270. /* Returns true if inflate is currently at the end of a block generated
  271.  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  272.  * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  273.  * but removes the length bytes of the resulting empty stored block. When
  274.  * decompressing, PPP checks that at the end of input packet, inflate is
  275.  * waiting for these length bytes.
  276.  */
  277. int ZEXPORT zlib_inflateSyncPoint(z)
  278. z_streamp z;
  279. {
  280.   if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
  281.     return Z_STREAM_ERROR;
  282.   return zlib_inflate_blocks_sync_point(z->state->blocks);
  283. }
  284. /*
  285.  * This subroutine adds the data at next_in/avail_in to the output history
  286.  * without performing any output.  The output buffer must be "caught up";
  287.  * i.e. no pending output (hence s->read equals s->write), and the state must
  288.  * be BLOCKS (i.e. we should be willing to see the start of a series of
  289.  * BLOCKS).  On exit, the output will also be caught up, and the checksum
  290.  * will have been updated if need be.
  291.  */
  292. static int zlib_inflate_addhistory(inflate_blocks_statef *s,
  293.       z_stream              *z)
  294. {
  295.     uLong b;              /* bit buffer */  /* NOT USED HERE */
  296.     uInt k;               /* bits in bit buffer */ /* NOT USED HERE */
  297.     uInt t;               /* temporary storage */
  298.     Bytef *p;             /* input data pointer */
  299.     uInt n;               /* bytes available there */
  300.     Bytef *q;             /* output window write pointer */
  301.     uInt m;               /* bytes to end of window or read pointer */
  302.     if (s->read != s->write)
  303. return Z_STREAM_ERROR;
  304.     if (s->mode != TYPE)
  305. return Z_DATA_ERROR;
  306.     /* we're ready to rock */
  307.     LOAD
  308.     /* while there is input ready, copy to output buffer, moving
  309.      * pointers as needed.
  310.      */
  311.     while (n) {
  312. t = n;  /* how many to do */
  313. /* is there room until end of buffer? */
  314. if (t > m) t = m;
  315. /* update check information */
  316. if (s->checkfn != Z_NULL)
  317.     s->check = (*s->checkfn)(s->check, q, t);
  318. memcpy(q, p, t);
  319. q += t;
  320. p += t;
  321. n -= t;
  322. z->total_out += t;
  323. s->read = q;    /* drag read pointer forward */
  324. /*      WWRAP  */  /* expand WWRAP macro by hand to handle s->read */
  325. if (q == s->end) {
  326.     s->read = q = s->window;
  327.     m = WAVAIL;
  328. }
  329.     }
  330.     UPDATE
  331.     return Z_OK;
  332. }
  333. /*
  334.  * This subroutine adds the data at next_in/avail_in to the output history
  335.  * without performing any output.  The output buffer must be "caught up";
  336.  * i.e. no pending output (hence s->read equals s->write), and the state must
  337.  * be BLOCKS (i.e. we should be willing to see the start of a series of
  338.  * BLOCKS).  On exit, the output will also be caught up, and the checksum
  339.  * will have been updated if need be.
  340.  */
  341. int ZEXPORT zlib_inflateIncomp(z)
  342. z_stream *z;
  343. {
  344.     if (z->state->mode != BLOCKS)
  345. return Z_DATA_ERROR;
  346.     return zlib_inflate_addhistory(z->state->blocks, z);
  347. }