compress-debug.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* compress-debug.c - compress debug sections
  2. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "config.h"
  17. #include <stdio.h>
  18. #include <zlib.h>
  19. #include "ansidecl.h"
  20. #include "compress-debug.h"
  21. /* Initialize the compression engine. */
  22. struct z_stream_s *
  23. compress_init (void)
  24. {
  25. static struct z_stream_s strm;
  26. strm.zalloc = NULL;
  27. strm.zfree = NULL;
  28. strm.opaque = NULL;
  29. deflateInit (&strm, Z_DEFAULT_COMPRESSION);
  30. return &strm;
  31. }
  32. /* Stream the contents of a frag to the compression engine. Output
  33. from the engine goes into the current frag on the obstack. */
  34. int
  35. compress_data (struct z_stream_s *strm, const char **next_in,
  36. int *avail_in, char **next_out, int *avail_out)
  37. {
  38. int out_size = 0;
  39. int x;
  40. strm->next_in = (Bytef *) (*next_in);
  41. strm->avail_in = *avail_in;
  42. strm->next_out = (Bytef *) (*next_out);
  43. strm->avail_out = *avail_out;
  44. x = deflate (strm, Z_NO_FLUSH);
  45. if (x != Z_OK)
  46. return -1;
  47. out_size = *avail_out - strm->avail_out;
  48. *next_in = (char *) (strm->next_in);
  49. *avail_in = strm->avail_in;
  50. *next_out = (char *) (strm->next_out);
  51. *avail_out = strm->avail_out;
  52. return out_size;
  53. }
  54. /* Finish the compression and consume the remaining compressed output.
  55. Returns -1 for error, 0 when done, 1 when more output buffer is
  56. needed. */
  57. int
  58. compress_finish (struct z_stream_s *strm, char **next_out,
  59. int *avail_out, int *out_size)
  60. {
  61. int x;
  62. strm->avail_in = 0;
  63. strm->next_out = (Bytef *) (*next_out);
  64. strm->avail_out = *avail_out;
  65. x = deflate (strm, Z_FINISH);
  66. *out_size = *avail_out - strm->avail_out;
  67. *next_out = (char *) (strm->next_out);
  68. *avail_out = strm->avail_out;
  69. if (x == Z_STREAM_END)
  70. {
  71. deflateEnd (strm);
  72. return 0;
  73. }
  74. if (strm->avail_out != 0)
  75. return -1;
  76. return 1;
  77. }