Improve contrib/blast to return unused bytes. Fix the test code
to report the count of unused bytes at the end correctly. Add the ability to provide initial input to blast().
This commit is contained in:
parent
89e335abb4
commit
799c87c0d8
@ -1,7 +1,7 @@
|
|||||||
/* blast.c
|
/* blast.c
|
||||||
* Copyright (C) 2003, 2012 Mark Adler
|
* Copyright (C) 2003, 2012, 2013 Mark Adler
|
||||||
* For conditions of distribution and use, see copyright notice in blast.h
|
* For conditions of distribution and use, see copyright notice in blast.h
|
||||||
* version 1.2, 24 Oct 2012
|
* version 1.3, 24 Aug 2013
|
||||||
*
|
*
|
||||||
* blast.c decompresses data compressed by the PKWare Compression Library.
|
* blast.c decompresses data compressed by the PKWare Compression Library.
|
||||||
* This function provides functionality similar to the explode() function of
|
* This function provides functionality similar to the explode() function of
|
||||||
@ -24,8 +24,12 @@
|
|||||||
* 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
|
* 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
|
||||||
* 1.2 24 Oct 2012 - Add note about using binary mode in stdio
|
* 1.2 24 Oct 2012 - Add note about using binary mode in stdio
|
||||||
* - Fix comparisons of differently signed integers
|
* - Fix comparisons of differently signed integers
|
||||||
|
* 1.3 24 Aug 2013 - Return unused input from blast()
|
||||||
|
* - Fix test code to correctly report unused input
|
||||||
|
* - Enable the provision of initial input to blast()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stddef.h> /* for NULL */
|
||||||
#include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
|
#include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
|
||||||
#include "blast.h" /* prototype for blast() */
|
#include "blast.h" /* prototype for blast() */
|
||||||
|
|
||||||
@ -376,7 +380,8 @@ local int decomp(struct state *s)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* See comments in blast.h */
|
/* See comments in blast.h */
|
||||||
int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
|
int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow,
|
||||||
|
unsigned *left, unsigned char **in)
|
||||||
{
|
{
|
||||||
struct state s; /* input/output state */
|
struct state s; /* input/output state */
|
||||||
int err; /* return value */
|
int err; /* return value */
|
||||||
@ -384,7 +389,12 @@ int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
|
|||||||
/* initialize input state */
|
/* initialize input state */
|
||||||
s.infun = infun;
|
s.infun = infun;
|
||||||
s.inhow = inhow;
|
s.inhow = inhow;
|
||||||
s.left = 0;
|
if (left != NULL && *left) {
|
||||||
|
s.left = *left;
|
||||||
|
s.in = *in;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
s.left = 0;
|
||||||
s.bitbuf = 0;
|
s.bitbuf = 0;
|
||||||
s.bitcnt = 0;
|
s.bitcnt = 0;
|
||||||
|
|
||||||
@ -400,6 +410,12 @@ int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
|
|||||||
else
|
else
|
||||||
err = decomp(&s); /* decompress */
|
err = decomp(&s); /* decompress */
|
||||||
|
|
||||||
|
/* return unused input */
|
||||||
|
if (left != NULL)
|
||||||
|
*left = s.left;
|
||||||
|
if (in != NULL)
|
||||||
|
*in = s.left ? s.in : NULL;
|
||||||
|
|
||||||
/* write any leftover output and update the error code if needed */
|
/* write any leftover output and update the error code if needed */
|
||||||
if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
|
if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
|
||||||
err = 1;
|
err = 1;
|
||||||
@ -429,16 +445,20 @@ local int outf(void *how, unsigned char *buf, unsigned len)
|
|||||||
/* Decompress a PKWare Compression Library stream from stdin to stdout */
|
/* Decompress a PKWare Compression Library stream from stdin to stdout */
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int ret, n;
|
int ret;
|
||||||
|
unsigned left;
|
||||||
|
|
||||||
/* decompress to stdout */
|
/* decompress to stdout */
|
||||||
ret = blast(inf, stdin, outf, stdout);
|
left = 0;
|
||||||
if (ret != 0) fprintf(stderr, "blast error: %d\n", ret);
|
ret = blast(inf, stdin, outf, stdout, &left, NULL);
|
||||||
|
if (ret != 0)
|
||||||
|
fprintf(stderr, "blast error: %d\n", ret);
|
||||||
|
|
||||||
/* see if there are any leftover bytes */
|
/* count any leftover bytes */
|
||||||
n = 0;
|
while (getchar() != EOF)
|
||||||
while (getchar() != EOF) n++;
|
left++;
|
||||||
if (n) fprintf(stderr, "blast warning: %d unused bytes of input\n", n);
|
if (left)
|
||||||
|
fprintf(stderr, "blast warning: %u unused bytes of input\n", left);
|
||||||
|
|
||||||
/* return blast() error code */
|
/* return blast() error code */
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/* blast.h -- interface for blast.c
|
/* blast.h -- interface for blast.c
|
||||||
Copyright (C) 2003, 2012 Mark Adler
|
Copyright (C) 2003, 2012, 2013 Mark Adler
|
||||||
version 1.2, 24 Oct 2012
|
version 1.3, 24 Aug 2013
|
||||||
|
|
||||||
This software is provided 'as-is', without any express or implied
|
This software is provided 'as-is', without any express or implied
|
||||||
warranty. In no event will the author be held liable for any damages
|
warranty. In no event will the author be held liable for any damages
|
||||||
@ -42,7 +42,8 @@ typedef int (*blast_out)(void *how, unsigned char *buf, unsigned len);
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow);
|
int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow,
|
||||||
|
unsigned *left, unsigned char **in);
|
||||||
/* Decompress input to output using the provided infun() and outfun() calls.
|
/* Decompress input to output using the provided infun() and outfun() calls.
|
||||||
* On success, the return value of blast() is zero. If there is an error in
|
* On success, the return value of blast() is zero. If there is an error in
|
||||||
* the source data, i.e. it is not in the proper format, then a negative value
|
* the source data, i.e. it is not in the proper format, then a negative value
|
||||||
@ -55,12 +56,19 @@ int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow);
|
|||||||
* an input error. (blast() only asks for input if it needs it.) inhow is for
|
* an input error. (blast() only asks for input if it needs it.) inhow is for
|
||||||
* use by the application to pass an input descriptor to infun(), if desired.
|
* use by the application to pass an input descriptor to infun(), if desired.
|
||||||
*
|
*
|
||||||
|
* If left and in are not NULL and *left is not zero when blast() is called,
|
||||||
|
* then the *left bytes are *in are consumed for input before infun() is used.
|
||||||
|
*
|
||||||
* The output function is invoked: err = outfun(how, buf, len), where the bytes
|
* The output function is invoked: err = outfun(how, buf, len), where the bytes
|
||||||
* to be written are buf[0..len-1]. If err is not zero, then blast() returns
|
* to be written are buf[0..len-1]. If err is not zero, then blast() returns
|
||||||
* with an output error. outfun() is always called with len <= 4096. outhow
|
* with an output error. outfun() is always called with len <= 4096. outhow
|
||||||
* is for use by the application to pass an output descriptor to outfun(), if
|
* is for use by the application to pass an output descriptor to outfun(), if
|
||||||
* desired.
|
* desired.
|
||||||
*
|
*
|
||||||
|
* If there is any unused input, *left is set to the number of bytes that were
|
||||||
|
* read and *in points to them. Otherwise *left is set to zero and *in is set
|
||||||
|
* to NULL. If left or in are NULL, then they are not set.
|
||||||
|
*
|
||||||
* The return codes are:
|
* The return codes are:
|
||||||
*
|
*
|
||||||
* 2: ran out of input before completing decompression
|
* 2: ran out of input before completing decompression
|
||||||
|
Loading…
Reference in New Issue
Block a user