pufftest.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * pufftest.c
  3. * Copyright (C) 2002-2013 Mark Adler
  4. * For conditions of distribution and use, see copyright notice in puff.h
  5. * version 2.3, 21 Jan 2013
  6. */
  7. /* Example of how to use puff().
  8. Usage: puff [-w] [-f] [-nnn] file
  9. ... | puff [-w] [-f] [-nnn]
  10. where file is the input file with deflate data, nnn is the number of bytes
  11. of input to skip before inflating (e.g. to skip a zlib or gzip header), and
  12. -w is used to write the decompressed data to stdout. -f is for coverage
  13. testing, and causes pufftest to fail with not enough output space (-f does
  14. a write like -w, so -w is not required). */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "puff.h"
  18. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  19. # include <fcntl.h>
  20. # include <io.h>
  21. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  22. #else
  23. # define SET_BINARY_MODE(file)
  24. #endif
  25. #define local static
  26. /* Return size times approximately the cube root of 2, keeping the result as 1,
  27. 3, or 5 times a power of 2 -- the result is always > size, until the result
  28. is the maximum value of an unsigned long, where it remains. This is useful
  29. to keep reallocations less than ~33% over the actual data. */
  30. local size_t bythirds(size_t size)
  31. {
  32. int n;
  33. size_t m;
  34. m = size;
  35. for (n = 0; m; n++)
  36. m >>= 1;
  37. if (n < 3)
  38. return size + 1;
  39. n -= 3;
  40. m = size >> n;
  41. m += m == 6 ? 2 : 1;
  42. m <<= n;
  43. return m > size ? m : (size_t)(-1);
  44. }
  45. /* Read the input file *name, or stdin if name is NULL, into allocated memory.
  46. Reallocate to larger buffers until the entire file is read in. Return a
  47. pointer to the allocated data, or NULL if there was a memory allocation
  48. failure. *len is the number of bytes of data read from the input file (even
  49. if load() returns NULL). If the input file was empty or could not be opened
  50. or read, *len is zero. */
  51. local void *load(const char *name, size_t *len)
  52. {
  53. size_t size;
  54. void *buf, *swap;
  55. FILE *in;
  56. *len = 0;
  57. buf = malloc(size = 4096);
  58. if (buf == NULL)
  59. return NULL;
  60. in = name == NULL ? stdin : fopen(name, "rb");
  61. if (in != NULL) {
  62. for (;;) {
  63. *len += fread((char *)buf + *len, 1, size - *len, in);
  64. if (*len < size) break;
  65. size = bythirds(size);
  66. if (size == *len || (swap = realloc(buf, size)) == NULL) {
  67. free(buf);
  68. buf = NULL;
  69. break;
  70. }
  71. buf = swap;
  72. }
  73. fclose(in);
  74. }
  75. return buf;
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. int ret, put = 0, fail = 0;
  80. unsigned skip = 0;
  81. char *arg, *name = NULL;
  82. unsigned char *source = NULL, *dest;
  83. size_t len = 0;
  84. unsigned long sourcelen, destlen;
  85. /* process arguments */
  86. while (arg = *++argv, --argc)
  87. if (arg[0] == '-') {
  88. if (arg[1] == 'w' && arg[2] == 0)
  89. put = 1;
  90. else if (arg[1] == 'f' && arg[2] == 0)
  91. fail = 1, put = 1;
  92. else if (arg[1] >= '0' && arg[1] <= '9')
  93. skip = (unsigned)atoi(arg + 1);
  94. else {
  95. fprintf(stderr, "invalid option %s\n", arg);
  96. return 3;
  97. }
  98. }
  99. else if (name != NULL) {
  100. fprintf(stderr, "only one file name allowed\n");
  101. return 3;
  102. }
  103. else
  104. name = arg;
  105. source = load(name, &len);
  106. if (source == NULL) {
  107. fprintf(stderr, "memory allocation failure\n");
  108. return 4;
  109. }
  110. if (len == 0) {
  111. fprintf(stderr, "could not read %s, or it was empty\n",
  112. name == NULL ? "<stdin>" : name);
  113. free(source);
  114. return 3;
  115. }
  116. if (skip >= len) {
  117. fprintf(stderr, "skip request of %d leaves no input\n", skip);
  118. free(source);
  119. return 3;
  120. }
  121. /* test inflate data with offset skip */
  122. len -= skip;
  123. sourcelen = (unsigned long)len;
  124. ret = puff(NIL, &destlen, source + skip, &sourcelen);
  125. if (ret)
  126. fprintf(stderr, "puff() failed with return code %d\n", ret);
  127. else {
  128. fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
  129. if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
  130. len - sourcelen);
  131. }
  132. /* if requested, inflate again and write decompressd data to stdout */
  133. if (put && ret == 0) {
  134. if (fail)
  135. destlen >>= 1;
  136. dest = malloc(destlen);
  137. if (dest == NULL) {
  138. fprintf(stderr, "memory allocation failure\n");
  139. free(source);
  140. return 4;
  141. }
  142. puff(dest, &destlen, source + skip, &sourcelen);
  143. SET_BINARY_MODE(stdout);
  144. fwrite(dest, 1, destlen, stdout);
  145. free(dest);
  146. }
  147. /* clean up */
  148. free(source);
  149. return ret;
  150. }