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/cryptodeflate.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 sys/contrib/zlib)
   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/kernel.h>
   43 #include <sys/sdt.h>
   44 #include <sys/systm.h>
   45 #include <contrib/zlib/zlib.h>
   46 
   47 #include <opencrypto/cryptodev.h>
   48 #include <opencrypto/deflate.h>
   49 
   50 SDT_PROVIDER_DECLARE(opencrypto);
   51 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
   52     "int", "u_int32_t");
   53 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, bad,
   54     "int", "int", "int", "int", "int", "int");
   55 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, iter,
   56     "int", "int", "int", "int", "int", "int");
   57 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, return,
   58     "int", "u_int32_t");
   59 
   60 int window_inflate = -1 * MAX_WBITS;
   61 int window_deflate = -12;
   62 
   63 static void *
   64 crypto_zalloc(void *nil, u_int type, u_int size)
   65 {
   66         void *ptr;
   67 
   68         ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
   69         return ptr;
   70 }
   71 
   72 static void
   73 crypto_zfree(void *nil, void *ptr)
   74 {
   75 
   76         free(ptr, M_CRYPTO_DATA);
   77 }
   78 
   79 /*
   80  * This function takes a block of data and (de)compress it using the deflate
   81  * algorithm
   82  */
   83 
   84 u_int32_t
   85 deflate_global(data, size, decomp, out)
   86         u_int8_t *data;
   87         u_int32_t size;
   88         int decomp;
   89         u_int8_t **out;
   90 {
   91         /* decomp indicates whether we compress (0) or decompress (1) */
   92 
   93         z_stream zbuf;
   94         u_int8_t *output;
   95         u_int32_t count, result;
   96         int error, i;
   97         struct deflate_buf *bufh, *bufp;
   98 
   99         SDT_PROBE2(opencrypto, deflate, deflate_global, entry, decomp, size);
  100 
  101         bufh = bufp = NULL;
  102         if (!decomp) {
  103                 i = 1;
  104         } else {
  105                 /*
  106                  * Choose a buffer with 4x the size of the input buffer
  107                  * for the size of the output buffer in the case of
  108                  * decompression. If it's not sufficient, it will need to be
  109                  * updated while the decompression is going on.
  110                  */
  111                 i = 4;
  112         }
  113         /*
  114          * Make sure we do have enough output space.  Repeated calls to
  115          * deflate need at least 6 bytes of output buffer space to avoid
  116          * repeated markers.  We will always provide at least 16 bytes.
  117          */
  118         while ((size * i) < 16)
  119                 i++;
  120 
  121         bufh = bufp = malloc(sizeof(*bufp) + (size_t)(size * i),
  122             M_CRYPTO_DATA, M_NOWAIT);
  123         if (bufp == NULL) {
  124                 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  125                     decomp, 0, __LINE__, 0, 0, 0);
  126                 goto bad2;
  127         }
  128         bufp->next = NULL;
  129         bufp->size = size * i;
  130 
  131         bzero(&zbuf, sizeof(z_stream));
  132         zbuf.zalloc = crypto_zalloc;
  133         zbuf.zfree = crypto_zfree;
  134         zbuf.opaque = Z_NULL;
  135         zbuf.next_in = data;    /* Data that is going to be processed. */
  136         zbuf.avail_in = size;   /* Total length of data to be processed. */
  137         zbuf.next_out = bufp->data;
  138         zbuf.avail_out = bufp->size;
  139 
  140         error = decomp ? inflateInit2(&zbuf, window_inflate) :
  141             deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
  142                     window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
  143         if (error != Z_OK) {
  144                 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  145                     decomp, error, __LINE__, 0, 0, 0);
  146                 goto bad;
  147         }
  148 
  149         for (;;) {
  150                 error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
  151                                  deflate(&zbuf, Z_FINISH);
  152                 if (error != Z_OK && error != Z_STREAM_END) {
  153                         SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  154                             decomp, error, __LINE__,
  155                             zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
  156                         goto bad;
  157                 }
  158                 SDT_PROBE6(opencrypto, deflate, deflate_global, iter,
  159                     decomp, error, __LINE__,
  160                     zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
  161                 if (decomp && zbuf.avail_in == 0 && error == Z_STREAM_END) {
  162                         /* Done. */
  163                         break;
  164                 } else if (!decomp && error == Z_STREAM_END) {
  165                         /* Done. */
  166                         break;
  167                 } else if (zbuf.avail_out == 0) {
  168                         struct deflate_buf *p;
  169 
  170                         /* We need more output space for another iteration. */
  171                         p = malloc(sizeof(*p) + (size_t)(size * i),
  172                             M_CRYPTO_DATA, M_NOWAIT);
  173                         if (p == NULL) {
  174                                 SDT_PROBE6(opencrypto, deflate, deflate_global,
  175                                     bad, decomp, 0, __LINE__, 0, 0, 0);
  176                                 goto bad;
  177                         }
  178                         p->next = NULL;
  179                         p->size = size * i;
  180                         bufp->next = p;
  181                         bufp = p;
  182                         zbuf.next_out = bufp->data;
  183                         zbuf.avail_out = bufp->size;
  184                 } else {
  185                         /* Unexpect result. */
  186                         SDT_PROBE6(opencrypto, deflate, deflate_global,
  187                             bad, decomp, error, __LINE__,
  188                             zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
  189                         goto bad;
  190                 }
  191         }
  192 
  193         result = count = zbuf.total_out;
  194 
  195         *out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
  196         if (*out == NULL) {
  197                 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  198                     decomp, 0, __LINE__, 0, 0, 0);
  199                 goto bad;
  200         }
  201         if (decomp)
  202                 inflateEnd(&zbuf);
  203         else
  204                 deflateEnd(&zbuf);
  205         output = *out;
  206         for (bufp = bufh; bufp != NULL; ) {
  207                 if (count > bufp->size) {
  208                         struct deflate_buf *p;
  209 
  210                         bcopy(bufp->data, *out, bufp->size);
  211                         *out += bufp->size;
  212                         count -= bufp->size;
  213                         p = bufp;
  214                         bufp = bufp->next;
  215                         free(p, M_CRYPTO_DATA);
  216                 } else {
  217                         /* It should be the last buffer. */
  218                         bcopy(bufp->data, *out, count);
  219                         *out += count;
  220                         free(bufp, M_CRYPTO_DATA);
  221                         bufp = NULL;
  222                         count = 0;
  223                 }
  224         }
  225         *out = output;
  226         SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
  227         return result;
  228 
  229 bad:
  230         if (decomp)
  231                 inflateEnd(&zbuf);
  232         else
  233                 deflateEnd(&zbuf);
  234         for (bufp = bufh; bufp != NULL; ) {
  235                 struct deflate_buf *p;
  236 
  237                 p = bufp;
  238                 bufp = bufp->next;
  239                 free(p, M_CRYPTO_DATA);
  240         }
  241 bad2:
  242         *out = NULL;
  243         return 0;
  244 }

Cache object: cdb8e9dad75cd9e01601a3798c59608e


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