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