testzlib.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Main program to compile ssh/zlib.c into a zlib decoding tool.
  3. *
  4. * This is potentially a handy tool in its own right for picking apart
  5. * Zip files or PDFs or PNGs, because it accepts the bare Deflate
  6. * format and the zlib wrapper format, unlike 'zcat' which accepts
  7. * only the gzip wrapper format.
  8. *
  9. * It's also useful as a means for a fuzzer to get reasonably direct
  10. * access to PuTTY's zlib decompressor.
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include "defs.h"
  17. #include "ssh.h"
  18. void out_of_memory(void)
  19. {
  20. fprintf(stderr, "Out of memory!\n");
  21. exit(1);
  22. }
  23. void dputs(const char *buf)
  24. {
  25. fputs(buf, stderr);
  26. }
  27. int main(int argc, char **argv)
  28. {
  29. unsigned char buf[16], *outbuf;
  30. int ret, outlen;
  31. ssh_decompressor *handle;
  32. int noheader = false, opts = true;
  33. char *filename = NULL;
  34. FILE *fp;
  35. while (--argc) {
  36. char *p = *++argv;
  37. if (p[0] == '-' && opts) {
  38. if (!strcmp(p, "-d")) {
  39. noheader = true;
  40. } else if (!strcmp(p, "--")) {
  41. opts = false; /* next thing is filename */
  42. } else if (!strcmp(p, "--help")) {
  43. printf("usage: testzlib decode zlib (RFC1950) data"
  44. " from standard input\n");
  45. printf(" testzlib -d decode Deflate (RFC1951) data"
  46. " from standard input\n");
  47. printf(" testzlib --help display this text\n");
  48. return 0;
  49. } else {
  50. fprintf(stderr, "unknown command line option '%s'\n", p);
  51. return 1;
  52. }
  53. } else if (!filename) {
  54. filename = p;
  55. } else {
  56. fprintf(stderr, "can only handle one filename\n");
  57. return 1;
  58. }
  59. }
  60. handle = ssh_decompressor_new(&ssh_zlib);
  61. if (noheader) {
  62. /*
  63. * Provide missing zlib header if -d was specified.
  64. */
  65. static const unsigned char ersatz_zlib_header[] = { 0x78, 0x9C };
  66. ssh_decompressor_decompress(
  67. handle, ersatz_zlib_header, sizeof(ersatz_zlib_header),
  68. &outbuf, &outlen);
  69. assert(outlen == 0);
  70. }
  71. if (filename)
  72. fp = fopen(filename, "rb");
  73. else
  74. fp = stdin;
  75. if (!fp) {
  76. assert(filename);
  77. fprintf(stderr, "unable to open '%s'\n", filename);
  78. return 1;
  79. }
  80. while (1) {
  81. ret = fread(buf, 1, sizeof(buf), fp);
  82. if (ret <= 0)
  83. break;
  84. ssh_decompressor_decompress(handle, buf, ret, &outbuf, &outlen);
  85. if (outbuf) {
  86. if (outlen)
  87. fwrite(outbuf, 1, outlen, stdout);
  88. sfree(outbuf);
  89. } else {
  90. fprintf(stderr, "decoding error\n");
  91. fclose(fp);
  92. return 1;
  93. }
  94. }
  95. ssh_decompressor_free(handle);
  96. if (filename)
  97. fclose(fp);
  98. return 0;
  99. }