🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB

  1. // example4.c - Uses tinfl.c to decompress a zlib stream in memory to an output file
  2. // Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c.
  3. #include "miniz.h"
  4. #include <stdio.h>
  5. #include <limits.h>
  6. typedef unsigned char uint8;
  7. typedef unsigned short uint16;
  8. typedef unsigned int uint;
  9. #define my_max(a,b) (((a) > (b)) ? (a) : (b))
  10. #define my_min(a,b) (((a) < (b)) ? (a) : (b))
  11. static int tinfl_put_buf_func(const void* pBuf, int len, void *pUser)
  12. {
  13. return len == (int)fwrite(pBuf, 1, len, (FILE*)pUser);
  14. }
  15. int main(int argc, char *argv[])
  16. {
  17. int status;
  18. FILE *pInfile, *pOutfile;
  19. uint infile_size, outfile_size;
  20. size_t in_buf_size;
  21. uint8 *pCmp_data;
  22. long file_loc;
  23. if (argc != 3)
  24. {
  25. printf("Usage: example4 infile outfile\n");
  26. printf("Decompresses zlib stream in file infile to file outfile.\n");
  27. printf("Input file must be able to fit entirely in memory.\n");
  28. printf("example3 can be used to create compressed zlib streams.\n");
  29. return EXIT_FAILURE;
  30. }
  31. // Open input file.
  32. pInfile = fopen(argv[1], "rb");
  33. if (!pInfile)
  34. {
  35. printf("Failed opening input file!\n");
  36. return EXIT_FAILURE;
  37. }
  38. // Determine input file's size.
  39. fseek(pInfile, 0, SEEK_END);
  40. file_loc = ftell(pInfile);
  41. fseek(pInfile, 0, SEEK_SET);
  42. if ((file_loc < 0) || (file_loc > INT_MAX))
  43. {
  44. // This is not a limitation of miniz or tinfl, but this example.
  45. printf("File is too large to be processed by this example.\n");
  46. return EXIT_FAILURE;
  47. }
  48. infile_size = (uint)file_loc;
  49. pCmp_data = (uint8 *)malloc(infile_size);
  50. if (!pCmp_data)
  51. {
  52. printf("Out of memory!\n");
  53. return EXIT_FAILURE;
  54. }
  55. if (fread(pCmp_data, 1, infile_size, pInfile) != infile_size)
  56. {
  57. printf("Failed reading input file!\n");
  58. return EXIT_FAILURE;
  59. }
  60. // Open output file.
  61. pOutfile = fopen(argv[2], "wb");
  62. if (!pOutfile)
  63. {
  64. printf("Failed opening output file!\n");
  65. return EXIT_FAILURE;
  66. }
  67. printf("Input file size: %u\n", infile_size);
  68. in_buf_size = infile_size;
  69. status = tinfl_decompress_mem_to_callback(pCmp_data, &in_buf_size, tinfl_put_buf_func, pOutfile, TINFL_FLAG_PARSE_ZLIB_HEADER);
  70. if (!status)
  71. {
  72. printf("tinfl_decompress_mem_to_callback() failed with status %i!\n", status);
  73. return EXIT_FAILURE;
  74. }
  75. outfile_size = ftell(pOutfile);
  76. fclose(pInfile);
  77. if (EOF == fclose(pOutfile))
  78. {
  79. printf("Failed writing to output file!\n");
  80. return EXIT_FAILURE;
  81. }
  82. printf("Total input bytes: %u\n", (uint)in_buf_size);
  83. printf("Total output bytes: %u\n", outfile_size);
  84. printf("Success.\n");
  85. return EXIT_SUCCESS;
  86. }