content_encoding.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifdef HAVE_LIBZ
  25. #include "urldata.h"
  26. #include <curl/curl.h>
  27. #include <curl/types.h>
  28. #include "sendf.h"
  29. #define DSIZ 4096 /* buffer size for decompressed data */
  30. static CURLcode
  31. process_zlib_error(struct SessionHandle *data, z_stream *z)
  32. {
  33. if (z->msg)
  34. failf (data, "Error while processing content unencoding.\n%s",
  35. z->msg);
  36. else
  37. failf (data, "Error while processing content unencoding.\n"
  38. "Unknown failure within decompression software.");
  39. return CURLE_BAD_CONTENT_ENCODING;
  40. }
  41. static CURLcode
  42. exit_zlib(z_stream *z, bool *zlib_init, CURLcode result)
  43. {
  44. inflateEnd(z);
  45. *zlib_init = 0;
  46. return result;
  47. }
  48. CURLcode
  49. Curl_unencode_deflate_write(struct SessionHandle *data,
  50. struct Curl_transfer_keeper *k,
  51. ssize_t nread)
  52. {
  53. int status; /* zlib status */
  54. int result; /* Curl_client_write status */
  55. char decomp[DSIZ]; /* Put the decompressed data here. */
  56. z_stream *z = &k->z; /* zlib state structure */
  57. /* Initialize zlib? */
  58. if (!k->zlib_init) {
  59. z->zalloc = (alloc_func)Z_NULL;
  60. z->zfree = (free_func)Z_NULL;
  61. z->opaque = 0; /* of dubious use 08/27/02 jhrg */
  62. if (inflateInit(z) != Z_OK)
  63. return process_zlib_error(data, z);
  64. k->zlib_init = 1;
  65. }
  66. /* Set the compressed input when this fucntion is called */
  67. z->next_in = (Bytef *)k->str;
  68. z->avail_in = nread;
  69. /* because the buffer size is fixed, iteratively decompress
  70. and transfer to the client via client_write. */
  71. for (;;) {
  72. /* (re)set buffer for decompressed output for every iteration */
  73. z->next_out = (Bytef *)&decomp[0];
  74. z->avail_out = DSIZ;
  75. status = inflate(z, Z_SYNC_FLUSH);
  76. if (status == Z_OK || status == Z_STREAM_END) {
  77. result = Curl_client_write(data, CLIENTWRITE_BODY, decomp,
  78. DSIZ - z->avail_out);
  79. /* if !CURLE_OK, clean up, return */
  80. if (result) {
  81. return exit_zlib(z, &k->zlib_init, result);
  82. }
  83. /* Done?; clean up, return */
  84. if (status == Z_STREAM_END) {
  85. if (inflateEnd(z) == Z_OK)
  86. return exit_zlib(z, &k->zlib_init, result);
  87. else
  88. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  89. }
  90. /* Done with these bytes, exit */
  91. if (status == Z_OK && z->avail_in == 0 && z->avail_out > 0)
  92. return result;
  93. }
  94. else { /* Error; exit loop, handle below */
  95. return exit_zlib(z, &k->zlib_init, process_zlib_error(data, z));
  96. }
  97. }
  98. }
  99. #endif /* HAVE_LIBZ */
  100. /*
  101. * local variables:
  102. * eval: (load-file "../curl-mode.el")
  103. * end:
  104. * vim600: fdm=marker
  105. * vim: et sw=2 ts=2 sts=2 tw=78
  106. */