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

Go to the source code of this file.

Macros

#define SET_BINARY_MODE(file)
 
#define local   static
 

Functions

size_t bythirds (size_t size)
 
void * load (const char *name, size_t *len)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ local

#define local   static

Definition at line 31 of file pufftest.c.

◆ SET_BINARY_MODE

#define SET_BINARY_MODE (   file)

Definition at line 28 of file pufftest.c.

Function Documentation

◆ bythirds()

size_t bythirds ( size_t  size)

Definition at line 37 of file pufftest.c.

38 {
39  int n;
40  size_t m;
41 
42  m = size;
43  for (n = 0; m; n++)
44  m >>= 1;
45  if (n < 3)
46  return size + 1;
47  n -= 3;
48  m = size >> n;
49  m += m == 6 ? 2 : 1;
50  m <<= n;
51  return m > size ? m : (size_t)(-1);
52 }
voidpf void uLong size
Definition: ioapi.h:138
int n
Definition: mipsasm.c:19
int size_t
Definition: sftypes.h:40

References regress::m, and n.

Referenced by load().

◆ load()

void* load ( const char *  name,
size_t len 
)

Definition at line 60 of file pufftest.c.

61 {
62  size_t size;
63  void *buf, *swap;
64  FILE *in;
65 
66  *len = 0;
67  buf = malloc(size = 4096);
68  if (buf == NULL)
69  return NULL;
70  in = name == NULL ? stdin : fopen(name, "rb");
71  if (in != NULL) {
72  for (;;) {
73  *len += fread((char *)buf + *len, 1, size - *len, in);
74  if (*len < size) break;
75  size = bythirds(size);
76  if (size == *len || (swap = realloc(buf, size)) == NULL) {
77  free(buf);
78  buf = NULL;
79  break;
80  }
81  buf = swap;
82  }
83  fclose(in);
84  }
85  return buf;
86 }
size_t len
Definition: 6502dis.c:15
const lzma_allocator const uint8_t * in
Definition: block.h:527
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
string FILE
Definition: benchmark.py:21
size_t bythirds(size_t size)
Definition: pufftest.c:37
#define swap(a, b)
Definition: qsort.h:111
Definition: z80asm.h:102

References bythirds(), benchmark::FILE, free(), in, len, malloc(), NULL, realloc(), and swap.

Referenced by avr_il_elpm(), avr_il_lac(), avr_il_las(), avr_il_lat(), avr_il_lpm(), bf_dec(), bf_inc(), ldm(), main(), rz_il_op_new_load(), rz_il_op_pure_dup(), and rz_il_op_pure_free().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 88 of file pufftest.c.

89 {
90  int ret, put = 0, fail = 0;
91  unsigned skip = 0;
92  char *arg, *name = NULL;
93  unsigned char *source = NULL, *dest;
94  size_t len = 0;
95  unsigned long sourcelen, destlen;
96 
97  /* process arguments */
98  while (arg = *++argv, --argc)
99  if (arg[0] == '-') {
100  if (arg[1] == 'w' && arg[2] == 0)
101  put = 1;
102  else if (arg[1] == 'f' && arg[2] == 0)
103  fail = 1, put = 1;
104  else if (arg[1] >= '0' && arg[1] <= '9')
105  skip = (unsigned)atoi(arg + 1);
106  else {
107  fprintf(stderr, "invalid option %s\n", arg);
108  return 3;
109  }
110  }
111  else if (name != NULL) {
112  fprintf(stderr, "only one file name allowed\n");
113  return 3;
114  }
115  else
116  name = arg;
117  source = load(name, &len);
118  if (source == NULL) {
119  fprintf(stderr, "memory allocation failure\n");
120  return 4;
121  }
122  if (len == 0) {
123  fprintf(stderr, "could not read %s, or it was empty\n",
124  name == NULL ? "<stdin>" : name);
125  free(source);
126  return 3;
127  }
128  if (skip >= len) {
129  fprintf(stderr, "skip request of %d leaves no input\n", skip);
130  free(source);
131  return 3;
132  }
133 
134  /* test inflate data with offset skip */
135  len -= skip;
136  sourcelen = (unsigned long)len;
137  ret = puff(NIL, &destlen, source + skip, &sourcelen);
138  if (ret)
139  fprintf(stderr, "puff() failed with return code %d\n", ret);
140  else {
141  fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
142  if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
143  len - sourcelen);
144  }
145 
146  /* if requested, inflate again and write decompressd data to stdout */
147  if (put && ret == 0) {
148  if (fail)
149  destlen >>= 1;
150  dest = malloc(destlen);
151  if (dest == NULL) {
152  fprintf(stderr, "memory allocation failure\n");
153  free(source);
154  return 4;
155  }
156  puff(dest, &destlen, source + skip, &sourcelen);
157  SET_BINARY_MODE(stdout);
158  fwrite(dest, 1, destlen, stdout);
159  free(dest);
160  }
161 
162  /* clean up */
163  free(source);
164  return ret;
165 }
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags long
Definition: sflib.h:79
void skip(file *in, unsigned n)
Definition: gzappend.c:202
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
const char * source
Definition: lz4.h:699
char * dest
Definition: lz4.h:697
int puff(unsigned char *dest, unsigned long *destlen, const unsigned char *source, unsigned long *sourcelen)
Definition: puff.c:793
#define NIL
Definition: puff.h:29
#define SET_BINARY_MODE(file)
Definition: pufftest.c:28
void * load(const char *name, size_t *len)
Definition: pufftest.c:60
#define fail(test)
Definition: tests.h:29

References arg(), argv, dest, fail, free(), len, load(), long, malloc(), NIL, NULL, puff(), SET_BINARY_MODE, skip(), source, and unsigned.