1 /* compress-debug.c - compress debug sections
2    Copyright (C) 2010-2016 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 "config.h"
22 #include <stdio.h>
23 #include <zlib.h>
24 #include "ansidecl.h"
25 #include "compress-debug.h"
26 
27 /* Initialize the compression engine.  */
28 
29 struct z_stream_s *
compress_init(void)30 compress_init (void)
31 {
32   static struct z_stream_s strm;
33 
34   strm.zalloc = NULL;
35   strm.zfree = NULL;
36   strm.opaque = NULL;
37   deflateInit (&strm, Z_DEFAULT_COMPRESSION);
38   return &strm;
39 }
40 
41 /* Stream the contents of a frag to the compression engine.  Output
42    from the engine goes into the current frag on the obstack.  */
43 
44 int
compress_data(struct z_stream_s * strm,const char ** next_in,int * avail_in,char ** next_out,int * avail_out)45 compress_data (struct z_stream_s *strm, const char **next_in,
46 	       int *avail_in, char **next_out, int *avail_out)
47 {
48   int out_size = 0;
49   int x;
50 
51   strm->next_in = (Bytef *) (*next_in);
52   strm->avail_in = *avail_in;
53   strm->next_out = (Bytef *) (*next_out);
54   strm->avail_out = *avail_out;
55 
56   x = deflate (strm, Z_NO_FLUSH);
57   if (x != Z_OK)
58     return -1;
59 
60   out_size = *avail_out - strm->avail_out;
61   *next_in = (char *) (strm->next_in);
62   *avail_in = strm->avail_in;
63   *next_out = (char *) (strm->next_out);
64   *avail_out = strm->avail_out;
65 
66   return out_size;
67 }
68 
69 /* Finish the compression and consume the remaining compressed output.
70    Returns -1 for error, 0 when done, 1 when more output buffer is
71    needed.  */
72 
73 int
compress_finish(struct z_stream_s * strm,char ** next_out,int * avail_out,int * out_size)74 compress_finish (struct z_stream_s *strm, char **next_out,
75 		 int *avail_out, int *out_size)
76 {
77   int x;
78 
79   strm->avail_in = 0;
80   strm->next_out = (Bytef *) (*next_out);
81   strm->avail_out = *avail_out;
82 
83   x = deflate (strm, Z_FINISH);
84 
85   *out_size = *avail_out - strm->avail_out;
86   *next_out = (char *) (strm->next_out);
87   *avail_out = strm->avail_out;
88 
89   if (x == Z_STREAM_END)
90     {
91       deflateEnd (strm);
92       return 0;
93     }
94   if (strm->avail_out != 0)
95     return -1;
96   return 1;
97 }
98