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 = 0, j;
   69         struct deflate_buf buf[ZBUF];
   70 
   71         bzero(&zbuf, sizeof(z_stream));
   72         for (j = 0; j < ZBUF; j++)
   73                 buf[j].flag = 0;
   74 
   75         zbuf.next_in = data;    /* data that is going to be processed */
   76         zbuf.zalloc = z_alloc;
   77         zbuf.zfree = z_free;
   78         zbuf.opaque = Z_NULL;
   79         zbuf.avail_in = size;   /* Total length of data to be processed */
   80 
   81         if (!decomp) {
   82                 MALLOC(buf[i].out, u_int8_t *, (u_long) size, M_CRYPTO_DATA, 
   83                     M_NOWAIT);
   84                 if (buf[i].out == NULL)
   85                         goto bad;
   86                 buf[i].size = size;
   87                 buf[i].flag = 1;
   88                 i++;
   89         } else {
   90                 /*
   91                  * Choose a buffer with 4x the size of the input buffer
   92                  * for the size of the output buffer in the case of
   93                  * decompression. If it's not sufficient, it will need to be
   94                  * updated while the decompression is going on
   95                  */
   96 
   97                 MALLOC(buf[i].out, u_int8_t *, (u_long) (size * 4), 
   98                     M_CRYPTO_DATA, M_NOWAIT);
   99                 if (buf[i].out == NULL)
  100                         goto bad;
  101                 buf[i].size = size * 4;
  102                 buf[i].flag = 1;
  103                 i++;
  104         }
  105 
  106         zbuf.next_out = buf[0].out;
  107         zbuf.avail_out = buf[0].size;
  108 
  109         error = decomp ? inflateInit2(&zbuf, window_inflate) :
  110             deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
  111                     window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
  112 
  113         if (error != Z_OK)
  114                 goto bad;
  115         for (;;) {
  116                 error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
  117                                  deflate(&zbuf, Z_PARTIAL_FLUSH);
  118                 if (error != Z_OK && error != Z_STREAM_END)
  119                         goto bad;
  120                 else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
  121                         goto end;
  122                 else if (zbuf.avail_out == 0 && i < (ZBUF - 1)) {
  123                         /* we need more output space, allocate size */
  124                         MALLOC(buf[i].out, u_int8_t *, (u_long) size,
  125                             M_CRYPTO_DATA, M_NOWAIT);
  126                         if (buf[i].out == NULL)
  127                                 goto bad;
  128                         zbuf.next_out = buf[i].out;
  129                         buf[i].size = size;
  130                         buf[i].flag = 1;
  131                         zbuf.avail_out = buf[i].size;
  132                         i++;
  133                 } else
  134                         goto bad;
  135         }
  136 
  137 end:
  138         result = count = zbuf.total_out;
  139 
  140         MALLOC(*out, u_int8_t *, (u_long) result, M_CRYPTO_DATA, M_NOWAIT);
  141         if (*out == NULL)
  142                 goto bad;
  143         if (decomp)
  144                 inflateEnd(&zbuf);
  145         else
  146                 deflateEnd(&zbuf);
  147         output = *out;
  148         for (j = 0; buf[j].flag != 0; j++) {
  149                 if (count > buf[j].size) {
  150                         bcopy(buf[j].out, *out, buf[j].size);
  151                         *out += buf[j].size;
  152                         FREE(buf[j].out, M_CRYPTO_DATA);
  153                         count -= buf[j].size;
  154                 } else {
  155                         /* it should be the last buffer */
  156                         bcopy(buf[j].out, *out, count);
  157                         *out += count;
  158                         FREE(buf[j].out, M_CRYPTO_DATA);
  159                         count = 0;
  160                 }
  161         }
  162         *out = output;
  163         return result;
  164 
  165 bad:
  166         *out = NULL;
  167         for (j = 0; buf[j].flag != 0; j++)
  168                 FREE(buf[j].out, M_CRYPTO_DATA);
  169         if (decomp)
  170                 inflateEnd(&zbuf);
  171         else
  172                 deflateEnd(&zbuf);
  173         return 0;
  174 }
  175 
  176 void *
  177 z_alloc(nil, type, size)
  178         void *nil;
  179         u_int type, size;
  180 {
  181         void *ptr;
  182 
  183         ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
  184         return ptr;
  185 }
  186 
  187 void
  188 z_free(nil, ptr)
  189         void *nil, *ptr;
  190 {
  191         free(ptr, M_CRYPTO_DATA);
  192 }

Cache object: 52fdaf8651a6d87708c0fe93798f9aa7


[ 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.