145548106Schristos /* compress-debug.c - compress debug sections
2*cb63e24eSchristos Copyright (C) 2010-2024 Free Software Foundation, Inc.
345548106Schristos
445548106Schristos This file is part of GAS, the GNU Assembler.
545548106Schristos
645548106Schristos GAS is free software; you can redistribute it and/or modify
745548106Schristos it under the terms of the GNU General Public License as published by
845548106Schristos the Free Software Foundation; either version 3, or (at your option)
945548106Schristos any later version.
1045548106Schristos
1145548106Schristos GAS is distributed in the hope that it will be useful,
1245548106Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1345548106Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1445548106Schristos GNU General Public License for more details.
1545548106Schristos
1645548106Schristos You should have received a copy of the GNU General Public License
1745548106Schristos along with GAS; see the file COPYING. If not, write to the Free
1845548106Schristos Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
1945548106Schristos 02110-1301, USA. */
2045548106Schristos
2145548106Schristos #include "config.h"
2245548106Schristos #include <stdio.h>
23*cb63e24eSchristos #include <string.h>
249573673dSchristos #include <zlib.h>
25*cb63e24eSchristos #if HAVE_ZSTD
26*cb63e24eSchristos #include <zstd.h>
27*cb63e24eSchristos #endif
2845548106Schristos #include "ansidecl.h"
2945548106Schristos #include "compress-debug.h"
3045548106Schristos
3145548106Schristos /* Initialize the compression engine. */
3245548106Schristos
33*cb63e24eSchristos void *
compress_init(bool use_zstd)34*cb63e24eSchristos compress_init (bool use_zstd)
3545548106Schristos {
36*cb63e24eSchristos if (use_zstd) {
37*cb63e24eSchristos #if HAVE_ZSTD
38*cb63e24eSchristos return ZSTD_createCCtx ();
39*cb63e24eSchristos #endif
40*cb63e24eSchristos }
4145548106Schristos
42*cb63e24eSchristos static struct z_stream_s strm;
43*cb63e24eSchristos memset (&strm, 0, sizeof (strm));
4445548106Schristos deflateInit (&strm, Z_DEFAULT_COMPRESSION);
4545548106Schristos return &strm;
4645548106Schristos }
4745548106Schristos
4845548106Schristos /* Stream the contents of a frag to the compression engine. Output
4945548106Schristos from the engine goes into the current frag on the obstack. */
5045548106Schristos
5145548106Schristos int
compress_data(bool use_zstd,void * ctx,const char ** next_in,int * avail_in,char ** next_out,int * avail_out)52*cb63e24eSchristos compress_data (bool use_zstd, void *ctx, const char **next_in, int *avail_in,
53*cb63e24eSchristos char **next_out, int *avail_out)
5445548106Schristos {
55*cb63e24eSchristos if (use_zstd)
56*cb63e24eSchristos {
57*cb63e24eSchristos #if HAVE_ZSTD
58*cb63e24eSchristos ZSTD_outBuffer ob = { *next_out, *avail_out, 0 };
59*cb63e24eSchristos ZSTD_inBuffer ib = { *next_in, *avail_in, 0 };
60*cb63e24eSchristos size_t ret = ZSTD_compressStream2 (ctx, &ob, &ib, ZSTD_e_continue);
61*cb63e24eSchristos *next_in += ib.pos;
62*cb63e24eSchristos *avail_in -= ib.pos;
63*cb63e24eSchristos *next_out += ob.pos;
64*cb63e24eSchristos *avail_out -= ob.pos;
65*cb63e24eSchristos if (ZSTD_isError (ret))
66*cb63e24eSchristos return -1;
67*cb63e24eSchristos return (int)ob.pos;
68*cb63e24eSchristos #endif
69*cb63e24eSchristos }
70*cb63e24eSchristos
71*cb63e24eSchristos struct z_stream_s *strm = ctx;
7245548106Schristos
7345548106Schristos strm->next_in = (Bytef *) (*next_in);
7445548106Schristos strm->avail_in = *avail_in;
7545548106Schristos strm->next_out = (Bytef *) (*next_out);
7645548106Schristos strm->avail_out = *avail_out;
7745548106Schristos
78*cb63e24eSchristos int x = deflate (strm, Z_NO_FLUSH);
7945548106Schristos if (x != Z_OK)
8045548106Schristos return -1;
8145548106Schristos
82*cb63e24eSchristos int out_size = *avail_out - strm->avail_out;
8345548106Schristos *next_in = (char *) (strm->next_in);
8445548106Schristos *avail_in = strm->avail_in;
8545548106Schristos *next_out = (char *) (strm->next_out);
8645548106Schristos *avail_out = strm->avail_out;
8745548106Schristos
8845548106Schristos return out_size;
8945548106Schristos }
9045548106Schristos
9145548106Schristos /* Finish the compression and consume the remaining compressed output.
9245548106Schristos Returns -1 for error, 0 when done, 1 when more output buffer is
9345548106Schristos needed. */
9445548106Schristos
9545548106Schristos int
compress_finish(bool use_zstd,void * ctx,char ** next_out,int * avail_out,int * out_size)96*cb63e24eSchristos compress_finish (bool use_zstd, void *ctx, char **next_out,
979573673dSchristos int *avail_out, int *out_size)
9845548106Schristos {
99*cb63e24eSchristos if (use_zstd)
100*cb63e24eSchristos {
101*cb63e24eSchristos #if HAVE_ZSTD
102*cb63e24eSchristos ZSTD_outBuffer ob = { *next_out, *avail_out, 0 };
103*cb63e24eSchristos ZSTD_inBuffer ib = { 0, 0, 0 };
104*cb63e24eSchristos size_t ret = ZSTD_compressStream2 (ctx, &ob, &ib, ZSTD_e_end);
105*cb63e24eSchristos *out_size = ob.pos;
106*cb63e24eSchristos *next_out += ob.pos;
107*cb63e24eSchristos *avail_out -= ob.pos;
108*cb63e24eSchristos if (ZSTD_isError (ret))
109*cb63e24eSchristos return -1;
110*cb63e24eSchristos if (ret == 0)
111*cb63e24eSchristos ZSTD_freeCCtx (ctx);
112*cb63e24eSchristos return ret ? 1 : 0;
113*cb63e24eSchristos #endif
114*cb63e24eSchristos }
115*cb63e24eSchristos
11645548106Schristos int x;
117*cb63e24eSchristos struct z_stream_s *strm = ctx;
11845548106Schristos
11945548106Schristos strm->avail_in = 0;
12045548106Schristos strm->next_out = (Bytef *) (*next_out);
12145548106Schristos strm->avail_out = *avail_out;
12245548106Schristos
12345548106Schristos x = deflate (strm, Z_FINISH);
12445548106Schristos
12545548106Schristos *out_size = *avail_out - strm->avail_out;
12645548106Schristos *next_out = (char *) (strm->next_out);
12745548106Schristos *avail_out = strm->avail_out;
12845548106Schristos
12945548106Schristos if (x == Z_STREAM_END)
13045548106Schristos {
13145548106Schristos deflateEnd (strm);
13245548106Schristos return 0;
13345548106Schristos }
13445548106Schristos if (strm->avail_out != 0)
13545548106Schristos return -1;
13645548106Schristos return 1;
13745548106Schristos }
138