The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/opencrypto/deflate.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  *
   10  * 1. Redistributions of source code must retain the above copyright
   11  *   notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *   notice, this list of conditions and the following disclaimer in the
   14  *   documentation and/or other materials provided with the distribution.
   15  * 3. The name of the author may not be used to endorse or promote products
   16  *   derived from this software without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   28  */
   29 
   30 /*
   31  * This file contains a wrapper around the deflate algo compression
   32  * functions using the zlib library (see net/zlib.{c,h})
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include <sys/types.h>
   39 #include <sys/param.h>
   40 #include <sys/malloc.h>
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <net/zlib.h>
   44 
   45 #include <opencrypto/cryptodev.h>
   46 #include <opencrypto/deflate.h>
   47 
   48 int window_inflate = -1 * MAX_WBITS;
   49 int window_deflate = -12;
   50 
   51 /*
   52  * This function takes a block of data and (de)compress it using the deflate
   53  * algorithm
   54  */
   55 
   56 u_int32_t
   57 deflate_global(data, size, decomp, out)
   58         u_int8_t *data;
   59         u_int32_t size;
   60         int decomp;
   61         u_int8_t **out;
   62 {
   63         /* decomp indicates whether we compress (0) or decompress (1) */
   64 
   65         z_stream zbuf;
   66         u_int8_t *output;
   67         u_int32_t count, result;
   68         int error, i;
   69         struct deflate_buf *bufh, *bufp;
   70 
   71         bufh = bufp = NULL;
   72         if (!decomp) {
   73                 i = 1;
   74         } else {
   75                 /*
   76                  * Choose a buffer with 4x the size of the input buffer
   77                  * for the size of the output buffer in the case of
   78                  * decompression. If it's not sufficient, it will need to be
   79                  * updated while the decompression is going on.
   80                  */
   81                 i = 4;
   82         }
   83         /*
   84          * Make sure we do have enough output space.  Repeated calls to
   85          * deflate need at least 6 bytes of output buffer space to avoid
   86          * repeated markers.  We will always provide at least 16 bytes.
   87          */
   88         while ((size * i) < 16)
   89                 i++;
   90 
   91         bufh = bufp = malloc(sizeof(*bufp) + (size_t)(size * i),
   92             M_CRYPTO_DATA, M_NOWAIT);
   93         if (bufp == NULL) {
   94                 goto bad2;
   95         }
   96         bufp->next = NULL;
   97         bufp->size = size * i;
   98 
   99         bzero(&zbuf, sizeof(z_stream));
  100         zbuf.zalloc = z_alloc;
  101         zbuf.zfree = z_free;
  102         zbuf.opaque = Z_NULL;
  103         zbuf.next_in = data;    /* Data that is going to be processed. */
  104         zbuf.avail_in = size;   /* Total length of data to be processed. */
  105         zbuf.next_out = bufp->data;
  106         zbuf.avail_out = bufp->size;
  107 
  108         error = decomp ? inflateInit2(&zbuf, window_inflate) :
  109             deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
  110                     window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
  111         if (error != Z_OK)
  112                 goto bad;
  113 
  114         for (;;) {
  115                 error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
  116                                  deflate(&zbuf, Z_FINISH);
  117                 if (error != Z_OK && error != Z_STREAM_END)
  118                         goto bad;
  119                 if (decomp && zbuf.avail_in == 0 && error == Z_STREAM_END) {
  120                         /* Done. */
  121                         break;
  122                 } else if (!decomp && error == Z_STREAM_END) {
  123                         /* Done. */
  124                         break;
  125                 } else if (zbuf.avail_out == 0) {
  126                         struct deflate_buf *p;
  127 
  128                         /* We need more output space for another iteration. */
  129                         p = malloc(sizeof(*p) + (size_t)(size * i),
  130                             M_CRYPTO_DATA, M_NOWAIT);
  131                         if (p == NULL) {
  132                                 goto bad;
  133                         }
  134                         p->next = NULL;
  135                         p->size = size * i;
  136                         bufp->next = p;
  137                         bufp = p;
  138                         zbuf.next_out = bufp->data;
  139                         zbuf.avail_out = bufp->size;
  140                 } else {
  141                         /* Unexpect result. */
  142                         goto bad;
  143                 }
  144         }
  145 
  146         result = count = zbuf.total_out;
  147 
  148         *out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
  149         if (*out == NULL) {
  150                 goto bad;
  151         }
  152         if (decomp)
  153                 inflateEnd(&zbuf);
  154         else
  155                 deflateEnd(&zbuf);
  156         output = *out;
  157         for (bufp = bufh; bufp != NULL; ) {
  158                 if (count > bufp->size) {
  159                         struct deflate_buf *p;
  160 
  161                         bcopy(bufp->data, *out, bufp->size);
  162                         *out += bufp->size;
  163                         count -= bufp->size;
  164                         p = bufp;
  165                         bufp = bufp->next;
  166                         free(p, M_CRYPTO_DATA);
  167                 } else {
  168                         /* It should be the last buffer. */
  169                         bcopy(bufp->data, *out, count);
  170                         *out += count;
  171                         free(bufp, M_CRYPTO_DATA);
  172                         bufp = NULL;
  173                         count = 0;
  174                 }
  175         }
  176         *out = output;
  177         return result;
  178 
  179 bad:
  180         if (decomp)
  181                 inflateEnd(&zbuf);
  182         else
  183                 deflateEnd(&zbuf);
  184         for (bufp = bufh; bufp != NULL; ) {
  185                 struct deflate_buf *p;
  186 
  187                 p = bufp;
  188                 bufp = bufp->next;
  189                 free(p, M_CRYPTO_DATA);
  190         }
  191 bad2:
  192         *out = NULL;
  193         return 0;
  194 }
  195 
  196 void *
  197 z_alloc(nil, type, size)
  198         void *nil;
  199         u_int type, size;
  200 {
  201         void *ptr;
  202 
  203         ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
  204         return ptr;
  205 }
  206 
  207 void
  208 z_free(nil, ptr)
  209         void *nil, *ptr;
  210 {
  211         free(ptr, M_CRYPTO_DATA);
  212 }

Cache object: 2ebe318ecba7ba09579eb97b52a547fa


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.