This source file includes following definitions.
- pmix_compress_zlib_module_init
- pmix_compress_zlib_module_finalize
- pmix_compress_zlib_compress_block
- pmix_compress_zlib_uncompress_block
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
62 *outbytes = NULL;
63
64
65 inlen = strlen(instring);
66 memset (&strm, 0, sizeof (strm));
67 deflateInit (&strm, 9);
68
69
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
79
80 strm.avail_out = len;
81 strm.next_out = tmp;
82
83 deflate (&strm, Z_FINISH);
84 deflateEnd (&strm);
85
86
87
88
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
99 memcpy(ptr, &inlen, sizeof(uint32_t));
100 ptr += sizeof(uint32_t);
101
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;
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
119 *outstring = NULL;
120
121
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
128
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
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 }