Update year range in copyright notice of binutils files
[binutils-gdb.git] / gas / compress-debug.c
1 /* compress-debug.c - compress debug sections
2 Copyright (C) 2010-2023 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 #if HAVE_ZSTD
25 #include <zstd.h>
26 #endif
27 #include "ansidecl.h"
28 #include "compress-debug.h"
29
30 /* Initialize the compression engine. */
31
32 void *
33 compress_init (bool use_zstd)
34 {
35 if (use_zstd) {
36 #if HAVE_ZSTD
37 return ZSTD_createCCtx ();
38 #endif
39 }
40
41 static struct z_stream_s strm;
42
43 strm.zalloc = NULL;
44 strm.zfree = NULL;
45 strm.opaque = NULL;
46 deflateInit (&strm, Z_DEFAULT_COMPRESSION);
47 return &strm;
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 (bool use_zstd, void *ctx, const char **next_in, int *avail_in,
55 char **next_out, int *avail_out)
56 {
57 if (use_zstd)
58 {
59 #if HAVE_ZSTD
60 ZSTD_outBuffer ob = { *next_out, *avail_out, 0 };
61 ZSTD_inBuffer ib = { *next_in, *avail_in, 0 };
62 size_t ret = ZSTD_compressStream2 (ctx, &ob, &ib, ZSTD_e_continue);
63 *next_in += ib.pos;
64 *avail_in -= ib.pos;
65 *next_out += ob.pos;
66 *avail_out -= ob.pos;
67 if (ZSTD_isError (ret))
68 return -1;
69 return (int)ob.pos;
70 #endif
71 }
72
73 struct z_stream_s *strm = ctx;
74
75 strm->next_in = (Bytef *) (*next_in);
76 strm->avail_in = *avail_in;
77 strm->next_out = (Bytef *) (*next_out);
78 strm->avail_out = *avail_out;
79
80 int x = deflate (strm, Z_NO_FLUSH);
81 if (x != Z_OK)
82 return -1;
83
84 int out_size = *avail_out - strm->avail_out;
85 *next_in = (char *) (strm->next_in);
86 *avail_in = strm->avail_in;
87 *next_out = (char *) (strm->next_out);
88 *avail_out = strm->avail_out;
89
90 return out_size;
91 }
92
93 /* Finish the compression and consume the remaining compressed output.
94 Returns -1 for error, 0 when done, 1 when more output buffer is
95 needed. */
96
97 int
98 compress_finish (bool use_zstd, void *ctx, char **next_out,
99 int *avail_out, int *out_size)
100 {
101 if (use_zstd)
102 {
103 #if HAVE_ZSTD
104 ZSTD_outBuffer ob = { *next_out, *avail_out, 0 };
105 ZSTD_inBuffer ib = { 0 };
106 size_t ret = ZSTD_compressStream2 (ctx, &ob, &ib, ZSTD_e_end);
107 *out_size = ob.pos;
108 *next_out += ob.pos;
109 *avail_out -= ob.pos;
110 if (ZSTD_isError (ret))
111 return -1;
112 if (ret == 0)
113 ZSTD_freeCCtx (ctx);
114 return ret ? 1 : 0;
115 #endif
116 }
117
118 int x;
119 struct z_stream_s *strm = ctx;
120
121 strm->avail_in = 0;
122 strm->next_out = (Bytef *) (*next_out);
123 strm->avail_out = *avail_out;
124
125 x = deflate (strm, Z_FINISH);
126
127 *out_size = *avail_out - strm->avail_out;
128 *next_out = (char *) (strm->next_out);
129 *avail_out = strm->avail_out;
130
131 if (x == Z_STREAM_END)
132 {
133 deflateEnd (strm);
134 return 0;
135 }
136 if (strm->avail_out != 0)
137 return -1;
138 return 1;
139 }