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