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

Go to the source code of this file.

Macros

#define local   static
 
#define RAWLEN   4096 /* intermediate uncompressed buffer size */
 
#define EXCESS   256 /* empirically determined stream overage */
 
#define MARGIN   8 /* amount to back off for completion */
 

Functions

void quit (char *why)
 
int partcompress (FILE *in, z_streamp def)
 
int recompress (z_streamp inf, z_streamp def)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ EXCESS

#define EXCESS   256 /* empirically determined stream overage */

Definition at line 123 of file fitblk.c.

◆ local

#define local   static

Definition at line 59 of file fitblk.c.

◆ MARGIN

#define MARGIN   8 /* amount to back off for completion */

Definition at line 124 of file fitblk.c.

◆ RAWLEN

#define RAWLEN   4096 /* intermediate uncompressed buffer size */

Definition at line 68 of file fitblk.c.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 127 of file fitblk.c.

128 {
129  int ret; /* return code */
130  unsigned size; /* requested fixed output block size */
131  unsigned have; /* bytes written by deflate() call */
132  unsigned char *blk; /* intermediate and final stream */
133  unsigned char *tmp; /* close to desired size stream */
134  z_stream def, inf; /* zlib deflate and inflate states */
135 
136  /* get requested output size */
137  if (argc != 2)
138  quit("need one argument: size of output block");
139  ret = strtol(argv[1], argv + 1, 10);
140  if (argv[1][0] != 0)
141  quit("argument must be a number");
142  if (ret < 8) /* 8 is minimum zlib stream size */
143  quit("need positive size of 8 or greater");
144  size = (unsigned)ret;
145 
146  /* allocate memory for buffers and compression engine */
147  blk = malloc(size + EXCESS);
148  def.zalloc = Z_NULL;
149  def.zfree = Z_NULL;
150  def.opaque = Z_NULL;
152  if (ret != Z_OK || blk == NULL)
153  quit("out of memory");
154 
155  /* compress from stdin until output full, or no more input */
156  def.avail_out = size + EXCESS;
157  def.next_out = blk;
158  ret = partcompress(stdin, &def);
159  if (ret == Z_ERRNO)
160  quit("error reading input");
161 
162  /* if it all fit, then size was undersubscribed -- done! */
163  if (ret == Z_STREAM_END && def.avail_out >= EXCESS) {
164  /* write block to stdout */
165  have = size + EXCESS - def.avail_out;
166  if (fwrite(blk, 1, have, stdout) != have || ferror(stdout))
167  quit("error writing output");
168 
169  /* clean up and print results to stderr */
170  ret = deflateEnd(&def);
171  assert(ret != Z_STREAM_ERROR);
172  free(blk);
173  fprintf(stderr,
174  "%u bytes unused out of %u requested (all input)\n",
175  size - have, size);
176  return 0;
177  }
178 
179  /* it didn't all fit -- set up for recompression */
180  inf.zalloc = Z_NULL;
181  inf.zfree = Z_NULL;
182  inf.opaque = Z_NULL;
183  inf.avail_in = 0;
184  inf.next_in = Z_NULL;
185  ret = inflateInit(&inf);
186  tmp = malloc(size + EXCESS);
187  if (ret != Z_OK || tmp == NULL)
188  quit("out of memory");
189  ret = deflateReset(&def);
190  assert(ret != Z_STREAM_ERROR);
191 
192  /* do first recompression close to the right amount */
193  inf.avail_in = size + EXCESS;
194  inf.next_in = blk;
195  def.avail_out = size + EXCESS;
196  def.next_out = tmp;
197  ret = recompress(&inf, &def);
198  if (ret == Z_MEM_ERROR)
199  quit("out of memory");
200 
201  /* set up for next reocmpression */
202  ret = inflateReset(&inf);
203  assert(ret != Z_STREAM_ERROR);
204  ret = deflateReset(&def);
205  assert(ret != Z_STREAM_ERROR);
206 
207  /* do second and final recompression (third compression) */
208  inf.avail_in = size - MARGIN; /* assure stream will complete */
209  inf.next_in = tmp;
210  def.avail_out = size;
211  def.next_out = blk;
212  ret = recompress(&inf, &def);
213  if (ret == Z_MEM_ERROR)
214  quit("out of memory");
215  assert(ret == Z_STREAM_END); /* otherwise MARGIN too small */
216 
217  /* done -- write block to stdout */
218  have = size - def.avail_out;
219  if (fwrite(blk, 1, have, stdout) != have || ferror(stdout))
220  quit("error writing output");
221 
222  /* clean up and print results to stderr */
223  free(tmp);
224  ret = inflateEnd(&inf);
225  assert(ret != Z_STREAM_ERROR);
226  ret = deflateEnd(&def);
227  assert(ret != Z_STREAM_ERROR);
228  free(blk);
229  fprintf(stderr,
230  "%u bytes unused out of %u requested (%lu input)\n",
231  size - have, size, def.total_in);
232  return 0;
233 }
#define NULL
Definition: cris-opc.c:27
int ZEXPORT deflateReset(z_streamp strm)
Definition: deflate.c:545
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1119
int partcompress(FILE *in, z_streamp def)
Definition: fitblk.c:73
#define EXCESS
Definition: fitblk.c:123
void quit(char *why)
Definition: fitblk.c:62
#define MARGIN
Definition: fitblk.c:124
int recompress(z_streamp inf, z_streamp def)
Definition: fitblk.c:96
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int ZEXPORT inflateReset(z_streamp strm)
Definition: inflate.c:145
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.c:1301
voidpf void uLong size
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static void struct sockaddr socklen_t static fromlen static backlog static fork char char char static envp int struct rusage static rusage struct utsname static buf struct sembuf unsigned
Definition: sflib.h:97
assert(limit<=UINT32_MAX/2)
#define Z_ERRNO
Definition: zlib.h:180
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_OK
Definition: zlib.h:177
#define Z_STREAM_ERROR
Definition: zlib.h:181
#define Z_NULL
Definition: zlib.h:212
#define inflateInit(strm)
Definition: zlib.h:1812
#define Z_MEM_ERROR
Definition: zlib.h:183
#define deflateInit(strm, level)
Definition: zlib.h:1810
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:193
int inf(FILE *source, FILE *dest)
Definition: zpipe.c:92
int def(FILE *source, FILE *dest, int level)
Definition: zpipe.c:36

References argv, assert(), def(), deflateEnd(), deflateInit, deflateReset(), EXCESS, free(), inf(), inflateEnd(), inflateInit, inflateReset(), malloc(), MARGIN, NULL, partcompress(), quit(), recompress(), autogen_x86imm::tmp, unsigned, Z_DEFAULT_COMPRESSION, Z_ERRNO, Z_MEM_ERROR, Z_NULL, Z_OK, Z_STREAM_END, and Z_STREAM_ERROR.

◆ partcompress()

int partcompress ( FILE *  in,
z_streamp  def 
)

Definition at line 73 of file fitblk.c.

74 {
75  int ret, flush;
76  unsigned char raw[RAWLEN];
77 
78  flush = Z_NO_FLUSH;
79  do {
80  def->avail_in = fread(raw, 1, RAWLEN, in);
81  if (ferror(in))
82  return Z_ERRNO;
83  def->next_in = raw;
84  if (feof(in))
85  flush = Z_FINISH;
86  ret = deflate(def, flush);
87  assert(ret != Z_STREAM_ERROR);
88  } while (def->avail_out != 0 && flush == Z_NO_FLUSH);
89  return ret;
90 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:804
#define RAWLEN
Definition: fitblk.c:68
#define Z_FINISH
Definition: zlib.h:172
#define Z_NO_FLUSH
Definition: zlib.h:168

References assert(), def(), deflate(), in, RAWLEN, Z_ERRNO, Z_FINISH, Z_NO_FLUSH, and Z_STREAM_ERROR.

Referenced by main().

◆ quit()

void quit ( char *  why)

Definition at line 62 of file fitblk.c.

63 {
64  fprintf(stderr, "fitblk abort: %s\n", why);
65  exit(1);
66 }

References test-lz4-list::exit.

Referenced by main().

◆ recompress()

int recompress ( z_streamp  inf,
z_streamp  def 
)

Definition at line 96 of file fitblk.c.

97 {
98  int ret, flush;
99  unsigned char raw[RAWLEN];
100 
101  flush = Z_NO_FLUSH;
102  do {
103  /* decompress */
104  inf->avail_out = RAWLEN;
105  inf->next_out = raw;
106  ret = inflate(inf, Z_NO_FLUSH);
107  assert(ret != Z_STREAM_ERROR && ret != Z_DATA_ERROR &&
108  ret != Z_NEED_DICT);
109  if (ret == Z_MEM_ERROR)
110  return ret;
111 
112  /* compress what was decompresed until done or no room */
113  def->avail_in = RAWLEN - inf->avail_out;
114  def->next_in = raw;
115  if (inf->avail_out != 0)
116  flush = Z_FINISH;
117  ret = deflate(def, flush);
118  assert(ret != Z_STREAM_ERROR);
119  } while (ret != Z_STREAM_END && def->avail_out != 0);
120  return ret;
121 }
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:623
#define Z_NEED_DICT
Definition: zlib.h:179
#define Z_DATA_ERROR
Definition: zlib.h:182

References assert(), def(), deflate(), inf(), inflate(), RAWLEN, Z_DATA_ERROR, Z_FINISH, Z_MEM_ERROR, Z_NEED_DICT, Z_NO_FLUSH, Z_STREAM_END, and Z_STREAM_ERROR.

Referenced by main().