1 Commits
v111 ... v109

Author SHA1 Message Date
richgel99@gmail.com
04debb7192 2011-05-17 09:39:27 +00:00
6 changed files with 220 additions and 746 deletions

View File

@@ -1,5 +1,5 @@
// example1.c - Demonstrates miniz.c's compress() and uncompress() functions (same as zlib's). // example1.c - Demonstrates miniz.c's compress() and uncompress() functions (same as zlib's).
// Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c. // Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com
#include "miniz.c" #include "miniz.c"
typedef unsigned char uint8; typedef unsigned char uint8;
@@ -26,13 +26,11 @@ int main(int argc, char *argv[])
uint total_succeeded = 0; uint total_succeeded = 0;
argc, argv; argc, argv;
printf("miniz.c version: %s\n", MZ_VERSION);
do do
{ {
// Allocate buffers to hold compressed and uncompressed data. // Allocate buffers to hold compressed and uncompressed data.
pCmp = (mz_uint8 *)malloc((size_t)cmp_len); pCmp = (mz_uint8 *)malloc(cmp_len);
pUncomp = (mz_uint8 *)malloc((size_t)src_len); pUncomp = (mz_uint8 *)malloc(src_len);
if ((!pCmp) || (!pUncomp)) if ((!pCmp) || (!pUncomp))
{ {
printf("Out of memory!\n"); printf("Out of memory!\n");
@@ -83,7 +81,7 @@ int main(int argc, char *argv[])
printf("Decompressed from %u to %u bytes\n", cmp_len, uncomp_len); printf("Decompressed from %u to %u bytes\n", cmp_len, uncomp_len);
// Ensure uncompress() returned the expected data. // Ensure uncompress() returned the expected data.
if ((uncomp_len != src_len) || (memcmp(pUncomp, s_pStr, (size_t)src_len))) if ((uncomp_len != src_len) || (memcmp(pUncomp, s_pStr, src_len)))
{ {
printf("Decompression failed!\n"); printf("Decompression failed!\n");
free(pCmp); free(pCmp);

View File

@@ -1,5 +1,5 @@
// example2.c - Simple demonstration of miniz.c's ZIP archive API's. // example2.c - Simple demonstration of miniz.c's ZIP archive API's.
// Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c. // Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com
#include "miniz.c" #include "miniz.c"
typedef unsigned char uint8; typedef unsigned char uint8;
@@ -8,13 +8,13 @@ typedef unsigned int uint;
// The string to compress. // The string to compress.
static const char *s_pStr = static const char *s_pStr =
"MISSION CONTROL I wouldn't worry too much about the computer. First of all, there is still a chance that he is right, despite your tests, and" \ "MISSION CONTROL I wouldn't worry too much about the computer. First of all, there is still a chance that he is right, despite your tests, and" \
"if it should happen again, we suggest eliminating this possibility by allowing the unit to remain in place and seeing whether or not it" \ "if it should happen again, we suggest eliminating this possibility by allowing the unit to remain in place and seeing whether or not it" \
"actually fails. If the computer should turn out to be wrong, the situation is still not alarming. The type of obsessional error he may be" \ "actually fails. If the computer should turn out to be wrong, the situation is still not alarming. The type of obsessional error he may be" \
"guilty of is not unknown among the latest generation of HAL 9000 computers. It has almost always revolved around a single detail, such as" \ "guilty of is not unknown among the latest generation of HAL 9000 computers. It has almost always revolved around a single detail, such as" \
"the one you have described, and it has never interfered with the integrity or reliability of the computer's performance in other areas." \ "the one you have described, and it has never interfered with the integrity or reliability of the computer's performance in other areas." \
"No one is certain of the cause of this kind of malfunctioning. It may be over-programming, but it could also be any number of reasons. In any" \ "No one is certain of the cause of this kind of malfunctioning. It may be over-programming, but it could also be any number of reasons. In any" \
"event, it is somewhat analogous to human neurotic behavior. Does this answer your query? Zero-five-three-Zero, MC, transmission concluded."; "event, it is somewhat analogous to human neurotic behavior. Does this answer your query? Zero-five-three-Zero, MC, transmission concluded.";
static const char *s_pComment = "This is a comment"; static const char *s_pComment = "This is a comment";
@@ -26,8 +26,6 @@ int main(int argc, char *argv[])
mz_zip_archive zip_archive; mz_zip_archive zip_archive;
void *p; void *p;
printf("miniz.c version: %s\n", MZ_VERSION);
argc, argv; argc, argv;
// Append a bunch of text files to test.zip // Append a bunch of text files to test.zip

View File

@@ -1,5 +1,5 @@
// example3.c - Demonstrates how to use miniz.c's deflate() and inflate() functions for simple file compression. // example3.c - Demonstrates how to use miniz.c's deflate() and inflate() functions for simple file compression.
// Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c. // Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com
// For simplicity, this example is limited to files smaller than 4GB, but this is not a limitation of miniz.c. // For simplicity, this example is limited to files smaller than 4GB, but this is not a limitation of miniz.c.
#include "miniz.c" #include "miniz.c"
@@ -19,73 +19,20 @@ int main(int argc, char *argv[])
const char *pMode; const char *pMode;
FILE *pInfile, *pOutfile; FILE *pInfile, *pOutfile;
uint infile_size; uint infile_size;
int level = Z_BEST_COMPRESSION;
z_stream stream; z_stream stream;
int n = 1;
const char *pSrc_filename; if (argc != 4)
const char *pDst_filename;
printf("miniz.c version: %s\n", MZ_VERSION);
if (argc < 4)
{ {
printf("Usage: example3 [options] [mode:c or d] infile outfile\n"); printf("Usage: example3 [c/d] infile outfile\n");
printf("\nModes:\n");
printf("c - Compresses file infile to a zlib stream in file outfile\n"); printf("c - Compresses file infile to a zlib stream in file outfile\n");
printf("d - Decompress zlib stream in file infile to file outfile\n"); printf("d - Decompress zlib stream in file infile to file outfile\n");
printf("\nOptions:\n");
printf("-l[0-10] - Compression level, higher values are slower.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
while ((n < argc) && (argv[n][0] == '-')) pMode = argv[1];
{
switch (argv[n][1])
{
case 'l':
{
level = atoi(&argv[1][2]);
if ((level < 0) || (level > 10))
{
printf("Invalid level!\n");
return EXIT_FAILURE;
}
break;
}
default:
{
printf("Invalid option: %s\n", argv[n]);
return EXIT_FAILURE;
}
}
n++;
}
if ((argc - n) < 3)
{
printf("Must specify mode, input filename, and output filename after options!\n");
return EXIT_FAILURE;
}
else if ((argc - n) > 3)
{
printf("Too many filenames!\n");
return EXIT_FAILURE;
}
pMode = argv[n++];
if (!strchr("cCdD", pMode[0]))
{
printf("Invalid mode!\n");
return EXIT_FAILURE;
}
pSrc_filename = argv[n++];
pDst_filename = argv[n++];
printf("Mode: %c, Level: %u\nInput File: \"%s\"\nOutput File: \"%s\"\n", pMode[0], level, pSrc_filename, pDst_filename);
// Open input file. // Open input file.
pInfile = fopen(pSrc_filename, "rb"); pInfile = fopen(argv[2], "rb");
if (!pInfile) if (!pInfile)
{ {
printf("Failed opening input file!\n"); printf("Failed opening input file!\n");
@@ -98,7 +45,7 @@ int main(int argc, char *argv[])
fseek(pInfile, 0, SEEK_SET); fseek(pInfile, 0, SEEK_SET);
// Open output file. // Open output file.
pOutfile = fopen(pDst_filename, "wb"); pOutfile = fopen(argv[3], "wb");
if (!pOutfile) if (!pOutfile)
{ {
printf("Failed opening output file!\n"); printf("Failed opening output file!\n");
@@ -119,7 +66,7 @@ int main(int argc, char *argv[])
// Compression. // Compression.
uint infile_remaining = infile_size; uint infile_remaining = infile_size;
if (deflateInit(&stream, level) != Z_OK) if (deflateInit(&stream, Z_BEST_COMPRESSION) != Z_OK)
{ {
printf("deflateInit() failed!\n"); printf("deflateInit() failed!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -143,7 +90,7 @@ int main(int argc, char *argv[])
stream.avail_in = n; stream.avail_in = n;
infile_remaining -= n; infile_remaining -= n;
//printf("Input bytes remaining: %u\n", infile_remaining); printf("Input bytes remaining: %u\n", infile_remaining);
} }
status = deflate(&stream, infile_remaining ? Z_NO_FLUSH : Z_FINISH); status = deflate(&stream, infile_remaining ? Z_NO_FLUSH : Z_FINISH);

View File

@@ -1,5 +1,5 @@
// example4.c - Uses tinfl.c to decompress a zlib stream in memory to an output file // example4.c - Uses tinfl.c to decompress a zlib stream in memory to an output file
// Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c. // Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com
#include "tinfl.c" #include "tinfl.c"
#include <stdio.h> #include <stdio.h>

833
miniz.c

File diff suppressed because it is too large Load Diff

32
tinfl.c
View File

@@ -1,6 +1,5 @@
/* tinfl.c v1.11 - public domain inflate with zlib header parsing/adler32 checking (inflate-only subset of miniz.c) /* tinfl.c v1.09 - public domain inflate with zlib header parsing/adler32 checking (inflate-only subset of miniz.c)
See "unlicense" statement at the end of this file. Rich Geldreich <richgel99@gmail.com>, last updated May 15, 2011
Rich Geldreich <richgel99@gmail.com>, last updated May 20, 2011
Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt
The entire decompressor coroutine is implemented in tinfl_decompress(). The other functions are optional high-level helpers. The entire decompressor coroutine is implemented in tinfl_decompress(). The other functions are optional high-level helpers.
@@ -555,30 +554,3 @@ int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size,
} }
#endif // #ifndef TINFL_HEADER_FILE_ONLY #endif // #ifndef TINFL_HEADER_FILE_ONLY
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/