root/opal/mca/pmix/pmix4x/pmix/src/mca/pcompress/zlib/compress_zlib.c

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

DEFINITIONS

This source file includes following definitions.
  1. pmix_compress_zlib_module_init
  2. pmix_compress_zlib_module_finalize
  3. pmix_compress_zlib_compress_block
  4. pmix_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 "pmix_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 "src/util/pmix_environ.h"
  30 #include "src/util/output.h"
  31 #include "src/util/argv.h"
  32 #include "src/util/pmix_environ.h"
  33 #include "src/util/printf.h"
  34 
  35 #include "pmix_common.h"
  36 #include "src/util/basename.h"
  37 
  38 #include "src/mca/pcompress/base/base.h"
  39 
  40 #include "compress_zlib.h"
  41 
  42 int pmix_compress_zlib_module_init(void)
  43 {
  44     return PMIX_SUCCESS;
  45 }
  46 
  47 int pmix_compress_zlib_module_finalize(void)
  48 {
  49     return PMIX_SUCCESS;
  50 }
  51 
  52 bool pmix_compress_zlib_compress_block(char *instring,
  53                                        uint8_t **outbytes,
  54                                        size_t *nbytes)
  55 {
  56     z_stream strm;
  57     size_t len, outlen;
  58     uint8_t *tmp, *ptr;
  59     uint32_t inlen;
  60 
  61     /* set default output */
  62     *outbytes = NULL;
  63 
  64     /* setup the stream */
  65     inlen = strlen(instring);
  66     memset (&strm, 0, sizeof (strm));
  67     deflateInit (&strm, 9);
  68 
  69     /* get an upper bound on the required output storage */
  70     len = deflateBound(&strm, inlen);
  71     if (NULL == (tmp = (uint8_t*)malloc(len))) {
  72         *outbytes = NULL;
  73         return false;
  74     }
  75     strm.next_in = (uint8_t*)instring;
  76     strm.avail_in = strlen(instring);
  77 
  78     /* allocating the upper bound guarantees zlib will
  79      * always successfully compress into the available space */
  80     strm.avail_out = len;
  81     strm.next_out = tmp;
  82 
  83     deflate (&strm, Z_FINISH);
  84     deflateEnd (&strm);
  85 
  86     /* allocate 4 bytes beyond the size reqd by zlib so we
  87      * can pass the size of the uncompressed string to the
  88      * decompress side */
  89     outlen = len - strm.avail_out + sizeof(uint32_t);
  90     ptr = (uint8_t*)malloc(outlen);
  91     if (NULL == ptr) {
  92         free(tmp);
  93         return false;
  94     }
  95     *outbytes = ptr;
  96     *nbytes = outlen;
  97 
  98     /* fold the uncompressed length into the buffer */
  99     memcpy(ptr, &inlen, sizeof(uint32_t));
 100     ptr += sizeof(uint32_t);
 101     /* bring over the compressed data */
 102     memcpy(ptr, tmp, outlen-sizeof(uint32_t));
 103     free(tmp);
 104     pmix_output_verbose(2, pmix_pcompress_base_framework.framework_output,
 105                         "COMPRESS INPUT STRING OF LEN %d OUTPUT SIZE %lu",
 106                         inlen, outlen-sizeof(uint32_t));
 107     return true;  // we did the compression
 108 }
 109 
 110 bool pmix_compress_zlib_uncompress_block(char **outstring,
 111                                          uint8_t *inbytes, size_t len)
 112 {
 113     uint8_t *dest;
 114     int32_t len2;
 115     z_stream strm;
 116     int rc;
 117 
 118     /* set the default error answer */
 119     *outstring = NULL;
 120 
 121     /* the first 4 bytes contains the uncompressed size */
 122     memcpy(&len2, inbytes, sizeof(uint32_t));
 123 
 124     pmix_output_verbose(2, pmix_pcompress_base_framework.framework_output,
 125                         "DECOMPRESSING INPUT OF LEN %lu OUTPUT %d", len, len2);
 126 
 127     /* setting destination to the fully decompressed size, +1 to
 128      * hold the NULL terminator */
 129     dest = (uint8_t*)malloc(len2+1);
 130     if (NULL == dest) {
 131         return false;
 132     }
 133     memset(dest, 0, len2+1);
 134 
 135     memset (&strm, 0, sizeof (strm));
 136     if (Z_OK != inflateInit(&strm)) {
 137         free(dest);
 138         return false;
 139     }
 140     strm.avail_in = len;
 141     strm.next_in = (uint8_t*)(inbytes + sizeof(uint32_t));
 142     strm.avail_out = len2;
 143     strm.next_out = (uint8_t*)dest;
 144 
 145     rc = inflate (&strm, Z_FINISH);
 146     inflateEnd (&strm);
 147     /* ensure this is NULL terminated! */
 148     dest[len2] = '\0';
 149     *outstring = (char*)dest;
 150     pmix_output_verbose(2, pmix_pcompress_base_framework.framework_output,
 151                         "\tFINAL LEN: %lu CODE: %d", strlen(*outstring), rc);
 152     return true;
 153 }

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