2018-11-27 19:22:15 +00:00
|
|
|
/*
|
|
|
|
* Main program to compile sshzlib.c into a zlib decoding tool.
|
|
|
|
*
|
|
|
|
* This is potentially a handy tool in its own right for picking apart
|
|
|
|
* Zip files or PDFs or PNGs, because it accepts the bare Deflate
|
|
|
|
* format and the zlib wrapper format, unlike 'zcat' which accepts
|
|
|
|
* only the gzip wrapper format.
|
|
|
|
*
|
|
|
|
* It's also useful as a means for a fuzzer to get reasonably direct
|
|
|
|
* access to PuTTY's zlib decompressor.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
|
Move standalone parts of misc.c into utils.c.
misc.c has always contained a combination of things that are tied
tightly into the PuTTY code base (e.g. they use the conf system, or
work with our sockets abstraction) and things that are pure standalone
utility functions like nullstrcmp() which could quite happily be
dropped into any C program without causing a link failure.
Now the latter kind of standalone utility code lives in the new source
file utils.c, whose only external dependency is on memory.c (for snew,
sfree etc), which in turn requires the user to provide an
out_of_memory() function. So it should now be much easier to link test
programs that use PuTTY's low-level functions without also pulling in
half its bulky infrastructure.
In the process, I came across a memory allocation logging system
enabled by -DMALLOC_LOG that looks long since bit-rotted; in any case
we have much more advanced tools for that kind of thing these days,
like valgrind and Leak Sanitiser, so I've just removed it rather than
trying to transplant it somewhere sensible. (We can always pull it
back out of the version control history if really necessary, but I
haven't used it in at least a decade.)
The other slightly silly thing I did was to give bufchain a function
pointer field that points to queue_idempotent_callback(), and disallow
direct setting of the 'ic' field in favour of calling
bufchain_set_callback which will fill that pointer in too. That allows
the bufchain system to live in utils.c rather than misc.c, so that
programs can use it without also having to link in the callback system
or provide an annoying stub of that function. In fact that's just
allowed me to remove stubs of that kind from PuTTYgen and Pageant!
2019-01-03 08:44:11 +00:00
|
|
|
void out_of_memory(void)
|
2018-11-27 19:22:15 +00:00
|
|
|
{
|
Move standalone parts of misc.c into utils.c.
misc.c has always contained a combination of things that are tied
tightly into the PuTTY code base (e.g. they use the conf system, or
work with our sockets abstraction) and things that are pure standalone
utility functions like nullstrcmp() which could quite happily be
dropped into any C program without causing a link failure.
Now the latter kind of standalone utility code lives in the new source
file utils.c, whose only external dependency is on memory.c (for snew,
sfree etc), which in turn requires the user to provide an
out_of_memory() function. So it should now be much easier to link test
programs that use PuTTY's low-level functions without also pulling in
half its bulky infrastructure.
In the process, I came across a memory allocation logging system
enabled by -DMALLOC_LOG that looks long since bit-rotted; in any case
we have much more advanced tools for that kind of thing these days,
like valgrind and Leak Sanitiser, so I've just removed it rather than
trying to transplant it somewhere sensible. (We can always pull it
back out of the version control history if really necessary, but I
haven't used it in at least a decade.)
The other slightly silly thing I did was to give bufchain a function
pointer field that points to queue_idempotent_callback(), and disallow
direct setting of the 'ic' field in favour of calling
bufchain_set_callback which will fill that pointer in too. That allows
the bufchain system to live in utils.c rather than misc.c, so that
programs can use it without also having to link in the callback system
or provide an annoying stub of that function. In fact that's just
allowed me to remove stubs of that kind from PuTTYgen and Pageant!
2019-01-03 08:44:11 +00:00
|
|
|
fprintf(stderr, "Out of memory!\n");
|
2018-11-27 19:22:15 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned char buf[16], *outbuf;
|
|
|
|
int ret, outlen;
|
|
|
|
ssh_decompressor *handle;
|
|
|
|
int noheader = false, opts = true;
|
|
|
|
char *filename = NULL;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
while (--argc) {
|
|
|
|
char *p = *++argv;
|
|
|
|
|
|
|
|
if (p[0] == '-' && opts) {
|
|
|
|
if (!strcmp(p, "-d")) {
|
|
|
|
noheader = true;
|
|
|
|
} else if (!strcmp(p, "--")) {
|
|
|
|
opts = false; /* next thing is filename */
|
|
|
|
} else if (!strcmp(p, "--help")) {
|
|
|
|
printf("usage: testzlib decode zlib (RFC1950) data"
|
|
|
|
" from standard input\n");
|
|
|
|
printf(" testzlib -d decode Deflate (RFC1951) data"
|
|
|
|
" from standard input\n");
|
|
|
|
printf(" testzlib --help display this text\n");
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "unknown command line option '%s'\n", p);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (!filename) {
|
|
|
|
filename = p;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "can only handle one filename\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = ssh_decompressor_new(&ssh_zlib);
|
|
|
|
|
|
|
|
if (noheader) {
|
|
|
|
/*
|
|
|
|
* Provide missing zlib header if -d was specified.
|
|
|
|
*/
|
|
|
|
static const unsigned char ersatz_zlib_header[] = { 0x78, 0x9C };
|
|
|
|
ssh_decompressor_decompress(
|
|
|
|
handle, ersatz_zlib_header, sizeof(ersatz_zlib_header),
|
|
|
|
&outbuf, &outlen);
|
|
|
|
assert(outlen == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
else
|
|
|
|
fp = stdin;
|
|
|
|
|
|
|
|
if (!fp) {
|
|
|
|
assert(filename);
|
|
|
|
fprintf(stderr, "unable to open '%s'\n", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2019-09-08 19:29:00 +00:00
|
|
|
ret = fread(buf, 1, sizeof(buf), fp);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
ssh_decompressor_decompress(handle, buf, ret, &outbuf, &outlen);
|
2018-11-27 19:22:15 +00:00
|
|
|
if (outbuf) {
|
|
|
|
if (outlen)
|
|
|
|
fwrite(outbuf, 1, outlen, stdout);
|
|
|
|
sfree(outbuf);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "decoding error\n");
|
|
|
|
fclose(fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_decompressor_free(handle);
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|