1 /* compress-debug.c - compress debug sections
2 Copyright (C) 2010-2023 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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
29 #include "compress-debug.h"
31 /* Initialize the compression engine. */
34 compress_init (bool use_zstd
)
38 return ZSTD_createCCtx ();
42 static struct z_stream_s strm
;
43 memset (&strm
, 0, sizeof (strm
));
44 deflateInit (&strm
, Z_DEFAULT_COMPRESSION
);
48 /* Stream the contents of a frag to the compression engine. Output
49 from the engine goes into the current frag on the obstack. */
52 compress_data (bool use_zstd
, void *ctx
, const char **next_in
, int *avail_in
,
53 char **next_out
, int *avail_out
)
58 ZSTD_outBuffer ob
= { *next_out
, *avail_out
, 0 };
59 ZSTD_inBuffer ib
= { *next_in
, *avail_in
, 0 };
60 size_t ret
= ZSTD_compressStream2 (ctx
, &ob
, &ib
, ZSTD_e_continue
);
65 if (ZSTD_isError (ret
))
71 struct z_stream_s
*strm
= ctx
;
73 strm
->next_in
= (Bytef
*) (*next_in
);
74 strm
->avail_in
= *avail_in
;
75 strm
->next_out
= (Bytef
*) (*next_out
);
76 strm
->avail_out
= *avail_out
;
78 int x
= deflate (strm
, Z_NO_FLUSH
);
82 int out_size
= *avail_out
- strm
->avail_out
;
83 *next_in
= (char *) (strm
->next_in
);
84 *avail_in
= strm
->avail_in
;
85 *next_out
= (char *) (strm
->next_out
);
86 *avail_out
= strm
->avail_out
;
91 /* Finish the compression and consume the remaining compressed output.
92 Returns -1 for error, 0 when done, 1 when more output buffer is
96 compress_finish (bool use_zstd
, void *ctx
, char **next_out
,
97 int *avail_out
, int *out_size
)
102 ZSTD_outBuffer ob
= { *next_out
, *avail_out
, 0 };
103 ZSTD_inBuffer ib
= { 0, 0, 0 };
104 size_t ret
= ZSTD_compressStream2 (ctx
, &ob
, &ib
, ZSTD_e_end
);
107 *avail_out
-= ob
.pos
;
108 if (ZSTD_isError (ret
))
117 struct z_stream_s
*strm
= ctx
;
120 strm
->next_out
= (Bytef
*) (*next_out
);
121 strm
->avail_out
= *avail_out
;
123 x
= deflate (strm
, Z_FINISH
);
125 *out_size
= *avail_out
- strm
->avail_out
;
126 *next_out
= (char *) (strm
->next_out
);
127 *avail_out
= strm
->avail_out
;
129 if (x
== Z_STREAM_END
)
134 if (strm
->avail_out
!= 0)