root/opal/mca/compress/zlib/compress_zlib.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_compress_zlib_module_init
  2. opal_compress_zlib_module_finalize
  3. opal_compress_zlib_compress_block
  4. opal_compress_zlib_uncompress_block

   1 /*
   2  * Copyright (c) 2004-2010 The Trustees of Indiana University.
   3  *                         All rights reserved.
   4  * Copyright (c) 2010      Oracle and/or its affiliates.  All rights reserved.
   5  *
   6  * Copyright (c) 2014 Cisco Systems, Inc.  All rights reserved.
   7  * Copyright (c) 2015      Research Organization for Information Science
   8  *                         and Technology (RIST). All rights reserved.
   9  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
  10  * Copyright (c) 2019      Intel, Inc.  All rights reserved.
  11  * $COPYRIGHT$
  12  *
  13  * Additional copyrights may follow
  14  *
  15  * $HEADER$
  16  */
  17 
  18 #include "opal_config.h"
  19 
  20 #include <string.h>
  21 #include <sys/types.h>
  22 #include <sys/wait.h>
  23 #include <sys/stat.h>
  24 #if HAVE_UNISTD_H
  25 #include <unistd.h>
  26 #endif  /* HAVE_UNISTD_H */
  27 #include <zlib.h>
  28 
  29 #include "opal/util/opal_environ.h"
  30 #include "opal/util/output.h"
  31 #include "opal/util/argv.h"
  32 #include "opal/util/opal_environ.h"
  33 #include "opal/util/printf.h"
  34 
  35 #include "opal/constants.h"
  36 #include "opal/util/basename.h"
  37 
  38 #include "opal/mca/compress/compress.h"
  39 #include "opal/mca/compress/base/base.h"
  40 
  41 #include "compress_zlib.h"
  42 
  43 int opal_compress_zlib_module_init(void)
  44 {
  45     return OPAL_SUCCESS;
  46 }
  47 
  48 int opal_compress_zlib_module_finalize(void)
  49 {
  50     return OPAL_SUCCESS;
  51 }
  52 
  53 bool opal_compress_zlib_compress_block(uint8_t *inbytes,
  54                                        size_t inlen,
  55                                        uint8_t **outbytes,
  56                                        size_t *olen)
  57 {
  58     z_stream strm;
  59     size_t len;
  60     uint8_t *tmp;
  61 
  62     if (inlen < opal_compress_base.compress_limit) {
  63         return false;
  64     }
  65     opal_output_verbose(2, opal_compress_base_framework.framework_output,
  66                         "COMPRESSING");
  67 
  68     /* set default output */
  69     *outbytes = NULL;
  70     *olen = 0;
  71 
  72     /* setup the stream */
  73     memset (&strm, 0, sizeof (strm));
  74     deflateInit (&strm, 9);
  75 
  76     /* get an upper bound on the required output storage */
  77     len = deflateBound(&strm, inlen);
  78     if (NULL == (tmp = (uint8_t*)malloc(len))) {
  79         return false;
  80     }
  81     strm.next_in = inbytes;
  82     strm.avail_in = inlen;
  83 
  84     /* allocating the upper bound guarantees zlib will
  85      * always successfully compress into the available space */
  86     strm.avail_out = len;
  87     strm.next_out = tmp;
  88 
  89     deflate (&strm, Z_FINISH);
  90     deflateEnd (&strm);
  91 
  92     *outbytes = tmp;
  93     *olen = len - strm.avail_out;
  94     opal_output_verbose(2, opal_compress_base_framework.framework_output,
  95                         "\tINSIZE %d OUTSIZE %d", (int)inlen, (int)*olen);
  96     return true;  // we did the compression
  97 }
  98 
  99 bool opal_compress_zlib_uncompress_block(uint8_t **outbytes, size_t olen,
 100                                          uint8_t *inbytes, size_t len)
 101 {
 102     uint8_t *dest;
 103     z_stream strm;
 104 
 105     /* set the default error answer */
 106     *outbytes = NULL;
 107     opal_output_verbose(2, opal_compress_base_framework.framework_output, "DECOMPRESS");
 108 
 109     /* setting destination to the fully decompressed size */
 110     dest = (uint8_t*)malloc(olen);
 111     if (NULL == dest) {
 112         return false;
 113     }
 114 
 115     memset (&strm, 0, sizeof (strm));
 116     if (Z_OK != inflateInit(&strm)) {
 117         free(dest);
 118         return false;
 119     }
 120     strm.avail_in = len;
 121     strm.next_in = inbytes;
 122     strm.avail_out = olen;
 123     strm.next_out = dest;
 124 
 125     if (Z_STREAM_END != inflate (&strm, Z_FINISH)) {
 126         opal_output(0, "\tDECOMPRESS FAILED: %s", strm.msg);
 127     }
 128     inflateEnd (&strm);
 129     *outbytes = dest;
 130     opal_output_verbose(2, opal_compress_base_framework.framework_output,
 131                         "\tINSIZE: %d OUTSIZE %d", (int)len, (int)olen);
 132     return true;
 133 }

/* [<][>][^][v][top][bottom][index][help] */