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

Cache object: 7d56759735cb4672d87ed8df0d13a2b4


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