compress from stdin to fixed-size block on stdout */
| 137 | |
| 138 | /* compress from stdin to fixed-size block on stdout */ |
| 139 | int main(int argc, char **argv) |
| 140 | { |
| 141 | int ret; /* return code */ |
| 142 | unsigned size; /* requested fixed output block size */ |
| 143 | unsigned have; /* bytes written by deflate() call */ |
| 144 | unsigned char *blk; /* intermediate and final stream */ |
| 145 | unsigned char *tmp; /* close to desired size stream */ |
| 146 | z_stream def, inf; /* zlib deflate and inflate states */ |
| 147 | |
| 148 | /* get requested output size */ |
| 149 | if (argc != 2) |
| 150 | quit("need one argument: size of output block"); |
| 151 | ret = (int)strtol(argv[1], argv + 1, 10); |
| 152 | if (argv[1][0] != 0) |
| 153 | quit("argument must be a number"); |
| 154 | if (ret < 8) /* 8 is minimum zlib stream size */ |
| 155 | quit("need positive size of 8 or greater"); |
| 156 | size = (unsigned)ret; |
| 157 | |
| 158 | printf("zlib version %s\n", ZLIB_VERSION); |
| 159 | if (ZWRAP_isUsingZSTDcompression()) printf("zstd version %s\n", zstdVersion()); |
| 160 | |
| 161 | /* allocate memory for buffers and compression engine */ |
| 162 | blk = (unsigned char*)malloc(size + EXCESS); |
| 163 | def.zalloc = Z_NULL; |
| 164 | def.zfree = Z_NULL; |
| 165 | def.opaque = Z_NULL; |
| 166 | ret = deflateInit(&def, Z_DEFAULT_COMPRESSION); |
| 167 | if (ret != Z_OK || blk == NULL) |
| 168 | quit("out of memory"); |
| 169 | |
| 170 | /* compress from stdin until output full, or no more input */ |
| 171 | def.avail_out = size + EXCESS; |
| 172 | def.next_out = blk; |
| 173 | LOG_FITBLK("partcompress1 total_in=%d total_out=%d\n", (int)def.total_in, (int)def.total_out); |
| 174 | ret = partcompress(stdin, &def); |
| 175 | printf("partcompress total_in=%d total_out=%d\n", (int)def.total_in, (int)def.total_out); |
| 176 | if (ret == Z_ERRNO) |
| 177 | quit("error reading input"); |
| 178 | |
| 179 | /* if it all fit, then size was undersubscribed -- done! */ |
| 180 | if (ret == Z_STREAM_END && def.avail_out >= EXCESS) { |
| 181 | /* write block to stdout */ |
| 182 | have = size + EXCESS - def.avail_out; |
| 183 | /* if (fwrite(blk, 1, have, stdout) != have || ferror(stdout)) |
| 184 | * quit("error writing output"); */ |
| 185 | |
| 186 | /* clean up and print results to stderr */ |
| 187 | ret = deflateEnd(&def); |
| 188 | assert(ret != Z_STREAM_ERROR); |
| 189 | free(blk); |
| 190 | fprintf(stderr, |
| 191 | "%u bytes unused out of %u requested (all input)\n", |
| 192 | size - have, size); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* it didn't all fit -- set up for recompression */ |
nothing calls this directly
no test coverage detected