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