Rizin
unix-like reverse engineering framework and cli tools
zpipe.c File Reference
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "zlib.h"

Go to the source code of this file.

Macros

#define SET_BINARY_MODE(file)
 
#define CHUNK   16384
 

Functions

int def (FILE *source, FILE *dest, int level)
 
int inf (FILE *source, FILE *dest)
 
void zerr (int ret)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ CHUNK

#define CHUNK   16384

Definition at line 28 of file zpipe.c.

◆ SET_BINARY_MODE

#define SET_BINARY_MODE (   file)

Definition at line 25 of file zpipe.c.

Function Documentation

◆ def()

int def ( FILE *  source,
FILE *  dest,
int  level 
)

Definition at line 36 of file zpipe.c.

37 {
38  int ret, flush;
39  unsigned have;
40  z_stream strm;
41  unsigned char in[CHUNK];
42  unsigned char out[CHUNK];
43 
44  /* allocate deflate state */
45  strm.zalloc = Z_NULL;
46  strm.zfree = Z_NULL;
47  strm.opaque = Z_NULL;
48  ret = deflateInit(&strm, level);
49  if (ret != Z_OK)
50  return ret;
51 
52  /* compress until end of file */
53  do {
54  strm.avail_in = fread(in, 1, CHUNK, source);
55  if (ferror(source)) {
56  (void)deflateEnd(&strm);
57  return Z_ERRNO;
58  }
59  flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
60  strm.next_in = in;
61 
62  /* run deflate() on input until output buffer not full, finish
63  compression if all of source has been read in */
64  do {
66  strm.next_out = out;
67  ret = deflate(&strm, flush); /* no bad return value */
68  assert(ret != Z_STREAM_ERROR); /* state not clobbered */
69  have = CHUNK - strm.avail_out;
70  if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
71  (void)deflateEnd(&strm);
72  return Z_ERRNO;
73  }
74  } while (strm.avail_out == 0);
75  assert(strm.avail_in == 0); /* all input will be used */
76 
77  /* done when last data in file processed */
78  } while (flush != Z_FINISH);
79  assert(ret == Z_STREAM_END); /* stream will be complete */
80 
81  /* clean up and return */
82  (void)deflateEnd(&strm);
83  return Z_OK;
84 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1119
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:804
static lzma_stream strm
Definition: full_flush.c:20
const char * source
Definition: lz4.h:699
char * dest
Definition: lz4.h:697
assert(limit<=UINT32_MAX/2)
uint8_t * next_out
Definition: base.h:490
size_t avail_out
Definition: base.h:491
const uint8_t * next_in
Definition: base.h:486
size_t avail_in
Definition: base.h:487
static int level
Definition: vmenus.c:2424
#define Z_ERRNO
Definition: zlib.h:180
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_FINISH
Definition: zlib.h:172
#define Z_OK
Definition: zlib.h:177
#define Z_STREAM_ERROR
Definition: zlib.h:181
#define Z_NO_FLUSH
Definition: zlib.h:168
#define Z_NULL
Definition: zlib.h:212
#define deflateInit(strm, level)
Definition: zlib.h:1810
#define CHUNK
Definition: zpipe.c:28

References assert(), lzma_stream::avail_in, lzma_stream::avail_out, CHUNK, deflate(), deflateEnd(), deflateInit, dest, in, level, lzma_stream::next_in, lzma_stream::next_out, out, source, strm, Z_ERRNO, Z_FINISH, Z_NO_FLUSH, Z_NULL, Z_OK, Z_STREAM_END, and Z_STREAM_ERROR.

Referenced by __show_status_yesno(), _zip_ef_clone(), getArg(), main(), parse_attr_value(), parse_def(), parse_offset(), parse_type(), partcompress(), recompress(), rz_cmd_alias(), rz_cons_yesno(), rz_lang_def_free(), rz_lang_define(), rz_lang_prompt(), rz_lang_undef(), rz_reg_profile_def_free(), rz_reg_set_profile_string(), rz_reg_set_reg_profile(), and wasm_dis().

◆ inf()

int inf ( FILE *  source,
FILE *  dest 
)

Definition at line 92 of file zpipe.c.

93 {
94  int ret;
95  unsigned have;
96  z_stream strm;
97  unsigned char in[CHUNK];
98  unsigned char out[CHUNK];
99 
100  /* allocate inflate state */
101  strm.zalloc = Z_NULL;
102  strm.zfree = Z_NULL;
103  strm.opaque = Z_NULL;
104  strm.avail_in = 0;
105  strm.next_in = Z_NULL;
106  ret = inflateInit(&strm);
107  if (ret != Z_OK)
108  return ret;
109 
110  /* decompress until deflate stream ends or end of file */
111  do {
112  strm.avail_in = fread(in, 1, CHUNK, source);
113  if (ferror(source)) {
114  (void)inflateEnd(&strm);
115  return Z_ERRNO;
116  }
117  if (strm.avail_in == 0)
118  break;
119  strm.next_in = in;
120 
121  /* run inflate() on input until output buffer not full */
122  do {
123  strm.avail_out = CHUNK;
124  strm.next_out = out;
125  ret = inflate(&strm, Z_NO_FLUSH);
126  assert(ret != Z_STREAM_ERROR); /* state not clobbered */
127  switch (ret) {
128  case Z_NEED_DICT:
129  ret = Z_DATA_ERROR; /* and fall through */
130  case Z_DATA_ERROR:
131  case Z_MEM_ERROR:
132  (void)inflateEnd(&strm);
133  return ret;
134  }
135  have = CHUNK - strm.avail_out;
136  if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
137  (void)inflateEnd(&strm);
138  return Z_ERRNO;
139  }
140  } while (strm.avail_out == 0);
141 
142  /* done when inflate() says it's done */
143  } while (ret != Z_STREAM_END);
144 
145  /* clean up and return */
146  (void)inflateEnd(&strm);
147  return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
148 }
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:623
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.c:1301
#define Z_NEED_DICT
Definition: zlib.h:179
#define Z_DATA_ERROR
Definition: zlib.h:182
#define inflateInit(strm)
Definition: zlib.h:1812
#define Z_MEM_ERROR
Definition: zlib.h:183

References assert(), lzma_stream::avail_in, lzma_stream::avail_out, CHUNK, dest, in, inflate(), inflateEnd(), inflateInit, lzma_stream::next_in, lzma_stream::next_out, out, source, strm, Z_DATA_ERROR, Z_ERRNO, Z_MEM_ERROR, Z_NEED_DICT, Z_NO_FLUSH, Z_NULL, Z_OK, Z_STREAM_END, and Z_STREAM_ERROR.

Referenced by init_debug_info(), main(), recompress(), rz_bin_dwarf_debug_info_free(), rz_core_bin_dwarf_print_debug_info(), and rz_core_bin_load().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 176 of file zpipe.c.

177 {
178  int ret;
179 
180  /* avoid end-of-line conversions */
181  SET_BINARY_MODE(stdin);
182  SET_BINARY_MODE(stdout);
183 
184  /* do compression if no arguments */
185  if (argc == 1) {
186  ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
187  if (ret != Z_OK)
188  zerr(ret);
189  return ret;
190  }
191 
192  /* do decompression if -d specified */
193  else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
194  ret = inf(stdin, stdout);
195  if (ret != Z_OK)
196  zerr(ret);
197  return ret;
198  }
199 
200  /* otherwise, report usage */
201  else {
202  fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
203  return 1;
204  }
205 }
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:193
void zerr(int ret)
Definition: zpipe.c:151
int inf(FILE *source, FILE *dest)
Definition: zpipe.c:92
int def(FILE *source, FILE *dest, int level)
Definition: zpipe.c:36
#define SET_BINARY_MODE(file)
Definition: zpipe.c:25

References argv, def(), inf(), SET_BINARY_MODE, Z_DEFAULT_COMPRESSION, Z_OK, and zerr().

◆ zerr()

void zerr ( int  ret)

Definition at line 151 of file zpipe.c.

152 {
153  fputs("zpipe: ", stderr);
154  switch (ret) {
155  case Z_ERRNO:
156  if (ferror(stdin))
157  fputs("error reading stdin\n", stderr);
158  if (ferror(stdout))
159  fputs("error writing stdout\n", stderr);
160  break;
161  case Z_STREAM_ERROR:
162  fputs("invalid compression level\n", stderr);
163  break;
164  case Z_DATA_ERROR:
165  fputs("invalid or incomplete deflate data\n", stderr);
166  break;
167  case Z_MEM_ERROR:
168  fputs("out of memory\n", stderr);
169  break;
170  case Z_VERSION_ERROR:
171  fputs("zlib version mismatch!\n", stderr);
172  }
173 }
#define Z_VERSION_ERROR
Definition: zlib.h:185

References Z_DATA_ERROR, Z_ERRNO, Z_MEM_ERROR, Z_STREAM_ERROR, and Z_VERSION_ERROR.

Referenced by main().