2010-09-17 Tejas Belagod <tejas.belagod@arm.com>
[external/binutils.git] / gas / compress-debug.c
1 /* compress-debug.c - compress debug sections
2    Copyright 2010 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to the Free
18    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19    02110-1301, USA.  */
20
21 #include <stdio.h>
22
23 #include "ansidecl.h"
24
25 #include "config.h"
26 #include "compress-debug.h"
27
28 #ifdef HAVE_ZLIB_H
29 #include <zlib.h>
30 #endif
31
32 /* Initialize the compression engine.  */
33
34 struct z_stream_s *
35 compress_init (void)
36 {
37 #ifndef HAVE_ZLIB_H
38   return NULL;
39 #else
40   static struct z_stream_s strm;
41
42   strm.zalloc = NULL;
43   strm.zfree = NULL;
44   strm.opaque = NULL;
45   deflateInit (&strm, Z_DEFAULT_COMPRESSION);
46   return &strm;
47 #endif /* HAVE_ZLIB_H */
48 }
49
50 /* Stream the contents of a frag to the compression engine.  Output
51    from the engine goes into the current frag on the obstack.  */
52
53 int
54 compress_data (struct z_stream_s *strm ATTRIBUTE_UNUSED,
55                const char **next_in ATTRIBUTE_UNUSED,
56                int *avail_in ATTRIBUTE_UNUSED,
57                char **next_out ATTRIBUTE_UNUSED,
58                int *avail_out ATTRIBUTE_UNUSED)
59 {
60 #ifndef HAVE_ZLIB_H
61   return -1;
62 #else
63   int out_size = 0;
64   int x;
65
66   strm->next_in = (Bytef *) (*next_in);
67   strm->avail_in = *avail_in;
68   strm->next_out = (Bytef *) (*next_out);
69   strm->avail_out = *avail_out;
70
71   x = deflate (strm, Z_NO_FLUSH);
72   if (x != Z_OK)
73     return -1;
74
75   out_size = *avail_out - strm->avail_out;
76   *next_in = (char *) (strm->next_in);
77   *avail_in = strm->avail_in;
78   *next_out = (char *) (strm->next_out);
79   *avail_out = strm->avail_out;
80
81   return out_size;
82 #endif /* HAVE_ZLIB_H */
83 }
84
85 /* Finish the compression and consume the remaining compressed output.
86    Returns -1 for error, 0 when done, 1 when more output buffer is
87    needed.  */
88
89 int
90 compress_finish (struct z_stream_s *strm ATTRIBUTE_UNUSED,
91                  char **next_out ATTRIBUTE_UNUSED,
92                  int *avail_out ATTRIBUTE_UNUSED,
93                  int *out_size ATTRIBUTE_UNUSED)
94 {
95 #ifndef HAVE_ZLIB_H
96   return -1;
97 #else
98   int x;
99
100   strm->avail_in = 0;
101   strm->next_out = (Bytef *) (*next_out);
102   strm->avail_out = *avail_out;
103
104   x = deflate (strm, Z_FINISH);
105
106   *out_size = *avail_out - strm->avail_out;
107   *next_out = (char *) (strm->next_out);
108   *avail_out = strm->avail_out;
109
110   if (x == Z_STREAM_END)
111     {
112       deflateEnd (strm);
113       return 0;
114     }
115   if (strm->avail_out != 0)
116     return -1;
117   return 1;
118 #endif /* HAVE_ZLIB_H */
119 }